blob: ef97339f3f67fd64b3a9e2e81f7408502f64d6e2 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeuf935893a2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williamseb99bd62018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Paolo Bonzinia175d512018-02-22 16:43:17 +010053#include <asm/microcode.h>
Thomas Gleixner7a2d2352018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020057#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058
Avi Kivity4ecac3f2008-05-13 13:23:38 +030059#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040060#define __ex_clear(x, reg) \
61 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Josh Triplette9bda3b2012-03-20 23:33:51 -070066static const struct x86_cpu_id vmx_cpu_id[] = {
67 X86_FEATURE_MATCH(X86_FEATURE_VMX),
68 {}
69};
70MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
71
Rusty Russell476bc002012-01-13 09:32:18 +103072static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020073module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080074
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020077
Rusty Russell476bc002012-01-13 09:32:18 +103078static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020079module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070082module_param_named(unrestricted_guest,
83 enable_unrestricted_guest, bool, S_IRUGO);
84
Xudong Hao83c3a332012-05-28 19:33:35 +080085static bool __read_mostly enable_ept_ad_bits = 1;
86module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
87
Avi Kivitya27685c2012-06-12 20:30:18 +030088static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020089module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030090
Rusty Russell476bc002012-01-13 09:32:18 +103091static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080092module_param(vmm_exclusive, bool, S_IRUGO);
93
Rusty Russell476bc002012-01-13 09:32:18 +103094static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030095module_param(fasteoi, bool, S_IRUGO);
96
Yang Zhang5a717852013-04-11 19:25:16 +080097static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080098module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080099
Abel Gordonabc4fc52013-04-18 14:35:25 +0300100static bool __read_mostly enable_shadow_vmcs = 1;
101module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300102/*
103 * If nested=1, nested virtualization is supported, i.e., guests may use
104 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
105 * use VMX instructions.
106 */
Rusty Russell476bc002012-01-13 09:32:18 +1030107static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300108module_param(nested, bool, S_IRUGO);
109
Wanpeng Li20300092014-12-02 19:14:59 +0800110static u64 __read_mostly host_xss;
111
Kai Huang843e4332015-01-28 10:54:28 +0800112static bool __read_mostly enable_pml = 1;
113module_param_named(pml, enable_pml, bool, S_IRUGO);
114
Paolo Bonzini6236b782018-01-16 16:51:18 +0100115#define MSR_TYPE_R 1
116#define MSR_TYPE_W 2
117#define MSR_TYPE_RW 3
118
119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
121#define MSR_BITMAP_MODE_LM 4
122
Haozhong Zhang64903d62015-10-20 15:39:09 +0800123#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
124
Yunhong Jiang64672c92016-06-13 14:19:59 -0700125/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
126static int __read_mostly cpu_preemption_timer_multi;
127static bool __read_mostly enable_preemption_timer = 1;
128#ifdef CONFIG_X86_64
129module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
130#endif
131
Gleb Natapov50378782013-02-04 16:00:28 +0200132#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
133#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200134#define KVM_VM_CR0_ALWAYS_ON \
135 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136#define KVM_CR4_GUEST_OWNED_BITS \
137 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700138 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200139
Avi Kivitycdc0e242009-12-06 17:21:14 +0200140#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
141#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
142
Avi Kivity78ac8b42010-04-08 18:19:35 +0300143#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
144
Jan Kiszkaf4124502014-03-07 20:03:13 +0100145#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
146
Jan Dakinevich04c776e2018-02-23 11:42:18 +0100147#define VMX_VPID_EXTENT_SUPPORTED_MASK \
148 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
149 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
150 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
151 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153/*
154 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
155 * ple_gap: upper bound on the amount of time between two successive
156 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500157 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800158 * ple_window: upper bound on the amount of time a guest is allowed to execute
159 * in a PAUSE loop. Tests indicate that most spinlocks are held for
160 * less than 2^12 cycles
161 * Time is measured based on a counter that runs at the same rate as the TSC,
162 * refer SDM volume 3b section 21.6.13 & 22.1.3.
163 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200164#define KVM_VMX_DEFAULT_PLE_GAP 128
165#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
166#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
167#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
168#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
169 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
170
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800171static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
172module_param(ple_gap, int, S_IRUGO);
173
174static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, int, S_IRUGO);
176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
178static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
179module_param(ple_window_grow, int, S_IRUGO);
180
181/* Default resets per-vcpu window every exit to ple_window. */
182static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
183module_param(ple_window_shrink, int, S_IRUGO);
184
185/* Default is to compute the maximum so we can never overflow. */
186static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, int, S_IRUGO);
189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange936f5662018-07-21 22:25:00 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200194static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200195
Thomas Gleixner641a2112018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
Paolo Bonzini87294122018-08-22 16:43:39 +0200201 bool for_parse;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200202} vmentry_l1d_param[] = {
Paolo Bonzini87294122018-08-22 16:43:39 +0200203 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
204 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
205 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
206 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
207 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
208 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200209};
210
Thomas Gleixner641a2112018-07-13 16:23:19 +0200211#define L1D_CACHE_ORDER 4
212static void *vmx_l1d_flush_pages;
213
214static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
215{
216 struct page *page;
Nicolai Stange587d4992018-07-18 19:07:38 +0200217 unsigned int i;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200218
Thomas Gleixner641a2112018-07-13 16:23:19 +0200219 if (!enable_ept) {
220 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
221 return 0;
222 }
223
Paolo Bonzini383f1602018-08-05 16:07:46 +0200224 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
225 u64 msr;
226
227 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
228 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
229 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
230 return 0;
231 }
232 }
233
Jiri Kosina2decbf52018-07-13 16:23:25 +0200234 /* If set to auto use the default l1tf mitigation method */
235 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
236 switch (l1tf_mitigation) {
237 case L1TF_MITIGATION_OFF:
238 l1tf = VMENTER_L1D_FLUSH_NEVER;
239 break;
240 case L1TF_MITIGATION_FLUSH_NOWARN:
241 case L1TF_MITIGATION_FLUSH:
242 case L1TF_MITIGATION_FLUSH_NOSMT:
243 l1tf = VMENTER_L1D_FLUSH_COND;
244 break;
245 case L1TF_MITIGATION_FULL:
246 case L1TF_MITIGATION_FULL_FORCE:
247 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
248 break;
249 }
250 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
251 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
252 }
253
Thomas Gleixner641a2112018-07-13 16:23:19 +0200254 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
255 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
256 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
257 if (!page)
258 return -ENOMEM;
259 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange587d4992018-07-18 19:07:38 +0200260
261 /*
262 * Initialize each page with a different pattern in
263 * order to protect against KSM in the nested
264 * virtualization case.
265 */
266 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
267 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
268 PAGE_SIZE);
269 }
Thomas Gleixner641a2112018-07-13 16:23:19 +0200270 }
271
272 l1tf_vmx_mitigation = l1tf;
273
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200274 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
275 static_branch_enable(&vmx_l1d_should_flush);
276 else
277 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixnerdff09822018-07-13 16:23:20 +0200278
Nicolai Stange936f5662018-07-21 22:25:00 +0200279 if (l1tf == VMENTER_L1D_FLUSH_COND)
280 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200281 else
Nicolai Stange936f5662018-07-21 22:25:00 +0200282 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner641a2112018-07-13 16:23:19 +0200283 return 0;
284}
285
286static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200287{
288 unsigned int i;
289
Thomas Gleixner641a2112018-07-13 16:23:19 +0200290 if (s) {
291 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini87294122018-08-22 16:43:39 +0200292 if (vmentry_l1d_param[i].for_parse &&
293 sysfs_streq(s, vmentry_l1d_param[i].option))
294 return i;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200295 }
296 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200297 return -EINVAL;
298}
299
Thomas Gleixner641a2112018-07-13 16:23:19 +0200300static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
301{
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200302 int l1tf, ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200303
Thomas Gleixner641a2112018-07-13 16:23:19 +0200304 l1tf = vmentry_l1d_flush_parse(s);
305 if (l1tf < 0)
306 return l1tf;
307
Paolo Bonzini87294122018-08-22 16:43:39 +0200308 if (!boot_cpu_has(X86_BUG_L1TF))
309 return 0;
310
Thomas Gleixner641a2112018-07-13 16:23:19 +0200311 /*
312 * Has vmx_init() run already? If not then this is the pre init
313 * parameter parsing. In that case just store the value and let
314 * vmx_init() do the proper setup after enable_ept has been
315 * established.
316 */
317 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
318 vmentry_l1d_flush_param = l1tf;
319 return 0;
320 }
321
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200322 mutex_lock(&vmx_l1d_flush_mutex);
323 ret = vmx_setup_l1d_flush(l1tf);
324 mutex_unlock(&vmx_l1d_flush_mutex);
325 return ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200326}
327
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200328static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
329{
Paolo Bonzini87294122018-08-22 16:43:39 +0200330 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
331 return sprintf(s, "???\n");
332
Thomas Gleixner641a2112018-07-13 16:23:19 +0200333 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200334}
335
336static const struct kernel_param_ops vmentry_l1d_flush_ops = {
337 .set = vmentry_l1d_flush_set,
338 .get = vmentry_l1d_flush_get,
339};
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200340module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200341
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200342#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300343
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400344struct vmcs {
345 u32 revision_id;
346 u32 abort;
347 char data[0];
348};
349
Nadav Har'Eld462b812011-05-24 15:26:10 +0300350/*
351 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
352 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
353 * loaded on this CPU (so we can clear them if the CPU goes down).
354 */
355struct loaded_vmcs {
356 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700357 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300358 int cpu;
359 int launched;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100360 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300361 struct list_head loaded_vmcss_on_cpu_link;
362};
363
Avi Kivity26bb0982009-09-07 11:14:12 +0300364struct shared_msr_entry {
365 unsigned index;
366 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200367 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300368};
369
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300370/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300371 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
372 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
373 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
374 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
375 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
376 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson46e24df2017-11-27 17:22:25 -0600377 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300378 * underlying hardware which will be used to run L2.
379 * This structure is packed to ensure that its layout is identical across
380 * machines (necessary for live migration).
381 * If there are changes in this struct, VMCS12_REVISION must be changed.
382 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300383typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300384struct __packed vmcs12 {
385 /* According to the Intel spec, a VMCS region must start with the
386 * following two fields. Then follow implementation-specific data.
387 */
388 u32 revision_id;
389 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300390
Nadav Har'El27d6c862011-05-25 23:06:59 +0300391 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
392 u32 padding[7]; /* room for future expansion */
393
Nadav Har'El22bd0352011-05-25 23:05:57 +0300394 u64 io_bitmap_a;
395 u64 io_bitmap_b;
396 u64 msr_bitmap;
397 u64 vm_exit_msr_store_addr;
398 u64 vm_exit_msr_load_addr;
399 u64 vm_entry_msr_load_addr;
400 u64 tsc_offset;
401 u64 virtual_apic_page_addr;
402 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800403 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300404 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800405 u64 eoi_exit_bitmap0;
406 u64 eoi_exit_bitmap1;
407 u64 eoi_exit_bitmap2;
408 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800409 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300410 u64 guest_physical_address;
411 u64 vmcs_link_pointer;
412 u64 guest_ia32_debugctl;
413 u64 guest_ia32_pat;
414 u64 guest_ia32_efer;
415 u64 guest_ia32_perf_global_ctrl;
416 u64 guest_pdptr0;
417 u64 guest_pdptr1;
418 u64 guest_pdptr2;
419 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100420 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300421 u64 host_ia32_pat;
422 u64 host_ia32_efer;
423 u64 host_ia32_perf_global_ctrl;
424 u64 padding64[8]; /* room for future expansion */
425 /*
426 * To allow migration of L1 (complete with its L2 guests) between
427 * machines of different natural widths (32 or 64 bit), we cannot have
428 * unsigned long fields with no explict size. We use u64 (aliased
429 * natural_width) instead. Luckily, x86 is little-endian.
430 */
431 natural_width cr0_guest_host_mask;
432 natural_width cr4_guest_host_mask;
433 natural_width cr0_read_shadow;
434 natural_width cr4_read_shadow;
435 natural_width cr3_target_value0;
436 natural_width cr3_target_value1;
437 natural_width cr3_target_value2;
438 natural_width cr3_target_value3;
439 natural_width exit_qualification;
440 natural_width guest_linear_address;
441 natural_width guest_cr0;
442 natural_width guest_cr3;
443 natural_width guest_cr4;
444 natural_width guest_es_base;
445 natural_width guest_cs_base;
446 natural_width guest_ss_base;
447 natural_width guest_ds_base;
448 natural_width guest_fs_base;
449 natural_width guest_gs_base;
450 natural_width guest_ldtr_base;
451 natural_width guest_tr_base;
452 natural_width guest_gdtr_base;
453 natural_width guest_idtr_base;
454 natural_width guest_dr7;
455 natural_width guest_rsp;
456 natural_width guest_rip;
457 natural_width guest_rflags;
458 natural_width guest_pending_dbg_exceptions;
459 natural_width guest_sysenter_esp;
460 natural_width guest_sysenter_eip;
461 natural_width host_cr0;
462 natural_width host_cr3;
463 natural_width host_cr4;
464 natural_width host_fs_base;
465 natural_width host_gs_base;
466 natural_width host_tr_base;
467 natural_width host_gdtr_base;
468 natural_width host_idtr_base;
469 natural_width host_ia32_sysenter_esp;
470 natural_width host_ia32_sysenter_eip;
471 natural_width host_rsp;
472 natural_width host_rip;
473 natural_width paddingl[8]; /* room for future expansion */
474 u32 pin_based_vm_exec_control;
475 u32 cpu_based_vm_exec_control;
476 u32 exception_bitmap;
477 u32 page_fault_error_code_mask;
478 u32 page_fault_error_code_match;
479 u32 cr3_target_count;
480 u32 vm_exit_controls;
481 u32 vm_exit_msr_store_count;
482 u32 vm_exit_msr_load_count;
483 u32 vm_entry_controls;
484 u32 vm_entry_msr_load_count;
485 u32 vm_entry_intr_info_field;
486 u32 vm_entry_exception_error_code;
487 u32 vm_entry_instruction_len;
488 u32 tpr_threshold;
489 u32 secondary_vm_exec_control;
490 u32 vm_instruction_error;
491 u32 vm_exit_reason;
492 u32 vm_exit_intr_info;
493 u32 vm_exit_intr_error_code;
494 u32 idt_vectoring_info_field;
495 u32 idt_vectoring_error_code;
496 u32 vm_exit_instruction_len;
497 u32 vmx_instruction_info;
498 u32 guest_es_limit;
499 u32 guest_cs_limit;
500 u32 guest_ss_limit;
501 u32 guest_ds_limit;
502 u32 guest_fs_limit;
503 u32 guest_gs_limit;
504 u32 guest_ldtr_limit;
505 u32 guest_tr_limit;
506 u32 guest_gdtr_limit;
507 u32 guest_idtr_limit;
508 u32 guest_es_ar_bytes;
509 u32 guest_cs_ar_bytes;
510 u32 guest_ss_ar_bytes;
511 u32 guest_ds_ar_bytes;
512 u32 guest_fs_ar_bytes;
513 u32 guest_gs_ar_bytes;
514 u32 guest_ldtr_ar_bytes;
515 u32 guest_tr_ar_bytes;
516 u32 guest_interruptibility_info;
517 u32 guest_activity_state;
518 u32 guest_sysenter_cs;
519 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100520 u32 vmx_preemption_timer_value;
521 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300522 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800523 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300524 u16 guest_es_selector;
525 u16 guest_cs_selector;
526 u16 guest_ss_selector;
527 u16 guest_ds_selector;
528 u16 guest_fs_selector;
529 u16 guest_gs_selector;
530 u16 guest_ldtr_selector;
531 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800532 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300533 u16 host_es_selector;
534 u16 host_cs_selector;
535 u16 host_ss_selector;
536 u16 host_ds_selector;
537 u16 host_fs_selector;
538 u16 host_gs_selector;
539 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300540};
541
542/*
543 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
544 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
545 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
546 */
547#define VMCS12_REVISION 0x11e57ed0
548
549/*
550 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
551 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
552 * current implementation, 4K are reserved to avoid future complications.
553 */
554#define VMCS12_SIZE 0x1000
555
556/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300557 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
558 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
559 */
560struct nested_vmx {
561 /* Has the level1 guest done vmxon? */
562 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400563 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300564
565 /* The guest-physical address of the current VMCS L1 keeps for L2 */
566 gpa_t current_vmptr;
567 /* The host-usable pointer to the above */
568 struct page *current_vmcs12_page;
569 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700570 /*
571 * Cache of the guest's VMCS, existing outside of guest memory.
572 * Loaded from guest memory during VMPTRLD. Flushed to guest
573 * memory during VMXOFF, VMCLEAR, VMPTRLD.
574 */
575 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300576 /*
577 * Indicates if the shadow vmcs must be updated with the
578 * data hold by vmcs12
579 */
580 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300581
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200582 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300583 /* L2 must run next, and mustn't decide to exit to L1. */
584 bool nested_run_pending;
Jim Mattson46e24df2017-11-27 17:22:25 -0600585
586 struct loaded_vmcs vmcs02;
587
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300588 /*
Jim Mattson46e24df2017-11-27 17:22:25 -0600589 * Guest pages referred to in the vmcs02 with host-physical
590 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300591 */
592 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800593 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800594 struct page *pi_desc_page;
595 struct pi_desc *pi_desc;
596 bool pi_pending;
597 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100598
599 struct hrtimer preemption_timer;
600 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200601
602 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
603 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800604
Wanpeng Li5c614b32015-10-13 09:18:36 -0700605 u16 vpid02;
606 u16 last_vpid;
607
Wincy Vanb9c237b2015-02-03 23:56:30 +0800608 u32 nested_vmx_procbased_ctls_low;
609 u32 nested_vmx_procbased_ctls_high;
610 u32 nested_vmx_true_procbased_ctls_low;
611 u32 nested_vmx_secondary_ctls_low;
612 u32 nested_vmx_secondary_ctls_high;
613 u32 nested_vmx_pinbased_ctls_low;
614 u32 nested_vmx_pinbased_ctls_high;
615 u32 nested_vmx_exit_ctls_low;
616 u32 nested_vmx_exit_ctls_high;
617 u32 nested_vmx_true_exit_ctls_low;
618 u32 nested_vmx_entry_ctls_low;
619 u32 nested_vmx_entry_ctls_high;
620 u32 nested_vmx_true_entry_ctls_low;
621 u32 nested_vmx_misc_low;
622 u32 nested_vmx_misc_high;
623 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700624 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300625};
626
Yang Zhang01e439b2013-04-11 19:25:12 +0800627#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800628#define POSTED_INTR_SN 1
629
Yang Zhang01e439b2013-04-11 19:25:12 +0800630/* Posted-Interrupt Descriptor */
631struct pi_desc {
632 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800633 union {
634 struct {
635 /* bit 256 - Outstanding Notification */
636 u16 on : 1,
637 /* bit 257 - Suppress Notification */
638 sn : 1,
639 /* bit 271:258 - Reserved */
640 rsvd_1 : 14;
641 /* bit 279:272 - Notification Vector */
642 u8 nv;
643 /* bit 287:280 - Reserved */
644 u8 rsvd_2;
645 /* bit 319:288 - Notification Destination */
646 u32 ndst;
647 };
648 u64 control;
649 };
650 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800651} __aligned(64);
652
Yang Zhanga20ed542013-04-11 19:25:15 +0800653static bool pi_test_and_set_on(struct pi_desc *pi_desc)
654{
655 return test_and_set_bit(POSTED_INTR_ON,
656 (unsigned long *)&pi_desc->control);
657}
658
659static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
660{
661 return test_and_clear_bit(POSTED_INTR_ON,
662 (unsigned long *)&pi_desc->control);
663}
664
665static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
666{
667 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
668}
669
Feng Wuebbfc762015-09-18 22:29:46 +0800670static inline void pi_clear_sn(struct pi_desc *pi_desc)
671{
672 return clear_bit(POSTED_INTR_SN,
673 (unsigned long *)&pi_desc->control);
674}
675
676static inline void pi_set_sn(struct pi_desc *pi_desc)
677{
678 return set_bit(POSTED_INTR_SN,
679 (unsigned long *)&pi_desc->control);
680}
681
682static inline int pi_test_on(struct pi_desc *pi_desc)
683{
684 return test_bit(POSTED_INTR_ON,
685 (unsigned long *)&pi_desc->control);
686}
687
688static inline int pi_test_sn(struct pi_desc *pi_desc)
689{
690 return test_bit(POSTED_INTR_SN,
691 (unsigned long *)&pi_desc->control);
692}
693
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400694struct vmx_msrs {
695 unsigned int nr;
696 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
697};
698
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400699struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000700 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300701 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300702 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200703 bool nmi_known_unmasked;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100704 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300705 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200706 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200707 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300708 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709 int nmsrs;
710 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800711 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400712#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300713 u64 msr_host_kernel_gs_base;
714 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400715#endif
Ashok Raj70131292018-02-01 22:59:43 +0100716
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100717 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100718
Gleb Natapov2961e8762013-11-25 15:37:13 +0200719 u32 vm_entry_controls_shadow;
720 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300721 /*
722 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
723 * non-nested (L1) guest, it always points to vmcs01. For a nested
724 * guest (L2), it points to a different VMCS.
725 */
726 struct loaded_vmcs vmcs01;
727 struct loaded_vmcs *loaded_vmcs;
728 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300729 struct msr_autoload {
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400730 struct vmx_msrs guest;
731 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300732 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400733 struct {
734 int loaded;
735 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300736#ifdef CONFIG_X86_64
737 u16 ds_sel, es_sel;
738#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200739 int gs_ldt_reload_needed;
740 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000741 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700742 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400743 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200744 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300745 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300746 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300747 struct kvm_segment segs[8];
748 } rmode;
749 struct {
750 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300751 struct kvm_save_segment {
752 u16 selector;
753 unsigned long base;
754 u32 limit;
755 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300756 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300757 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800758 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300759 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200760
761 /* Support for vnmi-less CPUs */
762 int soft_vnmi_blocked;
763 ktime_t entry_time;
764 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800765 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800766
Yang Zhang01e439b2013-04-11 19:25:12 +0800767 /* Posted interrupt descriptor */
768 struct pi_desc pi_desc;
769
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300770 /* Support for a guest hypervisor (nested VMX) */
771 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200772
773 /* Dynamic PLE window. */
774 int ple_window;
775 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800776
777 /* Support for PML */
778#define PML_ENTITY_NUM 512
779 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800780
Yunhong Jiang64672c92016-06-13 14:19:59 -0700781 /* apic deadline value in host tsc */
782 u64 hv_deadline_tsc;
783
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800784 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800785
786 bool guest_pkru_valid;
787 u32 guest_pkru;
788 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800789
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800790 /*
791 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
792 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
793 * in msr_ia32_feature_control_valid_bits.
794 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800795 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800796 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400797};
798
Avi Kivity2fb92db2011-04-27 19:42:18 +0300799enum segment_cache_field {
800 SEG_FIELD_SEL = 0,
801 SEG_FIELD_BASE = 1,
802 SEG_FIELD_LIMIT = 2,
803 SEG_FIELD_AR = 3,
804
805 SEG_FIELD_NR = 4
806};
807
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
809{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000810 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400811}
812
Feng Wuefc64402015-09-18 22:29:51 +0800813static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
814{
815 return &(to_vmx(vcpu)->pi_desc);
816}
817
Nadav Har'El22bd0352011-05-25 23:05:57 +0300818#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
819#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
820#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
821 [number##_HIGH] = VMCS12_OFFSET(name)+4
822
Abel Gordon4607c2d2013-04-18 14:35:55 +0300823
Bandan Dasfe2b2012014-04-21 15:20:14 -0400824static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300825 /*
826 * We do NOT shadow fields that are modified when L0
827 * traps and emulates any vmx instruction (e.g. VMPTRLD,
828 * VMXON...) executed by L1.
829 * For example, VM_INSTRUCTION_ERROR is read
830 * by L1 if a vmx instruction fails (part of the error path).
831 * Note the code assumes this logic. If for some reason
832 * we start shadowing these fields then we need to
833 * force a shadow sync when L0 emulates vmx instructions
834 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
835 * by nested_vmx_failValid)
836 */
837 VM_EXIT_REASON,
838 VM_EXIT_INTR_INFO,
839 VM_EXIT_INSTRUCTION_LEN,
840 IDT_VECTORING_INFO_FIELD,
841 IDT_VECTORING_ERROR_CODE,
842 VM_EXIT_INTR_ERROR_CODE,
843 EXIT_QUALIFICATION,
844 GUEST_LINEAR_ADDRESS,
845 GUEST_PHYSICAL_ADDRESS
846};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400847static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300848 ARRAY_SIZE(shadow_read_only_fields);
849
Bandan Dasfe2b2012014-04-21 15:20:14 -0400850static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800851 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300852 GUEST_RIP,
853 GUEST_RSP,
854 GUEST_CR0,
855 GUEST_CR3,
856 GUEST_CR4,
857 GUEST_INTERRUPTIBILITY_INFO,
858 GUEST_RFLAGS,
859 GUEST_CS_SELECTOR,
860 GUEST_CS_AR_BYTES,
861 GUEST_CS_LIMIT,
862 GUEST_CS_BASE,
863 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100864 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300865 CR0_GUEST_HOST_MASK,
866 CR0_READ_SHADOW,
867 CR4_READ_SHADOW,
868 TSC_OFFSET,
869 EXCEPTION_BITMAP,
870 CPU_BASED_VM_EXEC_CONTROL,
871 VM_ENTRY_EXCEPTION_ERROR_CODE,
872 VM_ENTRY_INTR_INFO_FIELD,
873 VM_ENTRY_INSTRUCTION_LEN,
874 VM_ENTRY_EXCEPTION_ERROR_CODE,
875 HOST_FS_BASE,
876 HOST_GS_BASE,
877 HOST_FS_SELECTOR,
878 HOST_GS_SELECTOR
879};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400880static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300881 ARRAY_SIZE(shadow_read_write_fields);
882
Mathias Krause772e0312012-08-30 01:30:19 +0200883static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300884 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800885 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300886 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
887 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
888 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
889 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
890 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
891 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
892 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
893 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800894 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300895 FIELD(HOST_ES_SELECTOR, host_es_selector),
896 FIELD(HOST_CS_SELECTOR, host_cs_selector),
897 FIELD(HOST_SS_SELECTOR, host_ss_selector),
898 FIELD(HOST_DS_SELECTOR, host_ds_selector),
899 FIELD(HOST_FS_SELECTOR, host_fs_selector),
900 FIELD(HOST_GS_SELECTOR, host_gs_selector),
901 FIELD(HOST_TR_SELECTOR, host_tr_selector),
902 FIELD64(IO_BITMAP_A, io_bitmap_a),
903 FIELD64(IO_BITMAP_B, io_bitmap_b),
904 FIELD64(MSR_BITMAP, msr_bitmap),
905 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
906 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
907 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
908 FIELD64(TSC_OFFSET, tsc_offset),
909 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
910 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800911 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300912 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800913 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
914 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
915 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
916 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800917 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300918 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
919 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
920 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
921 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
922 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
923 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
924 FIELD64(GUEST_PDPTR0, guest_pdptr0),
925 FIELD64(GUEST_PDPTR1, guest_pdptr1),
926 FIELD64(GUEST_PDPTR2, guest_pdptr2),
927 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100928 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300929 FIELD64(HOST_IA32_PAT, host_ia32_pat),
930 FIELD64(HOST_IA32_EFER, host_ia32_efer),
931 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
932 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
933 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
934 FIELD(EXCEPTION_BITMAP, exception_bitmap),
935 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
936 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
937 FIELD(CR3_TARGET_COUNT, cr3_target_count),
938 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
939 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
940 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
941 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
942 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
943 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
944 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
945 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
946 FIELD(TPR_THRESHOLD, tpr_threshold),
947 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
948 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
949 FIELD(VM_EXIT_REASON, vm_exit_reason),
950 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
951 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
952 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
953 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
954 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
955 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
956 FIELD(GUEST_ES_LIMIT, guest_es_limit),
957 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
958 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
959 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
960 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
961 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
962 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
963 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
964 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
965 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
966 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
967 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
968 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
969 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
970 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
971 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
972 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
973 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
974 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
975 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
976 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
977 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100978 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300979 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
980 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
981 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
982 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
983 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
984 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
985 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
986 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
987 FIELD(EXIT_QUALIFICATION, exit_qualification),
988 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
989 FIELD(GUEST_CR0, guest_cr0),
990 FIELD(GUEST_CR3, guest_cr3),
991 FIELD(GUEST_CR4, guest_cr4),
992 FIELD(GUEST_ES_BASE, guest_es_base),
993 FIELD(GUEST_CS_BASE, guest_cs_base),
994 FIELD(GUEST_SS_BASE, guest_ss_base),
995 FIELD(GUEST_DS_BASE, guest_ds_base),
996 FIELD(GUEST_FS_BASE, guest_fs_base),
997 FIELD(GUEST_GS_BASE, guest_gs_base),
998 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
999 FIELD(GUEST_TR_BASE, guest_tr_base),
1000 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1001 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1002 FIELD(GUEST_DR7, guest_dr7),
1003 FIELD(GUEST_RSP, guest_rsp),
1004 FIELD(GUEST_RIP, guest_rip),
1005 FIELD(GUEST_RFLAGS, guest_rflags),
1006 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1007 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1008 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1009 FIELD(HOST_CR0, host_cr0),
1010 FIELD(HOST_CR3, host_cr3),
1011 FIELD(HOST_CR4, host_cr4),
1012 FIELD(HOST_FS_BASE, host_fs_base),
1013 FIELD(HOST_GS_BASE, host_gs_base),
1014 FIELD(HOST_TR_BASE, host_tr_base),
1015 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1016 FIELD(HOST_IDTR_BASE, host_idtr_base),
1017 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1018 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1019 FIELD(HOST_RSP, host_rsp),
1020 FIELD(HOST_RIP, host_rip),
1021};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001022
1023static inline short vmcs_field_to_offset(unsigned long field)
1024{
Dan Williamseb99bd62018-01-31 17:47:03 -08001025 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1026 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001027
Dan Williamseb99bd62018-01-31 17:47:03 -08001028 BUILD_BUG_ON(size > SHRT_MAX);
1029 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -08001030 return -ENOENT;
1031
Dan Williamseb99bd62018-01-31 17:47:03 -08001032 field = array_index_nospec(field, size);
1033 offset = vmcs_field_to_offset_table[field];
1034 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001035 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -08001036 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001037}
1038
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001039static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1040{
David Matlack4f2777b2016-07-13 17:16:37 -07001041 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001042}
1043
1044static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
1045{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001046 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08001047 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001048 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +08001049
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001050 return page;
1051}
1052
1053static void nested_release_page(struct page *page)
1054{
1055 kvm_release_page_dirty(page);
1056}
1057
1058static void nested_release_page_clean(struct page *page)
1059{
1060 kvm_release_page_clean(page);
1061}
1062
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001063static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +08001064static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001065static void kvm_cpu_vmxon(u64 addr);
1066static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001067static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +02001068static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001069static void vmx_set_segment(struct kvm_vcpu *vcpu,
1070 struct kvm_segment *var, int seg);
1071static void vmx_get_segment(struct kvm_vcpu *vcpu,
1072 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001073static bool guest_state_valid(struct kvm_vcpu *vcpu);
1074static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +03001075static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +03001076static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +08001077static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +01001078static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Yi Wang0c4d57a2018-11-08 11:22:21 +08001079static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj70131292018-02-01 22:59:43 +01001080 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001081
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1083static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001084/*
1085 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1086 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1087 */
1088static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +03001089static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
Feng Wubf9f6ac2015-09-18 22:29:55 +08001091/*
1092 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1093 * can find which vCPU should be waken up.
1094 */
1095static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1096static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1097
Avi Kivity3e7c73e2009-02-24 21:46:19 +02001098static unsigned long *vmx_io_bitmap_a;
1099static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +03001100static unsigned long *vmx_vmread_bitmap;
1101static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +03001102
Avi Kivity110312c2010-12-21 12:54:20 +02001103static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001104static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001105
Sheng Yang2384d2b2008-01-17 15:14:33 +08001106static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1107static DEFINE_SPINLOCK(vmx_vpid_lock);
1108
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001109static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 int size;
1111 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001112 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001114 u32 pin_based_exec_ctrl;
1115 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001116 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001117 u32 vmexit_ctrl;
1118 u32 vmentry_ctrl;
1119} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120
Hannes Ederefff9e52008-11-28 17:02:06 +01001121static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001122 u32 ept;
1123 u32 vpid;
1124} vmx_capability;
1125
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126#define VMX_SEGMENT_FIELD(seg) \
1127 [VCPU_SREG_##seg] = { \
1128 .selector = GUEST_##seg##_SELECTOR, \
1129 .base = GUEST_##seg##_BASE, \
1130 .limit = GUEST_##seg##_LIMIT, \
1131 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1132 }
1133
Mathias Krause772e0312012-08-30 01:30:19 +02001134static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 unsigned selector;
1136 unsigned base;
1137 unsigned limit;
1138 unsigned ar_bytes;
1139} kvm_vmx_segment_fields[] = {
1140 VMX_SEGMENT_FIELD(CS),
1141 VMX_SEGMENT_FIELD(DS),
1142 VMX_SEGMENT_FIELD(ES),
1143 VMX_SEGMENT_FIELD(FS),
1144 VMX_SEGMENT_FIELD(GS),
1145 VMX_SEGMENT_FIELD(SS),
1146 VMX_SEGMENT_FIELD(TR),
1147 VMX_SEGMENT_FIELD(LDTR),
1148};
1149
Avi Kivity26bb0982009-09-07 11:14:12 +03001150static u64 host_efer;
1151
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001152static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1153
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001154/*
Brian Gerst8c065852010-07-17 09:03:26 -04001155 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001156 * away by decrementing the array size.
1157 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001159#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001160 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001162 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
Jan Kiszka5bb16012016-02-09 20:14:21 +01001165static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166{
1167 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1168 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001169 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1170}
1171
Jan Kiszka6f054852016-02-09 20:15:18 +01001172static inline bool is_debug(u32 intr_info)
1173{
1174 return is_exception_n(intr_info, DB_VECTOR);
1175}
1176
1177static inline bool is_breakpoint(u32 intr_info)
1178{
1179 return is_exception_n(intr_info, BP_VECTOR);
1180}
1181
Jan Kiszka5bb16012016-02-09 20:14:21 +01001182static inline bool is_page_fault(u32 intr_info)
1183{
1184 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185}
1186
Gui Jianfeng31299942010-03-15 17:29:09 +08001187static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001188{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001189 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001190}
1191
Gui Jianfeng31299942010-03-15 17:29:09 +08001192static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001193{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001194 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001195}
1196
Gui Jianfeng31299942010-03-15 17:29:09 +08001197static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
1199 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1200 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1201}
1202
Gui Jianfeng31299942010-03-15 17:29:09 +08001203static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001204{
1205 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1206 INTR_INFO_VALID_MASK)) ==
1207 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1208}
1209
Linus Torvalds587da2b2018-03-20 12:16:59 -07001210/* Undocumented: icebp/int1 */
1211static inline bool is_icebp(u32 intr_info)
1212{
1213 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1214 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1215}
1216
Gui Jianfeng31299942010-03-15 17:29:09 +08001217static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001218{
Sheng Yang04547152009-04-01 15:52:31 +08001219 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001220}
1221
Gui Jianfeng31299942010-03-15 17:29:09 +08001222static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001223{
Sheng Yang04547152009-04-01 15:52:31 +08001224 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001225}
1226
Paolo Bonzini35754c92015-07-29 12:05:37 +02001227static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001228{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001229 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001230}
1231
Gui Jianfeng31299942010-03-15 17:29:09 +08001232static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001233{
Sheng Yang04547152009-04-01 15:52:31 +08001234 return vmcs_config.cpu_based_exec_ctrl &
1235 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001236}
1237
Avi Kivity774ead32007-12-26 13:57:04 +02001238static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001239{
Sheng Yang04547152009-04-01 15:52:31 +08001240 return vmcs_config.cpu_based_2nd_exec_ctrl &
1241 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1242}
1243
Yang Zhang8d146952013-01-25 10:18:50 +08001244static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1245{
1246 return vmcs_config.cpu_based_2nd_exec_ctrl &
1247 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1248}
1249
Yang Zhang83d4c282013-01-25 10:18:49 +08001250static inline bool cpu_has_vmx_apic_register_virt(void)
1251{
1252 return vmcs_config.cpu_based_2nd_exec_ctrl &
1253 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1254}
1255
Yang Zhangc7c9c562013-01-25 10:18:51 +08001256static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1257{
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1260}
1261
Yunhong Jiang64672c92016-06-13 14:19:59 -07001262/*
1263 * Comment's format: document - errata name - stepping - processor name.
1264 * Refer from
1265 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1266 */
1267static u32 vmx_preemption_cpu_tfms[] = {
1268/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12690x000206E6,
1270/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1271/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1272/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12730x00020652,
1274/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12750x00020655,
1276/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1277/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1278/*
1279 * 320767.pdf - AAP86 - B1 -
1280 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1281 */
12820x000106E5,
1283/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12840x000106A0,
1285/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12860x000106A1,
1287/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
12880x000106A4,
1289 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1290 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1291 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
12920x000106A5,
1293};
1294
1295static inline bool cpu_has_broken_vmx_preemption_timer(void)
1296{
1297 u32 eax = cpuid_eax(0x00000001), i;
1298
1299 /* Clear the reserved bits */
1300 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001301 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001302 if (eax == vmx_preemption_cpu_tfms[i])
1303 return true;
1304
1305 return false;
1306}
1307
1308static inline bool cpu_has_vmx_preemption_timer(void)
1309{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001310 return vmcs_config.pin_based_exec_ctrl &
1311 PIN_BASED_VMX_PREEMPTION_TIMER;
1312}
1313
Yang Zhang01e439b2013-04-11 19:25:12 +08001314static inline bool cpu_has_vmx_posted_intr(void)
1315{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001316 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1317 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001318}
1319
1320static inline bool cpu_has_vmx_apicv(void)
1321{
1322 return cpu_has_vmx_apic_register_virt() &&
1323 cpu_has_vmx_virtual_intr_delivery() &&
1324 cpu_has_vmx_posted_intr();
1325}
1326
Sheng Yang04547152009-04-01 15:52:31 +08001327static inline bool cpu_has_vmx_flexpriority(void)
1328{
1329 return cpu_has_vmx_tpr_shadow() &&
1330 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001331}
1332
Marcelo Tosattie7997942009-06-11 12:07:40 -03001333static inline bool cpu_has_vmx_ept_execute_only(void)
1334{
Gui Jianfeng31299942010-03-15 17:29:09 +08001335 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001336}
1337
Marcelo Tosattie7997942009-06-11 12:07:40 -03001338static inline bool cpu_has_vmx_ept_2m_page(void)
1339{
Gui Jianfeng31299942010-03-15 17:29:09 +08001340 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001341}
1342
Sheng Yang878403b2010-01-05 19:02:29 +08001343static inline bool cpu_has_vmx_ept_1g_page(void)
1344{
Gui Jianfeng31299942010-03-15 17:29:09 +08001345 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001346}
1347
Sheng Yang4bc9b982010-06-02 14:05:24 +08001348static inline bool cpu_has_vmx_ept_4levels(void)
1349{
1350 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1351}
1352
Xudong Hao83c3a332012-05-28 19:33:35 +08001353static inline bool cpu_has_vmx_ept_ad_bits(void)
1354{
1355 return vmx_capability.ept & VMX_EPT_AD_BIT;
1356}
1357
Gui Jianfeng31299942010-03-15 17:29:09 +08001358static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001359{
Gui Jianfeng31299942010-03-15 17:29:09 +08001360 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001361}
1362
Gui Jianfeng31299942010-03-15 17:29:09 +08001363static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001364{
Gui Jianfeng31299942010-03-15 17:29:09 +08001365 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001366}
1367
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001368static inline bool cpu_has_vmx_invvpid_single(void)
1369{
1370 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1371}
1372
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001373static inline bool cpu_has_vmx_invvpid_global(void)
1374{
1375 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1376}
1377
Wanpeng Li2df19692017-03-23 05:30:08 -07001378static inline bool cpu_has_vmx_invvpid(void)
1379{
1380 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1381}
1382
Gui Jianfeng31299942010-03-15 17:29:09 +08001383static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001384{
Sheng Yang04547152009-04-01 15:52:31 +08001385 return vmcs_config.cpu_based_2nd_exec_ctrl &
1386 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001387}
1388
Gui Jianfeng31299942010-03-15 17:29:09 +08001389static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001390{
1391 return vmcs_config.cpu_based_2nd_exec_ctrl &
1392 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1393}
1394
Gui Jianfeng31299942010-03-15 17:29:09 +08001395static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001396{
1397 return vmcs_config.cpu_based_2nd_exec_ctrl &
1398 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1399}
1400
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001401static inline bool cpu_has_vmx_basic_inout(void)
1402{
1403 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1404}
1405
Paolo Bonzini35754c92015-07-29 12:05:37 +02001406static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001407{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001408 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001409}
1410
Gui Jianfeng31299942010-03-15 17:29:09 +08001411static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001412{
Sheng Yang04547152009-04-01 15:52:31 +08001413 return vmcs_config.cpu_based_2nd_exec_ctrl &
1414 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001415}
1416
Gui Jianfeng31299942010-03-15 17:29:09 +08001417static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001418{
1419 return vmcs_config.cpu_based_2nd_exec_ctrl &
1420 SECONDARY_EXEC_RDTSCP;
1421}
1422
Mao, Junjiead756a12012-07-02 01:18:48 +00001423static inline bool cpu_has_vmx_invpcid(void)
1424{
1425 return vmcs_config.cpu_based_2nd_exec_ctrl &
1426 SECONDARY_EXEC_ENABLE_INVPCID;
1427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001430{
1431 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1432}
1433
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001434static inline bool cpu_has_vmx_wbinvd_exit(void)
1435{
1436 return vmcs_config.cpu_based_2nd_exec_ctrl &
1437 SECONDARY_EXEC_WBINVD_EXITING;
1438}
1439
Abel Gordonabc4fc52013-04-18 14:35:25 +03001440static inline bool cpu_has_vmx_shadow_vmcs(void)
1441{
1442 u64 vmx_msr;
1443 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1444 /* check if the cpu supports writing r/o exit information fields */
1445 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1446 return false;
1447
1448 return vmcs_config.cpu_based_2nd_exec_ctrl &
1449 SECONDARY_EXEC_SHADOW_VMCS;
1450}
1451
Kai Huang843e4332015-01-28 10:54:28 +08001452static inline bool cpu_has_vmx_pml(void)
1453{
1454 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1455}
1456
Haozhong Zhang64903d62015-10-20 15:39:09 +08001457static inline bool cpu_has_vmx_tsc_scaling(void)
1458{
1459 return vmcs_config.cpu_based_2nd_exec_ctrl &
1460 SECONDARY_EXEC_TSC_SCALING;
1461}
1462
Sheng Yang04547152009-04-01 15:52:31 +08001463static inline bool report_flexpriority(void)
1464{
1465 return flexpriority_enabled;
1466}
1467
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001468static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1469{
1470 return vmcs12->cpu_based_vm_exec_control & bit;
1471}
1472
1473static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1474{
1475 return (vmcs12->cpu_based_vm_exec_control &
1476 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1477 (vmcs12->secondary_vm_exec_control & bit);
1478}
1479
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001480static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001481{
1482 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1483}
1484
Jan Kiszkaf4124502014-03-07 20:03:13 +01001485static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1486{
1487 return vmcs12->pin_based_vm_exec_control &
1488 PIN_BASED_VMX_PREEMPTION_TIMER;
1489}
1490
Nadav Har'El155a97a2013-08-05 11:07:16 +03001491static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1492{
1493 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1494}
1495
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001496static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1497{
1498 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1499 vmx_xsaves_supported();
1500}
1501
Wincy Vanf2b93282015-02-03 23:56:03 +08001502static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1503{
1504 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1505}
1506
Wanpeng Li5c614b32015-10-13 09:18:36 -07001507static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1508{
1509 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1510}
1511
Wincy Van82f0dd42015-02-03 23:57:18 +08001512static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1513{
1514 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1515}
1516
Wincy Van608406e2015-02-03 23:57:51 +08001517static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1518{
1519 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1520}
1521
Wincy Van705699a2015-02-03 23:58:17 +08001522static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1523{
1524 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1525}
1526
Jim Mattson3f618a02016-12-12 11:01:37 -08001527static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001528{
1529 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001530 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001531}
1532
Jan Kiszka533558b2014-01-04 18:47:20 +01001533static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1534 u32 exit_intr_info,
1535 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001536static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1537 struct vmcs12 *vmcs12,
1538 u32 reason, unsigned long qualification);
1539
Rusty Russell8b9cf982007-07-30 16:31:43 +10001540static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001541{
1542 int i;
1543
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001544 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001545 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001546 return i;
1547 return -1;
1548}
1549
Uros Bizjak3489f082018-10-11 19:40:43 +02001550static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001551{
1552 struct {
1553 u64 vpid : 16;
1554 u64 rsvd : 48;
1555 u64 gva;
1556 } operand = { vpid, 0, gva };
1557
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001558 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001559 /* CF==1 or ZF==1 --> rc = -1 */
1560 "; ja 1f ; ud2 ; 1:"
1561 : : "a"(&operand), "c"(ext) : "cc", "memory");
1562}
1563
Uros Bizjak3489f082018-10-11 19:40:43 +02001564static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08001565{
1566 struct {
1567 u64 eptp, gpa;
1568 } operand = {eptp, gpa};
1569
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001570 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001571 /* CF==1 or ZF==1 --> rc = -1 */
1572 "; ja 1f ; ud2 ; 1:\n"
1573 : : "a" (&operand), "c" (ext) : "cc", "memory");
1574}
1575
Avi Kivity26bb0982009-09-07 11:14:12 +03001576static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001577{
1578 int i;
1579
Rusty Russell8b9cf982007-07-30 16:31:43 +10001580 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001581 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001583 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001584}
1585
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586static void vmcs_clear(struct vmcs *vmcs)
1587{
1588 u64 phys_addr = __pa(vmcs);
1589 u8 error;
1590
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001591 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001592 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 : "cc", "memory");
1594 if (error)
1595 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1596 vmcs, phys_addr);
1597}
1598
Nadav Har'Eld462b812011-05-24 15:26:10 +03001599static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1600{
1601 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001602 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1603 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001604 loaded_vmcs->cpu = -1;
1605 loaded_vmcs->launched = 0;
1606}
1607
Dongxiao Xu7725b892010-05-11 18:29:38 +08001608static void vmcs_load(struct vmcs *vmcs)
1609{
1610 u64 phys_addr = __pa(vmcs);
1611 u8 error;
1612
1613 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001614 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001615 : "cc", "memory");
1616 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001617 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001618 vmcs, phys_addr);
1619}
1620
Dave Young2965faa2015-09-09 15:38:55 -07001621#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001622/*
1623 * This bitmap is used to indicate whether the vmclear
1624 * operation is enabled on all cpus. All disabled by
1625 * default.
1626 */
1627static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1628
1629static inline void crash_enable_local_vmclear(int cpu)
1630{
1631 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1632}
1633
1634static inline void crash_disable_local_vmclear(int cpu)
1635{
1636 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1637}
1638
1639static inline int crash_local_vmclear_enabled(int cpu)
1640{
1641 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1642}
1643
1644static void crash_vmclear_local_loaded_vmcss(void)
1645{
1646 int cpu = raw_smp_processor_id();
1647 struct loaded_vmcs *v;
1648
1649 if (!crash_local_vmclear_enabled(cpu))
1650 return;
1651
1652 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1653 loaded_vmcss_on_cpu_link)
1654 vmcs_clear(v->vmcs);
1655}
1656#else
1657static inline void crash_enable_local_vmclear(int cpu) { }
1658static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001659#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001660
Nadav Har'Eld462b812011-05-24 15:26:10 +03001661static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001663 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001664 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665
Nadav Har'Eld462b812011-05-24 15:26:10 +03001666 if (loaded_vmcs->cpu != cpu)
1667 return; /* vcpu migration can race with cpu offline */
1668 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001670 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001671 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001672
1673 /*
1674 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1675 * is before setting loaded_vmcs->vcpu to -1 which is done in
1676 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1677 * then adds the vmcs into percpu list before it is deleted.
1678 */
1679 smp_wmb();
1680
Nadav Har'Eld462b812011-05-24 15:26:10 +03001681 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001682 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683}
1684
Nadav Har'Eld462b812011-05-24 15:26:10 +03001685static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001686{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001687 int cpu = loaded_vmcs->cpu;
1688
1689 if (cpu != -1)
1690 smp_call_function_single(cpu,
1691 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001692}
1693
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001694static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001695{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001696 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001697 return;
1698
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001699 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001700 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001701}
1702
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001703static inline void vpid_sync_vcpu_global(void)
1704{
1705 if (cpu_has_vmx_invvpid_global())
1706 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1707}
1708
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001709static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001710{
1711 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001712 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001713 else
1714 vpid_sync_vcpu_global();
1715}
1716
Sheng Yang14394422008-04-28 12:24:45 +08001717static inline void ept_sync_global(void)
1718{
1719 if (cpu_has_vmx_invept_global())
1720 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1721}
1722
1723static inline void ept_sync_context(u64 eptp)
1724{
Avi Kivity089d0342009-03-23 18:26:32 +02001725 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001726 if (cpu_has_vmx_invept_context())
1727 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1728 else
1729 ept_sync_global();
1730 }
1731}
1732
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001733static __always_inline void vmcs_check16(unsigned long field)
1734{
1735 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1736 "16-bit accessor invalid for 64-bit field");
1737 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1738 "16-bit accessor invalid for 64-bit high field");
1739 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1740 "16-bit accessor invalid for 32-bit high field");
1741 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1742 "16-bit accessor invalid for natural width field");
1743}
1744
1745static __always_inline void vmcs_check32(unsigned long field)
1746{
1747 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1748 "32-bit accessor invalid for 16-bit field");
1749 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1750 "32-bit accessor invalid for natural width field");
1751}
1752
1753static __always_inline void vmcs_check64(unsigned long field)
1754{
1755 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1756 "64-bit accessor invalid for 16-bit field");
1757 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1758 "64-bit accessor invalid for 64-bit high field");
1759 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1760 "64-bit accessor invalid for 32-bit field");
1761 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1762 "64-bit accessor invalid for natural width field");
1763}
1764
1765static __always_inline void vmcs_checkl(unsigned long field)
1766{
1767 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1768 "Natural width accessor invalid for 16-bit field");
1769 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1770 "Natural width accessor invalid for 64-bit field");
1771 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1772 "Natural width accessor invalid for 64-bit high field");
1773 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1774 "Natural width accessor invalid for 32-bit field");
1775}
1776
1777static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778{
Avi Kivity5e520e62011-05-15 10:13:12 -04001779 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780
Avi Kivity5e520e62011-05-15 10:13:12 -04001781 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1782 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 return value;
1784}
1785
Avi Kivity96304212011-05-15 10:13:13 -04001786static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001788 vmcs_check16(field);
1789 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790}
1791
Avi Kivity96304212011-05-15 10:13:13 -04001792static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001794 vmcs_check32(field);
1795 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796}
1797
Avi Kivity96304212011-05-15 10:13:13 -04001798static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001800 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001801#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001802 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001804 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805#endif
1806}
1807
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001808static __always_inline unsigned long vmcs_readl(unsigned long field)
1809{
1810 vmcs_checkl(field);
1811 return __vmcs_readl(field);
1812}
1813
Avi Kivitye52de1b2007-01-05 16:36:56 -08001814static noinline void vmwrite_error(unsigned long field, unsigned long value)
1815{
1816 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1817 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1818 dump_stack();
1819}
1820
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001821static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822{
1823 u8 error;
1824
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001825 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001826 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001827 if (unlikely(error))
1828 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829}
1830
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001831static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001833 vmcs_check16(field);
1834 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835}
1836
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001837static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001839 vmcs_check32(field);
1840 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841}
1842
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001843static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001845 vmcs_check64(field);
1846 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001847#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001849 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850#endif
1851}
1852
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001853static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001854{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001855 vmcs_checkl(field);
1856 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001857}
1858
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001859static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001860{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001861 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1862 "vmcs_clear_bits does not support 64-bit fields");
1863 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1864}
1865
1866static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1867{
1868 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1869 "vmcs_set_bits does not support 64-bit fields");
1870 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001871}
1872
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001873static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1874{
1875 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1876}
1877
Gleb Natapov2961e8762013-11-25 15:37:13 +02001878static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1879{
1880 vmcs_write32(VM_ENTRY_CONTROLS, val);
1881 vmx->vm_entry_controls_shadow = val;
1882}
1883
1884static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1885{
1886 if (vmx->vm_entry_controls_shadow != val)
1887 vm_entry_controls_init(vmx, val);
1888}
1889
1890static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1891{
1892 return vmx->vm_entry_controls_shadow;
1893}
1894
1895
1896static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1897{
1898 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1899}
1900
1901static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1902{
1903 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1904}
1905
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001906static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1907{
1908 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1909}
1910
Gleb Natapov2961e8762013-11-25 15:37:13 +02001911static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1912{
1913 vmcs_write32(VM_EXIT_CONTROLS, val);
1914 vmx->vm_exit_controls_shadow = val;
1915}
1916
1917static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1918{
1919 if (vmx->vm_exit_controls_shadow != val)
1920 vm_exit_controls_init(vmx, val);
1921}
1922
1923static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1924{
1925 return vmx->vm_exit_controls_shadow;
1926}
1927
1928
1929static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1930{
1931 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1932}
1933
1934static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1935{
1936 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1937}
1938
Avi Kivity2fb92db2011-04-27 19:42:18 +03001939static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1940{
1941 vmx->segment_cache.bitmask = 0;
1942}
1943
1944static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1945 unsigned field)
1946{
1947 bool ret;
1948 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1949
1950 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1951 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1952 vmx->segment_cache.bitmask = 0;
1953 }
1954 ret = vmx->segment_cache.bitmask & mask;
1955 vmx->segment_cache.bitmask |= mask;
1956 return ret;
1957}
1958
1959static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1960{
1961 u16 *p = &vmx->segment_cache.seg[seg].selector;
1962
1963 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1964 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1965 return *p;
1966}
1967
1968static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1969{
1970 ulong *p = &vmx->segment_cache.seg[seg].base;
1971
1972 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1973 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1974 return *p;
1975}
1976
1977static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1978{
1979 u32 *p = &vmx->segment_cache.seg[seg].limit;
1980
1981 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1982 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1983 return *p;
1984}
1985
1986static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1987{
1988 u32 *p = &vmx->segment_cache.seg[seg].ar;
1989
1990 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1991 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1992 return *p;
1993}
1994
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001995static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1996{
1997 u32 eb;
1998
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001999 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01002000 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002001 if ((vcpu->guest_debug &
2002 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2003 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2004 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002005 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002006 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002007 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002008 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02002009 if (vcpu->fpu_active)
2010 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002011
2012 /* When we are running a nested L2 guest and L1 specified for it a
2013 * certain exception bitmap, we must trap the same exceptions and pass
2014 * them to L1. When running L2, we will only handle the exceptions
2015 * specified above if L1 did not want them.
2016 */
2017 if (is_guest_mode(vcpu))
2018 eb |= get_vmcs12(vcpu)->exception_bitmap;
2019
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002020 vmcs_write32(EXCEPTION_BITMAP, eb);
2021}
2022
Ashok Raj70131292018-02-01 22:59:43 +01002023/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01002024 * Check if MSR is intercepted for currently loaded MSR bitmap.
2025 */
2026static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2027{
2028 unsigned long *msr_bitmap;
2029 int f = sizeof(unsigned long);
2030
2031 if (!cpu_has_vmx_msr_bitmap())
2032 return true;
2033
2034 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2035
2036 if (msr <= 0x1fff) {
2037 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2038 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2039 msr &= 0x1fff;
2040 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2041 }
2042
2043 return true;
2044}
2045
2046/*
Ashok Raj70131292018-02-01 22:59:43 +01002047 * Check if MSR is intercepted for L01 MSR bitmap.
2048 */
2049static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2050{
2051 unsigned long *msr_bitmap;
2052 int f = sizeof(unsigned long);
2053
2054 if (!cpu_has_vmx_msr_bitmap())
2055 return true;
2056
2057 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2058
2059 if (msr <= 0x1fff) {
2060 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2061 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2062 msr &= 0x1fff;
2063 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2064 }
2065
2066 return true;
2067}
2068
Gleb Natapov2961e8762013-11-25 15:37:13 +02002069static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2070 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002071{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002072 vm_entry_controls_clearbit(vmx, entry);
2073 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002074}
2075
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002076static int find_msr(struct vmx_msrs *m, unsigned int msr)
2077{
2078 unsigned int i;
2079
2080 for (i = 0; i < m->nr; ++i) {
2081 if (m->val[i].index == msr)
2082 return i;
2083 }
2084 return -ENOENT;
2085}
2086
Avi Kivity61d2ef22010-04-28 16:40:38 +03002087static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2088{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002089 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002090 struct msr_autoload *m = &vmx->msr_autoload;
2091
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002092 switch (msr) {
2093 case MSR_EFER:
2094 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002095 clear_atomic_switch_msr_special(vmx,
2096 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002097 VM_EXIT_LOAD_IA32_EFER);
2098 return;
2099 }
2100 break;
2101 case MSR_CORE_PERF_GLOBAL_CTRL:
2102 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002103 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002104 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2105 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2106 return;
2107 }
2108 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002109 }
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002110 i = find_msr(&m->guest, msr);
2111 if (i < 0)
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002112 goto skip_guest;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002113 --m->guest.nr;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002114 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002115 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002116
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002117skip_guest:
2118 i = find_msr(&m->host, msr);
2119 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002120 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002121
2122 --m->host.nr;
2123 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002124 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002125}
2126
Gleb Natapov2961e8762013-11-25 15:37:13 +02002127static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2128 unsigned long entry, unsigned long exit,
2129 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2130 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002131{
2132 vmcs_write64(guest_val_vmcs, guest_val);
2133 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002134 vm_entry_controls_setbit(vmx, entry);
2135 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002136}
2137
Avi Kivity61d2ef22010-04-28 16:40:38 +03002138static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002139 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002140{
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002141 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002142 struct msr_autoload *m = &vmx->msr_autoload;
2143
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002144 switch (msr) {
2145 case MSR_EFER:
2146 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002147 add_atomic_switch_msr_special(vmx,
2148 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002149 VM_EXIT_LOAD_IA32_EFER,
2150 GUEST_IA32_EFER,
2151 HOST_IA32_EFER,
2152 guest_val, host_val);
2153 return;
2154 }
2155 break;
2156 case MSR_CORE_PERF_GLOBAL_CTRL:
2157 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002158 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002159 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2160 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2161 GUEST_IA32_PERF_GLOBAL_CTRL,
2162 HOST_IA32_PERF_GLOBAL_CTRL,
2163 guest_val, host_val);
2164 return;
2165 }
2166 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002167 case MSR_IA32_PEBS_ENABLE:
2168 /* PEBS needs a quiescent period after being disabled (to write
2169 * a record). Disabling PEBS through VMX MSR swapping doesn't
2170 * provide that period, so a CPU could write host's record into
2171 * guest's memory.
2172 */
2173 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002174 }
2175
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002176 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002177 if (!entry_only)
2178 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002179
Xiaoyao Liacd744b2019-02-14 12:08:58 +08002180 if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
2181 (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002182 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002183 "Can't add msr %x\n", msr);
2184 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002185 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002186 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002187 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002188 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002189 }
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002190 m->guest.val[i].index = msr;
2191 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002192
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002193 if (entry_only)
2194 return;
2195
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002196 if (j < 0) {
2197 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002198 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002199 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002200 m->host.val[j].index = msr;
2201 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002202}
2203
Avi Kivity33ed6322007-05-02 16:54:03 +03002204static void reload_tss(void)
2205{
Avi Kivity33ed6322007-05-02 16:54:03 +03002206 /*
2207 * VT restores TR but not its size. Useless.
2208 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002209 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002210 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002211
Avi Kivityd3591922010-07-26 18:32:39 +03002212 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002213 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2214 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002215}
2216
Avi Kivity92c0d902009-10-29 11:00:16 +02002217static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002218{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002219 u64 guest_efer = vmx->vcpu.arch.efer;
2220 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002221
Paolo Bonzini1b08d2a2019-10-27 16:23:23 +01002222 /* Shadow paging assumes NX to be available. */
2223 if (!enable_ept)
2224 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -07002225
Avi Kivity51c6cf62007-08-29 03:48:05 +03002226 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002227 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002228 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002229 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002230#ifdef CONFIG_X86_64
2231 ignore_bits |= EFER_LMA | EFER_LME;
2232 /* SCE is meaningful only in long mode on Intel */
2233 if (guest_efer & EFER_LMA)
2234 ignore_bits &= ~(u64)EFER_SCE;
2235#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002236
2237 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002238
2239 /*
2240 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2241 * On CPUs that support "load IA32_EFER", always switch EFER
2242 * atomically, since it's faster than switching it manually.
2243 */
2244 if (cpu_has_load_ia32_efer ||
2245 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002246 if (!(guest_efer & EFER_LMA))
2247 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002248 if (guest_efer != host_efer)
2249 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002250 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002251 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002252 } else {
2253 guest_efer &= ~ignore_bits;
2254 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002255
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002256 vmx->guest_msrs[efer_offset].data = guest_efer;
2257 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2258
2259 return true;
2260 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002261}
2262
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002263static unsigned long segment_base(u16 selector)
2264{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002265 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002266 struct desc_struct *d;
2267 unsigned long table_base;
2268 unsigned long v;
2269
2270 if (!(selector & ~3))
2271 return 0;
2272
Avi Kivityd3591922010-07-26 18:32:39 +03002273 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002274
2275 if (selector & 4) { /* from ldt */
2276 u16 ldt_selector = kvm_read_ldt();
2277
2278 if (!(ldt_selector & ~3))
2279 return 0;
2280
2281 table_base = segment_base(ldt_selector);
2282 }
2283 d = (struct desc_struct *)(table_base + (selector & ~7));
2284 v = get_desc_base(d);
2285#ifdef CONFIG_X86_64
2286 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2287 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2288#endif
2289 return v;
2290}
2291
2292static inline unsigned long kvm_read_tr_base(void)
2293{
2294 u16 tr;
2295 asm("str %0" : "=g"(tr));
2296 return segment_base(tr);
2297}
2298
Avi Kivity04d2cc72007-09-10 18:10:54 +03002299static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002300{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002301 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002302 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002303
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002304 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002305 return;
2306
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002307 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002308 /*
2309 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2310 * allow segment selectors with cpl > 0 or ti == 1.
2311 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002312 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002313 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002314 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002315 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002316 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002317 vmx->host_state.fs_reload_needed = 0;
2318 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002319 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002320 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002321 }
Avi Kivity9581d442010-10-19 16:46:55 +02002322 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002323 if (!(vmx->host_state.gs_sel & 7))
2324 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002325 else {
2326 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002327 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002328 }
2329
2330#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002331 savesegment(ds, vmx->host_state.ds_sel);
2332 savesegment(es, vmx->host_state.es_sel);
2333#endif
2334
2335#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002336 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2337 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2338#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002339 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2340 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002341#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002342
2343#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002344 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2345 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002346 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002347#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002348 if (boot_cpu_has(X86_FEATURE_MPX))
2349 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002350 for (i = 0; i < vmx->save_nmsrs; ++i)
2351 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002352 vmx->guest_msrs[i].data,
2353 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002354}
2355
Avi Kivitya9b21b62008-06-24 11:48:49 +03002356static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002357{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002358 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002359 return;
2360
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002361 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002362 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002363#ifdef CONFIG_X86_64
2364 if (is_long_mode(&vmx->vcpu))
2365 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2366#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002367 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002368 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002369#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002370 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002371#else
2372 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002373#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002374 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002375 if (vmx->host_state.fs_reload_needed)
2376 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002377#ifdef CONFIG_X86_64
2378 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2379 loadsegment(ds, vmx->host_state.ds_sel);
2380 loadsegment(es, vmx->host_state.es_sel);
2381 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002382#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002383 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002384#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002385 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002386#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002387 if (vmx->host_state.msr_host_bndcfgs)
2388 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002389 /*
2390 * If the FPU is not active (through the host task or
2391 * the guest vcpu), then restore the cr0.TS bit.
2392 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002393 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002394 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002395 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002396}
2397
Avi Kivitya9b21b62008-06-24 11:48:49 +03002398static void vmx_load_host_state(struct vcpu_vmx *vmx)
2399{
2400 preempt_disable();
2401 __vmx_load_host_state(vmx);
2402 preempt_enable();
2403}
2404
Feng Wu28b835d2015-09-18 22:29:54 +08002405static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2406{
2407 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2408 struct pi_desc old, new;
2409 unsigned int dest;
2410
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002411 /*
2412 * In case of hot-plug or hot-unplug, we may have to undo
2413 * vmx_vcpu_pi_put even if there is no assigned device. And we
2414 * always keep PI.NDST up to date for simplicity: it makes the
2415 * code easier, and CPU migration is not a fast path.
2416 */
2417 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002418 return;
2419
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002420 /*
2421 * First handle the simple case where no cmpxchg is necessary; just
2422 * allow posting non-urgent interrupts.
2423 *
2424 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2425 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2426 * expects the VCPU to be on the blocked_vcpu_list that matches
2427 * PI.NDST.
2428 */
2429 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2430 vcpu->cpu == cpu) {
2431 pi_clear_sn(pi_desc);
2432 return;
2433 }
2434
2435 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002436 do {
2437 old.control = new.control = pi_desc->control;
2438
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002439 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002440
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002441 if (x2apic_enabled())
2442 new.ndst = dest;
2443 else
2444 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002445
Feng Wu28b835d2015-09-18 22:29:54 +08002446 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002447 } while (cmpxchg64(&pi_desc->control, old.control,
2448 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002449}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002450
Peter Feinerc95ba922016-08-17 09:36:47 -07002451static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2452{
2453 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2454 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2455}
2456
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457/*
2458 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2459 * vcpu mutex is already taken.
2460 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002461static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002463 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002464 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002465 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002467 if (!vmm_exclusive)
2468 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002469 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002470 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002472 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002473 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002474 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002475
2476 /*
2477 * Read loaded_vmcs->cpu should be before fetching
2478 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2479 * See the comments in __loaded_vmcs_clear().
2480 */
2481 smp_rmb();
2482
Nadav Har'Eld462b812011-05-24 15:26:10 +03002483 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2484 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002485 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002486 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002487 }
2488
2489 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2490 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2491 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002492 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002493 }
2494
2495 if (!already_loaded) {
2496 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2497 unsigned long sysenter_esp;
2498
2499 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002500
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 /*
2502 * Linux uses per-cpu TSS and GDT, so set these when switching
2503 * processors.
2504 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002505 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002506 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507
2508 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2509 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002510
Nadav Har'Eld462b812011-05-24 15:26:10 +03002511 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 }
Feng Wu28b835d2015-09-18 22:29:54 +08002513
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002514 /* Setup TSC multiplier */
2515 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002516 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2517 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002518
Feng Wu28b835d2015-09-18 22:29:54 +08002519 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002520 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002521}
2522
2523static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2524{
2525 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2526
2527 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002528 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2529 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002530 return;
2531
2532 /* Set SN when the vCPU is preempted */
2533 if (vcpu->preempted)
2534 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535}
2536
2537static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2538{
Feng Wu28b835d2015-09-18 22:29:54 +08002539 vmx_vcpu_pi_put(vcpu);
2540
Avi Kivitya9b21b62008-06-24 11:48:49 +03002541 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002542 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002543 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2544 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002545 kvm_cpu_vmxoff();
2546 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547}
2548
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002549static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2550{
Avi Kivity81231c62010-01-24 16:26:40 +02002551 ulong cr0;
2552
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002553 if (vcpu->fpu_active)
2554 return;
2555 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002556 cr0 = vmcs_readl(GUEST_CR0);
2557 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2558 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2559 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002560 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002561 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002562 if (is_guest_mode(vcpu))
2563 vcpu->arch.cr0_guest_owned_bits &=
2564 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002565 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002566}
2567
Avi Kivityedcafe32009-12-30 18:07:40 +02002568static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2569
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002570/*
2571 * Return the cr0 value that a nested guest would read. This is a combination
2572 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2573 * its hypervisor (cr0_read_shadow).
2574 */
2575static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2576{
2577 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2578 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2579}
2580static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2581{
2582 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2583 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2584}
2585
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002586static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2587{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002588 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2589 * set this *before* calling this function.
2590 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002591 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002592 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002593 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002594 vcpu->arch.cr0_guest_owned_bits = 0;
2595 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002596 if (is_guest_mode(vcpu)) {
2597 /*
2598 * L1's specified read shadow might not contain the TS bit,
2599 * so now that we turned on shadowing of this bit, we need to
2600 * set this bit of the shadow. Like in nested_vmx_run we need
2601 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2602 * up-to-date here because we just decached cr0.TS (and we'll
2603 * only update vmcs12->guest_cr0 on nested exit).
2604 */
2605 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2606 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2607 (vcpu->arch.cr0 & X86_CR0_TS);
2608 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2609 } else
2610 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002611}
2612
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2614{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002615 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002616
Avi Kivity6de12732011-03-07 12:51:22 +02002617 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2618 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2619 rflags = vmcs_readl(GUEST_RFLAGS);
2620 if (to_vmx(vcpu)->rmode.vm86_active) {
2621 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2622 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2623 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2624 }
2625 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002626 }
Avi Kivity6de12732011-03-07 12:51:22 +02002627 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628}
2629
2630static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2631{
Avi Kivity6de12732011-03-07 12:51:22 +02002632 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2633 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002634 if (to_vmx(vcpu)->rmode.vm86_active) {
2635 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002636 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002637 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 vmcs_writel(GUEST_RFLAGS, rflags);
2639}
2640
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002641static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2642{
2643 return to_vmx(vcpu)->guest_pkru;
2644}
2645
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002646static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002647{
2648 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2649 int ret = 0;
2650
2651 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002652 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002653 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002654 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002655
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002656 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002657}
2658
2659static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2660{
2661 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2662 u32 interruptibility = interruptibility_old;
2663
2664 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2665
Jan Kiszka48005f62010-02-19 19:38:07 +01002666 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002667 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002668 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002669 interruptibility |= GUEST_INTR_STATE_STI;
2670
2671 if ((interruptibility != interruptibility_old))
2672 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2673}
2674
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2676{
2677 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002679 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002681 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682
Glauber Costa2809f5d2009-05-12 16:21:05 -04002683 /* skipping an emulated instruction also counts */
2684 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685}
2686
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002687/*
2688 * KVM wants to inject page-faults which it got to the guest. This function
2689 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002690 */
Gleb Natapove011c662013-09-25 12:51:35 +03002691static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002692{
2693 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2694
Gleb Natapove011c662013-09-25 12:51:35 +03002695 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002696 return 0;
2697
Wanpeng Lia29fd272017-06-05 05:19:09 -07002698 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002699 vmcs_read32(VM_EXIT_INTR_INFO),
2700 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002701 return 1;
2702}
2703
Avi Kivity298101d2007-11-25 13:41:11 +02002704static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002705 bool has_error_code, u32 error_code,
2706 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002707{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002708 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002709 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002710
Gleb Natapove011c662013-09-25 12:51:35 +03002711 if (!reinject && is_guest_mode(vcpu) &&
2712 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002713 return;
2714
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002715 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002716 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002717 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2718 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002719
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002720 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002721 int inc_eip = 0;
2722 if (kvm_exception_is_soft(nr))
2723 inc_eip = vcpu->arch.event_exit_inst_len;
2724 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002725 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002726 return;
2727 }
2728
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002729 WARN_ON_ONCE(vmx->emulation_required);
2730
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002731 if (kvm_exception_is_soft(nr)) {
2732 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2733 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002734 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2735 } else
2736 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2737
2738 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002739}
2740
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002741static bool vmx_rdtscp_supported(void)
2742{
2743 return cpu_has_vmx_rdtscp();
2744}
2745
Mao, Junjiead756a12012-07-02 01:18:48 +00002746static bool vmx_invpcid_supported(void)
2747{
2748 return cpu_has_vmx_invpcid() && enable_ept;
2749}
2750
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751/*
Eddie Donga75beee2007-05-17 18:55:15 +03002752 * Swap MSR entry in host/guest MSR entry array.
2753 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002754static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002755{
Avi Kivity26bb0982009-09-07 11:14:12 +03002756 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002757
2758 tmp = vmx->guest_msrs[to];
2759 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2760 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002761}
2762
2763/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002764 * Set up the vmcs to automatically save and restore system
2765 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2766 * mode, as fiddling with msrs is very expensive.
2767 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002768static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002769{
Avi Kivity26bb0982009-09-07 11:14:12 +03002770 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002771
Eddie Donga75beee2007-05-17 18:55:15 +03002772 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002773#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002774 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002775 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002776 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002777 move_msr_up(vmx, index, save_nmsrs++);
2778 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002779 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002780 move_msr_up(vmx, index, save_nmsrs++);
2781 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002782 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002783 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002784 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002785 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002786 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002787 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002788 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002789 * if efer.sce is enabled.
2790 */
Brian Gerst8c065852010-07-17 09:03:26 -04002791 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002792 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002793 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002794 }
Eddie Donga75beee2007-05-17 18:55:15 +03002795#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002796 index = __find_msr_index(vmx, MSR_EFER);
2797 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002798 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002799
Avi Kivity26bb0982009-09-07 11:14:12 +03002800 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002801
Yang Zhang8d146952013-01-25 10:18:50 +08002802 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002803 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002804}
2805
2806/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002808 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2809 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002811static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812{
2813 u64 host_tsc, tsc_offset;
2814
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002815 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002817 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818}
2819
2820/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002821 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002823static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002825 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002826 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002827 * We're here if L1 chose not to trap WRMSR to TSC. According
2828 * to the spec, this should set L1's TSC; The offset that L1
2829 * set for L2 remains unchanged, and still needs to be added
2830 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002831 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002832 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002833 /* recalculate vmcs02.TSC_OFFSET: */
2834 vmcs12 = get_vmcs12(vcpu);
2835 vmcs_write64(TSC_OFFSET, offset +
2836 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2837 vmcs12->tsc_offset : 0));
2838 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002839 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2840 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002841 vmcs_write64(TSC_OFFSET, offset);
2842 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843}
2844
Nadav Har'El801d3422011-05-25 23:02:23 +03002845static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2846{
2847 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2848 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2849}
2850
2851/*
2852 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2853 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2854 * all guests if the "nested" module option is off, and can also be disabled
2855 * for a single guest by disabling its VMX cpuid bit.
2856 */
2857static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2858{
2859 return nested && guest_cpuid_has_vmx(vcpu);
2860}
2861
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002863 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2864 * returned for the various VMX controls MSRs when nested VMX is enabled.
2865 * The same values should also be used to verify that vmcs12 control fields are
2866 * valid during nested entry from L1 to L2.
2867 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2868 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2869 * bit in the high half is on if the corresponding bit in the control field
2870 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002871 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002872static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002873{
2874 /*
2875 * Note that as a general rule, the high half of the MSRs (bits in
2876 * the control fields which may be 1) should be initialized by the
2877 * intersection of the underlying hardware's MSR (i.e., features which
2878 * can be supported) and the list of features we want to expose -
2879 * because they are known to be properly supported in our code.
2880 * Also, usually, the low half of the MSRs (bits which must be 1) can
2881 * be set to 0, meaning that L1 may turn off any of these bits. The
2882 * reason is that if one of these bits is necessary, it will appear
2883 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2884 * fields of vmcs01 and vmcs02, will turn these bits off - and
2885 * nested_vmx_exit_handled() will not pass related exits to L1.
2886 * These rules have exceptions below.
2887 */
2888
2889 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002890 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002891 vmx->nested.nested_vmx_pinbased_ctls_low,
2892 vmx->nested.nested_vmx_pinbased_ctls_high);
2893 vmx->nested.nested_vmx_pinbased_ctls_low |=
2894 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2895 vmx->nested.nested_vmx_pinbased_ctls_high &=
2896 PIN_BASED_EXT_INTR_MASK |
2897 PIN_BASED_NMI_EXITING |
2898 PIN_BASED_VIRTUAL_NMIS;
2899 vmx->nested.nested_vmx_pinbased_ctls_high |=
2900 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002901 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002902 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002903 vmx->nested.nested_vmx_pinbased_ctls_high |=
2904 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002905
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002906 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002907 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002908 vmx->nested.nested_vmx_exit_ctls_low,
2909 vmx->nested.nested_vmx_exit_ctls_high);
2910 vmx->nested.nested_vmx_exit_ctls_low =
2911 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002912
Wincy Vanb9c237b2015-02-03 23:56:30 +08002913 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002914#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002915 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002916#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002917 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002918 vmx->nested.nested_vmx_exit_ctls_high |=
2919 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002920 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002921 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2922
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002923 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002924 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002925
Jan Kiszka2996fca2014-06-16 13:59:43 +02002926 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002927 vmx->nested.nested_vmx_true_exit_ctls_low =
2928 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002929 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2930
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002931 /* entry controls */
2932 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002933 vmx->nested.nested_vmx_entry_ctls_low,
2934 vmx->nested.nested_vmx_entry_ctls_high);
2935 vmx->nested.nested_vmx_entry_ctls_low =
2936 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2937 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002938#ifdef CONFIG_X86_64
2939 VM_ENTRY_IA32E_MODE |
2940#endif
2941 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002942 vmx->nested.nested_vmx_entry_ctls_high |=
2943 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002944 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002945 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002946
Jan Kiszka2996fca2014-06-16 13:59:43 +02002947 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002948 vmx->nested.nested_vmx_true_entry_ctls_low =
2949 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002950 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2951
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002952 /* cpu-based controls */
2953 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002954 vmx->nested.nested_vmx_procbased_ctls_low,
2955 vmx->nested.nested_vmx_procbased_ctls_high);
2956 vmx->nested.nested_vmx_procbased_ctls_low =
2957 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2958 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002959 CPU_BASED_VIRTUAL_INTR_PENDING |
2960 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002961 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2962 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2963 CPU_BASED_CR3_STORE_EXITING |
2964#ifdef CONFIG_X86_64
2965 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2966#endif
2967 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002968 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2969 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2970 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2971 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002972 /*
2973 * We can allow some features even when not supported by the
2974 * hardware. For example, L1 can specify an MSR bitmap - and we
2975 * can use it to avoid exits to L1 - even when L0 runs L2
2976 * without MSR bitmaps.
2977 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002978 vmx->nested.nested_vmx_procbased_ctls_high |=
2979 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002980 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002981
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002982 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002983 vmx->nested.nested_vmx_true_procbased_ctls_low =
2984 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002985 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2986
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002987 /* secondary cpu-based controls */
2988 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002989 vmx->nested.nested_vmx_secondary_ctls_low,
2990 vmx->nested.nested_vmx_secondary_ctls_high);
2991 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2992 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002993 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002994 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002995 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002996 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002997 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002998 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002999 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07003000 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003001
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003002 if (enable_ept) {
3003 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003004 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003005 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003006 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01003007 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
3008 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003009 if (cpu_has_vmx_ept_execute_only())
3010 vmx->nested.nested_vmx_ept_caps |=
3011 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003012 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04003013 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3014 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003015 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08003016 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003017
Paolo Bonzinief697a72016-03-18 16:58:38 +01003018 /*
3019 * Old versions of KVM use the single-context version without
3020 * checking for support, so declare that it is supported even
3021 * though it is treated as global context. The alternative is
3022 * not failing the single-context invvpid, and it is worse.
3023 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07003024 if (enable_vpid)
3025 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01003026 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07003027 else
3028 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003029
Radim Krčmář0790ec12015-03-17 14:02:32 +01003030 if (enable_unrestricted_guest)
3031 vmx->nested.nested_vmx_secondary_ctls_high |=
3032 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3033
Jan Kiszkac18911a2013-03-13 16:06:41 +01003034 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003035 rdmsr(MSR_IA32_VMX_MISC,
3036 vmx->nested.nested_vmx_misc_low,
3037 vmx->nested.nested_vmx_misc_high);
3038 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3039 vmx->nested.nested_vmx_misc_low |=
3040 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003041 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003042 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003043}
3044
3045static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3046{
3047 /*
3048 * Bits 0 in high must be 0, and bits 1 in low must be 1.
3049 */
3050 return ((control & high) | low) == control;
3051}
3052
3053static inline u64 vmx_control_msr(u32 low, u32 high)
3054{
3055 return low | ((u64)high << 32);
3056}
3057
Jan Kiszkacae50132014-01-04 18:47:22 +01003058/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003059static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3060{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003061 struct vcpu_vmx *vmx = to_vmx(vcpu);
3062
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003063 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003064 case MSR_IA32_VMX_BASIC:
3065 /*
3066 * This MSR reports some information about VMX support. We
3067 * should return information about the VMX we emulate for the
3068 * guest, and the VMCS structure we give it - not about the
3069 * VMX support of the underlying hardware.
3070 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003071 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003072 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3073 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003074 if (cpu_has_vmx_basic_inout())
3075 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003076 break;
3077 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3078 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003079 *pdata = vmx_control_msr(
3080 vmx->nested.nested_vmx_pinbased_ctls_low,
3081 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003082 break;
3083 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003084 *pdata = vmx_control_msr(
3085 vmx->nested.nested_vmx_true_procbased_ctls_low,
3086 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02003087 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003088 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003089 *pdata = vmx_control_msr(
3090 vmx->nested.nested_vmx_procbased_ctls_low,
3091 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003092 break;
3093 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003094 *pdata = vmx_control_msr(
3095 vmx->nested.nested_vmx_true_exit_ctls_low,
3096 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003097 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003098 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003099 *pdata = vmx_control_msr(
3100 vmx->nested.nested_vmx_exit_ctls_low,
3101 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003102 break;
3103 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003104 *pdata = vmx_control_msr(
3105 vmx->nested.nested_vmx_true_entry_ctls_low,
3106 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003107 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003108 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003109 *pdata = vmx_control_msr(
3110 vmx->nested.nested_vmx_entry_ctls_low,
3111 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003112 break;
3113 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003114 *pdata = vmx_control_msr(
3115 vmx->nested.nested_vmx_misc_low,
3116 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003117 break;
3118 /*
3119 * These MSRs specify bits which the guest must keep fixed (on or off)
3120 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3121 * We picked the standard core2 setting.
3122 */
3123#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3124#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3125 case MSR_IA32_VMX_CR0_FIXED0:
3126 *pdata = VMXON_CR0_ALWAYSON;
3127 break;
3128 case MSR_IA32_VMX_CR0_FIXED1:
3129 *pdata = -1ULL;
3130 break;
3131 case MSR_IA32_VMX_CR4_FIXED0:
3132 *pdata = VMXON_CR4_ALWAYSON;
3133 break;
3134 case MSR_IA32_VMX_CR4_FIXED1:
3135 *pdata = -1ULL;
3136 break;
3137 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003138 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003139 break;
3140 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003141 *pdata = vmx_control_msr(
3142 vmx->nested.nested_vmx_secondary_ctls_low,
3143 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003144 break;
3145 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003146 *pdata = vmx->nested.nested_vmx_ept_caps |
3147 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003148 break;
3149 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003150 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003151 }
3152
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003153 return 0;
3154}
3155
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003156static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3157 uint64_t val)
3158{
3159 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3160
3161 return !(val & ~valid_bits);
3162}
3163
Tom Lendacky62d88fc2018-02-21 13:39:51 -06003164static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3165{
3166 return 1;
3167}
3168
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003169/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 * Reads an msr value (of 'msr_index') into 'pdata'.
3171 * Returns 0 on success, non-0 otherwise.
3172 * Assumes vcpu_load() was already called.
3173 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003174static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175{
Avi Kivity26bb0982009-09-07 11:14:12 +03003176 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003178 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003179#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003181 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 break;
3183 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003184 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003186 case MSR_KERNEL_GS_BASE:
3187 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003188 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003189 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003190#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003192 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303193 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003194 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003196 case MSR_IA32_SPEC_CTRL:
3197 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003198 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003199 return 1;
3200
3201 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3202 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003204 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 break;
3206 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003207 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 break;
3209 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003210 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003212 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003213 if (!kvm_mpx_supported() ||
3214 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003215 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003216 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003217 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003218 case MSR_IA32_MCG_EXT_CTL:
3219 if (!msr_info->host_initiated &&
3220 !(to_vmx(vcpu)->msr_ia32_feature_control &
3221 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003222 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003223 msr_info->data = vcpu->arch.mcg_ext_ctl;
3224 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003225 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003226 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003227 break;
3228 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3229 if (!nested_vmx_allowed(vcpu))
3230 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003231 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003232 case MSR_IA32_XSS:
3233 if (!vmx_xsaves_supported())
3234 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003235 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003236 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003237 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003238 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003239 return 1;
3240 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003242 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003243 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003244 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003245 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003247 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 }
3249
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 return 0;
3251}
3252
Jan Kiszkacae50132014-01-04 18:47:22 +01003253static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3254
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255/*
3256 * Writes msr value into into the appropriate "register".
3257 * Returns 0 on success, non-0 otherwise.
3258 * Assumes vcpu_load() was already called.
3259 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003260static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003262 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003263 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003264 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003265 u32 msr_index = msr_info->index;
3266 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003267
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003269 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003270 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003271 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003272#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003274 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 vmcs_writel(GUEST_FS_BASE, data);
3276 break;
3277 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003278 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279 vmcs_writel(GUEST_GS_BASE, data);
3280 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003281 case MSR_KERNEL_GS_BASE:
3282 vmx_load_host_state(vmx);
3283 vmx->msr_guest_kernel_gs_base = data;
3284 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285#endif
3286 case MSR_IA32_SYSENTER_CS:
3287 vmcs_write32(GUEST_SYSENTER_CS, data);
3288 break;
3289 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003290 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 break;
3292 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003293 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003295 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003296 if (!kvm_mpx_supported() ||
3297 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003298 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003299 if (is_noncanonical_address(data & PAGE_MASK) ||
3300 (data & MSR_IA32_BNDCFGS_RSVD))
3301 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003302 vmcs_write64(GUEST_BNDCFGS, data);
3303 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303304 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003305 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003307 case MSR_IA32_SPEC_CTRL:
3308 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003309 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003310 return 1;
3311
3312 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003313 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003314 return 1;
3315
3316 vmx->spec_ctrl = data;
3317
3318 if (!data)
3319 break;
3320
3321 /*
3322 * For non-nested:
3323 * When it's written (to non-zero) for the first time, pass
3324 * it through.
3325 *
3326 * For nested:
3327 * The handling of the MSR bitmap for L2 guests is done in
3328 * nested_vmx_merge_msr_bitmap. We should not touch the
3329 * vmcs02.msr_bitmap here since it gets completely overwritten
3330 * in the merging. We update the vmcs01 here for L1 as well
3331 * since it will end up touching the MSR anyway now.
3332 */
3333 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3334 MSR_IA32_SPEC_CTRL,
3335 MSR_TYPE_RW);
3336 break;
Ashok Raj70131292018-02-01 22:59:43 +01003337 case MSR_IA32_PRED_CMD:
3338 if (!msr_info->host_initiated &&
3339 !guest_cpuid_has_ibpb(vcpu))
3340 return 1;
3341
3342 if (data & ~PRED_CMD_IBPB)
3343 return 1;
3344
3345 if (!data)
3346 break;
3347
3348 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3349
3350 /*
3351 * For non-nested:
3352 * When it's written (to non-zero) for the first time, pass
3353 * it through.
3354 *
3355 * For nested:
3356 * The handling of the MSR bitmap for L2 guests is done in
3357 * nested_vmx_merge_msr_bitmap. We should not touch the
3358 * vmcs02.msr_bitmap here since it gets completely overwritten
3359 * in the merging.
3360 */
3361 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3362 MSR_TYPE_W);
3363 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003364 case MSR_IA32_CR_PAT:
3365 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003366 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3367 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003368 vmcs_write64(GUEST_IA32_PAT, data);
3369 vcpu->arch.pat = data;
3370 break;
3371 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003372 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003373 break;
Will Auldba904632012-11-29 12:42:50 -08003374 case MSR_IA32_TSC_ADJUST:
3375 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003376 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003377 case MSR_IA32_MCG_EXT_CTL:
3378 if ((!msr_info->host_initiated &&
3379 !(to_vmx(vcpu)->msr_ia32_feature_control &
3380 FEATURE_CONTROL_LMCE)) ||
3381 (data & ~MCG_EXT_CTL_LMCE_EN))
3382 return 1;
3383 vcpu->arch.mcg_ext_ctl = data;
3384 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003385 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003386 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003387 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003388 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3389 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003390 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003391 if (msr_info->host_initiated && data == 0)
3392 vmx_leave_nested(vcpu);
3393 break;
3394 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3395 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003396 case MSR_IA32_XSS:
3397 if (!vmx_xsaves_supported())
3398 return 1;
3399 /*
3400 * The only supported bit as of Skylake is bit 8, but
3401 * it is not supported on KVM.
3402 */
3403 if (data != 0)
3404 return 1;
3405 vcpu->arch.ia32_xss = data;
3406 if (vcpu->arch.ia32_xss != host_xss)
3407 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04003408 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08003409 else
3410 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3411 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003412 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003413 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003414 return 1;
3415 /* Check reserved bit, higher 32 bits should be zero */
3416 if ((data >> 32) != 0)
3417 return 1;
3418 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003420 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003421 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003422 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003423 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003424 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3425 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003426 ret = kvm_set_shared_msr(msr->index, msr->data,
3427 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003428 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003429 if (ret)
3430 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003431 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003432 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003434 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435 }
3436
Eddie Dong2cc51562007-05-21 07:28:09 +03003437 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438}
3439
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003440static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003442 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3443 switch (reg) {
3444 case VCPU_REGS_RSP:
3445 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3446 break;
3447 case VCPU_REGS_RIP:
3448 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3449 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003450 case VCPU_EXREG_PDPTR:
3451 if (enable_ept)
3452 ept_save_pdptrs(vcpu);
3453 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003454 default:
3455 break;
3456 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457}
3458
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459static __init int cpu_has_kvm_support(void)
3460{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003461 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462}
3463
3464static __init int vmx_disabled_by_bios(void)
3465{
3466 u64 msr;
3467
3468 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003469 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003470 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003471 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3472 && tboot_enabled())
3473 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003474 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003475 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003476 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003477 && !tboot_enabled()) {
3478 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003479 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003480 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003481 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003482 /* launched w/o TXT and VMX disabled */
3483 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3484 && !tboot_enabled())
3485 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003486 }
3487
3488 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489}
3490
Dongxiao Xu7725b892010-05-11 18:29:38 +08003491static void kvm_cpu_vmxon(u64 addr)
3492{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003493 intel_pt_handle_vmx(1);
3494
Dongxiao Xu7725b892010-05-11 18:29:38 +08003495 asm volatile (ASM_VMX_VMXON_RAX
3496 : : "a"(&addr), "m"(addr)
3497 : "memory", "cc");
3498}
3499
Radim Krčmář13a34e02014-08-28 15:13:03 +02003500static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501{
3502 int cpu = raw_smp_processor_id();
3503 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003504 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003506 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003507 return -EBUSY;
3508
Nadav Har'Eld462b812011-05-24 15:26:10 +03003509 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003510 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3511 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003512
3513 /*
3514 * Now we can enable the vmclear operation in kdump
3515 * since the loaded_vmcss_on_cpu list on this cpu
3516 * has been initialized.
3517 *
3518 * Though the cpu is not in VMX operation now, there
3519 * is no problem to enable the vmclear operation
3520 * for the loaded_vmcss_on_cpu list is empty!
3521 */
3522 crash_enable_local_vmclear(cpu);
3523
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003525
3526 test_bits = FEATURE_CONTROL_LOCKED;
3527 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3528 if (tboot_enabled())
3529 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3530
3531 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003533 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3534 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003535 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003536
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003537 if (vmm_exclusive) {
3538 kvm_cpu_vmxon(phys_addr);
3539 ept_sync_global();
3540 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003541
Christoph Lameter89cbc762014-08-17 12:30:40 -05003542 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003543
Alexander Graf10474ae2009-09-15 11:37:46 +02003544 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545}
3546
Nadav Har'Eld462b812011-05-24 15:26:10 +03003547static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003548{
3549 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003550 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003551
Nadav Har'Eld462b812011-05-24 15:26:10 +03003552 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3553 loaded_vmcss_on_cpu_link)
3554 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003555}
3556
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003557
3558/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3559 * tricks.
3560 */
3561static void kvm_cpu_vmxoff(void)
3562{
3563 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003564
3565 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003566}
3567
Radim Krčmář13a34e02014-08-28 15:13:03 +02003568static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003570 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003571 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003572 kvm_cpu_vmxoff();
3573 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003574 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575}
3576
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003577static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003578 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003579{
3580 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003581 u32 ctl = ctl_min | ctl_opt;
3582
3583 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3584
3585 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3586 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3587
3588 /* Ensure minimum (required) set of control bits are supported. */
3589 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003590 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003591
3592 *result = ctl;
3593 return 0;
3594}
3595
Avi Kivity110312c2010-12-21 12:54:20 +02003596static __init bool allow_1_setting(u32 msr, u32 ctl)
3597{
3598 u32 vmx_msr_low, vmx_msr_high;
3599
3600 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3601 return vmx_msr_high & ctl;
3602}
3603
Yang, Sheng002c7f72007-07-31 14:23:01 +03003604static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003605{
3606 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003607 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003608 u32 _pin_based_exec_control = 0;
3609 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003610 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003611 u32 _vmexit_control = 0;
3612 u32 _vmentry_control = 0;
3613
Raghavendra K T10166742012-02-07 23:19:20 +05303614 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003615#ifdef CONFIG_X86_64
3616 CPU_BASED_CR8_LOAD_EXITING |
3617 CPU_BASED_CR8_STORE_EXITING |
3618#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003619 CPU_BASED_CR3_LOAD_EXITING |
3620 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003621 CPU_BASED_USE_IO_BITMAPS |
3622 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003623 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003624 CPU_BASED_MWAIT_EXITING |
3625 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003626 CPU_BASED_INVLPG_EXITING |
3627 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003628
Sheng Yangf78e0e22007-10-29 09:40:42 +08003629 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003630 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003631 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003632 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3633 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003634 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003635#ifdef CONFIG_X86_64
3636 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3637 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3638 ~CPU_BASED_CR8_STORE_EXITING;
3639#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003640 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003641 min2 = 0;
3642 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003643 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003644 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003645 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003646 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003647 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003648 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003649 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003650 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003651 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003652 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003653 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003654 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003655 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003656 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003657 if (adjust_vmx_controls(min2, opt2,
3658 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003659 &_cpu_based_2nd_exec_control) < 0)
3660 return -EIO;
3661 }
3662#ifndef CONFIG_X86_64
3663 if (!(_cpu_based_2nd_exec_control &
3664 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3665 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3666#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003667
3668 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3669 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003670 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003671 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3672 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003673
Sheng Yangd56f5462008-04-25 10:13:16 +08003674 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003675 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3676 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003677 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3678 CPU_BASED_CR3_STORE_EXITING |
3679 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003680 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3681 vmx_capability.ept, vmx_capability.vpid);
3682 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003683
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003684 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003685#ifdef CONFIG_X86_64
3686 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3687#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003688 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003689 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003690 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3691 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003692 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003693
Yang Zhang01e439b2013-04-11 19:25:12 +08003694 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003695 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3696 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003697 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3698 &_pin_based_exec_control) < 0)
3699 return -EIO;
3700
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003701 if (cpu_has_broken_vmx_preemption_timer())
3702 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003703 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003704 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003705 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3706
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003707 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003708 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003709 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3710 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003711 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003713 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003714
3715 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3716 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003717 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003718
3719#ifdef CONFIG_X86_64
3720 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3721 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003722 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003723#endif
3724
3725 /* Require Write-Back (WB) memory type for VMCS accesses. */
3726 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003727 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003728
Yang, Sheng002c7f72007-07-31 14:23:01 +03003729 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003730 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003731 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003732 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003733
Yang, Sheng002c7f72007-07-31 14:23:01 +03003734 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3735 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003736 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003737 vmcs_conf->vmexit_ctrl = _vmexit_control;
3738 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003739
Avi Kivity110312c2010-12-21 12:54:20 +02003740 cpu_has_load_ia32_efer =
3741 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3742 VM_ENTRY_LOAD_IA32_EFER)
3743 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3744 VM_EXIT_LOAD_IA32_EFER);
3745
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003746 cpu_has_load_perf_global_ctrl =
3747 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3748 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3749 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3750 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3751
3752 /*
3753 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003754 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003755 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3756 *
3757 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3758 *
3759 * AAK155 (model 26)
3760 * AAP115 (model 30)
3761 * AAT100 (model 37)
3762 * BC86,AAY89,BD102 (model 44)
3763 * BA97 (model 46)
3764 *
3765 */
3766 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3767 switch (boot_cpu_data.x86_model) {
3768 case 26:
3769 case 30:
3770 case 37:
3771 case 44:
3772 case 46:
3773 cpu_has_load_perf_global_ctrl = false;
3774 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3775 "does not work properly. Using workaround\n");
3776 break;
3777 default:
3778 break;
3779 }
3780 }
3781
Borislav Petkov782511b2016-04-04 22:25:03 +02003782 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003783 rdmsrl(MSR_IA32_XSS, host_xss);
3784
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003785 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003786}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787
3788static struct vmcs *alloc_vmcs_cpu(int cpu)
3789{
3790 int node = cpu_to_node(cpu);
3791 struct page *pages;
3792 struct vmcs *vmcs;
3793
Vlastimil Babka96db8002015-09-08 15:03:50 -07003794 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795 if (!pages)
3796 return NULL;
3797 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003798 memset(vmcs, 0, vmcs_config.size);
3799 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800 return vmcs;
3801}
3802
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803static void free_vmcs(struct vmcs *vmcs)
3804{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003805 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806}
3807
Nadav Har'Eld462b812011-05-24 15:26:10 +03003808/*
3809 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3810 */
3811static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3812{
3813 if (!loaded_vmcs->vmcs)
3814 return;
3815 loaded_vmcs_clear(loaded_vmcs);
3816 free_vmcs(loaded_vmcs->vmcs);
3817 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003818 if (loaded_vmcs->msr_bitmap)
3819 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003820 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003821}
3822
Paolo Bonziniff546f92018-01-11 12:16:15 +01003823static struct vmcs *alloc_vmcs(void)
3824{
3825 return alloc_vmcs_cpu(raw_smp_processor_id());
3826}
3827
3828static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3829{
3830 loaded_vmcs->vmcs = alloc_vmcs();
3831 if (!loaded_vmcs->vmcs)
3832 return -ENOMEM;
3833
3834 loaded_vmcs->shadow_vmcs = NULL;
3835 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003836
3837 if (cpu_has_vmx_msr_bitmap()) {
3838 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3839 if (!loaded_vmcs->msr_bitmap)
3840 goto out_vmcs;
3841 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3842 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003843 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003844
3845out_vmcs:
3846 free_loaded_vmcs(loaded_vmcs);
3847 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003848}
3849
Sam Ravnborg39959582007-06-01 00:47:13 -07003850static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851{
3852 int cpu;
3853
Zachary Amsden3230bb42009-09-29 11:38:37 -10003854 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003856 per_cpu(vmxarea, cpu) = NULL;
3857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858}
3859
Bandan Dasfe2b2012014-04-21 15:20:14 -04003860static void init_vmcs_shadow_fields(void)
3861{
3862 int i, j;
3863
3864 /* No checks for read only fields yet */
3865
3866 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3867 switch (shadow_read_write_fields[i]) {
3868 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003869 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003870 continue;
3871 break;
3872 default:
3873 break;
3874 }
3875
3876 if (j < i)
3877 shadow_read_write_fields[j] =
3878 shadow_read_write_fields[i];
3879 j++;
3880 }
3881 max_shadow_read_write_fields = j;
3882
3883 /* shadowed fields guest access without vmexit */
3884 for (i = 0; i < max_shadow_read_write_fields; i++) {
3885 clear_bit(shadow_read_write_fields[i],
3886 vmx_vmwrite_bitmap);
3887 clear_bit(shadow_read_write_fields[i],
3888 vmx_vmread_bitmap);
3889 }
3890 for (i = 0; i < max_shadow_read_only_fields; i++)
3891 clear_bit(shadow_read_only_fields[i],
3892 vmx_vmread_bitmap);
3893}
3894
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895static __init int alloc_kvm_area(void)
3896{
3897 int cpu;
3898
Zachary Amsden3230bb42009-09-29 11:38:37 -10003899 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900 struct vmcs *vmcs;
3901
3902 vmcs = alloc_vmcs_cpu(cpu);
3903 if (!vmcs) {
3904 free_kvm_area();
3905 return -ENOMEM;
3906 }
3907
3908 per_cpu(vmxarea, cpu) = vmcs;
3909 }
3910 return 0;
3911}
3912
Gleb Natapov14168782013-01-21 15:36:49 +02003913static bool emulation_required(struct kvm_vcpu *vcpu)
3914{
3915 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3916}
3917
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003918static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003919 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003921 if (!emulate_invalid_guest_state) {
3922 /*
3923 * CS and SS RPL should be equal during guest entry according
3924 * to VMX spec, but in reality it is not always so. Since vcpu
3925 * is in the middle of the transition from real mode to
3926 * protected mode it is safe to assume that RPL 0 is a good
3927 * default value.
3928 */
3929 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003930 save->selector &= ~SEGMENT_RPL_MASK;
3931 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003932 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003934 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935}
3936
3937static void enter_pmode(struct kvm_vcpu *vcpu)
3938{
3939 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941
Gleb Natapovd99e4152012-12-20 16:57:45 +02003942 /*
3943 * Update real mode segment cache. It may be not up-to-date if sement
3944 * register was written while vcpu was in a guest mode.
3945 */
3946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3952
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003953 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954
Avi Kivity2fb92db2011-04-27 19:42:18 +03003955 vmx_segment_cache_clear(vmx);
3956
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003957 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958
3959 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003960 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3961 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 vmcs_writel(GUEST_RFLAGS, flags);
3963
Rusty Russell66aee912007-07-17 23:34:16 +10003964 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3965 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966
3967 update_exception_bitmap(vcpu);
3968
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003969 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3970 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3971 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3972 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3973 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3974 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975}
3976
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003977static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978{
Mathias Krause772e0312012-08-30 01:30:19 +02003979 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003980 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981
Gleb Natapovd99e4152012-12-20 16:57:45 +02003982 var.dpl = 0x3;
3983 if (seg == VCPU_SREG_CS)
3984 var.type = 0x3;
3985
3986 if (!emulate_invalid_guest_state) {
3987 var.selector = var.base >> 4;
3988 var.base = var.base & 0xffff0;
3989 var.limit = 0xffff;
3990 var.g = 0;
3991 var.db = 0;
3992 var.present = 1;
3993 var.s = 1;
3994 var.l = 0;
3995 var.unusable = 0;
3996 var.type = 0x3;
3997 var.avl = 0;
3998 if (save->base & 0xf)
3999 printk_once(KERN_WARNING "kvm: segment base is not "
4000 "paragraph aligned when entering "
4001 "protected mode (seg=%d)", seg);
4002 }
4003
4004 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05004005 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004006 vmcs_write32(sf->limit, var.limit);
4007 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008}
4009
4010static void enter_rmode(struct kvm_vcpu *vcpu)
4011{
4012 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004013 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004022
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004023 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024
Gleb Natapov776e58e2011-03-13 12:34:27 +02004025 /*
4026 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004027 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004028 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004029 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004030 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4031 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004032
Avi Kivity2fb92db2011-04-27 19:42:18 +03004033 vmx_segment_cache_clear(vmx);
4034
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004035 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004036 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4038
4039 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004040 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004041
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004042 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043
4044 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004045 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046 update_exception_bitmap(vcpu);
4047
Gleb Natapovd99e4152012-12-20 16:57:45 +02004048 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4049 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4050 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4051 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4052 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4053 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004054
Eddie Dong8668a3c2007-10-10 14:26:45 +08004055 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056}
4057
Amit Shah401d10d2009-02-20 22:53:37 +05304058static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4059{
4060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004061 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4062
4063 if (!msr)
4064 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304065
Avi Kivity44ea2b12009-09-06 15:55:37 +03004066 /*
4067 * Force kernel_gs_base reloading before EFER changes, as control
4068 * of this msr depends on is_long_mode().
4069 */
4070 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004071 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304072 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004073 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304074 msr->data = efer;
4075 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004076 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304077
4078 msr->data = efer & ~EFER_LME;
4079 }
4080 setup_msrs(vmx);
4081}
4082
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004083#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084
4085static void enter_lmode(struct kvm_vcpu *vcpu)
4086{
4087 u32 guest_tr_ar;
4088
Avi Kivity2fb92db2011-04-27 19:42:18 +03004089 vmx_segment_cache_clear(to_vmx(vcpu));
4090
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004092 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004093 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4094 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004096 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4097 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 }
Avi Kivityda38f432010-07-06 11:30:49 +03004099 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100}
4101
4102static void exit_lmode(struct kvm_vcpu *vcpu)
4103{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004105 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106}
4107
4108#endif
4109
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004110static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004111{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004112 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004113 if (enable_ept) {
4114 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4115 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004116 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004117 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004118}
4119
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004120static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4121{
4122 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4123}
4124
Jim Mattson8386ff52017-03-16 13:53:59 -07004125static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4126{
4127 if (enable_ept)
4128 vmx_flush_tlb(vcpu);
4129}
4130
Avi Kivitye8467fd2009-12-29 18:43:06 +02004131static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4132{
4133 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4134
4135 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4136 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4137}
4138
Avi Kivityaff48ba2010-12-05 18:56:11 +02004139static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4140{
4141 if (enable_ept && is_paging(vcpu))
4142 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4143 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4144}
4145
Anthony Liguori25c4c272007-04-27 09:29:21 +03004146static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004147{
Avi Kivityfc78f512009-12-07 12:16:48 +02004148 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4149
4150 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4151 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004152}
4153
Sheng Yang14394422008-04-28 12:24:45 +08004154static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4155{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004156 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4157
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004158 if (!test_bit(VCPU_EXREG_PDPTR,
4159 (unsigned long *)&vcpu->arch.regs_dirty))
4160 return;
4161
Sheng Yang14394422008-04-28 12:24:45 +08004162 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004163 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4164 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4165 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4166 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004167 }
4168}
4169
Avi Kivity8f5d5492009-05-31 18:41:29 +03004170static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4171{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004172 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4173
Avi Kivity8f5d5492009-05-31 18:41:29 +03004174 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004175 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4176 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4177 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4178 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004179 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004180
4181 __set_bit(VCPU_EXREG_PDPTR,
4182 (unsigned long *)&vcpu->arch.regs_avail);
4183 __set_bit(VCPU_EXREG_PDPTR,
4184 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004185}
4186
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004187static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004188
4189static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4190 unsigned long cr0,
4191 struct kvm_vcpu *vcpu)
4192{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004193 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4194 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004195 if (!(cr0 & X86_CR0_PG)) {
4196 /* From paging/starting to nonpaging */
4197 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004198 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004199 (CPU_BASED_CR3_LOAD_EXITING |
4200 CPU_BASED_CR3_STORE_EXITING));
4201 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004202 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004203 } else if (!is_paging(vcpu)) {
4204 /* From nonpaging to paging */
4205 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004206 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004207 ~(CPU_BASED_CR3_LOAD_EXITING |
4208 CPU_BASED_CR3_STORE_EXITING));
4209 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004210 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004211 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004212
4213 if (!(cr0 & X86_CR0_WP))
4214 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004215}
4216
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4218{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004220 unsigned long hw_cr0;
4221
Gleb Natapov50378782013-02-04 16:00:28 +02004222 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004223 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004224 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004225 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004226 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004227
Gleb Natapov218e7632013-01-21 15:36:45 +02004228 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4229 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230
Gleb Natapov218e7632013-01-21 15:36:45 +02004231 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4232 enter_rmode(vcpu);
4233 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004235#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004236 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004237 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004238 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004239 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240 exit_lmode(vcpu);
4241 }
4242#endif
4243
Avi Kivity089d0342009-03-23 18:26:32 +02004244 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004245 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4246
Avi Kivity02daab22009-12-30 12:40:26 +02004247 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004248 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004249
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004251 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004252 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004253
4254 /* depends on vcpu->arch.cr0 to be set to a new value */
4255 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256}
4257
Sheng Yang14394422008-04-28 12:24:45 +08004258static u64 construct_eptp(unsigned long root_hpa)
4259{
4260 u64 eptp;
4261
4262 /* TODO write the value reading from MSR */
4263 eptp = VMX_EPT_DEFAULT_MT |
4264 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004265 if (enable_ept_ad_bits)
4266 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004267 eptp |= (root_hpa & PAGE_MASK);
4268
4269 return eptp;
4270}
4271
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4273{
Sheng Yang14394422008-04-28 12:24:45 +08004274 unsigned long guest_cr3;
4275 u64 eptp;
4276
4277 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004278 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004279 eptp = construct_eptp(cr3);
4280 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004281 if (is_paging(vcpu) || is_guest_mode(vcpu))
4282 guest_cr3 = kvm_read_cr3(vcpu);
4283 else
4284 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004285 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004286 }
4287
Sheng Yang2384d2b2008-01-17 15:14:33 +08004288 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004289 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004290}
4291
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004292static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004294 /*
4295 * Pass through host's Machine Check Enable value to hw_cr4, which
4296 * is in force while we are in guest mode. Do not let guests control
4297 * this bit, even if host CR4.MCE == 0.
4298 */
4299 unsigned long hw_cr4 =
4300 (cr4_read_shadow() & X86_CR4_MCE) |
4301 (cr4 & ~X86_CR4_MCE) |
4302 (to_vmx(vcpu)->rmode.vm86_active ?
4303 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004304
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004305 if (cr4 & X86_CR4_VMXE) {
4306 /*
4307 * To use VMXON (and later other VMX instructions), a guest
4308 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4309 * So basically the check on whether to allow nested VMX
4310 * is here.
4311 */
4312 if (!nested_vmx_allowed(vcpu))
4313 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004314 }
4315 if (to_vmx(vcpu)->nested.vmxon &&
4316 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004317 return 1;
4318
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004319 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004320 if (enable_ept) {
4321 if (!is_paging(vcpu)) {
4322 hw_cr4 &= ~X86_CR4_PAE;
4323 hw_cr4 |= X86_CR4_PSE;
4324 } else if (!(cr4 & X86_CR4_PAE)) {
4325 hw_cr4 &= ~X86_CR4_PAE;
4326 }
4327 }
Sheng Yang14394422008-04-28 12:24:45 +08004328
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004329 if (!enable_unrestricted_guest && !is_paging(vcpu))
4330 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004331 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4332 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4333 * to be manually disabled when guest switches to non-paging
4334 * mode.
4335 *
4336 * If !enable_unrestricted_guest, the CPU is always running
4337 * with CR0.PG=1 and CR4 needs to be modified.
4338 * If enable_unrestricted_guest, the CPU automatically
4339 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004340 */
Huaitong Handdba2622016-03-22 16:51:15 +08004341 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004342
Sheng Yang14394422008-04-28 12:24:45 +08004343 vmcs_writel(CR4_READ_SHADOW, cr4);
4344 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004345 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346}
4347
Avi Kivity6aa8b732006-12-10 02:21:36 -08004348static void vmx_get_segment(struct kvm_vcpu *vcpu,
4349 struct kvm_segment *var, int seg)
4350{
Avi Kivitya9179492011-01-03 14:28:52 +02004351 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004352 u32 ar;
4353
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004354 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004355 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004356 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004357 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004358 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004359 var->base = vmx_read_guest_seg_base(vmx, seg);
4360 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4361 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004362 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004363 var->base = vmx_read_guest_seg_base(vmx, seg);
4364 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4365 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4366 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004367 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368 var->type = ar & 15;
4369 var->s = (ar >> 4) & 1;
4370 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004371 /*
4372 * Some userspaces do not preserve unusable property. Since usable
4373 * segment has to be present according to VMX spec we can use present
4374 * property to amend userspace bug by making unusable segment always
4375 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4376 * segment as unusable.
4377 */
4378 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004379 var->avl = (ar >> 12) & 1;
4380 var->l = (ar >> 13) & 1;
4381 var->db = (ar >> 14) & 1;
4382 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383}
4384
Avi Kivitya9179492011-01-03 14:28:52 +02004385static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4386{
Avi Kivitya9179492011-01-03 14:28:52 +02004387 struct kvm_segment s;
4388
4389 if (to_vmx(vcpu)->rmode.vm86_active) {
4390 vmx_get_segment(vcpu, &s, seg);
4391 return s.base;
4392 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004393 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004394}
4395
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004396static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004397{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004398 struct vcpu_vmx *vmx = to_vmx(vcpu);
4399
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004400 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004401 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004402 else {
4403 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004404 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004405 }
Avi Kivity69c73022011-03-07 15:26:44 +02004406}
4407
Avi Kivity653e3102007-05-07 10:55:37 +03004408static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410 u32 ar;
4411
Avi Kivityf0495f92012-06-07 17:06:10 +03004412 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413 ar = 1 << 16;
4414 else {
4415 ar = var->type & 15;
4416 ar |= (var->s & 1) << 4;
4417 ar |= (var->dpl & 3) << 5;
4418 ar |= (var->present & 1) << 7;
4419 ar |= (var->avl & 1) << 12;
4420 ar |= (var->l & 1) << 13;
4421 ar |= (var->db & 1) << 14;
4422 ar |= (var->g & 1) << 15;
4423 }
Avi Kivity653e3102007-05-07 10:55:37 +03004424
4425 return ar;
4426}
4427
4428static void vmx_set_segment(struct kvm_vcpu *vcpu,
4429 struct kvm_segment *var, int seg)
4430{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004431 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004432 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004433
Avi Kivity2fb92db2011-04-27 19:42:18 +03004434 vmx_segment_cache_clear(vmx);
4435
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004436 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4437 vmx->rmode.segs[seg] = *var;
4438 if (seg == VCPU_SREG_TR)
4439 vmcs_write16(sf->selector, var->selector);
4440 else if (var->s)
4441 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004442 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004443 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004444
Avi Kivity653e3102007-05-07 10:55:37 +03004445 vmcs_writel(sf->base, var->base);
4446 vmcs_write32(sf->limit, var->limit);
4447 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004448
4449 /*
4450 * Fix the "Accessed" bit in AR field of segment registers for older
4451 * qemu binaries.
4452 * IA32 arch specifies that at the time of processor reset the
4453 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004454 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004455 * state vmexit when "unrestricted guest" mode is turned on.
4456 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4457 * tree. Newer qemu binaries with that qemu fix would not need this
4458 * kvm hack.
4459 */
4460 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004461 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004462
Gleb Natapovf924d662012-12-12 19:10:55 +02004463 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004464
4465out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004466 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004467}
4468
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4470{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004471 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472
4473 *db = (ar >> 14) & 1;
4474 *l = (ar >> 13) & 1;
4475}
4476
Gleb Natapov89a27f42010-02-16 10:51:48 +02004477static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004479 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4480 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481}
4482
Gleb Natapov89a27f42010-02-16 10:51:48 +02004483static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004485 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4486 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487}
4488
Gleb Natapov89a27f42010-02-16 10:51:48 +02004489static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004491 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4492 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493}
4494
Gleb Natapov89a27f42010-02-16 10:51:48 +02004495static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004497 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4498 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499}
4500
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004501static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4502{
4503 struct kvm_segment var;
4504 u32 ar;
4505
4506 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004507 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004508 if (seg == VCPU_SREG_CS)
4509 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004510 ar = vmx_segment_access_rights(&var);
4511
4512 if (var.base != (var.selector << 4))
4513 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004514 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004515 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004516 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004517 return false;
4518
4519 return true;
4520}
4521
4522static bool code_segment_valid(struct kvm_vcpu *vcpu)
4523{
4524 struct kvm_segment cs;
4525 unsigned int cs_rpl;
4526
4527 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004528 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004529
Avi Kivity1872a3f2009-01-04 23:26:52 +02004530 if (cs.unusable)
4531 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004532 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004533 return false;
4534 if (!cs.s)
4535 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004536 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004537 if (cs.dpl > cs_rpl)
4538 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004539 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004540 if (cs.dpl != cs_rpl)
4541 return false;
4542 }
4543 if (!cs.present)
4544 return false;
4545
4546 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4547 return true;
4548}
4549
4550static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4551{
4552 struct kvm_segment ss;
4553 unsigned int ss_rpl;
4554
4555 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004556 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004557
Avi Kivity1872a3f2009-01-04 23:26:52 +02004558 if (ss.unusable)
4559 return true;
4560 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004561 return false;
4562 if (!ss.s)
4563 return false;
4564 if (ss.dpl != ss_rpl) /* DPL != RPL */
4565 return false;
4566 if (!ss.present)
4567 return false;
4568
4569 return true;
4570}
4571
4572static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4573{
4574 struct kvm_segment var;
4575 unsigned int rpl;
4576
4577 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004578 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004579
Avi Kivity1872a3f2009-01-04 23:26:52 +02004580 if (var.unusable)
4581 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004582 if (!var.s)
4583 return false;
4584 if (!var.present)
4585 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004586 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004587 if (var.dpl < rpl) /* DPL < RPL */
4588 return false;
4589 }
4590
4591 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4592 * rights flags
4593 */
4594 return true;
4595}
4596
4597static bool tr_valid(struct kvm_vcpu *vcpu)
4598{
4599 struct kvm_segment tr;
4600
4601 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4602
Avi Kivity1872a3f2009-01-04 23:26:52 +02004603 if (tr.unusable)
4604 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004605 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004606 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004607 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004608 return false;
4609 if (!tr.present)
4610 return false;
4611
4612 return true;
4613}
4614
4615static bool ldtr_valid(struct kvm_vcpu *vcpu)
4616{
4617 struct kvm_segment ldtr;
4618
4619 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4620
Avi Kivity1872a3f2009-01-04 23:26:52 +02004621 if (ldtr.unusable)
4622 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004623 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004624 return false;
4625 if (ldtr.type != 2)
4626 return false;
4627 if (!ldtr.present)
4628 return false;
4629
4630 return true;
4631}
4632
4633static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4634{
4635 struct kvm_segment cs, ss;
4636
4637 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4638 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4639
Nadav Amitb32a9912015-03-29 16:33:04 +03004640 return ((cs.selector & SEGMENT_RPL_MASK) ==
4641 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004642}
4643
Oliver Upton35523a22020-02-04 15:26:30 -08004644static bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu,
4645 unsigned int port, int size);
4646static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
4647 struct vmcs12 *vmcs12)
4648{
4649 unsigned long exit_qualification;
Oliver Uptonf3e0dfb2020-02-04 15:26:31 -08004650 unsigned short port;
Oliver Upton35523a22020-02-04 15:26:30 -08004651 int size;
4652
4653 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
4654 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
4655
4656 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4657
4658 port = exit_qualification >> 16;
4659 size = (exit_qualification & 7) + 1;
4660
4661 return nested_vmx_check_io_bitmaps(vcpu, port, size);
4662}
4663
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004664/*
4665 * Check if guest state is valid. Returns true if valid, false if
4666 * not.
4667 * We assume that registers are always usable
4668 */
4669static bool guest_state_valid(struct kvm_vcpu *vcpu)
4670{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004671 if (enable_unrestricted_guest)
4672 return true;
4673
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004674 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004675 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004676 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4677 return false;
4678 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4679 return false;
4680 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4681 return false;
4682 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4683 return false;
4684 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4685 return false;
4686 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4687 return false;
4688 } else {
4689 /* protected mode guest state checks */
4690 if (!cs_ss_rpl_check(vcpu))
4691 return false;
4692 if (!code_segment_valid(vcpu))
4693 return false;
4694 if (!stack_segment_valid(vcpu))
4695 return false;
4696 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4697 return false;
4698 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4699 return false;
4700 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4701 return false;
4702 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4703 return false;
4704 if (!tr_valid(vcpu))
4705 return false;
4706 if (!ldtr_valid(vcpu))
4707 return false;
4708 }
4709 /* TODO:
4710 * - Add checks on RIP
4711 * - Add checks on RFLAGS
4712 */
4713
4714 return true;
4715}
4716
Mike Dayd77c26f2007-10-08 09:02:08 -04004717static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004719 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004720 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004721 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004722
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004723 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004724 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004725 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4726 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004727 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004728 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004729 r = kvm_write_guest_page(kvm, fn++, &data,
4730 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004731 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004732 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004733 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4734 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004735 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004736 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4737 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004738 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004739 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004740 r = kvm_write_guest_page(kvm, fn, &data,
4741 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4742 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004743out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004744 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004745 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746}
4747
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004748static int init_rmode_identity_map(struct kvm *kvm)
4749{
Tang Chenf51770e2014-09-16 18:41:59 +08004750 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004751 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004752 u32 tmp;
4753
Avi Kivity089d0342009-03-23 18:26:32 +02004754 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004755 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004756
4757 /* Protect kvm->arch.ept_identity_pagetable_done. */
4758 mutex_lock(&kvm->slots_lock);
4759
Tang Chenf51770e2014-09-16 18:41:59 +08004760 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004761 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004762
Sheng Yangb927a3c2009-07-21 10:42:48 +08004763 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004764
4765 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004766 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004767 goto out2;
4768
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004769 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004770 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4771 if (r < 0)
4772 goto out;
4773 /* Set up identity-mapping pagetable for EPT in real mode */
4774 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4775 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4776 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4777 r = kvm_write_guest_page(kvm, identity_map_pfn,
4778 &tmp, i * sizeof(tmp), sizeof(tmp));
4779 if (r < 0)
4780 goto out;
4781 }
4782 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004783
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004784out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004785 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004786
4787out2:
4788 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004789 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004790}
4791
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792static void seg_setup(int seg)
4793{
Mathias Krause772e0312012-08-30 01:30:19 +02004794 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004795 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796
4797 vmcs_write16(sf->selector, 0);
4798 vmcs_writel(sf->base, 0);
4799 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004800 ar = 0x93;
4801 if (seg == VCPU_SREG_CS)
4802 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004803
4804 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805}
4806
Sheng Yangf78e0e22007-10-29 09:40:42 +08004807static int alloc_apic_access_page(struct kvm *kvm)
4808{
Xiao Guangrong44841412012-09-07 14:14:20 +08004809 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004810 int r = 0;
4811
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004812 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004813 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004814 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004815 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4816 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004817 if (r)
4818 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004819
Tang Chen73a6d942014-09-11 13:38:00 +08004820 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004821 if (is_error_page(page)) {
4822 r = -EFAULT;
4823 goto out;
4824 }
4825
Tang Chenc24ae0d2014-09-24 15:57:58 +08004826 /*
4827 * Do not pin the page in memory, so that memory hot-unplug
4828 * is able to migrate it.
4829 */
4830 put_page(page);
4831 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004832out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004833 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004834 return r;
4835}
4836
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004837static int alloc_identity_pagetable(struct kvm *kvm)
4838{
Tang Chena255d472014-09-16 18:41:58 +08004839 /* Called with kvm->slots_lock held. */
4840
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004841 int r = 0;
4842
Tang Chena255d472014-09-16 18:41:58 +08004843 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4844
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004845 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4846 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004847
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004848 return r;
4849}
4850
Wanpeng Li991e7a02015-09-16 17:30:05 +08004851static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004852{
4853 int vpid;
4854
Avi Kivity919818a2009-03-23 18:01:29 +02004855 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004856 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004857 spin_lock(&vmx_vpid_lock);
4858 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004859 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004860 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004861 else
4862 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004863 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004864 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004865}
4866
Wanpeng Li991e7a02015-09-16 17:30:05 +08004867static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004868{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004869 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004870 return;
4871 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004872 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004873 spin_unlock(&vmx_vpid_lock);
4874}
4875
Yi Wang0c4d57a2018-11-08 11:22:21 +08004876static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004877 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004878{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004879 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004880
4881 if (!cpu_has_vmx_msr_bitmap())
4882 return;
4883
4884 /*
4885 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4886 * have the write-low and read-high bitmap offsets the wrong way round.
4887 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4888 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004889 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004890 if (type & MSR_TYPE_R)
4891 /* read-low */
4892 __clear_bit(msr, msr_bitmap + 0x000 / f);
4893
4894 if (type & MSR_TYPE_W)
4895 /* write-low */
4896 __clear_bit(msr, msr_bitmap + 0x800 / f);
4897
Sheng Yang25c5f222008-03-28 13:18:56 +08004898 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4899 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004900 if (type & MSR_TYPE_R)
4901 /* read-high */
4902 __clear_bit(msr, msr_bitmap + 0x400 / f);
4903
4904 if (type & MSR_TYPE_W)
4905 /* write-high */
4906 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4907
4908 }
4909}
4910
Yi Wang0c4d57a2018-11-08 11:22:21 +08004911static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004912 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004913{
4914 int f = sizeof(unsigned long);
4915
4916 if (!cpu_has_vmx_msr_bitmap())
4917 return;
4918
4919 /*
4920 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4921 * have the write-low and read-high bitmap offsets the wrong way round.
4922 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4923 */
4924 if (msr <= 0x1fff) {
4925 if (type & MSR_TYPE_R)
4926 /* read-low */
4927 __set_bit(msr, msr_bitmap + 0x000 / f);
4928
4929 if (type & MSR_TYPE_W)
4930 /* write-low */
4931 __set_bit(msr, msr_bitmap + 0x800 / f);
4932
4933 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4934 msr &= 0x1fff;
4935 if (type & MSR_TYPE_R)
4936 /* read-high */
4937 __set_bit(msr, msr_bitmap + 0x400 / f);
4938
4939 if (type & MSR_TYPE_W)
4940 /* write-high */
4941 __set_bit(msr, msr_bitmap + 0xc00 / f);
4942
Sheng Yang25c5f222008-03-28 13:18:56 +08004943 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004944}
4945
Yi Wang0c4d57a2018-11-08 11:22:21 +08004946static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004947 u32 msr, int type, bool value)
4948{
4949 if (value)
4950 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4951 else
4952 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4953}
4954
Wincy Vanf2b93282015-02-03 23:56:03 +08004955/*
4956 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4957 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4958 */
4959static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4960 unsigned long *msr_bitmap_nested,
4961 u32 msr, int type)
4962{
4963 int f = sizeof(unsigned long);
4964
4965 if (!cpu_has_vmx_msr_bitmap()) {
4966 WARN_ON(1);
4967 return;
4968 }
4969
4970 /*
4971 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4972 * have the write-low and read-high bitmap offsets the wrong way round.
4973 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4974 */
4975 if (msr <= 0x1fff) {
4976 if (type & MSR_TYPE_R &&
4977 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4978 /* read-low */
4979 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4980
4981 if (type & MSR_TYPE_W &&
4982 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4983 /* write-low */
4984 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4985
4986 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4987 msr &= 0x1fff;
4988 if (type & MSR_TYPE_R &&
4989 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4990 /* read-high */
4991 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4992
4993 if (type & MSR_TYPE_W &&
4994 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4995 /* write-high */
4996 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4997
4998 }
4999}
5000
Paolo Bonzini6236b782018-01-16 16:51:18 +01005001static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005002{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005003 u8 mode = 0;
5004
5005 if (cpu_has_secondary_exec_ctrls() &&
5006 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5007 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5008 mode |= MSR_BITMAP_MODE_X2APIC;
5009 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5010 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5011 }
5012
5013 if (is_long_mode(vcpu))
5014 mode |= MSR_BITMAP_MODE_LM;
5015
5016 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005017}
5018
Paolo Bonzini6236b782018-01-16 16:51:18 +01005019#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5020
5021static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5022 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005023{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005024 int msr;
5025
5026 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5027 unsigned word = msr / BITS_PER_LONG;
5028 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5029 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5030 }
5031
5032 if (mode & MSR_BITMAP_MODE_X2APIC) {
5033 /*
5034 * TPR reads and writes can be virtualized even if virtual interrupt
5035 * delivery is not in use.
5036 */
5037 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5038 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5039 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5040 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5041 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5042 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005043 }
Yang Zhang8d146952013-01-25 10:18:50 +08005044}
5045
Paolo Bonzini6236b782018-01-16 16:51:18 +01005046static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005047{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005048 struct vcpu_vmx *vmx = to_vmx(vcpu);
5049 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5050 u8 mode = vmx_msr_bitmap_mode(vcpu);
5051 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005052
Paolo Bonzini6236b782018-01-16 16:51:18 +01005053 if (!changed)
5054 return;
5055
5056 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5057 !(mode & MSR_BITMAP_MODE_LM));
5058
5059 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5060 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5061
5062 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005063}
5064
Andrey Smetanind62caab2015-11-10 15:36:33 +03005065static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005066{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005067 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005068}
5069
David Matlackb7649e12017-08-01 14:00:40 -07005070static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5071{
5072 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5073 gfn_t gfn;
5074
5075 /*
5076 * Don't need to mark the APIC access page dirty; it is never
5077 * written to by the CPU during APIC virtualization.
5078 */
5079
5080 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5081 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5082 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5083 }
5084
5085 if (nested_cpu_has_posted_intr(vmcs12)) {
5086 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5087 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5088 }
5089}
5090
5091
David Hildenbrand1edccf22017-01-25 11:58:58 +01005092static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005093{
5094 struct vcpu_vmx *vmx = to_vmx(vcpu);
5095 int max_irr;
5096 void *vapic_page;
5097 u16 status;
5098
David Matlackb7649e12017-08-01 14:00:40 -07005099 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5100 return;
Wincy Van705699a2015-02-03 23:58:17 +08005101
David Matlackb7649e12017-08-01 14:00:40 -07005102 vmx->nested.pi_pending = false;
5103 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5104 return;
Wincy Van705699a2015-02-03 23:58:17 +08005105
David Matlackb7649e12017-08-01 14:00:40 -07005106 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5107 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005108 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005109 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5110 kunmap(vmx->nested.virtual_apic_page);
5111
5112 status = vmcs_read16(GUEST_INTR_STATUS);
5113 if ((u8)max_irr > ((u8)status & 0xff)) {
5114 status &= ~0xff;
5115 status |= (u8)max_irr;
5116 vmcs_write16(GUEST_INTR_STATUS, status);
5117 }
5118 }
David Matlackb7649e12017-08-01 14:00:40 -07005119
5120 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005121}
5122
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005123static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5124{
5125#ifdef CONFIG_SMP
5126 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005127 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005128 * The vector of interrupt to be delivered to vcpu had
5129 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005130 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005131 * Following cases will be reached in this block, and
5132 * we always send a notification event in all cases as
5133 * explained below.
5134 *
5135 * Case 1: vcpu keeps in non-root mode. Sending a
5136 * notification event posts the interrupt to vcpu.
5137 *
5138 * Case 2: vcpu exits to root mode and is still
5139 * runnable. PIR will be synced to vIRR before the
5140 * next vcpu entry. Sending a notification event in
5141 * this case has no effect, as vcpu is not in root
5142 * mode.
5143 *
5144 * Case 3: vcpu exits to root mode and is blocked.
5145 * vcpu_block() has already synced PIR to vIRR and
5146 * never blocks vcpu if vIRR is not cleared. Therefore,
5147 * a blocked vcpu here does not wait for any requested
5148 * interrupts in PIR, and sending a notification event
5149 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005150 */
Feng Wu28b835d2015-09-18 22:29:54 +08005151
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005152 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5153 POSTED_INTR_VECTOR);
5154 return true;
5155 }
5156#endif
5157 return false;
5158}
5159
Wincy Van705699a2015-02-03 23:58:17 +08005160static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5161 int vector)
5162{
5163 struct vcpu_vmx *vmx = to_vmx(vcpu);
5164
5165 if (is_guest_mode(vcpu) &&
5166 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005167 /*
5168 * If a posted intr is not recognized by hardware,
5169 * we will accomplish it in the next vmentry.
5170 */
5171 vmx->nested.pi_pending = true;
5172 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005173 /* the PIR and ON have been set by L1. */
5174 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5175 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005176 return 0;
5177 }
5178 return -1;
5179}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005181 * Send interrupt to vcpu via posted interrupt way.
5182 * 1. If target vcpu is running(non-root mode), send posted interrupt
5183 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5184 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5185 * interrupt from PIR in next vmentry.
5186 */
5187static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5188{
5189 struct vcpu_vmx *vmx = to_vmx(vcpu);
5190 int r;
5191
Wincy Van705699a2015-02-03 23:58:17 +08005192 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5193 if (!r)
5194 return;
5195
Yang Zhanga20ed542013-04-11 19:25:15 +08005196 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5197 return;
5198
5199 r = pi_test_and_set_on(&vmx->pi_desc);
5200 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005201 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005202 kvm_vcpu_kick(vcpu);
5203}
5204
5205static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5206{
5207 struct vcpu_vmx *vmx = to_vmx(vcpu);
5208
5209 if (!pi_test_and_clear_on(&vmx->pi_desc))
5210 return;
5211
5212 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5213}
5214
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005216 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5217 * will not change in the lifetime of the guest.
5218 * Note that host-state that does change is set elsewhere. E.g., host-state
5219 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5220 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005221static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005222{
5223 u32 low32, high32;
5224 unsigned long tmpl;
5225 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005226 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005227
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005228 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005229 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5230
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005231 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005232 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005233 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5234 vmx->host_state.vmcs_host_cr4 = cr4;
5235
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005236 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005237#ifdef CONFIG_X86_64
5238 /*
5239 * Load null selectors, so we can avoid reloading them in
5240 * __vmx_load_host_state(), in case userspace uses the null selectors
5241 * too (the expected case).
5242 */
5243 vmcs_write16(HOST_DS_SELECTOR, 0);
5244 vmcs_write16(HOST_ES_SELECTOR, 0);
5245#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005246 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5247 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005248#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005249 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5250 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5251
5252 native_store_idt(&dt);
5253 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005254 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005255
Avi Kivity83287ea422012-09-16 15:10:57 +03005256 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005257
5258 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5259 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5260 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5261 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5262
5263 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5264 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5265 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5266 }
5267}
5268
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005269static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5270{
5271 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5272 if (enable_ept)
5273 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005274 if (is_guest_mode(&vmx->vcpu))
5275 vmx->vcpu.arch.cr4_guest_owned_bits &=
5276 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005277 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5278}
5279
Yang Zhang01e439b2013-04-11 19:25:12 +08005280static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5281{
5282 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5283
Andrey Smetanind62caab2015-11-10 15:36:33 +03005284 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005285 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005286 /* Enable the preemption timer dynamically */
5287 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005288 return pin_based_exec_ctrl;
5289}
5290
Andrey Smetanind62caab2015-11-10 15:36:33 +03005291static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5292{
5293 struct vcpu_vmx *vmx = to_vmx(vcpu);
5294
5295 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005296 if (cpu_has_secondary_exec_ctrls()) {
5297 if (kvm_vcpu_apicv_active(vcpu))
5298 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5299 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5300 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5301 else
5302 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5303 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5304 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5305 }
5306
5307 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005308 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005309}
5310
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005311static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5312{
5313 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005314
5315 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5316 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5317
Paolo Bonzini35754c92015-07-29 12:05:37 +02005318 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005319 exec_control &= ~CPU_BASED_TPR_SHADOW;
5320#ifdef CONFIG_X86_64
5321 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5322 CPU_BASED_CR8_LOAD_EXITING;
5323#endif
5324 }
5325 if (!enable_ept)
5326 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5327 CPU_BASED_CR3_LOAD_EXITING |
5328 CPU_BASED_INVLPG_EXITING;
5329 return exec_control;
5330}
5331
5332static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5333{
5334 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005335 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005336 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5337 if (vmx->vpid == 0)
5338 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5339 if (!enable_ept) {
5340 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5341 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005342 /* Enable INVPCID for non-ept guests may cause performance regression. */
5343 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005344 }
5345 if (!enable_unrestricted_guest)
5346 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5347 if (!ple_gap)
5348 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005349 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005350 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5351 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005352 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005353 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5354 (handle_vmptrld).
5355 We can NOT enable shadow_vmcs here because we don't have yet
5356 a current VMCS12
5357 */
5358 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005359
5360 if (!enable_pml)
5361 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005362
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005363 return exec_control;
5364}
5365
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005366static void ept_set_mmio_spte_mask(void)
5367{
5368 /*
5369 * EPT Misconfigurations can be generated if the value of bits 2:0
5370 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005371 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005372 * spte.
5373 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005374 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005375}
5376
Wanpeng Lif53cd632014-12-02 19:14:58 +08005377#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005378/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379 * Sets up the vmcs for emulated real mode.
5380 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005381static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005382{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005383#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005384 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005385#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005386 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005389 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5390 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005391
Abel Gordon4607c2d2013-04-18 14:35:55 +03005392 if (enable_shadow_vmcs) {
5393 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5394 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5395 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005396 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005397 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005398
Avi Kivity6aa8b732006-12-10 02:21:36 -08005399 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5400
Avi Kivity6aa8b732006-12-10 02:21:36 -08005401 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005402 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005403 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005404
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005405 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005406
Dan Williamsdfa169b2016-06-02 11:17:24 -07005407 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005408 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5409 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005410 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005411
Andrey Smetanind62caab2015-11-10 15:36:33 +03005412 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005413 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5414 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5415 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5416 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5417
5418 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005419
Li RongQing0bcf2612015-12-03 13:29:34 +08005420 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005421 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005422 }
5423
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005424 if (ple_gap) {
5425 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005426 vmx->ple_window = ple_window;
5427 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005428 }
5429
Xiao Guangrongc3707952011-07-12 03:28:04 +08005430 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5431 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005432 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5433
Avi Kivity9581d442010-10-19 16:46:55 +02005434 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5435 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005436 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005437#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005438 rdmsrl(MSR_FS_BASE, a);
5439 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5440 rdmsrl(MSR_GS_BASE, a);
5441 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5442#else
5443 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5444 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5445#endif
5446
Eddie Dong2cc51562007-05-21 07:28:09 +03005447 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5448 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005449 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005450 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005451 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452
Radim Krčmář74545702015-04-27 15:11:25 +02005453 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5454 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005455
Paolo Bonzini03916db2014-07-24 14:21:57 +02005456 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 u32 index = vmx_msr_index[i];
5458 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005459 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005460
5461 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5462 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005463 if (wrmsr_safe(index, data_low, data_high) < 0)
5464 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005465 vmx->guest_msrs[j].index = i;
5466 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005467 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005468 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005470
Gleb Natapov2961e8762013-11-25 15:37:13 +02005471 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005472
5473 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005474 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005475
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005476 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005477 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005478
Wanpeng Lif53cd632014-12-02 19:14:58 +08005479 if (vmx_xsaves_supported())
5480 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5481
Peter Feiner4e595162016-07-07 14:49:58 -07005482 if (enable_pml) {
5483 ASSERT(vmx->pml_pg);
5484 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5485 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5486 }
5487
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005488 return 0;
5489}
5490
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005491static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005492{
5493 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005494 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005495 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005496
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005497 vmx->rmode.vm86_active = 0;
Wanpeng Li7a1eac82018-02-28 14:03:31 +08005498 vcpu->arch.microcode_version = 0x100000000ULL;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005499 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005500
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005501 vmx->soft_vnmi_blocked = 0;
5502
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005503 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005504 kvm_set_cr8(vcpu, 0);
5505
5506 if (!init_event) {
5507 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5508 MSR_IA32_APICBASE_ENABLE;
5509 if (kvm_vcpu_is_reset_bsp(vcpu))
5510 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5511 apic_base_msr.host_initiated = true;
5512 kvm_set_apic_base(vcpu, &apic_base_msr);
5513 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005514
Avi Kivity2fb92db2011-04-27 19:42:18 +03005515 vmx_segment_cache_clear(vmx);
5516
Avi Kivity5706be02008-08-20 15:07:31 +03005517 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005518 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005519 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005520
5521 seg_setup(VCPU_SREG_DS);
5522 seg_setup(VCPU_SREG_ES);
5523 seg_setup(VCPU_SREG_FS);
5524 seg_setup(VCPU_SREG_GS);
5525 seg_setup(VCPU_SREG_SS);
5526
5527 vmcs_write16(GUEST_TR_SELECTOR, 0);
5528 vmcs_writel(GUEST_TR_BASE, 0);
5529 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5530 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5531
5532 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5533 vmcs_writel(GUEST_LDTR_BASE, 0);
5534 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5535 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5536
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005537 if (!init_event) {
5538 vmcs_write32(GUEST_SYSENTER_CS, 0);
5539 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5540 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5541 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5542 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005543
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005544 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005545 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005546
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005547 vmcs_writel(GUEST_GDTR_BASE, 0);
5548 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5549
5550 vmcs_writel(GUEST_IDTR_BASE, 0);
5551 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5552
Anthony Liguori443381a2010-12-06 10:53:38 -06005553 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005554 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005555 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005556
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005557 setup_msrs(vmx);
5558
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5560
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005561 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005562 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005563 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005564 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005565 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005566 vmcs_write32(TPR_THRESHOLD, 0);
5567 }
5568
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005569 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570
Andrey Smetanind62caab2015-11-10 15:36:33 +03005571 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005572 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5573
Sheng Yang2384d2b2008-01-17 15:14:33 +08005574 if (vmx->vpid != 0)
5575 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5576
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005577 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005578 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005579 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005580 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005581 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005582 vmx_fpu_activate(vcpu);
5583 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005584
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005585 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005586}
5587
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005588/*
5589 * In nested virtualization, check if L1 asked to exit on external interrupts.
5590 * For most existing hypervisors, this will always return true.
5591 */
5592static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5593{
5594 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5595 PIN_BASED_EXT_INTR_MASK;
5596}
5597
Bandan Das77b0f5d2014-04-19 18:17:45 -04005598/*
5599 * In nested virtualization, check if L1 has set
5600 * VM_EXIT_ACK_INTR_ON_EXIT
5601 */
5602static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5603{
5604 return get_vmcs12(vcpu)->vm_exit_controls &
5605 VM_EXIT_ACK_INTR_ON_EXIT;
5606}
5607
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005608static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5609{
5610 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5611 PIN_BASED_NMI_EXITING;
5612}
5613
Jan Kiszkac9a79532014-03-07 20:03:15 +01005614static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005615{
5616 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005617
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005618 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5619 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5620 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5621}
5622
Jan Kiszkac9a79532014-03-07 20:03:15 +01005623static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005624{
5625 u32 cpu_based_vm_exec_control;
5626
Jan Kiszkac9a79532014-03-07 20:03:15 +01005627 if (!cpu_has_virtual_nmis() ||
5628 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5629 enable_irq_window(vcpu);
5630 return;
5631 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005632
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005633 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5634 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5635 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5636}
5637
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005638static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005639{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005641 uint32_t intr;
5642 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005643
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005644 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005645
Avi Kivityfa89a812008-09-01 15:57:51 +03005646 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005647 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005648 int inc_eip = 0;
5649 if (vcpu->arch.interrupt.soft)
5650 inc_eip = vcpu->arch.event_exit_inst_len;
5651 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005652 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005653 return;
5654 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005655 intr = irq | INTR_INFO_VALID_MASK;
5656 if (vcpu->arch.interrupt.soft) {
5657 intr |= INTR_TYPE_SOFT_INTR;
5658 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5659 vmx->vcpu.arch.event_exit_inst_len);
5660 } else
5661 intr |= INTR_TYPE_EXT_INTR;
5662 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005663}
5664
Sheng Yangf08864b2008-05-15 18:23:25 +08005665static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5666{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005667 struct vcpu_vmx *vmx = to_vmx(vcpu);
5668
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005669 if (!is_guest_mode(vcpu)) {
5670 if (!cpu_has_virtual_nmis()) {
5671 /*
5672 * Tracking the NMI-blocked state in software is built upon
5673 * finding the next open IRQ window. This, in turn, depends on
5674 * well-behaving guests: They have to keep IRQs disabled at
5675 * least as long as the NMI handler runs. Otherwise we may
5676 * cause NMI nesting, maybe breaking the guest. But as this is
5677 * highly unlikely, we can live with the residual risk.
5678 */
5679 vmx->soft_vnmi_blocked = 1;
5680 vmx->vnmi_blocked_time = 0;
5681 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005682
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005683 ++vcpu->stat.nmi_injections;
5684 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005685 }
5686
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005687 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005688 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005689 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005690 return;
5691 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005692
Sheng Yangf08864b2008-05-15 18:23:25 +08005693 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5694 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005695}
5696
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005697static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5698{
5699 if (!cpu_has_virtual_nmis())
5700 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005701 if (to_vmx(vcpu)->nmi_known_unmasked)
5702 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005703 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005704}
5705
5706static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5707{
5708 struct vcpu_vmx *vmx = to_vmx(vcpu);
5709
5710 if (!cpu_has_virtual_nmis()) {
5711 if (vmx->soft_vnmi_blocked != masked) {
5712 vmx->soft_vnmi_blocked = masked;
5713 vmx->vnmi_blocked_time = 0;
5714 }
5715 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005716 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005717 if (masked)
5718 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5719 GUEST_INTR_STATE_NMI);
5720 else
5721 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5722 GUEST_INTR_STATE_NMI);
5723 }
5724}
5725
Jan Kiszka2505dc92013-04-14 12:12:47 +02005726static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5727{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005728 if (to_vmx(vcpu)->nested.nested_run_pending)
5729 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005730
Jan Kiszka2505dc92013-04-14 12:12:47 +02005731 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5732 return 0;
5733
5734 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5735 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5736 | GUEST_INTR_STATE_NMI));
5737}
5738
Gleb Natapov78646122009-03-23 12:12:11 +02005739static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5740{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005741 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5742 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005743 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5744 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005745}
5746
Izik Eiduscbc94022007-10-25 00:29:55 +02005747static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5748{
5749 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005750
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005751 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5752 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005753 if (ret)
5754 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005755 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005756 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005757}
5758
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005759static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005760{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005761 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005762 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005763 /*
5764 * Update instruction length as we may reinject the exception
5765 * from user space while in guest debugging mode.
5766 */
5767 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5768 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005769 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005770 return false;
5771 /* fall through */
5772 case DB_VECTOR:
5773 if (vcpu->guest_debug &
5774 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5775 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005776 /* fall through */
5777 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005778 case OF_VECTOR:
5779 case BR_VECTOR:
5780 case UD_VECTOR:
5781 case DF_VECTOR:
5782 case SS_VECTOR:
5783 case GP_VECTOR:
5784 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005785 return true;
5786 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005787 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005788 return false;
5789}
5790
5791static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5792 int vec, u32 err_code)
5793{
5794 /*
5795 * Instruction with address size override prefix opcode 0x67
5796 * Cause the #SS fault with 0 error code in VM86 mode.
5797 */
5798 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5799 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5800 if (vcpu->arch.halt_request) {
5801 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005802 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005803 }
5804 return 1;
5805 }
5806 return 0;
5807 }
5808
5809 /*
5810 * Forward all other exceptions that are valid in real mode.
5811 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5812 * the required debugging infrastructure rework.
5813 */
5814 kvm_queue_exception(vcpu, vec);
5815 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005816}
5817
Andi Kleena0861c02009-06-08 17:37:09 +08005818/*
5819 * Trigger machine check on the host. We assume all the MSRs are already set up
5820 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5821 * We pass a fake environment to the machine check handler because we want
5822 * the guest to be always treated like user space, no matter what context
5823 * it used internally.
5824 */
5825static void kvm_machine_check(void)
5826{
5827#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5828 struct pt_regs regs = {
5829 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5830 .flags = X86_EFLAGS_IF,
5831 };
5832
5833 do_machine_check(&regs, 0);
5834#endif
5835}
5836
Avi Kivity851ba692009-08-24 11:10:17 +03005837static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005838{
5839 /* already handled by vcpu_run */
5840 return 1;
5841}
5842
Avi Kivity851ba692009-08-24 11:10:17 +03005843static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005844{
Avi Kivity1155f762007-11-22 11:30:47 +02005845 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005846 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005847 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005848 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005849 u32 vect_info;
5850 enum emulation_result er;
5851
Avi Kivity1155f762007-11-22 11:30:47 +02005852 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005853 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005854
Andi Kleena0861c02009-06-08 17:37:09 +08005855 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005856 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005857
Jim Mattson3f618a02016-12-12 11:01:37 -08005858 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005859 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005860
5861 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005862 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005863 return 1;
5864 }
5865
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005866 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005867 if (is_guest_mode(vcpu)) {
5868 kvm_queue_exception(vcpu, UD_VECTOR);
5869 return 1;
5870 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005871 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005872 if (er == EMULATE_USER_EXIT)
5873 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005874 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005875 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005876 return 1;
5877 }
5878
Avi Kivity6aa8b732006-12-10 02:21:36 -08005879 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005880 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005881 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005882
5883 /*
5884 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5885 * MMIO, it is better to report an internal error.
5886 * See the comments in vmx_handle_exit.
5887 */
5888 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5889 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5890 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5891 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005892 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005893 vcpu->run->internal.data[0] = vect_info;
5894 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005895 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005896 return 0;
5897 }
5898
Avi Kivity6aa8b732006-12-10 02:21:36 -08005899 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005900 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005901 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005902 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005903 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005904 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005905
Gleb Natapov3298b752009-05-11 13:35:46 +03005906 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005907 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005908 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005909 }
5910
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005911 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005912
5913 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5914 return handle_rmode_exception(vcpu, ex_no, error_code);
5915
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005916 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005917 case AC_VECTOR:
5918 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5919 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005920 case DB_VECTOR:
5921 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5922 if (!(vcpu->guest_debug &
5923 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005924 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005925 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005926 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005927 skip_emulated_instruction(vcpu);
5928
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005929 kvm_queue_exception(vcpu, DB_VECTOR);
5930 return 1;
5931 }
5932 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5933 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5934 /* fall through */
5935 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005936 /*
5937 * Update instruction length as we may reinject #BP from
5938 * user space while in guest debugging mode. Reading it for
5939 * #DB as well causes no harm, it is not used in that case.
5940 */
5941 vmx->vcpu.arch.event_exit_inst_len =
5942 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005943 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005944 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005945 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5946 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005947 break;
5948 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005949 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5950 kvm_run->ex.exception = ex_no;
5951 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005952 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005953 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954 return 0;
5955}
5956
Avi Kivity851ba692009-08-24 11:10:17 +03005957static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005958{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005959 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005960 return 1;
5961}
5962
Avi Kivity851ba692009-08-24 11:10:17 +03005963static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005964{
Avi Kivity851ba692009-08-24 11:10:17 +03005965 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Li5e29da02017-08-09 22:33:12 -07005966 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08005967 return 0;
5968}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005969
Avi Kivity851ba692009-08-24 11:10:17 +03005970static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005971{
He, Qingbfdaab02007-09-12 14:18:28 +08005972 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005973 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005974 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005975
He, Qingbfdaab02007-09-12 14:18:28 +08005976 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005977 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005978 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005979
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005980 ++vcpu->stat.io_exits;
5981
5982 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005983 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005984
5985 port = exit_qualification >> 16;
5986 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005987 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005988
5989 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005990}
5991
Ingo Molnar102d8322007-02-19 14:37:47 +02005992static void
5993vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5994{
5995 /*
5996 * Patch in the VMCALL instruction:
5997 */
5998 hypercall[0] = 0x0f;
5999 hypercall[1] = 0x01;
6000 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006001}
6002
Wincy Vanb9c237b2015-02-03 23:56:30 +08006003static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006004{
6005 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08006006 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006007
Wincy Vanb9c237b2015-02-03 23:56:30 +08006008 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006009 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
6010 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
6011 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
6012 return (val & always_on) == always_on;
6013}
6014
Guo Chao0fa06072012-06-28 15:16:19 +08006015/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006016static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6017{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006018 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006019 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6020 unsigned long orig_val = val;
6021
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006022 /*
6023 * We get here when L2 changed cr0 in a way that did not change
6024 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006025 * but did change L0 shadowed bits. So we first calculate the
6026 * effective cr0 value that L1 would like to write into the
6027 * hardware. It consists of the L2-owned bits from the new
6028 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006029 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006030 val = (val & ~vmcs12->cr0_guest_host_mask) |
6031 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6032
Wincy Vanb9c237b2015-02-03 23:56:30 +08006033 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006034 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006035
6036 if (kvm_set_cr0(vcpu, val))
6037 return 1;
6038 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006039 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006040 } else {
6041 if (to_vmx(vcpu)->nested.vmxon &&
6042 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
6043 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006044 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006045 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006046}
6047
6048static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6049{
6050 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006051 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6052 unsigned long orig_val = val;
6053
6054 /* analogously to handle_set_cr0 */
6055 val = (val & ~vmcs12->cr4_guest_host_mask) |
6056 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6057 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006058 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006059 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006060 return 0;
6061 } else
6062 return kvm_set_cr4(vcpu, val);
6063}
6064
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08006065/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006066static void handle_clts(struct kvm_vcpu *vcpu)
6067{
6068 if (is_guest_mode(vcpu)) {
6069 /*
6070 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
6071 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
6072 * just pretend it's off (also in arch.cr0 for fpu_activate).
6073 */
6074 vmcs_writel(CR0_READ_SHADOW,
6075 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
6076 vcpu->arch.cr0 &= ~X86_CR0_TS;
6077 } else
6078 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6079}
6080
Avi Kivity851ba692009-08-24 11:10:17 +03006081static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006082{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006083 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006084 int cr;
6085 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006086 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006087
He, Qingbfdaab02007-09-12 14:18:28 +08006088 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006089 cr = exit_qualification & 15;
6090 reg = (exit_qualification >> 8) & 15;
6091 switch ((exit_qualification >> 4) & 3) {
6092 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006093 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006094 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006095 switch (cr) {
6096 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006097 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006098 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006099 return 1;
6100 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006101 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006102 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006103 return 1;
6104 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006105 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006106 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006107 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006108 case 8: {
6109 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006110 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006111 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006112 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006113 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006114 return 1;
6115 if (cr8_prev <= cr8)
6116 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006117 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006118 return 0;
6119 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006120 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006121 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006122 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006123 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006124 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006125 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006126 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006127 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006128 case 1: /*mov from cr*/
6129 switch (cr) {
6130 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006131 val = kvm_read_cr3(vcpu);
6132 kvm_register_write(vcpu, reg, val);
6133 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006134 skip_emulated_instruction(vcpu);
6135 return 1;
6136 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006137 val = kvm_get_cr8(vcpu);
6138 kvm_register_write(vcpu, reg, val);
6139 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006140 skip_emulated_instruction(vcpu);
6141 return 1;
6142 }
6143 break;
6144 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006145 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006146 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006147 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006148
6149 skip_emulated_instruction(vcpu);
6150 return 1;
6151 default:
6152 break;
6153 }
Avi Kivity851ba692009-08-24 11:10:17 +03006154 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006155 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006156 (int)(exit_qualification >> 4) & 3, cr);
6157 return 0;
6158}
6159
Avi Kivity851ba692009-08-24 11:10:17 +03006160static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006161{
He, Qingbfdaab02007-09-12 14:18:28 +08006162 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006163 int dr, dr7, reg;
6164
6165 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6166 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6167
6168 /* First, if DR does not exist, trigger UD */
6169 if (!kvm_require_dr(vcpu, dr))
6170 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006171
Jan Kiszkaf2483412010-01-20 18:20:20 +01006172 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006173 if (!kvm_require_cpl(vcpu, 0))
6174 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006175 dr7 = vmcs_readl(GUEST_DR7);
6176 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006177 /*
6178 * As the vm-exit takes precedence over the debug trap, we
6179 * need to emulate the latter, either for the host or the
6180 * guest debugging itself.
6181 */
6182 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006183 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006184 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006185 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006186 vcpu->run->debug.arch.exception = DB_VECTOR;
6187 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006188 return 0;
6189 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006190 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006191 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006192 kvm_queue_exception(vcpu, DB_VECTOR);
6193 return 1;
6194 }
6195 }
6196
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006197 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006198 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6199 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006200
6201 /*
6202 * No more DR vmexits; force a reload of the debug registers
6203 * and reenter on this instruction. The next vmexit will
6204 * retrieve the full state of the debug registers.
6205 */
6206 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6207 return 1;
6208 }
6209
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006210 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6211 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006212 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006213
6214 if (kvm_get_dr(vcpu, dr, &val))
6215 return 1;
6216 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006217 } else
Nadav Amit57773922014-06-18 17:19:23 +03006218 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006219 return 1;
6220
Avi Kivity6aa8b732006-12-10 02:21:36 -08006221 skip_emulated_instruction(vcpu);
6222 return 1;
6223}
6224
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006225static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6226{
6227 return vcpu->arch.dr6;
6228}
6229
6230static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6231{
6232}
6233
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006234static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6235{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006236 get_debugreg(vcpu->arch.db[0], 0);
6237 get_debugreg(vcpu->arch.db[1], 1);
6238 get_debugreg(vcpu->arch.db[2], 2);
6239 get_debugreg(vcpu->arch.db[3], 3);
6240 get_debugreg(vcpu->arch.dr6, 6);
6241 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6242
6243 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006244 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006245}
6246
Gleb Natapov020df072010-04-13 10:05:23 +03006247static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6248{
6249 vmcs_writel(GUEST_DR7, val);
6250}
6251
Avi Kivity851ba692009-08-24 11:10:17 +03006252static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006253{
Avi Kivity06465c52007-02-28 20:46:53 +02006254 kvm_emulate_cpuid(vcpu);
6255 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006256}
6257
Avi Kivity851ba692009-08-24 11:10:17 +03006258static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006259{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006260 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006261 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006262
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006263 msr_info.index = ecx;
6264 msr_info.host_initiated = false;
6265 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006266 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006267 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006268 return 1;
6269 }
6270
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006271 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006272
Avi Kivity6aa8b732006-12-10 02:21:36 -08006273 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006274 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6275 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006276 skip_emulated_instruction(vcpu);
6277 return 1;
6278}
6279
Avi Kivity851ba692009-08-24 11:10:17 +03006280static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006281{
Will Auld8fe8ab42012-11-29 12:42:12 -08006282 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006283 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6284 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6285 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006286
Will Auld8fe8ab42012-11-29 12:42:12 -08006287 msr.data = data;
6288 msr.index = ecx;
6289 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006290 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006291 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006292 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006293 return 1;
6294 }
6295
Avi Kivity59200272010-01-25 19:47:02 +02006296 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297 skip_emulated_instruction(vcpu);
6298 return 1;
6299}
6300
Avi Kivity851ba692009-08-24 11:10:17 +03006301static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006302{
Avi Kivity3842d132010-07-27 12:30:24 +03006303 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006304 return 1;
6305}
6306
Avi Kivity851ba692009-08-24 11:10:17 +03006307static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006308{
Eddie Dong85f455f2007-07-06 12:20:49 +03006309 u32 cpu_based_vm_exec_control;
6310
6311 /* clear pending irq */
6312 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6313 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6314 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006315
Avi Kivity3842d132010-07-27 12:30:24 +03006316 kvm_make_request(KVM_REQ_EVENT, vcpu);
6317
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006318 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319 return 1;
6320}
6321
Avi Kivity851ba692009-08-24 11:10:17 +03006322static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006323{
Avi Kivityd3bef152007-06-05 15:53:05 +03006324 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325}
6326
Avi Kivity851ba692009-08-24 11:10:17 +03006327static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006328{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006329 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006330}
6331
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006332static int handle_invd(struct kvm_vcpu *vcpu)
6333{
Andre Przywara51d8b662010-12-21 11:12:02 +01006334 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006335}
6336
Avi Kivity851ba692009-08-24 11:10:17 +03006337static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006338{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006339 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006340
6341 kvm_mmu_invlpg(vcpu, exit_qualification);
6342 skip_emulated_instruction(vcpu);
6343 return 1;
6344}
6345
Avi Kivityfee84b02011-11-10 14:57:25 +02006346static int handle_rdpmc(struct kvm_vcpu *vcpu)
6347{
6348 int err;
6349
6350 err = kvm_rdpmc(vcpu);
6351 kvm_complete_insn_gp(vcpu, err);
6352
6353 return 1;
6354}
6355
Avi Kivity851ba692009-08-24 11:10:17 +03006356static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006357{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006358 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006359 return 1;
6360}
6361
Dexuan Cui2acf9232010-06-10 11:27:12 +08006362static int handle_xsetbv(struct kvm_vcpu *vcpu)
6363{
6364 u64 new_bv = kvm_read_edx_eax(vcpu);
6365 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6366
6367 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6368 skip_emulated_instruction(vcpu);
6369 return 1;
6370}
6371
Wanpeng Lif53cd632014-12-02 19:14:58 +08006372static int handle_xsaves(struct kvm_vcpu *vcpu)
6373{
6374 skip_emulated_instruction(vcpu);
6375 WARN(1, "this should never happen\n");
6376 return 1;
6377}
6378
6379static int handle_xrstors(struct kvm_vcpu *vcpu)
6380{
6381 skip_emulated_instruction(vcpu);
6382 WARN(1, "this should never happen\n");
6383 return 1;
6384}
6385
Avi Kivity851ba692009-08-24 11:10:17 +03006386static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006387{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006388 if (likely(fasteoi)) {
6389 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6390 int access_type, offset;
6391
6392 access_type = exit_qualification & APIC_ACCESS_TYPE;
6393 offset = exit_qualification & APIC_ACCESS_OFFSET;
6394 /*
6395 * Sane guest uses MOV to write EOI, with written value
6396 * not cared. So make a short-circuit here by avoiding
6397 * heavy instruction emulation.
6398 */
6399 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6400 (offset == APIC_EOI)) {
6401 kvm_lapic_set_eoi(vcpu);
6402 skip_emulated_instruction(vcpu);
6403 return 1;
6404 }
6405 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006406 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006407}
6408
Yang Zhangc7c9c562013-01-25 10:18:51 +08006409static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6410{
6411 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6412 int vector = exit_qualification & 0xff;
6413
6414 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6415 kvm_apic_set_eoi_accelerated(vcpu, vector);
6416 return 1;
6417}
6418
Yang Zhang83d4c282013-01-25 10:18:49 +08006419static int handle_apic_write(struct kvm_vcpu *vcpu)
6420{
6421 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6422 u32 offset = exit_qualification & 0xfff;
6423
6424 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6425 kvm_apic_write_nodecode(vcpu, offset);
6426 return 1;
6427}
6428
Avi Kivity851ba692009-08-24 11:10:17 +03006429static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006430{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006431 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006432 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006433 bool has_error_code = false;
6434 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006435 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006436 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006437
6438 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006439 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006440 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006441
6442 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6443
6444 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006445 if (reason == TASK_SWITCH_GATE && idt_v) {
6446 switch (type) {
6447 case INTR_TYPE_NMI_INTR:
6448 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006449 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006450 break;
6451 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006452 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006453 kvm_clear_interrupt_queue(vcpu);
6454 break;
6455 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006456 if (vmx->idt_vectoring_info &
6457 VECTORING_INFO_DELIVER_CODE_MASK) {
6458 has_error_code = true;
6459 error_code =
6460 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6461 }
6462 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006463 case INTR_TYPE_SOFT_EXCEPTION:
6464 kvm_clear_exception_queue(vcpu);
6465 break;
6466 default:
6467 break;
6468 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006469 }
Izik Eidus37817f22008-03-24 23:14:53 +02006470 tss_selector = exit_qualification;
6471
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006472 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6473 type != INTR_TYPE_EXT_INTR &&
6474 type != INTR_TYPE_NMI_INTR))
6475 skip_emulated_instruction(vcpu);
6476
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006477 if (kvm_task_switch(vcpu, tss_selector,
6478 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6479 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006480 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6481 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6482 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006483 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006484 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006485
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006486 /*
6487 * TODO: What about debug traps on tss switch?
6488 * Are we supposed to inject them and update dr6?
6489 */
6490
6491 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006492}
6493
Avi Kivity851ba692009-08-24 11:10:17 +03006494static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006495{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006496 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006497 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006498 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006499 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006500
Sheng Yangf9c617f2009-03-25 10:08:52 +08006501 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006502
Sheng Yang14394422008-04-28 12:24:45 +08006503 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006504 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006505 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6506 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6507 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006508 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006509 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6510 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006511 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6512 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006513 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006514 }
6515
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006516 /*
6517 * EPT violation happened while executing iret from NMI,
6518 * "blocked by NMI" bit has to be set before next VM entry.
6519 * There are errata that may cause this bit to not be set:
6520 * AAK134, BY25.
6521 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006522 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6523 cpu_has_virtual_nmis() &&
6524 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006525 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6526
Sheng Yang14394422008-04-28 12:24:45 +08006527 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006528 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006529
Bandan Dasd95c5562016-07-12 18:18:51 -04006530 /* it is a read fault? */
6531 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6532 /* it is a write fault? */
6533 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006534 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006535 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006536 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006537 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006538
Yang Zhang25d92082013-08-06 12:00:32 +03006539 vcpu->arch.exit_qualification = exit_qualification;
6540
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006541 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006542}
6543
Avi Kivity851ba692009-08-24 11:10:17 +03006544static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006545{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006546 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006547 gpa_t gpa;
6548
6549 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006550 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006551 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsov737dcb92018-01-25 16:37:07 +01006552 /*
6553 * Doing kvm_skip_emulated_instruction() depends on undefined
6554 * behavior: Intel's manual doesn't mandate
6555 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6556 * occurs and while on real hardware it was observed to be set,
6557 * other hypervisors (namely Hyper-V) don't set it, we end up
6558 * advancing IP with some random value. Disable fast mmio when
6559 * running nested and keep it for real hardware in hope that
6560 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6561 */
6562 if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) {
6563 skip_emulated_instruction(vcpu);
6564 return 1;
6565 }
6566 else
6567 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6568 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006569 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006570
Paolo Bonzini43a39a32017-08-17 18:36:56 +02006571 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6572 if (ret >= 0)
6573 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006574
6575 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006576 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006577
Avi Kivity851ba692009-08-24 11:10:17 +03006578 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6579 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006580
6581 return 0;
6582}
6583
Avi Kivity851ba692009-08-24 11:10:17 +03006584static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006585{
6586 u32 cpu_based_vm_exec_control;
6587
6588 /* clear pending NMI */
6589 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6590 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6591 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6592 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006593 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006594
6595 return 1;
6596}
6597
Mohammed Gamal80ced182009-09-01 12:48:18 +02006598static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006599{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006600 struct vcpu_vmx *vmx = to_vmx(vcpu);
6601 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006602 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006603 u32 cpu_exec_ctrl;
6604 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006605 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006606
6607 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6608 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006609
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006610 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006611 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006612 return handle_interrupt_window(&vmx->vcpu);
6613
Avi Kivityde87dcd2012-06-12 20:21:38 +03006614 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6615 return 1;
6616
Liran Alon114de9b2017-11-05 16:56:34 +02006617 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006618
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006619 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006620 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006621 ret = 0;
6622 goto out;
6623 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006624
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006625 if (err != EMULATE_DONE)
6626 goto emulation_error;
6627
6628 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6629 vcpu->arch.exception.pending)
6630 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006631
Gleb Natapov8d76c492013-05-08 18:38:44 +03006632 if (vcpu->arch.halt_request) {
6633 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006634 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006635 goto out;
6636 }
6637
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006638 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006639 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006640 if (need_resched())
6641 schedule();
6642 }
6643
Mohammed Gamal80ced182009-09-01 12:48:18 +02006644out:
6645 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006646
6647emulation_error:
6648 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6649 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6650 vcpu->run->internal.ndata = 0;
6651 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006652}
6653
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006654static int __grow_ple_window(int val)
6655{
6656 if (ple_window_grow < 1)
6657 return ple_window;
6658
6659 val = min(val, ple_window_actual_max);
6660
6661 if (ple_window_grow < ple_window)
6662 val *= ple_window_grow;
6663 else
6664 val += ple_window_grow;
6665
6666 return val;
6667}
6668
6669static int __shrink_ple_window(int val, int modifier, int minimum)
6670{
6671 if (modifier < 1)
6672 return ple_window;
6673
6674 if (modifier < ple_window)
6675 val /= modifier;
6676 else
6677 val -= modifier;
6678
6679 return max(val, minimum);
6680}
6681
6682static void grow_ple_window(struct kvm_vcpu *vcpu)
6683{
6684 struct vcpu_vmx *vmx = to_vmx(vcpu);
6685 int old = vmx->ple_window;
6686
6687 vmx->ple_window = __grow_ple_window(old);
6688
6689 if (vmx->ple_window != old)
6690 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006691
6692 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006693}
6694
6695static void shrink_ple_window(struct kvm_vcpu *vcpu)
6696{
6697 struct vcpu_vmx *vmx = to_vmx(vcpu);
6698 int old = vmx->ple_window;
6699
6700 vmx->ple_window = __shrink_ple_window(old,
6701 ple_window_shrink, ple_window);
6702
6703 if (vmx->ple_window != old)
6704 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006705
6706 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006707}
6708
6709/*
6710 * ple_window_actual_max is computed to be one grow_ple_window() below
6711 * ple_window_max. (See __grow_ple_window for the reason.)
6712 * This prevents overflows, because ple_window_max is int.
6713 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6714 * this process.
6715 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6716 */
6717static void update_ple_window_actual_max(void)
6718{
6719 ple_window_actual_max =
6720 __shrink_ple_window(max(ple_window_max, ple_window),
6721 ple_window_grow, INT_MIN);
6722}
6723
Feng Wubf9f6ac2015-09-18 22:29:55 +08006724/*
6725 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6726 */
6727static void wakeup_handler(void)
6728{
6729 struct kvm_vcpu *vcpu;
6730 int cpu = smp_processor_id();
6731
6732 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6733 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6734 blocked_vcpu_list) {
6735 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6736
6737 if (pi_test_on(pi_desc) == 1)
6738 kvm_vcpu_kick(vcpu);
6739 }
6740 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6741}
6742
Tiejun Chenf2c76482014-10-28 10:14:47 +08006743static __init int hardware_setup(void)
6744{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006745 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006746
6747 rdmsrl_safe(MSR_EFER, &host_efer);
6748
6749 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6750 kvm_define_shared_msr(i, vmx_msr_index[i]);
6751
6752 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6753 if (!vmx_io_bitmap_a)
6754 return r;
6755
6756 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6757 if (!vmx_io_bitmap_b)
6758 goto out;
6759
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006760 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6761 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006762 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006763
6764 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6765 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006766 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006767
6768 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6769 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6770
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006771 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006772
6773 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6774
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006775 if (setup_vmcs_config(&vmcs_config) < 0) {
6776 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006777 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006778 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006779
6780 if (boot_cpu_has(X86_FEATURE_NX))
6781 kvm_enable_efer_bits(EFER_NX);
6782
Wanpeng Li2df19692017-03-23 05:30:08 -07006783 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6784 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006785 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006786
Tiejun Chenf2c76482014-10-28 10:14:47 +08006787 if (!cpu_has_vmx_shadow_vmcs())
6788 enable_shadow_vmcs = 0;
6789 if (enable_shadow_vmcs)
6790 init_vmcs_shadow_fields();
6791
6792 if (!cpu_has_vmx_ept() ||
6793 !cpu_has_vmx_ept_4levels()) {
6794 enable_ept = 0;
6795 enable_unrestricted_guest = 0;
6796 enable_ept_ad_bits = 0;
6797 }
6798
6799 if (!cpu_has_vmx_ept_ad_bits())
6800 enable_ept_ad_bits = 0;
6801
6802 if (!cpu_has_vmx_unrestricted_guest())
6803 enable_unrestricted_guest = 0;
6804
Paolo Bonziniad15a292015-01-30 16:18:49 +01006805 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006806 flexpriority_enabled = 0;
6807
Paolo Bonziniad15a292015-01-30 16:18:49 +01006808 /*
6809 * set_apic_access_page_addr() is used to reload apic access
6810 * page upon invalidation. No need to do anything if not
6811 * using the APIC_ACCESS_ADDR VMCS field.
6812 */
6813 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006814 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006815
6816 if (!cpu_has_vmx_tpr_shadow())
6817 kvm_x86_ops->update_cr8_intercept = NULL;
6818
6819 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6820 kvm_disable_largepages();
6821
6822 if (!cpu_has_vmx_ple())
6823 ple_gap = 0;
6824
6825 if (!cpu_has_vmx_apicv())
6826 enable_apicv = 0;
6827
Haozhong Zhang64903d62015-10-20 15:39:09 +08006828 if (cpu_has_vmx_tsc_scaling()) {
6829 kvm_has_tsc_control = true;
6830 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6831 kvm_tsc_scaling_ratio_frac_bits = 48;
6832 }
6833
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006834 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6835
Tiejun Chenbaa03522014-12-23 16:21:11 +08006836 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006837 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006838 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6839 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006840 0ull, VMX_EPT_EXECUTABLE_MASK,
6841 cpu_has_vmx_ept_execute_only() ?
6842 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006843 ept_set_mmio_spte_mask();
6844 kvm_enable_tdp();
6845 } else
6846 kvm_disable_tdp();
6847
6848 update_ple_window_actual_max();
6849
Kai Huang843e4332015-01-28 10:54:28 +08006850 /*
6851 * Only enable PML when hardware supports PML feature, and both EPT
6852 * and EPT A/D bit features are enabled -- PML depends on them to work.
6853 */
6854 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6855 enable_pml = 0;
6856
6857 if (!enable_pml) {
6858 kvm_x86_ops->slot_enable_log_dirty = NULL;
6859 kvm_x86_ops->slot_disable_log_dirty = NULL;
6860 kvm_x86_ops->flush_log_dirty = NULL;
6861 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6862 }
6863
Yunhong Jiang64672c92016-06-13 14:19:59 -07006864 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6865 u64 vmx_msr;
6866
6867 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6868 cpu_preemption_timer_multi =
6869 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6870 } else {
6871 kvm_x86_ops->set_hv_timer = NULL;
6872 kvm_x86_ops->cancel_hv_timer = NULL;
6873 }
6874
Feng Wubf9f6ac2015-09-18 22:29:55 +08006875 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6876
Ashok Rajc45dcc72016-06-22 14:59:56 +08006877 kvm_mce_cap_supported |= MCG_LMCE_P;
6878
Tiejun Chenf2c76482014-10-28 10:14:47 +08006879 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006880
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006881out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006882 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006883out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006884 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006885out1:
6886 free_page((unsigned long)vmx_io_bitmap_b);
6887out:
6888 free_page((unsigned long)vmx_io_bitmap_a);
6889
6890 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006891}
6892
6893static __exit void hardware_unsetup(void)
6894{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006895 free_page((unsigned long)vmx_io_bitmap_b);
6896 free_page((unsigned long)vmx_io_bitmap_a);
6897 free_page((unsigned long)vmx_vmwrite_bitmap);
6898 free_page((unsigned long)vmx_vmread_bitmap);
6899
Tiejun Chenf2c76482014-10-28 10:14:47 +08006900 free_kvm_area();
6901}
6902
Avi Kivity6aa8b732006-12-10 02:21:36 -08006903/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006904 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6905 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6906 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006907static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006908{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006909 if (ple_gap)
6910 grow_ple_window(vcpu);
6911
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006912 skip_emulated_instruction(vcpu);
6913 kvm_vcpu_on_spin(vcpu);
6914
6915 return 1;
6916}
6917
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006918static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006919{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006920 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006921 return 1;
6922}
6923
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006924static int handle_mwait(struct kvm_vcpu *vcpu)
6925{
6926 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6927 return handle_nop(vcpu);
6928}
6929
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006930static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6931{
6932 return 1;
6933}
6934
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006935static int handle_monitor(struct kvm_vcpu *vcpu)
6936{
6937 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6938 return handle_nop(vcpu);
6939}
6940
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006941/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006942 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6943 * set the success or error code of an emulated VMX instruction, as specified
6944 * by Vol 2B, VMX Instruction Reference, "Conventions".
6945 */
6946static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6947{
6948 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6949 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6950 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6951}
6952
6953static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6954{
6955 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6956 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6957 X86_EFLAGS_SF | X86_EFLAGS_OF))
6958 | X86_EFLAGS_CF);
6959}
6960
Abel Gordon145c28d2013-04-18 14:36:55 +03006961static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006962 u32 vm_instruction_error)
6963{
6964 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6965 /*
6966 * failValid writes the error number to the current VMCS, which
6967 * can't be done there isn't a current VMCS.
6968 */
6969 nested_vmx_failInvalid(vcpu);
6970 return;
6971 }
6972 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6973 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6974 X86_EFLAGS_SF | X86_EFLAGS_OF))
6975 | X86_EFLAGS_ZF);
6976 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6977 /*
6978 * We don't need to force a shadow sync because
6979 * VM_INSTRUCTION_ERROR is not shadowed
6980 */
6981}
Abel Gordon145c28d2013-04-18 14:36:55 +03006982
Wincy Vanff651cb2014-12-11 08:52:58 +03006983static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6984{
6985 /* TODO: not to reset guest simply here. */
6986 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006987 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006988}
6989
Jan Kiszkaf4124502014-03-07 20:03:13 +01006990static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6991{
6992 struct vcpu_vmx *vmx =
6993 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6994
6995 vmx->nested.preemption_timer_expired = true;
6996 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6997 kvm_vcpu_kick(&vmx->vcpu);
6998
6999 return HRTIMER_NORESTART;
7000}
7001
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007002/*
Bandan Das19677e32014-05-06 02:19:15 -04007003 * Decode the memory-address operand of a vmx instruction, as recorded on an
7004 * exit caused by such an instruction (run by a guest hypervisor).
7005 * On success, returns 0. When the operand is invalid, returns 1 and throws
7006 * #UD or #GP.
7007 */
7008static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7009 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007010 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007011{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007012 gva_t off;
7013 bool exn;
7014 struct kvm_segment s;
7015
Bandan Das19677e32014-05-06 02:19:15 -04007016 /*
7017 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7018 * Execution", on an exit, vmx_instruction_info holds most of the
7019 * addressing components of the operand. Only the displacement part
7020 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7021 * For how an actual address is calculated from all these components,
7022 * refer to Vol. 1, "Operand Addressing".
7023 */
7024 int scaling = vmx_instruction_info & 3;
7025 int addr_size = (vmx_instruction_info >> 7) & 7;
7026 bool is_reg = vmx_instruction_info & (1u << 10);
7027 int seg_reg = (vmx_instruction_info >> 15) & 7;
7028 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7029 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7030 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7031 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7032
7033 if (is_reg) {
7034 kvm_queue_exception(vcpu, UD_VECTOR);
7035 return 1;
7036 }
7037
7038 /* Addr = segment_base + offset */
7039 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007040 off = exit_qualification; /* holds the displacement */
Sean Christopherson97483542019-01-23 14:39:23 -08007041 if (addr_size == 1)
7042 off = (gva_t)sign_extend64(off, 31);
7043 else if (addr_size == 0)
7044 off = (gva_t)sign_extend64(off, 15);
Bandan Das19677e32014-05-06 02:19:15 -04007045 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007046 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007047 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007048 off += kvm_register_read(vcpu, index_reg)<<scaling;
7049 vmx_get_segment(vcpu, &s, seg_reg);
7050 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007051
7052 if (addr_size == 1) /* 32 bit */
7053 *ret &= 0xffffffff;
7054
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007055 /* Checks for #GP/#SS exceptions. */
7056 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007057 if (is_long_mode(vcpu)) {
7058 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7059 * non-canonical form. This is the only check on the memory
7060 * destination for long mode!
7061 */
7062 exn = is_noncanonical_address(*ret);
7063 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007064 /* Protected mode: apply checks for segment validity in the
7065 * following order:
7066 * - segment type check (#GP(0) may be thrown)
7067 * - usability check (#GP(0)/#SS(0))
7068 * - limit check (#GP(0)/#SS(0))
7069 */
7070 if (wr)
7071 /* #GP(0) if the destination operand is located in a
7072 * read-only data segment or any code segment.
7073 */
7074 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7075 else
7076 /* #GP(0) if the source operand is located in an
7077 * execute-only code segment
7078 */
7079 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007080 if (exn) {
7081 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7082 return 1;
7083 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007084 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7085 */
7086 exn = (s.unusable != 0);
Sean Christopherson7b3c6c42019-01-23 14:39:25 -08007087
7088 /*
7089 * Protected mode: #GP(0)/#SS(0) if the memory operand is
7090 * outside the segment limit. All CPUs that support VMX ignore
7091 * limit checks for flat segments, i.e. segments with base==0,
7092 * limit==0xffffffff and of type expand-up data or code.
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007093 */
Sean Christopherson7b3c6c42019-01-23 14:39:25 -08007094 if (!(s.base == 0 && s.limit == 0xffffffff &&
7095 ((s.type & 8) || !(s.type & 4))))
7096 exn = exn || (off + sizeof(u64) > s.limit);
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007097 }
7098 if (exn) {
7099 kvm_queue_exception_e(vcpu,
7100 seg_reg == VCPU_SREG_SS ?
7101 SS_VECTOR : GP_VECTOR,
7102 0);
7103 return 1;
7104 }
7105
Bandan Das19677e32014-05-06 02:19:15 -04007106 return 0;
7107}
7108
7109/*
Bandan Das3573e222014-05-06 02:19:16 -04007110 * This function performs the various checks including
7111 * - if it's 4KB aligned
7112 * - No bits beyond the physical address width are set
7113 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04007114 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04007115 */
Bandan Das4291b582014-05-06 02:19:18 -04007116static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
7117 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007118{
7119 gva_t gva;
7120 gpa_t vmptr;
7121 struct x86_exception e;
7122 struct page *page;
7123 struct vcpu_vmx *vmx = to_vmx(vcpu);
7124 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7125
7126 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007127 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007128 return 1;
7129
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007130 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007131 kvm_inject_page_fault(vcpu, &e);
7132 return 1;
7133 }
7134
7135 switch (exit_reason) {
7136 case EXIT_REASON_VMON:
7137 /*
7138 * SDM 3: 24.11.5
7139 * The first 4 bytes of VMXON region contain the supported
7140 * VMCS revision identifier
7141 *
7142 * Note - IA32_VMX_BASIC[48] will never be 1
7143 * for the nested case;
7144 * which replaces physical address width with 32
7145 *
7146 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007147 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007148 nested_vmx_failInvalid(vcpu);
7149 skip_emulated_instruction(vcpu);
7150 return 1;
7151 }
7152
7153 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007154 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007155 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007156 skip_emulated_instruction(vcpu);
7157 return 1;
7158 }
7159 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007160 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007161 nested_release_page_clean(page);
7162 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007163 skip_emulated_instruction(vcpu);
7164 return 1;
7165 }
7166 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007167 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007168 vmx->nested.vmxon_ptr = vmptr;
7169 break;
Bandan Das4291b582014-05-06 02:19:18 -04007170 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007171 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007172 nested_vmx_failValid(vcpu,
7173 VMXERR_VMCLEAR_INVALID_ADDRESS);
7174 skip_emulated_instruction(vcpu);
7175 return 1;
7176 }
Bandan Das3573e222014-05-06 02:19:16 -04007177
Bandan Das4291b582014-05-06 02:19:18 -04007178 if (vmptr == vmx->nested.vmxon_ptr) {
7179 nested_vmx_failValid(vcpu,
7180 VMXERR_VMCLEAR_VMXON_POINTER);
7181 skip_emulated_instruction(vcpu);
7182 return 1;
7183 }
7184 break;
7185 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007186 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007187 nested_vmx_failValid(vcpu,
7188 VMXERR_VMPTRLD_INVALID_ADDRESS);
7189 skip_emulated_instruction(vcpu);
7190 return 1;
7191 }
7192
7193 if (vmptr == vmx->nested.vmxon_ptr) {
7194 nested_vmx_failValid(vcpu,
7195 VMXERR_VMCLEAR_VMXON_POINTER);
7196 skip_emulated_instruction(vcpu);
7197 return 1;
7198 }
7199 break;
Bandan Das3573e222014-05-06 02:19:16 -04007200 default:
7201 return 1; /* shouldn't happen */
7202 }
7203
Bandan Das4291b582014-05-06 02:19:18 -04007204 if (vmpointer)
7205 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007206 return 0;
7207}
7208
7209/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007210 * Emulate the VMXON instruction.
7211 * Currently, we just remember that VMX is active, and do not save or even
7212 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7213 * do not currently need to store anything in that guest-allocated memory
7214 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7215 * argument is different from the VMXON pointer (which the spec says they do).
7216 */
7217static int handle_vmon(struct kvm_vcpu *vcpu)
7218{
7219 struct kvm_segment cs;
7220 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007221 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007222 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7223 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007224 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007225
7226 /* The Intel VMX Instruction Reference lists a bunch of bits that
7227 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7228 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7229 * Otherwise, we should fail with #UD. We test these now:
7230 */
7231 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7232 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7233 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7234 kvm_queue_exception(vcpu, UD_VECTOR);
7235 return 1;
7236 }
7237
7238 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7239 if (is_long_mode(vcpu) && !cs.l) {
7240 kvm_queue_exception(vcpu, UD_VECTOR);
7241 return 1;
7242 }
7243
7244 if (vmx_get_cpl(vcpu)) {
7245 kvm_inject_gp(vcpu, 0);
7246 return 1;
7247 }
Bandan Das3573e222014-05-06 02:19:16 -04007248
Bandan Das4291b582014-05-06 02:19:18 -04007249 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007250 return 1;
7251
Abel Gordon145c28d2013-04-18 14:36:55 +03007252 if (vmx->nested.vmxon) {
7253 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7254 skip_emulated_instruction(vcpu);
7255 return 1;
7256 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007257
Haozhong Zhang3b840802016-06-22 14:59:54 +08007258 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007259 != VMXON_NEEDED_FEATURES) {
7260 kvm_inject_gp(vcpu, 0);
7261 return 1;
7262 }
7263
Paolo Bonziniff546f92018-01-11 12:16:15 +01007264 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7265 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007266 goto out_vmcs02;
Radim Krčmářd048c092016-08-08 20:16:22 +02007267
David Matlack4f2777b2016-07-13 17:16:37 -07007268 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7269 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007270 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007271
Abel Gordon8de48832013-04-18 14:37:25 +03007272 if (enable_shadow_vmcs) {
7273 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007274 if (!shadow_vmcs)
7275 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007276 /* mark vmcs as shadow */
7277 shadow_vmcs->revision_id |= (1u << 31);
7278 /* init shadow vmcs */
7279 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007280 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007281 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007282
Jan Kiszkaf4124502014-03-07 20:03:13 +01007283 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007284 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007285 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7286
Roman Kagan020a90f2018-07-19 21:59:07 +03007287 vmx->nested.vpid02 = allocate_vpid();
7288
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007289 vmx->nested.vmxon = true;
7290
7291 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007292 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007293 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007294
7295out_shadow_vmcs:
7296 kfree(vmx->nested.cached_vmcs12);
7297
7298out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007299 free_loaded_vmcs(&vmx->nested.vmcs02);
Radim Krčmářd048c092016-08-08 20:16:22 +02007300
Jim Mattson46e24df2017-11-27 17:22:25 -06007301out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007302 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007303}
7304
7305/*
7306 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7307 * for running VMX instructions (except VMXON, whose prerequisites are
7308 * slightly different). It also specifies what exception to inject otherwise.
7309 */
7310static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7311{
7312 struct kvm_segment cs;
7313 struct vcpu_vmx *vmx = to_vmx(vcpu);
7314
7315 if (!vmx->nested.vmxon) {
7316 kvm_queue_exception(vcpu, UD_VECTOR);
7317 return 0;
7318 }
7319
7320 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7321 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7322 (is_long_mode(vcpu) && !cs.l)) {
7323 kvm_queue_exception(vcpu, UD_VECTOR);
7324 return 0;
7325 }
7326
7327 if (vmx_get_cpl(vcpu)) {
7328 kvm_inject_gp(vcpu, 0);
7329 return 0;
7330 }
7331
7332 return 1;
7333}
7334
Abel Gordone7953d72013-04-18 14:37:55 +03007335static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7336{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007337 if (vmx->nested.current_vmptr == -1ull)
7338 return;
7339
7340 /* current_vmptr and current_vmcs12 are always set/reset together */
7341 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7342 return;
7343
Abel Gordon012f83c2013-04-18 14:39:25 +03007344 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007345 /* copy to memory all shadowed fields in case
7346 they were modified */
7347 copy_shadow_to_vmcs12(vmx);
7348 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007349 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7350 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007351 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007352 }
Wincy Van705699a2015-02-03 23:58:17 +08007353 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007354
7355 /* Flush VMCS12 to guest memory */
7356 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7357 VMCS12_SIZE);
7358
Abel Gordone7953d72013-04-18 14:37:55 +03007359 kunmap(vmx->nested.current_vmcs12_page);
7360 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007361 vmx->nested.current_vmptr = -1ull;
7362 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007363}
7364
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007365/*
7366 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7367 * just stops using VMX.
7368 */
7369static void free_nested(struct vcpu_vmx *vmx)
7370{
7371 if (!vmx->nested.vmxon)
7372 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007373
Peter Shiera2c34d22018-10-11 11:46:46 -07007374 hrtimer_cancel(&vmx->nested.preemption_timer);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007375 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007376 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007377 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007378 if (enable_shadow_vmcs) {
7379 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7380 free_vmcs(vmx->vmcs01.shadow_vmcs);
7381 vmx->vmcs01.shadow_vmcs = NULL;
7382 }
David Matlack4f2777b2016-07-13 17:16:37 -07007383 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007384 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007385 if (vmx->nested.apic_access_page) {
7386 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007387 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007388 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007389 if (vmx->nested.virtual_apic_page) {
7390 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007391 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007392 }
Wincy Van705699a2015-02-03 23:58:17 +08007393 if (vmx->nested.pi_desc_page) {
7394 kunmap(vmx->nested.pi_desc_page);
7395 nested_release_page(vmx->nested.pi_desc_page);
7396 vmx->nested.pi_desc_page = NULL;
7397 vmx->nested.pi_desc = NULL;
7398 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007399
Jim Mattson46e24df2017-11-27 17:22:25 -06007400 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007401}
7402
7403/* Emulate the VMXOFF instruction */
7404static int handle_vmoff(struct kvm_vcpu *vcpu)
7405{
7406 if (!nested_vmx_check_permission(vcpu))
7407 return 1;
7408 free_nested(to_vmx(vcpu));
7409 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007410 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007411 return 1;
7412}
7413
Nadav Har'El27d6c862011-05-25 23:06:59 +03007414/* Emulate the VMCLEAR instruction */
7415static int handle_vmclear(struct kvm_vcpu *vcpu)
7416{
7417 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007418 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007419 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007420
7421 if (!nested_vmx_check_permission(vcpu))
7422 return 1;
7423
Bandan Das4291b582014-05-06 02:19:18 -04007424 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007425 return 1;
7426
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007427 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007428 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007429
Jim Mattson29deec42017-03-02 12:41:48 -08007430 kvm_vcpu_write_guest(vcpu,
7431 vmptr + offsetof(struct vmcs12, launch_state),
7432 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007433
Nadav Har'El27d6c862011-05-25 23:06:59 +03007434 skip_emulated_instruction(vcpu);
7435 nested_vmx_succeed(vcpu);
7436 return 1;
7437}
7438
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007439static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7440
7441/* Emulate the VMLAUNCH instruction */
7442static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7443{
7444 return nested_vmx_run(vcpu, true);
7445}
7446
7447/* Emulate the VMRESUME instruction */
7448static int handle_vmresume(struct kvm_vcpu *vcpu)
7449{
7450
7451 return nested_vmx_run(vcpu, false);
7452}
7453
Nadav Har'El49f705c2011-05-25 23:08:30 +03007454enum vmcs_field_type {
7455 VMCS_FIELD_TYPE_U16 = 0,
7456 VMCS_FIELD_TYPE_U64 = 1,
7457 VMCS_FIELD_TYPE_U32 = 2,
7458 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7459};
7460
7461static inline int vmcs_field_type(unsigned long field)
7462{
7463 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7464 return VMCS_FIELD_TYPE_U32;
7465 return (field >> 13) & 0x3 ;
7466}
7467
7468static inline int vmcs_field_readonly(unsigned long field)
7469{
7470 return (((field >> 10) & 0x3) == 1);
7471}
7472
7473/*
7474 * Read a vmcs12 field. Since these can have varying lengths and we return
7475 * one type, we chose the biggest type (u64) and zero-extend the return value
7476 * to that size. Note that the caller, handle_vmread, might need to use only
7477 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7478 * 64-bit fields are to be returned).
7479 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007480static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7481 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007482{
7483 short offset = vmcs_field_to_offset(field);
7484 char *p;
7485
7486 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007487 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007488
7489 p = ((char *)(get_vmcs12(vcpu))) + offset;
7490
7491 switch (vmcs_field_type(field)) {
7492 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7493 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007494 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007495 case VMCS_FIELD_TYPE_U16:
7496 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007497 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007498 case VMCS_FIELD_TYPE_U32:
7499 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007500 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007501 case VMCS_FIELD_TYPE_U64:
7502 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007503 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007504 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007505 WARN_ON(1);
7506 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007507 }
7508}
7509
Abel Gordon20b97fe2013-04-18 14:36:25 +03007510
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007511static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7512 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007513 short offset = vmcs_field_to_offset(field);
7514 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7515 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007516 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007517
7518 switch (vmcs_field_type(field)) {
7519 case VMCS_FIELD_TYPE_U16:
7520 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007521 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007522 case VMCS_FIELD_TYPE_U32:
7523 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007524 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007525 case VMCS_FIELD_TYPE_U64:
7526 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007527 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007528 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7529 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007530 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007531 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007532 WARN_ON(1);
7533 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007534 }
7535
7536}
7537
Abel Gordon16f5b902013-04-18 14:38:25 +03007538static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7539{
7540 int i;
7541 unsigned long field;
7542 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007543 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007544 const unsigned long *fields = shadow_read_write_fields;
7545 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007546
Jan Kiszka282da872014-10-08 18:05:39 +02007547 preempt_disable();
7548
Abel Gordon16f5b902013-04-18 14:38:25 +03007549 vmcs_load(shadow_vmcs);
7550
7551 for (i = 0; i < num_fields; i++) {
7552 field = fields[i];
7553 switch (vmcs_field_type(field)) {
7554 case VMCS_FIELD_TYPE_U16:
7555 field_value = vmcs_read16(field);
7556 break;
7557 case VMCS_FIELD_TYPE_U32:
7558 field_value = vmcs_read32(field);
7559 break;
7560 case VMCS_FIELD_TYPE_U64:
7561 field_value = vmcs_read64(field);
7562 break;
7563 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7564 field_value = vmcs_readl(field);
7565 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007566 default:
7567 WARN_ON(1);
7568 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007569 }
7570 vmcs12_write_any(&vmx->vcpu, field, field_value);
7571 }
7572
7573 vmcs_clear(shadow_vmcs);
7574 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007575
7576 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007577}
7578
Abel Gordonc3114422013-04-18 14:38:55 +03007579static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7580{
Mathias Krausec2bae892013-06-26 20:36:21 +02007581 const unsigned long *fields[] = {
7582 shadow_read_write_fields,
7583 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007584 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007585 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007586 max_shadow_read_write_fields,
7587 max_shadow_read_only_fields
7588 };
7589 int i, q;
7590 unsigned long field;
7591 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007592 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007593
7594 vmcs_load(shadow_vmcs);
7595
Mathias Krausec2bae892013-06-26 20:36:21 +02007596 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007597 for (i = 0; i < max_fields[q]; i++) {
7598 field = fields[q][i];
7599 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7600
7601 switch (vmcs_field_type(field)) {
7602 case VMCS_FIELD_TYPE_U16:
7603 vmcs_write16(field, (u16)field_value);
7604 break;
7605 case VMCS_FIELD_TYPE_U32:
7606 vmcs_write32(field, (u32)field_value);
7607 break;
7608 case VMCS_FIELD_TYPE_U64:
7609 vmcs_write64(field, (u64)field_value);
7610 break;
7611 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7612 vmcs_writel(field, (long)field_value);
7613 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007614 default:
7615 WARN_ON(1);
7616 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007617 }
7618 }
7619 }
7620
7621 vmcs_clear(shadow_vmcs);
7622 vmcs_load(vmx->loaded_vmcs->vmcs);
7623}
7624
Nadav Har'El49f705c2011-05-25 23:08:30 +03007625/*
7626 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7627 * used before) all generate the same failure when it is missing.
7628 */
7629static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7630{
7631 struct vcpu_vmx *vmx = to_vmx(vcpu);
7632 if (vmx->nested.current_vmptr == -1ull) {
7633 nested_vmx_failInvalid(vcpu);
7634 skip_emulated_instruction(vcpu);
7635 return 0;
7636 }
7637 return 1;
7638}
7639
7640static int handle_vmread(struct kvm_vcpu *vcpu)
7641{
7642 unsigned long field;
7643 u64 field_value;
7644 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7645 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7646 gva_t gva = 0;
Paolo Bonzinidfa37272019-09-14 00:26:27 +02007647 struct x86_exception e;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007648
7649 if (!nested_vmx_check_permission(vcpu) ||
7650 !nested_vmx_check_vmcs12(vcpu))
7651 return 1;
7652
7653 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007654 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007655 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007656 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007657 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7658 skip_emulated_instruction(vcpu);
7659 return 1;
7660 }
7661 /*
7662 * Now copy part of this value to register or memory, as requested.
7663 * Note that the number of bits actually copied is 32 or 64 depending
7664 * on the guest's mode (32 or 64 bit), not on the given field's length.
7665 */
7666 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007667 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007668 field_value);
7669 } else {
7670 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007671 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007672 return 1;
7673 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzinidfa37272019-09-14 00:26:27 +02007674 if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
7675 (is_long_mode(vcpu) ? 8 : 4),
Miaohe Lin0b414c52019-12-28 14:25:24 +08007676 &e)) {
Paolo Bonzinidfa37272019-09-14 00:26:27 +02007677 kvm_inject_page_fault(vcpu, &e);
Miaohe Lin0b414c52019-12-28 14:25:24 +08007678 return 1;
7679 }
Nadav Har'El49f705c2011-05-25 23:08:30 +03007680 }
7681
7682 nested_vmx_succeed(vcpu);
7683 skip_emulated_instruction(vcpu);
7684 return 1;
7685}
7686
7687
7688static int handle_vmwrite(struct kvm_vcpu *vcpu)
7689{
7690 unsigned long field;
7691 gva_t gva;
7692 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7693 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007694 /* The value to write might be 32 or 64 bits, depending on L1's long
7695 * mode, and eventually we need to write that into a field of several
7696 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007697 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007698 * bits into the vmcs12 field.
7699 */
7700 u64 field_value = 0;
7701 struct x86_exception e;
7702
7703 if (!nested_vmx_check_permission(vcpu) ||
7704 !nested_vmx_check_vmcs12(vcpu))
7705 return 1;
7706
7707 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007708 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007709 (((vmx_instruction_info) >> 3) & 0xf));
7710 else {
7711 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007712 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007713 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007714 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7715 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007716 kvm_inject_page_fault(vcpu, &e);
7717 return 1;
7718 }
7719 }
7720
7721
Nadav Amit27e6fb52014-06-18 17:19:26 +03007722 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007723 if (vmcs_field_readonly(field)) {
7724 nested_vmx_failValid(vcpu,
7725 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7726 skip_emulated_instruction(vcpu);
7727 return 1;
7728 }
7729
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007730 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007731 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7732 skip_emulated_instruction(vcpu);
7733 return 1;
7734 }
7735
7736 nested_vmx_succeed(vcpu);
7737 skip_emulated_instruction(vcpu);
7738 return 1;
7739}
7740
Nadav Har'El63846662011-05-25 23:07:29 +03007741/* Emulate the VMPTRLD instruction */
7742static int handle_vmptrld(struct kvm_vcpu *vcpu)
7743{
7744 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007745 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007746
7747 if (!nested_vmx_check_permission(vcpu))
7748 return 1;
7749
Bandan Das4291b582014-05-06 02:19:18 -04007750 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007751 return 1;
7752
Nadav Har'El63846662011-05-25 23:07:29 +03007753 if (vmx->nested.current_vmptr != vmptr) {
7754 struct vmcs12 *new_vmcs12;
7755 struct page *page;
7756 page = nested_get_page(vcpu, vmptr);
7757 if (page == NULL) {
7758 nested_vmx_failInvalid(vcpu);
7759 skip_emulated_instruction(vcpu);
7760 return 1;
7761 }
7762 new_vmcs12 = kmap(page);
7763 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7764 kunmap(page);
7765 nested_release_page_clean(page);
7766 nested_vmx_failValid(vcpu,
7767 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7768 skip_emulated_instruction(vcpu);
7769 return 1;
7770 }
Nadav Har'El63846662011-05-25 23:07:29 +03007771
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007772 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007773 vmx->nested.current_vmptr = vmptr;
7774 vmx->nested.current_vmcs12 = new_vmcs12;
7775 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007776 /*
7777 * Load VMCS12 from guest memory since it is not already
7778 * cached.
7779 */
7780 memcpy(vmx->nested.cached_vmcs12,
7781 vmx->nested.current_vmcs12, VMCS12_SIZE);
7782
Abel Gordon012f83c2013-04-18 14:39:25 +03007783 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007784 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7785 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007786 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007787 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007788 vmx->nested.sync_shadow_vmcs = true;
7789 }
Nadav Har'El63846662011-05-25 23:07:29 +03007790 }
7791
7792 nested_vmx_succeed(vcpu);
7793 skip_emulated_instruction(vcpu);
7794 return 1;
7795}
7796
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007797/* Emulate the VMPTRST instruction */
7798static int handle_vmptrst(struct kvm_vcpu *vcpu)
7799{
7800 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7801 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7802 gva_t vmcs_gva;
7803 struct x86_exception e;
7804
7805 if (!nested_vmx_check_permission(vcpu))
7806 return 1;
7807
7808 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007809 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007810 return 1;
7811 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007812 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7813 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7814 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007815 kvm_inject_page_fault(vcpu, &e);
7816 return 1;
7817 }
7818 nested_vmx_succeed(vcpu);
7819 skip_emulated_instruction(vcpu);
7820 return 1;
7821}
7822
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007823/* Emulate the INVEPT instruction */
7824static int handle_invept(struct kvm_vcpu *vcpu)
7825{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007826 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007827 u32 vmx_instruction_info, types;
7828 unsigned long type;
7829 gva_t gva;
7830 struct x86_exception e;
7831 struct {
7832 u64 eptp, gpa;
7833 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007834
Wincy Vanb9c237b2015-02-03 23:56:30 +08007835 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7836 SECONDARY_EXEC_ENABLE_EPT) ||
7837 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007838 kvm_queue_exception(vcpu, UD_VECTOR);
7839 return 1;
7840 }
7841
7842 if (!nested_vmx_check_permission(vcpu))
7843 return 1;
7844
7845 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7846 kvm_queue_exception(vcpu, UD_VECTOR);
7847 return 1;
7848 }
7849
7850 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007851 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007852
Wincy Vanb9c237b2015-02-03 23:56:30 +08007853 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007854
Jim Mattson85c856b2016-10-26 08:38:38 -07007855 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007856 nested_vmx_failValid(vcpu,
7857 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007858 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007859 return 1;
7860 }
7861
7862 /* According to the Intel VMX instruction reference, the memory
7863 * operand is read even if it isn't needed (e.g., for type==global)
7864 */
7865 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007866 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007867 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007868 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007869 kvm_inject_page_fault(vcpu, &e);
7870 return 1;
7871 }
7872
7873 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007874 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007875 /*
7876 * TODO: track mappings and invalidate
7877 * single context requests appropriately
7878 */
7879 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007880 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007881 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007882 nested_vmx_succeed(vcpu);
7883 break;
7884 default:
7885 BUG_ON(1);
7886 break;
7887 }
7888
7889 skip_emulated_instruction(vcpu);
7890 return 1;
7891}
7892
Petr Matouseka642fc32014-09-23 20:22:30 +02007893static int handle_invvpid(struct kvm_vcpu *vcpu)
7894{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007895 struct vcpu_vmx *vmx = to_vmx(vcpu);
7896 u32 vmx_instruction_info;
7897 unsigned long type, types;
7898 gva_t gva;
7899 struct x86_exception e;
7900 int vpid;
7901
7902 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7903 SECONDARY_EXEC_ENABLE_VPID) ||
7904 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7905 kvm_queue_exception(vcpu, UD_VECTOR);
7906 return 1;
7907 }
7908
7909 if (!nested_vmx_check_permission(vcpu))
7910 return 1;
7911
7912 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7913 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7914
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007915 types = (vmx->nested.nested_vmx_vpid_caps &
7916 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007917
Jim Mattson85c856b2016-10-26 08:38:38 -07007918 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007919 nested_vmx_failValid(vcpu,
7920 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007921 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007922 return 1;
7923 }
7924
7925 /* according to the intel vmx instruction reference, the memory
7926 * operand is read even if it isn't needed (e.g., for type==global)
7927 */
7928 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7929 vmx_instruction_info, false, &gva))
7930 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007931 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007932 kvm_inject_page_fault(vcpu, &e);
7933 return 1;
7934 }
7935
7936 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007937 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007938 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007939 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7940 if (!vpid) {
7941 nested_vmx_failValid(vcpu,
7942 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7943 skip_emulated_instruction(vcpu);
7944 return 1;
7945 }
7946 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007947 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007948 break;
7949 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007950 WARN_ON_ONCE(1);
7951 skip_emulated_instruction(vcpu);
7952 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007953 }
7954
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007955 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7956 nested_vmx_succeed(vcpu);
7957
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007958 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007959 return 1;
7960}
7961
Kai Huang843e4332015-01-28 10:54:28 +08007962static int handle_pml_full(struct kvm_vcpu *vcpu)
7963{
7964 unsigned long exit_qualification;
7965
7966 trace_kvm_pml_full(vcpu->vcpu_id);
7967
7968 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7969
7970 /*
7971 * PML buffer FULL happened while executing iret from NMI,
7972 * "blocked by NMI" bit has to be set before next VM entry.
7973 */
7974 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7975 cpu_has_virtual_nmis() &&
7976 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7977 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7978 GUEST_INTR_STATE_NMI);
7979
7980 /*
7981 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7982 * here.., and there's no userspace involvement needed for PML.
7983 */
7984 return 1;
7985}
7986
Yunhong Jiang64672c92016-06-13 14:19:59 -07007987static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7988{
7989 kvm_lapic_expired_hv_timer(vcpu);
7990 return 1;
7991}
7992
Nadav Har'El0140cae2011-05-25 23:06:28 +03007993/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007994 * The exit handlers return 1 if the exit was handled fully and guest execution
7995 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7996 * to be done to userspace and return 0.
7997 */
Mathias Krause772e0312012-08-30 01:30:19 +02007998static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007999 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8000 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008001 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008002 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008003 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008004 [EXIT_REASON_CR_ACCESS] = handle_cr,
8005 [EXIT_REASON_DR_ACCESS] = handle_dr,
8006 [EXIT_REASON_CPUID] = handle_cpuid,
8007 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8008 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8009 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8010 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008011 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008012 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008013 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008014 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008015 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008016 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008017 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008018 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008019 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008020 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008021 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008022 [EXIT_REASON_VMOFF] = handle_vmoff,
8023 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008024 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8025 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008026 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008027 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008028 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008029 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008030 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008031 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008032 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8033 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008034 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008035 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008036 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008037 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008038 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008039 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008040 [EXIT_REASON_XSAVES] = handle_xsaves,
8041 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008042 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008043 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008044};
8045
8046static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008047 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008048
Oliver Upton35523a22020-02-04 15:26:30 -08008049/*
8050 * Return true if an IO instruction with the specified port and size should cause
8051 * a VM-exit into L1.
8052 */
8053bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
8054 int size)
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008055{
Oliver Upton35523a22020-02-04 15:26:30 -08008056 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008057 gpa_t bitmap, last_bitmap;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008058 u8 b;
8059
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008060 last_bitmap = (gpa_t)-1;
8061 b = -1;
8062
8063 while (size > 0) {
8064 if (port < 0x8000)
8065 bitmap = vmcs12->io_bitmap_a;
8066 else if (port < 0x10000)
8067 bitmap = vmcs12->io_bitmap_b;
8068 else
Joe Perches1d804d02015-03-30 16:46:09 -07008069 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008070 bitmap += (port & 0x7fff) / 8;
8071
8072 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008073 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008074 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008075 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008076 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008077
8078 port++;
8079 size--;
8080 last_bitmap = bitmap;
8081 }
8082
Joe Perches1d804d02015-03-30 16:46:09 -07008083 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008084}
8085
Nadav Har'El644d7112011-05-25 23:12:35 +03008086/*
8087 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8088 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8089 * disinterest in the current event (read or write a specific MSR) by using an
8090 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8091 */
8092static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8093 struct vmcs12 *vmcs12, u32 exit_reason)
8094{
8095 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8096 gpa_t bitmap;
8097
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008098 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008099 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008100
8101 /*
8102 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8103 * for the four combinations of read/write and low/high MSR numbers.
8104 * First we need to figure out which of the four to use:
8105 */
8106 bitmap = vmcs12->msr_bitmap;
8107 if (exit_reason == EXIT_REASON_MSR_WRITE)
8108 bitmap += 2048;
8109 if (msr_index >= 0xc0000000) {
8110 msr_index -= 0xc0000000;
8111 bitmap += 1024;
8112 }
8113
8114 /* Then read the msr_index'th bit from this bitmap: */
8115 if (msr_index < 1024*8) {
8116 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008117 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008118 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008119 return 1 & (b >> (msr_index & 7));
8120 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008121 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008122}
8123
8124/*
8125 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8126 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8127 * intercept (via guest_host_mask etc.) the current event.
8128 */
8129static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8130 struct vmcs12 *vmcs12)
8131{
8132 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8133 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008134 int reg;
8135 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008136
8137 switch ((exit_qualification >> 4) & 3) {
8138 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008139 reg = (exit_qualification >> 8) & 15;
8140 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008141 switch (cr) {
8142 case 0:
8143 if (vmcs12->cr0_guest_host_mask &
8144 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008145 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008146 break;
8147 case 3:
8148 if ((vmcs12->cr3_target_count >= 1 &&
8149 vmcs12->cr3_target_value0 == val) ||
8150 (vmcs12->cr3_target_count >= 2 &&
8151 vmcs12->cr3_target_value1 == val) ||
8152 (vmcs12->cr3_target_count >= 3 &&
8153 vmcs12->cr3_target_value2 == val) ||
8154 (vmcs12->cr3_target_count >= 4 &&
8155 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008156 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008157 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008158 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008159 break;
8160 case 4:
8161 if (vmcs12->cr4_guest_host_mask &
8162 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008163 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008164 break;
8165 case 8:
8166 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008167 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008168 break;
8169 }
8170 break;
8171 case 2: /* clts */
8172 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8173 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008174 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008175 break;
8176 case 1: /* mov from cr */
8177 switch (cr) {
8178 case 3:
8179 if (vmcs12->cpu_based_vm_exec_control &
8180 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008181 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008182 break;
8183 case 8:
8184 if (vmcs12->cpu_based_vm_exec_control &
8185 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008186 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008187 break;
8188 }
8189 break;
8190 case 3: /* lmsw */
8191 /*
8192 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8193 * cr0. Other attempted changes are ignored, with no exit.
8194 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008195 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008196 if (vmcs12->cr0_guest_host_mask & 0xe &
8197 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008198 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008199 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8200 !(vmcs12->cr0_read_shadow & 0x1) &&
8201 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008202 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008203 break;
8204 }
Joe Perches1d804d02015-03-30 16:46:09 -07008205 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008206}
8207
8208/*
8209 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8210 * should handle it ourselves in L0 (and then continue L2). Only call this
8211 * when in is_guest_mode (L2).
8212 */
8213static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8214{
Nadav Har'El644d7112011-05-25 23:12:35 +03008215 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8216 struct vcpu_vmx *vmx = to_vmx(vcpu);
8217 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008218 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008219
Jan Kiszka542060e2014-01-04 18:47:21 +01008220 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8221 vmcs_readl(EXIT_QUALIFICATION),
8222 vmx->idt_vectoring_info,
8223 intr_info,
8224 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8225 KVM_ISA_VMX);
8226
David Matlackb7649e12017-08-01 14:00:40 -07008227 /*
8228 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008229 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8230 * Page). The CPU may write to these pages via their host
8231 * physical address while L2 is running, bypassing any
8232 * address-translation-based dirty tracking (e.g. EPT write
8233 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008234 *
8235 * Mark them dirty on every exit from L2 to prevent them from
8236 * getting out of sync with dirty tracking.
8237 */
8238 nested_mark_vmcs12_pages_dirty(vcpu);
8239
Nadav Har'El644d7112011-05-25 23:12:35 +03008240 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008241 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008242
8243 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008244 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8245 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008246 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008247 }
8248
8249 switch (exit_reason) {
8250 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008251 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008252 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008253 else if (is_page_fault(intr_info))
8254 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008255 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008256 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008257 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008258 else if (is_debug(intr_info) &&
8259 vcpu->guest_debug &
8260 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8261 return false;
8262 else if (is_breakpoint(intr_info) &&
8263 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8264 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008265 return vmcs12->exception_bitmap &
8266 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8267 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008268 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008269 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008270 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008271 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008272 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008273 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008274 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008275 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008276 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008277 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008278 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008279 case EXIT_REASON_HLT:
8280 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8281 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008282 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008283 case EXIT_REASON_INVLPG:
8284 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8285 case EXIT_REASON_RDPMC:
8286 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008287 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008288 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8289 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8290 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8291 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8292 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8293 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008294 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008295 /*
8296 * VMX instructions trap unconditionally. This allows L1 to
8297 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8298 */
Joe Perches1d804d02015-03-30 16:46:09 -07008299 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008300 case EXIT_REASON_CR_ACCESS:
8301 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8302 case EXIT_REASON_DR_ACCESS:
8303 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8304 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008305 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008306 case EXIT_REASON_MSR_READ:
8307 case EXIT_REASON_MSR_WRITE:
8308 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8309 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008310 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008311 case EXIT_REASON_MWAIT_INSTRUCTION:
8312 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008313 case EXIT_REASON_MONITOR_TRAP_FLAG:
8314 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008315 case EXIT_REASON_MONITOR_INSTRUCTION:
8316 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8317 case EXIT_REASON_PAUSE_INSTRUCTION:
8318 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8319 nested_cpu_has2(vmcs12,
8320 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8321 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008322 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008323 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008324 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008325 case EXIT_REASON_APIC_ACCESS:
8326 return nested_cpu_has2(vmcs12,
8327 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008328 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008329 case EXIT_REASON_EOI_INDUCED:
8330 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008331 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008332 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008333 /*
8334 * L0 always deals with the EPT violation. If nested EPT is
8335 * used, and the nested mmu code discovers that the address is
8336 * missing in the guest EPT table (EPT12), the EPT violation
8337 * will be injected with nested_ept_inject_page_fault()
8338 */
Joe Perches1d804d02015-03-30 16:46:09 -07008339 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008340 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008341 /*
8342 * L2 never uses directly L1's EPT, but rather L0's own EPT
8343 * table (shadow on EPT) or a merged EPT table that L0 built
8344 * (EPT on EPT). So any problems with the structure of the
8345 * table is L0's fault.
8346 */
Joe Perches1d804d02015-03-30 16:46:09 -07008347 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008348 case EXIT_REASON_WBINVD:
8349 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8350 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008351 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008352 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8353 /*
8354 * This should never happen, since it is not possible to
8355 * set XSS to a non-zero value---neither in L1 nor in L2.
8356 * If if it were, XSS would have to be checked against
8357 * the XSS exit bitmap in vmcs12.
8358 */
8359 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008360 case EXIT_REASON_PREEMPTION_TIMER:
8361 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008362 case EXIT_REASON_PML_FULL:
8363 /* We don't expose PML support to L1. */
8364 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008365 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008366 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008367 }
8368}
8369
Avi Kivity586f9602010-11-18 13:09:54 +02008370static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8371{
8372 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8373 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8374}
8375
Kai Huanga3eaa862015-11-04 13:46:05 +08008376static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008377{
Kai Huanga3eaa862015-11-04 13:46:05 +08008378 if (vmx->pml_pg) {
8379 __free_page(vmx->pml_pg);
8380 vmx->pml_pg = NULL;
8381 }
Kai Huang843e4332015-01-28 10:54:28 +08008382}
8383
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008384static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008385{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008386 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008387 u64 *pml_buf;
8388 u16 pml_idx;
8389
8390 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8391
8392 /* Do nothing if PML buffer is empty */
8393 if (pml_idx == (PML_ENTITY_NUM - 1))
8394 return;
8395
8396 /* PML index always points to next available PML buffer entity */
8397 if (pml_idx >= PML_ENTITY_NUM)
8398 pml_idx = 0;
8399 else
8400 pml_idx++;
8401
8402 pml_buf = page_address(vmx->pml_pg);
8403 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8404 u64 gpa;
8405
8406 gpa = pml_buf[pml_idx];
8407 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008408 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008409 }
8410
8411 /* reset PML index */
8412 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8413}
8414
8415/*
8416 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8417 * Called before reporting dirty_bitmap to userspace.
8418 */
8419static void kvm_flush_pml_buffers(struct kvm *kvm)
8420{
8421 int i;
8422 struct kvm_vcpu *vcpu;
8423 /*
8424 * We only need to kick vcpu out of guest mode here, as PML buffer
8425 * is flushed at beginning of all VMEXITs, and it's obvious that only
8426 * vcpus running in guest are possible to have unflushed GPAs in PML
8427 * buffer.
8428 */
8429 kvm_for_each_vcpu(i, vcpu, kvm)
8430 kvm_vcpu_kick(vcpu);
8431}
8432
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008433static void vmx_dump_sel(char *name, uint32_t sel)
8434{
8435 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008436 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008437 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8438 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8439 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8440}
8441
8442static void vmx_dump_dtsel(char *name, uint32_t limit)
8443{
8444 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8445 name, vmcs_read32(limit),
8446 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8447}
8448
8449static void dump_vmcs(void)
8450{
8451 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8452 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8453 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8454 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8455 u32 secondary_exec_control = 0;
8456 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008457 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008458 int i, n;
8459
8460 if (cpu_has_secondary_exec_ctrls())
8461 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8462
8463 pr_err("*** Guest State ***\n");
8464 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8465 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8466 vmcs_readl(CR0_GUEST_HOST_MASK));
8467 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8468 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8469 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8470 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8471 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8472 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008473 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8474 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8475 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8476 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008477 }
8478 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8479 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8480 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8481 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8482 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8483 vmcs_readl(GUEST_SYSENTER_ESP),
8484 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8485 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8486 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8487 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8488 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8489 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8490 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8491 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8492 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8493 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8494 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8495 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8496 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008497 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8498 efer, vmcs_read64(GUEST_IA32_PAT));
8499 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8500 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008501 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8502 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008503 pr_err("PerfGlobCtl = 0x%016llx\n",
8504 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008505 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008506 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008507 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8508 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8509 vmcs_read32(GUEST_ACTIVITY_STATE));
8510 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8511 pr_err("InterruptStatus = %04x\n",
8512 vmcs_read16(GUEST_INTR_STATUS));
8513
8514 pr_err("*** Host State ***\n");
8515 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8516 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8517 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8518 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8519 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8520 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8521 vmcs_read16(HOST_TR_SELECTOR));
8522 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8523 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8524 vmcs_readl(HOST_TR_BASE));
8525 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8526 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8527 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8528 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8529 vmcs_readl(HOST_CR4));
8530 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8531 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8532 vmcs_read32(HOST_IA32_SYSENTER_CS),
8533 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8534 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008535 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8536 vmcs_read64(HOST_IA32_EFER),
8537 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008538 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008539 pr_err("PerfGlobCtl = 0x%016llx\n",
8540 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008541
8542 pr_err("*** Control State ***\n");
8543 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8544 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8545 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8546 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8547 vmcs_read32(EXCEPTION_BITMAP),
8548 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8549 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8550 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8551 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8552 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8553 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8554 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8555 vmcs_read32(VM_EXIT_INTR_INFO),
8556 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8557 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8558 pr_err(" reason=%08x qualification=%016lx\n",
8559 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8560 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8561 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8562 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008563 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008564 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008565 pr_err("TSC Multiplier = 0x%016llx\n",
8566 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008567 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8568 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8569 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8570 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8571 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008572 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008573 n = vmcs_read32(CR3_TARGET_COUNT);
8574 for (i = 0; i + 1 < n; i += 4)
8575 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8576 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8577 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8578 if (i < n)
8579 pr_err("CR3 target%u=%016lx\n",
8580 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8581 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8582 pr_err("PLE Gap=%08x Window=%08x\n",
8583 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8584 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8585 pr_err("Virtual processor ID = 0x%04x\n",
8586 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8587}
8588
Avi Kivity6aa8b732006-12-10 02:21:36 -08008589/*
8590 * The guest has exited. See if we can fix it or if we need userspace
8591 * assistance.
8592 */
Avi Kivity851ba692009-08-24 11:10:17 +03008593static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008594{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008595 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008596 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008597 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008598
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008599 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8600
Kai Huang843e4332015-01-28 10:54:28 +08008601 /*
8602 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8603 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8604 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8605 * mode as if vcpus is in root mode, the PML buffer must has been
8606 * flushed already.
8607 */
8608 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008609 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008610
Mohammed Gamal80ced182009-09-01 12:48:18 +02008611 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008612 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008613 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008614
Nadav Har'El644d7112011-05-25 23:12:35 +03008615 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008616 nested_vmx_vmexit(vcpu, exit_reason,
8617 vmcs_read32(VM_EXIT_INTR_INFO),
8618 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008619 return 1;
8620 }
8621
Mohammed Gamal51207022010-05-31 22:40:54 +03008622 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008623 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008624 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8625 vcpu->run->fail_entry.hardware_entry_failure_reason
8626 = exit_reason;
8627 return 0;
8628 }
8629
Avi Kivity29bd8a72007-09-10 17:27:03 +03008630 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008631 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8632 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008633 = vmcs_read32(VM_INSTRUCTION_ERROR);
8634 return 0;
8635 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008636
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008637 /*
8638 * Note:
8639 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8640 * delivery event since it indicates guest is accessing MMIO.
8641 * The vm-exit can be triggered again after return to guest that
8642 * will cause infinite loop.
8643 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008644 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008645 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008646 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008647 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008648 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8649 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8650 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8651 vcpu->run->internal.ndata = 2;
8652 vcpu->run->internal.data[0] = vectoring_info;
8653 vcpu->run->internal.data[1] = exit_reason;
8654 return 0;
8655 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008656
Nadav Har'El644d7112011-05-25 23:12:35 +03008657 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8658 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008659 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008660 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008661 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008662 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008663 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008664 /*
8665 * This CPU don't support us in finding the end of an
8666 * NMI-blocked window if the guest runs with IRQs
8667 * disabled. So we pull the trigger after 1 s of
8668 * futile waiting, but inform the user about this.
8669 */
8670 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8671 "state on VCPU %d after 1 s timeout\n",
8672 __func__, vcpu->vcpu_id);
8673 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008674 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008675 }
8676
Avi Kivity6aa8b732006-12-10 02:21:36 -08008677 if (exit_reason < kvm_vmx_max_exit_handlers
8678 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008679 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008680 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008681 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8682 kvm_queue_exception(vcpu, UD_VECTOR);
8683 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008684 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008685}
8686
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008687/*
8688 * Software based L1D cache flush which is used when microcode providing
8689 * the cache control MSR is not loaded.
8690 *
8691 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8692 * flush it is required to read in 64 KiB because the replacement algorithm
8693 * is not exactly LRU. This could be sized at runtime via topology
8694 * information but as all relevant affected CPUs have 32KiB L1D cache size
8695 * there is no point in doing so.
8696 */
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008697static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008698{
8699 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008700
8701 /*
Thomas Gleixner31282cf2018-07-13 16:23:17 +02008702 * This code is only executed when the the flush mode is 'cond' or
8703 * 'always'
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008704 */
Nicolai Stange936f5662018-07-21 22:25:00 +02008705 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stangee371c922018-07-27 13:22:16 +02008706 bool flush_l1d;
Nicolai Stange90bc3062018-07-21 22:35:28 +02008707
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008708 /*
Nicolai Stangee371c922018-07-27 13:22:16 +02008709 * Clear the per-vcpu flush bit, it gets set again
8710 * either from vcpu_run() or from one of the unsafe
8711 * VMEXIT handlers.
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008712 */
Nicolai Stangee371c922018-07-27 13:22:16 +02008713 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixnerdff09822018-07-13 16:23:20 +02008714 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stangee371c922018-07-27 13:22:16 +02008715
8716 /*
8717 * Clear the per-cpu flush bit, it gets set again from
8718 * the interrupt handlers.
8719 */
8720 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8721 kvm_clear_cpu_l1tf_flush_l1d();
8722
Nicolai Stange90bc3062018-07-21 22:35:28 +02008723 if (!flush_l1d)
8724 return;
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008725 }
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008726
8727 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008728
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008729 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8730 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8731 return;
8732 }
8733
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008734 asm volatile(
8735 /* First ensure the pages are in the TLB */
8736 "xorl %%eax, %%eax\n"
8737 ".Lpopulate_tlb:\n\t"
Nicolai Stange587d4992018-07-18 19:07:38 +02008738 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008739 "addl $4096, %%eax\n\t"
8740 "cmpl %%eax, %[size]\n\t"
8741 "jne .Lpopulate_tlb\n\t"
8742 "xorl %%eax, %%eax\n\t"
8743 "cpuid\n\t"
8744 /* Now fill the cache */
8745 "xorl %%eax, %%eax\n"
8746 ".Lfill_cache:\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008747 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008748 "addl $64, %%eax\n\t"
8749 "cmpl %%eax, %[size]\n\t"
8750 "jne .Lfill_cache\n\t"
8751 "lfence\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008752 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008753 [size] "r" (size)
8754 : "eax", "ebx", "ecx", "edx");
8755}
8756
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008757static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008758{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008759 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8760
8761 if (is_guest_mode(vcpu) &&
8762 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8763 return;
8764
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008765 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008766 vmcs_write32(TPR_THRESHOLD, 0);
8767 return;
8768 }
8769
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008770 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008771}
8772
Yang Zhang8d146952013-01-25 10:18:50 +08008773static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8774{
8775 u32 sec_exec_control;
8776
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008777 /* Postpone execution until vmcs01 is the current VMCS. */
8778 if (is_guest_mode(vcpu)) {
8779 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8780 return;
8781 }
8782
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008783 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008784 return;
8785
Paolo Bonzini35754c92015-07-29 12:05:37 +02008786 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008787 return;
8788
8789 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8790
8791 if (set) {
8792 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8793 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8794 } else {
8795 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8796 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008797 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008798 }
8799 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8800
Paolo Bonzini6236b782018-01-16 16:51:18 +01008801 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008802}
8803
Tang Chen38b99172014-09-24 15:57:54 +08008804static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8805{
8806 struct vcpu_vmx *vmx = to_vmx(vcpu);
8807
8808 /*
8809 * Currently we do not handle the nested case where L2 has an
8810 * APIC access page of its own; that page is still pinned.
8811 * Hence, we skip the case where the VCPU is in guest mode _and_
8812 * L1 prepared an APIC access page for L2.
8813 *
8814 * For the case where L1 and L2 share the same APIC access page
8815 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8816 * in the vmcs12), this function will only update either the vmcs01
8817 * or the vmcs02. If the former, the vmcs02 will be updated by
8818 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8819 * the next L2->L1 exit.
8820 */
8821 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008822 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008823 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008824 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008825 vmx_flush_tlb_ept_only(vcpu);
8826 }
Tang Chen38b99172014-09-24 15:57:54 +08008827}
8828
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008829static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008830{
8831 u16 status;
8832 u8 old;
8833
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008834 if (max_isr == -1)
8835 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008836
8837 status = vmcs_read16(GUEST_INTR_STATUS);
8838 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008839 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008840 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008841 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008842 vmcs_write16(GUEST_INTR_STATUS, status);
8843 }
8844}
8845
8846static void vmx_set_rvi(int vector)
8847{
8848 u16 status;
8849 u8 old;
8850
Wei Wang4114c272014-11-05 10:53:43 +08008851 if (vector == -1)
8852 vector = 0;
8853
Yang Zhangc7c9c562013-01-25 10:18:51 +08008854 status = vmcs_read16(GUEST_INTR_STATUS);
8855 old = (u8)status & 0xff;
8856 if ((u8)vector != old) {
8857 status &= ~0xff;
8858 status |= (u8)vector;
8859 vmcs_write16(GUEST_INTR_STATUS, status);
8860 }
8861}
8862
8863static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8864{
Wanpeng Li963fee12014-07-17 19:03:00 +08008865 if (!is_guest_mode(vcpu)) {
8866 vmx_set_rvi(max_irr);
8867 return;
8868 }
8869
Wei Wang4114c272014-11-05 10:53:43 +08008870 if (max_irr == -1)
8871 return;
8872
Wanpeng Li963fee12014-07-17 19:03:00 +08008873 /*
Wei Wang4114c272014-11-05 10:53:43 +08008874 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8875 * handles it.
8876 */
8877 if (nested_exit_on_intr(vcpu))
8878 return;
8879
8880 /*
8881 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008882 * is run without virtual interrupt delivery.
8883 */
8884 if (!kvm_event_needs_reinjection(vcpu) &&
8885 vmx_interrupt_allowed(vcpu)) {
8886 kvm_queue_interrupt(vcpu, max_irr, false);
8887 vmx_inject_irq(vcpu);
8888 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008889}
8890
Andrey Smetanin63086302015-11-10 15:36:32 +03008891static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008892{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008893 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008894 return;
8895
Yang Zhangc7c9c562013-01-25 10:18:51 +08008896 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8897 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8898 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8899 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8900}
8901
Avi Kivity51aa01d2010-07-20 14:31:20 +03008902static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008903{
Avi Kivity00eba012011-03-07 17:24:54 +02008904 u32 exit_intr_info;
8905
8906 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8907 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8908 return;
8909
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008910 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008911 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008912
8913 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008914 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008915 kvm_machine_check();
8916
Gleb Natapov20f65982009-05-11 13:35:55 +03008917 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008918 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008919 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008920 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008921 kvm_after_handle_nmi(&vmx->vcpu);
8922 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008923}
Gleb Natapov20f65982009-05-11 13:35:55 +03008924
Yang Zhanga547c6d2013-04-11 19:25:10 +08008925static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8926{
8927 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8928
8929 /*
8930 * If external interrupt exists, IF bit is set in rflags/eflags on the
8931 * interrupt stack frame, and interrupt will be enabled on a return
8932 * from interrupt handler.
8933 */
8934 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8935 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8936 unsigned int vector;
8937 unsigned long entry;
8938 gate_desc *desc;
8939 struct vcpu_vmx *vmx = to_vmx(vcpu);
8940#ifdef CONFIG_X86_64
8941 unsigned long tmp;
8942#endif
8943
8944 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8945 desc = (gate_desc *)vmx->host_idt_base + vector;
8946 entry = gate_offset(*desc);
8947 asm volatile(
8948#ifdef CONFIG_X86_64
8949 "mov %%" _ASM_SP ", %[sp]\n\t"
8950 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8951 "push $%c[ss]\n\t"
8952 "push %[sp]\n\t"
8953#endif
8954 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008955 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008956 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008957 :
8958#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008959 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008960#endif
Josh Poimboeufd5ea93e2017-09-20 16:24:33 -05008961 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08008962 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008963 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008964 [ss]"i"(__KERNEL_DS),
8965 [cs]"i"(__KERNEL_CS)
8966 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008967 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008968}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008969STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008970
Tom Lendackyb9655922018-05-10 22:06:39 +02008971static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008972{
Tom Lendackyb9655922018-05-10 22:06:39 +02008973 switch (index) {
8974 case MSR_IA32_SMBASE:
8975 /*
8976 * We cannot do SMM unless we can run the guest in big
8977 * real mode.
8978 */
8979 return enable_unrestricted_guest || emulate_invalid_guest_state;
8980 case MSR_AMD64_VIRT_SPEC_CTRL:
8981 /* This is AMD only. */
8982 return false;
8983 default:
8984 return true;
8985 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008986}
8987
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008988static bool vmx_mpx_supported(void)
8989{
8990 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8991 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8992}
8993
Wanpeng Li55412b22014-12-02 19:21:30 +08008994static bool vmx_xsaves_supported(void)
8995{
8996 return vmcs_config.cpu_based_2nd_exec_ctrl &
8997 SECONDARY_EXEC_XSAVES;
8998}
8999
Avi Kivity51aa01d2010-07-20 14:31:20 +03009000static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9001{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009002 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009003 bool unblock_nmi;
9004 u8 vector;
9005 bool idtv_info_valid;
9006
9007 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009008
Avi Kivitycf393f72008-07-01 16:20:21 +03009009 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02009010 if (vmx->nmi_known_unmasked)
9011 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009012 /*
9013 * Can't use vmx->exit_intr_info since we're not sure what
9014 * the exit reason is.
9015 */
9016 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03009017 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9018 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9019 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009020 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03009021 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9022 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009023 * SDM 3: 23.2.2 (September 2008)
9024 * Bit 12 is undefined in any of the following cases:
9025 * If the VM exit sets the valid bit in the IDT-vectoring
9026 * information field.
9027 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03009028 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009029 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9030 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03009031 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9032 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02009033 else
9034 vmx->nmi_known_unmasked =
9035 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9036 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009037 } else if (unlikely(vmx->soft_vnmi_blocked))
9038 vmx->vnmi_blocked_time +=
9039 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009040}
9041
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009042static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009043 u32 idt_vectoring_info,
9044 int instr_len_field,
9045 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009046{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009047 u8 vector;
9048 int type;
9049 bool idtv_info_valid;
9050
9051 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009052
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009053 vcpu->arch.nmi_injected = false;
9054 kvm_clear_exception_queue(vcpu);
9055 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009056
9057 if (!idtv_info_valid)
9058 return;
9059
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009060 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009061
Avi Kivity668f6122008-07-02 09:28:55 +03009062 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9063 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009064
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009065 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009066 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009067 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009068 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009069 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009070 * Clear bit "block by NMI" before VM entry if a NMI
9071 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009072 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009073 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009074 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009075 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009076 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009077 /* fall through */
9078 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009079 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009080 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009081 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009082 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009083 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009084 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009085 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009086 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009087 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009088 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009089 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009090 break;
9091 default:
9092 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009093 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009094}
9095
Avi Kivity83422e12010-07-20 14:43:23 +03009096static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9097{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009098 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009099 VM_EXIT_INSTRUCTION_LEN,
9100 IDT_VECTORING_ERROR_CODE);
9101}
9102
Avi Kivityb463a6f2010-07-20 15:06:17 +03009103static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9104{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009105 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009106 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9107 VM_ENTRY_INSTRUCTION_LEN,
9108 VM_ENTRY_EXCEPTION_ERROR_CODE);
9109
9110 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9111}
9112
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009113static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9114{
9115 int i, nr_msrs;
9116 struct perf_guest_switch_msr *msrs;
9117
9118 msrs = perf_guest_get_msrs(&nr_msrs);
9119
9120 if (!msrs)
9121 return;
9122
9123 for (i = 0; i < nr_msrs; i++)
9124 if (msrs[i].host == msrs[i].guest)
9125 clear_atomic_switch_msr(vmx, msrs[i].msr);
9126 else
9127 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04009128 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009129}
9130
Yunhong Jiang64672c92016-06-13 14:19:59 -07009131void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9132{
9133 struct vcpu_vmx *vmx = to_vmx(vcpu);
9134 u64 tscl;
9135 u32 delta_tsc;
9136
9137 if (vmx->hv_deadline_tsc == -1)
9138 return;
9139
9140 tscl = rdtsc();
9141 if (vmx->hv_deadline_tsc > tscl)
9142 /* sure to be 32 bit only because checked on set_hv_timer */
9143 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9144 cpu_preemption_timer_multi);
9145 else
9146 delta_tsc = 0;
9147
9148 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9149}
9150
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009151static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009152{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009153 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009154 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009155
9156 /* Record the guest's net vcpu time for enforced NMI injections. */
9157 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9158 vmx->entry_time = ktime_get();
9159
9160 /* Don't enter VMX if guest state is invalid, let the exit handler
9161 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009162 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009163 return;
9164
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009165 if (vmx->ple_window_dirty) {
9166 vmx->ple_window_dirty = false;
9167 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9168 }
9169
Abel Gordon012f83c2013-04-18 14:39:25 +03009170 if (vmx->nested.sync_shadow_vmcs) {
9171 copy_vmcs12_to_shadow(vmx);
9172 vmx->nested.sync_shadow_vmcs = false;
9173 }
9174
Avi Kivity104f2262010-11-18 13:12:52 +02009175 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9176 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9177 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9178 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9179
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009180 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009181 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9182 vmcs_writel(HOST_CR4, cr4);
9183 vmx->host_state.vmcs_host_cr4 = cr4;
9184 }
9185
Avi Kivity104f2262010-11-18 13:12:52 +02009186 /* When single-stepping over STI and MOV SS, we must clear the
9187 * corresponding interruptibility bits in the guest state. Otherwise
9188 * vmentry fails as it then expects bit 14 (BS) in pending debug
9189 * exceptions being set, but that's not correct for the guest debugging
9190 * case. */
9191 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9192 vmx_set_interrupt_shadow(vcpu, 0);
9193
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009194 if (vmx->guest_pkru_valid)
9195 __write_pkru(vmx->guest_pkru);
9196
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009197 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009198 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009199
Yunhong Jiang64672c92016-06-13 14:19:59 -07009200 vmx_arm_hv_timer(vcpu);
9201
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009202 /*
9203 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9204 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9205 * is no need to worry about the conditional branch over the wrmsr
9206 * being speculatively taken.
9207 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009208 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009209
Nadav Har'Eld462b812011-05-24 15:26:10 +03009210 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009211
Thomas Gleixner3a8e7f62019-02-27 12:48:14 +01009212 /* L1D Flush includes CPU buffer clear to mitigate MDS */
Nicolai Stange90bc3062018-07-21 22:35:28 +02009213 if (static_branch_unlikely(&vmx_l1d_should_flush))
9214 vmx_l1d_flush(vcpu);
Thomas Gleixner3a8e7f62019-02-27 12:48:14 +01009215 else if (static_branch_unlikely(&mds_user_clear))
9216 mds_clear_cpu_buffers();
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009217
Avi Kivity104f2262010-11-18 13:12:52 +02009218 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009219 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009220 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9221 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9222 "push %%" _ASM_CX " \n\t"
9223 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009224 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009225 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009226 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009227 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009228 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009229 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9230 "mov %%cr2, %%" _ASM_DX " \n\t"
9231 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009232 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009233 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009234 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009235 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009236 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009237 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009238 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9239 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9240 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9241 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9242 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9243 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009244#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009245 "mov %c[r8](%0), %%r8 \n\t"
9246 "mov %c[r9](%0), %%r9 \n\t"
9247 "mov %c[r10](%0), %%r10 \n\t"
9248 "mov %c[r11](%0), %%r11 \n\t"
9249 "mov %c[r12](%0), %%r12 \n\t"
9250 "mov %c[r13](%0), %%r13 \n\t"
9251 "mov %c[r14](%0), %%r14 \n\t"
9252 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009253#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009254 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009255
Avi Kivity6aa8b732006-12-10 02:21:36 -08009256 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009257 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009258 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009259 "jmp 2f \n\t"
9260 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9261 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009262 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009263 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009264 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009265 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009266 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9267 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9268 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9269 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9270 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9271 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9272 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009273#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009274 "mov %%r8, %c[r8](%0) \n\t"
9275 "mov %%r9, %c[r9](%0) \n\t"
9276 "mov %%r10, %c[r10](%0) \n\t"
9277 "mov %%r11, %c[r11](%0) \n\t"
9278 "mov %%r12, %c[r12](%0) \n\t"
9279 "mov %%r13, %c[r13](%0) \n\t"
9280 "mov %%r14, %c[r14](%0) \n\t"
9281 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009282 "xor %%r8d, %%r8d \n\t"
9283 "xor %%r9d, %%r9d \n\t"
9284 "xor %%r10d, %%r10d \n\t"
9285 "xor %%r11d, %%r11d \n\t"
9286 "xor %%r12d, %%r12d \n\t"
9287 "xor %%r13d, %%r13d \n\t"
9288 "xor %%r14d, %%r14d \n\t"
9289 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009290#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009291 "mov %%cr2, %%" _ASM_AX " \n\t"
9292 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009293
Jim Mattson491c0ca2018-01-03 14:31:38 -08009294 "xor %%eax, %%eax \n\t"
9295 "xor %%ebx, %%ebx \n\t"
9296 "xor %%esi, %%esi \n\t"
9297 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009298 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009299 ".pushsection .rodata \n\t"
9300 ".global vmx_return \n\t"
9301 "vmx_return: " _ASM_PTR " 2b \n\t"
9302 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009303 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009304 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009305 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009306 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009307 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9308 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9309 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9310 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9311 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9312 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9313 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009314#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009315 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9316 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9317 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9318 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9319 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9320 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9321 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9322 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009323#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009324 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9325 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009326 : "cc", "memory"
9327#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009328 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009329 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009330#else
9331 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009332#endif
9333 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009334
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009335 /*
9336 * We do not use IBRS in the kernel. If this vCPU has used the
9337 * SPEC_CTRL MSR it may have left it on; save the value and
9338 * turn it off. This is much more efficient than blindly adding
9339 * it to the atomic save/restore list. Especially as the former
9340 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9341 *
9342 * For non-nested case:
9343 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9344 * save it.
9345 *
9346 * For nested case:
9347 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9348 * save it.
9349 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009350 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009351 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009352
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009353 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009354
David Woodhousec1ddd992018-01-12 11:11:27 +00009355 /* Eliminate branch target predictions from guest mode */
9356 vmexit_fill_RSB();
9357
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009358 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9359 if (debugctlmsr)
9360 update_debugctlmsr(debugctlmsr);
9361
Avi Kivityaa67f602012-08-01 16:48:03 +03009362#ifndef CONFIG_X86_64
9363 /*
9364 * The sysexit path does not restore ds/es, so we must set them to
9365 * a reasonable value ourselves.
9366 *
9367 * We can't defer this to vmx_load_host_state() since that function
9368 * may be executed in interrupt context, which saves and restore segments
9369 * around it, nullifying its effect.
9370 */
9371 loadsegment(ds, __USER_DS);
9372 loadsegment(es, __USER_DS);
9373#endif
9374
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009375 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009376 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009377 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009378 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009379 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009380 vcpu->arch.regs_dirty = 0;
9381
Avi Kivity1155f762007-11-22 11:30:47 +02009382 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9383
Nadav Har'Eld462b812011-05-24 15:26:10 +03009384 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009385
Avi Kivity51aa01d2010-07-20 14:31:20 +03009386 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009387
Gleb Natapove0b890d2013-09-25 12:51:33 +03009388 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009389 * eager fpu is enabled if PKEY is supported and CR4 is switched
9390 * back on host, so it is safe to read guest PKRU from current
9391 * XSAVE.
9392 */
9393 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9394 vmx->guest_pkru = __read_pkru();
9395 if (vmx->guest_pkru != vmx->host_pkru) {
9396 vmx->guest_pkru_valid = true;
9397 __write_pkru(vmx->host_pkru);
9398 } else
9399 vmx->guest_pkru_valid = false;
9400 }
9401
9402 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009403 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9404 * we did not inject a still-pending event to L1 now because of
9405 * nested_run_pending, we need to re-enable this bit.
9406 */
9407 if (vmx->nested.nested_run_pending)
9408 kvm_make_request(KVM_REQ_EVENT, vcpu);
9409
9410 vmx->nested.nested_run_pending = 0;
9411
Avi Kivity51aa01d2010-07-20 14:31:20 +03009412 vmx_complete_atomic_exit(vmx);
9413 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009414 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009415}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009416STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009417
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009418static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9419{
9420 struct vcpu_vmx *vmx = to_vmx(vcpu);
9421 int cpu;
9422
9423 if (vmx->loaded_vmcs == &vmx->vmcs01)
9424 return;
9425
9426 cpu = get_cpu();
9427 vmx->loaded_vmcs = &vmx->vmcs01;
9428 vmx_vcpu_put(vcpu);
9429 vmx_vcpu_load(vcpu, cpu);
9430 vcpu->cpu = cpu;
9431 put_cpu();
9432}
9433
Jim Mattson2f1fe812016-07-08 15:36:06 -07009434/*
9435 * Ensure that the current vmcs of the logical processor is the
9436 * vmcs01 of the vcpu before calling free_nested().
9437 */
9438static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9439{
9440 struct vcpu_vmx *vmx = to_vmx(vcpu);
9441 int r;
9442
9443 r = vcpu_load(vcpu);
9444 BUG_ON(r);
9445 vmx_load_vmcs01(vcpu);
9446 free_nested(vmx);
9447 vcpu_put(vcpu);
9448}
9449
Avi Kivity6aa8b732006-12-10 02:21:36 -08009450static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9451{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009452 struct vcpu_vmx *vmx = to_vmx(vcpu);
9453
Kai Huang843e4332015-01-28 10:54:28 +08009454 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009455 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009456 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009457 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009458 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009459 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009460 kfree(vmx->guest_msrs);
9461 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009462 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009463}
9464
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009465static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009466{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009467 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009468 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009469 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009470 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009471
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009472 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009473 return ERR_PTR(-ENOMEM);
9474
Wanpeng Li991e7a02015-09-16 17:30:05 +08009475 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009476
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009477 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9478 if (err)
9479 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009480
Peter Feiner4e595162016-07-07 14:49:58 -07009481 err = -ENOMEM;
9482
9483 /*
9484 * If PML is turned on, failure on enabling PML just results in failure
9485 * of creating the vcpu, therefore we can simplify PML logic (by
9486 * avoiding dealing with cases, such as enabling PML partially on vcpus
9487 * for the guest, etc.
9488 */
9489 if (enable_pml) {
9490 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9491 if (!vmx->pml_pg)
9492 goto uninit_vcpu;
9493 }
9494
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009495 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009496 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9497 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009498
Peter Feiner4e595162016-07-07 14:49:58 -07009499 if (!vmx->guest_msrs)
9500 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009501
Nadav Har'Eld462b812011-05-24 15:26:10 +03009502 if (!vmm_exclusive)
9503 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009504 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009505 if (!vmm_exclusive)
9506 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009507 if (err < 0)
9508 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009509
Paolo Bonzini6236b782018-01-16 16:51:18 +01009510 msr_bitmap = vmx->vmcs01.msr_bitmap;
9511 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9512 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9513 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9514 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9515 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9516 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9517 vmx->msr_bitmap_mode = 0;
9518
Paolo Bonziniff546f92018-01-11 12:16:15 +01009519 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009520 cpu = get_cpu();
9521 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009522 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009523 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009524 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009525 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009526 if (err)
9527 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009528 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009529 err = alloc_apic_access_page(kvm);
9530 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009531 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009532 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009533
Sheng Yangb927a3c2009-07-21 10:42:48 +08009534 if (enable_ept) {
9535 if (!kvm->arch.ept_identity_map_addr)
9536 kvm->arch.ept_identity_map_addr =
9537 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009538 err = init_rmode_identity_map(kvm);
9539 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009540 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009541 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009542
Roman Kagan020a90f2018-07-19 21:59:07 +03009543 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009544 nested_vmx_setup_ctls_msrs(vmx);
9545
Wincy Van705699a2015-02-03 23:58:17 +08009546 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009547 vmx->nested.current_vmptr = -1ull;
9548 vmx->nested.current_vmcs12 = NULL;
9549
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009550 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9551
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009552 /*
9553 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9554 * or POSTED_INTR_WAKEUP_VECTOR.
9555 */
9556 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9557 vmx->pi_desc.sn = 1;
9558
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009559 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009560
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009561free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009562 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009563free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009564 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009565free_pml:
9566 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009567uninit_vcpu:
9568 kvm_vcpu_uninit(&vmx->vcpu);
9569free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009570 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009571 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009572 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009573}
9574
Thomas Gleixnercb106032019-02-19 11:10:49 +01009575#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
9576#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009577
9578static int vmx_vm_init(struct kvm *kvm)
9579{
Jiri Kosina2decbf52018-07-13 16:23:25 +02009580 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9581 switch (l1tf_mitigation) {
9582 case L1TF_MITIGATION_OFF:
9583 case L1TF_MITIGATION_FLUSH_NOWARN:
9584 /* 'I explicitly don't care' is set */
9585 break;
9586 case L1TF_MITIGATION_FLUSH:
9587 case L1TF_MITIGATION_FLUSH_NOSMT:
9588 case L1TF_MITIGATION_FULL:
9589 /*
9590 * Warn upon starting the first VM in a potentially
9591 * insecure environment.
9592 */
9593 if (cpu_smt_control == CPU_SMT_ENABLED)
9594 pr_warn_once(L1TF_MSG_SMT);
9595 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9596 pr_warn_once(L1TF_MSG_L1D);
9597 break;
9598 case L1TF_MITIGATION_FULL_FORCE:
9599 /* Flush is enforced */
9600 break;
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009601 }
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009602 }
9603 return 0;
9604}
9605
Yang, Sheng002c7f72007-07-31 14:23:01 +03009606static void __init vmx_check_processor_compat(void *rtn)
9607{
9608 struct vmcs_config vmcs_conf;
9609
9610 *(int *)rtn = 0;
9611 if (setup_vmcs_config(&vmcs_conf) < 0)
9612 *(int *)rtn = -EIO;
9613 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9614 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9615 smp_processor_id());
9616 *(int *)rtn = -EIO;
9617 }
9618}
9619
Sheng Yang67253af2008-04-25 10:20:22 +08009620static int get_ept_level(void)
9621{
9622 return VMX_EPT_DEFAULT_GAW + 1;
9623}
9624
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009625static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009626{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009627 u8 cache;
9628 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009629
Sheng Yang522c68c2009-04-27 20:35:43 +08009630 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009631 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009632 * 2. EPT with VT-d:
9633 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009634 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009635 * b. VT-d with snooping control feature: snooping control feature of
9636 * VT-d engine can guarantee the cache correctness. Just set it
9637 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009638 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009639 * consistent with host MTRR
9640 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009641 if (is_mmio) {
9642 cache = MTRR_TYPE_UNCACHABLE;
9643 goto exit;
9644 }
9645
9646 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009647 ipat = VMX_EPT_IPAT_BIT;
9648 cache = MTRR_TYPE_WRBACK;
9649 goto exit;
9650 }
9651
9652 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9653 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009654 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009655 cache = MTRR_TYPE_WRBACK;
9656 else
9657 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009658 goto exit;
9659 }
9660
Xiao Guangrongff536042015-06-15 16:55:22 +08009661 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009662
9663exit:
9664 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009665}
9666
Sheng Yang17cc3932010-01-05 19:02:27 +08009667static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009668{
Sheng Yang878403b2010-01-05 19:02:29 +08009669 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9670 return PT_DIRECTORY_LEVEL;
9671 else
9672 /* For shadow and EPT supported 1GB page */
9673 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009674}
9675
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009676static void vmcs_set_secondary_exec_control(u32 new_ctl)
9677{
9678 /*
9679 * These bits in the secondary execution controls field
9680 * are dynamic, the others are mostly based on the hypervisor
9681 * architecture and the guest's CPUID. Do not touch the
9682 * dynamic bits.
9683 */
9684 u32 mask =
9685 SECONDARY_EXEC_SHADOW_VMCS |
9686 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9687 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9688
9689 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9690
9691 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9692 (new_ctl & ~mask) | (cur_ctl & mask));
9693}
9694
Sheng Yang0e851882009-12-18 16:48:46 +08009695static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9696{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009697 struct kvm_cpuid_entry2 *best;
9698 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009699 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009700
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009701 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009702 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9703 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009704 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009705
Paolo Bonzini8b972652015-09-15 17:34:42 +02009706 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009707 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009708 vmx->nested.nested_vmx_secondary_ctls_high |=
9709 SECONDARY_EXEC_RDTSCP;
9710 else
9711 vmx->nested.nested_vmx_secondary_ctls_high &=
9712 ~SECONDARY_EXEC_RDTSCP;
9713 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009714 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009715
Mao, Junjiead756a12012-07-02 01:18:48 +00009716 /* Exposing INVPCID only when PCID is exposed */
9717 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9718 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009719 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9720 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009721 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009722
Mao, Junjiead756a12012-07-02 01:18:48 +00009723 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009724 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009725 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009726
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009727 if (cpu_has_secondary_exec_ctrls())
9728 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009729
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009730 if (nested_vmx_allowed(vcpu))
9731 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9732 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9733 else
9734 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9735 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009736}
9737
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009738static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9739{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009740 if (func == 1 && nested)
9741 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009742}
9743
Yang Zhang25d92082013-08-06 12:00:32 +03009744static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9745 struct x86_exception *fault)
9746{
Jan Kiszka533558b2014-01-04 18:47:20 +01009747 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9748 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009749
9750 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009751 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009752 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009753 exit_reason = EXIT_REASON_EPT_VIOLATION;
9754 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009755 vmcs12->guest_physical_address = fault->address;
9756}
9757
Nadav Har'El155a97a2013-08-05 11:07:16 +03009758/* Callbacks for nested_ept_init_mmu_context: */
9759
9760static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9761{
9762 /* return the page table to be shadowed - in our case, EPT12 */
9763 return get_vmcs12(vcpu)->ept_pointer;
9764}
9765
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009766static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009767{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009768 WARN_ON(mmu_is_nested(vcpu));
9769 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009770 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9771 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009772 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9773 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9774 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9775
9776 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009777}
9778
9779static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9780{
9781 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9782}
9783
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009784static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9785 u16 error_code)
9786{
9787 bool inequality, bit;
9788
9789 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9790 inequality =
9791 (error_code & vmcs12->page_fault_error_code_mask) !=
9792 vmcs12->page_fault_error_code_match;
9793 return inequality ^ bit;
9794}
9795
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009796static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9797 struct x86_exception *fault)
9798{
9799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9800
9801 WARN_ON(!is_guest_mode(vcpu));
9802
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009803 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009804 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9805 vmcs_read32(VM_EXIT_INTR_INFO),
9806 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009807 else
9808 kvm_inject_page_fault(vcpu, fault);
9809}
9810
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009811static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9812 struct vmcs12 *vmcs12)
9813{
9814 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009815 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009816
9817 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009818 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9819 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009820 return false;
9821
9822 /*
9823 * Translate L1 physical address to host physical
9824 * address for vmcs02. Keep the page pinned, so this
9825 * physical address remains valid. We keep a reference
9826 * to it so we can release it later.
9827 */
9828 if (vmx->nested.apic_access_page) /* shouldn't happen */
9829 nested_release_page(vmx->nested.apic_access_page);
9830 vmx->nested.apic_access_page =
9831 nested_get_page(vcpu, vmcs12->apic_access_addr);
9832 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009833
9834 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009835 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9836 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009837 return false;
9838
9839 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9840 nested_release_page(vmx->nested.virtual_apic_page);
9841 vmx->nested.virtual_apic_page =
9842 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9843
9844 /*
9845 * Failing the vm entry is _not_ what the processor does
9846 * but it's basically the only possibility we have.
9847 * We could still enter the guest if CR8 load exits are
9848 * enabled, CR8 store exits are enabled, and virtualize APIC
9849 * access is disabled; in this case the processor would never
9850 * use the TPR shadow and we could simply clear the bit from
9851 * the execution control. But such a configuration is useless,
9852 * so let's keep the code simple.
9853 */
9854 if (!vmx->nested.virtual_apic_page)
9855 return false;
9856 }
9857
Wincy Van705699a2015-02-03 23:58:17 +08009858 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009859 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9860 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009861 return false;
9862
9863 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9864 kunmap(vmx->nested.pi_desc_page);
9865 nested_release_page(vmx->nested.pi_desc_page);
9866 }
9867 vmx->nested.pi_desc_page =
9868 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9869 if (!vmx->nested.pi_desc_page)
9870 return false;
9871
9872 vmx->nested.pi_desc =
9873 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9874 if (!vmx->nested.pi_desc) {
9875 nested_release_page_clean(vmx->nested.pi_desc_page);
9876 return false;
9877 }
9878 vmx->nested.pi_desc =
9879 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9880 (unsigned long)(vmcs12->posted_intr_desc_addr &
9881 (PAGE_SIZE - 1)));
9882 }
9883
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009884 return true;
9885}
9886
Jan Kiszkaf4124502014-03-07 20:03:13 +01009887static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9888{
9889 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9890 struct vcpu_vmx *vmx = to_vmx(vcpu);
9891
9892 if (vcpu->arch.virtual_tsc_khz == 0)
9893 return;
9894
9895 /* Make sure short timeouts reliably trigger an immediate vmexit.
9896 * hrtimer_start does not guarantee this. */
9897 if (preemption_timeout <= 1) {
9898 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9899 return;
9900 }
9901
9902 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9903 preemption_timeout *= 1000000;
9904 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9905 hrtimer_start(&vmx->nested.preemption_timer,
9906 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9907}
9908
Wincy Van3af18d92015-02-03 23:49:31 +08009909static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9910 struct vmcs12 *vmcs12)
9911{
9912 int maxphyaddr;
9913 u64 addr;
9914
9915 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9916 return 0;
9917
9918 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9919 WARN_ON(1);
9920 return -EINVAL;
9921 }
9922 maxphyaddr = cpuid_maxphyaddr(vcpu);
9923
9924 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9925 ((addr + PAGE_SIZE) >> maxphyaddr))
9926 return -EINVAL;
9927
9928 return 0;
9929}
9930
9931/*
9932 * Merge L0's and L1's MSR bitmap, return false to indicate that
9933 * we do not use the hardware.
9934 */
9935static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9936 struct vmcs12 *vmcs12)
9937{
Wincy Van82f0dd42015-02-03 23:57:18 +08009938 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009939 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009940 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009941 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009942 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009943 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009944 *
9945 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9946 * ensures that we do not accidentally generate an L02 MSR bitmap
9947 * from the L12 MSR bitmap that is too permissive.
9948 * 2. That L1 or L2s have actually used the MSR. This avoids
9949 * unnecessarily merging of the bitmap if the MSR is unused. This
9950 * works properly because we only update the L01 MSR bitmap lazily.
9951 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9952 * updated to reflect this when L1 (or its L2s) actually write to
9953 * the MSR.
9954 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009955 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9956 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009957
Ashok Raj70131292018-02-01 22:59:43 +01009958 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009959 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009960 return false;
9961
9962 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009963 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009964 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009965 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009966
Radim Krčmářd048c092016-08-08 20:16:22 +02009967 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9968
Wincy Vanf2b93282015-02-03 23:56:03 +08009969 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009970 if (nested_cpu_has_apic_reg_virt(vmcs12))
9971 for (msr = 0x800; msr <= 0x8ff; msr++)
9972 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009973 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009974 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009975
9976 nested_vmx_disable_intercept_for_msr(
9977 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009978 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9979 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009980
Wincy Van608406e2015-02-03 23:57:51 +08009981 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009982 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009983 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009984 APIC_BASE_MSR + (APIC_EOI >> 4),
9985 MSR_TYPE_W);
9986 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009987 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009988 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9989 MSR_TYPE_W);
9990 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009991 }
Ashok Raj70131292018-02-01 22:59:43 +01009992
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009993 if (spec_ctrl)
9994 nested_vmx_disable_intercept_for_msr(
9995 msr_bitmap_l1, msr_bitmap_l0,
9996 MSR_IA32_SPEC_CTRL,
9997 MSR_TYPE_R | MSR_TYPE_W);
9998
Ashok Raj70131292018-02-01 22:59:43 +01009999 if (pred_cmd)
10000 nested_vmx_disable_intercept_for_msr(
10001 msr_bitmap_l1, msr_bitmap_l0,
10002 MSR_IA32_PRED_CMD,
10003 MSR_TYPE_W);
10004
Wincy Vanf2b93282015-02-03 23:56:03 +080010005 kunmap(page);
10006 nested_release_page_clean(page);
10007
10008 return true;
10009}
10010
10011static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10012 struct vmcs12 *vmcs12)
10013{
Wincy Van82f0dd42015-02-03 23:57:18 +080010014 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010015 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010016 !nested_cpu_has_vid(vmcs12) &&
10017 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010018 return 0;
10019
10020 /*
10021 * If virtualize x2apic mode is enabled,
10022 * virtualize apic access must be disabled.
10023 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010024 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10025 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010026 return -EINVAL;
10027
Wincy Van608406e2015-02-03 23:57:51 +080010028 /*
10029 * If virtual interrupt delivery is enabled,
10030 * we must exit on external interrupts.
10031 */
10032 if (nested_cpu_has_vid(vmcs12) &&
10033 !nested_exit_on_intr(vcpu))
10034 return -EINVAL;
10035
Wincy Van705699a2015-02-03 23:58:17 +080010036 /*
10037 * bits 15:8 should be zero in posted_intr_nv,
10038 * the descriptor address has been already checked
10039 * in nested_get_vmcs12_pages.
10040 */
10041 if (nested_cpu_has_posted_intr(vmcs12) &&
10042 (!nested_cpu_has_vid(vmcs12) ||
10043 !nested_exit_intr_ack_set(vcpu) ||
10044 vmcs12->posted_intr_nv & 0xff00))
10045 return -EINVAL;
10046
Wincy Vanf2b93282015-02-03 23:56:03 +080010047 /* tpr shadow is needed by all apicv features. */
10048 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10049 return -EINVAL;
10050
10051 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010052}
10053
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010054static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10055 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010056 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010057{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010058 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010059 u64 count, addr;
10060
10061 if (vmcs12_read_any(vcpu, count_field, &count) ||
10062 vmcs12_read_any(vcpu, addr_field, &addr)) {
10063 WARN_ON(1);
10064 return -EINVAL;
10065 }
10066 if (count == 0)
10067 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010068 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010069 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10070 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010071 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010072 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10073 addr_field, maxphyaddr, count, addr);
10074 return -EINVAL;
10075 }
10076 return 0;
10077}
10078
10079static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10080 struct vmcs12 *vmcs12)
10081{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010082 if (vmcs12->vm_exit_msr_load_count == 0 &&
10083 vmcs12->vm_exit_msr_store_count == 0 &&
10084 vmcs12->vm_entry_msr_load_count == 0)
10085 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010086 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010087 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010088 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010089 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010090 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010091 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010092 return -EINVAL;
10093 return 0;
10094}
10095
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010096static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10097 struct vmx_msr_entry *e)
10098{
10099 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010100 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010101 return -EINVAL;
10102 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10103 e->index == MSR_IA32_UCODE_REV)
10104 return -EINVAL;
10105 if (e->reserved != 0)
10106 return -EINVAL;
10107 return 0;
10108}
10109
10110static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10111 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010112{
10113 if (e->index == MSR_FS_BASE ||
10114 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010115 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10116 nested_vmx_msr_check_common(vcpu, e))
10117 return -EINVAL;
10118 return 0;
10119}
10120
10121static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10122 struct vmx_msr_entry *e)
10123{
10124 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10125 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010126 return -EINVAL;
10127 return 0;
10128}
10129
10130/*
10131 * Load guest's/host's msr at nested entry/exit.
10132 * return 0 for success, entry index for failure.
10133 */
10134static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10135{
10136 u32 i;
10137 struct vmx_msr_entry e;
10138 struct msr_data msr;
10139
10140 msr.host_initiated = false;
10141 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010142 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10143 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010144 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010145 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10146 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010147 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010148 }
10149 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010150 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010151 "%s check failed (%u, 0x%x, 0x%x)\n",
10152 __func__, i, e.index, e.reserved);
10153 goto fail;
10154 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010155 msr.index = e.index;
10156 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010157 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010158 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010159 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10160 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010161 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010162 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010163 }
10164 return 0;
10165fail:
10166 return i + 1;
10167}
10168
10169static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10170{
10171 u32 i;
10172 struct vmx_msr_entry e;
10173
10174 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010175 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010176 if (kvm_vcpu_read_guest(vcpu,
10177 gpa + i * sizeof(e),
10178 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010179 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010180 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10181 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010182 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010183 }
10184 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010185 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010186 "%s check failed (%u, 0x%x, 0x%x)\n",
10187 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010188 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010189 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010190 msr_info.host_initiated = false;
10191 msr_info.index = e.index;
10192 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010193 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010194 "%s cannot read MSR (%u, 0x%x)\n",
10195 __func__, i, e.index);
10196 return -EINVAL;
10197 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010198 if (kvm_vcpu_write_guest(vcpu,
10199 gpa + i * sizeof(e) +
10200 offsetof(struct vmx_msr_entry, value),
10201 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010202 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010203 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010204 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010205 return -EINVAL;
10206 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010207 }
10208 return 0;
10209}
10210
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010211/*
10212 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10213 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010214 * 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 +030010215 * guest in a way that will both be appropriate to L1's requests, and our
10216 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10217 * function also has additional necessary side-effects, like setting various
10218 * vcpu->arch fields.
10219 */
10220static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10221{
10222 struct vcpu_vmx *vmx = to_vmx(vcpu);
10223 u32 exec_control;
10224
10225 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10226 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10227 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10228 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10229 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10230 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10231 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10232 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10233 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10234 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10235 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10236 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10237 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10238 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10239 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10240 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10241 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10242 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10243 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10244 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10245 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10246 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10247 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10248 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10249 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10250 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10251 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10252 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10253 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10254 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10255 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10256 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10257 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10258 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10259 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10260 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10261
Jan Kiszka2996fca2014-06-16 13:59:43 +020010262 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10263 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10264 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10265 } else {
10266 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10267 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10268 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010269 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10270 vmcs12->vm_entry_intr_info_field);
10271 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10272 vmcs12->vm_entry_exception_error_code);
10273 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10274 vmcs12->vm_entry_instruction_len);
10275 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10276 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010277 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010278 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010279 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10280 vmcs12->guest_pending_dbg_exceptions);
10281 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10282 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10283
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010284 if (nested_cpu_has_xsaves(vmcs12))
10285 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010286 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10287
Jan Kiszkaf4124502014-03-07 20:03:13 +010010288 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010289
Paolo Bonzini93140062016-07-06 13:23:51 +020010290 /* Preemption timer setting is only taken from vmcs01. */
10291 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10292 exec_control |= vmcs_config.pin_based_exec_ctrl;
10293 if (vmx->hv_deadline_tsc == -1)
10294 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10295
10296 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010297 if (nested_cpu_has_posted_intr(vmcs12)) {
10298 /*
10299 * Note that we use L0's vector here and in
10300 * vmx_deliver_nested_posted_interrupt.
10301 */
10302 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10303 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010304 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010305 vmcs_write64(POSTED_INTR_DESC_ADDR,
10306 page_to_phys(vmx->nested.pi_desc_page) +
10307 (unsigned long)(vmcs12->posted_intr_desc_addr &
10308 (PAGE_SIZE - 1)));
10309 } else
10310 exec_control &= ~PIN_BASED_POSTED_INTR;
10311
Jan Kiszkaf4124502014-03-07 20:03:13 +010010312 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010313
Jan Kiszkaf4124502014-03-07 20:03:13 +010010314 vmx->nested.preemption_timer_expired = false;
10315 if (nested_cpu_has_preemption_timer(vmcs12))
10316 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010317
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010318 /*
10319 * Whether page-faults are trapped is determined by a combination of
10320 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10321 * If enable_ept, L0 doesn't care about page faults and we should
10322 * set all of these to L1's desires. However, if !enable_ept, L0 does
10323 * care about (at least some) page faults, and because it is not easy
10324 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10325 * to exit on each and every L2 page fault. This is done by setting
10326 * MASK=MATCH=0 and (see below) EB.PF=1.
10327 * Note that below we don't need special code to set EB.PF beyond the
10328 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10329 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10330 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10331 *
10332 * A problem with this approach (when !enable_ept) is that L1 may be
10333 * injected with more page faults than it asked for. This could have
10334 * caused problems, but in practice existing hypervisors don't care.
10335 * To fix this, we will need to emulate the PFEC checking (on the L1
10336 * page tables), using walk_addr(), when injecting PFs to L1.
10337 */
10338 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10339 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10340 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10341 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10342
10343 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010344 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010345
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010346 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010347 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010348 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010349 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010350 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010351 if (nested_cpu_has(vmcs12,
10352 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10353 exec_control |= vmcs12->secondary_vm_exec_control;
10354
10355 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10356 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010357 * If translation failed, no matter: This feature asks
10358 * to exit when accessing the given address, and if it
10359 * can never be accessed, this feature won't do
10360 * anything anyway.
10361 */
10362 if (!vmx->nested.apic_access_page)
10363 exec_control &=
10364 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10365 else
10366 vmcs_write64(APIC_ACCESS_ADDR,
10367 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010368 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010369 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010370 exec_control |=
10371 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010372 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010373 }
10374
Wincy Van608406e2015-02-03 23:57:51 +080010375 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10376 vmcs_write64(EOI_EXIT_BITMAP0,
10377 vmcs12->eoi_exit_bitmap0);
10378 vmcs_write64(EOI_EXIT_BITMAP1,
10379 vmcs12->eoi_exit_bitmap1);
10380 vmcs_write64(EOI_EXIT_BITMAP2,
10381 vmcs12->eoi_exit_bitmap2);
10382 vmcs_write64(EOI_EXIT_BITMAP3,
10383 vmcs12->eoi_exit_bitmap3);
10384 vmcs_write16(GUEST_INTR_STATUS,
10385 vmcs12->guest_intr_status);
10386 }
10387
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010388 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10389 }
10390
10391
10392 /*
10393 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10394 * Some constant fields are set here by vmx_set_constant_host_state().
10395 * Other fields are different per CPU, and will be set later when
10396 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10397 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010398 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010399
10400 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010401 * Set the MSR load/store lists to match L0's settings.
10402 */
10403 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010404 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10405 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10406 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10407 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010408
10409 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010410 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10411 * entry, but only if the current (host) sp changed from the value
10412 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10413 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10414 * here we just force the write to happen on entry.
10415 */
10416 vmx->host_rsp = 0;
10417
10418 exec_control = vmx_exec_control(vmx); /* L0's desires */
10419 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10420 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10421 exec_control &= ~CPU_BASED_TPR_SHADOW;
10422 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010423
10424 if (exec_control & CPU_BASED_TPR_SHADOW) {
10425 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10426 page_to_phys(vmx->nested.virtual_apic_page));
10427 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010428 } else {
10429#ifdef CONFIG_X86_64
10430 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10431 CPU_BASED_CR8_STORE_EXITING;
10432#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010433 }
10434
Wincy Van3af18d92015-02-03 23:49:31 +080010435 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010436 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10437 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10438 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10439 else
Wincy Van3af18d92015-02-03 23:49:31 +080010440 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10441
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010442 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010443 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010444 * Rather, exit every time.
10445 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010446 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10447 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10448
10449 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10450
10451 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10452 * bitwise-or of what L1 wants to trap for L2, and what we want to
10453 * trap. Note that CR0.TS also needs updating - we do this later.
10454 */
10455 update_exception_bitmap(vcpu);
10456 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10457 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10458
Nadav Har'El8049d652013-08-05 11:07:06 +030010459 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10460 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10461 * bits are further modified by vmx_set_efer() below.
10462 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010463 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010464
10465 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10466 * emulated by vmx_set_efer(), below.
10467 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010468 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010469 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10470 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010471 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10472
Jan Kiszka44811c02013-08-04 17:17:27 +020010473 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010474 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010475 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10476 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010477 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10478
10479
10480 set_cr4_guest_host_mask(vmx);
10481
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010482 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10483 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10484
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010485 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10486 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010487 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010488 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010489 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010490 if (kvm_has_tsc_control)
10491 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010492
Paolo Bonzini6236b782018-01-16 16:51:18 +010010493 if (cpu_has_vmx_msr_bitmap())
10494 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10495
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010496 if (enable_vpid) {
10497 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010498 * There is no direct mapping between vpid02 and vpid12, the
10499 * vpid02 is per-vCPU for L0 and reused while the value of
10500 * vpid12 is changed w/ one invvpid during nested vmentry.
10501 * The vpid12 is allocated by L1 for L2, so it will not
10502 * influence global bitmap(for vpid01 and vpid02 allocation)
10503 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010504 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010505 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10506 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10507 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10508 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10509 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10510 }
10511 } else {
10512 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10513 vmx_flush_tlb(vcpu);
10514 }
10515
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010516 }
10517
Ladi Prosek560a9792017-04-04 14:18:53 +020010518 if (enable_pml) {
10519 /*
10520 * Conceptually we want to copy the PML address and index from
10521 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10522 * since we always flush the log on each vmexit, this happens
10523 * to be equivalent to simply resetting the fields in vmcs02.
10524 */
10525 ASSERT(vmx->pml_pg);
10526 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10527 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10528 }
10529
Nadav Har'El155a97a2013-08-05 11:07:16 +030010530 if (nested_cpu_has_ept(vmcs12)) {
10531 kvm_mmu_unload(vcpu);
10532 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010533 } else if (nested_cpu_has2(vmcs12,
10534 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10535 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010536 }
10537
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010538 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10539 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010540 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010541 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10542 else
10543 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10544 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10545 vmx_set_efer(vcpu, vcpu->arch.efer);
10546
10547 /*
10548 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10549 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10550 * The CR0_READ_SHADOW is what L2 should have expected to read given
10551 * the specifications by L1; It's not enough to take
10552 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10553 * have more bits than L1 expected.
10554 */
10555 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10556 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10557
10558 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10559 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10560
10561 /* shadow page tables on either EPT or shadow page tables */
10562 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10563 kvm_mmu_reset_context(vcpu);
10564
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010565 if (!enable_ept)
10566 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10567
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010568 /*
10569 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10570 */
10571 if (enable_ept) {
10572 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10573 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10574 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10575 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10576 }
10577
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010578 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10579 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10580}
10581
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010582/*
10583 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10584 * for running an L2 nested guest.
10585 */
10586static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10587{
10588 struct vmcs12 *vmcs12;
10589 struct vcpu_vmx *vmx = to_vmx(vcpu);
10590 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010591 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010592 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010593
10594 if (!nested_vmx_check_permission(vcpu) ||
10595 !nested_vmx_check_vmcs12(vcpu))
10596 return 1;
10597
10598 skip_emulated_instruction(vcpu);
10599 vmcs12 = get_vmcs12(vcpu);
10600
Abel Gordon012f83c2013-04-18 14:39:25 +030010601 if (enable_shadow_vmcs)
10602 copy_shadow_to_vmcs12(vmx);
10603
Nadav Har'El7c177932011-05-25 23:12:04 +030010604 /*
10605 * The nested entry process starts with enforcing various prerequisites
10606 * on vmcs12 as required by the Intel SDM, and act appropriately when
10607 * they fail: As the SDM explains, some conditions should cause the
10608 * instruction to fail, while others will cause the instruction to seem
10609 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10610 * To speed up the normal (success) code path, we should avoid checking
10611 * for misconfigurations which will anyway be caught by the processor
10612 * when using the merged vmcs02.
10613 */
10614 if (vmcs12->launch_state == launch) {
10615 nested_vmx_failValid(vcpu,
10616 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10617 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10618 return 1;
10619 }
10620
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010621 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10622 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010623 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10624 return 1;
10625 }
10626
Wincy Van3af18d92015-02-03 23:49:31 +080010627 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010628 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10629 return 1;
10630 }
10631
Wincy Van3af18d92015-02-03 23:49:31 +080010632 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010633 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10634 return 1;
10635 }
10636
Wincy Vanf2b93282015-02-03 23:56:03 +080010637 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10638 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10639 return 1;
10640 }
10641
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010642 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10643 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10644 return 1;
10645 }
10646
Nadav Har'El7c177932011-05-25 23:12:04 +030010647 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010648 vmx->nested.nested_vmx_true_procbased_ctls_low,
10649 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010650 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010651 vmx->nested.nested_vmx_secondary_ctls_low,
10652 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010653 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010654 vmx->nested.nested_vmx_pinbased_ctls_low,
10655 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010656 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010657 vmx->nested.nested_vmx_true_exit_ctls_low,
10658 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010659 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010660 vmx->nested.nested_vmx_true_entry_ctls_low,
10661 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010662 {
10663 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10664 return 1;
10665 }
10666
10667 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10668 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10669 nested_vmx_failValid(vcpu,
10670 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10671 return 1;
10672 }
10673
Wincy Vanb9c237b2015-02-03 23:56:30 +080010674 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010675 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10676 nested_vmx_entry_failure(vcpu, vmcs12,
10677 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10678 return 1;
10679 }
10680 if (vmcs12->vmcs_link_pointer != -1ull) {
10681 nested_vmx_entry_failure(vcpu, vmcs12,
10682 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10683 return 1;
10684 }
10685
10686 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010687 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010688 * are performed on the field for the IA32_EFER MSR:
10689 * - Bits reserved in the IA32_EFER MSR must be 0.
10690 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10691 * the IA-32e mode guest VM-exit control. It must also be identical
10692 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10693 * CR0.PG) is 1.
10694 */
10695 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10696 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10697 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10698 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10699 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10700 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10701 nested_vmx_entry_failure(vcpu, vmcs12,
10702 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10703 return 1;
10704 }
10705 }
10706
10707 /*
10708 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10709 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10710 * the values of the LMA and LME bits in the field must each be that of
10711 * the host address-space size VM-exit control.
10712 */
10713 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10714 ia32e = (vmcs12->vm_exit_controls &
10715 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10716 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10717 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10718 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10719 nested_vmx_entry_failure(vcpu, vmcs12,
10720 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10721 return 1;
10722 }
10723 }
10724
10725 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010726 * We're finally done with prerequisite checking, and can start with
10727 * the nested entry.
10728 */
10729
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010730 enter_guest_mode(vcpu);
10731
Jan Kiszka2996fca2014-06-16 13:59:43 +020010732 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10733 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10734
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010735 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010736 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010737 vmx_vcpu_put(vcpu);
10738 vmx_vcpu_load(vcpu, cpu);
10739 vcpu->cpu = cpu;
10740 put_cpu();
10741
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010742 vmx_segment_cache_clear(vmx);
10743
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010744 prepare_vmcs02(vcpu, vmcs12);
10745
Wincy Vanff651cb2014-12-11 08:52:58 +030010746 msr_entry_idx = nested_vmx_load_msr(vcpu,
10747 vmcs12->vm_entry_msr_load_addr,
10748 vmcs12->vm_entry_msr_load_count);
10749 if (msr_entry_idx) {
10750 leave_guest_mode(vcpu);
10751 vmx_load_vmcs01(vcpu);
10752 nested_vmx_entry_failure(vcpu, vmcs12,
10753 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10754 return 1;
10755 }
10756
10757 vmcs12->launch_state = 1;
10758
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010759 /* Hide L1D cache contents from the nested guest. */
10760 vmx->vcpu.arch.l1tf_flush_l1d = true;
10761
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010762 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010763 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010764
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010765 vmx->nested.nested_run_pending = 1;
10766
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010767 /*
10768 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10769 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10770 * returned as far as L1 is concerned. It will only return (and set
10771 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10772 */
10773 return 1;
10774}
10775
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010776/*
10777 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10778 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10779 * This function returns the new value we should put in vmcs12.guest_cr0.
10780 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10781 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10782 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10783 * didn't trap the bit, because if L1 did, so would L0).
10784 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10785 * been modified by L2, and L1 knows it. So just leave the old value of
10786 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10787 * isn't relevant, because if L0 traps this bit it can set it to anything.
10788 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10789 * changed these bits, and therefore they need to be updated, but L0
10790 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10791 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10792 */
10793static inline unsigned long
10794vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10795{
10796 return
10797 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10798 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10799 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10800 vcpu->arch.cr0_guest_owned_bits));
10801}
10802
10803static inline unsigned long
10804vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10805{
10806 return
10807 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10808 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10809 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10810 vcpu->arch.cr4_guest_owned_bits));
10811}
10812
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010813static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10814 struct vmcs12 *vmcs12)
10815{
10816 u32 idt_vectoring;
10817 unsigned int nr;
10818
Gleb Natapov851eb6672013-09-25 12:51:34 +030010819 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010820 nr = vcpu->arch.exception.nr;
10821 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10822
10823 if (kvm_exception_is_soft(nr)) {
10824 vmcs12->vm_exit_instruction_len =
10825 vcpu->arch.event_exit_inst_len;
10826 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10827 } else
10828 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10829
10830 if (vcpu->arch.exception.has_error_code) {
10831 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10832 vmcs12->idt_vectoring_error_code =
10833 vcpu->arch.exception.error_code;
10834 }
10835
10836 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010837 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010838 vmcs12->idt_vectoring_info_field =
10839 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10840 } else if (vcpu->arch.interrupt.pending) {
10841 nr = vcpu->arch.interrupt.nr;
10842 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10843
10844 if (vcpu->arch.interrupt.soft) {
10845 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10846 vmcs12->vm_entry_instruction_len =
10847 vcpu->arch.event_exit_inst_len;
10848 } else
10849 idt_vectoring |= INTR_TYPE_EXT_INTR;
10850
10851 vmcs12->idt_vectoring_info_field = idt_vectoring;
10852 }
10853}
10854
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010855static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10856{
10857 struct vcpu_vmx *vmx = to_vmx(vcpu);
10858
Jan Kiszkaf4124502014-03-07 20:03:13 +010010859 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10860 vmx->nested.preemption_timer_expired) {
10861 if (vmx->nested.nested_run_pending)
10862 return -EBUSY;
10863 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10864 return 0;
10865 }
10866
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010867 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010868 if (vmx->nested.nested_run_pending ||
10869 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010870 return -EBUSY;
10871 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10872 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10873 INTR_INFO_VALID_MASK, 0);
10874 /*
10875 * The NMI-triggered VM exit counts as injection:
10876 * clear this one and block further NMIs.
10877 */
10878 vcpu->arch.nmi_pending = 0;
10879 vmx_set_nmi_mask(vcpu, true);
10880 return 0;
10881 }
10882
10883 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10884 nested_exit_on_intr(vcpu)) {
10885 if (vmx->nested.nested_run_pending)
10886 return -EBUSY;
10887 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010888 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010889 }
10890
David Hildenbrand1edccf22017-01-25 11:58:58 +010010891 vmx_complete_nested_posted_interrupt(vcpu);
10892 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010893}
10894
Jan Kiszkaf4124502014-03-07 20:03:13 +010010895static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10896{
10897 ktime_t remaining =
10898 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10899 u64 value;
10900
10901 if (ktime_to_ns(remaining) <= 0)
10902 return 0;
10903
10904 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10905 do_div(value, 1000000);
10906 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10907}
10908
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010909/*
10910 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10911 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10912 * and this function updates it to reflect the changes to the guest state while
10913 * L2 was running (and perhaps made some exits which were handled directly by L0
10914 * without going back to L1), and to reflect the exit reason.
10915 * Note that we do not have to copy here all VMCS fields, just those that
10916 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10917 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10918 * which already writes to vmcs12 directly.
10919 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010920static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10921 u32 exit_reason, u32 exit_intr_info,
10922 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010923{
10924 /* update guest state fields: */
10925 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10926 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10927
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010928 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10929 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10930 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10931
10932 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10933 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10934 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10935 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10936 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10937 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10938 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10939 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10940 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10941 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10942 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10943 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10944 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10945 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10946 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10947 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10948 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10949 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10950 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10951 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10952 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10953 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10954 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10955 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10956 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10957 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10958 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10959 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10960 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10961 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10962 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10963 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10964 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10965 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10966 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10967 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10968
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010969 vmcs12->guest_interruptibility_info =
10970 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10971 vmcs12->guest_pending_dbg_exceptions =
10972 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010973 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10974 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10975 else
10976 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010977
Jan Kiszkaf4124502014-03-07 20:03:13 +010010978 if (nested_cpu_has_preemption_timer(vmcs12)) {
10979 if (vmcs12->vm_exit_controls &
10980 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10981 vmcs12->vmx_preemption_timer_value =
10982 vmx_get_preemption_timer_value(vcpu);
10983 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10984 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010985
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010986 /*
10987 * In some cases (usually, nested EPT), L2 is allowed to change its
10988 * own CR3 without exiting. If it has changed it, we must keep it.
10989 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10990 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10991 *
10992 * Additionally, restore L2's PDPTR to vmcs12.
10993 */
10994 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010995 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010996 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10997 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10998 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10999 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11000 }
11001
Jim Mattson4933e9f2017-06-01 12:44:46 -070011002 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011003
Wincy Van608406e2015-02-03 23:57:51 +080011004 if (nested_cpu_has_vid(vmcs12))
11005 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11006
Jan Kiszkac18911a2013-03-13 16:06:41 +010011007 vmcs12->vm_entry_controls =
11008 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011009 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011010
Jan Kiszka2996fca2014-06-16 13:59:43 +020011011 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11012 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11013 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11014 }
11015
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011016 /* TODO: These cannot have changed unless we have MSR bitmaps and
11017 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011018 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011019 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011020 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11021 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011022 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11023 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11024 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011025 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011026 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080011027 if (nested_cpu_has_xsaves(vmcs12))
11028 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011029
11030 /* update exit information fields: */
11031
Jan Kiszka533558b2014-01-04 18:47:20 +010011032 vmcs12->vm_exit_reason = exit_reason;
11033 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011034
Jan Kiszka533558b2014-01-04 18:47:20 +010011035 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020011036 if ((vmcs12->vm_exit_intr_info &
11037 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
11038 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
11039 vmcs12->vm_exit_intr_error_code =
11040 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011041 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011042 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11043 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11044
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011045 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11046 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11047 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011048 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011049
11050 /*
11051 * Transfer the event that L0 or L1 may wanted to inject into
11052 * L2 to IDT_VECTORING_INFO_FIELD.
11053 */
11054 vmcs12_save_pending_event(vcpu, vmcs12);
11055 }
11056
11057 /*
11058 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11059 * preserved above and would only end up incorrectly in L1.
11060 */
11061 vcpu->arch.nmi_injected = false;
11062 kvm_clear_exception_queue(vcpu);
11063 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011064}
11065
11066/*
11067 * A part of what we need to when the nested L2 guest exits and we want to
11068 * run its L1 parent, is to reset L1's guest state to the host state specified
11069 * in vmcs12.
11070 * This function is to be called not only on normal nested exit, but also on
11071 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11072 * Failures During or After Loading Guest State").
11073 * This function should be called when the active VMCS is L1's (vmcs01).
11074 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011075static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11076 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011077{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011078 struct kvm_segment seg;
11079
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011080 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11081 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011082 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011083 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11084 else
11085 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11086 vmx_set_efer(vcpu, vcpu->arch.efer);
11087
11088 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11089 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011090 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011091 /*
11092 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11093 * actually changed, because it depends on the current state of
11094 * fpu_active (which may have changed).
11095 * Note that vmx_set_cr0 refers to efer set above.
11096 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011097 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011098 /*
11099 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
11100 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
11101 * but we also need to update cr0_guest_host_mask and exception_bitmap.
11102 */
11103 update_exception_bitmap(vcpu);
11104 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
11105 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11106
11107 /*
11108 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
11109 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
11110 */
11111 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080011112 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011113
Jan Kiszka29bf08f2013-12-28 16:31:52 +010011114 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011115
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011116 kvm_set_cr3(vcpu, vmcs12->host_cr3);
11117 kvm_mmu_reset_context(vcpu);
11118
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011119 if (!enable_ept)
11120 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11121
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011122 if (enable_vpid) {
11123 /*
11124 * Trivially support vpid by letting L2s share their parent
11125 * L1's vpid. TODO: move to a more elaborate solution, giving
11126 * each L2 its own vpid and exposing the vpid feature to L1.
11127 */
11128 vmx_flush_tlb(vcpu);
11129 }
11130
11131
11132 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11133 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11134 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11135 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11136 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020011137 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11138 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011139
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011140 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11141 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11142 vmcs_write64(GUEST_BNDCFGS, 0);
11143
Jan Kiszka44811c02013-08-04 17:17:27 +020011144 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011145 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011146 vcpu->arch.pat = vmcs12->host_ia32_pat;
11147 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011148 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11149 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11150 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011151
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011152 /* Set L1 segment info according to Intel SDM
11153 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11154 seg = (struct kvm_segment) {
11155 .base = 0,
11156 .limit = 0xFFFFFFFF,
11157 .selector = vmcs12->host_cs_selector,
11158 .type = 11,
11159 .present = 1,
11160 .s = 1,
11161 .g = 1
11162 };
11163 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11164 seg.l = 1;
11165 else
11166 seg.db = 1;
11167 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11168 seg = (struct kvm_segment) {
11169 .base = 0,
11170 .limit = 0xFFFFFFFF,
11171 .type = 3,
11172 .present = 1,
11173 .s = 1,
11174 .db = 1,
11175 .g = 1
11176 };
11177 seg.selector = vmcs12->host_ds_selector;
11178 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11179 seg.selector = vmcs12->host_es_selector;
11180 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11181 seg.selector = vmcs12->host_ss_selector;
11182 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11183 seg.selector = vmcs12->host_fs_selector;
11184 seg.base = vmcs12->host_fs_base;
11185 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11186 seg.selector = vmcs12->host_gs_selector;
11187 seg.base = vmcs12->host_gs_base;
11188 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11189 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011190 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011191 .limit = 0x67,
11192 .selector = vmcs12->host_tr_selector,
11193 .type = 11,
11194 .present = 1
11195 };
11196 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11197
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011198 kvm_set_dr(vcpu, 7, 0x400);
11199 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011200
Wincy Van3af18d92015-02-03 23:49:31 +080011201 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011202 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011203
Wincy Vanff651cb2014-12-11 08:52:58 +030011204 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11205 vmcs12->vm_exit_msr_load_count))
11206 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011207}
11208
11209/*
11210 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11211 * and modify vmcs12 to make it see what it would expect to see there if
11212 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11213 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011214static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11215 u32 exit_intr_info,
11216 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011217{
11218 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011219 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11220
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011221 /* trying to cancel vmlaunch/vmresume is a bug */
11222 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11223
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011224 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011225 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11226 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011227
Wincy Vanff651cb2014-12-11 08:52:58 +030011228 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11229 vmcs12->vm_exit_msr_store_count))
11230 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11231
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011232 vmx_load_vmcs01(vcpu);
11233
Bandan Das77b0f5d2014-04-19 18:17:45 -040011234 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11235 && nested_exit_intr_ack_set(vcpu)) {
11236 int irq = kvm_cpu_get_interrupt(vcpu);
11237 WARN_ON(irq < 0);
11238 vmcs12->vm_exit_intr_info = irq |
11239 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11240 }
11241
Jan Kiszka542060e2014-01-04 18:47:21 +010011242 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11243 vmcs12->exit_qualification,
11244 vmcs12->idt_vectoring_info_field,
11245 vmcs12->vm_exit_intr_info,
11246 vmcs12->vm_exit_intr_error_code,
11247 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011248
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011249 vm_entry_controls_reset_shadow(vmx);
11250 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011251 vmx_segment_cache_clear(vmx);
11252
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011253 load_vmcs12_host_state(vcpu, vmcs12);
11254
Paolo Bonzini93140062016-07-06 13:23:51 +020011255 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011256 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11257 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011258 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011259 if (vmx->hv_deadline_tsc == -1)
11260 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11261 PIN_BASED_VMX_PREEMPTION_TIMER);
11262 else
11263 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11264 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011265 if (kvm_has_tsc_control)
11266 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011267
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011268 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11269 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11270 vmx_set_virtual_x2apic_mode(vcpu,
11271 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011272 } else if (!nested_cpu_has_ept(vmcs12) &&
11273 nested_cpu_has2(vmcs12,
11274 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11275 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011276 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011277
11278 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11279 vmx->host_rsp = 0;
11280
11281 /* Unpin physical memory we referred to in vmcs02 */
11282 if (vmx->nested.apic_access_page) {
11283 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011284 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011285 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011286 if (vmx->nested.virtual_apic_page) {
11287 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011288 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011289 }
Wincy Van705699a2015-02-03 23:58:17 +080011290 if (vmx->nested.pi_desc_page) {
11291 kunmap(vmx->nested.pi_desc_page);
11292 nested_release_page(vmx->nested.pi_desc_page);
11293 vmx->nested.pi_desc_page = NULL;
11294 vmx->nested.pi_desc = NULL;
11295 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011296
11297 /*
Tang Chen38b99172014-09-24 15:57:54 +080011298 * We are now running in L2, mmu_notifier will force to reload the
11299 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11300 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011301 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011302
11303 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011304 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11305 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11306 * success or failure flag accordingly.
11307 */
11308 if (unlikely(vmx->fail)) {
11309 vmx->fail = 0;
11310 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11311 } else
11312 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011313 if (enable_shadow_vmcs)
11314 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011315
11316 /* in case we halted in L2 */
11317 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011318}
11319
Nadav Har'El7c177932011-05-25 23:12:04 +030011320/*
Jan Kiszka42124922014-01-04 18:47:19 +010011321 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11322 */
11323static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11324{
Wanpeng Lic886f282017-03-06 04:03:28 -080011325 if (is_guest_mode(vcpu)) {
11326 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011327 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011328 }
Jan Kiszka42124922014-01-04 18:47:19 +010011329 free_nested(to_vmx(vcpu));
11330}
11331
11332/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011333 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11334 * 23.7 "VM-entry failures during or after loading guest state" (this also
11335 * lists the acceptable exit-reason and exit-qualification parameters).
11336 * It should only be called before L2 actually succeeded to run, and when
11337 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11338 */
11339static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11340 struct vmcs12 *vmcs12,
11341 u32 reason, unsigned long qualification)
11342{
11343 load_vmcs12_host_state(vcpu, vmcs12);
11344 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11345 vmcs12->exit_qualification = qualification;
11346 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011347 if (enable_shadow_vmcs)
11348 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011349}
11350
Oliver Uptonf3e0dfb2020-02-04 15:26:31 -080011351static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
11352 struct x86_instruction_info *info)
11353{
11354 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11355 unsigned short port;
11356 bool intercept;
11357 int size;
11358
11359 if (info->intercept == x86_intercept_in ||
11360 info->intercept == x86_intercept_ins) {
11361 port = info->src_val;
11362 size = info->dst_bytes;
11363 } else {
11364 port = info->dst_val;
11365 size = info->src_bytes;
11366 }
11367
11368 /*
11369 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
11370 * VM-exits depend on the 'unconditional IO exiting' VM-execution
11371 * control.
11372 *
11373 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
11374 */
11375 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11376 intercept = nested_cpu_has(vmcs12,
11377 CPU_BASED_UNCOND_IO_EXITING);
11378 else
11379 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
11380
11381 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
11382}
11383
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011384static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11385 struct x86_instruction_info *info,
11386 enum x86_intercept_stage stage)
11387{
Paolo Bonzini01b7a502016-07-12 11:04:26 +020011388 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11389 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11390
Oliver Uptonf3e0dfb2020-02-04 15:26:31 -080011391 switch (info->intercept) {
Paolo Bonzini01b7a502016-07-12 11:04:26 +020011392 /*
11393 * RDPID causes #UD if disabled through secondary execution controls.
11394 * Because it is marked as EmulateOnUD, we need to intercept it here.
11395 */
Oliver Uptonf3e0dfb2020-02-04 15:26:31 -080011396 case x86_intercept_rdtscp:
11397 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11398 ctxt->exception.vector = UD_VECTOR;
11399 ctxt->exception.error_code_valid = false;
11400 return X86EMUL_PROPAGATE_FAULT;
11401 }
11402 break;
11403
11404 case x86_intercept_in:
11405 case x86_intercept_ins:
11406 case x86_intercept_out:
11407 case x86_intercept_outs:
11408 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzini01b7a502016-07-12 11:04:26 +020011409
11410 /* TODO: check more intercepts... */
Oliver Uptonf3e0dfb2020-02-04 15:26:31 -080011411 default:
11412 break;
11413 }
11414
Paolo Bonzini86dc39e2020-02-04 15:26:29 -080011415 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011416}
11417
Yunhong Jiang64672c92016-06-13 14:19:59 -070011418#ifdef CONFIG_X86_64
11419/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11420static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11421 u64 divisor, u64 *result)
11422{
11423 u64 low = a << shift, high = a >> (64 - shift);
11424
11425 /* To avoid the overflow on divq */
11426 if (high >= divisor)
11427 return 1;
11428
11429 /* Low hold the result, high hold rem which is discarded */
11430 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11431 "rm" (divisor), "0" (low), "1" (high));
11432 *result = low;
11433
11434 return 0;
11435}
11436
11437static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11438{
11439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011440 u64 tscl = rdtsc();
11441 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11442 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011443
11444 /* Convert to host delta tsc if tsc scaling is enabled */
11445 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11446 u64_shl_div_u64(delta_tsc,
11447 kvm_tsc_scaling_ratio_frac_bits,
11448 vcpu->arch.tsc_scaling_ratio,
11449 &delta_tsc))
11450 return -ERANGE;
11451
11452 /*
11453 * If the delta tsc can't fit in the 32 bit after the multi shift,
11454 * we can't use the preemption timer.
11455 * It's possible that it fits on later vmentries, but checking
11456 * on every vmentry is costly so we just use an hrtimer.
11457 */
11458 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11459 return -ERANGE;
11460
11461 vmx->hv_deadline_tsc = tscl + delta_tsc;
11462 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11463 PIN_BASED_VMX_PREEMPTION_TIMER);
11464 return 0;
11465}
11466
11467static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11468{
11469 struct vcpu_vmx *vmx = to_vmx(vcpu);
11470 vmx->hv_deadline_tsc = -1;
11471 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11472 PIN_BASED_VMX_PREEMPTION_TIMER);
11473}
11474#endif
11475
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011476static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011477{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011478 if (ple_gap)
11479 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011480}
11481
Kai Huang843e4332015-01-28 10:54:28 +080011482static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11483 struct kvm_memory_slot *slot)
11484{
11485 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11486 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11487}
11488
11489static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11490 struct kvm_memory_slot *slot)
11491{
11492 kvm_mmu_slot_set_dirty(kvm, slot);
11493}
11494
11495static void vmx_flush_log_dirty(struct kvm *kvm)
11496{
11497 kvm_flush_pml_buffers(kvm);
11498}
11499
11500static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11501 struct kvm_memory_slot *memslot,
11502 gfn_t offset, unsigned long mask)
11503{
11504 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11505}
11506
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011507static void __pi_post_block(struct kvm_vcpu *vcpu)
11508{
11509 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11510 struct pi_desc old, new;
11511 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011512
11513 do {
11514 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011515 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11516 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011517
11518 dest = cpu_physical_id(vcpu->cpu);
11519
11520 if (x2apic_enabled())
11521 new.ndst = dest;
11522 else
11523 new.ndst = (dest << 8) & 0xFF00;
11524
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011525 /* set 'NV' to 'notification vector' */
11526 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011527 } while (cmpxchg64(&pi_desc->control, old.control,
11528 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011529
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011530 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11531 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011532 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011533 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011534 vcpu->pre_pcpu = -1;
11535 }
11536}
11537
Feng Wuefc64402015-09-18 22:29:51 +080011538/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011539 * This routine does the following things for vCPU which is going
11540 * to be blocked if VT-d PI is enabled.
11541 * - Store the vCPU to the wakeup list, so when interrupts happen
11542 * we can find the right vCPU to wake up.
11543 * - Change the Posted-interrupt descriptor as below:
11544 * 'NDST' <-- vcpu->pre_pcpu
11545 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11546 * - If 'ON' is set during this process, which means at least one
11547 * interrupt is posted for this vCPU, we cannot block it, in
11548 * this case, return 1, otherwise, return 0.
11549 *
11550 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011551static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011552{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011553 unsigned int dest;
11554 struct pi_desc old, new;
11555 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11556
11557 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011558 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11559 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011560 return 0;
11561
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011562 WARN_ON(irqs_disabled());
11563 local_irq_disable();
11564 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11565 vcpu->pre_pcpu = vcpu->cpu;
11566 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11567 list_add_tail(&vcpu->blocked_vcpu_list,
11568 &per_cpu(blocked_vcpu_on_cpu,
11569 vcpu->pre_pcpu));
11570 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11571 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011572
11573 do {
11574 old.control = new.control = pi_desc->control;
11575
Feng Wubf9f6ac2015-09-18 22:29:55 +080011576 WARN((pi_desc->sn == 1),
11577 "Warning: SN field of posted-interrupts "
11578 "is set before blocking\n");
11579
11580 /*
11581 * Since vCPU can be preempted during this process,
11582 * vcpu->cpu could be different with pre_pcpu, we
11583 * need to set pre_pcpu as the destination of wakeup
11584 * notification event, then we can find the right vCPU
11585 * to wakeup in wakeup handler if interrupts happen
11586 * when the vCPU is in blocked state.
11587 */
11588 dest = cpu_physical_id(vcpu->pre_pcpu);
11589
11590 if (x2apic_enabled())
11591 new.ndst = dest;
11592 else
11593 new.ndst = (dest << 8) & 0xFF00;
11594
11595 /* set 'NV' to 'wakeup vector' */
11596 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011597 } while (cmpxchg64(&pi_desc->control, old.control,
11598 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011599
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011600 /* We should not block the vCPU if an interrupt is posted for it. */
11601 if (pi_test_on(pi_desc) == 1)
11602 __pi_post_block(vcpu);
11603
11604 local_irq_enable();
11605 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011606}
11607
Yunhong Jiangbc225122016-06-13 14:19:58 -070011608static int vmx_pre_block(struct kvm_vcpu *vcpu)
11609{
11610 if (pi_pre_block(vcpu))
11611 return 1;
11612
Yunhong Jiang64672c92016-06-13 14:19:59 -070011613 if (kvm_lapic_hv_timer_in_use(vcpu))
11614 kvm_lapic_switch_to_sw_timer(vcpu);
11615
Yunhong Jiangbc225122016-06-13 14:19:58 -070011616 return 0;
11617}
11618
11619static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011620{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011621 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011622 return;
11623
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011624 WARN_ON(irqs_disabled());
11625 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011626 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011627 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011628}
11629
Yunhong Jiangbc225122016-06-13 14:19:58 -070011630static void vmx_post_block(struct kvm_vcpu *vcpu)
11631{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011632 if (kvm_x86_ops->set_hv_timer)
11633 kvm_lapic_switch_to_hv_timer(vcpu);
11634
Yunhong Jiangbc225122016-06-13 14:19:58 -070011635 pi_post_block(vcpu);
11636}
11637
Feng Wubf9f6ac2015-09-18 22:29:55 +080011638/*
Feng Wuefc64402015-09-18 22:29:51 +080011639 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11640 *
11641 * @kvm: kvm
11642 * @host_irq: host irq of the interrupt
11643 * @guest_irq: gsi of the interrupt
11644 * @set: set or unset PI
11645 * returns 0 on success, < 0 on failure
11646 */
11647static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11648 uint32_t guest_irq, bool set)
11649{
11650 struct kvm_kernel_irq_routing_entry *e;
11651 struct kvm_irq_routing_table *irq_rt;
11652 struct kvm_lapic_irq irq;
11653 struct kvm_vcpu *vcpu;
11654 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011655 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011656
11657 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011658 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11659 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011660 return 0;
11661
11662 idx = srcu_read_lock(&kvm->irq_srcu);
11663 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011664 if (guest_irq >= irq_rt->nr_rt_entries ||
11665 hlist_empty(&irq_rt->map[guest_irq])) {
11666 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11667 guest_irq, irq_rt->nr_rt_entries);
11668 goto out;
11669 }
Feng Wuefc64402015-09-18 22:29:51 +080011670
11671 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11672 if (e->type != KVM_IRQ_ROUTING_MSI)
11673 continue;
11674 /*
11675 * VT-d PI cannot support posting multicast/broadcast
11676 * interrupts to a vCPU, we still use interrupt remapping
11677 * for these kind of interrupts.
11678 *
11679 * For lowest-priority interrupts, we only support
11680 * those with single CPU as the destination, e.g. user
11681 * configures the interrupts via /proc/irq or uses
11682 * irqbalance to make the interrupts single-CPU.
11683 *
11684 * We will support full lowest-priority interrupt later.
11685 */
11686
Radim Krčmář371313132016-07-12 22:09:27 +020011687 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011688 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11689 /*
11690 * Make sure the IRTE is in remapped mode if
11691 * we don't handle it in posted mode.
11692 */
11693 ret = irq_set_vcpu_affinity(host_irq, NULL);
11694 if (ret < 0) {
11695 printk(KERN_INFO
11696 "failed to back to remapped mode, irq: %u\n",
11697 host_irq);
11698 goto out;
11699 }
11700
Feng Wuefc64402015-09-18 22:29:51 +080011701 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011702 }
Feng Wuefc64402015-09-18 22:29:51 +080011703
11704 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11705 vcpu_info.vector = irq.vector;
11706
Feng Wub6ce9782016-01-25 16:53:35 +080011707 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011708 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11709
11710 if (set)
11711 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011712 else
Feng Wuefc64402015-09-18 22:29:51 +080011713 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011714
11715 if (ret < 0) {
11716 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11717 __func__);
11718 goto out;
11719 }
11720 }
11721
11722 ret = 0;
11723out:
11724 srcu_read_unlock(&kvm->irq_srcu, idx);
11725 return ret;
11726}
11727
Ashok Rajc45dcc72016-06-22 14:59:56 +080011728static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11729{
11730 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11731 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11732 FEATURE_CONTROL_LMCE;
11733 else
11734 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11735 ~FEATURE_CONTROL_LMCE;
11736}
11737
Kees Cook404f6aa2016-08-08 16:29:06 -070011738static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011739 .cpu_has_kvm_support = cpu_has_kvm_support,
11740 .disabled_by_bios = vmx_disabled_by_bios,
11741 .hardware_setup = hardware_setup,
11742 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011743 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011744 .hardware_enable = hardware_enable,
11745 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011746 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011747 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011748
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011749 .vm_init = vmx_vm_init,
11750
Avi Kivity6aa8b732006-12-10 02:21:36 -080011751 .vcpu_create = vmx_create_vcpu,
11752 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011753 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011754
Avi Kivity04d2cc72007-09-10 18:10:54 +030011755 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011756 .vcpu_load = vmx_vcpu_load,
11757 .vcpu_put = vmx_vcpu_put,
11758
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011759 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky62d88fc2018-02-21 13:39:51 -060011760 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011761 .get_msr = vmx_get_msr,
11762 .set_msr = vmx_set_msr,
11763 .get_segment_base = vmx_get_segment_base,
11764 .get_segment = vmx_get_segment,
11765 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011766 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011767 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011768 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011769 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011770 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011771 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011772 .set_cr3 = vmx_set_cr3,
11773 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011774 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011775 .get_idt = vmx_get_idt,
11776 .set_idt = vmx_set_idt,
11777 .get_gdt = vmx_get_gdt,
11778 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011779 .get_dr6 = vmx_get_dr6,
11780 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011781 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011782 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011783 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011784 .get_rflags = vmx_get_rflags,
11785 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011786
11787 .get_pkru = vmx_get_pkru,
11788
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011789 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011790 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011791
11792 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011793
Avi Kivity6aa8b732006-12-10 02:21:36 -080011794 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011795 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011796 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011797 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11798 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011799 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011800 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011801 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011802 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011803 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011804 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011805 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011806 .get_nmi_mask = vmx_get_nmi_mask,
11807 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011808 .enable_nmi_window = enable_nmi_window,
11809 .enable_irq_window = enable_irq_window,
11810 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011811 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011812 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011813 .get_enable_apicv = vmx_get_enable_apicv,
11814 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011815 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11816 .hwapic_irr_update = vmx_hwapic_irr_update,
11817 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011818 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11819 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011820
Izik Eiduscbc94022007-10-25 00:29:55 +020011821 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011822 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011823 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011824
Avi Kivity586f9602010-11-18 13:09:54 +020011825 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011826
Sheng Yang17cc3932010-01-05 19:02:27 +080011827 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011828
11829 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011830
11831 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011832 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011833
11834 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011835
11836 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011837
11838 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011839
11840 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011841
11842 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011843 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011844 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011845 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011846
11847 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011848
11849 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011850
11851 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11852 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11853 .flush_log_dirty = vmx_flush_log_dirty,
11854 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011855
Feng Wubf9f6ac2015-09-18 22:29:55 +080011856 .pre_block = vmx_pre_block,
11857 .post_block = vmx_post_block,
11858
Wei Huang25462f72015-06-19 15:45:05 +020011859 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011860
11861 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011862
11863#ifdef CONFIG_X86_64
11864 .set_hv_timer = vmx_set_hv_timer,
11865 .cancel_hv_timer = vmx_cancel_hv_timer,
11866#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011867
11868 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011869};
11870
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011871static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011872{
11873 if (vmx_l1d_flush_pages) {
11874 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11875 vmx_l1d_flush_pages = NULL;
11876 }
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011877 /* Restore state so sysfs ignores VMX */
11878 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011879}
11880
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011881
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011882static void vmx_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011883{
Dave Young2965faa2015-09-09 15:38:55 -070011884#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011885 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011886 synchronize_rcu();
11887#endif
11888
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011889 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011890
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011891 vmx_cleanup_l1d_flush();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011892}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011893module_exit(vmx_exit)
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011894
Avi Kivity6aa8b732006-12-10 02:21:36 -080011895static int __init vmx_init(void)
11896{
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011897 int r;
11898
11899 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11900 __alignof__(struct vcpu_vmx), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011901 if (r)
11902 return r;
11903
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011904 /*
Thomas Gleixner641a2112018-07-13 16:23:19 +020011905 * Must be called after kvm_init() so enable_ept is properly set
11906 * up. Hand the parameter mitigation value in which was stored in
11907 * the pre module init parser. If no parameter was given, it will
11908 * contain 'auto' which will be turned into the default 'cond'
11909 * mitigation mode.
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011910 */
Thomas Gleixner641a2112018-07-13 16:23:19 +020011911 if (boot_cpu_has(X86_BUG_L1TF)) {
11912 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
11913 if (r) {
11914 vmx_exit();
11915 return r;
11916 }
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011917 }
11918
Avi Kivity6aa8b732006-12-10 02:21:36 -080011919#ifdef CONFIG_KEXEC_CORE
11920 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11921 crash_vmclear_local_loaded_vmcss);
11922#endif
11923
11924 return 0;
11925}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011926module_init(vmx_init)