blob: 8384f7cb30d80982cb8af6b26b271e1595c906d9 [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 Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-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>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200193static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200195/* Storage for pre module init parameter parsing */
196static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197
198static const struct {
199 const char *option;
200 enum vmx_l1d_flush_state cmd;
201} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200202 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203 {"never", VMENTER_L1D_FLUSH_NEVER},
204 {"cond", VMENTER_L1D_FLUSH_COND},
205 {"always", VMENTER_L1D_FLUSH_ALWAYS},
206};
207
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200208#define L1D_CACHE_ORDER 4
209static void *vmx_l1d_flush_pages;
210
211static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
212{
213 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200214 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200215
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200216 if (!enable_ept) {
217 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
218 return 0;
219 }
220
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200221 /* If set to auto use the default l1tf mitigation method */
222 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
223 switch (l1tf_mitigation) {
224 case L1TF_MITIGATION_OFF:
225 l1tf = VMENTER_L1D_FLUSH_NEVER;
226 break;
227 case L1TF_MITIGATION_FLUSH_NOWARN:
228 case L1TF_MITIGATION_FLUSH:
229 case L1TF_MITIGATION_FLUSH_NOSMT:
230 l1tf = VMENTER_L1D_FLUSH_COND;
231 break;
232 case L1TF_MITIGATION_FULL:
233 case L1TF_MITIGATION_FULL_FORCE:
234 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
235 break;
236 }
237 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
238 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
239 }
240
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200241 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
242 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
243 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
244 if (!page)
245 return -ENOMEM;
246 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200247
248 /*
249 * Initialize each page with a different pattern in
250 * order to protect against KSM in the nested
251 * virtualization case.
252 */
253 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
254 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
255 PAGE_SIZE);
256 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200257 }
258
259 l1tf_vmx_mitigation = l1tf;
260
Thomas Gleixner895ae472018-07-13 16:23:22 +0200261 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
262 static_branch_enable(&vmx_l1d_should_flush);
263 else
264 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200265
Nicolai Stange427362a2018-07-21 22:25:00 +0200266 if (l1tf == VMENTER_L1D_FLUSH_COND)
267 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200268 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200269 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200270 return 0;
271}
272
273static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200274{
275 unsigned int i;
276
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200277 if (s) {
278 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 if (sysfs_streq(s, vmentry_l1d_param[i].option))
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200280 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200281 }
282 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200283 return -EINVAL;
284}
285
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200286static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
287{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200288 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200289
290 if (!boot_cpu_has(X86_BUG_L1TF))
291 return 0;
292
293 l1tf = vmentry_l1d_flush_parse(s);
294 if (l1tf < 0)
295 return l1tf;
296
297 /*
298 * Has vmx_init() run already? If not then this is the pre init
299 * parameter parsing. In that case just store the value and let
300 * vmx_init() do the proper setup after enable_ept has been
301 * established.
302 */
303 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
304 vmentry_l1d_flush_param = l1tf;
305 return 0;
306 }
307
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200308 mutex_lock(&vmx_l1d_flush_mutex);
309 ret = vmx_setup_l1d_flush(l1tf);
310 mutex_unlock(&vmx_l1d_flush_mutex);
311 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200312}
313
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200314static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
315{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200316 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200317}
318
319static const struct kernel_param_ops vmentry_l1d_flush_ops = {
320 .set = vmentry_l1d_flush_set,
321 .get = vmentry_l1d_flush_get,
322};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200323module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200324
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700325struct kvm_vmx {
326 struct kvm kvm;
327
328 unsigned int tss_addr;
329 bool ept_identity_pagetable_done;
330 gpa_t ept_identity_map_addr;
331};
332
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200333#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300334
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400335struct vmcs {
336 u32 revision_id;
337 u32 abort;
338 char data[0];
339};
340
Nadav Har'Eld462b812011-05-24 15:26:10 +0300341/*
342 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
343 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
344 * loaded on this CPU (so we can clear them if the CPU goes down).
345 */
346struct loaded_vmcs {
347 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700348 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300349 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200350 bool launched;
351 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200352 unsigned long vmcs_host_cr3; /* May not match real cr3 */
353 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100354 /* Support for vnmi-less CPUs */
355 int soft_vnmi_blocked;
356 ktime_t entry_time;
357 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100358 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300359 struct list_head loaded_vmcss_on_cpu_link;
360};
361
Avi Kivity26bb0982009-09-07 11:14:12 +0300362struct shared_msr_entry {
363 unsigned index;
364 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200365 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300366};
367
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300368/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300369 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
370 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
371 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
372 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
373 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
374 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600375 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300376 * underlying hardware which will be used to run L2.
377 * This structure is packed to ensure that its layout is identical across
378 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700379 *
380 * IMPORTANT: Changing the layout of existing fields in this structure
381 * will break save/restore compatibility with older kvm releases. When
382 * adding new fields, either use space in the reserved padding* arrays
383 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300384 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300385typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300386struct __packed vmcs12 {
387 /* According to the Intel spec, a VMCS region must start with the
388 * following two fields. Then follow implementation-specific data.
389 */
390 u32 revision_id;
391 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300392
Nadav Har'El27d6c862011-05-25 23:06:59 +0300393 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
394 u32 padding[7]; /* room for future expansion */
395
Nadav Har'El22bd0352011-05-25 23:05:57 +0300396 u64 io_bitmap_a;
397 u64 io_bitmap_b;
398 u64 msr_bitmap;
399 u64 vm_exit_msr_store_addr;
400 u64 vm_exit_msr_load_addr;
401 u64 vm_entry_msr_load_addr;
402 u64 tsc_offset;
403 u64 virtual_apic_page_addr;
404 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800405 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300406 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800407 u64 eoi_exit_bitmap0;
408 u64 eoi_exit_bitmap1;
409 u64 eoi_exit_bitmap2;
410 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800411 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300412 u64 guest_physical_address;
413 u64 vmcs_link_pointer;
414 u64 guest_ia32_debugctl;
415 u64 guest_ia32_pat;
416 u64 guest_ia32_efer;
417 u64 guest_ia32_perf_global_ctrl;
418 u64 guest_pdptr0;
419 u64 guest_pdptr1;
420 u64 guest_pdptr2;
421 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100422 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300423 u64 host_ia32_pat;
424 u64 host_ia32_efer;
425 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700426 u64 vmread_bitmap;
427 u64 vmwrite_bitmap;
428 u64 vm_function_control;
429 u64 eptp_list_address;
430 u64 pml_address;
431 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300432 /*
433 * To allow migration of L1 (complete with its L2 guests) between
434 * machines of different natural widths (32 or 64 bit), we cannot have
435 * unsigned long fields with no explict size. We use u64 (aliased
436 * natural_width) instead. Luckily, x86 is little-endian.
437 */
438 natural_width cr0_guest_host_mask;
439 natural_width cr4_guest_host_mask;
440 natural_width cr0_read_shadow;
441 natural_width cr4_read_shadow;
442 natural_width cr3_target_value0;
443 natural_width cr3_target_value1;
444 natural_width cr3_target_value2;
445 natural_width cr3_target_value3;
446 natural_width exit_qualification;
447 natural_width guest_linear_address;
448 natural_width guest_cr0;
449 natural_width guest_cr3;
450 natural_width guest_cr4;
451 natural_width guest_es_base;
452 natural_width guest_cs_base;
453 natural_width guest_ss_base;
454 natural_width guest_ds_base;
455 natural_width guest_fs_base;
456 natural_width guest_gs_base;
457 natural_width guest_ldtr_base;
458 natural_width guest_tr_base;
459 natural_width guest_gdtr_base;
460 natural_width guest_idtr_base;
461 natural_width guest_dr7;
462 natural_width guest_rsp;
463 natural_width guest_rip;
464 natural_width guest_rflags;
465 natural_width guest_pending_dbg_exceptions;
466 natural_width guest_sysenter_esp;
467 natural_width guest_sysenter_eip;
468 natural_width host_cr0;
469 natural_width host_cr3;
470 natural_width host_cr4;
471 natural_width host_fs_base;
472 natural_width host_gs_base;
473 natural_width host_tr_base;
474 natural_width host_gdtr_base;
475 natural_width host_idtr_base;
476 natural_width host_ia32_sysenter_esp;
477 natural_width host_ia32_sysenter_eip;
478 natural_width host_rsp;
479 natural_width host_rip;
480 natural_width paddingl[8]; /* room for future expansion */
481 u32 pin_based_vm_exec_control;
482 u32 cpu_based_vm_exec_control;
483 u32 exception_bitmap;
484 u32 page_fault_error_code_mask;
485 u32 page_fault_error_code_match;
486 u32 cr3_target_count;
487 u32 vm_exit_controls;
488 u32 vm_exit_msr_store_count;
489 u32 vm_exit_msr_load_count;
490 u32 vm_entry_controls;
491 u32 vm_entry_msr_load_count;
492 u32 vm_entry_intr_info_field;
493 u32 vm_entry_exception_error_code;
494 u32 vm_entry_instruction_len;
495 u32 tpr_threshold;
496 u32 secondary_vm_exec_control;
497 u32 vm_instruction_error;
498 u32 vm_exit_reason;
499 u32 vm_exit_intr_info;
500 u32 vm_exit_intr_error_code;
501 u32 idt_vectoring_info_field;
502 u32 idt_vectoring_error_code;
503 u32 vm_exit_instruction_len;
504 u32 vmx_instruction_info;
505 u32 guest_es_limit;
506 u32 guest_cs_limit;
507 u32 guest_ss_limit;
508 u32 guest_ds_limit;
509 u32 guest_fs_limit;
510 u32 guest_gs_limit;
511 u32 guest_ldtr_limit;
512 u32 guest_tr_limit;
513 u32 guest_gdtr_limit;
514 u32 guest_idtr_limit;
515 u32 guest_es_ar_bytes;
516 u32 guest_cs_ar_bytes;
517 u32 guest_ss_ar_bytes;
518 u32 guest_ds_ar_bytes;
519 u32 guest_fs_ar_bytes;
520 u32 guest_gs_ar_bytes;
521 u32 guest_ldtr_ar_bytes;
522 u32 guest_tr_ar_bytes;
523 u32 guest_interruptibility_info;
524 u32 guest_activity_state;
525 u32 guest_sysenter_cs;
526 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100527 u32 vmx_preemption_timer_value;
528 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300529 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800530 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300531 u16 guest_es_selector;
532 u16 guest_cs_selector;
533 u16 guest_ss_selector;
534 u16 guest_ds_selector;
535 u16 guest_fs_selector;
536 u16 guest_gs_selector;
537 u16 guest_ldtr_selector;
538 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800539 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300540 u16 host_es_selector;
541 u16 host_cs_selector;
542 u16 host_ss_selector;
543 u16 host_ds_selector;
544 u16 host_fs_selector;
545 u16 host_gs_selector;
546 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700547 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300548};
549
550/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700551 * For save/restore compatibility, the vmcs12 field offsets must not change.
552 */
553#define CHECK_OFFSET(field, loc) \
554 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
555 "Offset of " #field " in struct vmcs12 has changed.")
556
557static inline void vmx_check_vmcs12_offsets(void) {
558 CHECK_OFFSET(revision_id, 0);
559 CHECK_OFFSET(abort, 4);
560 CHECK_OFFSET(launch_state, 8);
561 CHECK_OFFSET(io_bitmap_a, 40);
562 CHECK_OFFSET(io_bitmap_b, 48);
563 CHECK_OFFSET(msr_bitmap, 56);
564 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
565 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
566 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
567 CHECK_OFFSET(tsc_offset, 88);
568 CHECK_OFFSET(virtual_apic_page_addr, 96);
569 CHECK_OFFSET(apic_access_addr, 104);
570 CHECK_OFFSET(posted_intr_desc_addr, 112);
571 CHECK_OFFSET(ept_pointer, 120);
572 CHECK_OFFSET(eoi_exit_bitmap0, 128);
573 CHECK_OFFSET(eoi_exit_bitmap1, 136);
574 CHECK_OFFSET(eoi_exit_bitmap2, 144);
575 CHECK_OFFSET(eoi_exit_bitmap3, 152);
576 CHECK_OFFSET(xss_exit_bitmap, 160);
577 CHECK_OFFSET(guest_physical_address, 168);
578 CHECK_OFFSET(vmcs_link_pointer, 176);
579 CHECK_OFFSET(guest_ia32_debugctl, 184);
580 CHECK_OFFSET(guest_ia32_pat, 192);
581 CHECK_OFFSET(guest_ia32_efer, 200);
582 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
583 CHECK_OFFSET(guest_pdptr0, 216);
584 CHECK_OFFSET(guest_pdptr1, 224);
585 CHECK_OFFSET(guest_pdptr2, 232);
586 CHECK_OFFSET(guest_pdptr3, 240);
587 CHECK_OFFSET(guest_bndcfgs, 248);
588 CHECK_OFFSET(host_ia32_pat, 256);
589 CHECK_OFFSET(host_ia32_efer, 264);
590 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
591 CHECK_OFFSET(vmread_bitmap, 280);
592 CHECK_OFFSET(vmwrite_bitmap, 288);
593 CHECK_OFFSET(vm_function_control, 296);
594 CHECK_OFFSET(eptp_list_address, 304);
595 CHECK_OFFSET(pml_address, 312);
596 CHECK_OFFSET(cr0_guest_host_mask, 344);
597 CHECK_OFFSET(cr4_guest_host_mask, 352);
598 CHECK_OFFSET(cr0_read_shadow, 360);
599 CHECK_OFFSET(cr4_read_shadow, 368);
600 CHECK_OFFSET(cr3_target_value0, 376);
601 CHECK_OFFSET(cr3_target_value1, 384);
602 CHECK_OFFSET(cr3_target_value2, 392);
603 CHECK_OFFSET(cr3_target_value3, 400);
604 CHECK_OFFSET(exit_qualification, 408);
605 CHECK_OFFSET(guest_linear_address, 416);
606 CHECK_OFFSET(guest_cr0, 424);
607 CHECK_OFFSET(guest_cr3, 432);
608 CHECK_OFFSET(guest_cr4, 440);
609 CHECK_OFFSET(guest_es_base, 448);
610 CHECK_OFFSET(guest_cs_base, 456);
611 CHECK_OFFSET(guest_ss_base, 464);
612 CHECK_OFFSET(guest_ds_base, 472);
613 CHECK_OFFSET(guest_fs_base, 480);
614 CHECK_OFFSET(guest_gs_base, 488);
615 CHECK_OFFSET(guest_ldtr_base, 496);
616 CHECK_OFFSET(guest_tr_base, 504);
617 CHECK_OFFSET(guest_gdtr_base, 512);
618 CHECK_OFFSET(guest_idtr_base, 520);
619 CHECK_OFFSET(guest_dr7, 528);
620 CHECK_OFFSET(guest_rsp, 536);
621 CHECK_OFFSET(guest_rip, 544);
622 CHECK_OFFSET(guest_rflags, 552);
623 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
624 CHECK_OFFSET(guest_sysenter_esp, 568);
625 CHECK_OFFSET(guest_sysenter_eip, 576);
626 CHECK_OFFSET(host_cr0, 584);
627 CHECK_OFFSET(host_cr3, 592);
628 CHECK_OFFSET(host_cr4, 600);
629 CHECK_OFFSET(host_fs_base, 608);
630 CHECK_OFFSET(host_gs_base, 616);
631 CHECK_OFFSET(host_tr_base, 624);
632 CHECK_OFFSET(host_gdtr_base, 632);
633 CHECK_OFFSET(host_idtr_base, 640);
634 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
635 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
636 CHECK_OFFSET(host_rsp, 664);
637 CHECK_OFFSET(host_rip, 672);
638 CHECK_OFFSET(pin_based_vm_exec_control, 744);
639 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
640 CHECK_OFFSET(exception_bitmap, 752);
641 CHECK_OFFSET(page_fault_error_code_mask, 756);
642 CHECK_OFFSET(page_fault_error_code_match, 760);
643 CHECK_OFFSET(cr3_target_count, 764);
644 CHECK_OFFSET(vm_exit_controls, 768);
645 CHECK_OFFSET(vm_exit_msr_store_count, 772);
646 CHECK_OFFSET(vm_exit_msr_load_count, 776);
647 CHECK_OFFSET(vm_entry_controls, 780);
648 CHECK_OFFSET(vm_entry_msr_load_count, 784);
649 CHECK_OFFSET(vm_entry_intr_info_field, 788);
650 CHECK_OFFSET(vm_entry_exception_error_code, 792);
651 CHECK_OFFSET(vm_entry_instruction_len, 796);
652 CHECK_OFFSET(tpr_threshold, 800);
653 CHECK_OFFSET(secondary_vm_exec_control, 804);
654 CHECK_OFFSET(vm_instruction_error, 808);
655 CHECK_OFFSET(vm_exit_reason, 812);
656 CHECK_OFFSET(vm_exit_intr_info, 816);
657 CHECK_OFFSET(vm_exit_intr_error_code, 820);
658 CHECK_OFFSET(idt_vectoring_info_field, 824);
659 CHECK_OFFSET(idt_vectoring_error_code, 828);
660 CHECK_OFFSET(vm_exit_instruction_len, 832);
661 CHECK_OFFSET(vmx_instruction_info, 836);
662 CHECK_OFFSET(guest_es_limit, 840);
663 CHECK_OFFSET(guest_cs_limit, 844);
664 CHECK_OFFSET(guest_ss_limit, 848);
665 CHECK_OFFSET(guest_ds_limit, 852);
666 CHECK_OFFSET(guest_fs_limit, 856);
667 CHECK_OFFSET(guest_gs_limit, 860);
668 CHECK_OFFSET(guest_ldtr_limit, 864);
669 CHECK_OFFSET(guest_tr_limit, 868);
670 CHECK_OFFSET(guest_gdtr_limit, 872);
671 CHECK_OFFSET(guest_idtr_limit, 876);
672 CHECK_OFFSET(guest_es_ar_bytes, 880);
673 CHECK_OFFSET(guest_cs_ar_bytes, 884);
674 CHECK_OFFSET(guest_ss_ar_bytes, 888);
675 CHECK_OFFSET(guest_ds_ar_bytes, 892);
676 CHECK_OFFSET(guest_fs_ar_bytes, 896);
677 CHECK_OFFSET(guest_gs_ar_bytes, 900);
678 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
679 CHECK_OFFSET(guest_tr_ar_bytes, 908);
680 CHECK_OFFSET(guest_interruptibility_info, 912);
681 CHECK_OFFSET(guest_activity_state, 916);
682 CHECK_OFFSET(guest_sysenter_cs, 920);
683 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
684 CHECK_OFFSET(vmx_preemption_timer_value, 928);
685 CHECK_OFFSET(virtual_processor_id, 960);
686 CHECK_OFFSET(posted_intr_nv, 962);
687 CHECK_OFFSET(guest_es_selector, 964);
688 CHECK_OFFSET(guest_cs_selector, 966);
689 CHECK_OFFSET(guest_ss_selector, 968);
690 CHECK_OFFSET(guest_ds_selector, 970);
691 CHECK_OFFSET(guest_fs_selector, 972);
692 CHECK_OFFSET(guest_gs_selector, 974);
693 CHECK_OFFSET(guest_ldtr_selector, 976);
694 CHECK_OFFSET(guest_tr_selector, 978);
695 CHECK_OFFSET(guest_intr_status, 980);
696 CHECK_OFFSET(host_es_selector, 982);
697 CHECK_OFFSET(host_cs_selector, 984);
698 CHECK_OFFSET(host_ss_selector, 986);
699 CHECK_OFFSET(host_ds_selector, 988);
700 CHECK_OFFSET(host_fs_selector, 990);
701 CHECK_OFFSET(host_gs_selector, 992);
702 CHECK_OFFSET(host_tr_selector, 994);
703 CHECK_OFFSET(guest_pml_index, 996);
704}
705
706/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300707 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
708 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
709 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700710 *
711 * IMPORTANT: Changing this value will break save/restore compatibility with
712 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300713 */
714#define VMCS12_REVISION 0x11e57ed0
715
716/*
717 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
718 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
719 * current implementation, 4K are reserved to avoid future complications.
720 */
721#define VMCS12_SIZE 0x1000
722
723/*
Jim Mattson5b157062017-12-22 12:11:12 -0800724 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
725 * supported VMCS12 field encoding.
726 */
727#define VMCS12_MAX_FIELD_INDEX 0x17
728
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100729struct nested_vmx_msrs {
730 /*
731 * We only store the "true" versions of the VMX capability MSRs. We
732 * generate the "non-true" versions by setting the must-be-1 bits
733 * according to the SDM.
734 */
735 u32 procbased_ctls_low;
736 u32 procbased_ctls_high;
737 u32 secondary_ctls_low;
738 u32 secondary_ctls_high;
739 u32 pinbased_ctls_low;
740 u32 pinbased_ctls_high;
741 u32 exit_ctls_low;
742 u32 exit_ctls_high;
743 u32 entry_ctls_low;
744 u32 entry_ctls_high;
745 u32 misc_low;
746 u32 misc_high;
747 u32 ept_caps;
748 u32 vpid_caps;
749 u64 basic;
750 u64 cr0_fixed0;
751 u64 cr0_fixed1;
752 u64 cr4_fixed0;
753 u64 cr4_fixed1;
754 u64 vmcs_enum;
755 u64 vmfunc_controls;
756};
757
Jim Mattson5b157062017-12-22 12:11:12 -0800758/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300759 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
760 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
761 */
762struct nested_vmx {
763 /* Has the level1 guest done vmxon? */
764 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400765 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400766 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300767
768 /* The guest-physical address of the current VMCS L1 keeps for L2 */
769 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700770 /*
771 * Cache of the guest's VMCS, existing outside of guest memory.
772 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700773 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700774 */
775 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300776 /*
777 * Indicates if the shadow vmcs must be updated with the
778 * data hold by vmcs12
779 */
780 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100781 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300782
Jim Mattson8d860bb2018-05-09 16:56:05 -0400783 bool change_vmcs01_virtual_apic_mode;
784
Nadav Har'El644d7112011-05-25 23:12:35 +0300785 /* L2 must run next, and mustn't decide to exit to L1. */
786 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600787
788 struct loaded_vmcs vmcs02;
789
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300790 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600791 * Guest pages referred to in the vmcs02 with host-physical
792 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300793 */
794 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800795 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800796 struct page *pi_desc_page;
797 struct pi_desc *pi_desc;
798 bool pi_pending;
799 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100800
801 struct hrtimer preemption_timer;
802 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200803
804 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
805 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800806
Wanpeng Li5c614b32015-10-13 09:18:36 -0700807 u16 vpid02;
808 u16 last_vpid;
809
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100810 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200811
812 /* SMM related state */
813 struct {
814 /* in VMX operation on SMM entry? */
815 bool vmxon;
816 /* in guest mode on SMM entry? */
817 bool guest_mode;
818 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300819};
820
Yang Zhang01e439b2013-04-11 19:25:12 +0800821#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800822#define POSTED_INTR_SN 1
823
Yang Zhang01e439b2013-04-11 19:25:12 +0800824/* Posted-Interrupt Descriptor */
825struct pi_desc {
826 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800827 union {
828 struct {
829 /* bit 256 - Outstanding Notification */
830 u16 on : 1,
831 /* bit 257 - Suppress Notification */
832 sn : 1,
833 /* bit 271:258 - Reserved */
834 rsvd_1 : 14;
835 /* bit 279:272 - Notification Vector */
836 u8 nv;
837 /* bit 287:280 - Reserved */
838 u8 rsvd_2;
839 /* bit 319:288 - Notification Destination */
840 u32 ndst;
841 };
842 u64 control;
843 };
844 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800845} __aligned(64);
846
Yang Zhanga20ed542013-04-11 19:25:15 +0800847static bool pi_test_and_set_on(struct pi_desc *pi_desc)
848{
849 return test_and_set_bit(POSTED_INTR_ON,
850 (unsigned long *)&pi_desc->control);
851}
852
853static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
854{
855 return test_and_clear_bit(POSTED_INTR_ON,
856 (unsigned long *)&pi_desc->control);
857}
858
859static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
860{
861 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
862}
863
Feng Wuebbfc762015-09-18 22:29:46 +0800864static inline void pi_clear_sn(struct pi_desc *pi_desc)
865{
866 return clear_bit(POSTED_INTR_SN,
867 (unsigned long *)&pi_desc->control);
868}
869
870static inline void pi_set_sn(struct pi_desc *pi_desc)
871{
872 return set_bit(POSTED_INTR_SN,
873 (unsigned long *)&pi_desc->control);
874}
875
Paolo Bonziniad361092016-09-20 16:15:05 +0200876static inline void pi_clear_on(struct pi_desc *pi_desc)
877{
878 clear_bit(POSTED_INTR_ON,
879 (unsigned long *)&pi_desc->control);
880}
881
Feng Wuebbfc762015-09-18 22:29:46 +0800882static inline int pi_test_on(struct pi_desc *pi_desc)
883{
884 return test_bit(POSTED_INTR_ON,
885 (unsigned long *)&pi_desc->control);
886}
887
888static inline int pi_test_sn(struct pi_desc *pi_desc)
889{
890 return test_bit(POSTED_INTR_SN,
891 (unsigned long *)&pi_desc->control);
892}
893
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400894struct vmx_msrs {
895 unsigned int nr;
896 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
897};
898
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000900 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300901 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300902 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100903 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300904 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200905 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200906 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300907 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400908 int nmsrs;
909 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800910 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400911#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300912 u64 msr_host_kernel_gs_base;
913 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400914#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100915
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100916 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100917 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100918
Gleb Natapov2961e8762013-11-25 15:37:13 +0200919 u32 vm_entry_controls_shadow;
920 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200921 u32 secondary_exec_control;
922
Nadav Har'Eld462b812011-05-24 15:26:10 +0300923 /*
924 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
925 * non-nested (L1) guest, it always points to vmcs01. For a nested
926 * guest (L2), it points to a different VMCS.
927 */
928 struct loaded_vmcs vmcs01;
929 struct loaded_vmcs *loaded_vmcs;
930 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300931 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400932 struct vmx_msrs guest;
933 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300934 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400935 struct {
936 int loaded;
937 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300938#ifdef CONFIG_X86_64
939 u16 ds_sel, es_sel;
940#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200941 int gs_ldt_reload_needed;
942 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000943 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400944 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200945 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300946 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300947 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300948 struct kvm_segment segs[8];
949 } rmode;
950 struct {
951 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300952 struct kvm_save_segment {
953 u16 selector;
954 unsigned long base;
955 u32 limit;
956 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300957 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300958 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800959 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300960 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200961
Andi Kleena0861c02009-06-08 17:37:09 +0800962 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800963
Yang Zhang01e439b2013-04-11 19:25:12 +0800964 /* Posted interrupt descriptor */
965 struct pi_desc pi_desc;
966
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300967 /* Support for a guest hypervisor (nested VMX) */
968 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200969
970 /* Dynamic PLE window. */
971 int ple_window;
972 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800973
974 /* Support for PML */
975#define PML_ENTITY_NUM 512
976 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800977
Yunhong Jiang64672c92016-06-13 14:19:59 -0700978 /* apic deadline value in host tsc */
979 u64 hv_deadline_tsc;
980
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800981 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800982
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800983 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800984
Wanpeng Li74c55932017-11-29 01:31:20 -0800985 unsigned long host_debugctlmsr;
986
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800987 /*
988 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
989 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
990 * in msr_ia32_feature_control_valid_bits.
991 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800992 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800993 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400994};
995
Avi Kivity2fb92db2011-04-27 19:42:18 +0300996enum segment_cache_field {
997 SEG_FIELD_SEL = 0,
998 SEG_FIELD_BASE = 1,
999 SEG_FIELD_LIMIT = 2,
1000 SEG_FIELD_AR = 3,
1001
1002 SEG_FIELD_NR = 4
1003};
1004
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001005static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1006{
1007 return container_of(kvm, struct kvm_vmx, kvm);
1008}
1009
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001010static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1011{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001012 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001013}
1014
Feng Wuefc64402015-09-18 22:29:51 +08001015static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1016{
1017 return &(to_vmx(vcpu)->pi_desc);
1018}
1019
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001020#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001021#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001022#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1023#define FIELD64(number, name) \
1024 FIELD(number, name), \
1025 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001026
Abel Gordon4607c2d2013-04-18 14:35:55 +03001027
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001028static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001029#define SHADOW_FIELD_RO(x) x,
1030#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001031};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001032static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001033 ARRAY_SIZE(shadow_read_only_fields);
1034
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001035static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001036#define SHADOW_FIELD_RW(x) x,
1037#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001038};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001039static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001040 ARRAY_SIZE(shadow_read_write_fields);
1041
Mathias Krause772e0312012-08-30 01:30:19 +02001042static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001043 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001044 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001045 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1046 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1047 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1048 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1049 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1050 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1051 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1052 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001053 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001054 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001055 FIELD(HOST_ES_SELECTOR, host_es_selector),
1056 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1057 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1058 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1059 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1060 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1061 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1062 FIELD64(IO_BITMAP_A, io_bitmap_a),
1063 FIELD64(IO_BITMAP_B, io_bitmap_b),
1064 FIELD64(MSR_BITMAP, msr_bitmap),
1065 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1066 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1067 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001068 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001069 FIELD64(TSC_OFFSET, tsc_offset),
1070 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1071 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001072 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001073 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001074 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001075 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1076 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1077 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1078 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001079 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001080 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1081 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001082 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001083 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1084 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1085 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1086 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1087 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1088 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1089 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1090 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1091 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1092 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001093 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001094 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1095 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1096 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1097 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1098 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1099 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1100 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1101 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1102 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1103 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1104 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1105 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1106 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1107 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1108 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1109 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1110 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1111 FIELD(TPR_THRESHOLD, tpr_threshold),
1112 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1113 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1114 FIELD(VM_EXIT_REASON, vm_exit_reason),
1115 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1116 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1117 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1118 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1119 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1120 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1121 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1122 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1123 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1124 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1125 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1126 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1127 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1128 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1129 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1130 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1131 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1132 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1133 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1134 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1135 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1136 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1137 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1138 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1139 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1140 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1141 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1142 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001143 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001144 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1145 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1146 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1147 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1148 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1149 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1150 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1151 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1152 FIELD(EXIT_QUALIFICATION, exit_qualification),
1153 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1154 FIELD(GUEST_CR0, guest_cr0),
1155 FIELD(GUEST_CR3, guest_cr3),
1156 FIELD(GUEST_CR4, guest_cr4),
1157 FIELD(GUEST_ES_BASE, guest_es_base),
1158 FIELD(GUEST_CS_BASE, guest_cs_base),
1159 FIELD(GUEST_SS_BASE, guest_ss_base),
1160 FIELD(GUEST_DS_BASE, guest_ds_base),
1161 FIELD(GUEST_FS_BASE, guest_fs_base),
1162 FIELD(GUEST_GS_BASE, guest_gs_base),
1163 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1164 FIELD(GUEST_TR_BASE, guest_tr_base),
1165 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1166 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1167 FIELD(GUEST_DR7, guest_dr7),
1168 FIELD(GUEST_RSP, guest_rsp),
1169 FIELD(GUEST_RIP, guest_rip),
1170 FIELD(GUEST_RFLAGS, guest_rflags),
1171 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1172 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1173 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1174 FIELD(HOST_CR0, host_cr0),
1175 FIELD(HOST_CR3, host_cr3),
1176 FIELD(HOST_CR4, host_cr4),
1177 FIELD(HOST_FS_BASE, host_fs_base),
1178 FIELD(HOST_GS_BASE, host_gs_base),
1179 FIELD(HOST_TR_BASE, host_tr_base),
1180 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1181 FIELD(HOST_IDTR_BASE, host_idtr_base),
1182 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1183 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1184 FIELD(HOST_RSP, host_rsp),
1185 FIELD(HOST_RIP, host_rip),
1186};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001187
1188static inline short vmcs_field_to_offset(unsigned long field)
1189{
Dan Williams085331d2018-01-31 17:47:03 -08001190 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1191 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001192 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001193
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001194 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001195 return -ENOENT;
1196
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001197 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001198 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001199 return -ENOENT;
1200
Linus Torvalds15303ba2018-02-10 13:16:35 -08001201 index = array_index_nospec(index, size);
1202 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001203 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001204 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001205 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001206}
1207
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001208static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1209{
David Matlack4f2777b2016-07-13 17:16:37 -07001210 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001211}
1212
Peter Feiner995f00a2017-06-30 17:26:32 -07001213static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001214static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001215static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001216static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001217static void vmx_set_segment(struct kvm_vcpu *vcpu,
1218 struct kvm_segment *var, int seg);
1219static void vmx_get_segment(struct kvm_vcpu *vcpu,
1220 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001221static bool guest_state_valid(struct kvm_vcpu *vcpu);
1222static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001223static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001224static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1225static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1226static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1227 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001228static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001229static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1230 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001231
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1233static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001234/*
1235 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1236 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1237 */
1238static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239
Feng Wubf9f6ac2015-09-18 22:29:55 +08001240/*
1241 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1242 * can find which vCPU should be waken up.
1243 */
1244static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1245static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1246
Radim Krčmář23611332016-09-29 22:41:33 +02001247enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001248 VMX_VMREAD_BITMAP,
1249 VMX_VMWRITE_BITMAP,
1250 VMX_BITMAP_NR
1251};
1252
1253static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1254
Radim Krčmář23611332016-09-29 22:41:33 +02001255#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1256#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001257
Avi Kivity110312c2010-12-21 12:54:20 +02001258static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001259static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001260
Sheng Yang2384d2b2008-01-17 15:14:33 +08001261static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1262static DEFINE_SPINLOCK(vmx_vpid_lock);
1263
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001264static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265 int size;
1266 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001267 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001269 u32 pin_based_exec_ctrl;
1270 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001271 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001272 u32 vmexit_ctrl;
1273 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001274 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001275} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276
Hannes Ederefff9e52008-11-28 17:02:06 +01001277static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001278 u32 ept;
1279 u32 vpid;
1280} vmx_capability;
1281
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282#define VMX_SEGMENT_FIELD(seg) \
1283 [VCPU_SREG_##seg] = { \
1284 .selector = GUEST_##seg##_SELECTOR, \
1285 .base = GUEST_##seg##_BASE, \
1286 .limit = GUEST_##seg##_LIMIT, \
1287 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1288 }
1289
Mathias Krause772e0312012-08-30 01:30:19 +02001290static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 unsigned selector;
1292 unsigned base;
1293 unsigned limit;
1294 unsigned ar_bytes;
1295} kvm_vmx_segment_fields[] = {
1296 VMX_SEGMENT_FIELD(CS),
1297 VMX_SEGMENT_FIELD(DS),
1298 VMX_SEGMENT_FIELD(ES),
1299 VMX_SEGMENT_FIELD(FS),
1300 VMX_SEGMENT_FIELD(GS),
1301 VMX_SEGMENT_FIELD(SS),
1302 VMX_SEGMENT_FIELD(TR),
1303 VMX_SEGMENT_FIELD(LDTR),
1304};
1305
Avi Kivity26bb0982009-09-07 11:14:12 +03001306static u64 host_efer;
1307
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001308static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1309
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001310/*
Brian Gerst8c065852010-07-17 09:03:26 -04001311 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001312 * away by decrementing the array size.
1313 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001315#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001316 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001318 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001321DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1322
1323#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1324
1325#define KVM_EVMCS_VERSION 1
1326
1327#if IS_ENABLED(CONFIG_HYPERV)
1328static bool __read_mostly enlightened_vmcs = true;
1329module_param(enlightened_vmcs, bool, 0444);
1330
1331static inline void evmcs_write64(unsigned long field, u64 value)
1332{
1333 u16 clean_field;
1334 int offset = get_evmcs_offset(field, &clean_field);
1335
1336 if (offset < 0)
1337 return;
1338
1339 *(u64 *)((char *)current_evmcs + offset) = value;
1340
1341 current_evmcs->hv_clean_fields &= ~clean_field;
1342}
1343
1344static inline void evmcs_write32(unsigned long field, u32 value)
1345{
1346 u16 clean_field;
1347 int offset = get_evmcs_offset(field, &clean_field);
1348
1349 if (offset < 0)
1350 return;
1351
1352 *(u32 *)((char *)current_evmcs + offset) = value;
1353 current_evmcs->hv_clean_fields &= ~clean_field;
1354}
1355
1356static inline void evmcs_write16(unsigned long field, u16 value)
1357{
1358 u16 clean_field;
1359 int offset = get_evmcs_offset(field, &clean_field);
1360
1361 if (offset < 0)
1362 return;
1363
1364 *(u16 *)((char *)current_evmcs + offset) = value;
1365 current_evmcs->hv_clean_fields &= ~clean_field;
1366}
1367
1368static inline u64 evmcs_read64(unsigned long field)
1369{
1370 int offset = get_evmcs_offset(field, NULL);
1371
1372 if (offset < 0)
1373 return 0;
1374
1375 return *(u64 *)((char *)current_evmcs + offset);
1376}
1377
1378static inline u32 evmcs_read32(unsigned long field)
1379{
1380 int offset = get_evmcs_offset(field, NULL);
1381
1382 if (offset < 0)
1383 return 0;
1384
1385 return *(u32 *)((char *)current_evmcs + offset);
1386}
1387
1388static inline u16 evmcs_read16(unsigned long field)
1389{
1390 int offset = get_evmcs_offset(field, NULL);
1391
1392 if (offset < 0)
1393 return 0;
1394
1395 return *(u16 *)((char *)current_evmcs + offset);
1396}
1397
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001398static inline void evmcs_touch_msr_bitmap(void)
1399{
1400 if (unlikely(!current_evmcs))
1401 return;
1402
1403 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1404 current_evmcs->hv_clean_fields &=
1405 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1406}
1407
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001408static void evmcs_load(u64 phys_addr)
1409{
1410 struct hv_vp_assist_page *vp_ap =
1411 hv_get_vp_assist_page(smp_processor_id());
1412
1413 vp_ap->current_nested_vmcs = phys_addr;
1414 vp_ap->enlighten_vmentry = 1;
1415}
1416
1417static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1418{
1419 /*
1420 * Enlightened VMCSv1 doesn't support these:
1421 *
1422 * POSTED_INTR_NV = 0x00000002,
1423 * GUEST_INTR_STATUS = 0x00000810,
1424 * APIC_ACCESS_ADDR = 0x00002014,
1425 * POSTED_INTR_DESC_ADDR = 0x00002016,
1426 * EOI_EXIT_BITMAP0 = 0x0000201c,
1427 * EOI_EXIT_BITMAP1 = 0x0000201e,
1428 * EOI_EXIT_BITMAP2 = 0x00002020,
1429 * EOI_EXIT_BITMAP3 = 0x00002022,
1430 */
1431 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1432 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1433 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1434 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1435 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1436 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1437 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1438
1439 /*
1440 * GUEST_PML_INDEX = 0x00000812,
1441 * PML_ADDRESS = 0x0000200e,
1442 */
1443 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1444
1445 /* VM_FUNCTION_CONTROL = 0x00002018, */
1446 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1447
1448 /*
1449 * EPTP_LIST_ADDRESS = 0x00002024,
1450 * VMREAD_BITMAP = 0x00002026,
1451 * VMWRITE_BITMAP = 0x00002028,
1452 */
1453 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1454
1455 /*
1456 * TSC_MULTIPLIER = 0x00002032,
1457 */
1458 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1459
1460 /*
1461 * PLE_GAP = 0x00004020,
1462 * PLE_WINDOW = 0x00004022,
1463 */
1464 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1465
1466 /*
1467 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1468 */
1469 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1470
1471 /*
1472 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1473 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1474 */
1475 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1476 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1477
1478 /*
1479 * Currently unsupported in KVM:
1480 * GUEST_IA32_RTIT_CTL = 0x00002814,
1481 */
1482}
1483#else /* !IS_ENABLED(CONFIG_HYPERV) */
1484static inline void evmcs_write64(unsigned long field, u64 value) {}
1485static inline void evmcs_write32(unsigned long field, u32 value) {}
1486static inline void evmcs_write16(unsigned long field, u16 value) {}
1487static inline u64 evmcs_read64(unsigned long field) { return 0; }
1488static inline u32 evmcs_read32(unsigned long field) { return 0; }
1489static inline u16 evmcs_read16(unsigned long field) { return 0; }
1490static inline void evmcs_load(u64 phys_addr) {}
1491static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001492static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001493#endif /* IS_ENABLED(CONFIG_HYPERV) */
1494
Jan Kiszka5bb16012016-02-09 20:14:21 +01001495static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496{
1497 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1498 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001499 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1500}
1501
Jan Kiszka6f054852016-02-09 20:15:18 +01001502static inline bool is_debug(u32 intr_info)
1503{
1504 return is_exception_n(intr_info, DB_VECTOR);
1505}
1506
1507static inline bool is_breakpoint(u32 intr_info)
1508{
1509 return is_exception_n(intr_info, BP_VECTOR);
1510}
1511
Jan Kiszka5bb16012016-02-09 20:14:21 +01001512static inline bool is_page_fault(u32 intr_info)
1513{
1514 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515}
1516
Gui Jianfeng31299942010-03-15 17:29:09 +08001517static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001518{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001519 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001520}
1521
Gui Jianfeng31299942010-03-15 17:29:09 +08001522static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001523{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001524 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001525}
1526
Liran Alon9e869482018-03-12 13:12:51 +02001527static inline bool is_gp_fault(u32 intr_info)
1528{
1529 return is_exception_n(intr_info, GP_VECTOR);
1530}
1531
Gui Jianfeng31299942010-03-15 17:29:09 +08001532static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533{
1534 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1535 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1536}
1537
Gui Jianfeng31299942010-03-15 17:29:09 +08001538static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001539{
1540 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1541 INTR_INFO_VALID_MASK)) ==
1542 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1543}
1544
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001545/* Undocumented: icebp/int1 */
1546static inline bool is_icebp(u32 intr_info)
1547{
1548 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1549 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1550}
1551
Gui Jianfeng31299942010-03-15 17:29:09 +08001552static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001553{
Sheng Yang04547152009-04-01 15:52:31 +08001554 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001555}
1556
Gui Jianfeng31299942010-03-15 17:29:09 +08001557static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001558{
Sheng Yang04547152009-04-01 15:52:31 +08001559 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001560}
1561
Paolo Bonzini35754c92015-07-29 12:05:37 +02001562static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001563{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001564 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001565}
1566
Gui Jianfeng31299942010-03-15 17:29:09 +08001567static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001568{
Sheng Yang04547152009-04-01 15:52:31 +08001569 return vmcs_config.cpu_based_exec_ctrl &
1570 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001571}
1572
Avi Kivity774ead32007-12-26 13:57:04 +02001573static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001574{
Sheng Yang04547152009-04-01 15:52:31 +08001575 return vmcs_config.cpu_based_2nd_exec_ctrl &
1576 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1577}
1578
Yang Zhang8d146952013-01-25 10:18:50 +08001579static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1580{
1581 return vmcs_config.cpu_based_2nd_exec_ctrl &
1582 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1583}
1584
Yang Zhang83d4c282013-01-25 10:18:49 +08001585static inline bool cpu_has_vmx_apic_register_virt(void)
1586{
1587 return vmcs_config.cpu_based_2nd_exec_ctrl &
1588 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1589}
1590
Yang Zhangc7c9c562013-01-25 10:18:51 +08001591static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1592{
1593 return vmcs_config.cpu_based_2nd_exec_ctrl &
1594 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1595}
1596
Yunhong Jiang64672c92016-06-13 14:19:59 -07001597/*
1598 * Comment's format: document - errata name - stepping - processor name.
1599 * Refer from
1600 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1601 */
1602static u32 vmx_preemption_cpu_tfms[] = {
1603/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
16040x000206E6,
1605/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1606/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1607/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
16080x00020652,
1609/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
16100x00020655,
1611/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1612/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1613/*
1614 * 320767.pdf - AAP86 - B1 -
1615 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1616 */
16170x000106E5,
1618/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
16190x000106A0,
1620/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
16210x000106A1,
1622/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
16230x000106A4,
1624 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1625 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1626 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
16270x000106A5,
1628};
1629
1630static inline bool cpu_has_broken_vmx_preemption_timer(void)
1631{
1632 u32 eax = cpuid_eax(0x00000001), i;
1633
1634 /* Clear the reserved bits */
1635 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001636 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001637 if (eax == vmx_preemption_cpu_tfms[i])
1638 return true;
1639
1640 return false;
1641}
1642
1643static inline bool cpu_has_vmx_preemption_timer(void)
1644{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001645 return vmcs_config.pin_based_exec_ctrl &
1646 PIN_BASED_VMX_PREEMPTION_TIMER;
1647}
1648
Yang Zhang01e439b2013-04-11 19:25:12 +08001649static inline bool cpu_has_vmx_posted_intr(void)
1650{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001651 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1652 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001653}
1654
1655static inline bool cpu_has_vmx_apicv(void)
1656{
1657 return cpu_has_vmx_apic_register_virt() &&
1658 cpu_has_vmx_virtual_intr_delivery() &&
1659 cpu_has_vmx_posted_intr();
1660}
1661
Sheng Yang04547152009-04-01 15:52:31 +08001662static inline bool cpu_has_vmx_flexpriority(void)
1663{
1664 return cpu_has_vmx_tpr_shadow() &&
1665 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001666}
1667
Marcelo Tosattie7997942009-06-11 12:07:40 -03001668static inline bool cpu_has_vmx_ept_execute_only(void)
1669{
Gui Jianfeng31299942010-03-15 17:29:09 +08001670 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001671}
1672
Marcelo Tosattie7997942009-06-11 12:07:40 -03001673static inline bool cpu_has_vmx_ept_2m_page(void)
1674{
Gui Jianfeng31299942010-03-15 17:29:09 +08001675 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001676}
1677
Sheng Yang878403b2010-01-05 19:02:29 +08001678static inline bool cpu_has_vmx_ept_1g_page(void)
1679{
Gui Jianfeng31299942010-03-15 17:29:09 +08001680 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001681}
1682
Sheng Yang4bc9b982010-06-02 14:05:24 +08001683static inline bool cpu_has_vmx_ept_4levels(void)
1684{
1685 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1686}
1687
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001688static inline bool cpu_has_vmx_ept_mt_wb(void)
1689{
1690 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1691}
1692
Yu Zhang855feb62017-08-24 20:27:55 +08001693static inline bool cpu_has_vmx_ept_5levels(void)
1694{
1695 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1696}
1697
Xudong Hao83c3a332012-05-28 19:33:35 +08001698static inline bool cpu_has_vmx_ept_ad_bits(void)
1699{
1700 return vmx_capability.ept & VMX_EPT_AD_BIT;
1701}
1702
Gui Jianfeng31299942010-03-15 17:29:09 +08001703static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001704{
Gui Jianfeng31299942010-03-15 17:29:09 +08001705 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001706}
1707
Gui Jianfeng31299942010-03-15 17:29:09 +08001708static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001709{
Gui Jianfeng31299942010-03-15 17:29:09 +08001710 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001711}
1712
Liran Aloncd9a4912018-05-22 17:16:15 +03001713static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1714{
1715 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1716}
1717
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001718static inline bool cpu_has_vmx_invvpid_single(void)
1719{
1720 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1721}
1722
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001723static inline bool cpu_has_vmx_invvpid_global(void)
1724{
1725 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1726}
1727
Wanpeng Li08d839c2017-03-23 05:30:08 -07001728static inline bool cpu_has_vmx_invvpid(void)
1729{
1730 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1731}
1732
Gui Jianfeng31299942010-03-15 17:29:09 +08001733static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001734{
Sheng Yang04547152009-04-01 15:52:31 +08001735 return vmcs_config.cpu_based_2nd_exec_ctrl &
1736 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001737}
1738
Gui Jianfeng31299942010-03-15 17:29:09 +08001739static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001740{
1741 return vmcs_config.cpu_based_2nd_exec_ctrl &
1742 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1743}
1744
Gui Jianfeng31299942010-03-15 17:29:09 +08001745static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001746{
1747 return vmcs_config.cpu_based_2nd_exec_ctrl &
1748 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1749}
1750
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001751static inline bool cpu_has_vmx_basic_inout(void)
1752{
1753 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1754}
1755
Paolo Bonzini35754c92015-07-29 12:05:37 +02001756static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001757{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001758 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001759}
1760
Gui Jianfeng31299942010-03-15 17:29:09 +08001761static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001762{
Sheng Yang04547152009-04-01 15:52:31 +08001763 return vmcs_config.cpu_based_2nd_exec_ctrl &
1764 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001765}
1766
Gui Jianfeng31299942010-03-15 17:29:09 +08001767static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001768{
1769 return vmcs_config.cpu_based_2nd_exec_ctrl &
1770 SECONDARY_EXEC_RDTSCP;
1771}
1772
Mao, Junjiead756a12012-07-02 01:18:48 +00001773static inline bool cpu_has_vmx_invpcid(void)
1774{
1775 return vmcs_config.cpu_based_2nd_exec_ctrl &
1776 SECONDARY_EXEC_ENABLE_INVPCID;
1777}
1778
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001779static inline bool cpu_has_virtual_nmis(void)
1780{
1781 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1782}
1783
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001784static inline bool cpu_has_vmx_wbinvd_exit(void)
1785{
1786 return vmcs_config.cpu_based_2nd_exec_ctrl &
1787 SECONDARY_EXEC_WBINVD_EXITING;
1788}
1789
Abel Gordonabc4fc52013-04-18 14:35:25 +03001790static inline bool cpu_has_vmx_shadow_vmcs(void)
1791{
1792 u64 vmx_msr;
1793 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1794 /* check if the cpu supports writing r/o exit information fields */
1795 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1796 return false;
1797
1798 return vmcs_config.cpu_based_2nd_exec_ctrl &
1799 SECONDARY_EXEC_SHADOW_VMCS;
1800}
1801
Kai Huang843e4332015-01-28 10:54:28 +08001802static inline bool cpu_has_vmx_pml(void)
1803{
1804 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1805}
1806
Haozhong Zhang64903d62015-10-20 15:39:09 +08001807static inline bool cpu_has_vmx_tsc_scaling(void)
1808{
1809 return vmcs_config.cpu_based_2nd_exec_ctrl &
1810 SECONDARY_EXEC_TSC_SCALING;
1811}
1812
Bandan Das2a499e42017-08-03 15:54:41 -04001813static inline bool cpu_has_vmx_vmfunc(void)
1814{
1815 return vmcs_config.cpu_based_2nd_exec_ctrl &
1816 SECONDARY_EXEC_ENABLE_VMFUNC;
1817}
1818
Sean Christopherson64f7a112018-04-30 10:01:06 -07001819static bool vmx_umip_emulated(void)
1820{
1821 return vmcs_config.cpu_based_2nd_exec_ctrl &
1822 SECONDARY_EXEC_DESC;
1823}
1824
Sheng Yang04547152009-04-01 15:52:31 +08001825static inline bool report_flexpriority(void)
1826{
1827 return flexpriority_enabled;
1828}
1829
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001830static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1831{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001832 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001833}
1834
Jim Mattsonf4160e42018-05-29 09:11:33 -07001835/*
1836 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1837 * to modify any valid field of the VMCS, or are the VM-exit
1838 * information fields read-only?
1839 */
1840static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1841{
1842 return to_vmx(vcpu)->nested.msrs.misc_low &
1843 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1844}
1845
Marc Orr04473782018-06-20 17:21:29 -07001846static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1847{
1848 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1849}
1850
1851static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1852{
1853 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1854 CPU_BASED_MONITOR_TRAP_FLAG;
1855}
1856
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001857static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1858{
1859 return vmcs12->cpu_based_vm_exec_control & bit;
1860}
1861
1862static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1863{
1864 return (vmcs12->cpu_based_vm_exec_control &
1865 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1866 (vmcs12->secondary_vm_exec_control & bit);
1867}
1868
Jan Kiszkaf4124502014-03-07 20:03:13 +01001869static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1870{
1871 return vmcs12->pin_based_vm_exec_control &
1872 PIN_BASED_VMX_PREEMPTION_TIMER;
1873}
1874
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001875static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1876{
1877 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1878}
1879
1880static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1881{
1882 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1883}
1884
Nadav Har'El155a97a2013-08-05 11:07:16 +03001885static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1886{
1887 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1888}
1889
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001890static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1891{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001892 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001893}
1894
Bandan Dasc5f983f2017-05-05 15:25:14 -04001895static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1896{
1897 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1898}
1899
Wincy Vanf2b93282015-02-03 23:56:03 +08001900static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1901{
1902 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1903}
1904
Wanpeng Li5c614b32015-10-13 09:18:36 -07001905static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1906{
1907 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1908}
1909
Wincy Van82f0dd42015-02-03 23:57:18 +08001910static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1911{
1912 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1913}
1914
Wincy Van608406e2015-02-03 23:57:51 +08001915static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1916{
1917 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1918}
1919
Wincy Van705699a2015-02-03 23:58:17 +08001920static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1921{
1922 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1923}
1924
Bandan Das27c42a12017-08-03 15:54:42 -04001925static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1926{
1927 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1928}
1929
Bandan Das41ab9372017-08-03 15:54:43 -04001930static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1931{
1932 return nested_cpu_has_vmfunc(vmcs12) &&
1933 (vmcs12->vm_function_control &
1934 VMX_VMFUNC_EPTP_SWITCHING);
1935}
1936
Jim Mattsonef85b672016-12-12 11:01:37 -08001937static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001938{
1939 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001940 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001941}
1942
Jan Kiszka533558b2014-01-04 18:47:20 +01001943static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1944 u32 exit_intr_info,
1945 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001946static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1947 struct vmcs12 *vmcs12,
1948 u32 reason, unsigned long qualification);
1949
Rusty Russell8b9cf982007-07-30 16:31:43 +10001950static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001951{
1952 int i;
1953
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001954 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001955 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001956 return i;
1957 return -1;
1958}
1959
Sheng Yang2384d2b2008-01-17 15:14:33 +08001960static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1961{
1962 struct {
1963 u64 vpid : 16;
1964 u64 rsvd : 48;
1965 u64 gva;
1966 } operand = { vpid, 0, gva };
1967
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001968 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001969 /* CF==1 or ZF==1 --> rc = -1 */
1970 "; ja 1f ; ud2 ; 1:"
1971 : : "a"(&operand), "c"(ext) : "cc", "memory");
1972}
1973
Sheng Yang14394422008-04-28 12:24:45 +08001974static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1975{
1976 struct {
1977 u64 eptp, gpa;
1978 } operand = {eptp, gpa};
1979
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001980 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001981 /* CF==1 or ZF==1 --> rc = -1 */
1982 "; ja 1f ; ud2 ; 1:\n"
1983 : : "a" (&operand), "c" (ext) : "cc", "memory");
1984}
1985
Avi Kivity26bb0982009-09-07 11:14:12 +03001986static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001987{
1988 int i;
1989
Rusty Russell8b9cf982007-07-30 16:31:43 +10001990 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001991 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001992 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001993 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001994}
1995
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996static void vmcs_clear(struct vmcs *vmcs)
1997{
1998 u64 phys_addr = __pa(vmcs);
1999 u8 error;
2000
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002001 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02002002 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 : "cc", "memory");
2004 if (error)
2005 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2006 vmcs, phys_addr);
2007}
2008
Nadav Har'Eld462b812011-05-24 15:26:10 +03002009static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2010{
2011 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002012 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2013 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002014 loaded_vmcs->cpu = -1;
2015 loaded_vmcs->launched = 0;
2016}
2017
Dongxiao Xu7725b892010-05-11 18:29:38 +08002018static void vmcs_load(struct vmcs *vmcs)
2019{
2020 u64 phys_addr = __pa(vmcs);
2021 u8 error;
2022
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002023 if (static_branch_unlikely(&enable_evmcs))
2024 return evmcs_load(phys_addr);
2025
Dongxiao Xu7725b892010-05-11 18:29:38 +08002026 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02002027 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002028 : "cc", "memory");
2029 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03002030 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002031 vmcs, phys_addr);
2032}
2033
Dave Young2965faa2015-09-09 15:38:55 -07002034#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002035/*
2036 * This bitmap is used to indicate whether the vmclear
2037 * operation is enabled on all cpus. All disabled by
2038 * default.
2039 */
2040static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2041
2042static inline void crash_enable_local_vmclear(int cpu)
2043{
2044 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2045}
2046
2047static inline void crash_disable_local_vmclear(int cpu)
2048{
2049 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2050}
2051
2052static inline int crash_local_vmclear_enabled(int cpu)
2053{
2054 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2055}
2056
2057static void crash_vmclear_local_loaded_vmcss(void)
2058{
2059 int cpu = raw_smp_processor_id();
2060 struct loaded_vmcs *v;
2061
2062 if (!crash_local_vmclear_enabled(cpu))
2063 return;
2064
2065 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2066 loaded_vmcss_on_cpu_link)
2067 vmcs_clear(v->vmcs);
2068}
2069#else
2070static inline void crash_enable_local_vmclear(int cpu) { }
2071static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002072#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002073
Nadav Har'Eld462b812011-05-24 15:26:10 +03002074static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002076 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002077 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
Nadav Har'Eld462b812011-05-24 15:26:10 +03002079 if (loaded_vmcs->cpu != cpu)
2080 return; /* vcpu migration can race with cpu offline */
2081 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002083 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002084 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002085
2086 /*
2087 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2088 * is before setting loaded_vmcs->vcpu to -1 which is done in
2089 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2090 * then adds the vmcs into percpu list before it is deleted.
2091 */
2092 smp_wmb();
2093
Nadav Har'Eld462b812011-05-24 15:26:10 +03002094 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002095 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096}
2097
Nadav Har'Eld462b812011-05-24 15:26:10 +03002098static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002099{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002100 int cpu = loaded_vmcs->cpu;
2101
2102 if (cpu != -1)
2103 smp_call_function_single(cpu,
2104 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002105}
2106
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002107static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002108{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002109 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002110 return;
2111
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002112 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002113 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002114}
2115
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002116static inline void vpid_sync_vcpu_global(void)
2117{
2118 if (cpu_has_vmx_invvpid_global())
2119 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2120}
2121
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002122static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002123{
2124 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002125 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002126 else
2127 vpid_sync_vcpu_global();
2128}
2129
Sheng Yang14394422008-04-28 12:24:45 +08002130static inline void ept_sync_global(void)
2131{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002132 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002133}
2134
2135static inline void ept_sync_context(u64 eptp)
2136{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002137 if (cpu_has_vmx_invept_context())
2138 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2139 else
2140 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002141}
2142
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002143static __always_inline void vmcs_check16(unsigned long field)
2144{
2145 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2146 "16-bit accessor invalid for 64-bit field");
2147 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2148 "16-bit accessor invalid for 64-bit high field");
2149 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2150 "16-bit accessor invalid for 32-bit high field");
2151 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2152 "16-bit accessor invalid for natural width field");
2153}
2154
2155static __always_inline void vmcs_check32(unsigned long field)
2156{
2157 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2158 "32-bit accessor invalid for 16-bit field");
2159 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2160 "32-bit accessor invalid for natural width field");
2161}
2162
2163static __always_inline void vmcs_check64(unsigned long field)
2164{
2165 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2166 "64-bit accessor invalid for 16-bit field");
2167 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2168 "64-bit accessor invalid for 64-bit high field");
2169 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2170 "64-bit accessor invalid for 32-bit field");
2171 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2172 "64-bit accessor invalid for natural width field");
2173}
2174
2175static __always_inline void vmcs_checkl(unsigned long field)
2176{
2177 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2178 "Natural width accessor invalid for 16-bit field");
2179 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2180 "Natural width accessor invalid for 64-bit field");
2181 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2182 "Natural width accessor invalid for 64-bit high field");
2183 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2184 "Natural width accessor invalid for 32-bit field");
2185}
2186
2187static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188{
Avi Kivity5e520e62011-05-15 10:13:12 -04002189 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190
Avi Kivity5e520e62011-05-15 10:13:12 -04002191 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2192 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 return value;
2194}
2195
Avi Kivity96304212011-05-15 10:13:13 -04002196static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002198 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002199 if (static_branch_unlikely(&enable_evmcs))
2200 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002201 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202}
2203
Avi Kivity96304212011-05-15 10:13:13 -04002204static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002206 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002207 if (static_branch_unlikely(&enable_evmcs))
2208 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002209 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210}
2211
Avi Kivity96304212011-05-15 10:13:13 -04002212static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002214 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002215 if (static_branch_unlikely(&enable_evmcs))
2216 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002217#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002218 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002220 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221#endif
2222}
2223
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002224static __always_inline unsigned long vmcs_readl(unsigned long field)
2225{
2226 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002227 if (static_branch_unlikely(&enable_evmcs))
2228 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002229 return __vmcs_readl(field);
2230}
2231
Avi Kivitye52de1b2007-01-05 16:36:56 -08002232static noinline void vmwrite_error(unsigned long field, unsigned long value)
2233{
2234 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2235 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2236 dump_stack();
2237}
2238
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002239static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240{
2241 u8 error;
2242
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002243 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002244 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002245 if (unlikely(error))
2246 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247}
2248
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002249static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002251 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002252 if (static_branch_unlikely(&enable_evmcs))
2253 return evmcs_write16(field, value);
2254
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002255 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256}
2257
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002258static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002260 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002261 if (static_branch_unlikely(&enable_evmcs))
2262 return evmcs_write32(field, value);
2263
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002264 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265}
2266
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002267static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002269 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002270 if (static_branch_unlikely(&enable_evmcs))
2271 return evmcs_write64(field, value);
2272
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002273 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002274#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002276 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277#endif
2278}
2279
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002280static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002281{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002282 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002283 if (static_branch_unlikely(&enable_evmcs))
2284 return evmcs_write64(field, value);
2285
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002286 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002287}
2288
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002289static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002290{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002291 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2292 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002293 if (static_branch_unlikely(&enable_evmcs))
2294 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2295
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002296 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2297}
2298
2299static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2300{
2301 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2302 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002303 if (static_branch_unlikely(&enable_evmcs))
2304 return evmcs_write32(field, evmcs_read32(field) | mask);
2305
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002306 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002307}
2308
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002309static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2310{
2311 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2312}
2313
Gleb Natapov2961e8762013-11-25 15:37:13 +02002314static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2315{
2316 vmcs_write32(VM_ENTRY_CONTROLS, val);
2317 vmx->vm_entry_controls_shadow = val;
2318}
2319
2320static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2321{
2322 if (vmx->vm_entry_controls_shadow != val)
2323 vm_entry_controls_init(vmx, val);
2324}
2325
2326static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2327{
2328 return vmx->vm_entry_controls_shadow;
2329}
2330
2331
2332static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2333{
2334 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2335}
2336
2337static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2338{
2339 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2340}
2341
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002342static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2343{
2344 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2345}
2346
Gleb Natapov2961e8762013-11-25 15:37:13 +02002347static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2348{
2349 vmcs_write32(VM_EXIT_CONTROLS, val);
2350 vmx->vm_exit_controls_shadow = val;
2351}
2352
2353static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2354{
2355 if (vmx->vm_exit_controls_shadow != val)
2356 vm_exit_controls_init(vmx, val);
2357}
2358
2359static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2360{
2361 return vmx->vm_exit_controls_shadow;
2362}
2363
2364
2365static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2366{
2367 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2368}
2369
2370static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2371{
2372 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2373}
2374
Avi Kivity2fb92db2011-04-27 19:42:18 +03002375static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2376{
2377 vmx->segment_cache.bitmask = 0;
2378}
2379
2380static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2381 unsigned field)
2382{
2383 bool ret;
2384 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2385
2386 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2387 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2388 vmx->segment_cache.bitmask = 0;
2389 }
2390 ret = vmx->segment_cache.bitmask & mask;
2391 vmx->segment_cache.bitmask |= mask;
2392 return ret;
2393}
2394
2395static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2396{
2397 u16 *p = &vmx->segment_cache.seg[seg].selector;
2398
2399 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2400 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2401 return *p;
2402}
2403
2404static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2405{
2406 ulong *p = &vmx->segment_cache.seg[seg].base;
2407
2408 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2409 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2410 return *p;
2411}
2412
2413static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2414{
2415 u32 *p = &vmx->segment_cache.seg[seg].limit;
2416
2417 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2418 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2419 return *p;
2420}
2421
2422static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2423{
2424 u32 *p = &vmx->segment_cache.seg[seg].ar;
2425
2426 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2427 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2428 return *p;
2429}
2430
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002431static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2432{
2433 u32 eb;
2434
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002435 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002436 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002437 /*
2438 * Guest access to VMware backdoor ports could legitimately
2439 * trigger #GP because of TSS I/O permission bitmap.
2440 * We intercept those #GP and allow access to them anyway
2441 * as VMware does.
2442 */
2443 if (enable_vmware_backdoor)
2444 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002445 if ((vcpu->guest_debug &
2446 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2447 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2448 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002449 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002450 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002451 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002452 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002453
2454 /* When we are running a nested L2 guest and L1 specified for it a
2455 * certain exception bitmap, we must trap the same exceptions and pass
2456 * them to L1. When running L2, we will only handle the exceptions
2457 * specified above if L1 did not want them.
2458 */
2459 if (is_guest_mode(vcpu))
2460 eb |= get_vmcs12(vcpu)->exception_bitmap;
2461
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002462 vmcs_write32(EXCEPTION_BITMAP, eb);
2463}
2464
Ashok Raj15d45072018-02-01 22:59:43 +01002465/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002466 * Check if MSR is intercepted for currently loaded MSR bitmap.
2467 */
2468static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2469{
2470 unsigned long *msr_bitmap;
2471 int f = sizeof(unsigned long);
2472
2473 if (!cpu_has_vmx_msr_bitmap())
2474 return true;
2475
2476 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2477
2478 if (msr <= 0x1fff) {
2479 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2480 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2481 msr &= 0x1fff;
2482 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2483 }
2484
2485 return true;
2486}
2487
2488/*
Ashok Raj15d45072018-02-01 22:59:43 +01002489 * Check if MSR is intercepted for L01 MSR bitmap.
2490 */
2491static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2492{
2493 unsigned long *msr_bitmap;
2494 int f = sizeof(unsigned long);
2495
2496 if (!cpu_has_vmx_msr_bitmap())
2497 return true;
2498
2499 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2500
2501 if (msr <= 0x1fff) {
2502 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2503 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2504 msr &= 0x1fff;
2505 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2506 }
2507
2508 return true;
2509}
2510
Gleb Natapov2961e8762013-11-25 15:37:13 +02002511static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2512 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002513{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002514 vm_entry_controls_clearbit(vmx, entry);
2515 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002516}
2517
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002518static int find_msr(struct vmx_msrs *m, unsigned int msr)
2519{
2520 unsigned int i;
2521
2522 for (i = 0; i < m->nr; ++i) {
2523 if (m->val[i].index == msr)
2524 return i;
2525 }
2526 return -ENOENT;
2527}
2528
Avi Kivity61d2ef22010-04-28 16:40:38 +03002529static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2530{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002531 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002532 struct msr_autoload *m = &vmx->msr_autoload;
2533
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002534 switch (msr) {
2535 case MSR_EFER:
2536 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002537 clear_atomic_switch_msr_special(vmx,
2538 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002539 VM_EXIT_LOAD_IA32_EFER);
2540 return;
2541 }
2542 break;
2543 case MSR_CORE_PERF_GLOBAL_CTRL:
2544 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002545 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002546 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2547 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2548 return;
2549 }
2550 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002551 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002552 i = find_msr(&m->guest, msr);
2553 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002554 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002555 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002556 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002557 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002558
2559skip_guest:
2560 i = find_msr(&m->host, msr);
2561 if (i < 0)
2562 return;
2563
2564 --m->host.nr;
2565 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002566 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002567}
2568
Gleb Natapov2961e8762013-11-25 15:37:13 +02002569static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2570 unsigned long entry, unsigned long exit,
2571 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2572 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002573{
2574 vmcs_write64(guest_val_vmcs, guest_val);
2575 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002576 vm_entry_controls_setbit(vmx, entry);
2577 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002578}
2579
Avi Kivity61d2ef22010-04-28 16:40:38 +03002580static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002581 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002582{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002583 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002584 struct msr_autoload *m = &vmx->msr_autoload;
2585
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002586 switch (msr) {
2587 case MSR_EFER:
2588 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002589 add_atomic_switch_msr_special(vmx,
2590 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002591 VM_EXIT_LOAD_IA32_EFER,
2592 GUEST_IA32_EFER,
2593 HOST_IA32_EFER,
2594 guest_val, host_val);
2595 return;
2596 }
2597 break;
2598 case MSR_CORE_PERF_GLOBAL_CTRL:
2599 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002600 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002601 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2602 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2603 GUEST_IA32_PERF_GLOBAL_CTRL,
2604 HOST_IA32_PERF_GLOBAL_CTRL,
2605 guest_val, host_val);
2606 return;
2607 }
2608 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002609 case MSR_IA32_PEBS_ENABLE:
2610 /* PEBS needs a quiescent period after being disabled (to write
2611 * a record). Disabling PEBS through VMX MSR swapping doesn't
2612 * provide that period, so a CPU could write host's record into
2613 * guest's memory.
2614 */
2615 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002616 }
2617
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002618 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002619 if (!entry_only)
2620 j = find_msr(&m->host, msr);
2621
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002622 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002623 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002624 "Can't add msr %x\n", msr);
2625 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002626 }
2627 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002628 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002629 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002630 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002631 m->guest.val[i].index = msr;
2632 m->guest.val[i].value = guest_val;
2633
2634 if (entry_only)
2635 return;
2636
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002637 if (j < 0) {
2638 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002639 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002640 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002641 m->host.val[j].index = msr;
2642 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002643}
2644
Avi Kivity92c0d902009-10-29 11:00:16 +02002645static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002646{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002647 u64 guest_efer = vmx->vcpu.arch.efer;
2648 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002649
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002650 if (!enable_ept) {
2651 /*
2652 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2653 * host CPUID is more efficient than testing guest CPUID
2654 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2655 */
2656 if (boot_cpu_has(X86_FEATURE_SMEP))
2657 guest_efer |= EFER_NX;
2658 else if (!(guest_efer & EFER_NX))
2659 ignore_bits |= EFER_NX;
2660 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002661
Avi Kivity51c6cf62007-08-29 03:48:05 +03002662 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002663 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002664 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002665 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002666#ifdef CONFIG_X86_64
2667 ignore_bits |= EFER_LMA | EFER_LME;
2668 /* SCE is meaningful only in long mode on Intel */
2669 if (guest_efer & EFER_LMA)
2670 ignore_bits &= ~(u64)EFER_SCE;
2671#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002672
2673 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002674
2675 /*
2676 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2677 * On CPUs that support "load IA32_EFER", always switch EFER
2678 * atomically, since it's faster than switching it manually.
2679 */
2680 if (cpu_has_load_ia32_efer ||
2681 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002682 if (!(guest_efer & EFER_LMA))
2683 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002684 if (guest_efer != host_efer)
2685 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002686 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002687 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002688 } else {
2689 guest_efer &= ~ignore_bits;
2690 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002691
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002692 vmx->guest_msrs[efer_offset].data = guest_efer;
2693 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2694
2695 return true;
2696 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002697}
2698
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002699#ifdef CONFIG_X86_32
2700/*
2701 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2702 * VMCS rather than the segment table. KVM uses this helper to figure
2703 * out the current bases to poke them into the VMCS before entry.
2704 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002705static unsigned long segment_base(u16 selector)
2706{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002707 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002708 unsigned long v;
2709
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002710 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002711 return 0;
2712
Thomas Garnier45fc8752017-03-14 10:05:08 -07002713 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002714
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002715 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002716 u16 ldt_selector = kvm_read_ldt();
2717
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002718 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002719 return 0;
2720
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002721 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002722 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002723 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002724 return v;
2725}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002726#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002727
Avi Kivity04d2cc72007-09-10 18:10:54 +03002728static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002729{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002730 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002731#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002732 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002733 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002734#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002735 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002736
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002737 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002738 return;
2739
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002740 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002741 /*
2742 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2743 * allow segment selectors with cpl > 0 or ti == 1.
2744 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002745 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002746 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002747
2748#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002749 if (likely(is_64bit_mm(current->mm))) {
2750 save_fsgs_for_kvm();
2751 vmx->host_state.fs_sel = current->thread.fsindex;
2752 vmx->host_state.gs_sel = current->thread.gsindex;
2753 fs_base = current->thread.fsbase;
2754 kernel_gs_base = current->thread.gsbase;
2755 } else {
2756#endif
2757 savesegment(fs, vmx->host_state.fs_sel);
2758 savesegment(gs, vmx->host_state.gs_sel);
2759#ifdef CONFIG_X86_64
2760 fs_base = read_msr(MSR_FS_BASE);
2761 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2762 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002763#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002764 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002765 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002766 vmx->host_state.fs_reload_needed = 0;
2767 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002768 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002769 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002770 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002771 if (!(vmx->host_state.gs_sel & 7))
2772 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002773 else {
2774 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002775 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002776 }
2777
2778#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002779 savesegment(ds, vmx->host_state.ds_sel);
2780 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002781
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002782 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002783 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002784
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002785 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002786 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002787 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002788#else
2789 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2790 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2791#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002792 if (boot_cpu_has(X86_FEATURE_MPX))
2793 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002794 for (i = 0; i < vmx->save_nmsrs; ++i)
2795 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002796 vmx->guest_msrs[i].data,
2797 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002798}
2799
Avi Kivitya9b21b62008-06-24 11:48:49 +03002800static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002801{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002802 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002803 return;
2804
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002805 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002806 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002807#ifdef CONFIG_X86_64
2808 if (is_long_mode(&vmx->vcpu))
2809 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2810#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002811 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002812 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002813#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002814 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002815#else
2816 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002817#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002818 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002819 if (vmx->host_state.fs_reload_needed)
2820 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002821#ifdef CONFIG_X86_64
2822 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2823 loadsegment(ds, vmx->host_state.ds_sel);
2824 loadsegment(es, vmx->host_state.es_sel);
2825 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002826#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002827 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002828#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002829 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002830#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002831 if (vmx->host_state.msr_host_bndcfgs)
2832 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002833 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002834}
2835
Avi Kivitya9b21b62008-06-24 11:48:49 +03002836static void vmx_load_host_state(struct vcpu_vmx *vmx)
2837{
2838 preempt_disable();
2839 __vmx_load_host_state(vmx);
2840 preempt_enable();
2841}
2842
Feng Wu28b835d2015-09-18 22:29:54 +08002843static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2844{
2845 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2846 struct pi_desc old, new;
2847 unsigned int dest;
2848
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002849 /*
2850 * In case of hot-plug or hot-unplug, we may have to undo
2851 * vmx_vcpu_pi_put even if there is no assigned device. And we
2852 * always keep PI.NDST up to date for simplicity: it makes the
2853 * code easier, and CPU migration is not a fast path.
2854 */
2855 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002856 return;
2857
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002858 /*
2859 * First handle the simple case where no cmpxchg is necessary; just
2860 * allow posting non-urgent interrupts.
2861 *
2862 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2863 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2864 * expects the VCPU to be on the blocked_vcpu_list that matches
2865 * PI.NDST.
2866 */
2867 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2868 vcpu->cpu == cpu) {
2869 pi_clear_sn(pi_desc);
2870 return;
2871 }
2872
2873 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002874 do {
2875 old.control = new.control = pi_desc->control;
2876
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002877 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002878
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002879 if (x2apic_enabled())
2880 new.ndst = dest;
2881 else
2882 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002883
Feng Wu28b835d2015-09-18 22:29:54 +08002884 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002885 } while (cmpxchg64(&pi_desc->control, old.control,
2886 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002887}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002888
Peter Feinerc95ba922016-08-17 09:36:47 -07002889static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2890{
2891 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2892 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2893}
2894
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895/*
2896 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2897 * vcpu mutex is already taken.
2898 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002899static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002901 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002902 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002904 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002905 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002906 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002907 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002908
2909 /*
2910 * Read loaded_vmcs->cpu should be before fetching
2911 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2912 * See the comments in __loaded_vmcs_clear().
2913 */
2914 smp_rmb();
2915
Nadav Har'Eld462b812011-05-24 15:26:10 +03002916 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2917 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002918 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002919 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002920 }
2921
2922 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2923 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2924 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002925 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002926 }
2927
2928 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002929 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002930 unsigned long sysenter_esp;
2931
2932 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002933
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 /*
2935 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002936 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002938 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002939 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002940 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002942 /*
2943 * VM exits change the host TR limit to 0x67 after a VM
2944 * exit. This is okay, since 0x67 covers everything except
2945 * the IO bitmap and have have code to handle the IO bitmap
2946 * being lost after a VM exit.
2947 */
2948 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2949
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2951 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002952
Nadav Har'Eld462b812011-05-24 15:26:10 +03002953 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 }
Feng Wu28b835d2015-09-18 22:29:54 +08002955
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002956 /* Setup TSC multiplier */
2957 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002958 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2959 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002960
Feng Wu28b835d2015-09-18 22:29:54 +08002961 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002962 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002963 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002964}
2965
2966static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2967{
2968 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2969
2970 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002971 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2972 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002973 return;
2974
2975 /* Set SN when the vCPU is preempted */
2976 if (vcpu->preempted)
2977 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978}
2979
2980static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2981{
Feng Wu28b835d2015-09-18 22:29:54 +08002982 vmx_vcpu_pi_put(vcpu);
2983
Avi Kivitya9b21b62008-06-24 11:48:49 +03002984 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985}
2986
Wanpeng Lif244dee2017-07-20 01:11:54 -07002987static bool emulation_required(struct kvm_vcpu *vcpu)
2988{
2989 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2990}
2991
Avi Kivityedcafe32009-12-30 18:07:40 +02002992static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2993
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002994/*
2995 * Return the cr0 value that a nested guest would read. This is a combination
2996 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2997 * its hypervisor (cr0_read_shadow).
2998 */
2999static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3000{
3001 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3002 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3003}
3004static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3005{
3006 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3007 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3008}
3009
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3011{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003012 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003013
Avi Kivity6de12732011-03-07 12:51:22 +02003014 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3015 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3016 rflags = vmcs_readl(GUEST_RFLAGS);
3017 if (to_vmx(vcpu)->rmode.vm86_active) {
3018 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3019 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3020 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3021 }
3022 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003023 }
Avi Kivity6de12732011-03-07 12:51:22 +02003024 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025}
3026
3027static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3028{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003029 unsigned long old_rflags = vmx_get_rflags(vcpu);
3030
Avi Kivity6de12732011-03-07 12:51:22 +02003031 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3032 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003033 if (to_vmx(vcpu)->rmode.vm86_active) {
3034 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003035 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003036 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003038
3039 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3040 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041}
3042
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003043static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003044{
3045 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3046 int ret = 0;
3047
3048 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003049 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003050 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003051 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003052
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003053 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003054}
3055
3056static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3057{
3058 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3059 u32 interruptibility = interruptibility_old;
3060
3061 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3062
Jan Kiszka48005f62010-02-19 19:38:07 +01003063 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003064 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003065 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003066 interruptibility |= GUEST_INTR_STATE_STI;
3067
3068 if ((interruptibility != interruptibility_old))
3069 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3070}
3071
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3073{
3074 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003076 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003078 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079
Glauber Costa2809f5d2009-05-12 16:21:05 -04003080 /* skipping an emulated instruction also counts */
3081 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082}
3083
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003084static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3085 unsigned long exit_qual)
3086{
3087 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3088 unsigned int nr = vcpu->arch.exception.nr;
3089 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3090
3091 if (vcpu->arch.exception.has_error_code) {
3092 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3093 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3094 }
3095
3096 if (kvm_exception_is_soft(nr))
3097 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3098 else
3099 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3100
3101 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3102 vmx_get_nmi_mask(vcpu))
3103 intr_info |= INTR_INFO_UNBLOCK_NMI;
3104
3105 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3106}
3107
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003108/*
3109 * KVM wants to inject page-faults which it got to the guest. This function
3110 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003111 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003112static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003113{
3114 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003115 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003116
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003117 if (nr == PF_VECTOR) {
3118 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003119 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003120 return 1;
3121 }
3122 /*
3123 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3124 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3125 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3126 * can be written only when inject_pending_event runs. This should be
3127 * conditional on a new capability---if the capability is disabled,
3128 * kvm_multiple_exception would write the ancillary information to
3129 * CR2 or DR6, for backwards ABI-compatibility.
3130 */
3131 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3132 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003133 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003134 return 1;
3135 }
3136 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003137 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003138 if (nr == DB_VECTOR)
3139 *exit_qual = vcpu->arch.dr6;
3140 else
3141 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003142 return 1;
3143 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003144 }
3145
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003146 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003147}
3148
Wanpeng Licaa057a2018-03-12 04:53:03 -07003149static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3150{
3151 /*
3152 * Ensure that we clear the HLT state in the VMCS. We don't need to
3153 * explicitly skip the instruction because if the HLT state is set,
3154 * then the instruction is already executing and RIP has already been
3155 * advanced.
3156 */
3157 if (kvm_hlt_in_guest(vcpu->kvm) &&
3158 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3159 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3160}
3161
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003162static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003163{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003164 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003165 unsigned nr = vcpu->arch.exception.nr;
3166 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003167 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003168 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003169
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003170 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003171 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003172 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3173 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003174
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003175 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003176 int inc_eip = 0;
3177 if (kvm_exception_is_soft(nr))
3178 inc_eip = vcpu->arch.event_exit_inst_len;
3179 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003180 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003181 return;
3182 }
3183
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003184 WARN_ON_ONCE(vmx->emulation_required);
3185
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003186 if (kvm_exception_is_soft(nr)) {
3187 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3188 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003189 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3190 } else
3191 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3192
3193 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003194
3195 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003196}
3197
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003198static bool vmx_rdtscp_supported(void)
3199{
3200 return cpu_has_vmx_rdtscp();
3201}
3202
Mao, Junjiead756a12012-07-02 01:18:48 +00003203static bool vmx_invpcid_supported(void)
3204{
3205 return cpu_has_vmx_invpcid() && enable_ept;
3206}
3207
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208/*
Eddie Donga75beee2007-05-17 18:55:15 +03003209 * Swap MSR entry in host/guest MSR entry array.
3210 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003211static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003212{
Avi Kivity26bb0982009-09-07 11:14:12 +03003213 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003214
3215 tmp = vmx->guest_msrs[to];
3216 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3217 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003218}
3219
3220/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003221 * Set up the vmcs to automatically save and restore system
3222 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3223 * mode, as fiddling with msrs is very expensive.
3224 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003225static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003226{
Avi Kivity26bb0982009-09-07 11:14:12 +03003227 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003228
Eddie Donga75beee2007-05-17 18:55:15 +03003229 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003230#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003231 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003232 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003233 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003234 move_msr_up(vmx, index, save_nmsrs++);
3235 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003236 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003237 move_msr_up(vmx, index, save_nmsrs++);
3238 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003239 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003240 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003241 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003242 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003243 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003244 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003245 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003246 * if efer.sce is enabled.
3247 */
Brian Gerst8c065852010-07-17 09:03:26 -04003248 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003249 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003250 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003251 }
Eddie Donga75beee2007-05-17 18:55:15 +03003252#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003253 index = __find_msr_index(vmx, MSR_EFER);
3254 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003255 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003256
Avi Kivity26bb0982009-09-07 11:14:12 +03003257 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003258
Yang Zhang8d146952013-01-25 10:18:50 +08003259 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003260 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003261}
3262
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003263static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003265 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003267 if (is_guest_mode(vcpu) &&
3268 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3269 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3270
3271 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272}
3273
3274/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003275 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003277static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003279 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003280 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003281 * We're here if L1 chose not to trap WRMSR to TSC. According
3282 * to the spec, this should set L1's TSC; The offset that L1
3283 * set for L2 remains unchanged, and still needs to be added
3284 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003285 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003286 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003287 /* recalculate vmcs02.TSC_OFFSET: */
3288 vmcs12 = get_vmcs12(vcpu);
3289 vmcs_write64(TSC_OFFSET, offset +
3290 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3291 vmcs12->tsc_offset : 0));
3292 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003293 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3294 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003295 vmcs_write64(TSC_OFFSET, offset);
3296 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297}
3298
Nadav Har'El801d3422011-05-25 23:02:23 +03003299/*
3300 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3301 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3302 * all guests if the "nested" module option is off, and can also be disabled
3303 * for a single guest by disabling its VMX cpuid bit.
3304 */
3305static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3306{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003307 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003308}
3309
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003311 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3312 * returned for the various VMX controls MSRs when nested VMX is enabled.
3313 * The same values should also be used to verify that vmcs12 control fields are
3314 * valid during nested entry from L1 to L2.
3315 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3316 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3317 * bit in the high half is on if the corresponding bit in the control field
3318 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003319 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003320static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003321{
Paolo Bonzini13893092018-02-26 13:40:09 +01003322 if (!nested) {
3323 memset(msrs, 0, sizeof(*msrs));
3324 return;
3325 }
3326
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003327 /*
3328 * Note that as a general rule, the high half of the MSRs (bits in
3329 * the control fields which may be 1) should be initialized by the
3330 * intersection of the underlying hardware's MSR (i.e., features which
3331 * can be supported) and the list of features we want to expose -
3332 * because they are known to be properly supported in our code.
3333 * Also, usually, the low half of the MSRs (bits which must be 1) can
3334 * be set to 0, meaning that L1 may turn off any of these bits. The
3335 * reason is that if one of these bits is necessary, it will appear
3336 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3337 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003338 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003339 * These rules have exceptions below.
3340 */
3341
3342 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003343 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003344 msrs->pinbased_ctls_low,
3345 msrs->pinbased_ctls_high);
3346 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003347 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003348 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003349 PIN_BASED_EXT_INTR_MASK |
3350 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003351 PIN_BASED_VIRTUAL_NMIS |
3352 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003353 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003354 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003355 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003356
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003357 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003358 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003359 msrs->exit_ctls_low,
3360 msrs->exit_ctls_high);
3361 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003362 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003363
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003364 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003365#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003366 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003367#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003368 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003370 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003371 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003372 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3373
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003374 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003375 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003376
Jan Kiszka2996fca2014-06-16 13:59:43 +02003377 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003378 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003379
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003380 /* entry controls */
3381 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003382 msrs->entry_ctls_low,
3383 msrs->entry_ctls_high);
3384 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003385 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003386 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003387#ifdef CONFIG_X86_64
3388 VM_ENTRY_IA32E_MODE |
3389#endif
3390 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003391 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003392 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003393 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003394 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003395
Jan Kiszka2996fca2014-06-16 13:59:43 +02003396 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003398
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003399 /* cpu-based controls */
3400 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003401 msrs->procbased_ctls_low,
3402 msrs->procbased_ctls_high);
3403 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003404 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003405 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003406 CPU_BASED_VIRTUAL_INTR_PENDING |
3407 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003408 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3409 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3410 CPU_BASED_CR3_STORE_EXITING |
3411#ifdef CONFIG_X86_64
3412 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3413#endif
3414 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003415 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3416 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3417 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3418 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003419 /*
3420 * We can allow some features even when not supported by the
3421 * hardware. For example, L1 can specify an MSR bitmap - and we
3422 * can use it to avoid exits to L1 - even when L0 runs L2
3423 * without MSR bitmaps.
3424 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003425 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003426 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003427 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003428
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003429 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003430 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003431 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3432
Paolo Bonzini80154d72017-08-24 13:55:35 +02003433 /*
3434 * secondary cpu-based controls. Do not include those that
3435 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3436 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003437 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003438 msrs->secondary_ctls_low,
3439 msrs->secondary_ctls_high);
3440 msrs->secondary_ctls_low = 0;
3441 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003442 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003443 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003444 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003445 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003446 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003447 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003448
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003449 if (enable_ept) {
3450 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003451 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003452 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003453 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003454 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003455 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003456 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003457 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003458 msrs->ept_caps &= vmx_capability.ept;
3459 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003460 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3461 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003462 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003463 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003464 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003466 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003467 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003468
Bandan Das27c42a12017-08-03 15:54:42 -04003469 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003470 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003471 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003472 /*
3473 * Advertise EPTP switching unconditionally
3474 * since we emulate it
3475 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003476 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003477 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003478 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003479 }
3480
Paolo Bonzinief697a72016-03-18 16:58:38 +01003481 /*
3482 * Old versions of KVM use the single-context version without
3483 * checking for support, so declare that it is supported even
3484 * though it is treated as global context. The alternative is
3485 * not failing the single-context invvpid, and it is worse.
3486 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003487 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003488 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003489 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003490 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003491 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003492 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003493
Radim Krčmář0790ec12015-03-17 14:02:32 +01003494 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003495 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003496 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3497
Jan Kiszkac18911a2013-03-13 16:06:41 +01003498 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003499 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003500 msrs->misc_low,
3501 msrs->misc_high);
3502 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3503 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003504 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003505 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003506 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003507 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003508
3509 /*
3510 * This MSR reports some information about VMX support. We
3511 * should return information about the VMX we emulate for the
3512 * guest, and the VMCS structure we give it - not about the
3513 * VMX support of the underlying hardware.
3514 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003515 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003516 VMCS12_REVISION |
3517 VMX_BASIC_TRUE_CTLS |
3518 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3519 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3520
3521 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003522 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003523
3524 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003525 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003526 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3527 * We picked the standard core2 setting.
3528 */
3529#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3530#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003531 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3532 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003533
3534 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003535 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3536 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003537
3538 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003540}
3541
David Matlack38991522016-11-29 18:14:08 -08003542/*
3543 * if fixed0[i] == 1: val[i] must be 1
3544 * if fixed1[i] == 0: val[i] must be 0
3545 */
3546static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3547{
3548 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003549}
3550
3551static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3552{
David Matlack38991522016-11-29 18:14:08 -08003553 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003554}
3555
3556static inline u64 vmx_control_msr(u32 low, u32 high)
3557{
3558 return low | ((u64)high << 32);
3559}
3560
David Matlack62cc6b9d2016-11-29 18:14:07 -08003561static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3562{
3563 superset &= mask;
3564 subset &= mask;
3565
3566 return (superset | subset) == superset;
3567}
3568
3569static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3570{
3571 const u64 feature_and_reserved =
3572 /* feature (except bit 48; see below) */
3573 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3574 /* reserved */
3575 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003577
3578 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3579 return -EINVAL;
3580
3581 /*
3582 * KVM does not emulate a version of VMX that constrains physical
3583 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3584 */
3585 if (data & BIT_ULL(48))
3586 return -EINVAL;
3587
3588 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3589 vmx_basic_vmcs_revision_id(data))
3590 return -EINVAL;
3591
3592 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3593 return -EINVAL;
3594
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003596 return 0;
3597}
3598
3599static int
3600vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3601{
3602 u64 supported;
3603 u32 *lowp, *highp;
3604
3605 switch (msr_index) {
3606 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3608 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003609 break;
3610 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003611 lowp = &vmx->nested.msrs.procbased_ctls_low;
3612 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003613 break;
3614 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 lowp = &vmx->nested.msrs.exit_ctls_low;
3616 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003617 break;
3618 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 lowp = &vmx->nested.msrs.entry_ctls_low;
3620 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003621 break;
3622 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003623 lowp = &vmx->nested.msrs.secondary_ctls_low;
3624 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003625 break;
3626 default:
3627 BUG();
3628 }
3629
3630 supported = vmx_control_msr(*lowp, *highp);
3631
3632 /* Check must-be-1 bits are still 1. */
3633 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3634 return -EINVAL;
3635
3636 /* Check must-be-0 bits are still 0. */
3637 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3638 return -EINVAL;
3639
3640 *lowp = data;
3641 *highp = data >> 32;
3642 return 0;
3643}
3644
3645static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3646{
3647 const u64 feature_and_reserved_bits =
3648 /* feature */
3649 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3650 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3651 /* reserved */
3652 GENMASK_ULL(13, 9) | BIT_ULL(31);
3653 u64 vmx_misc;
3654
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003655 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3656 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003657
3658 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3659 return -EINVAL;
3660
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003661 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003662 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3663 vmx_misc_preemption_timer_rate(data) !=
3664 vmx_misc_preemption_timer_rate(vmx_misc))
3665 return -EINVAL;
3666
3667 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3668 return -EINVAL;
3669
3670 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3671 return -EINVAL;
3672
3673 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3674 return -EINVAL;
3675
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003676 vmx->nested.msrs.misc_low = data;
3677 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003678
3679 /*
3680 * If L1 has read-only VM-exit information fields, use the
3681 * less permissive vmx_vmwrite_bitmap to specify write
3682 * permissions for the shadow VMCS.
3683 */
3684 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3685 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3686
David Matlack62cc6b9d2016-11-29 18:14:07 -08003687 return 0;
3688}
3689
3690static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3691{
3692 u64 vmx_ept_vpid_cap;
3693
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3695 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003696
3697 /* Every bit is either reserved or a feature bit. */
3698 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3699 return -EINVAL;
3700
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003701 vmx->nested.msrs.ept_caps = data;
3702 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003703 return 0;
3704}
3705
3706static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3707{
3708 u64 *msr;
3709
3710 switch (msr_index) {
3711 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003712 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003713 break;
3714 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003715 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003716 break;
3717 default:
3718 BUG();
3719 }
3720
3721 /*
3722 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3723 * must be 1 in the restored value.
3724 */
3725 if (!is_bitwise_subset(data, *msr, -1ULL))
3726 return -EINVAL;
3727
3728 *msr = data;
3729 return 0;
3730}
3731
3732/*
3733 * Called when userspace is restoring VMX MSRs.
3734 *
3735 * Returns 0 on success, non-0 otherwise.
3736 */
3737static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3738{
3739 struct vcpu_vmx *vmx = to_vmx(vcpu);
3740
Jim Mattsona943ac52018-05-29 09:11:32 -07003741 /*
3742 * Don't allow changes to the VMX capability MSRs while the vCPU
3743 * is in VMX operation.
3744 */
3745 if (vmx->nested.vmxon)
3746 return -EBUSY;
3747
David Matlack62cc6b9d2016-11-29 18:14:07 -08003748 switch (msr_index) {
3749 case MSR_IA32_VMX_BASIC:
3750 return vmx_restore_vmx_basic(vmx, data);
3751 case MSR_IA32_VMX_PINBASED_CTLS:
3752 case MSR_IA32_VMX_PROCBASED_CTLS:
3753 case MSR_IA32_VMX_EXIT_CTLS:
3754 case MSR_IA32_VMX_ENTRY_CTLS:
3755 /*
3756 * The "non-true" VMX capability MSRs are generated from the
3757 * "true" MSRs, so we do not support restoring them directly.
3758 *
3759 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3760 * should restore the "true" MSRs with the must-be-1 bits
3761 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3762 * DEFAULT SETTINGS".
3763 */
3764 return -EINVAL;
3765 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3766 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3767 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3768 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3769 case MSR_IA32_VMX_PROCBASED_CTLS2:
3770 return vmx_restore_control_msr(vmx, msr_index, data);
3771 case MSR_IA32_VMX_MISC:
3772 return vmx_restore_vmx_misc(vmx, data);
3773 case MSR_IA32_VMX_CR0_FIXED0:
3774 case MSR_IA32_VMX_CR4_FIXED0:
3775 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3776 case MSR_IA32_VMX_CR0_FIXED1:
3777 case MSR_IA32_VMX_CR4_FIXED1:
3778 /*
3779 * These MSRs are generated based on the vCPU's CPUID, so we
3780 * do not support restoring them directly.
3781 */
3782 return -EINVAL;
3783 case MSR_IA32_VMX_EPT_VPID_CAP:
3784 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3785 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003787 return 0;
3788 default:
3789 /*
3790 * The rest of the VMX capability MSRs do not support restore.
3791 */
3792 return -EINVAL;
3793 }
3794}
3795
Jan Kiszkacae50132014-01-04 18:47:22 +01003796/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003797static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003798{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003799 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003800 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003801 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003802 break;
3803 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3804 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003805 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 msrs->pinbased_ctls_low,
3807 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003808 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3809 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003810 break;
3811 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3812 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003813 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003814 msrs->procbased_ctls_low,
3815 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003816 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3817 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003818 break;
3819 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3820 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003821 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003822 msrs->exit_ctls_low,
3823 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003824 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3825 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003826 break;
3827 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3828 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003829 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003830 msrs->entry_ctls_low,
3831 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003832 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3833 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003834 break;
3835 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003836 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003837 msrs->misc_low,
3838 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003839 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003840 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003841 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003842 break;
3843 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003844 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003845 break;
3846 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003847 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003848 break;
3849 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003850 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003851 break;
3852 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003853 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003854 break;
3855 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003856 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003857 msrs->secondary_ctls_low,
3858 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003859 break;
3860 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003861 *pdata = msrs->ept_caps |
3862 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003863 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003864 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003865 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003866 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003867 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003868 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003869 }
3870
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003871 return 0;
3872}
3873
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003874static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3875 uint64_t val)
3876{
3877 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3878
3879 return !(val & ~valid_bits);
3880}
3881
Tom Lendacky801e4592018-02-21 13:39:51 -06003882static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3883{
Paolo Bonzini13893092018-02-26 13:40:09 +01003884 switch (msr->index) {
3885 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3886 if (!nested)
3887 return 1;
3888 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3889 default:
3890 return 1;
3891 }
3892
3893 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003894}
3895
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003896/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897 * Reads an msr value (of 'msr_index') into 'pdata'.
3898 * Returns 0 on success, non-0 otherwise.
3899 * Assumes vcpu_load() was already called.
3900 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003901static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003903 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003904 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003906 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003907#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003909 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
3911 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003912 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003914 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003915 vmx_load_host_state(vmx);
3916 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003917 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003918#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003920 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003921 case MSR_IA32_SPEC_CTRL:
3922 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003923 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3924 return 1;
3925
3926 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3927 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003928 case MSR_IA32_ARCH_CAPABILITIES:
3929 if (!msr_info->host_initiated &&
3930 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3931 return 1;
3932 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3933 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003935 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 break;
3937 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003938 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939 break;
3940 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003941 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003943 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003944 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003945 (!msr_info->host_initiated &&
3946 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003947 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003948 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003949 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003950 case MSR_IA32_MCG_EXT_CTL:
3951 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003952 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003953 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003954 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003955 msr_info->data = vcpu->arch.mcg_ext_ctl;
3956 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003957 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003958 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003959 break;
3960 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3961 if (!nested_vmx_allowed(vcpu))
3962 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003963 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3964 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003965 case MSR_IA32_XSS:
3966 if (!vmx_xsaves_supported())
3967 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003968 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003969 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003970 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003971 if (!msr_info->host_initiated &&
3972 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003973 return 1;
3974 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003976 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003977 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003978 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003979 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003981 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982 }
3983
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984 return 0;
3985}
3986
Jan Kiszkacae50132014-01-04 18:47:22 +01003987static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3988
Avi Kivity6aa8b732006-12-10 02:21:36 -08003989/*
3990 * Writes msr value into into the appropriate "register".
3991 * Returns 0 on success, non-0 otherwise.
3992 * Assumes vcpu_load() was already called.
3993 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003994static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003996 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003997 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003998 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003999 u32 msr_index = msr_info->index;
4000 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004001
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004003 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004004 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004005 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004006#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004007 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004008 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009 vmcs_writel(GUEST_FS_BASE, data);
4010 break;
4011 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004012 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 vmcs_writel(GUEST_GS_BASE, data);
4014 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004015 case MSR_KERNEL_GS_BASE:
4016 vmx_load_host_state(vmx);
4017 vmx->msr_guest_kernel_gs_base = data;
4018 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004019#endif
4020 case MSR_IA32_SYSENTER_CS:
4021 vmcs_write32(GUEST_SYSENTER_CS, data);
4022 break;
4023 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004024 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004025 break;
4026 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004027 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004029 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004030 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004031 (!msr_info->host_initiated &&
4032 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004033 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004034 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004035 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004036 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004037 vmcs_write64(GUEST_BNDCFGS, data);
4038 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004039 case MSR_IA32_SPEC_CTRL:
4040 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004041 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4042 return 1;
4043
4044 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004045 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004046 return 1;
4047
4048 vmx->spec_ctrl = data;
4049
4050 if (!data)
4051 break;
4052
4053 /*
4054 * For non-nested:
4055 * When it's written (to non-zero) for the first time, pass
4056 * it through.
4057 *
4058 * For nested:
4059 * The handling of the MSR bitmap for L2 guests is done in
4060 * nested_vmx_merge_msr_bitmap. We should not touch the
4061 * vmcs02.msr_bitmap here since it gets completely overwritten
4062 * in the merging. We update the vmcs01 here for L1 as well
4063 * since it will end up touching the MSR anyway now.
4064 */
4065 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4066 MSR_IA32_SPEC_CTRL,
4067 MSR_TYPE_RW);
4068 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004069 case MSR_IA32_PRED_CMD:
4070 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004071 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4072 return 1;
4073
4074 if (data & ~PRED_CMD_IBPB)
4075 return 1;
4076
4077 if (!data)
4078 break;
4079
4080 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4081
4082 /*
4083 * For non-nested:
4084 * When it's written (to non-zero) for the first time, pass
4085 * it through.
4086 *
4087 * For nested:
4088 * The handling of the MSR bitmap for L2 guests is done in
4089 * nested_vmx_merge_msr_bitmap. We should not touch the
4090 * vmcs02.msr_bitmap here since it gets completely overwritten
4091 * in the merging.
4092 */
4093 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4094 MSR_TYPE_W);
4095 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004096 case MSR_IA32_ARCH_CAPABILITIES:
4097 if (!msr_info->host_initiated)
4098 return 1;
4099 vmx->arch_capabilities = data;
4100 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004101 case MSR_IA32_CR_PAT:
4102 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004103 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4104 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004105 vmcs_write64(GUEST_IA32_PAT, data);
4106 vcpu->arch.pat = data;
4107 break;
4108 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004109 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004110 break;
Will Auldba904632012-11-29 12:42:50 -08004111 case MSR_IA32_TSC_ADJUST:
4112 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004113 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004114 case MSR_IA32_MCG_EXT_CTL:
4115 if ((!msr_info->host_initiated &&
4116 !(to_vmx(vcpu)->msr_ia32_feature_control &
4117 FEATURE_CONTROL_LMCE)) ||
4118 (data & ~MCG_EXT_CTL_LMCE_EN))
4119 return 1;
4120 vcpu->arch.mcg_ext_ctl = data;
4121 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004122 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004123 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004124 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004125 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4126 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004127 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004128 if (msr_info->host_initiated && data == 0)
4129 vmx_leave_nested(vcpu);
4130 break;
4131 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004132 if (!msr_info->host_initiated)
4133 return 1; /* they are read-only */
4134 if (!nested_vmx_allowed(vcpu))
4135 return 1;
4136 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004137 case MSR_IA32_XSS:
4138 if (!vmx_xsaves_supported())
4139 return 1;
4140 /*
4141 * The only supported bit as of Skylake is bit 8, but
4142 * it is not supported on KVM.
4143 */
4144 if (data != 0)
4145 return 1;
4146 vcpu->arch.ia32_xss = data;
4147 if (vcpu->arch.ia32_xss != host_xss)
4148 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004149 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004150 else
4151 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4152 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004153 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004154 if (!msr_info->host_initiated &&
4155 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004156 return 1;
4157 /* Check reserved bit, higher 32 bits should be zero */
4158 if ((data >> 32) != 0)
4159 return 1;
4160 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004162 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004163 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004164 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004165 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004166 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4167 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004168 ret = kvm_set_shared_msr(msr->index, msr->data,
4169 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004170 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004171 if (ret)
4172 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004173 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004174 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004176 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177 }
4178
Eddie Dong2cc51562007-05-21 07:28:09 +03004179 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180}
4181
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004182static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004184 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4185 switch (reg) {
4186 case VCPU_REGS_RSP:
4187 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4188 break;
4189 case VCPU_REGS_RIP:
4190 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4191 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004192 case VCPU_EXREG_PDPTR:
4193 if (enable_ept)
4194 ept_save_pdptrs(vcpu);
4195 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004196 default:
4197 break;
4198 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004199}
4200
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201static __init int cpu_has_kvm_support(void)
4202{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004203 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204}
4205
4206static __init int vmx_disabled_by_bios(void)
4207{
4208 u64 msr;
4209
4210 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004211 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004212 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004213 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4214 && tboot_enabled())
4215 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004216 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004217 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004218 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004219 && !tboot_enabled()) {
4220 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004221 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004222 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004223 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004224 /* launched w/o TXT and VMX disabled */
4225 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4226 && !tboot_enabled())
4227 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004228 }
4229
4230 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004231}
4232
Dongxiao Xu7725b892010-05-11 18:29:38 +08004233static void kvm_cpu_vmxon(u64 addr)
4234{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004235 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004236 intel_pt_handle_vmx(1);
4237
Dongxiao Xu7725b892010-05-11 18:29:38 +08004238 asm volatile (ASM_VMX_VMXON_RAX
4239 : : "a"(&addr), "m"(addr)
4240 : "memory", "cc");
4241}
4242
Radim Krčmář13a34e02014-08-28 15:13:03 +02004243static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244{
4245 int cpu = raw_smp_processor_id();
4246 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004247 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004249 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004250 return -EBUSY;
4251
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004252 /*
4253 * This can happen if we hot-added a CPU but failed to allocate
4254 * VP assist page for it.
4255 */
4256 if (static_branch_unlikely(&enable_evmcs) &&
4257 !hv_get_vp_assist_page(cpu))
4258 return -EFAULT;
4259
Nadav Har'Eld462b812011-05-24 15:26:10 +03004260 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004261 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4262 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004263
4264 /*
4265 * Now we can enable the vmclear operation in kdump
4266 * since the loaded_vmcss_on_cpu list on this cpu
4267 * has been initialized.
4268 *
4269 * Though the cpu is not in VMX operation now, there
4270 * is no problem to enable the vmclear operation
4271 * for the loaded_vmcss_on_cpu list is empty!
4272 */
4273 crash_enable_local_vmclear(cpu);
4274
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004276
4277 test_bits = FEATURE_CONTROL_LOCKED;
4278 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4279 if (tboot_enabled())
4280 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4281
4282 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004284 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4285 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004286 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004287 if (enable_ept)
4288 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004289
4290 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004291}
4292
Nadav Har'Eld462b812011-05-24 15:26:10 +03004293static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004294{
4295 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004296 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004297
Nadav Har'Eld462b812011-05-24 15:26:10 +03004298 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4299 loaded_vmcss_on_cpu_link)
4300 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004301}
4302
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004303
4304/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4305 * tricks.
4306 */
4307static void kvm_cpu_vmxoff(void)
4308{
4309 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004310
4311 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004312 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004313}
4314
Radim Krčmář13a34e02014-08-28 15:13:03 +02004315static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004317 vmclear_local_loaded_vmcss();
4318 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319}
4320
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004321static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004322 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004323{
4324 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004325 u32 ctl = ctl_min | ctl_opt;
4326
4327 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4328
4329 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4330 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4331
4332 /* Ensure minimum (required) set of control bits are supported. */
4333 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004335
4336 *result = ctl;
4337 return 0;
4338}
4339
Avi Kivity110312c2010-12-21 12:54:20 +02004340static __init bool allow_1_setting(u32 msr, u32 ctl)
4341{
4342 u32 vmx_msr_low, vmx_msr_high;
4343
4344 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4345 return vmx_msr_high & ctl;
4346}
4347
Yang, Sheng002c7f72007-07-31 14:23:01 +03004348static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004349{
4350 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004351 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004352 u32 _pin_based_exec_control = 0;
4353 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004354 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004355 u32 _vmexit_control = 0;
4356 u32 _vmentry_control = 0;
4357
Paolo Bonzini13893092018-02-26 13:40:09 +01004358 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304359 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004360#ifdef CONFIG_X86_64
4361 CPU_BASED_CR8_LOAD_EXITING |
4362 CPU_BASED_CR8_STORE_EXITING |
4363#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004364 CPU_BASED_CR3_LOAD_EXITING |
4365 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004366 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004367 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004368 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004369 CPU_BASED_MWAIT_EXITING |
4370 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004371 CPU_BASED_INVLPG_EXITING |
4372 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004373
Sheng Yangf78e0e22007-10-29 09:40:42 +08004374 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004375 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004376 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004377 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4378 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004379 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004380#ifdef CONFIG_X86_64
4381 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4382 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4383 ~CPU_BASED_CR8_STORE_EXITING;
4384#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004385 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004386 min2 = 0;
4387 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004388 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004389 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004390 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004391 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004392 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004393 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004394 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004395 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004396 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004397 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004398 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004399 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004400 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004401 SECONDARY_EXEC_RDSEED_EXITING |
4402 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004403 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004404 SECONDARY_EXEC_TSC_SCALING |
4405 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004406 if (adjust_vmx_controls(min2, opt2,
4407 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004408 &_cpu_based_2nd_exec_control) < 0)
4409 return -EIO;
4410 }
4411#ifndef CONFIG_X86_64
4412 if (!(_cpu_based_2nd_exec_control &
4413 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4414 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4415#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004416
4417 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4418 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004419 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004420 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4421 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004422
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004423 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4424 &vmx_capability.ept, &vmx_capability.vpid);
4425
Sheng Yangd56f5462008-04-25 10:13:16 +08004426 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004427 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4428 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004429 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4430 CPU_BASED_CR3_STORE_EXITING |
4431 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004432 } else if (vmx_capability.ept) {
4433 vmx_capability.ept = 0;
4434 pr_warn_once("EPT CAP should not exist if not support "
4435 "1-setting enable EPT VM-execution control\n");
4436 }
4437 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4438 vmx_capability.vpid) {
4439 vmx_capability.vpid = 0;
4440 pr_warn_once("VPID CAP should not exist if not support "
4441 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004442 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004443
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004444 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004445#ifdef CONFIG_X86_64
4446 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4447#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004448 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004449 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004450 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4451 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004452 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004453
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004454 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4455 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4456 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004457 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4458 &_pin_based_exec_control) < 0)
4459 return -EIO;
4460
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004461 if (cpu_has_broken_vmx_preemption_timer())
4462 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004463 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004464 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004465 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4466
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004467 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004468 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004469 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4470 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004471 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004473 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004474
4475 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4476 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004477 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004478
4479#ifdef CONFIG_X86_64
4480 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4481 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004482 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004483#endif
4484
4485 /* Require Write-Back (WB) memory type for VMCS accesses. */
4486 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004487 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004488
Yang, Sheng002c7f72007-07-31 14:23:01 +03004489 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004490 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004491 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004492
Liran Alon2307af12018-06-29 22:59:04 +03004493 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004494
Yang, Sheng002c7f72007-07-31 14:23:01 +03004495 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4496 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004497 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004498 vmcs_conf->vmexit_ctrl = _vmexit_control;
4499 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004500
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004501 if (static_branch_unlikely(&enable_evmcs))
4502 evmcs_sanitize_exec_ctrls(vmcs_conf);
4503
Avi Kivity110312c2010-12-21 12:54:20 +02004504 cpu_has_load_ia32_efer =
4505 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4506 VM_ENTRY_LOAD_IA32_EFER)
4507 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4508 VM_EXIT_LOAD_IA32_EFER);
4509
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004510 cpu_has_load_perf_global_ctrl =
4511 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4512 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4513 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4514 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4515
4516 /*
4517 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004518 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004519 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4520 *
4521 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4522 *
4523 * AAK155 (model 26)
4524 * AAP115 (model 30)
4525 * AAT100 (model 37)
4526 * BC86,AAY89,BD102 (model 44)
4527 * BA97 (model 46)
4528 *
4529 */
4530 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4531 switch (boot_cpu_data.x86_model) {
4532 case 26:
4533 case 30:
4534 case 37:
4535 case 44:
4536 case 46:
4537 cpu_has_load_perf_global_ctrl = false;
4538 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4539 "does not work properly. Using workaround\n");
4540 break;
4541 default:
4542 break;
4543 }
4544 }
4545
Borislav Petkov782511b2016-04-04 22:25:03 +02004546 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004547 rdmsrl(MSR_IA32_XSS, host_xss);
4548
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004549 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004550}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551
4552static struct vmcs *alloc_vmcs_cpu(int cpu)
4553{
4554 int node = cpu_to_node(cpu);
4555 struct page *pages;
4556 struct vmcs *vmcs;
4557
Vlastimil Babka96db8002015-09-08 15:03:50 -07004558 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004559 if (!pages)
4560 return NULL;
4561 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004562 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004563
4564 /* KVM supports Enlightened VMCS v1 only */
4565 if (static_branch_unlikely(&enable_evmcs))
4566 vmcs->revision_id = KVM_EVMCS_VERSION;
4567 else
4568 vmcs->revision_id = vmcs_config.revision_id;
4569
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570 return vmcs;
4571}
4572
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573static void free_vmcs(struct vmcs *vmcs)
4574{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004575 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576}
4577
Nadav Har'Eld462b812011-05-24 15:26:10 +03004578/*
4579 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4580 */
4581static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4582{
4583 if (!loaded_vmcs->vmcs)
4584 return;
4585 loaded_vmcs_clear(loaded_vmcs);
4586 free_vmcs(loaded_vmcs->vmcs);
4587 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004588 if (loaded_vmcs->msr_bitmap)
4589 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004590 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004591}
4592
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004593static struct vmcs *alloc_vmcs(void)
4594{
4595 return alloc_vmcs_cpu(raw_smp_processor_id());
4596}
4597
4598static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4599{
4600 loaded_vmcs->vmcs = alloc_vmcs();
4601 if (!loaded_vmcs->vmcs)
4602 return -ENOMEM;
4603
4604 loaded_vmcs->shadow_vmcs = NULL;
4605 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004606
4607 if (cpu_has_vmx_msr_bitmap()) {
4608 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4609 if (!loaded_vmcs->msr_bitmap)
4610 goto out_vmcs;
4611 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004612
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004613 if (IS_ENABLED(CONFIG_HYPERV) &&
4614 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004615 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4616 struct hv_enlightened_vmcs *evmcs =
4617 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4618
4619 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4620 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004621 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004622 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004623
4624out_vmcs:
4625 free_loaded_vmcs(loaded_vmcs);
4626 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004627}
4628
Sam Ravnborg39959582007-06-01 00:47:13 -07004629static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630{
4631 int cpu;
4632
Zachary Amsden3230bb42009-09-29 11:38:37 -10004633 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004635 per_cpu(vmxarea, cpu) = NULL;
4636 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004637}
4638
Jim Mattsond37f4262017-12-22 12:12:16 -08004639enum vmcs_field_width {
4640 VMCS_FIELD_WIDTH_U16 = 0,
4641 VMCS_FIELD_WIDTH_U64 = 1,
4642 VMCS_FIELD_WIDTH_U32 = 2,
4643 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004644};
4645
Jim Mattsond37f4262017-12-22 12:12:16 -08004646static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004647{
4648 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004649 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004650 return (field >> 13) & 0x3 ;
4651}
4652
4653static inline int vmcs_field_readonly(unsigned long field)
4654{
4655 return (((field >> 10) & 0x3) == 1);
4656}
4657
Bandan Dasfe2b2012014-04-21 15:20:14 -04004658static void init_vmcs_shadow_fields(void)
4659{
4660 int i, j;
4661
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004662 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4663 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004664 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004665 (i + 1 == max_shadow_read_only_fields ||
4666 shadow_read_only_fields[i + 1] != field + 1))
4667 pr_err("Missing field from shadow_read_only_field %x\n",
4668 field + 1);
4669
4670 clear_bit(field, vmx_vmread_bitmap);
4671#ifdef CONFIG_X86_64
4672 if (field & 1)
4673 continue;
4674#endif
4675 if (j < i)
4676 shadow_read_only_fields[j] = field;
4677 j++;
4678 }
4679 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004680
4681 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004682 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004683 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004684 (i + 1 == max_shadow_read_write_fields ||
4685 shadow_read_write_fields[i + 1] != field + 1))
4686 pr_err("Missing field from shadow_read_write_field %x\n",
4687 field + 1);
4688
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004689 /*
4690 * PML and the preemption timer can be emulated, but the
4691 * processor cannot vmwrite to fields that don't exist
4692 * on bare metal.
4693 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004694 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004695 case GUEST_PML_INDEX:
4696 if (!cpu_has_vmx_pml())
4697 continue;
4698 break;
4699 case VMX_PREEMPTION_TIMER_VALUE:
4700 if (!cpu_has_vmx_preemption_timer())
4701 continue;
4702 break;
4703 case GUEST_INTR_STATUS:
4704 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004705 continue;
4706 break;
4707 default:
4708 break;
4709 }
4710
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004711 clear_bit(field, vmx_vmwrite_bitmap);
4712 clear_bit(field, vmx_vmread_bitmap);
4713#ifdef CONFIG_X86_64
4714 if (field & 1)
4715 continue;
4716#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004717 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004718 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004719 j++;
4720 }
4721 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004722}
4723
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724static __init int alloc_kvm_area(void)
4725{
4726 int cpu;
4727
Zachary Amsden3230bb42009-09-29 11:38:37 -10004728 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729 struct vmcs *vmcs;
4730
4731 vmcs = alloc_vmcs_cpu(cpu);
4732 if (!vmcs) {
4733 free_kvm_area();
4734 return -ENOMEM;
4735 }
4736
Liran Alon2307af12018-06-29 22:59:04 +03004737 /*
4738 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4739 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4740 * revision_id reported by MSR_IA32_VMX_BASIC.
4741 *
4742 * However, even though not explictly documented by
4743 * TLFS, VMXArea passed as VMXON argument should
4744 * still be marked with revision_id reported by
4745 * physical CPU.
4746 */
4747 if (static_branch_unlikely(&enable_evmcs))
4748 vmcs->revision_id = vmcs_config.revision_id;
4749
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750 per_cpu(vmxarea, cpu) = vmcs;
4751 }
4752 return 0;
4753}
4754
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004755static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004756 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004758 if (!emulate_invalid_guest_state) {
4759 /*
4760 * CS and SS RPL should be equal during guest entry according
4761 * to VMX spec, but in reality it is not always so. Since vcpu
4762 * is in the middle of the transition from real mode to
4763 * protected mode it is safe to assume that RPL 0 is a good
4764 * default value.
4765 */
4766 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004767 save->selector &= ~SEGMENT_RPL_MASK;
4768 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004769 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004771 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772}
4773
4774static void enter_pmode(struct kvm_vcpu *vcpu)
4775{
4776 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004777 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004778
Gleb Natapovd99e4152012-12-20 16:57:45 +02004779 /*
4780 * Update real mode segment cache. It may be not up-to-date if sement
4781 * register was written while vcpu was in a guest mode.
4782 */
4783 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4784 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4785 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4786 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4787 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4788 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4789
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004790 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791
Avi Kivity2fb92db2011-04-27 19:42:18 +03004792 vmx_segment_cache_clear(vmx);
4793
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004794 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795
4796 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004797 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4798 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 vmcs_writel(GUEST_RFLAGS, flags);
4800
Rusty Russell66aee912007-07-17 23:34:16 +10004801 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4802 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004803
4804 update_exception_bitmap(vcpu);
4805
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004806 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4807 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4808 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4809 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4810 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4811 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812}
4813
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004814static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815{
Mathias Krause772e0312012-08-30 01:30:19 +02004816 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004817 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818
Gleb Natapovd99e4152012-12-20 16:57:45 +02004819 var.dpl = 0x3;
4820 if (seg == VCPU_SREG_CS)
4821 var.type = 0x3;
4822
4823 if (!emulate_invalid_guest_state) {
4824 var.selector = var.base >> 4;
4825 var.base = var.base & 0xffff0;
4826 var.limit = 0xffff;
4827 var.g = 0;
4828 var.db = 0;
4829 var.present = 1;
4830 var.s = 1;
4831 var.l = 0;
4832 var.unusable = 0;
4833 var.type = 0x3;
4834 var.avl = 0;
4835 if (save->base & 0xf)
4836 printk_once(KERN_WARNING "kvm: segment base is not "
4837 "paragraph aligned when entering "
4838 "protected mode (seg=%d)", seg);
4839 }
4840
4841 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004842 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004843 vmcs_write32(sf->limit, var.limit);
4844 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845}
4846
4847static void enter_rmode(struct kvm_vcpu *vcpu)
4848{
4849 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004850 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004851 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004853 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4854 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4855 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4856 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4857 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004858 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4859 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004860
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004861 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862
Gleb Natapov776e58e2011-03-13 12:34:27 +02004863 /*
4864 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004865 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004866 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004867 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004868 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4869 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004870
Avi Kivity2fb92db2011-04-27 19:42:18 +03004871 vmx_segment_cache_clear(vmx);
4872
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004873 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4876
4877 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004878 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004880 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881
4882 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004883 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 update_exception_bitmap(vcpu);
4885
Gleb Natapovd99e4152012-12-20 16:57:45 +02004886 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4887 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4888 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4889 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4890 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4891 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004892
Eddie Dong8668a3c2007-10-10 14:26:45 +08004893 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894}
4895
Amit Shah401d10d2009-02-20 22:53:37 +05304896static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4897{
4898 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004899 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4900
4901 if (!msr)
4902 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304903
Avi Kivity44ea2b12009-09-06 15:55:37 +03004904 /*
4905 * Force kernel_gs_base reloading before EFER changes, as control
4906 * of this msr depends on is_long_mode().
4907 */
4908 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004909 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304910 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004911 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304912 msr->data = efer;
4913 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004914 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304915
4916 msr->data = efer & ~EFER_LME;
4917 }
4918 setup_msrs(vmx);
4919}
4920
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004921#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004922
4923static void enter_lmode(struct kvm_vcpu *vcpu)
4924{
4925 u32 guest_tr_ar;
4926
Avi Kivity2fb92db2011-04-27 19:42:18 +03004927 vmx_segment_cache_clear(to_vmx(vcpu));
4928
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004930 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004931 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4932 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004934 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4935 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936 }
Avi Kivityda38f432010-07-06 11:30:49 +03004937 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004938}
4939
4940static void exit_lmode(struct kvm_vcpu *vcpu)
4941{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004942 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004943 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004944}
4945
4946#endif
4947
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004948static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4949 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004950{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004951 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004952 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4953 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004954 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004955 } else {
4956 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004957 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004958}
4959
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004960static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004961{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004962 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004963}
4964
Avi Kivitye8467fd2009-12-29 18:43:06 +02004965static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4966{
4967 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4968
4969 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4970 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4971}
4972
Avi Kivityaff48ba2010-12-05 18:56:11 +02004973static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4974{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004975 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004976 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4977 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4978}
4979
Anthony Liguori25c4c272007-04-27 09:29:21 +03004980static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004981{
Avi Kivityfc78f512009-12-07 12:16:48 +02004982 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4983
4984 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4985 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004986}
4987
Sheng Yang14394422008-04-28 12:24:45 +08004988static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4989{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004990 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4991
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004992 if (!test_bit(VCPU_EXREG_PDPTR,
4993 (unsigned long *)&vcpu->arch.regs_dirty))
4994 return;
4995
Sheng Yang14394422008-04-28 12:24:45 +08004996 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004997 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4998 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4999 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5000 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005001 }
5002}
5003
Avi Kivity8f5d5492009-05-31 18:41:29 +03005004static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5005{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005006 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5007
Avi Kivity8f5d5492009-05-31 18:41:29 +03005008 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005009 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5010 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5011 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5012 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005013 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005014
5015 __set_bit(VCPU_EXREG_PDPTR,
5016 (unsigned long *)&vcpu->arch.regs_avail);
5017 __set_bit(VCPU_EXREG_PDPTR,
5018 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005019}
5020
David Matlack38991522016-11-29 18:14:08 -08005021static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5022{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005023 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5024 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005025 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5026
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005027 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005028 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5029 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5030 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5031
5032 return fixed_bits_valid(val, fixed0, fixed1);
5033}
5034
5035static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5036{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005037 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5038 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005039
5040 return fixed_bits_valid(val, fixed0, fixed1);
5041}
5042
5043static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5044{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005045 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5046 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005047
5048 return fixed_bits_valid(val, fixed0, fixed1);
5049}
5050
5051/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5052#define nested_guest_cr4_valid nested_cr4_valid
5053#define nested_host_cr4_valid nested_cr4_valid
5054
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005055static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005056
5057static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5058 unsigned long cr0,
5059 struct kvm_vcpu *vcpu)
5060{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005061 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5062 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005063 if (!(cr0 & X86_CR0_PG)) {
5064 /* From paging/starting to nonpaging */
5065 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005066 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005067 (CPU_BASED_CR3_LOAD_EXITING |
5068 CPU_BASED_CR3_STORE_EXITING));
5069 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005070 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005071 } else if (!is_paging(vcpu)) {
5072 /* From nonpaging to paging */
5073 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005074 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005075 ~(CPU_BASED_CR3_LOAD_EXITING |
5076 CPU_BASED_CR3_STORE_EXITING));
5077 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005078 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005079 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005080
5081 if (!(cr0 & X86_CR0_WP))
5082 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005083}
5084
Avi Kivity6aa8b732006-12-10 02:21:36 -08005085static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5086{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005087 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005088 unsigned long hw_cr0;
5089
Gleb Natapov50378782013-02-04 16:00:28 +02005090 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005091 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005092 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005093 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005094 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005095
Gleb Natapov218e7632013-01-21 15:36:45 +02005096 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5097 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098
Gleb Natapov218e7632013-01-21 15:36:45 +02005099 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5100 enter_rmode(vcpu);
5101 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005102
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005103#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005104 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005105 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005107 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005108 exit_lmode(vcpu);
5109 }
5110#endif
5111
Sean Christophersonb4d18512018-03-05 12:04:40 -08005112 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005113 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5114
Avi Kivity6aa8b732006-12-10 02:21:36 -08005115 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005116 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005117 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005118
5119 /* depends on vcpu->arch.cr0 to be set to a new value */
5120 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005121}
5122
Yu Zhang855feb62017-08-24 20:27:55 +08005123static int get_ept_level(struct kvm_vcpu *vcpu)
5124{
5125 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5126 return 5;
5127 return 4;
5128}
5129
Peter Feiner995f00a2017-06-30 17:26:32 -07005130static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005131{
Yu Zhang855feb62017-08-24 20:27:55 +08005132 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005133
Yu Zhang855feb62017-08-24 20:27:55 +08005134 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005135
Peter Feiner995f00a2017-06-30 17:26:32 -07005136 if (enable_ept_ad_bits &&
5137 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005138 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005139 eptp |= (root_hpa & PAGE_MASK);
5140
5141 return eptp;
5142}
5143
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5145{
Sheng Yang14394422008-04-28 12:24:45 +08005146 unsigned long guest_cr3;
5147 u64 eptp;
5148
5149 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005150 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005151 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005152 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005153 if (enable_unrestricted_guest || is_paging(vcpu) ||
5154 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005155 guest_cr3 = kvm_read_cr3(vcpu);
5156 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005157 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005158 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005159 }
5160
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005161 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005162 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163}
5164
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005165static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005167 /*
5168 * Pass through host's Machine Check Enable value to hw_cr4, which
5169 * is in force while we are in guest mode. Do not let guests control
5170 * this bit, even if host CR4.MCE == 0.
5171 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005172 unsigned long hw_cr4;
5173
5174 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5175 if (enable_unrestricted_guest)
5176 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5177 else if (to_vmx(vcpu)->rmode.vm86_active)
5178 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5179 else
5180 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005181
Sean Christopherson64f7a112018-04-30 10:01:06 -07005182 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5183 if (cr4 & X86_CR4_UMIP) {
5184 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005185 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005186 hw_cr4 &= ~X86_CR4_UMIP;
5187 } else if (!is_guest_mode(vcpu) ||
5188 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5189 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5190 SECONDARY_EXEC_DESC);
5191 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005192
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005193 if (cr4 & X86_CR4_VMXE) {
5194 /*
5195 * To use VMXON (and later other VMX instructions), a guest
5196 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5197 * So basically the check on whether to allow nested VMX
5198 * is here.
5199 */
5200 if (!nested_vmx_allowed(vcpu))
5201 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005202 }
David Matlack38991522016-11-29 18:14:08 -08005203
5204 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005205 return 1;
5206
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005207 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005208
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005209 if (!enable_unrestricted_guest) {
5210 if (enable_ept) {
5211 if (!is_paging(vcpu)) {
5212 hw_cr4 &= ~X86_CR4_PAE;
5213 hw_cr4 |= X86_CR4_PSE;
5214 } else if (!(cr4 & X86_CR4_PAE)) {
5215 hw_cr4 &= ~X86_CR4_PAE;
5216 }
5217 }
5218
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005219 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005220 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5221 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5222 * to be manually disabled when guest switches to non-paging
5223 * mode.
5224 *
5225 * If !enable_unrestricted_guest, the CPU is always running
5226 * with CR0.PG=1 and CR4 needs to be modified.
5227 * If enable_unrestricted_guest, the CPU automatically
5228 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005229 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005230 if (!is_paging(vcpu))
5231 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5232 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005233
Sheng Yang14394422008-04-28 12:24:45 +08005234 vmcs_writel(CR4_READ_SHADOW, cr4);
5235 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005236 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005237}
5238
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239static void vmx_get_segment(struct kvm_vcpu *vcpu,
5240 struct kvm_segment *var, int seg)
5241{
Avi Kivitya9179492011-01-03 14:28:52 +02005242 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005243 u32 ar;
5244
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005245 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005246 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005247 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005248 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005249 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005250 var->base = vmx_read_guest_seg_base(vmx, seg);
5251 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5252 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005253 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005254 var->base = vmx_read_guest_seg_base(vmx, seg);
5255 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5256 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5257 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005258 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259 var->type = ar & 15;
5260 var->s = (ar >> 4) & 1;
5261 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005262 /*
5263 * Some userspaces do not preserve unusable property. Since usable
5264 * segment has to be present according to VMX spec we can use present
5265 * property to amend userspace bug by making unusable segment always
5266 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5267 * segment as unusable.
5268 */
5269 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270 var->avl = (ar >> 12) & 1;
5271 var->l = (ar >> 13) & 1;
5272 var->db = (ar >> 14) & 1;
5273 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274}
5275
Avi Kivitya9179492011-01-03 14:28:52 +02005276static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5277{
Avi Kivitya9179492011-01-03 14:28:52 +02005278 struct kvm_segment s;
5279
5280 if (to_vmx(vcpu)->rmode.vm86_active) {
5281 vmx_get_segment(vcpu, &s, seg);
5282 return s.base;
5283 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005284 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005285}
5286
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005287static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005288{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005289 struct vcpu_vmx *vmx = to_vmx(vcpu);
5290
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005291 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005292 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005293 else {
5294 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005295 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005296 }
Avi Kivity69c73022011-03-07 15:26:44 +02005297}
5298
Avi Kivity653e3102007-05-07 10:55:37 +03005299static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005300{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301 u32 ar;
5302
Avi Kivityf0495f92012-06-07 17:06:10 +03005303 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304 ar = 1 << 16;
5305 else {
5306 ar = var->type & 15;
5307 ar |= (var->s & 1) << 4;
5308 ar |= (var->dpl & 3) << 5;
5309 ar |= (var->present & 1) << 7;
5310 ar |= (var->avl & 1) << 12;
5311 ar |= (var->l & 1) << 13;
5312 ar |= (var->db & 1) << 14;
5313 ar |= (var->g & 1) << 15;
5314 }
Avi Kivity653e3102007-05-07 10:55:37 +03005315
5316 return ar;
5317}
5318
5319static void vmx_set_segment(struct kvm_vcpu *vcpu,
5320 struct kvm_segment *var, int seg)
5321{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005322 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005323 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005324
Avi Kivity2fb92db2011-04-27 19:42:18 +03005325 vmx_segment_cache_clear(vmx);
5326
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005327 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5328 vmx->rmode.segs[seg] = *var;
5329 if (seg == VCPU_SREG_TR)
5330 vmcs_write16(sf->selector, var->selector);
5331 else if (var->s)
5332 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005333 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005334 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005335
Avi Kivity653e3102007-05-07 10:55:37 +03005336 vmcs_writel(sf->base, var->base);
5337 vmcs_write32(sf->limit, var->limit);
5338 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005339
5340 /*
5341 * Fix the "Accessed" bit in AR field of segment registers for older
5342 * qemu binaries.
5343 * IA32 arch specifies that at the time of processor reset the
5344 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005345 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005346 * state vmexit when "unrestricted guest" mode is turned on.
5347 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5348 * tree. Newer qemu binaries with that qemu fix would not need this
5349 * kvm hack.
5350 */
5351 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005352 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005353
Gleb Natapovf924d662012-12-12 19:10:55 +02005354 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005355
5356out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005357 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005358}
5359
Avi Kivity6aa8b732006-12-10 02:21:36 -08005360static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5361{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005362 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005363
5364 *db = (ar >> 14) & 1;
5365 *l = (ar >> 13) & 1;
5366}
5367
Gleb Natapov89a27f42010-02-16 10:51:48 +02005368static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005370 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5371 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005372}
5373
Gleb Natapov89a27f42010-02-16 10:51:48 +02005374static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005375{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005376 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5377 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005378}
5379
Gleb Natapov89a27f42010-02-16 10:51:48 +02005380static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005381{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005382 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5383 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005384}
5385
Gleb Natapov89a27f42010-02-16 10:51:48 +02005386static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005388 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5389 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390}
5391
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005392static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5393{
5394 struct kvm_segment var;
5395 u32 ar;
5396
5397 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005398 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005399 if (seg == VCPU_SREG_CS)
5400 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005401 ar = vmx_segment_access_rights(&var);
5402
5403 if (var.base != (var.selector << 4))
5404 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005405 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005406 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005407 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005408 return false;
5409
5410 return true;
5411}
5412
5413static bool code_segment_valid(struct kvm_vcpu *vcpu)
5414{
5415 struct kvm_segment cs;
5416 unsigned int cs_rpl;
5417
5418 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005419 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005420
Avi Kivity1872a3f2009-01-04 23:26:52 +02005421 if (cs.unusable)
5422 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005423 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005424 return false;
5425 if (!cs.s)
5426 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005427 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005428 if (cs.dpl > cs_rpl)
5429 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005430 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005431 if (cs.dpl != cs_rpl)
5432 return false;
5433 }
5434 if (!cs.present)
5435 return false;
5436
5437 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5438 return true;
5439}
5440
5441static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5442{
5443 struct kvm_segment ss;
5444 unsigned int ss_rpl;
5445
5446 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005447 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005448
Avi Kivity1872a3f2009-01-04 23:26:52 +02005449 if (ss.unusable)
5450 return true;
5451 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005452 return false;
5453 if (!ss.s)
5454 return false;
5455 if (ss.dpl != ss_rpl) /* DPL != RPL */
5456 return false;
5457 if (!ss.present)
5458 return false;
5459
5460 return true;
5461}
5462
5463static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5464{
5465 struct kvm_segment var;
5466 unsigned int rpl;
5467
5468 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005469 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005470
Avi Kivity1872a3f2009-01-04 23:26:52 +02005471 if (var.unusable)
5472 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005473 if (!var.s)
5474 return false;
5475 if (!var.present)
5476 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005477 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005478 if (var.dpl < rpl) /* DPL < RPL */
5479 return false;
5480 }
5481
5482 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5483 * rights flags
5484 */
5485 return true;
5486}
5487
5488static bool tr_valid(struct kvm_vcpu *vcpu)
5489{
5490 struct kvm_segment tr;
5491
5492 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5493
Avi Kivity1872a3f2009-01-04 23:26:52 +02005494 if (tr.unusable)
5495 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005496 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005497 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005498 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005499 return false;
5500 if (!tr.present)
5501 return false;
5502
5503 return true;
5504}
5505
5506static bool ldtr_valid(struct kvm_vcpu *vcpu)
5507{
5508 struct kvm_segment ldtr;
5509
5510 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5511
Avi Kivity1872a3f2009-01-04 23:26:52 +02005512 if (ldtr.unusable)
5513 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005514 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005515 return false;
5516 if (ldtr.type != 2)
5517 return false;
5518 if (!ldtr.present)
5519 return false;
5520
5521 return true;
5522}
5523
5524static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5525{
5526 struct kvm_segment cs, ss;
5527
5528 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5529 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5530
Nadav Amitb32a9912015-03-29 16:33:04 +03005531 return ((cs.selector & SEGMENT_RPL_MASK) ==
5532 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005533}
5534
5535/*
5536 * Check if guest state is valid. Returns true if valid, false if
5537 * not.
5538 * We assume that registers are always usable
5539 */
5540static bool guest_state_valid(struct kvm_vcpu *vcpu)
5541{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005542 if (enable_unrestricted_guest)
5543 return true;
5544
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005545 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005546 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005547 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5548 return false;
5549 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5550 return false;
5551 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5552 return false;
5553 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5554 return false;
5555 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5556 return false;
5557 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5558 return false;
5559 } else {
5560 /* protected mode guest state checks */
5561 if (!cs_ss_rpl_check(vcpu))
5562 return false;
5563 if (!code_segment_valid(vcpu))
5564 return false;
5565 if (!stack_segment_valid(vcpu))
5566 return false;
5567 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5568 return false;
5569 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5570 return false;
5571 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5572 return false;
5573 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5574 return false;
5575 if (!tr_valid(vcpu))
5576 return false;
5577 if (!ldtr_valid(vcpu))
5578 return false;
5579 }
5580 /* TODO:
5581 * - Add checks on RIP
5582 * - Add checks on RFLAGS
5583 */
5584
5585 return true;
5586}
5587
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005588static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5589{
5590 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5591}
5592
Mike Dayd77c26f2007-10-08 09:02:08 -04005593static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005594{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005595 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005596 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005597 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005598
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005599 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005600 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005601 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5602 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005603 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005604 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005605 r = kvm_write_guest_page(kvm, fn++, &data,
5606 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005607 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005608 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005609 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5610 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005611 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005612 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5613 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005614 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005615 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005616 r = kvm_write_guest_page(kvm, fn, &data,
5617 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5618 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005619out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005620 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005621 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005622}
5623
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005624static int init_rmode_identity_map(struct kvm *kvm)
5625{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005626 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005627 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005628 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005629 u32 tmp;
5630
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005631 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005632 mutex_lock(&kvm->slots_lock);
5633
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005634 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005635 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005636
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005637 if (!kvm_vmx->ept_identity_map_addr)
5638 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5639 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005640
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005641 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005642 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005643 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005644 goto out2;
5645
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005646 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005647 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5648 if (r < 0)
5649 goto out;
5650 /* Set up identity-mapping pagetable for EPT in real mode */
5651 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5652 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5653 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5654 r = kvm_write_guest_page(kvm, identity_map_pfn,
5655 &tmp, i * sizeof(tmp), sizeof(tmp));
5656 if (r < 0)
5657 goto out;
5658 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005659 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005660
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005661out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005662 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005663
5664out2:
5665 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005666 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005667}
5668
Avi Kivity6aa8b732006-12-10 02:21:36 -08005669static void seg_setup(int seg)
5670{
Mathias Krause772e0312012-08-30 01:30:19 +02005671 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005672 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005673
5674 vmcs_write16(sf->selector, 0);
5675 vmcs_writel(sf->base, 0);
5676 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005677 ar = 0x93;
5678 if (seg == VCPU_SREG_CS)
5679 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005680
5681 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005682}
5683
Sheng Yangf78e0e22007-10-29 09:40:42 +08005684static int alloc_apic_access_page(struct kvm *kvm)
5685{
Xiao Guangrong44841412012-09-07 14:14:20 +08005686 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005687 int r = 0;
5688
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005689 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005690 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005691 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005692 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5693 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005694 if (r)
5695 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005696
Tang Chen73a6d942014-09-11 13:38:00 +08005697 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005698 if (is_error_page(page)) {
5699 r = -EFAULT;
5700 goto out;
5701 }
5702
Tang Chenc24ae0d2014-09-24 15:57:58 +08005703 /*
5704 * Do not pin the page in memory, so that memory hot-unplug
5705 * is able to migrate it.
5706 */
5707 put_page(page);
5708 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005709out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005710 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005711 return r;
5712}
5713
Wanpeng Li991e7a02015-09-16 17:30:05 +08005714static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005715{
5716 int vpid;
5717
Avi Kivity919818a2009-03-23 18:01:29 +02005718 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005719 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005720 spin_lock(&vmx_vpid_lock);
5721 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005722 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005723 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005724 else
5725 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005726 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005727 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005728}
5729
Wanpeng Li991e7a02015-09-16 17:30:05 +08005730static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005731{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005732 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005733 return;
5734 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005735 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005736 spin_unlock(&vmx_vpid_lock);
5737}
5738
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005739static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5740 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005741{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005742 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005743
5744 if (!cpu_has_vmx_msr_bitmap())
5745 return;
5746
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005747 if (static_branch_unlikely(&enable_evmcs))
5748 evmcs_touch_msr_bitmap();
5749
Sheng Yang25c5f222008-03-28 13:18:56 +08005750 /*
5751 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5752 * have the write-low and read-high bitmap offsets the wrong way round.
5753 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5754 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005755 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005756 if (type & MSR_TYPE_R)
5757 /* read-low */
5758 __clear_bit(msr, msr_bitmap + 0x000 / f);
5759
5760 if (type & MSR_TYPE_W)
5761 /* write-low */
5762 __clear_bit(msr, msr_bitmap + 0x800 / f);
5763
Sheng Yang25c5f222008-03-28 13:18:56 +08005764 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5765 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005766 if (type & MSR_TYPE_R)
5767 /* read-high */
5768 __clear_bit(msr, msr_bitmap + 0x400 / f);
5769
5770 if (type & MSR_TYPE_W)
5771 /* write-high */
5772 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5773
5774 }
5775}
5776
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005777static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5778 u32 msr, int type)
5779{
5780 int f = sizeof(unsigned long);
5781
5782 if (!cpu_has_vmx_msr_bitmap())
5783 return;
5784
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005785 if (static_branch_unlikely(&enable_evmcs))
5786 evmcs_touch_msr_bitmap();
5787
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005788 /*
5789 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5790 * have the write-low and read-high bitmap offsets the wrong way round.
5791 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5792 */
5793 if (msr <= 0x1fff) {
5794 if (type & MSR_TYPE_R)
5795 /* read-low */
5796 __set_bit(msr, msr_bitmap + 0x000 / f);
5797
5798 if (type & MSR_TYPE_W)
5799 /* write-low */
5800 __set_bit(msr, msr_bitmap + 0x800 / f);
5801
5802 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5803 msr &= 0x1fff;
5804 if (type & MSR_TYPE_R)
5805 /* read-high */
5806 __set_bit(msr, msr_bitmap + 0x400 / f);
5807
5808 if (type & MSR_TYPE_W)
5809 /* write-high */
5810 __set_bit(msr, msr_bitmap + 0xc00 / f);
5811
5812 }
5813}
5814
5815static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5816 u32 msr, int type, bool value)
5817{
5818 if (value)
5819 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5820 else
5821 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5822}
5823
Wincy Vanf2b93282015-02-03 23:56:03 +08005824/*
5825 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5826 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5827 */
5828static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5829 unsigned long *msr_bitmap_nested,
5830 u32 msr, int type)
5831{
5832 int f = sizeof(unsigned long);
5833
Wincy Vanf2b93282015-02-03 23:56:03 +08005834 /*
5835 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5836 * have the write-low and read-high bitmap offsets the wrong way round.
5837 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5838 */
5839 if (msr <= 0x1fff) {
5840 if (type & MSR_TYPE_R &&
5841 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5842 /* read-low */
5843 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5844
5845 if (type & MSR_TYPE_W &&
5846 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5847 /* write-low */
5848 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5849
5850 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5851 msr &= 0x1fff;
5852 if (type & MSR_TYPE_R &&
5853 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5854 /* read-high */
5855 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5856
5857 if (type & MSR_TYPE_W &&
5858 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5859 /* write-high */
5860 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5861
5862 }
5863}
5864
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005865static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005866{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005867 u8 mode = 0;
5868
5869 if (cpu_has_secondary_exec_ctrls() &&
5870 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5871 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5872 mode |= MSR_BITMAP_MODE_X2APIC;
5873 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5874 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5875 }
5876
5877 if (is_long_mode(vcpu))
5878 mode |= MSR_BITMAP_MODE_LM;
5879
5880 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005881}
5882
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005883#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5884
5885static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5886 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005887{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005888 int msr;
5889
5890 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5891 unsigned word = msr / BITS_PER_LONG;
5892 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5893 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005894 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005895
5896 if (mode & MSR_BITMAP_MODE_X2APIC) {
5897 /*
5898 * TPR reads and writes can be virtualized even if virtual interrupt
5899 * delivery is not in use.
5900 */
5901 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5902 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5903 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5904 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5905 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5906 }
5907 }
5908}
5909
5910static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5911{
5912 struct vcpu_vmx *vmx = to_vmx(vcpu);
5913 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5914 u8 mode = vmx_msr_bitmap_mode(vcpu);
5915 u8 changed = mode ^ vmx->msr_bitmap_mode;
5916
5917 if (!changed)
5918 return;
5919
5920 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5921 !(mode & MSR_BITMAP_MODE_LM));
5922
5923 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5924 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5925
5926 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005927}
5928
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005929static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005930{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005931 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005932}
5933
David Matlackc9f04402017-08-01 14:00:40 -07005934static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5935{
5936 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5937 gfn_t gfn;
5938
5939 /*
5940 * Don't need to mark the APIC access page dirty; it is never
5941 * written to by the CPU during APIC virtualization.
5942 */
5943
5944 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5945 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5946 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5947 }
5948
5949 if (nested_cpu_has_posted_intr(vmcs12)) {
5950 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5951 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5952 }
5953}
5954
5955
David Hildenbrand6342c502017-01-25 11:58:58 +01005956static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005957{
5958 struct vcpu_vmx *vmx = to_vmx(vcpu);
5959 int max_irr;
5960 void *vapic_page;
5961 u16 status;
5962
David Matlackc9f04402017-08-01 14:00:40 -07005963 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5964 return;
Wincy Van705699a2015-02-03 23:58:17 +08005965
David Matlackc9f04402017-08-01 14:00:40 -07005966 vmx->nested.pi_pending = false;
5967 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5968 return;
Wincy Van705699a2015-02-03 23:58:17 +08005969
David Matlackc9f04402017-08-01 14:00:40 -07005970 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5971 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005972 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005973 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5974 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005975 kunmap(vmx->nested.virtual_apic_page);
5976
5977 status = vmcs_read16(GUEST_INTR_STATUS);
5978 if ((u8)max_irr > ((u8)status & 0xff)) {
5979 status &= ~0xff;
5980 status |= (u8)max_irr;
5981 vmcs_write16(GUEST_INTR_STATUS, status);
5982 }
5983 }
David Matlackc9f04402017-08-01 14:00:40 -07005984
5985 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005986}
5987
Wincy Van06a55242017-04-28 13:13:59 +08005988static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5989 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005990{
5991#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005992 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5993
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005994 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005995 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005996 * The vector of interrupt to be delivered to vcpu had
5997 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005998 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005999 * Following cases will be reached in this block, and
6000 * we always send a notification event in all cases as
6001 * explained below.
6002 *
6003 * Case 1: vcpu keeps in non-root mode. Sending a
6004 * notification event posts the interrupt to vcpu.
6005 *
6006 * Case 2: vcpu exits to root mode and is still
6007 * runnable. PIR will be synced to vIRR before the
6008 * next vcpu entry. Sending a notification event in
6009 * this case has no effect, as vcpu is not in root
6010 * mode.
6011 *
6012 * Case 3: vcpu exits to root mode and is blocked.
6013 * vcpu_block() has already synced PIR to vIRR and
6014 * never blocks vcpu if vIRR is not cleared. Therefore,
6015 * a blocked vcpu here does not wait for any requested
6016 * interrupts in PIR, and sending a notification event
6017 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006018 */
Feng Wu28b835d2015-09-18 22:29:54 +08006019
Wincy Van06a55242017-04-28 13:13:59 +08006020 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006021 return true;
6022 }
6023#endif
6024 return false;
6025}
6026
Wincy Van705699a2015-02-03 23:58:17 +08006027static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6028 int vector)
6029{
6030 struct vcpu_vmx *vmx = to_vmx(vcpu);
6031
6032 if (is_guest_mode(vcpu) &&
6033 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006034 /*
6035 * If a posted intr is not recognized by hardware,
6036 * we will accomplish it in the next vmentry.
6037 */
6038 vmx->nested.pi_pending = true;
6039 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006040 /* the PIR and ON have been set by L1. */
6041 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6042 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006043 return 0;
6044 }
6045 return -1;
6046}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006047/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006048 * Send interrupt to vcpu via posted interrupt way.
6049 * 1. If target vcpu is running(non-root mode), send posted interrupt
6050 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6051 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6052 * interrupt from PIR in next vmentry.
6053 */
6054static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6055{
6056 struct vcpu_vmx *vmx = to_vmx(vcpu);
6057 int r;
6058
Wincy Van705699a2015-02-03 23:58:17 +08006059 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6060 if (!r)
6061 return;
6062
Yang Zhanga20ed542013-04-11 19:25:15 +08006063 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6064 return;
6065
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006066 /* If a previous notification has sent the IPI, nothing to do. */
6067 if (pi_test_and_set_on(&vmx->pi_desc))
6068 return;
6069
Wincy Van06a55242017-04-28 13:13:59 +08006070 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006071 kvm_vcpu_kick(vcpu);
6072}
6073
Avi Kivity6aa8b732006-12-10 02:21:36 -08006074/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006075 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6076 * will not change in the lifetime of the guest.
6077 * Note that host-state that does change is set elsewhere. E.g., host-state
6078 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6079 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006080static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006081{
6082 u32 low32, high32;
6083 unsigned long tmpl;
6084 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006085 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006086
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006087 cr0 = read_cr0();
6088 WARN_ON(cr0 & X86_CR0_TS);
6089 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006090
6091 /*
6092 * Save the most likely value for this task's CR3 in the VMCS.
6093 * We can't use __get_current_cr3_fast() because we're not atomic.
6094 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006095 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006096 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006097 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006098
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006099 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006100 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006101 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006102 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006103
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006104 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006105#ifdef CONFIG_X86_64
6106 /*
6107 * Load null selectors, so we can avoid reloading them in
6108 * __vmx_load_host_state(), in case userspace uses the null selectors
6109 * too (the expected case).
6110 */
6111 vmcs_write16(HOST_DS_SELECTOR, 0);
6112 vmcs_write16(HOST_ES_SELECTOR, 0);
6113#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006114 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6115 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006116#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006117 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6118 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6119
Juergen Gross87930012017-09-04 12:25:27 +02006120 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006121 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006122 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006123
Avi Kivity83287ea422012-09-16 15:10:57 +03006124 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006125
6126 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6127 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6128 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6129 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6130
6131 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6132 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6133 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6134 }
6135}
6136
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006137static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6138{
6139 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6140 if (enable_ept)
6141 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006142 if (is_guest_mode(&vmx->vcpu))
6143 vmx->vcpu.arch.cr4_guest_owned_bits &=
6144 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006145 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6146}
6147
Yang Zhang01e439b2013-04-11 19:25:12 +08006148static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6149{
6150 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6151
Andrey Smetanind62caab2015-11-10 15:36:33 +03006152 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006153 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006154
6155 if (!enable_vnmi)
6156 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6157
Yunhong Jiang64672c92016-06-13 14:19:59 -07006158 /* Enable the preemption timer dynamically */
6159 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006160 return pin_based_exec_ctrl;
6161}
6162
Andrey Smetanind62caab2015-11-10 15:36:33 +03006163static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6164{
6165 struct vcpu_vmx *vmx = to_vmx(vcpu);
6166
6167 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006168 if (cpu_has_secondary_exec_ctrls()) {
6169 if (kvm_vcpu_apicv_active(vcpu))
6170 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6171 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6172 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6173 else
6174 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6175 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6176 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6177 }
6178
6179 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006180 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006181}
6182
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006183static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6184{
6185 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006186
6187 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6188 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6189
Paolo Bonzini35754c92015-07-29 12:05:37 +02006190 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006191 exec_control &= ~CPU_BASED_TPR_SHADOW;
6192#ifdef CONFIG_X86_64
6193 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6194 CPU_BASED_CR8_LOAD_EXITING;
6195#endif
6196 }
6197 if (!enable_ept)
6198 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6199 CPU_BASED_CR3_LOAD_EXITING |
6200 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006201 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6202 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6203 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006204 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6205 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006206 return exec_control;
6207}
6208
Jim Mattson45ec3682017-08-23 16:32:04 -07006209static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006210{
Jim Mattson45ec3682017-08-23 16:32:04 -07006211 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006212 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006213}
6214
Jim Mattson75f4fc82017-08-23 16:32:03 -07006215static bool vmx_rdseed_supported(void)
6216{
6217 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006218 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006219}
6220
Paolo Bonzini80154d72017-08-24 13:55:35 +02006221static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006222{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006223 struct kvm_vcpu *vcpu = &vmx->vcpu;
6224
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006225 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006226
Paolo Bonzini80154d72017-08-24 13:55:35 +02006227 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006228 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6229 if (vmx->vpid == 0)
6230 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6231 if (!enable_ept) {
6232 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6233 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006234 /* Enable INVPCID for non-ept guests may cause performance regression. */
6235 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006236 }
6237 if (!enable_unrestricted_guest)
6238 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006239 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006240 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006241 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006242 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6243 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006244 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006245
6246 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6247 * in vmx_set_cr4. */
6248 exec_control &= ~SECONDARY_EXEC_DESC;
6249
Abel Gordonabc4fc52013-04-18 14:35:25 +03006250 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6251 (handle_vmptrld).
6252 We can NOT enable shadow_vmcs here because we don't have yet
6253 a current VMCS12
6254 */
6255 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006256
6257 if (!enable_pml)
6258 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006259
Paolo Bonzini3db13482017-08-24 14:48:03 +02006260 if (vmx_xsaves_supported()) {
6261 /* Exposing XSAVES only when XSAVE is exposed */
6262 bool xsaves_enabled =
6263 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6264 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6265
6266 if (!xsaves_enabled)
6267 exec_control &= ~SECONDARY_EXEC_XSAVES;
6268
6269 if (nested) {
6270 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006271 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006272 SECONDARY_EXEC_XSAVES;
6273 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006274 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006275 ~SECONDARY_EXEC_XSAVES;
6276 }
6277 }
6278
Paolo Bonzini80154d72017-08-24 13:55:35 +02006279 if (vmx_rdtscp_supported()) {
6280 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6281 if (!rdtscp_enabled)
6282 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6283
6284 if (nested) {
6285 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006286 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006287 SECONDARY_EXEC_RDTSCP;
6288 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006289 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006290 ~SECONDARY_EXEC_RDTSCP;
6291 }
6292 }
6293
6294 if (vmx_invpcid_supported()) {
6295 /* Exposing INVPCID only when PCID is exposed */
6296 bool invpcid_enabled =
6297 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6298 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6299
6300 if (!invpcid_enabled) {
6301 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6302 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6303 }
6304
6305 if (nested) {
6306 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006307 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006308 SECONDARY_EXEC_ENABLE_INVPCID;
6309 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006310 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006311 ~SECONDARY_EXEC_ENABLE_INVPCID;
6312 }
6313 }
6314
Jim Mattson45ec3682017-08-23 16:32:04 -07006315 if (vmx_rdrand_supported()) {
6316 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6317 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006318 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006319
6320 if (nested) {
6321 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006322 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006323 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006324 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006325 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006326 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006327 }
6328 }
6329
Jim Mattson75f4fc82017-08-23 16:32:03 -07006330 if (vmx_rdseed_supported()) {
6331 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6332 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006333 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006334
6335 if (nested) {
6336 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006337 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006338 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006339 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006340 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006341 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006342 }
6343 }
6344
Paolo Bonzini80154d72017-08-24 13:55:35 +02006345 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006346}
6347
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006348static void ept_set_mmio_spte_mask(void)
6349{
6350 /*
6351 * EPT Misconfigurations can be generated if the value of bits 2:0
6352 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006353 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006354 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6355 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006356}
6357
Wanpeng Lif53cd632014-12-02 19:14:58 +08006358#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006359/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006360 * Sets up the vmcs for emulated real mode.
6361 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006362static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006363{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006364#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006365 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006366#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006367 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368
Abel Gordon4607c2d2013-04-18 14:35:55 +03006369 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006370 /*
6371 * At vCPU creation, "VMWRITE to any supported field
6372 * in the VMCS" is supported, so use the more
6373 * permissive vmx_vmread_bitmap to specify both read
6374 * and write permissions for the shadow VMCS.
6375 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006376 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006377 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006378 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006379 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006380 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006381
Avi Kivity6aa8b732006-12-10 02:21:36 -08006382 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6383
Avi Kivity6aa8b732006-12-10 02:21:36 -08006384 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006385 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006386 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006387
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006388 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006389
Dan Williamsdfa169b2016-06-02 11:17:24 -07006390 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006391 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006392 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006393 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006394 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006395
Andrey Smetanind62caab2015-11-10 15:36:33 +03006396 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006397 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6398 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6399 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6400 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6401
6402 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006403
Li RongQing0bcf2612015-12-03 13:29:34 +08006404 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006405 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006406 }
6407
Wanpeng Lib31c1142018-03-12 04:53:04 -07006408 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006409 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006410 vmx->ple_window = ple_window;
6411 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006412 }
6413
Xiao Guangrongc3707952011-07-12 03:28:04 +08006414 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6415 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6417
Avi Kivity9581d442010-10-19 16:46:55 +02006418 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6419 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006420 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006421#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006422 rdmsrl(MSR_FS_BASE, a);
6423 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6424 rdmsrl(MSR_GS_BASE, a);
6425 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6426#else
6427 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6428 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6429#endif
6430
Bandan Das2a499e42017-08-03 15:54:41 -04006431 if (cpu_has_vmx_vmfunc())
6432 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6433
Eddie Dong2cc51562007-05-21 07:28:09 +03006434 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6435 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006436 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006437 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006438 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006439
Radim Krčmář74545702015-04-27 15:11:25 +02006440 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6441 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006442
Paolo Bonzini03916db2014-07-24 14:21:57 +02006443 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006444 u32 index = vmx_msr_index[i];
6445 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006446 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006447
6448 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6449 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006450 if (wrmsr_safe(index, data_low, data_high) < 0)
6451 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006452 vmx->guest_msrs[j].index = i;
6453 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006454 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006455 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006456 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006457
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006458 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6459 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006460
6461 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006462
6463 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006464 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006465
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006466 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6467 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6468
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006469 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006470
Wanpeng Lif53cd632014-12-02 19:14:58 +08006471 if (vmx_xsaves_supported())
6472 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6473
Peter Feiner4e595162016-07-07 14:49:58 -07006474 if (enable_pml) {
6475 ASSERT(vmx->pml_pg);
6476 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6477 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6478 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006479}
6480
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006481static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006482{
6483 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006484 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006485 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006486
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006487 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006488 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006489
Wanpeng Li518e7b92018-02-28 14:03:31 +08006490 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006491 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006492 kvm_set_cr8(vcpu, 0);
6493
6494 if (!init_event) {
6495 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6496 MSR_IA32_APICBASE_ENABLE;
6497 if (kvm_vcpu_is_reset_bsp(vcpu))
6498 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6499 apic_base_msr.host_initiated = true;
6500 kvm_set_apic_base(vcpu, &apic_base_msr);
6501 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006502
Avi Kivity2fb92db2011-04-27 19:42:18 +03006503 vmx_segment_cache_clear(vmx);
6504
Avi Kivity5706be02008-08-20 15:07:31 +03006505 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006506 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006507 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006508
6509 seg_setup(VCPU_SREG_DS);
6510 seg_setup(VCPU_SREG_ES);
6511 seg_setup(VCPU_SREG_FS);
6512 seg_setup(VCPU_SREG_GS);
6513 seg_setup(VCPU_SREG_SS);
6514
6515 vmcs_write16(GUEST_TR_SELECTOR, 0);
6516 vmcs_writel(GUEST_TR_BASE, 0);
6517 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6518 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6519
6520 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6521 vmcs_writel(GUEST_LDTR_BASE, 0);
6522 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6523 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6524
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006525 if (!init_event) {
6526 vmcs_write32(GUEST_SYSENTER_CS, 0);
6527 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6528 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6529 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6530 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006531
Wanpeng Lic37c2872017-11-20 14:52:21 -08006532 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006533 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006534
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006535 vmcs_writel(GUEST_GDTR_BASE, 0);
6536 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6537
6538 vmcs_writel(GUEST_IDTR_BASE, 0);
6539 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6540
Anthony Liguori443381a2010-12-06 10:53:38 -06006541 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006542 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006543 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006544 if (kvm_mpx_supported())
6545 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006546
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006547 setup_msrs(vmx);
6548
Avi Kivity6aa8b732006-12-10 02:21:36 -08006549 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6550
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006551 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006552 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006553 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006554 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006555 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006556 vmcs_write32(TPR_THRESHOLD, 0);
6557 }
6558
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006559 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006560
Sheng Yang2384d2b2008-01-17 15:14:33 +08006561 if (vmx->vpid != 0)
6562 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6563
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006564 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006565 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006566 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006567 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006568 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006569
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006570 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006571
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006572 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006573 if (init_event)
6574 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006575}
6576
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006577/*
6578 * In nested virtualization, check if L1 asked to exit on external interrupts.
6579 * For most existing hypervisors, this will always return true.
6580 */
6581static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6582{
6583 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6584 PIN_BASED_EXT_INTR_MASK;
6585}
6586
Bandan Das77b0f5d2014-04-19 18:17:45 -04006587/*
6588 * In nested virtualization, check if L1 has set
6589 * VM_EXIT_ACK_INTR_ON_EXIT
6590 */
6591static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6592{
6593 return get_vmcs12(vcpu)->vm_exit_controls &
6594 VM_EXIT_ACK_INTR_ON_EXIT;
6595}
6596
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006597static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6598{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006599 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006600}
6601
Jan Kiszkac9a79532014-03-07 20:03:15 +01006602static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006603{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006604 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6605 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006606}
6607
Jan Kiszkac9a79532014-03-07 20:03:15 +01006608static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006609{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006610 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006611 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006612 enable_irq_window(vcpu);
6613 return;
6614 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006615
Paolo Bonzini47c01522016-12-19 11:44:07 +01006616 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6617 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006618}
6619
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006620static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006621{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006622 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006623 uint32_t intr;
6624 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006625
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006626 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006627
Avi Kivityfa89a812008-09-01 15:57:51 +03006628 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006629 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006630 int inc_eip = 0;
6631 if (vcpu->arch.interrupt.soft)
6632 inc_eip = vcpu->arch.event_exit_inst_len;
6633 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006634 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006635 return;
6636 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006637 intr = irq | INTR_INFO_VALID_MASK;
6638 if (vcpu->arch.interrupt.soft) {
6639 intr |= INTR_TYPE_SOFT_INTR;
6640 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6641 vmx->vcpu.arch.event_exit_inst_len);
6642 } else
6643 intr |= INTR_TYPE_EXT_INTR;
6644 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006645
6646 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006647}
6648
Sheng Yangf08864b2008-05-15 18:23:25 +08006649static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6650{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006651 struct vcpu_vmx *vmx = to_vmx(vcpu);
6652
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006653 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006654 /*
6655 * Tracking the NMI-blocked state in software is built upon
6656 * finding the next open IRQ window. This, in turn, depends on
6657 * well-behaving guests: They have to keep IRQs disabled at
6658 * least as long as the NMI handler runs. Otherwise we may
6659 * cause NMI nesting, maybe breaking the guest. But as this is
6660 * highly unlikely, we can live with the residual risk.
6661 */
6662 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6663 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6664 }
6665
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006666 ++vcpu->stat.nmi_injections;
6667 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006668
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006669 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006670 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006671 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006672 return;
6673 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006674
Sheng Yangf08864b2008-05-15 18:23:25 +08006675 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6676 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006677
6678 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006679}
6680
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006681static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6682{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006683 struct vcpu_vmx *vmx = to_vmx(vcpu);
6684 bool masked;
6685
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006686 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006687 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006688 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006689 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006690 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6691 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6692 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006693}
6694
6695static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6696{
6697 struct vcpu_vmx *vmx = to_vmx(vcpu);
6698
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006699 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006700 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6701 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6702 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6703 }
6704 } else {
6705 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6706 if (masked)
6707 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6708 GUEST_INTR_STATE_NMI);
6709 else
6710 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6711 GUEST_INTR_STATE_NMI);
6712 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006713}
6714
Jan Kiszka2505dc92013-04-14 12:12:47 +02006715static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6716{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006717 if (to_vmx(vcpu)->nested.nested_run_pending)
6718 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006719
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006720 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006721 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6722 return 0;
6723
Jan Kiszka2505dc92013-04-14 12:12:47 +02006724 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6725 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6726 | GUEST_INTR_STATE_NMI));
6727}
6728
Gleb Natapov78646122009-03-23 12:12:11 +02006729static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6730{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006731 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6732 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006733 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6734 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006735}
6736
Izik Eiduscbc94022007-10-25 00:29:55 +02006737static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6738{
6739 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006740
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006741 if (enable_unrestricted_guest)
6742 return 0;
6743
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006744 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6745 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006746 if (ret)
6747 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006748 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006749 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006750}
6751
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006752static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6753{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006754 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006755 return 0;
6756}
6757
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006758static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006759{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006760 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006761 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006762 /*
6763 * Update instruction length as we may reinject the exception
6764 * from user space while in guest debugging mode.
6765 */
6766 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6767 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006768 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006769 return false;
6770 /* fall through */
6771 case DB_VECTOR:
6772 if (vcpu->guest_debug &
6773 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6774 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006775 /* fall through */
6776 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006777 case OF_VECTOR:
6778 case BR_VECTOR:
6779 case UD_VECTOR:
6780 case DF_VECTOR:
6781 case SS_VECTOR:
6782 case GP_VECTOR:
6783 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006784 return true;
6785 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006786 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006787 return false;
6788}
6789
6790static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6791 int vec, u32 err_code)
6792{
6793 /*
6794 * Instruction with address size override prefix opcode 0x67
6795 * Cause the #SS fault with 0 error code in VM86 mode.
6796 */
6797 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6798 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6799 if (vcpu->arch.halt_request) {
6800 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006801 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006802 }
6803 return 1;
6804 }
6805 return 0;
6806 }
6807
6808 /*
6809 * Forward all other exceptions that are valid in real mode.
6810 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6811 * the required debugging infrastructure rework.
6812 */
6813 kvm_queue_exception(vcpu, vec);
6814 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006815}
6816
Andi Kleena0861c02009-06-08 17:37:09 +08006817/*
6818 * Trigger machine check on the host. We assume all the MSRs are already set up
6819 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6820 * We pass a fake environment to the machine check handler because we want
6821 * the guest to be always treated like user space, no matter what context
6822 * it used internally.
6823 */
6824static void kvm_machine_check(void)
6825{
6826#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6827 struct pt_regs regs = {
6828 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6829 .flags = X86_EFLAGS_IF,
6830 };
6831
6832 do_machine_check(&regs, 0);
6833#endif
6834}
6835
Avi Kivity851ba692009-08-24 11:10:17 +03006836static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006837{
6838 /* already handled by vcpu_run */
6839 return 1;
6840}
6841
Avi Kivity851ba692009-08-24 11:10:17 +03006842static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006843{
Avi Kivity1155f762007-11-22 11:30:47 +02006844 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006845 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006846 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006847 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006848 u32 vect_info;
6849 enum emulation_result er;
6850
Avi Kivity1155f762007-11-22 11:30:47 +02006851 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006852 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006853
Andi Kleena0861c02009-06-08 17:37:09 +08006854 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006855 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006856
Jim Mattsonef85b672016-12-12 11:01:37 -08006857 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006858 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006859
Wanpeng Li082d06e2018-04-03 16:28:48 -07006860 if (is_invalid_opcode(intr_info))
6861 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006862
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006864 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006865 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006866
Liran Alon9e869482018-03-12 13:12:51 +02006867 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6868 WARN_ON_ONCE(!enable_vmware_backdoor);
6869 er = emulate_instruction(vcpu,
6870 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6871 if (er == EMULATE_USER_EXIT)
6872 return 0;
6873 else if (er != EMULATE_DONE)
6874 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6875 return 1;
6876 }
6877
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006878 /*
6879 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6880 * MMIO, it is better to report an internal error.
6881 * See the comments in vmx_handle_exit.
6882 */
6883 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6884 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6885 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6886 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006887 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006888 vcpu->run->internal.data[0] = vect_info;
6889 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006890 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006891 return 0;
6892 }
6893
Avi Kivity6aa8b732006-12-10 02:21:36 -08006894 if (is_page_fault(intr_info)) {
6895 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006896 /* EPT won't cause page fault directly */
6897 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006898 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006899 }
6900
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006901 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006902
6903 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6904 return handle_rmode_exception(vcpu, ex_no, error_code);
6905
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006906 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006907 case AC_VECTOR:
6908 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6909 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006910 case DB_VECTOR:
6911 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6912 if (!(vcpu->guest_debug &
6913 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006914 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006915 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006916 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006917 skip_emulated_instruction(vcpu);
6918
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006919 kvm_queue_exception(vcpu, DB_VECTOR);
6920 return 1;
6921 }
6922 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6923 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6924 /* fall through */
6925 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006926 /*
6927 * Update instruction length as we may reinject #BP from
6928 * user space while in guest debugging mode. Reading it for
6929 * #DB as well causes no harm, it is not used in that case.
6930 */
6931 vmx->vcpu.arch.event_exit_inst_len =
6932 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006933 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006934 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006935 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6936 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006937 break;
6938 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006939 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6940 kvm_run->ex.exception = ex_no;
6941 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006942 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006943 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006944 return 0;
6945}
6946
Avi Kivity851ba692009-08-24 11:10:17 +03006947static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006948{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006949 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006950 return 1;
6951}
6952
Avi Kivity851ba692009-08-24 11:10:17 +03006953static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006954{
Avi Kivity851ba692009-08-24 11:10:17 +03006955 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006956 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006957 return 0;
6958}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006959
Avi Kivity851ba692009-08-24 11:10:17 +03006960static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006961{
He, Qingbfdaab02007-09-12 14:18:28 +08006962 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006963 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006964 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006965
He, Qingbfdaab02007-09-12 14:18:28 +08006966 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006967 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006968
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006969 ++vcpu->stat.io_exits;
6970
Sean Christopherson432baf62018-03-08 08:57:26 -08006971 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006972 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006973
6974 port = exit_qualification >> 16;
6975 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006976 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006977
Sean Christophersondca7f122018-03-08 08:57:27 -08006978 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006979}
6980
Ingo Molnar102d8322007-02-19 14:37:47 +02006981static void
6982vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6983{
6984 /*
6985 * Patch in the VMCALL instruction:
6986 */
6987 hypercall[0] = 0x0f;
6988 hypercall[1] = 0x01;
6989 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006990}
6991
Guo Chao0fa06072012-06-28 15:16:19 +08006992/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006993static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6994{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006995 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006996 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6997 unsigned long orig_val = val;
6998
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006999 /*
7000 * We get here when L2 changed cr0 in a way that did not change
7001 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007002 * but did change L0 shadowed bits. So we first calculate the
7003 * effective cr0 value that L1 would like to write into the
7004 * hardware. It consists of the L2-owned bits from the new
7005 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007006 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007007 val = (val & ~vmcs12->cr0_guest_host_mask) |
7008 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7009
David Matlack38991522016-11-29 18:14:08 -08007010 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007011 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007012
7013 if (kvm_set_cr0(vcpu, val))
7014 return 1;
7015 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007016 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007017 } else {
7018 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007019 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007020 return 1;
David Matlack38991522016-11-29 18:14:08 -08007021
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007022 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007023 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007024}
7025
7026static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7027{
7028 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007029 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7030 unsigned long orig_val = val;
7031
7032 /* analogously to handle_set_cr0 */
7033 val = (val & ~vmcs12->cr4_guest_host_mask) |
7034 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7035 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007036 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007037 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007038 return 0;
7039 } else
7040 return kvm_set_cr4(vcpu, val);
7041}
7042
Paolo Bonzini0367f202016-07-12 10:44:55 +02007043static int handle_desc(struct kvm_vcpu *vcpu)
7044{
7045 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7046 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7047}
7048
Avi Kivity851ba692009-08-24 11:10:17 +03007049static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007051 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052 int cr;
7053 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007054 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007055 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007056
He, Qingbfdaab02007-09-12 14:18:28 +08007057 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058 cr = exit_qualification & 15;
7059 reg = (exit_qualification >> 8) & 15;
7060 switch ((exit_qualification >> 4) & 3) {
7061 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007062 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007063 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064 switch (cr) {
7065 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007066 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007067 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007068 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007069 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007070 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007071 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007072 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007073 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007074 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007075 case 8: {
7076 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007077 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007078 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007079 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007080 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007081 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007082 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007083 return ret;
7084 /*
7085 * TODO: we might be squashing a
7086 * KVM_GUESTDBG_SINGLESTEP-triggered
7087 * KVM_EXIT_DEBUG here.
7088 */
Avi Kivity851ba692009-08-24 11:10:17 +03007089 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007090 return 0;
7091 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007092 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007094 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007095 WARN_ONCE(1, "Guest should always own CR0.TS");
7096 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007097 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007098 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007099 case 1: /*mov from cr*/
7100 switch (cr) {
7101 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007102 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007103 val = kvm_read_cr3(vcpu);
7104 kvm_register_write(vcpu, reg, val);
7105 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007106 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007107 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007108 val = kvm_get_cr8(vcpu);
7109 kvm_register_write(vcpu, reg, val);
7110 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007111 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007112 }
7113 break;
7114 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007115 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007116 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007117 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007118
Kyle Huey6affcbe2016-11-29 12:40:40 -08007119 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120 default:
7121 break;
7122 }
Avi Kivity851ba692009-08-24 11:10:17 +03007123 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007124 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007125 (int)(exit_qualification >> 4) & 3, cr);
7126 return 0;
7127}
7128
Avi Kivity851ba692009-08-24 11:10:17 +03007129static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130{
He, Qingbfdaab02007-09-12 14:18:28 +08007131 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007132 int dr, dr7, reg;
7133
7134 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7135 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7136
7137 /* First, if DR does not exist, trigger UD */
7138 if (!kvm_require_dr(vcpu, dr))
7139 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007140
Jan Kiszkaf2483412010-01-20 18:20:20 +01007141 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007142 if (!kvm_require_cpl(vcpu, 0))
7143 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007144 dr7 = vmcs_readl(GUEST_DR7);
7145 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007146 /*
7147 * As the vm-exit takes precedence over the debug trap, we
7148 * need to emulate the latter, either for the host or the
7149 * guest debugging itself.
7150 */
7151 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007152 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007153 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007154 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007155 vcpu->run->debug.arch.exception = DB_VECTOR;
7156 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007157 return 0;
7158 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007159 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007160 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007161 kvm_queue_exception(vcpu, DB_VECTOR);
7162 return 1;
7163 }
7164 }
7165
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007166 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007167 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7168 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007169
7170 /*
7171 * No more DR vmexits; force a reload of the debug registers
7172 * and reenter on this instruction. The next vmexit will
7173 * retrieve the full state of the debug registers.
7174 */
7175 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7176 return 1;
7177 }
7178
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007179 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7180 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007181 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007182
7183 if (kvm_get_dr(vcpu, dr, &val))
7184 return 1;
7185 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007186 } else
Nadav Amit57773922014-06-18 17:19:23 +03007187 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007188 return 1;
7189
Kyle Huey6affcbe2016-11-29 12:40:40 -08007190 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007191}
7192
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007193static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7194{
7195 return vcpu->arch.dr6;
7196}
7197
7198static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7199{
7200}
7201
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007202static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7203{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007204 get_debugreg(vcpu->arch.db[0], 0);
7205 get_debugreg(vcpu->arch.db[1], 1);
7206 get_debugreg(vcpu->arch.db[2], 2);
7207 get_debugreg(vcpu->arch.db[3], 3);
7208 get_debugreg(vcpu->arch.dr6, 6);
7209 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7210
7211 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007212 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007213}
7214
Gleb Natapov020df072010-04-13 10:05:23 +03007215static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7216{
7217 vmcs_writel(GUEST_DR7, val);
7218}
7219
Avi Kivity851ba692009-08-24 11:10:17 +03007220static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007221{
Kyle Huey6a908b62016-11-29 12:40:37 -08007222 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007223}
7224
Avi Kivity851ba692009-08-24 11:10:17 +03007225static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007226{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007227 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007228 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007229
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007230 msr_info.index = ecx;
7231 msr_info.host_initiated = false;
7232 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007233 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007234 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007235 return 1;
7236 }
7237
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007238 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007239
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007241 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7242 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007243 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007244}
7245
Avi Kivity851ba692009-08-24 11:10:17 +03007246static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007247{
Will Auld8fe8ab42012-11-29 12:42:12 -08007248 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007249 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7250 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7251 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007252
Will Auld8fe8ab42012-11-29 12:42:12 -08007253 msr.data = data;
7254 msr.index = ecx;
7255 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007256 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007257 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007258 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007259 return 1;
7260 }
7261
Avi Kivity59200272010-01-25 19:47:02 +02007262 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007263 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007264}
7265
Avi Kivity851ba692009-08-24 11:10:17 +03007266static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007267{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007268 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007269 return 1;
7270}
7271
Avi Kivity851ba692009-08-24 11:10:17 +03007272static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007274 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7275 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007276
Avi Kivity3842d132010-07-27 12:30:24 +03007277 kvm_make_request(KVM_REQ_EVENT, vcpu);
7278
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007279 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007280 return 1;
7281}
7282
Avi Kivity851ba692009-08-24 11:10:17 +03007283static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284{
Avi Kivityd3bef152007-06-05 15:53:05 +03007285 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007286}
7287
Avi Kivity851ba692009-08-24 11:10:17 +03007288static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007289{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007290 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007291}
7292
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007293static int handle_invd(struct kvm_vcpu *vcpu)
7294{
Andre Przywara51d8b662010-12-21 11:12:02 +01007295 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007296}
7297
Avi Kivity851ba692009-08-24 11:10:17 +03007298static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007299{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007300 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007301
7302 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007303 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007304}
7305
Avi Kivityfee84b02011-11-10 14:57:25 +02007306static int handle_rdpmc(struct kvm_vcpu *vcpu)
7307{
7308 int err;
7309
7310 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007311 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007312}
7313
Avi Kivity851ba692009-08-24 11:10:17 +03007314static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007315{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007316 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007317}
7318
Dexuan Cui2acf9232010-06-10 11:27:12 +08007319static int handle_xsetbv(struct kvm_vcpu *vcpu)
7320{
7321 u64 new_bv = kvm_read_edx_eax(vcpu);
7322 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7323
7324 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007325 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007326 return 1;
7327}
7328
Wanpeng Lif53cd632014-12-02 19:14:58 +08007329static int handle_xsaves(struct kvm_vcpu *vcpu)
7330{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007331 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007332 WARN(1, "this should never happen\n");
7333 return 1;
7334}
7335
7336static int handle_xrstors(struct kvm_vcpu *vcpu)
7337{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007338 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007339 WARN(1, "this should never happen\n");
7340 return 1;
7341}
7342
Avi Kivity851ba692009-08-24 11:10:17 +03007343static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007344{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007345 if (likely(fasteoi)) {
7346 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7347 int access_type, offset;
7348
7349 access_type = exit_qualification & APIC_ACCESS_TYPE;
7350 offset = exit_qualification & APIC_ACCESS_OFFSET;
7351 /*
7352 * Sane guest uses MOV to write EOI, with written value
7353 * not cared. So make a short-circuit here by avoiding
7354 * heavy instruction emulation.
7355 */
7356 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7357 (offset == APIC_EOI)) {
7358 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007359 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007360 }
7361 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007362 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007363}
7364
Yang Zhangc7c9c562013-01-25 10:18:51 +08007365static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7366{
7367 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7368 int vector = exit_qualification & 0xff;
7369
7370 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7371 kvm_apic_set_eoi_accelerated(vcpu, vector);
7372 return 1;
7373}
7374
Yang Zhang83d4c282013-01-25 10:18:49 +08007375static int handle_apic_write(struct kvm_vcpu *vcpu)
7376{
7377 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7378 u32 offset = exit_qualification & 0xfff;
7379
7380 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7381 kvm_apic_write_nodecode(vcpu, offset);
7382 return 1;
7383}
7384
Avi Kivity851ba692009-08-24 11:10:17 +03007385static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007386{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007387 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007388 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007389 bool has_error_code = false;
7390 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007391 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007392 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007393
7394 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007395 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007396 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007397
7398 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7399
7400 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007401 if (reason == TASK_SWITCH_GATE && idt_v) {
7402 switch (type) {
7403 case INTR_TYPE_NMI_INTR:
7404 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007405 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007406 break;
7407 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007408 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007409 kvm_clear_interrupt_queue(vcpu);
7410 break;
7411 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007412 if (vmx->idt_vectoring_info &
7413 VECTORING_INFO_DELIVER_CODE_MASK) {
7414 has_error_code = true;
7415 error_code =
7416 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7417 }
7418 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007419 case INTR_TYPE_SOFT_EXCEPTION:
7420 kvm_clear_exception_queue(vcpu);
7421 break;
7422 default:
7423 break;
7424 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007425 }
Izik Eidus37817f22008-03-24 23:14:53 +02007426 tss_selector = exit_qualification;
7427
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007428 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7429 type != INTR_TYPE_EXT_INTR &&
7430 type != INTR_TYPE_NMI_INTR))
7431 skip_emulated_instruction(vcpu);
7432
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007433 if (kvm_task_switch(vcpu, tss_selector,
7434 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7435 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007436 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7437 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7438 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007439 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007440 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007441
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007442 /*
7443 * TODO: What about debug traps on tss switch?
7444 * Are we supposed to inject them and update dr6?
7445 */
7446
7447 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007448}
7449
Avi Kivity851ba692009-08-24 11:10:17 +03007450static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007451{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007452 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007453 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007454 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007455
Sheng Yangf9c617f2009-03-25 10:08:52 +08007456 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007457
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007458 /*
7459 * EPT violation happened while executing iret from NMI,
7460 * "blocked by NMI" bit has to be set before next VM entry.
7461 * There are errata that may cause this bit to not be set:
7462 * AAK134, BY25.
7463 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007464 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007465 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007466 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007467 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7468
Sheng Yang14394422008-04-28 12:24:45 +08007469 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007470 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007471
Junaid Shahid27959a42016-12-06 16:46:10 -08007472 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007473 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007474 ? PFERR_USER_MASK : 0;
7475 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007476 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007477 ? PFERR_WRITE_MASK : 0;
7478 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007479 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007480 ? PFERR_FETCH_MASK : 0;
7481 /* ept page table entry is present? */
7482 error_code |= (exit_qualification &
7483 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7484 EPT_VIOLATION_EXECUTABLE))
7485 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007486
Paolo Bonzinieebed242016-11-28 14:39:58 +01007487 error_code |= (exit_qualification & 0x100) != 0 ?
7488 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007489
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007490 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007491 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007492}
7493
Avi Kivity851ba692009-08-24 11:10:17 +03007494static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007495{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007496 gpa_t gpa;
7497
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007498 /*
7499 * A nested guest cannot optimize MMIO vmexits, because we have an
7500 * nGPA here instead of the required GPA.
7501 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007502 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007503 if (!is_guest_mode(vcpu) &&
7504 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007505 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007506 /*
7507 * Doing kvm_skip_emulated_instruction() depends on undefined
7508 * behavior: Intel's manual doesn't mandate
7509 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7510 * occurs and while on real hardware it was observed to be set,
7511 * other hypervisors (namely Hyper-V) don't set it, we end up
7512 * advancing IP with some random value. Disable fast mmio when
7513 * running nested and keep it for real hardware in hope that
7514 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7515 */
7516 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7517 return kvm_skip_emulated_instruction(vcpu);
7518 else
7519 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7520 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007521 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007522
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007523 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007524}
7525
Avi Kivity851ba692009-08-24 11:10:17 +03007526static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007527{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007528 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007529 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7530 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007531 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007532 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007533
7534 return 1;
7535}
7536
Mohammed Gamal80ced182009-09-01 12:48:18 +02007537static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007538{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007539 struct vcpu_vmx *vmx = to_vmx(vcpu);
7540 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007541 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007542 u32 cpu_exec_ctrl;
7543 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007544 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007545
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007546 /*
7547 * We should never reach the point where we are emulating L2
7548 * due to invalid guest state as that means we incorrectly
7549 * allowed a nested VMEntry with an invalid vmcs12.
7550 */
7551 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7552
Avi Kivity49e9d552010-09-19 14:34:08 +02007553 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7554 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007555
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007556 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007557 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007558 return handle_interrupt_window(&vmx->vcpu);
7559
Radim Krčmář72875d82017-04-26 22:32:19 +02007560 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007561 return 1;
7562
Liran Alon9b8ae632017-11-05 16:56:34 +02007563 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007564
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007565 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007566 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007567 ret = 0;
7568 goto out;
7569 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007570
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007571 if (err != EMULATE_DONE)
7572 goto emulation_error;
7573
7574 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7575 vcpu->arch.exception.pending)
7576 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007577
Gleb Natapov8d76c492013-05-08 18:38:44 +03007578 if (vcpu->arch.halt_request) {
7579 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007580 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007581 goto out;
7582 }
7583
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007584 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007585 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007586 if (need_resched())
7587 schedule();
7588 }
7589
Mohammed Gamal80ced182009-09-01 12:48:18 +02007590out:
7591 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007592
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007593emulation_error:
7594 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7595 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7596 vcpu->run->internal.ndata = 0;
7597 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007598}
7599
7600static void grow_ple_window(struct kvm_vcpu *vcpu)
7601{
7602 struct vcpu_vmx *vmx = to_vmx(vcpu);
7603 int old = vmx->ple_window;
7604
Babu Mogerc8e88712018-03-16 16:37:24 -04007605 vmx->ple_window = __grow_ple_window(old, ple_window,
7606 ple_window_grow,
7607 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007608
7609 if (vmx->ple_window != old)
7610 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007611
7612 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007613}
7614
7615static void shrink_ple_window(struct kvm_vcpu *vcpu)
7616{
7617 struct vcpu_vmx *vmx = to_vmx(vcpu);
7618 int old = vmx->ple_window;
7619
Babu Mogerc8e88712018-03-16 16:37:24 -04007620 vmx->ple_window = __shrink_ple_window(old, ple_window,
7621 ple_window_shrink,
7622 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007623
7624 if (vmx->ple_window != old)
7625 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007626
7627 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007628}
7629
7630/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007631 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7632 */
7633static void wakeup_handler(void)
7634{
7635 struct kvm_vcpu *vcpu;
7636 int cpu = smp_processor_id();
7637
7638 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7639 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7640 blocked_vcpu_list) {
7641 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7642
7643 if (pi_test_on(pi_desc) == 1)
7644 kvm_vcpu_kick(vcpu);
7645 }
7646 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7647}
7648
Peng Haoe01bca22018-04-07 05:47:32 +08007649static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007650{
7651 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7652 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7653 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7654 0ull, VMX_EPT_EXECUTABLE_MASK,
7655 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007656 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007657
7658 ept_set_mmio_spte_mask();
7659 kvm_enable_tdp();
7660}
7661
Tiejun Chenf2c76482014-10-28 10:14:47 +08007662static __init int hardware_setup(void)
7663{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007664 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007665
7666 rdmsrl_safe(MSR_EFER, &host_efer);
7667
7668 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7669 kvm_define_shared_msr(i, vmx_msr_index[i]);
7670
Radim Krčmář23611332016-09-29 22:41:33 +02007671 for (i = 0; i < VMX_BITMAP_NR; i++) {
7672 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7673 if (!vmx_bitmap[i])
7674 goto out;
7675 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007676
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007677 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7678 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7679
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007680 if (setup_vmcs_config(&vmcs_config) < 0) {
7681 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007682 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007683 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007684
7685 if (boot_cpu_has(X86_FEATURE_NX))
7686 kvm_enable_efer_bits(EFER_NX);
7687
Wanpeng Li08d839c2017-03-23 05:30:08 -07007688 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7689 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007690 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007691
Tiejun Chenf2c76482014-10-28 10:14:47 +08007692 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007693 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007694 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007695 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007696 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007697
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007698 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007699 enable_ept_ad_bits = 0;
7700
Wanpeng Li8ad81822017-10-09 15:51:53 -07007701 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007702 enable_unrestricted_guest = 0;
7703
Paolo Bonziniad15a292015-01-30 16:18:49 +01007704 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007705 flexpriority_enabled = 0;
7706
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007707 if (!cpu_has_virtual_nmis())
7708 enable_vnmi = 0;
7709
Paolo Bonziniad15a292015-01-30 16:18:49 +01007710 /*
7711 * set_apic_access_page_addr() is used to reload apic access
7712 * page upon invalidation. No need to do anything if not
7713 * using the APIC_ACCESS_ADDR VMCS field.
7714 */
7715 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007716 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007717
7718 if (!cpu_has_vmx_tpr_shadow())
7719 kvm_x86_ops->update_cr8_intercept = NULL;
7720
7721 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7722 kvm_disable_largepages();
7723
Wanpeng Li0f107682017-09-28 18:06:24 -07007724 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007725 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007726 ple_window = 0;
7727 ple_window_grow = 0;
7728 ple_window_max = 0;
7729 ple_window_shrink = 0;
7730 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007731
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007732 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007733 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007734 kvm_x86_ops->sync_pir_to_irr = NULL;
7735 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007736
Haozhong Zhang64903d62015-10-20 15:39:09 +08007737 if (cpu_has_vmx_tsc_scaling()) {
7738 kvm_has_tsc_control = true;
7739 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7740 kvm_tsc_scaling_ratio_frac_bits = 48;
7741 }
7742
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007743 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7744
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007745 if (enable_ept)
7746 vmx_enable_tdp();
7747 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007748 kvm_disable_tdp();
7749
Kai Huang843e4332015-01-28 10:54:28 +08007750 /*
7751 * Only enable PML when hardware supports PML feature, and both EPT
7752 * and EPT A/D bit features are enabled -- PML depends on them to work.
7753 */
7754 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7755 enable_pml = 0;
7756
7757 if (!enable_pml) {
7758 kvm_x86_ops->slot_enable_log_dirty = NULL;
7759 kvm_x86_ops->slot_disable_log_dirty = NULL;
7760 kvm_x86_ops->flush_log_dirty = NULL;
7761 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7762 }
7763
Yunhong Jiang64672c92016-06-13 14:19:59 -07007764 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7765 u64 vmx_msr;
7766
7767 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7768 cpu_preemption_timer_multi =
7769 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7770 } else {
7771 kvm_x86_ops->set_hv_timer = NULL;
7772 kvm_x86_ops->cancel_hv_timer = NULL;
7773 }
7774
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007775 if (!cpu_has_vmx_shadow_vmcs())
7776 enable_shadow_vmcs = 0;
7777 if (enable_shadow_vmcs)
7778 init_vmcs_shadow_fields();
7779
Feng Wubf9f6ac2015-09-18 22:29:55 +08007780 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007781 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007782
Ashok Rajc45dcc72016-06-22 14:59:56 +08007783 kvm_mce_cap_supported |= MCG_LMCE_P;
7784
Tiejun Chenf2c76482014-10-28 10:14:47 +08007785 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007786
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007787out:
Radim Krčmář23611332016-09-29 22:41:33 +02007788 for (i = 0; i < VMX_BITMAP_NR; i++)
7789 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007790
7791 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007792}
7793
7794static __exit void hardware_unsetup(void)
7795{
Radim Krčmář23611332016-09-29 22:41:33 +02007796 int i;
7797
7798 for (i = 0; i < VMX_BITMAP_NR; i++)
7799 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007800
Tiejun Chenf2c76482014-10-28 10:14:47 +08007801 free_kvm_area();
7802}
7803
Avi Kivity6aa8b732006-12-10 02:21:36 -08007804/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007805 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7806 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7807 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007808static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007809{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007810 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007811 grow_ple_window(vcpu);
7812
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007813 /*
7814 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7815 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7816 * never set PAUSE_EXITING and just set PLE if supported,
7817 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7818 */
7819 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007820 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007821}
7822
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007823static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007824{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007825 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007826}
7827
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007828static int handle_mwait(struct kvm_vcpu *vcpu)
7829{
7830 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7831 return handle_nop(vcpu);
7832}
7833
Jim Mattson45ec3682017-08-23 16:32:04 -07007834static int handle_invalid_op(struct kvm_vcpu *vcpu)
7835{
7836 kvm_queue_exception(vcpu, UD_VECTOR);
7837 return 1;
7838}
7839
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007840static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7841{
7842 return 1;
7843}
7844
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007845static int handle_monitor(struct kvm_vcpu *vcpu)
7846{
7847 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7848 return handle_nop(vcpu);
7849}
7850
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007851/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007852 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7853 * set the success or error code of an emulated VMX instruction, as specified
7854 * by Vol 2B, VMX Instruction Reference, "Conventions".
7855 */
7856static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7857{
7858 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7859 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7860 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7861}
7862
7863static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7864{
7865 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7866 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7867 X86_EFLAGS_SF | X86_EFLAGS_OF))
7868 | X86_EFLAGS_CF);
7869}
7870
Abel Gordon145c28d2013-04-18 14:36:55 +03007871static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007872 u32 vm_instruction_error)
7873{
7874 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7875 /*
7876 * failValid writes the error number to the current VMCS, which
7877 * can't be done there isn't a current VMCS.
7878 */
7879 nested_vmx_failInvalid(vcpu);
7880 return;
7881 }
7882 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7883 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7884 X86_EFLAGS_SF | X86_EFLAGS_OF))
7885 | X86_EFLAGS_ZF);
7886 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7887 /*
7888 * We don't need to force a shadow sync because
7889 * VM_INSTRUCTION_ERROR is not shadowed
7890 */
7891}
Abel Gordon145c28d2013-04-18 14:36:55 +03007892
Wincy Vanff651cb2014-12-11 08:52:58 +03007893static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7894{
7895 /* TODO: not to reset guest simply here. */
7896 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007897 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007898}
7899
Jan Kiszkaf4124502014-03-07 20:03:13 +01007900static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7901{
7902 struct vcpu_vmx *vmx =
7903 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7904
7905 vmx->nested.preemption_timer_expired = true;
7906 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7907 kvm_vcpu_kick(&vmx->vcpu);
7908
7909 return HRTIMER_NORESTART;
7910}
7911
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007912/*
Bandan Das19677e32014-05-06 02:19:15 -04007913 * Decode the memory-address operand of a vmx instruction, as recorded on an
7914 * exit caused by such an instruction (run by a guest hypervisor).
7915 * On success, returns 0. When the operand is invalid, returns 1 and throws
7916 * #UD or #GP.
7917 */
7918static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7919 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007920 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007921{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007922 gva_t off;
7923 bool exn;
7924 struct kvm_segment s;
7925
Bandan Das19677e32014-05-06 02:19:15 -04007926 /*
7927 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7928 * Execution", on an exit, vmx_instruction_info holds most of the
7929 * addressing components of the operand. Only the displacement part
7930 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7931 * For how an actual address is calculated from all these components,
7932 * refer to Vol. 1, "Operand Addressing".
7933 */
7934 int scaling = vmx_instruction_info & 3;
7935 int addr_size = (vmx_instruction_info >> 7) & 7;
7936 bool is_reg = vmx_instruction_info & (1u << 10);
7937 int seg_reg = (vmx_instruction_info >> 15) & 7;
7938 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7939 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7940 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7941 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7942
7943 if (is_reg) {
7944 kvm_queue_exception(vcpu, UD_VECTOR);
7945 return 1;
7946 }
7947
7948 /* Addr = segment_base + offset */
7949 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007950 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007951 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007952 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007953 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007954 off += kvm_register_read(vcpu, index_reg)<<scaling;
7955 vmx_get_segment(vcpu, &s, seg_reg);
7956 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007957
7958 if (addr_size == 1) /* 32 bit */
7959 *ret &= 0xffffffff;
7960
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007961 /* Checks for #GP/#SS exceptions. */
7962 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007963 if (is_long_mode(vcpu)) {
7964 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7965 * non-canonical form. This is the only check on the memory
7966 * destination for long mode!
7967 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007968 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007969 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007970 /* Protected mode: apply checks for segment validity in the
7971 * following order:
7972 * - segment type check (#GP(0) may be thrown)
7973 * - usability check (#GP(0)/#SS(0))
7974 * - limit check (#GP(0)/#SS(0))
7975 */
7976 if (wr)
7977 /* #GP(0) if the destination operand is located in a
7978 * read-only data segment or any code segment.
7979 */
7980 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7981 else
7982 /* #GP(0) if the source operand is located in an
7983 * execute-only code segment
7984 */
7985 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007986 if (exn) {
7987 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7988 return 1;
7989 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007990 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7991 */
7992 exn = (s.unusable != 0);
7993 /* Protected mode: #GP(0)/#SS(0) if the memory
7994 * operand is outside the segment limit.
7995 */
7996 exn = exn || (off + sizeof(u64) > s.limit);
7997 }
7998 if (exn) {
7999 kvm_queue_exception_e(vcpu,
8000 seg_reg == VCPU_SREG_SS ?
8001 SS_VECTOR : GP_VECTOR,
8002 0);
8003 return 1;
8004 }
8005
Bandan Das19677e32014-05-06 02:19:15 -04008006 return 0;
8007}
8008
Radim Krčmářcbf71272017-05-19 15:48:51 +02008009static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008010{
8011 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008012 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008013
8014 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008015 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008016 return 1;
8017
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008018 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008019 kvm_inject_page_fault(vcpu, &e);
8020 return 1;
8021 }
8022
Bandan Das3573e222014-05-06 02:19:16 -04008023 return 0;
8024}
8025
Jim Mattsone29acc52016-11-30 12:03:43 -08008026static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8027{
8028 struct vcpu_vmx *vmx = to_vmx(vcpu);
8029 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008030 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008031
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008032 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8033 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008034 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008035
8036 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8037 if (!vmx->nested.cached_vmcs12)
8038 goto out_cached_vmcs12;
8039
8040 if (enable_shadow_vmcs) {
8041 shadow_vmcs = alloc_vmcs();
8042 if (!shadow_vmcs)
8043 goto out_shadow_vmcs;
8044 /* mark vmcs as shadow */
8045 shadow_vmcs->revision_id |= (1u << 31);
8046 /* init shadow vmcs */
8047 vmcs_clear(shadow_vmcs);
8048 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
8049 }
8050
Jim Mattsone29acc52016-11-30 12:03:43 -08008051 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8052 HRTIMER_MODE_REL_PINNED);
8053 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8054
8055 vmx->nested.vmxon = true;
8056 return 0;
8057
8058out_shadow_vmcs:
8059 kfree(vmx->nested.cached_vmcs12);
8060
8061out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008062 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008063
Jim Mattsonde3a0022017-11-27 17:22:25 -06008064out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008065 return -ENOMEM;
8066}
8067
Bandan Das3573e222014-05-06 02:19:16 -04008068/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008069 * Emulate the VMXON instruction.
8070 * Currently, we just remember that VMX is active, and do not save or even
8071 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8072 * do not currently need to store anything in that guest-allocated memory
8073 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8074 * argument is different from the VMXON pointer (which the spec says they do).
8075 */
8076static int handle_vmon(struct kvm_vcpu *vcpu)
8077{
Jim Mattsone29acc52016-11-30 12:03:43 -08008078 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008079 gpa_t vmptr;
8080 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008081 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008082 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8083 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008084
Jim Mattson70f3aac2017-04-26 08:53:46 -07008085 /*
8086 * The Intel VMX Instruction Reference lists a bunch of bits that are
8087 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8088 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8089 * Otherwise, we should fail with #UD. But most faulting conditions
8090 * have already been checked by hardware, prior to the VM-exit for
8091 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8092 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008093 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008094 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008095 kvm_queue_exception(vcpu, UD_VECTOR);
8096 return 1;
8097 }
8098
Felix Wilhelm727ba742018-06-11 09:43:44 +02008099 /* CPL=0 must be checked manually. */
8100 if (vmx_get_cpl(vcpu)) {
8101 kvm_queue_exception(vcpu, UD_VECTOR);
8102 return 1;
8103 }
8104
Abel Gordon145c28d2013-04-18 14:36:55 +03008105 if (vmx->nested.vmxon) {
8106 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008107 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008108 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008109
Haozhong Zhang3b840802016-06-22 14:59:54 +08008110 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008111 != VMXON_NEEDED_FEATURES) {
8112 kvm_inject_gp(vcpu, 0);
8113 return 1;
8114 }
8115
Radim Krčmářcbf71272017-05-19 15:48:51 +02008116 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008117 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008118
8119 /*
8120 * SDM 3: 24.11.5
8121 * The first 4 bytes of VMXON region contain the supported
8122 * VMCS revision identifier
8123 *
8124 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8125 * which replaces physical address width with 32
8126 */
8127 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8128 nested_vmx_failInvalid(vcpu);
8129 return kvm_skip_emulated_instruction(vcpu);
8130 }
8131
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008132 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8133 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008134 nested_vmx_failInvalid(vcpu);
8135 return kvm_skip_emulated_instruction(vcpu);
8136 }
8137 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8138 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008139 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008140 nested_vmx_failInvalid(vcpu);
8141 return kvm_skip_emulated_instruction(vcpu);
8142 }
8143 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008144 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008145
8146 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008147 ret = enter_vmx_operation(vcpu);
8148 if (ret)
8149 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008150
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008151 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008152 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008153}
8154
8155/*
8156 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8157 * for running VMX instructions (except VMXON, whose prerequisites are
8158 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008159 * Note that many of these exceptions have priority over VM exits, so they
8160 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008161 */
8162static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8163{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008164 if (vmx_get_cpl(vcpu)) {
8165 kvm_queue_exception(vcpu, UD_VECTOR);
8166 return 0;
8167 }
8168
Jim Mattson70f3aac2017-04-26 08:53:46 -07008169 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008170 kvm_queue_exception(vcpu, UD_VECTOR);
8171 return 0;
8172 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008173 return 1;
8174}
8175
David Matlack8ca44e82017-08-01 14:00:39 -07008176static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8177{
8178 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8179 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8180}
8181
Abel Gordone7953d72013-04-18 14:37:55 +03008182static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8183{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008184 if (vmx->nested.current_vmptr == -1ull)
8185 return;
8186
Abel Gordon012f83c2013-04-18 14:39:25 +03008187 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008188 /* copy to memory all shadowed fields in case
8189 they were modified */
8190 copy_shadow_to_vmcs12(vmx);
8191 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008192 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008193 }
Wincy Van705699a2015-02-03 23:58:17 +08008194 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008195
8196 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008197 kvm_vcpu_write_guest_page(&vmx->vcpu,
8198 vmx->nested.current_vmptr >> PAGE_SHIFT,
8199 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008200
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008201 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008202}
8203
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008204/*
8205 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8206 * just stops using VMX.
8207 */
8208static void free_nested(struct vcpu_vmx *vmx)
8209{
Wanpeng Lib7455822017-11-22 14:04:00 -08008210 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008211 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008212
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008213 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008214 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008215 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008216 vmx->nested.posted_intr_nv = -1;
8217 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008218 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008219 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008220 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8221 free_vmcs(vmx->vmcs01.shadow_vmcs);
8222 vmx->vmcs01.shadow_vmcs = NULL;
8223 }
David Matlack4f2777b2016-07-13 17:16:37 -07008224 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008225 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008226 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008227 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008228 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008229 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008230 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008231 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008232 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008233 }
Wincy Van705699a2015-02-03 23:58:17 +08008234 if (vmx->nested.pi_desc_page) {
8235 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008236 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008237 vmx->nested.pi_desc_page = NULL;
8238 vmx->nested.pi_desc = NULL;
8239 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008240
Jim Mattsonde3a0022017-11-27 17:22:25 -06008241 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008242}
8243
8244/* Emulate the VMXOFF instruction */
8245static int handle_vmoff(struct kvm_vcpu *vcpu)
8246{
8247 if (!nested_vmx_check_permission(vcpu))
8248 return 1;
8249 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008250 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008251 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008252}
8253
Nadav Har'El27d6c862011-05-25 23:06:59 +03008254/* Emulate the VMCLEAR instruction */
8255static int handle_vmclear(struct kvm_vcpu *vcpu)
8256{
8257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008258 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008259 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008260
8261 if (!nested_vmx_check_permission(vcpu))
8262 return 1;
8263
Radim Krčmářcbf71272017-05-19 15:48:51 +02008264 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008265 return 1;
8266
Radim Krčmářcbf71272017-05-19 15:48:51 +02008267 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8268 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8269 return kvm_skip_emulated_instruction(vcpu);
8270 }
8271
8272 if (vmptr == vmx->nested.vmxon_ptr) {
8273 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8274 return kvm_skip_emulated_instruction(vcpu);
8275 }
8276
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008277 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008278 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008279
Jim Mattson587d7e722017-03-02 12:41:48 -08008280 kvm_vcpu_write_guest(vcpu,
8281 vmptr + offsetof(struct vmcs12, launch_state),
8282 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008283
Nadav Har'El27d6c862011-05-25 23:06:59 +03008284 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008285 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008286}
8287
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008288static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8289
8290/* Emulate the VMLAUNCH instruction */
8291static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8292{
8293 return nested_vmx_run(vcpu, true);
8294}
8295
8296/* Emulate the VMRESUME instruction */
8297static int handle_vmresume(struct kvm_vcpu *vcpu)
8298{
8299
8300 return nested_vmx_run(vcpu, false);
8301}
8302
Nadav Har'El49f705c2011-05-25 23:08:30 +03008303/*
8304 * Read a vmcs12 field. Since these can have varying lengths and we return
8305 * one type, we chose the biggest type (u64) and zero-extend the return value
8306 * to that size. Note that the caller, handle_vmread, might need to use only
8307 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8308 * 64-bit fields are to be returned).
8309 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008310static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8311 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008312{
8313 short offset = vmcs_field_to_offset(field);
8314 char *p;
8315
8316 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008317 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008318
8319 p = ((char *)(get_vmcs12(vcpu))) + offset;
8320
Jim Mattsond37f4262017-12-22 12:12:16 -08008321 switch (vmcs_field_width(field)) {
8322 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008324 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008325 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008326 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008327 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008328 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008329 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008330 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008331 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008333 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008334 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008335 WARN_ON(1);
8336 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008337 }
8338}
8339
Abel Gordon20b97fe2013-04-18 14:36:25 +03008340
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008341static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8342 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008343 short offset = vmcs_field_to_offset(field);
8344 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8345 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008346 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008347
Jim Mattsond37f4262017-12-22 12:12:16 -08008348 switch (vmcs_field_width(field)) {
8349 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008350 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008351 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008352 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008353 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008354 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008355 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008356 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008357 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008358 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008359 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008360 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008361 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008362 WARN_ON(1);
8363 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008364 }
8365
8366}
8367
Jim Mattsonf4160e42018-05-29 09:11:33 -07008368/*
8369 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8370 * they have been modified by the L1 guest. Note that the "read-only"
8371 * VM-exit information fields are actually writable if the vCPU is
8372 * configured to support "VMWRITE to any supported field in the VMCS."
8373 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008374static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8375{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008376 const u16 *fields[] = {
8377 shadow_read_write_fields,
8378 shadow_read_only_fields
8379 };
8380 const int max_fields[] = {
8381 max_shadow_read_write_fields,
8382 max_shadow_read_only_fields
8383 };
8384 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008385 unsigned long field;
8386 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008387 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008388
Jan Kiszka282da872014-10-08 18:05:39 +02008389 preempt_disable();
8390
Abel Gordon16f5b902013-04-18 14:38:25 +03008391 vmcs_load(shadow_vmcs);
8392
Jim Mattsonf4160e42018-05-29 09:11:33 -07008393 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8394 for (i = 0; i < max_fields[q]; i++) {
8395 field = fields[q][i];
8396 field_value = __vmcs_readl(field);
8397 vmcs12_write_any(&vmx->vcpu, field, field_value);
8398 }
8399 /*
8400 * Skip the VM-exit information fields if they are read-only.
8401 */
8402 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8403 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008404 }
8405
8406 vmcs_clear(shadow_vmcs);
8407 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008408
8409 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008410}
8411
Abel Gordonc3114422013-04-18 14:38:55 +03008412static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8413{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008414 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008415 shadow_read_write_fields,
8416 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008417 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008418 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008419 max_shadow_read_write_fields,
8420 max_shadow_read_only_fields
8421 };
8422 int i, q;
8423 unsigned long field;
8424 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008425 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008426
8427 vmcs_load(shadow_vmcs);
8428
Mathias Krausec2bae892013-06-26 20:36:21 +02008429 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008430 for (i = 0; i < max_fields[q]; i++) {
8431 field = fields[q][i];
8432 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008433 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008434 }
8435 }
8436
8437 vmcs_clear(shadow_vmcs);
8438 vmcs_load(vmx->loaded_vmcs->vmcs);
8439}
8440
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441/*
8442 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8443 * used before) all generate the same failure when it is missing.
8444 */
8445static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8446{
8447 struct vcpu_vmx *vmx = to_vmx(vcpu);
8448 if (vmx->nested.current_vmptr == -1ull) {
8449 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008450 return 0;
8451 }
8452 return 1;
8453}
8454
8455static int handle_vmread(struct kvm_vcpu *vcpu)
8456{
8457 unsigned long field;
8458 u64 field_value;
8459 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8460 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8461 gva_t gva = 0;
8462
Kyle Hueyeb277562016-11-29 12:40:39 -08008463 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008464 return 1;
8465
Kyle Huey6affcbe2016-11-29 12:40:40 -08008466 if (!nested_vmx_check_vmcs12(vcpu))
8467 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008468
Nadav Har'El49f705c2011-05-25 23:08:30 +03008469 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008470 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008471 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008472 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008473 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008474 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008475 }
8476 /*
8477 * Now copy part of this value to register or memory, as requested.
8478 * Note that the number of bits actually copied is 32 or 64 depending
8479 * on the guest's mode (32 or 64 bit), not on the given field's length.
8480 */
8481 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008482 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008483 field_value);
8484 } else {
8485 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008486 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008487 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008488 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008489 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8490 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008491 }
8492
8493 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008494 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008495}
8496
8497
8498static int handle_vmwrite(struct kvm_vcpu *vcpu)
8499{
8500 unsigned long field;
8501 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008502 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008503 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8504 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008505
Nadav Har'El49f705c2011-05-25 23:08:30 +03008506 /* The value to write might be 32 or 64 bits, depending on L1's long
8507 * mode, and eventually we need to write that into a field of several
8508 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008509 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008510 * bits into the vmcs12 field.
8511 */
8512 u64 field_value = 0;
8513 struct x86_exception e;
8514
Kyle Hueyeb277562016-11-29 12:40:39 -08008515 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008516 return 1;
8517
Kyle Huey6affcbe2016-11-29 12:40:40 -08008518 if (!nested_vmx_check_vmcs12(vcpu))
8519 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008520
Nadav Har'El49f705c2011-05-25 23:08:30 +03008521 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008522 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008523 (((vmx_instruction_info) >> 3) & 0xf));
8524 else {
8525 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008526 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008527 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008528 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8529 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008530 kvm_inject_page_fault(vcpu, &e);
8531 return 1;
8532 }
8533 }
8534
8535
Nadav Amit27e6fb52014-06-18 17:19:26 +03008536 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008537 /*
8538 * If the vCPU supports "VMWRITE to any supported field in the
8539 * VMCS," then the "read-only" fields are actually read/write.
8540 */
8541 if (vmcs_field_readonly(field) &&
8542 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543 nested_vmx_failValid(vcpu,
8544 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008545 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008546 }
8547
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008548 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008549 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008550 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551 }
8552
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008553 switch (field) {
8554#define SHADOW_FIELD_RW(x) case x:
8555#include "vmx_shadow_fields.h"
8556 /*
8557 * The fields that can be updated by L1 without a vmexit are
8558 * always updated in the vmcs02, the others go down the slow
8559 * path of prepare_vmcs02.
8560 */
8561 break;
8562 default:
8563 vmx->nested.dirty_vmcs12 = true;
8564 break;
8565 }
8566
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008568 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008569}
8570
Jim Mattsona8bc2842016-11-30 12:03:44 -08008571static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8572{
8573 vmx->nested.current_vmptr = vmptr;
8574 if (enable_shadow_vmcs) {
8575 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8576 SECONDARY_EXEC_SHADOW_VMCS);
8577 vmcs_write64(VMCS_LINK_POINTER,
8578 __pa(vmx->vmcs01.shadow_vmcs));
8579 vmx->nested.sync_shadow_vmcs = true;
8580 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008581 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008582}
8583
Nadav Har'El63846662011-05-25 23:07:29 +03008584/* Emulate the VMPTRLD instruction */
8585static int handle_vmptrld(struct kvm_vcpu *vcpu)
8586{
8587 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008588 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008589
8590 if (!nested_vmx_check_permission(vcpu))
8591 return 1;
8592
Radim Krčmářcbf71272017-05-19 15:48:51 +02008593 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008594 return 1;
8595
Radim Krčmářcbf71272017-05-19 15:48:51 +02008596 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8597 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8598 return kvm_skip_emulated_instruction(vcpu);
8599 }
8600
8601 if (vmptr == vmx->nested.vmxon_ptr) {
8602 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8603 return kvm_skip_emulated_instruction(vcpu);
8604 }
8605
Nadav Har'El63846662011-05-25 23:07:29 +03008606 if (vmx->nested.current_vmptr != vmptr) {
8607 struct vmcs12 *new_vmcs12;
8608 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008609 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8610 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008611 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008612 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008613 }
8614 new_vmcs12 = kmap(page);
8615 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8616 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008617 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008618 nested_vmx_failValid(vcpu,
8619 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008620 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008621 }
Nadav Har'El63846662011-05-25 23:07:29 +03008622
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008623 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008624 /*
8625 * Load VMCS12 from guest memory since it is not already
8626 * cached.
8627 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008628 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8629 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008630 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008631
Jim Mattsona8bc2842016-11-30 12:03:44 -08008632 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008633 }
8634
8635 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008636 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008637}
8638
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008639/* Emulate the VMPTRST instruction */
8640static int handle_vmptrst(struct kvm_vcpu *vcpu)
8641{
8642 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8643 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8644 gva_t vmcs_gva;
8645 struct x86_exception e;
8646
8647 if (!nested_vmx_check_permission(vcpu))
8648 return 1;
8649
8650 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008651 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008652 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008653 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008654 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8655 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8656 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008657 kvm_inject_page_fault(vcpu, &e);
8658 return 1;
8659 }
8660 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008661 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008662}
8663
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008664/* Emulate the INVEPT instruction */
8665static int handle_invept(struct kvm_vcpu *vcpu)
8666{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008667 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008668 u32 vmx_instruction_info, types;
8669 unsigned long type;
8670 gva_t gva;
8671 struct x86_exception e;
8672 struct {
8673 u64 eptp, gpa;
8674 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008675
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008676 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008677 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008678 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008679 kvm_queue_exception(vcpu, UD_VECTOR);
8680 return 1;
8681 }
8682
8683 if (!nested_vmx_check_permission(vcpu))
8684 return 1;
8685
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008686 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008687 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008688
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008689 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008690
Jim Mattson85c856b2016-10-26 08:38:38 -07008691 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008692 nested_vmx_failValid(vcpu,
8693 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008694 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008695 }
8696
8697 /* According to the Intel VMX instruction reference, the memory
8698 * operand is read even if it isn't needed (e.g., for type==global)
8699 */
8700 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008701 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008702 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008703 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008704 kvm_inject_page_fault(vcpu, &e);
8705 return 1;
8706 }
8707
8708 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008709 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008710 /*
8711 * TODO: track mappings and invalidate
8712 * single context requests appropriately
8713 */
8714 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008715 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008716 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008717 nested_vmx_succeed(vcpu);
8718 break;
8719 default:
8720 BUG_ON(1);
8721 break;
8722 }
8723
Kyle Huey6affcbe2016-11-29 12:40:40 -08008724 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008725}
8726
Petr Matouseka642fc32014-09-23 20:22:30 +02008727static int handle_invvpid(struct kvm_vcpu *vcpu)
8728{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008729 struct vcpu_vmx *vmx = to_vmx(vcpu);
8730 u32 vmx_instruction_info;
8731 unsigned long type, types;
8732 gva_t gva;
8733 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008734 struct {
8735 u64 vpid;
8736 u64 gla;
8737 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008738
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008739 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008740 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008741 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008742 kvm_queue_exception(vcpu, UD_VECTOR);
8743 return 1;
8744 }
8745
8746 if (!nested_vmx_check_permission(vcpu))
8747 return 1;
8748
8749 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8750 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8751
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008752 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008753 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008754
Jim Mattson85c856b2016-10-26 08:38:38 -07008755 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008756 nested_vmx_failValid(vcpu,
8757 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008758 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008759 }
8760
8761 /* according to the intel vmx instruction reference, the memory
8762 * operand is read even if it isn't needed (e.g., for type==global)
8763 */
8764 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8765 vmx_instruction_info, false, &gva))
8766 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008767 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008768 kvm_inject_page_fault(vcpu, &e);
8769 return 1;
8770 }
Jim Mattson40352602017-06-28 09:37:37 -07008771 if (operand.vpid >> 16) {
8772 nested_vmx_failValid(vcpu,
8773 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8774 return kvm_skip_emulated_instruction(vcpu);
8775 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008776
8777 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008778 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008779 if (!operand.vpid ||
8780 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008781 nested_vmx_failValid(vcpu,
8782 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8783 return kvm_skip_emulated_instruction(vcpu);
8784 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008785 if (cpu_has_vmx_invvpid_individual_addr() &&
8786 vmx->nested.vpid02) {
8787 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8788 vmx->nested.vpid02, operand.gla);
8789 } else
8790 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8791 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008792 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008793 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008794 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008795 nested_vmx_failValid(vcpu,
8796 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008797 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008798 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008799 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008800 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008801 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008802 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008803 break;
8804 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008805 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008806 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008807 }
8808
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008809 nested_vmx_succeed(vcpu);
8810
Kyle Huey6affcbe2016-11-29 12:40:40 -08008811 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008812}
8813
Kai Huang843e4332015-01-28 10:54:28 +08008814static int handle_pml_full(struct kvm_vcpu *vcpu)
8815{
8816 unsigned long exit_qualification;
8817
8818 trace_kvm_pml_full(vcpu->vcpu_id);
8819
8820 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8821
8822 /*
8823 * PML buffer FULL happened while executing iret from NMI,
8824 * "blocked by NMI" bit has to be set before next VM entry.
8825 */
8826 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008827 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008828 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8829 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8830 GUEST_INTR_STATE_NMI);
8831
8832 /*
8833 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8834 * here.., and there's no userspace involvement needed for PML.
8835 */
8836 return 1;
8837}
8838
Yunhong Jiang64672c92016-06-13 14:19:59 -07008839static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8840{
8841 kvm_lapic_expired_hv_timer(vcpu);
8842 return 1;
8843}
8844
Bandan Das41ab9372017-08-03 15:54:43 -04008845static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8846{
8847 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008848 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8849
8850 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008851 switch (address & VMX_EPTP_MT_MASK) {
8852 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008853 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008854 return false;
8855 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008856 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008857 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008858 return false;
8859 break;
8860 default:
8861 return false;
8862 }
8863
David Hildenbrandbb97a012017-08-10 23:15:28 +02008864 /* only 4 levels page-walk length are valid */
8865 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008866 return false;
8867
8868 /* Reserved bits should not be set */
8869 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8870 return false;
8871
8872 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008873 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008874 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008875 return false;
8876 }
8877
8878 return true;
8879}
8880
8881static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8882 struct vmcs12 *vmcs12)
8883{
8884 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8885 u64 address;
8886 bool accessed_dirty;
8887 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8888
8889 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8890 !nested_cpu_has_ept(vmcs12))
8891 return 1;
8892
8893 if (index >= VMFUNC_EPTP_ENTRIES)
8894 return 1;
8895
8896
8897 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8898 &address, index * 8, 8))
8899 return 1;
8900
David Hildenbrandbb97a012017-08-10 23:15:28 +02008901 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008902
8903 /*
8904 * If the (L2) guest does a vmfunc to the currently
8905 * active ept pointer, we don't have to do anything else
8906 */
8907 if (vmcs12->ept_pointer != address) {
8908 if (!valid_ept_address(vcpu, address))
8909 return 1;
8910
8911 kvm_mmu_unload(vcpu);
8912 mmu->ept_ad = accessed_dirty;
8913 mmu->base_role.ad_disabled = !accessed_dirty;
8914 vmcs12->ept_pointer = address;
8915 /*
8916 * TODO: Check what's the correct approach in case
8917 * mmu reload fails. Currently, we just let the next
8918 * reload potentially fail
8919 */
8920 kvm_mmu_reload(vcpu);
8921 }
8922
8923 return 0;
8924}
8925
Bandan Das2a499e42017-08-03 15:54:41 -04008926static int handle_vmfunc(struct kvm_vcpu *vcpu)
8927{
Bandan Das27c42a12017-08-03 15:54:42 -04008928 struct vcpu_vmx *vmx = to_vmx(vcpu);
8929 struct vmcs12 *vmcs12;
8930 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8931
8932 /*
8933 * VMFUNC is only supported for nested guests, but we always enable the
8934 * secondary control for simplicity; for non-nested mode, fake that we
8935 * didn't by injecting #UD.
8936 */
8937 if (!is_guest_mode(vcpu)) {
8938 kvm_queue_exception(vcpu, UD_VECTOR);
8939 return 1;
8940 }
8941
8942 vmcs12 = get_vmcs12(vcpu);
8943 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8944 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008945
8946 switch (function) {
8947 case 0:
8948 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8949 goto fail;
8950 break;
8951 default:
8952 goto fail;
8953 }
8954 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008955
8956fail:
8957 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8958 vmcs_read32(VM_EXIT_INTR_INFO),
8959 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008960 return 1;
8961}
8962
Nadav Har'El0140cae2011-05-25 23:06:28 +03008963/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008964 * The exit handlers return 1 if the exit was handled fully and guest execution
8965 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8966 * to be done to userspace and return 0.
8967 */
Mathias Krause772e0312012-08-30 01:30:19 +02008968static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008969 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8970 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008971 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008972 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008973 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008974 [EXIT_REASON_CR_ACCESS] = handle_cr,
8975 [EXIT_REASON_DR_ACCESS] = handle_dr,
8976 [EXIT_REASON_CPUID] = handle_cpuid,
8977 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8978 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8979 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8980 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008981 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008982 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008983 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008984 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008985 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008986 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008987 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008988 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008989 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008990 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008991 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008992 [EXIT_REASON_VMOFF] = handle_vmoff,
8993 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008994 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8995 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008996 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008997 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008998 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008999 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009000 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009001 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009002 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9003 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009004 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9005 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009006 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009007 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009008 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009009 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009010 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009011 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009012 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009013 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009014 [EXIT_REASON_XSAVES] = handle_xsaves,
9015 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009016 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04009017 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009018 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009019};
9020
9021static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009022 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009023
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009024static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9025 struct vmcs12 *vmcs12)
9026{
9027 unsigned long exit_qualification;
9028 gpa_t bitmap, last_bitmap;
9029 unsigned int port;
9030 int size;
9031 u8 b;
9032
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009033 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009034 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009035
9036 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9037
9038 port = exit_qualification >> 16;
9039 size = (exit_qualification & 7) + 1;
9040
9041 last_bitmap = (gpa_t)-1;
9042 b = -1;
9043
9044 while (size > 0) {
9045 if (port < 0x8000)
9046 bitmap = vmcs12->io_bitmap_a;
9047 else if (port < 0x10000)
9048 bitmap = vmcs12->io_bitmap_b;
9049 else
Joe Perches1d804d02015-03-30 16:46:09 -07009050 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009051 bitmap += (port & 0x7fff) / 8;
9052
9053 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009054 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009055 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009056 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009057 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009058
9059 port++;
9060 size--;
9061 last_bitmap = bitmap;
9062 }
9063
Joe Perches1d804d02015-03-30 16:46:09 -07009064 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009065}
9066
Nadav Har'El644d7112011-05-25 23:12:35 +03009067/*
9068 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9069 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9070 * disinterest in the current event (read or write a specific MSR) by using an
9071 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9072 */
9073static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9074 struct vmcs12 *vmcs12, u32 exit_reason)
9075{
9076 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9077 gpa_t bitmap;
9078
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009079 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009080 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009081
9082 /*
9083 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9084 * for the four combinations of read/write and low/high MSR numbers.
9085 * First we need to figure out which of the four to use:
9086 */
9087 bitmap = vmcs12->msr_bitmap;
9088 if (exit_reason == EXIT_REASON_MSR_WRITE)
9089 bitmap += 2048;
9090 if (msr_index >= 0xc0000000) {
9091 msr_index -= 0xc0000000;
9092 bitmap += 1024;
9093 }
9094
9095 /* Then read the msr_index'th bit from this bitmap: */
9096 if (msr_index < 1024*8) {
9097 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009098 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009099 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009100 return 1 & (b >> (msr_index & 7));
9101 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009102 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009103}
9104
9105/*
9106 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9107 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9108 * intercept (via guest_host_mask etc.) the current event.
9109 */
9110static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9111 struct vmcs12 *vmcs12)
9112{
9113 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9114 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009115 int reg;
9116 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009117
9118 switch ((exit_qualification >> 4) & 3) {
9119 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009120 reg = (exit_qualification >> 8) & 15;
9121 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009122 switch (cr) {
9123 case 0:
9124 if (vmcs12->cr0_guest_host_mask &
9125 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009126 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009127 break;
9128 case 3:
9129 if ((vmcs12->cr3_target_count >= 1 &&
9130 vmcs12->cr3_target_value0 == val) ||
9131 (vmcs12->cr3_target_count >= 2 &&
9132 vmcs12->cr3_target_value1 == val) ||
9133 (vmcs12->cr3_target_count >= 3 &&
9134 vmcs12->cr3_target_value2 == val) ||
9135 (vmcs12->cr3_target_count >= 4 &&
9136 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009137 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009138 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009139 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009140 break;
9141 case 4:
9142 if (vmcs12->cr4_guest_host_mask &
9143 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009144 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009145 break;
9146 case 8:
9147 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009148 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009149 break;
9150 }
9151 break;
9152 case 2: /* clts */
9153 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9154 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009155 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009156 break;
9157 case 1: /* mov from cr */
9158 switch (cr) {
9159 case 3:
9160 if (vmcs12->cpu_based_vm_exec_control &
9161 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009162 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009163 break;
9164 case 8:
9165 if (vmcs12->cpu_based_vm_exec_control &
9166 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009167 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009168 break;
9169 }
9170 break;
9171 case 3: /* lmsw */
9172 /*
9173 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9174 * cr0. Other attempted changes are ignored, with no exit.
9175 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009176 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009177 if (vmcs12->cr0_guest_host_mask & 0xe &
9178 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009179 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009180 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9181 !(vmcs12->cr0_read_shadow & 0x1) &&
9182 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009183 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009184 break;
9185 }
Joe Perches1d804d02015-03-30 16:46:09 -07009186 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009187}
9188
9189/*
9190 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9191 * should handle it ourselves in L0 (and then continue L2). Only call this
9192 * when in is_guest_mode (L2).
9193 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009194static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009195{
Nadav Har'El644d7112011-05-25 23:12:35 +03009196 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9197 struct vcpu_vmx *vmx = to_vmx(vcpu);
9198 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9199
Jim Mattson4f350c62017-09-14 16:31:44 -07009200 if (vmx->nested.nested_run_pending)
9201 return false;
9202
9203 if (unlikely(vmx->fail)) {
9204 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9205 vmcs_read32(VM_INSTRUCTION_ERROR));
9206 return true;
9207 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009208
David Matlackc9f04402017-08-01 14:00:40 -07009209 /*
9210 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009211 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9212 * Page). The CPU may write to these pages via their host
9213 * physical address while L2 is running, bypassing any
9214 * address-translation-based dirty tracking (e.g. EPT write
9215 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009216 *
9217 * Mark them dirty on every exit from L2 to prevent them from
9218 * getting out of sync with dirty tracking.
9219 */
9220 nested_mark_vmcs12_pages_dirty(vcpu);
9221
Jim Mattson4f350c62017-09-14 16:31:44 -07009222 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9223 vmcs_readl(EXIT_QUALIFICATION),
9224 vmx->idt_vectoring_info,
9225 intr_info,
9226 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9227 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009228
9229 switch (exit_reason) {
9230 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009231 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009232 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009233 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009234 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009235 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009236 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009237 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009238 else if (is_debug(intr_info) &&
9239 vcpu->guest_debug &
9240 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9241 return false;
9242 else if (is_breakpoint(intr_info) &&
9243 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9244 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009245 return vmcs12->exception_bitmap &
9246 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9247 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009248 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009249 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009250 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009251 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009252 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009253 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009254 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009255 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009256 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009257 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009258 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009259 case EXIT_REASON_HLT:
9260 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9261 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009262 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009263 case EXIT_REASON_INVLPG:
9264 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9265 case EXIT_REASON_RDPMC:
9266 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009267 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009268 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009269 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009270 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009271 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009272 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9273 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9274 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9275 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9276 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9277 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009278 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009279 /*
9280 * VMX instructions trap unconditionally. This allows L1 to
9281 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9282 */
Joe Perches1d804d02015-03-30 16:46:09 -07009283 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009284 case EXIT_REASON_CR_ACCESS:
9285 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9286 case EXIT_REASON_DR_ACCESS:
9287 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9288 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009289 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009290 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9291 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009292 case EXIT_REASON_MSR_READ:
9293 case EXIT_REASON_MSR_WRITE:
9294 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9295 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009296 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009297 case EXIT_REASON_MWAIT_INSTRUCTION:
9298 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009299 case EXIT_REASON_MONITOR_TRAP_FLAG:
9300 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009301 case EXIT_REASON_MONITOR_INSTRUCTION:
9302 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9303 case EXIT_REASON_PAUSE_INSTRUCTION:
9304 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9305 nested_cpu_has2(vmcs12,
9306 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9307 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009308 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009309 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009310 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009311 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009312 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009313 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009314 /*
9315 * The controls for "virtualize APIC accesses," "APIC-
9316 * register virtualization," and "virtual-interrupt
9317 * delivery" only come from vmcs12.
9318 */
Joe Perches1d804d02015-03-30 16:46:09 -07009319 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009320 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009321 /*
9322 * L0 always deals with the EPT violation. If nested EPT is
9323 * used, and the nested mmu code discovers that the address is
9324 * missing in the guest EPT table (EPT12), the EPT violation
9325 * will be injected with nested_ept_inject_page_fault()
9326 */
Joe Perches1d804d02015-03-30 16:46:09 -07009327 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009328 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009329 /*
9330 * L2 never uses directly L1's EPT, but rather L0's own EPT
9331 * table (shadow on EPT) or a merged EPT table that L0 built
9332 * (EPT on EPT). So any problems with the structure of the
9333 * table is L0's fault.
9334 */
Joe Perches1d804d02015-03-30 16:46:09 -07009335 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009336 case EXIT_REASON_INVPCID:
9337 return
9338 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9339 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009340 case EXIT_REASON_WBINVD:
9341 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9342 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009343 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009344 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9345 /*
9346 * This should never happen, since it is not possible to
9347 * set XSS to a non-zero value---neither in L1 nor in L2.
9348 * If if it were, XSS would have to be checked against
9349 * the XSS exit bitmap in vmcs12.
9350 */
9351 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009352 case EXIT_REASON_PREEMPTION_TIMER:
9353 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009354 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009355 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009356 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009357 case EXIT_REASON_VMFUNC:
9358 /* VM functions are emulated through L2->L0 vmexits. */
9359 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009360 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009361 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009362 }
9363}
9364
Paolo Bonzini7313c692017-07-27 10:31:25 +02009365static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9366{
9367 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9368
9369 /*
9370 * At this point, the exit interruption info in exit_intr_info
9371 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9372 * we need to query the in-kernel LAPIC.
9373 */
9374 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9375 if ((exit_intr_info &
9376 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9377 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9378 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9379 vmcs12->vm_exit_intr_error_code =
9380 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9381 }
9382
9383 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9384 vmcs_readl(EXIT_QUALIFICATION));
9385 return 1;
9386}
9387
Avi Kivity586f9602010-11-18 13:09:54 +02009388static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9389{
9390 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9391 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9392}
9393
Kai Huanga3eaa862015-11-04 13:46:05 +08009394static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009395{
Kai Huanga3eaa862015-11-04 13:46:05 +08009396 if (vmx->pml_pg) {
9397 __free_page(vmx->pml_pg);
9398 vmx->pml_pg = NULL;
9399 }
Kai Huang843e4332015-01-28 10:54:28 +08009400}
9401
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009402static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009403{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009404 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009405 u64 *pml_buf;
9406 u16 pml_idx;
9407
9408 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9409
9410 /* Do nothing if PML buffer is empty */
9411 if (pml_idx == (PML_ENTITY_NUM - 1))
9412 return;
9413
9414 /* PML index always points to next available PML buffer entity */
9415 if (pml_idx >= PML_ENTITY_NUM)
9416 pml_idx = 0;
9417 else
9418 pml_idx++;
9419
9420 pml_buf = page_address(vmx->pml_pg);
9421 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9422 u64 gpa;
9423
9424 gpa = pml_buf[pml_idx];
9425 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009426 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009427 }
9428
9429 /* reset PML index */
9430 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9431}
9432
9433/*
9434 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9435 * Called before reporting dirty_bitmap to userspace.
9436 */
9437static void kvm_flush_pml_buffers(struct kvm *kvm)
9438{
9439 int i;
9440 struct kvm_vcpu *vcpu;
9441 /*
9442 * We only need to kick vcpu out of guest mode here, as PML buffer
9443 * is flushed at beginning of all VMEXITs, and it's obvious that only
9444 * vcpus running in guest are possible to have unflushed GPAs in PML
9445 * buffer.
9446 */
9447 kvm_for_each_vcpu(i, vcpu, kvm)
9448 kvm_vcpu_kick(vcpu);
9449}
9450
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009451static void vmx_dump_sel(char *name, uint32_t sel)
9452{
9453 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009454 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009455 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9456 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9457 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9458}
9459
9460static void vmx_dump_dtsel(char *name, uint32_t limit)
9461{
9462 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9463 name, vmcs_read32(limit),
9464 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9465}
9466
9467static void dump_vmcs(void)
9468{
9469 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9470 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9471 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9472 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9473 u32 secondary_exec_control = 0;
9474 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009475 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009476 int i, n;
9477
9478 if (cpu_has_secondary_exec_ctrls())
9479 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9480
9481 pr_err("*** Guest State ***\n");
9482 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9483 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9484 vmcs_readl(CR0_GUEST_HOST_MASK));
9485 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9486 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9487 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9488 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9489 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9490 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009491 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9492 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9493 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9494 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009495 }
9496 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9497 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9498 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9499 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9500 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9501 vmcs_readl(GUEST_SYSENTER_ESP),
9502 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9503 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9504 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9505 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9506 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9507 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9508 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9509 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9510 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9511 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9512 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9513 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9514 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009515 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9516 efer, vmcs_read64(GUEST_IA32_PAT));
9517 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9518 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009519 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009520 if (cpu_has_load_perf_global_ctrl &&
9521 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009522 pr_err("PerfGlobCtl = 0x%016llx\n",
9523 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009524 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009525 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009526 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9527 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9528 vmcs_read32(GUEST_ACTIVITY_STATE));
9529 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9530 pr_err("InterruptStatus = %04x\n",
9531 vmcs_read16(GUEST_INTR_STATUS));
9532
9533 pr_err("*** Host State ***\n");
9534 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9535 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9536 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9537 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9538 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9539 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9540 vmcs_read16(HOST_TR_SELECTOR));
9541 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9542 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9543 vmcs_readl(HOST_TR_BASE));
9544 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9545 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9546 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9547 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9548 vmcs_readl(HOST_CR4));
9549 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9550 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9551 vmcs_read32(HOST_IA32_SYSENTER_CS),
9552 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9553 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009554 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9555 vmcs_read64(HOST_IA32_EFER),
9556 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009557 if (cpu_has_load_perf_global_ctrl &&
9558 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009559 pr_err("PerfGlobCtl = 0x%016llx\n",
9560 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009561
9562 pr_err("*** Control State ***\n");
9563 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9564 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9565 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9566 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9567 vmcs_read32(EXCEPTION_BITMAP),
9568 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9569 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9570 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9571 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9572 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9573 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9574 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9575 vmcs_read32(VM_EXIT_INTR_INFO),
9576 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9577 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9578 pr_err(" reason=%08x qualification=%016lx\n",
9579 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9580 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9581 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9582 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009583 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009584 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009585 pr_err("TSC Multiplier = 0x%016llx\n",
9586 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009587 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9588 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9589 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9590 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9591 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009592 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009593 n = vmcs_read32(CR3_TARGET_COUNT);
9594 for (i = 0; i + 1 < n; i += 4)
9595 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9596 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9597 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9598 if (i < n)
9599 pr_err("CR3 target%u=%016lx\n",
9600 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9601 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9602 pr_err("PLE Gap=%08x Window=%08x\n",
9603 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9604 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9605 pr_err("Virtual processor ID = 0x%04x\n",
9606 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9607}
9608
Avi Kivity6aa8b732006-12-10 02:21:36 -08009609/*
9610 * The guest has exited. See if we can fix it or if we need userspace
9611 * assistance.
9612 */
Avi Kivity851ba692009-08-24 11:10:17 +03009613static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009614{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009615 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009616 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009617 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009618
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009619 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9620
Kai Huang843e4332015-01-28 10:54:28 +08009621 /*
9622 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9623 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9624 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9625 * mode as if vcpus is in root mode, the PML buffer must has been
9626 * flushed already.
9627 */
9628 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009629 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009630
Mohammed Gamal80ced182009-09-01 12:48:18 +02009631 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009632 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009633 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009634
Paolo Bonzini7313c692017-07-27 10:31:25 +02009635 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9636 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009637
Mohammed Gamal51207022010-05-31 22:40:54 +03009638 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009639 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009640 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9641 vcpu->run->fail_entry.hardware_entry_failure_reason
9642 = exit_reason;
9643 return 0;
9644 }
9645
Avi Kivity29bd8a72007-09-10 17:27:03 +03009646 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009647 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9648 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009649 = vmcs_read32(VM_INSTRUCTION_ERROR);
9650 return 0;
9651 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009652
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009653 /*
9654 * Note:
9655 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9656 * delivery event since it indicates guest is accessing MMIO.
9657 * The vm-exit can be triggered again after return to guest that
9658 * will cause infinite loop.
9659 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009660 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009661 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009662 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009663 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009664 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9665 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9666 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009667 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009668 vcpu->run->internal.data[0] = vectoring_info;
9669 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009670 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9671 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9672 vcpu->run->internal.ndata++;
9673 vcpu->run->internal.data[3] =
9674 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9675 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009676 return 0;
9677 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009678
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009679 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009680 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9681 if (vmx_interrupt_allowed(vcpu)) {
9682 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9683 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9684 vcpu->arch.nmi_pending) {
9685 /*
9686 * This CPU don't support us in finding the end of an
9687 * NMI-blocked window if the guest runs with IRQs
9688 * disabled. So we pull the trigger after 1 s of
9689 * futile waiting, but inform the user about this.
9690 */
9691 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9692 "state on VCPU %d after 1 s timeout\n",
9693 __func__, vcpu->vcpu_id);
9694 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9695 }
9696 }
9697
Avi Kivity6aa8b732006-12-10 02:21:36 -08009698 if (exit_reason < kvm_vmx_max_exit_handlers
9699 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009700 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009701 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009702 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9703 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009704 kvm_queue_exception(vcpu, UD_VECTOR);
9705 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009706 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009707}
9708
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009709/*
9710 * Software based L1D cache flush which is used when microcode providing
9711 * the cache control MSR is not loaded.
9712 *
9713 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9714 * flush it is required to read in 64 KiB because the replacement algorithm
9715 * is not exactly LRU. This could be sized at runtime via topology
9716 * information but as all relevant affected CPUs have 32KiB L1D cache size
9717 * there is no point in doing so.
9718 */
9719#define L1D_CACHE_ORDER 4
9720static void *vmx_l1d_flush_pages;
9721
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009722static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009723{
9724 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009725
9726 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009727 * This code is only executed when the the flush mode is 'cond' or
9728 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009729 */
Nicolai Stange427362a2018-07-21 22:25:00 +02009730 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02009731 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02009732
Nicolai Stange379fd0c2018-07-21 22:16:56 +02009733 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02009734 * Clear the per-vcpu flush bit, it gets set again
9735 * either from vcpu_run() or from one of the unsafe
9736 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02009737 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02009738 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02009739 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02009740
9741 /*
9742 * Clear the per-cpu flush bit, it gets set again from
9743 * the interrupt handlers.
9744 */
9745 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
9746 kvm_clear_cpu_l1tf_flush_l1d();
9747
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02009748 if (!flush_l1d)
9749 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02009750 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009751
9752 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009753
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009754 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9755 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9756 return;
9757 }
9758
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009759 asm volatile(
9760 /* First ensure the pages are in the TLB */
9761 "xorl %%eax, %%eax\n"
9762 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02009763 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009764 "addl $4096, %%eax\n\t"
9765 "cmpl %%eax, %[size]\n\t"
9766 "jne .Lpopulate_tlb\n\t"
9767 "xorl %%eax, %%eax\n\t"
9768 "cpuid\n\t"
9769 /* Now fill the cache */
9770 "xorl %%eax, %%eax\n"
9771 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02009772 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009773 "addl $64, %%eax\n\t"
9774 "cmpl %%eax, %[size]\n\t"
9775 "jne .Lfill_cache\n\t"
9776 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02009777 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009778 [size] "r" (size)
9779 : "eax", "ebx", "ecx", "edx");
9780}
9781
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009782static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009783{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009784 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9785
9786 if (is_guest_mode(vcpu) &&
9787 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9788 return;
9789
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009790 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009791 vmcs_write32(TPR_THRESHOLD, 0);
9792 return;
9793 }
9794
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009795 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009796}
9797
Jim Mattson8d860bb2018-05-09 16:56:05 -04009798static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009799{
9800 u32 sec_exec_control;
9801
Jim Mattson8d860bb2018-05-09 16:56:05 -04009802 if (!lapic_in_kernel(vcpu))
9803 return;
9804
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009805 /* Postpone execution until vmcs01 is the current VMCS. */
9806 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009807 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009808 return;
9809 }
9810
Paolo Bonzini35754c92015-07-29 12:05:37 +02009811 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009812 return;
9813
9814 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009815 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9816 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009817
Jim Mattson8d860bb2018-05-09 16:56:05 -04009818 switch (kvm_get_apic_mode(vcpu)) {
9819 case LAPIC_MODE_INVALID:
9820 WARN_ONCE(true, "Invalid local APIC state");
9821 case LAPIC_MODE_DISABLED:
9822 break;
9823 case LAPIC_MODE_XAPIC:
9824 if (flexpriority_enabled) {
9825 sec_exec_control |=
9826 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9827 vmx_flush_tlb(vcpu, true);
9828 }
9829 break;
9830 case LAPIC_MODE_X2APIC:
9831 if (cpu_has_vmx_virtualize_x2apic_mode())
9832 sec_exec_control |=
9833 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9834 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009835 }
9836 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9837
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009838 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009839}
9840
Tang Chen38b99172014-09-24 15:57:54 +08009841static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9842{
Jim Mattsonab5df312018-05-09 17:02:03 -04009843 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009844 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009845 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009846 }
Tang Chen38b99172014-09-24 15:57:54 +08009847}
9848
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009849static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009850{
9851 u16 status;
9852 u8 old;
9853
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009854 if (max_isr == -1)
9855 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009856
9857 status = vmcs_read16(GUEST_INTR_STATUS);
9858 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009859 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009860 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009861 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009862 vmcs_write16(GUEST_INTR_STATUS, status);
9863 }
9864}
9865
9866static void vmx_set_rvi(int vector)
9867{
9868 u16 status;
9869 u8 old;
9870
Wei Wang4114c272014-11-05 10:53:43 +08009871 if (vector == -1)
9872 vector = 0;
9873
Yang Zhangc7c9c562013-01-25 10:18:51 +08009874 status = vmcs_read16(GUEST_INTR_STATUS);
9875 old = (u8)status & 0xff;
9876 if ((u8)vector != old) {
9877 status &= ~0xff;
9878 status |= (u8)vector;
9879 vmcs_write16(GUEST_INTR_STATUS, status);
9880 }
9881}
9882
9883static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9884{
Liran Alon851c1a182017-12-24 18:12:56 +02009885 /*
9886 * When running L2, updating RVI is only relevant when
9887 * vmcs12 virtual-interrupt-delivery enabled.
9888 * However, it can be enabled only when L1 also
9889 * intercepts external-interrupts and in that case
9890 * we should not update vmcs02 RVI but instead intercept
9891 * interrupt. Therefore, do nothing when running L2.
9892 */
9893 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009894 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009895}
9896
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009897static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009898{
9899 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009900 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009901 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009902
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009903 WARN_ON(!vcpu->arch.apicv_active);
9904 if (pi_test_on(&vmx->pi_desc)) {
9905 pi_clear_on(&vmx->pi_desc);
9906 /*
9907 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9908 * But on x86 this is just a compiler barrier anyway.
9909 */
9910 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009911 max_irr_updated =
9912 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9913
9914 /*
9915 * If we are running L2 and L1 has a new pending interrupt
9916 * which can be injected, we should re-evaluate
9917 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009918 * If L1 intercepts external-interrupts, we should
9919 * exit from L2 to L1. Otherwise, interrupt should be
9920 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009921 */
Liran Alon851c1a182017-12-24 18:12:56 +02009922 if (is_guest_mode(vcpu) && max_irr_updated) {
9923 if (nested_exit_on_intr(vcpu))
9924 kvm_vcpu_exiting_guest_mode(vcpu);
9925 else
9926 kvm_make_request(KVM_REQ_EVENT, vcpu);
9927 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009928 } else {
9929 max_irr = kvm_lapic_find_highest_irr(vcpu);
9930 }
9931 vmx_hwapic_irr_update(vcpu, max_irr);
9932 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009933}
9934
Andrey Smetanin63086302015-11-10 15:36:32 +03009935static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009936{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009937 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009938 return;
9939
Yang Zhangc7c9c562013-01-25 10:18:51 +08009940 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9941 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9942 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9943 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9944}
9945
Paolo Bonzini967235d2016-12-19 14:03:45 +01009946static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9947{
9948 struct vcpu_vmx *vmx = to_vmx(vcpu);
9949
9950 pi_clear_on(&vmx->pi_desc);
9951 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9952}
9953
Avi Kivity51aa01d2010-07-20 14:31:20 +03009954static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009955{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009956 u32 exit_intr_info = 0;
9957 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009958
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009959 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9960 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009961 return;
9962
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009963 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9964 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9965 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009966
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009967 /* if exit due to PF check for async PF */
9968 if (is_page_fault(exit_intr_info))
9969 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9970
Andi Kleena0861c02009-06-08 17:37:09 +08009971 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009972 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9973 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009974 kvm_machine_check();
9975
Gleb Natapov20f65982009-05-11 13:35:55 +03009976 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009977 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009978 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009979 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009980 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009981 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009982}
Gleb Natapov20f65982009-05-11 13:35:55 +03009983
Yang Zhanga547c6d2013-04-11 19:25:10 +08009984static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9985{
9986 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9987
Yang Zhanga547c6d2013-04-11 19:25:10 +08009988 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9989 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9990 unsigned int vector;
9991 unsigned long entry;
9992 gate_desc *desc;
9993 struct vcpu_vmx *vmx = to_vmx(vcpu);
9994#ifdef CONFIG_X86_64
9995 unsigned long tmp;
9996#endif
9997
9998 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9999 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010000 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010001 asm volatile(
10002#ifdef CONFIG_X86_64
10003 "mov %%" _ASM_SP ", %[sp]\n\t"
10004 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10005 "push $%c[ss]\n\t"
10006 "push %[sp]\n\t"
10007#endif
10008 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010009 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010010 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010011 :
10012#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010013 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010014#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010015 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010016 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010017 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010018 [ss]"i"(__KERNEL_DS),
10019 [cs]"i"(__KERNEL_CS)
10020 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010021 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010022}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010023STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010024
Tom Lendackybc226f02018-05-10 22:06:39 +020010025static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010026{
Tom Lendackybc226f02018-05-10 22:06:39 +020010027 switch (index) {
10028 case MSR_IA32_SMBASE:
10029 /*
10030 * We cannot do SMM unless we can run the guest in big
10031 * real mode.
10032 */
10033 return enable_unrestricted_guest || emulate_invalid_guest_state;
10034 case MSR_AMD64_VIRT_SPEC_CTRL:
10035 /* This is AMD only. */
10036 return false;
10037 default:
10038 return true;
10039 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010040}
10041
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010042static bool vmx_mpx_supported(void)
10043{
10044 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10045 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10046}
10047
Wanpeng Li55412b22014-12-02 19:21:30 +080010048static bool vmx_xsaves_supported(void)
10049{
10050 return vmcs_config.cpu_based_2nd_exec_ctrl &
10051 SECONDARY_EXEC_XSAVES;
10052}
10053
Avi Kivity51aa01d2010-07-20 14:31:20 +030010054static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10055{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010056 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010057 bool unblock_nmi;
10058 u8 vector;
10059 bool idtv_info_valid;
10060
10061 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010062
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010063 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010064 if (vmx->loaded_vmcs->nmi_known_unmasked)
10065 return;
10066 /*
10067 * Can't use vmx->exit_intr_info since we're not sure what
10068 * the exit reason is.
10069 */
10070 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10071 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10072 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10073 /*
10074 * SDM 3: 27.7.1.2 (September 2008)
10075 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10076 * a guest IRET fault.
10077 * SDM 3: 23.2.2 (September 2008)
10078 * Bit 12 is undefined in any of the following cases:
10079 * If the VM exit sets the valid bit in the IDT-vectoring
10080 * information field.
10081 * If the VM exit is due to a double fault.
10082 */
10083 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10084 vector != DF_VECTOR && !idtv_info_valid)
10085 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10086 GUEST_INTR_STATE_NMI);
10087 else
10088 vmx->loaded_vmcs->nmi_known_unmasked =
10089 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10090 & GUEST_INTR_STATE_NMI);
10091 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10092 vmx->loaded_vmcs->vnmi_blocked_time +=
10093 ktime_to_ns(ktime_sub(ktime_get(),
10094 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010095}
10096
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010097static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010098 u32 idt_vectoring_info,
10099 int instr_len_field,
10100 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010101{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010102 u8 vector;
10103 int type;
10104 bool idtv_info_valid;
10105
10106 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010107
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010108 vcpu->arch.nmi_injected = false;
10109 kvm_clear_exception_queue(vcpu);
10110 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010111
10112 if (!idtv_info_valid)
10113 return;
10114
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010115 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010116
Avi Kivity668f6122008-07-02 09:28:55 +030010117 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10118 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010119
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010120 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010121 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010122 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010123 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010124 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010125 * Clear bit "block by NMI" before VM entry if a NMI
10126 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010127 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010128 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010129 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010130 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010131 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010132 /* fall through */
10133 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010134 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010135 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010136 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010137 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010138 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010139 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010140 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010141 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010142 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010143 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010144 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010145 break;
10146 default:
10147 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010148 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010149}
10150
Avi Kivity83422e12010-07-20 14:43:23 +030010151static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10152{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010153 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010154 VM_EXIT_INSTRUCTION_LEN,
10155 IDT_VECTORING_ERROR_CODE);
10156}
10157
Avi Kivityb463a6f2010-07-20 15:06:17 +030010158static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10159{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010160 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010161 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10162 VM_ENTRY_INSTRUCTION_LEN,
10163 VM_ENTRY_EXCEPTION_ERROR_CODE);
10164
10165 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10166}
10167
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010168static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10169{
10170 int i, nr_msrs;
10171 struct perf_guest_switch_msr *msrs;
10172
10173 msrs = perf_guest_get_msrs(&nr_msrs);
10174
10175 if (!msrs)
10176 return;
10177
10178 for (i = 0; i < nr_msrs; i++)
10179 if (msrs[i].host == msrs[i].guest)
10180 clear_atomic_switch_msr(vmx, msrs[i].msr);
10181 else
10182 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010183 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010184}
10185
Jiang Biao33365e72016-11-03 15:03:37 +080010186static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010187{
10188 struct vcpu_vmx *vmx = to_vmx(vcpu);
10189 u64 tscl;
10190 u32 delta_tsc;
10191
10192 if (vmx->hv_deadline_tsc == -1)
10193 return;
10194
10195 tscl = rdtsc();
10196 if (vmx->hv_deadline_tsc > tscl)
10197 /* sure to be 32 bit only because checked on set_hv_timer */
10198 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10199 cpu_preemption_timer_multi);
10200 else
10201 delta_tsc = 0;
10202
10203 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10204}
10205
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010206static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010207{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010208 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010209 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010210
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010211 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010212 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010213 vmx->loaded_vmcs->soft_vnmi_blocked))
10214 vmx->loaded_vmcs->entry_time = ktime_get();
10215
Avi Kivity104f2262010-11-18 13:12:52 +020010216 /* Don't enter VMX if guest state is invalid, let the exit handler
10217 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010218 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010219 return;
10220
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010221 if (vmx->ple_window_dirty) {
10222 vmx->ple_window_dirty = false;
10223 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10224 }
10225
Abel Gordon012f83c2013-04-18 14:39:25 +030010226 if (vmx->nested.sync_shadow_vmcs) {
10227 copy_vmcs12_to_shadow(vmx);
10228 vmx->nested.sync_shadow_vmcs = false;
10229 }
10230
Avi Kivity104f2262010-11-18 13:12:52 +020010231 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10232 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10233 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10234 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10235
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010236 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010237 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010238 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010239 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010240 }
10241
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010242 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010243 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010244 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010245 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010246 }
10247
Avi Kivity104f2262010-11-18 13:12:52 +020010248 /* When single-stepping over STI and MOV SS, we must clear the
10249 * corresponding interruptibility bits in the guest state. Otherwise
10250 * vmentry fails as it then expects bit 14 (BS) in pending debug
10251 * exceptions being set, but that's not correct for the guest debugging
10252 * case. */
10253 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10254 vmx_set_interrupt_shadow(vcpu, 0);
10255
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010256 if (static_cpu_has(X86_FEATURE_PKU) &&
10257 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10258 vcpu->arch.pkru != vmx->host_pkru)
10259 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010260
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010261 atomic_switch_perf_msrs(vmx);
10262
Yunhong Jiang64672c92016-06-13 14:19:59 -070010263 vmx_arm_hv_timer(vcpu);
10264
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010265 /*
10266 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10267 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10268 * is no need to worry about the conditional branch over the wrmsr
10269 * being speculatively taken.
10270 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010271 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010272
Nadav Har'Eld462b812011-05-24 15:26:10 +030010273 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010274
10275 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10276 (unsigned long)&current_evmcs->host_rsp : 0;
10277
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010278 if (static_branch_unlikely(&vmx_l1d_should_flush))
10279 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010280
Avi Kivity104f2262010-11-18 13:12:52 +020010281 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010282 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010283 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10284 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10285 "push %%" _ASM_CX " \n\t"
10286 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010287 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010288 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010289 /* Avoid VMWRITE when Enlightened VMCS is in use */
10290 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10291 "jz 2f \n\t"
10292 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10293 "jmp 1f \n\t"
10294 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010295 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010296 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010297 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010298 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10299 "mov %%cr2, %%" _ASM_DX " \n\t"
10300 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010301 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010302 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010303 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010304 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010305 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010306 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010307 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10308 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10309 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10310 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10311 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10312 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010313#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010314 "mov %c[r8](%0), %%r8 \n\t"
10315 "mov %c[r9](%0), %%r9 \n\t"
10316 "mov %c[r10](%0), %%r10 \n\t"
10317 "mov %c[r11](%0), %%r11 \n\t"
10318 "mov %c[r12](%0), %%r12 \n\t"
10319 "mov %c[r13](%0), %%r13 \n\t"
10320 "mov %c[r14](%0), %%r14 \n\t"
10321 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010322#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010323 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010324
Avi Kivity6aa8b732006-12-10 02:21:36 -080010325 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010326 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010327 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010328 "jmp 2f \n\t"
10329 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10330 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010331 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010332 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010333 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010334 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010335 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10336 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10337 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10338 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10339 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10340 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10341 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010342#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010343 "mov %%r8, %c[r8](%0) \n\t"
10344 "mov %%r9, %c[r9](%0) \n\t"
10345 "mov %%r10, %c[r10](%0) \n\t"
10346 "mov %%r11, %c[r11](%0) \n\t"
10347 "mov %%r12, %c[r12](%0) \n\t"
10348 "mov %%r13, %c[r13](%0) \n\t"
10349 "mov %%r14, %c[r14](%0) \n\t"
10350 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010351 "xor %%r8d, %%r8d \n\t"
10352 "xor %%r9d, %%r9d \n\t"
10353 "xor %%r10d, %%r10d \n\t"
10354 "xor %%r11d, %%r11d \n\t"
10355 "xor %%r12d, %%r12d \n\t"
10356 "xor %%r13d, %%r13d \n\t"
10357 "xor %%r14d, %%r14d \n\t"
10358 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010359#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010360 "mov %%cr2, %%" _ASM_AX " \n\t"
10361 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010362
Jim Mattson0cb5b302018-01-03 14:31:38 -080010363 "xor %%eax, %%eax \n\t"
10364 "xor %%ebx, %%ebx \n\t"
10365 "xor %%esi, %%esi \n\t"
10366 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010367 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010368 ".pushsection .rodata \n\t"
10369 ".global vmx_return \n\t"
10370 "vmx_return: " _ASM_PTR " 2b \n\t"
10371 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010372 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010373 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010374 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010375 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010376 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10377 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10378 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10379 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10380 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10381 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10382 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010383#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010384 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10385 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10386 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10387 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10388 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10389 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10390 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10391 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010392#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010393 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10394 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010395 : "cc", "memory"
10396#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010397 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010398 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010399#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010400 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010401#endif
10402 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010403
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010404 /*
10405 * We do not use IBRS in the kernel. If this vCPU has used the
10406 * SPEC_CTRL MSR it may have left it on; save the value and
10407 * turn it off. This is much more efficient than blindly adding
10408 * it to the atomic save/restore list. Especially as the former
10409 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10410 *
10411 * For non-nested case:
10412 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10413 * save it.
10414 *
10415 * For nested case:
10416 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10417 * save it.
10418 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010419 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010420 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010421
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010422 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010423
David Woodhouse117cc7a2018-01-12 11:11:27 +000010424 /* Eliminate branch target predictions from guest mode */
10425 vmexit_fill_RSB();
10426
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010427 /* All fields are clean at this point */
10428 if (static_branch_unlikely(&enable_evmcs))
10429 current_evmcs->hv_clean_fields |=
10430 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10431
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010432 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010433 if (vmx->host_debugctlmsr)
10434 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010435
Avi Kivityaa67f602012-08-01 16:48:03 +030010436#ifndef CONFIG_X86_64
10437 /*
10438 * The sysexit path does not restore ds/es, so we must set them to
10439 * a reasonable value ourselves.
10440 *
10441 * We can't defer this to vmx_load_host_state() since that function
10442 * may be executed in interrupt context, which saves and restore segments
10443 * around it, nullifying its effect.
10444 */
10445 loadsegment(ds, __USER_DS);
10446 loadsegment(es, __USER_DS);
10447#endif
10448
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010449 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010450 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010451 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010452 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010453 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010454 vcpu->arch.regs_dirty = 0;
10455
Gleb Natapove0b890d2013-09-25 12:51:33 +030010456 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010457 * eager fpu is enabled if PKEY is supported and CR4 is switched
10458 * back on host, so it is safe to read guest PKRU from current
10459 * XSAVE.
10460 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010461 if (static_cpu_has(X86_FEATURE_PKU) &&
10462 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10463 vcpu->arch.pkru = __read_pkru();
10464 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010465 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010466 }
10467
Gleb Natapove0b890d2013-09-25 12:51:33 +030010468 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010469 vmx->idt_vectoring_info = 0;
10470
10471 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10472 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10473 return;
10474
10475 vmx->loaded_vmcs->launched = 1;
10476 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010477
Avi Kivity51aa01d2010-07-20 14:31:20 +030010478 vmx_complete_atomic_exit(vmx);
10479 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010480 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010481}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010482STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010483
Sean Christopherson434a1e92018-03-20 12:17:18 -070010484static struct kvm *vmx_vm_alloc(void)
10485{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010486 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010487 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010488}
10489
10490static void vmx_vm_free(struct kvm *kvm)
10491{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010492 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010493}
10494
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010495static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010496{
10497 struct vcpu_vmx *vmx = to_vmx(vcpu);
10498 int cpu;
10499
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010500 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010501 return;
10502
10503 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010504 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010505 vmx_vcpu_put(vcpu);
10506 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010507 put_cpu();
10508}
10509
Jim Mattson2f1fe812016-07-08 15:36:06 -070010510/*
10511 * Ensure that the current vmcs of the logical processor is the
10512 * vmcs01 of the vcpu before calling free_nested().
10513 */
10514static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10515{
10516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010517
Christoffer Dallec7660c2017-12-04 21:35:23 +010010518 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010519 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010520 free_nested(vmx);
10521 vcpu_put(vcpu);
10522}
10523
Avi Kivity6aa8b732006-12-10 02:21:36 -080010524static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10525{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010526 struct vcpu_vmx *vmx = to_vmx(vcpu);
10527
Kai Huang843e4332015-01-28 10:54:28 +080010528 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010529 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010530 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010531 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010532 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010533 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010534 kfree(vmx->guest_msrs);
10535 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010536 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010537}
10538
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010539static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010540{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010541 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010542 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010543 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010544 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010545
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010546 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010547 return ERR_PTR(-ENOMEM);
10548
Wanpeng Li991e7a02015-09-16 17:30:05 +080010549 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010550
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010551 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10552 if (err)
10553 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010554
Peter Feiner4e595162016-07-07 14:49:58 -070010555 err = -ENOMEM;
10556
10557 /*
10558 * If PML is turned on, failure on enabling PML just results in failure
10559 * of creating the vcpu, therefore we can simplify PML logic (by
10560 * avoiding dealing with cases, such as enabling PML partially on vcpus
10561 * for the guest, etc.
10562 */
10563 if (enable_pml) {
10564 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10565 if (!vmx->pml_pg)
10566 goto uninit_vcpu;
10567 }
10568
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010569 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010570 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10571 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010572
Peter Feiner4e595162016-07-07 14:49:58 -070010573 if (!vmx->guest_msrs)
10574 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010575
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010576 err = alloc_loaded_vmcs(&vmx->vmcs01);
10577 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010578 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010579
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010580 msr_bitmap = vmx->vmcs01.msr_bitmap;
10581 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10582 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10583 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10584 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10585 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10586 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10587 vmx->msr_bitmap_mode = 0;
10588
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010589 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010590 cpu = get_cpu();
10591 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010592 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010593 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010594 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010595 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010596 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010597 err = alloc_apic_access_page(kvm);
10598 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010599 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010600 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010601
Sean Christophersone90008d2018-03-05 12:04:37 -080010602 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010603 err = init_rmode_identity_map(kvm);
10604 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010605 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010606 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010607
Wanpeng Li5c614b32015-10-13 09:18:36 -070010608 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010609 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10610 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010611 vmx->nested.vpid02 = allocate_vpid();
10612 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010613
Wincy Van705699a2015-02-03 23:58:17 +080010614 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010615 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010616
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010617 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10618
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010619 /*
10620 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10621 * or POSTED_INTR_WAKEUP_VECTOR.
10622 */
10623 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10624 vmx->pi_desc.sn = 1;
10625
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010626 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010627
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010628free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010629 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010630 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010631free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010632 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010633free_pml:
10634 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010635uninit_vcpu:
10636 kvm_vcpu_uninit(&vmx->vcpu);
10637free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010638 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010639 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010640 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010641}
10642
Jiri Kosinad90a7a02018-07-13 16:23:25 +020010643#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
10644#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010645
Wanpeng Lib31c1142018-03-12 04:53:04 -070010646static int vmx_vm_init(struct kvm *kvm)
10647{
10648 if (!ple_gap)
10649 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010650
Jiri Kosinad90a7a02018-07-13 16:23:25 +020010651 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
10652 switch (l1tf_mitigation) {
10653 case L1TF_MITIGATION_OFF:
10654 case L1TF_MITIGATION_FLUSH_NOWARN:
10655 /* 'I explicitly don't care' is set */
10656 break;
10657 case L1TF_MITIGATION_FLUSH:
10658 case L1TF_MITIGATION_FLUSH_NOSMT:
10659 case L1TF_MITIGATION_FULL:
10660 /*
10661 * Warn upon starting the first VM in a potentially
10662 * insecure environment.
10663 */
10664 if (cpu_smt_control == CPU_SMT_ENABLED)
10665 pr_warn_once(L1TF_MSG_SMT);
10666 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
10667 pr_warn_once(L1TF_MSG_L1D);
10668 break;
10669 case L1TF_MITIGATION_FULL_FORCE:
10670 /* Flush is enforced */
10671 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010672 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010673 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010674 return 0;
10675}
10676
Yang, Sheng002c7f72007-07-31 14:23:01 +030010677static void __init vmx_check_processor_compat(void *rtn)
10678{
10679 struct vmcs_config vmcs_conf;
10680
10681 *(int *)rtn = 0;
10682 if (setup_vmcs_config(&vmcs_conf) < 0)
10683 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010684 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010685 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10686 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10687 smp_processor_id());
10688 *(int *)rtn = -EIO;
10689 }
10690}
10691
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010692static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010693{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010694 u8 cache;
10695 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010696
Sheng Yang522c68c2009-04-27 20:35:43 +080010697 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010698 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010699 * 2. EPT with VT-d:
10700 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010701 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010702 * b. VT-d with snooping control feature: snooping control feature of
10703 * VT-d engine can guarantee the cache correctness. Just set it
10704 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010705 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010706 * consistent with host MTRR
10707 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010708 if (is_mmio) {
10709 cache = MTRR_TYPE_UNCACHABLE;
10710 goto exit;
10711 }
10712
10713 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010714 ipat = VMX_EPT_IPAT_BIT;
10715 cache = MTRR_TYPE_WRBACK;
10716 goto exit;
10717 }
10718
10719 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10720 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010721 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010722 cache = MTRR_TYPE_WRBACK;
10723 else
10724 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010725 goto exit;
10726 }
10727
Xiao Guangrongff536042015-06-15 16:55:22 +080010728 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010729
10730exit:
10731 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010732}
10733
Sheng Yang17cc3932010-01-05 19:02:27 +080010734static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010735{
Sheng Yang878403b2010-01-05 19:02:29 +080010736 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10737 return PT_DIRECTORY_LEVEL;
10738 else
10739 /* For shadow and EPT supported 1GB page */
10740 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010741}
10742
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010743static void vmcs_set_secondary_exec_control(u32 new_ctl)
10744{
10745 /*
10746 * These bits in the secondary execution controls field
10747 * are dynamic, the others are mostly based on the hypervisor
10748 * architecture and the guest's CPUID. Do not touch the
10749 * dynamic bits.
10750 */
10751 u32 mask =
10752 SECONDARY_EXEC_SHADOW_VMCS |
10753 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010754 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10755 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010756
10757 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10758
10759 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10760 (new_ctl & ~mask) | (cur_ctl & mask));
10761}
10762
David Matlack8322ebb2016-11-29 18:14:09 -080010763/*
10764 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10765 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10766 */
10767static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10768{
10769 struct vcpu_vmx *vmx = to_vmx(vcpu);
10770 struct kvm_cpuid_entry2 *entry;
10771
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010772 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10773 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010774
10775#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10776 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010777 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010778} while (0)
10779
10780 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10781 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10782 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10783 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10784 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10785 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10786 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10787 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10788 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10789 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10790 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10791 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10792 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10793 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10794 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10795
10796 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10797 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10798 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10799 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10800 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010801 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010802
10803#undef cr4_fixed1_update
10804}
10805
Sheng Yang0e851882009-12-18 16:48:46 +080010806static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10807{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010808 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010809
Paolo Bonzini80154d72017-08-24 13:55:35 +020010810 if (cpu_has_secondary_exec_ctrls()) {
10811 vmx_compute_secondary_exec_control(vmx);
10812 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010813 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010814
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010815 if (nested_vmx_allowed(vcpu))
10816 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10817 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10818 else
10819 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10820 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010821
10822 if (nested_vmx_allowed(vcpu))
10823 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010824}
10825
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010826static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10827{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010828 if (func == 1 && nested)
10829 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010830}
10831
Yang Zhang25d92082013-08-06 12:00:32 +030010832static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10833 struct x86_exception *fault)
10834{
Jan Kiszka533558b2014-01-04 18:47:20 +010010835 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010837 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010838 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010839
Bandan Dasc5f983f2017-05-05 15:25:14 -040010840 if (vmx->nested.pml_full) {
10841 exit_reason = EXIT_REASON_PML_FULL;
10842 vmx->nested.pml_full = false;
10843 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10844 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010845 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010846 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010847 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010848
10849 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010850 vmcs12->guest_physical_address = fault->address;
10851}
10852
Peter Feiner995f00a2017-06-30 17:26:32 -070010853static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10854{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010855 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010856}
10857
Nadav Har'El155a97a2013-08-05 11:07:16 +030010858/* Callbacks for nested_ept_init_mmu_context: */
10859
10860static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10861{
10862 /* return the page table to be shadowed - in our case, EPT12 */
10863 return get_vmcs12(vcpu)->ept_pointer;
10864}
10865
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010866static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010867{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010868 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010869 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010870 return 1;
10871
10872 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010873 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010874 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010875 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010876 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010877 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10878 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10879 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10880
10881 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010882 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010883}
10884
10885static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10886{
10887 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10888}
10889
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010890static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10891 u16 error_code)
10892{
10893 bool inequality, bit;
10894
10895 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10896 inequality =
10897 (error_code & vmcs12->page_fault_error_code_mask) !=
10898 vmcs12->page_fault_error_code_match;
10899 return inequality ^ bit;
10900}
10901
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010902static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10903 struct x86_exception *fault)
10904{
10905 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10906
10907 WARN_ON(!is_guest_mode(vcpu));
10908
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010909 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10910 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010911 vmcs12->vm_exit_intr_error_code = fault->error_code;
10912 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10913 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10914 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10915 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010916 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010917 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010918 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010919}
10920
Paolo Bonzinic9923842017-12-13 14:16:30 +010010921static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10922 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010923
10924static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010925 struct vmcs12 *vmcs12)
10926{
10927 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010928 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010929 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010930
10931 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010932 /*
10933 * Translate L1 physical address to host physical
10934 * address for vmcs02. Keep the page pinned, so this
10935 * physical address remains valid. We keep a reference
10936 * to it so we can release it later.
10937 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010938 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010939 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010940 vmx->nested.apic_access_page = NULL;
10941 }
10942 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010943 /*
10944 * If translation failed, no matter: This feature asks
10945 * to exit when accessing the given address, and if it
10946 * can never be accessed, this feature won't do
10947 * anything anyway.
10948 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010949 if (!is_error_page(page)) {
10950 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010951 hpa = page_to_phys(vmx->nested.apic_access_page);
10952 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10953 } else {
10954 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10955 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10956 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010957 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010958
10959 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010960 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010961 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010962 vmx->nested.virtual_apic_page = NULL;
10963 }
10964 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010965
10966 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010967 * If translation failed, VM entry will fail because
10968 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10969 * Failing the vm entry is _not_ what the processor
10970 * does but it's basically the only possibility we
10971 * have. We could still enter the guest if CR8 load
10972 * exits are enabled, CR8 store exits are enabled, and
10973 * virtualize APIC access is disabled; in this case
10974 * the processor would never use the TPR shadow and we
10975 * could simply clear the bit from the execution
10976 * control. But such a configuration is useless, so
10977 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010978 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010979 if (!is_error_page(page)) {
10980 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010981 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10982 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10983 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010984 }
10985
Wincy Van705699a2015-02-03 23:58:17 +080010986 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010987 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10988 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010989 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010990 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010991 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010992 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10993 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010994 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010995 vmx->nested.pi_desc_page = page;
10996 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010997 vmx->nested.pi_desc =
10998 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10999 (unsigned long)(vmcs12->posted_intr_desc_addr &
11000 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011001 vmcs_write64(POSTED_INTR_DESC_ADDR,
11002 page_to_phys(vmx->nested.pi_desc_page) +
11003 (unsigned long)(vmcs12->posted_intr_desc_addr &
11004 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011005 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011006 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011007 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11008 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011009 else
11010 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11011 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011012}
11013
Jan Kiszkaf4124502014-03-07 20:03:13 +010011014static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11015{
11016 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11017 struct vcpu_vmx *vmx = to_vmx(vcpu);
11018
11019 if (vcpu->arch.virtual_tsc_khz == 0)
11020 return;
11021
11022 /* Make sure short timeouts reliably trigger an immediate vmexit.
11023 * hrtimer_start does not guarantee this. */
11024 if (preemption_timeout <= 1) {
11025 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11026 return;
11027 }
11028
11029 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11030 preemption_timeout *= 1000000;
11031 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11032 hrtimer_start(&vmx->nested.preemption_timer,
11033 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11034}
11035
Jim Mattson56a20512017-07-06 16:33:06 -070011036static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11037 struct vmcs12 *vmcs12)
11038{
11039 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11040 return 0;
11041
11042 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11043 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11044 return -EINVAL;
11045
11046 return 0;
11047}
11048
Wincy Van3af18d92015-02-03 23:49:31 +080011049static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11050 struct vmcs12 *vmcs12)
11051{
Wincy Van3af18d92015-02-03 23:49:31 +080011052 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11053 return 0;
11054
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011055 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011056 return -EINVAL;
11057
11058 return 0;
11059}
11060
Jim Mattson712b12d2017-08-24 13:24:47 -070011061static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11062 struct vmcs12 *vmcs12)
11063{
11064 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11065 return 0;
11066
11067 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11068 return -EINVAL;
11069
11070 return 0;
11071}
11072
Wincy Van3af18d92015-02-03 23:49:31 +080011073/*
11074 * Merge L0's and L1's MSR bitmap, return false to indicate that
11075 * we do not use the hardware.
11076 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011077static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11078 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011079{
Wincy Van82f0dd42015-02-03 23:57:18 +080011080 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011081 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011082 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011083 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011084 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011085 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011086 *
11087 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11088 * ensures that we do not accidentally generate an L02 MSR bitmap
11089 * from the L12 MSR bitmap that is too permissive.
11090 * 2. That L1 or L2s have actually used the MSR. This avoids
11091 * unnecessarily merging of the bitmap if the MSR is unused. This
11092 * works properly because we only update the L01 MSR bitmap lazily.
11093 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11094 * updated to reflect this when L1 (or its L2s) actually write to
11095 * the MSR.
11096 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011097 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11098 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011099
Paolo Bonzinic9923842017-12-13 14:16:30 +010011100 /* Nothing to do if the MSR bitmap is not in use. */
11101 if (!cpu_has_vmx_msr_bitmap() ||
11102 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11103 return false;
11104
Ashok Raj15d45072018-02-01 22:59:43 +010011105 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011106 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011107 return false;
11108
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011109 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11110 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011111 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011112
Radim Krčmářd048c092016-08-08 20:16:22 +020011113 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011114 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11115 /*
11116 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11117 * just lets the processor take the value from the virtual-APIC page;
11118 * take those 256 bits directly from the L1 bitmap.
11119 */
11120 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11121 unsigned word = msr / BITS_PER_LONG;
11122 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11123 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011124 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011125 } else {
11126 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11127 unsigned word = msr / BITS_PER_LONG;
11128 msr_bitmap_l0[word] = ~0;
11129 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11130 }
11131 }
11132
11133 nested_vmx_disable_intercept_for_msr(
11134 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011135 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011136 MSR_TYPE_W);
11137
11138 if (nested_cpu_has_vid(vmcs12)) {
11139 nested_vmx_disable_intercept_for_msr(
11140 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011141 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011142 MSR_TYPE_W);
11143 nested_vmx_disable_intercept_for_msr(
11144 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011145 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011146 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011147 }
Ashok Raj15d45072018-02-01 22:59:43 +010011148
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011149 if (spec_ctrl)
11150 nested_vmx_disable_intercept_for_msr(
11151 msr_bitmap_l1, msr_bitmap_l0,
11152 MSR_IA32_SPEC_CTRL,
11153 MSR_TYPE_R | MSR_TYPE_W);
11154
Ashok Raj15d45072018-02-01 22:59:43 +010011155 if (pred_cmd)
11156 nested_vmx_disable_intercept_for_msr(
11157 msr_bitmap_l1, msr_bitmap_l0,
11158 MSR_IA32_PRED_CMD,
11159 MSR_TYPE_W);
11160
Wincy Vanf2b93282015-02-03 23:56:03 +080011161 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011162 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011163
11164 return true;
11165}
11166
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011167static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11168 struct vmcs12 *vmcs12)
11169{
11170 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11171 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11172 return -EINVAL;
11173 else
11174 return 0;
11175}
11176
Wincy Vanf2b93282015-02-03 23:56:03 +080011177static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11178 struct vmcs12 *vmcs12)
11179{
Wincy Van82f0dd42015-02-03 23:57:18 +080011180 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011181 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011182 !nested_cpu_has_vid(vmcs12) &&
11183 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011184 return 0;
11185
11186 /*
11187 * If virtualize x2apic mode is enabled,
11188 * virtualize apic access must be disabled.
11189 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011190 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11191 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011192 return -EINVAL;
11193
Wincy Van608406e2015-02-03 23:57:51 +080011194 /*
11195 * If virtual interrupt delivery is enabled,
11196 * we must exit on external interrupts.
11197 */
11198 if (nested_cpu_has_vid(vmcs12) &&
11199 !nested_exit_on_intr(vcpu))
11200 return -EINVAL;
11201
Wincy Van705699a2015-02-03 23:58:17 +080011202 /*
11203 * bits 15:8 should be zero in posted_intr_nv,
11204 * the descriptor address has been already checked
11205 * in nested_get_vmcs12_pages.
11206 */
11207 if (nested_cpu_has_posted_intr(vmcs12) &&
11208 (!nested_cpu_has_vid(vmcs12) ||
11209 !nested_exit_intr_ack_set(vcpu) ||
11210 vmcs12->posted_intr_nv & 0xff00))
11211 return -EINVAL;
11212
Wincy Vanf2b93282015-02-03 23:56:03 +080011213 /* tpr shadow is needed by all apicv features. */
11214 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11215 return -EINVAL;
11216
11217 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011218}
11219
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011220static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11221 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011222 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011223{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011224 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011225 u64 count, addr;
11226
11227 if (vmcs12_read_any(vcpu, count_field, &count) ||
11228 vmcs12_read_any(vcpu, addr_field, &addr)) {
11229 WARN_ON(1);
11230 return -EINVAL;
11231 }
11232 if (count == 0)
11233 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011234 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011235 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11236 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011237 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011238 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11239 addr_field, maxphyaddr, count, addr);
11240 return -EINVAL;
11241 }
11242 return 0;
11243}
11244
11245static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11246 struct vmcs12 *vmcs12)
11247{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011248 if (vmcs12->vm_exit_msr_load_count == 0 &&
11249 vmcs12->vm_exit_msr_store_count == 0 &&
11250 vmcs12->vm_entry_msr_load_count == 0)
11251 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011252 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011253 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011254 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011255 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011256 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011257 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011258 return -EINVAL;
11259 return 0;
11260}
11261
Bandan Dasc5f983f2017-05-05 15:25:14 -040011262static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11263 struct vmcs12 *vmcs12)
11264{
11265 u64 address = vmcs12->pml_address;
11266 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11267
11268 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11269 if (!nested_cpu_has_ept(vmcs12) ||
11270 !IS_ALIGNED(address, 4096) ||
11271 address >> maxphyaddr)
11272 return -EINVAL;
11273 }
11274
11275 return 0;
11276}
11277
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011278static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11279 struct vmx_msr_entry *e)
11280{
11281 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011282 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011283 return -EINVAL;
11284 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11285 e->index == MSR_IA32_UCODE_REV)
11286 return -EINVAL;
11287 if (e->reserved != 0)
11288 return -EINVAL;
11289 return 0;
11290}
11291
11292static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11293 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011294{
11295 if (e->index == MSR_FS_BASE ||
11296 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011297 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11298 nested_vmx_msr_check_common(vcpu, e))
11299 return -EINVAL;
11300 return 0;
11301}
11302
11303static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11304 struct vmx_msr_entry *e)
11305{
11306 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11307 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011308 return -EINVAL;
11309 return 0;
11310}
11311
11312/*
11313 * Load guest's/host's msr at nested entry/exit.
11314 * return 0 for success, entry index for failure.
11315 */
11316static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11317{
11318 u32 i;
11319 struct vmx_msr_entry e;
11320 struct msr_data msr;
11321
11322 msr.host_initiated = false;
11323 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011324 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11325 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011326 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011327 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11328 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011329 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011330 }
11331 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011332 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011333 "%s check failed (%u, 0x%x, 0x%x)\n",
11334 __func__, i, e.index, e.reserved);
11335 goto fail;
11336 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011337 msr.index = e.index;
11338 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011339 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011340 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011341 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11342 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011343 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011344 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011345 }
11346 return 0;
11347fail:
11348 return i + 1;
11349}
11350
11351static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11352{
11353 u32 i;
11354 struct vmx_msr_entry e;
11355
11356 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011357 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011358 if (kvm_vcpu_read_guest(vcpu,
11359 gpa + i * sizeof(e),
11360 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011361 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011362 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11363 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011364 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011365 }
11366 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011367 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011368 "%s check failed (%u, 0x%x, 0x%x)\n",
11369 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011370 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011371 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011372 msr_info.host_initiated = false;
11373 msr_info.index = e.index;
11374 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011375 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011376 "%s cannot read MSR (%u, 0x%x)\n",
11377 __func__, i, e.index);
11378 return -EINVAL;
11379 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011380 if (kvm_vcpu_write_guest(vcpu,
11381 gpa + i * sizeof(e) +
11382 offsetof(struct vmx_msr_entry, value),
11383 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011384 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011385 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011386 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011387 return -EINVAL;
11388 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011389 }
11390 return 0;
11391}
11392
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011393static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11394{
11395 unsigned long invalid_mask;
11396
11397 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11398 return (val & invalid_mask) == 0;
11399}
11400
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011401/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011402 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11403 * emulating VM entry into a guest with EPT enabled.
11404 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11405 * is assigned to entry_failure_code on failure.
11406 */
11407static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011408 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011409{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011410 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011411 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011412 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11413 return 1;
11414 }
11415
11416 /*
11417 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11418 * must not be dereferenced.
11419 */
11420 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11421 !nested_ept) {
11422 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11423 *entry_failure_code = ENTRY_FAIL_PDPTE;
11424 return 1;
11425 }
11426 }
11427
11428 vcpu->arch.cr3 = cr3;
11429 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11430 }
11431
11432 kvm_mmu_reset_context(vcpu);
11433 return 0;
11434}
11435
Jim Mattson6514dc32018-04-26 16:09:12 -070011436static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011437{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011438 struct vcpu_vmx *vmx = to_vmx(vcpu);
11439
11440 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11441 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11442 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11443 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11444 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11445 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11446 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11447 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11448 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11449 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11450 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11451 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11452 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11453 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11454 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11455 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11456 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11457 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11458 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11459 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11460 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11461 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11462 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11463 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11464 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11465 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11466 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11467 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11468 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11469 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11470 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011471
11472 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11473 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11474 vmcs12->guest_pending_dbg_exceptions);
11475 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11476 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11477
11478 if (nested_cpu_has_xsaves(vmcs12))
11479 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11480 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11481
11482 if (cpu_has_vmx_posted_intr())
11483 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11484
11485 /*
11486 * Whether page-faults are trapped is determined by a combination of
11487 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11488 * If enable_ept, L0 doesn't care about page faults and we should
11489 * set all of these to L1's desires. However, if !enable_ept, L0 does
11490 * care about (at least some) page faults, and because it is not easy
11491 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11492 * to exit on each and every L2 page fault. This is done by setting
11493 * MASK=MATCH=0 and (see below) EB.PF=1.
11494 * Note that below we don't need special code to set EB.PF beyond the
11495 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11496 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11497 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11498 */
11499 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11500 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11501 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11502 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11503
11504 /* All VMFUNCs are currently emulated through L0 vmexits. */
11505 if (cpu_has_vmx_vmfunc())
11506 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11507
11508 if (cpu_has_vmx_apicv()) {
11509 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11510 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11511 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11512 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11513 }
11514
11515 /*
11516 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11517 * Some constant fields are set here by vmx_set_constant_host_state().
11518 * Other fields are different per CPU, and will be set later when
11519 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11520 */
11521 vmx_set_constant_host_state(vmx);
11522
11523 /*
11524 * Set the MSR load/store lists to match L0's settings.
11525 */
11526 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011527 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11528 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11529 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11530 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011531
11532 set_cr4_guest_host_mask(vmx);
11533
11534 if (vmx_mpx_supported())
11535 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11536
11537 if (enable_vpid) {
11538 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11539 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11540 else
11541 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11542 }
11543
11544 /*
11545 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11546 */
11547 if (enable_ept) {
11548 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11549 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11550 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11551 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11552 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011553
11554 if (cpu_has_vmx_msr_bitmap())
11555 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011556}
11557
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011558/*
11559 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11560 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011561 * 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 +030011562 * guest in a way that will both be appropriate to L1's requests, and our
11563 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11564 * function also has additional necessary side-effects, like setting various
11565 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011566 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11567 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011568 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011569static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011570 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011571{
11572 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011573 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011574
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011575 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011576 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011577 vmx->nested.dirty_vmcs12 = false;
11578 }
11579
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011580 /*
11581 * First, the fields that are shadowed. This must be kept in sync
11582 * with vmx_shadow_fields.h.
11583 */
11584
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011585 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011586 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011587 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011588 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11589 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011590
11591 /*
11592 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11593 * HOST_FS_BASE, HOST_GS_BASE.
11594 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011595
Jim Mattson6514dc32018-04-26 16:09:12 -070011596 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011597 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011598 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11599 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11600 } else {
11601 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11602 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11603 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011604 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011605 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11606 vmcs12->vm_entry_intr_info_field);
11607 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11608 vmcs12->vm_entry_exception_error_code);
11609 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11610 vmcs12->vm_entry_instruction_len);
11611 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11612 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011613 vmx->loaded_vmcs->nmi_known_unmasked =
11614 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011615 } else {
11616 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11617 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011618 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011619
Jan Kiszkaf4124502014-03-07 20:03:13 +010011620 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011621
Paolo Bonzini93140062016-07-06 13:23:51 +020011622 /* Preemption timer setting is only taken from vmcs01. */
11623 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11624 exec_control |= vmcs_config.pin_based_exec_ctrl;
11625 if (vmx->hv_deadline_tsc == -1)
11626 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11627
11628 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011629 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011630 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11631 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011632 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011633 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011634 }
Wincy Van705699a2015-02-03 23:58:17 +080011635
Jan Kiszkaf4124502014-03-07 20:03:13 +010011636 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011637
Jan Kiszkaf4124502014-03-07 20:03:13 +010011638 vmx->nested.preemption_timer_expired = false;
11639 if (nested_cpu_has_preemption_timer(vmcs12))
11640 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011641
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011642 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011643 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011644
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011645 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011646 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011647 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011648 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011649 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011650 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011651 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11652 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011653 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011654 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11655 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11656 ~SECONDARY_EXEC_ENABLE_PML;
11657 exec_control |= vmcs12_exec_ctrl;
11658 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011659
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011660 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011661 vmcs_write16(GUEST_INTR_STATUS,
11662 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011663
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011664 /*
11665 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11666 * nested_get_vmcs12_pages will either fix it up or
11667 * remove the VM execution control.
11668 */
11669 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11670 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11671
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011672 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11673 }
11674
Jim Mattson83bafef2016-10-04 10:48:38 -070011675 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011676 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11677 * entry, but only if the current (host) sp changed from the value
11678 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11679 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11680 * here we just force the write to happen on entry.
11681 */
11682 vmx->host_rsp = 0;
11683
11684 exec_control = vmx_exec_control(vmx); /* L0's desires */
11685 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11686 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11687 exec_control &= ~CPU_BASED_TPR_SHADOW;
11688 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011689
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011690 /*
11691 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11692 * nested_get_vmcs12_pages can't fix it up, the illegal value
11693 * will result in a VM entry failure.
11694 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011695 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011696 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011697 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011698 } else {
11699#ifdef CONFIG_X86_64
11700 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11701 CPU_BASED_CR8_STORE_EXITING;
11702#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011703 }
11704
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011705 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011706 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11707 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011708 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011709 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11710 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11711
11712 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11713
11714 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11715 * bitwise-or of what L1 wants to trap for L2, and what we want to
11716 * trap. Note that CR0.TS also needs updating - we do this later.
11717 */
11718 update_exception_bitmap(vcpu);
11719 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11720 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11721
Nadav Har'El8049d652013-08-05 11:07:06 +030011722 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11723 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11724 * bits are further modified by vmx_set_efer() below.
11725 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011726 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011727
11728 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11729 * emulated by vmx_set_efer(), below.
11730 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011731 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011732 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11733 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011734 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11735
Jim Mattson6514dc32018-04-26 16:09:12 -070011736 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011737 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011738 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011739 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011740 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011741 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011742 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011743
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011744 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11745
Peter Feinerc95ba922016-08-17 09:36:47 -070011746 if (kvm_has_tsc_control)
11747 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011748
11749 if (enable_vpid) {
11750 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011751 * There is no direct mapping between vpid02 and vpid12, the
11752 * vpid02 is per-vCPU for L0 and reused while the value of
11753 * vpid12 is changed w/ one invvpid during nested vmentry.
11754 * The vpid12 is allocated by L1 for L2, so it will not
11755 * influence global bitmap(for vpid01 and vpid02 allocation)
11756 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011757 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011758 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011759 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11760 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011761 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011762 }
11763 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011764 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011765 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011766 }
11767
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011768 if (enable_pml) {
11769 /*
11770 * Conceptually we want to copy the PML address and index from
11771 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11772 * since we always flush the log on each vmexit, this happens
11773 * to be equivalent to simply resetting the fields in vmcs02.
11774 */
11775 ASSERT(vmx->pml_pg);
11776 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11777 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11778 }
11779
Nadav Har'El155a97a2013-08-05 11:07:16 +030011780 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011781 if (nested_ept_init_mmu_context(vcpu)) {
11782 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11783 return 1;
11784 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011785 } else if (nested_cpu_has2(vmcs12,
11786 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011787 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011788 }
11789
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011790 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011791 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11792 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011793 * The CR0_READ_SHADOW is what L2 should have expected to read given
11794 * the specifications by L1; It's not enough to take
11795 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11796 * have more bits than L1 expected.
11797 */
11798 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11799 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11800
11801 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11802 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11803
Jim Mattson6514dc32018-04-26 16:09:12 -070011804 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011805 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011806 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11807 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11808 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11809 else
11810 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11811 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11812 vmx_set_efer(vcpu, vcpu->arch.efer);
11813
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011814 /*
11815 * Guest state is invalid and unrestricted guest is disabled,
11816 * which means L1 attempted VMEntry to L2 with invalid state.
11817 * Fail the VMEntry.
11818 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011819 if (vmx->emulation_required) {
11820 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011821 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011822 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011823
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011824 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011825 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011826 entry_failure_code))
11827 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011828
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011829 if (!enable_ept)
11830 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11831
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011832 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11833 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011834 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011835}
11836
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011837static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11838{
11839 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11840 nested_cpu_has_virtual_nmis(vmcs12))
11841 return -EINVAL;
11842
11843 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11844 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11845 return -EINVAL;
11846
11847 return 0;
11848}
11849
Jim Mattsonca0bde22016-11-30 12:03:46 -080011850static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11851{
11852 struct vcpu_vmx *vmx = to_vmx(vcpu);
11853
11854 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11855 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11856 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11857
Jim Mattson56a20512017-07-06 16:33:06 -070011858 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11859 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11860
Jim Mattsonca0bde22016-11-30 12:03:46 -080011861 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11862 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11863
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011864 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11865 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11866
Jim Mattson712b12d2017-08-24 13:24:47 -070011867 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11868 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11869
Jim Mattsonca0bde22016-11-30 12:03:46 -080011870 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11871 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11872
11873 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11874 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11875
Bandan Dasc5f983f2017-05-05 15:25:14 -040011876 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11877 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11878
Jim Mattsonca0bde22016-11-30 12:03:46 -080011879 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011880 vmx->nested.msrs.procbased_ctls_low,
11881 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011882 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11883 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011884 vmx->nested.msrs.secondary_ctls_low,
11885 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011886 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011887 vmx->nested.msrs.pinbased_ctls_low,
11888 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011889 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011890 vmx->nested.msrs.exit_ctls_low,
11891 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011892 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011893 vmx->nested.msrs.entry_ctls_low,
11894 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011895 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11896
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011897 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011898 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11899
Bandan Das41ab9372017-08-03 15:54:43 -040011900 if (nested_cpu_has_vmfunc(vmcs12)) {
11901 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011902 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011903 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11904
11905 if (nested_cpu_has_eptp_switching(vmcs12)) {
11906 if (!nested_cpu_has_ept(vmcs12) ||
11907 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11908 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11909 }
11910 }
Bandan Das27c42a12017-08-03 15:54:42 -040011911
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011912 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11913 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11914
Jim Mattsonca0bde22016-11-30 12:03:46 -080011915 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11916 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11917 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11918 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11919
Marc Orr04473782018-06-20 17:21:29 -070011920 /*
11921 * From the Intel SDM, volume 3:
11922 * Fields relevant to VM-entry event injection must be set properly.
11923 * These fields are the VM-entry interruption-information field, the
11924 * VM-entry exception error code, and the VM-entry instruction length.
11925 */
11926 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11927 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11928 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11929 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11930 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11931 bool should_have_error_code;
11932 bool urg = nested_cpu_has2(vmcs12,
11933 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11934 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11935
11936 /* VM-entry interruption-info field: interruption type */
11937 if (intr_type == INTR_TYPE_RESERVED ||
11938 (intr_type == INTR_TYPE_OTHER_EVENT &&
11939 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11940 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11941
11942 /* VM-entry interruption-info field: vector */
11943 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11944 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11945 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11946 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11947
11948 /* VM-entry interruption-info field: deliver error code */
11949 should_have_error_code =
11950 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11951 x86_exception_has_error_code(vector);
11952 if (has_error_code != should_have_error_code)
11953 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11954
11955 /* VM-entry exception error code */
11956 if (has_error_code &&
11957 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11958 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11959
11960 /* VM-entry interruption-info field: reserved bits */
11961 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11962 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11963
11964 /* VM-entry instruction length */
11965 switch (intr_type) {
11966 case INTR_TYPE_SOFT_EXCEPTION:
11967 case INTR_TYPE_SOFT_INTR:
11968 case INTR_TYPE_PRIV_SW_EXCEPTION:
11969 if ((vmcs12->vm_entry_instruction_len > 15) ||
11970 (vmcs12->vm_entry_instruction_len == 0 &&
11971 !nested_cpu_has_zero_length_injection(vcpu)))
11972 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11973 }
11974 }
11975
Jim Mattsonca0bde22016-11-30 12:03:46 -080011976 return 0;
11977}
11978
11979static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11980 u32 *exit_qual)
11981{
11982 bool ia32e;
11983
11984 *exit_qual = ENTRY_FAIL_DEFAULT;
11985
11986 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11987 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11988 return 1;
11989
11990 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11991 vmcs12->vmcs_link_pointer != -1ull) {
11992 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11993 return 1;
11994 }
11995
11996 /*
11997 * If the load IA32_EFER VM-entry control is 1, the following checks
11998 * are performed on the field for the IA32_EFER MSR:
11999 * - Bits reserved in the IA32_EFER MSR must be 0.
12000 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12001 * the IA-32e mode guest VM-exit control. It must also be identical
12002 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12003 * CR0.PG) is 1.
12004 */
12005 if (to_vmx(vcpu)->nested.nested_run_pending &&
12006 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12007 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12008 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12009 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12010 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12011 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12012 return 1;
12013 }
12014
12015 /*
12016 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12017 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12018 * the values of the LMA and LME bits in the field must each be that of
12019 * the host address-space size VM-exit control.
12020 */
12021 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12022 ia32e = (vmcs12->vm_exit_controls &
12023 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12024 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12025 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12026 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12027 return 1;
12028 }
12029
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012030 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12031 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12032 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12033 return 1;
12034
Jim Mattsonca0bde22016-11-30 12:03:46 -080012035 return 0;
12036}
12037
Jim Mattson6514dc32018-04-26 16:09:12 -070012038static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080012039{
12040 struct vcpu_vmx *vmx = to_vmx(vcpu);
12041 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012042 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012043 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012044
Jim Mattson858e25c2016-11-30 12:03:47 -080012045 enter_guest_mode(vcpu);
12046
12047 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12048 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12049
Jim Mattsonde3a0022017-11-27 17:22:25 -060012050 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012051 vmx_segment_cache_clear(vmx);
12052
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012053 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12054 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12055
12056 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070012057 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012058 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012059
12060 nested_get_vmcs12_pages(vcpu, vmcs12);
12061
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012062 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson0b88abd2018-05-30 16:00:02 -070012063 exit_qual = nested_vmx_load_msr(vcpu,
12064 vmcs12->vm_entry_msr_load_addr,
12065 vmcs12->vm_entry_msr_load_count);
12066 if (exit_qual)
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012067 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012068
Jim Mattson858e25c2016-11-30 12:03:47 -080012069 /*
12070 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12071 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12072 * returned as far as L1 is concerned. It will only return (and set
12073 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12074 */
12075 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012076
12077fail:
12078 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12079 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12080 leave_guest_mode(vcpu);
12081 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12082 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
12083 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012084}
12085
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012086/*
12087 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12088 * for running an L2 nested guest.
12089 */
12090static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12091{
12092 struct vmcs12 *vmcs12;
12093 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012094 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012095 u32 exit_qual;
12096 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012097
Kyle Hueyeb277562016-11-29 12:40:39 -080012098 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012099 return 1;
12100
Kyle Hueyeb277562016-11-29 12:40:39 -080012101 if (!nested_vmx_check_vmcs12(vcpu))
12102 goto out;
12103
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012104 vmcs12 = get_vmcs12(vcpu);
12105
Abel Gordon012f83c2013-04-18 14:39:25 +030012106 if (enable_shadow_vmcs)
12107 copy_shadow_to_vmcs12(vmx);
12108
Nadav Har'El7c177932011-05-25 23:12:04 +030012109 /*
12110 * The nested entry process starts with enforcing various prerequisites
12111 * on vmcs12 as required by the Intel SDM, and act appropriately when
12112 * they fail: As the SDM explains, some conditions should cause the
12113 * instruction to fail, while others will cause the instruction to seem
12114 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12115 * To speed up the normal (success) code path, we should avoid checking
12116 * for misconfigurations which will anyway be caught by the processor
12117 * when using the merged vmcs02.
12118 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012119 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12120 nested_vmx_failValid(vcpu,
12121 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12122 goto out;
12123 }
12124
Nadav Har'El7c177932011-05-25 23:12:04 +030012125 if (vmcs12->launch_state == launch) {
12126 nested_vmx_failValid(vcpu,
12127 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12128 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012129 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012130 }
12131
Jim Mattsonca0bde22016-11-30 12:03:46 -080012132 ret = check_vmentry_prereqs(vcpu, vmcs12);
12133 if (ret) {
12134 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012135 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012136 }
12137
Nadav Har'El7c177932011-05-25 23:12:04 +030012138 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012139 * After this point, the trap flag no longer triggers a singlestep trap
12140 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12141 * This is not 100% correct; for performance reasons, we delegate most
12142 * of the checks on host state to the processor. If those fail,
12143 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012144 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012145 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012146
Jim Mattsonca0bde22016-11-30 12:03:46 -080012147 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12148 if (ret) {
12149 nested_vmx_entry_failure(vcpu, vmcs12,
12150 EXIT_REASON_INVALID_STATE, exit_qual);
12151 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012152 }
12153
12154 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012155 * We're finally done with prerequisite checking, and can start with
12156 * the nested entry.
12157 */
12158
Jim Mattson6514dc32018-04-26 16:09:12 -070012159 vmx->nested.nested_run_pending = 1;
12160 ret = enter_vmx_non_root_mode(vcpu);
12161 if (ret) {
12162 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012163 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070012164 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012165
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012166 /* Hide L1D cache contents from the nested guest. */
12167 vmx->vcpu.arch.l1tf_flush_l1d = true;
12168
Chao Gao135a06c2018-02-11 10:06:30 +080012169 /*
12170 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12171 * by event injection, halt vcpu.
12172 */
12173 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012174 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12175 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012176 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012177 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012178 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012179
12180out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012181 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012182}
12183
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012184/*
12185 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12186 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12187 * This function returns the new value we should put in vmcs12.guest_cr0.
12188 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12189 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12190 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12191 * didn't trap the bit, because if L1 did, so would L0).
12192 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12193 * been modified by L2, and L1 knows it. So just leave the old value of
12194 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12195 * isn't relevant, because if L0 traps this bit it can set it to anything.
12196 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12197 * changed these bits, and therefore they need to be updated, but L0
12198 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12199 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12200 */
12201static inline unsigned long
12202vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12203{
12204 return
12205 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12206 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12207 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12208 vcpu->arch.cr0_guest_owned_bits));
12209}
12210
12211static inline unsigned long
12212vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12213{
12214 return
12215 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12216 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12217 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12218 vcpu->arch.cr4_guest_owned_bits));
12219}
12220
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012221static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12222 struct vmcs12 *vmcs12)
12223{
12224 u32 idt_vectoring;
12225 unsigned int nr;
12226
Wanpeng Li664f8e22017-08-24 03:35:09 -070012227 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012228 nr = vcpu->arch.exception.nr;
12229 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12230
12231 if (kvm_exception_is_soft(nr)) {
12232 vmcs12->vm_exit_instruction_len =
12233 vcpu->arch.event_exit_inst_len;
12234 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12235 } else
12236 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12237
12238 if (vcpu->arch.exception.has_error_code) {
12239 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12240 vmcs12->idt_vectoring_error_code =
12241 vcpu->arch.exception.error_code;
12242 }
12243
12244 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012245 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012246 vmcs12->idt_vectoring_info_field =
12247 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012248 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012249 nr = vcpu->arch.interrupt.nr;
12250 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12251
12252 if (vcpu->arch.interrupt.soft) {
12253 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12254 vmcs12->vm_entry_instruction_len =
12255 vcpu->arch.event_exit_inst_len;
12256 } else
12257 idt_vectoring |= INTR_TYPE_EXT_INTR;
12258
12259 vmcs12->idt_vectoring_info_field = idt_vectoring;
12260 }
12261}
12262
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012263static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12264{
12265 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012266 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012267 bool block_nested_events =
12268 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012269
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012270 if (vcpu->arch.exception.pending &&
12271 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012272 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012273 return -EBUSY;
12274 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012275 return 0;
12276 }
12277
Jan Kiszkaf4124502014-03-07 20:03:13 +010012278 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12279 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012280 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012281 return -EBUSY;
12282 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12283 return 0;
12284 }
12285
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012286 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012287 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012288 return -EBUSY;
12289 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12290 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12291 INTR_INFO_VALID_MASK, 0);
12292 /*
12293 * The NMI-triggered VM exit counts as injection:
12294 * clear this one and block further NMIs.
12295 */
12296 vcpu->arch.nmi_pending = 0;
12297 vmx_set_nmi_mask(vcpu, true);
12298 return 0;
12299 }
12300
12301 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12302 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012303 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012304 return -EBUSY;
12305 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012306 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012307 }
12308
David Hildenbrand6342c502017-01-25 11:58:58 +010012309 vmx_complete_nested_posted_interrupt(vcpu);
12310 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012311}
12312
Jan Kiszkaf4124502014-03-07 20:03:13 +010012313static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12314{
12315 ktime_t remaining =
12316 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12317 u64 value;
12318
12319 if (ktime_to_ns(remaining) <= 0)
12320 return 0;
12321
12322 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12323 do_div(value, 1000000);
12324 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12325}
12326
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012327/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012328 * Update the guest state fields of vmcs12 to reflect changes that
12329 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12330 * VM-entry controls is also updated, since this is really a guest
12331 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012332 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012333static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012334{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012335 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12336 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12337
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012338 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12339 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12340 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12341
12342 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12343 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12344 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12345 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12346 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12347 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12348 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12349 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12350 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12351 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12352 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12353 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12354 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12355 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12356 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12357 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12358 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12359 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12360 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12361 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12362 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12363 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12364 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12365 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12366 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12367 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12368 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12369 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12370 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12371 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12372 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12373 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12374 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12375 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12376 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12377 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12378
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012379 vmcs12->guest_interruptibility_info =
12380 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12381 vmcs12->guest_pending_dbg_exceptions =
12382 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012383 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12384 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12385 else
12386 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012387
Jan Kiszkaf4124502014-03-07 20:03:13 +010012388 if (nested_cpu_has_preemption_timer(vmcs12)) {
12389 if (vmcs12->vm_exit_controls &
12390 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12391 vmcs12->vmx_preemption_timer_value =
12392 vmx_get_preemption_timer_value(vcpu);
12393 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12394 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012395
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012396 /*
12397 * In some cases (usually, nested EPT), L2 is allowed to change its
12398 * own CR3 without exiting. If it has changed it, we must keep it.
12399 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12400 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12401 *
12402 * Additionally, restore L2's PDPTR to vmcs12.
12403 */
12404 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012405 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012406 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12407 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12408 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12409 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12410 }
12411
Jim Mattsond281e132017-06-01 12:44:46 -070012412 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012413
Wincy Van608406e2015-02-03 23:57:51 +080012414 if (nested_cpu_has_vid(vmcs12))
12415 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12416
Jan Kiszkac18911a2013-03-13 16:06:41 +010012417 vmcs12->vm_entry_controls =
12418 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012419 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012420
Jan Kiszka2996fca2014-06-16 13:59:43 +020012421 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12422 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12423 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12424 }
12425
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012426 /* TODO: These cannot have changed unless we have MSR bitmaps and
12427 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012428 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012429 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012430 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12431 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012432 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12433 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12434 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012435 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012436 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012437}
12438
12439/*
12440 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12441 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12442 * and this function updates it to reflect the changes to the guest state while
12443 * L2 was running (and perhaps made some exits which were handled directly by L0
12444 * without going back to L1), and to reflect the exit reason.
12445 * Note that we do not have to copy here all VMCS fields, just those that
12446 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12447 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12448 * which already writes to vmcs12 directly.
12449 */
12450static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12451 u32 exit_reason, u32 exit_intr_info,
12452 unsigned long exit_qualification)
12453{
12454 /* update guest state fields: */
12455 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012456
12457 /* update exit information fields: */
12458
Jan Kiszka533558b2014-01-04 18:47:20 +010012459 vmcs12->vm_exit_reason = exit_reason;
12460 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012461 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012462
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012463 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012464 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12465 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12466
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012467 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012468 vmcs12->launch_state = 1;
12469
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012470 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12471 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012472 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012473
12474 /*
12475 * Transfer the event that L0 or L1 may wanted to inject into
12476 * L2 to IDT_VECTORING_INFO_FIELD.
12477 */
12478 vmcs12_save_pending_event(vcpu, vmcs12);
12479 }
12480
12481 /*
12482 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12483 * preserved above and would only end up incorrectly in L1.
12484 */
12485 vcpu->arch.nmi_injected = false;
12486 kvm_clear_exception_queue(vcpu);
12487 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012488}
12489
Wanpeng Li5af41572017-11-05 16:54:49 -080012490static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12491 struct vmcs12 *vmcs12)
12492{
12493 u32 entry_failure_code;
12494
12495 nested_ept_uninit_mmu_context(vcpu);
12496
12497 /*
12498 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12499 * couldn't have changed.
12500 */
12501 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12502 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12503
12504 if (!enable_ept)
12505 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12506}
12507
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012508/*
12509 * A part of what we need to when the nested L2 guest exits and we want to
12510 * run its L1 parent, is to reset L1's guest state to the host state specified
12511 * in vmcs12.
12512 * This function is to be called not only on normal nested exit, but also on
12513 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12514 * Failures During or After Loading Guest State").
12515 * This function should be called when the active VMCS is L1's (vmcs01).
12516 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012517static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12518 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012519{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012520 struct kvm_segment seg;
12521
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012522 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12523 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012524 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012525 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12526 else
12527 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12528 vmx_set_efer(vcpu, vcpu->arch.efer);
12529
12530 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12531 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012532 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012533 /*
12534 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012535 * actually changed, because vmx_set_cr0 refers to efer set above.
12536 *
12537 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12538 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012539 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012540 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012541 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012542
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012543 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012544 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012545 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012546
Wanpeng Li5af41572017-11-05 16:54:49 -080012547 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012548
Liran Alon6f1e03b2018-05-22 17:16:14 +030012549 /*
12550 * If vmcs01 don't use VPID, CPU flushes TLB on every
12551 * VMEntry/VMExit. Thus, no need to flush TLB.
12552 *
12553 * If vmcs12 uses VPID, TLB entries populated by L2 are
12554 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12555 * with vmx->vpid. Thus, no need to flush TLB.
12556 *
12557 * Therefore, flush TLB only in case vmcs01 uses VPID and
12558 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12559 * are both tagged with vmx->vpid.
12560 */
12561 if (enable_vpid &&
12562 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012563 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012564 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012565
12566 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12567 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12568 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12569 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12570 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012571 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12572 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012573
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012574 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12575 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12576 vmcs_write64(GUEST_BNDCFGS, 0);
12577
Jan Kiszka44811c02013-08-04 17:17:27 +020012578 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012579 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012580 vcpu->arch.pat = vmcs12->host_ia32_pat;
12581 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012582 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12583 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12584 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012585
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012586 /* Set L1 segment info according to Intel SDM
12587 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12588 seg = (struct kvm_segment) {
12589 .base = 0,
12590 .limit = 0xFFFFFFFF,
12591 .selector = vmcs12->host_cs_selector,
12592 .type = 11,
12593 .present = 1,
12594 .s = 1,
12595 .g = 1
12596 };
12597 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12598 seg.l = 1;
12599 else
12600 seg.db = 1;
12601 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12602 seg = (struct kvm_segment) {
12603 .base = 0,
12604 .limit = 0xFFFFFFFF,
12605 .type = 3,
12606 .present = 1,
12607 .s = 1,
12608 .db = 1,
12609 .g = 1
12610 };
12611 seg.selector = vmcs12->host_ds_selector;
12612 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12613 seg.selector = vmcs12->host_es_selector;
12614 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12615 seg.selector = vmcs12->host_ss_selector;
12616 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12617 seg.selector = vmcs12->host_fs_selector;
12618 seg.base = vmcs12->host_fs_base;
12619 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12620 seg.selector = vmcs12->host_gs_selector;
12621 seg.base = vmcs12->host_gs_base;
12622 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12623 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012624 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012625 .limit = 0x67,
12626 .selector = vmcs12->host_tr_selector,
12627 .type = 11,
12628 .present = 1
12629 };
12630 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12631
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012632 kvm_set_dr(vcpu, 7, 0x400);
12633 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012634
Wincy Van3af18d92015-02-03 23:49:31 +080012635 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012636 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012637
Wincy Vanff651cb2014-12-11 08:52:58 +030012638 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12639 vmcs12->vm_exit_msr_load_count))
12640 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012641}
12642
12643/*
12644 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12645 * and modify vmcs12 to make it see what it would expect to see there if
12646 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12647 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012648static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12649 u32 exit_intr_info,
12650 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012651{
12652 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012653 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12654
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012655 /* trying to cancel vmlaunch/vmresume is a bug */
12656 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12657
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012658 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012659 * The only expected VM-instruction error is "VM entry with
12660 * invalid control field(s)." Anything else indicates a
12661 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012662 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012663 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12664 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12665
12666 leave_guest_mode(vcpu);
12667
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012668 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12669 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12670
Jim Mattson4f350c62017-09-14 16:31:44 -070012671 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012672 if (exit_reason == -1)
12673 sync_vmcs12(vcpu, vmcs12);
12674 else
12675 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12676 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012677
12678 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12679 vmcs12->vm_exit_msr_store_count))
12680 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012681 }
12682
Jim Mattson4f350c62017-09-14 16:31:44 -070012683 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012684 vm_entry_controls_reset_shadow(vmx);
12685 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012686 vmx_segment_cache_clear(vmx);
12687
Paolo Bonzini93140062016-07-06 13:23:51 +020012688 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012689 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12690 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012691 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012692 if (vmx->hv_deadline_tsc == -1)
12693 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12694 PIN_BASED_VMX_PREEMPTION_TIMER);
12695 else
12696 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12697 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012698 if (kvm_has_tsc_control)
12699 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012700
Jim Mattson8d860bb2018-05-09 16:56:05 -040012701 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12702 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12703 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012704 } else if (!nested_cpu_has_ept(vmcs12) &&
12705 nested_cpu_has2(vmcs12,
12706 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012707 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012708 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012709
12710 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12711 vmx->host_rsp = 0;
12712
12713 /* Unpin physical memory we referred to in vmcs02 */
12714 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012715 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012716 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012717 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012718 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012719 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012720 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012721 }
Wincy Van705699a2015-02-03 23:58:17 +080012722 if (vmx->nested.pi_desc_page) {
12723 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012724 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012725 vmx->nested.pi_desc_page = NULL;
12726 vmx->nested.pi_desc = NULL;
12727 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012728
12729 /*
Tang Chen38b99172014-09-24 15:57:54 +080012730 * We are now running in L2, mmu_notifier will force to reload the
12731 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12732 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012733 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012734
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012735 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012736 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012737
12738 /* in case we halted in L2 */
12739 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012740
12741 if (likely(!vmx->fail)) {
12742 /*
12743 * TODO: SDM says that with acknowledge interrupt on
12744 * exit, bit 31 of the VM-exit interrupt information
12745 * (valid interrupt) is always set to 1 on
12746 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12747 * need kvm_cpu_has_interrupt(). See the commit
12748 * message for details.
12749 */
12750 if (nested_exit_intr_ack_set(vcpu) &&
12751 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12752 kvm_cpu_has_interrupt(vcpu)) {
12753 int irq = kvm_cpu_get_interrupt(vcpu);
12754 WARN_ON(irq < 0);
12755 vmcs12->vm_exit_intr_info = irq |
12756 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12757 }
12758
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012759 if (exit_reason != -1)
12760 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12761 vmcs12->exit_qualification,
12762 vmcs12->idt_vectoring_info_field,
12763 vmcs12->vm_exit_intr_info,
12764 vmcs12->vm_exit_intr_error_code,
12765 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012766
12767 load_vmcs12_host_state(vcpu, vmcs12);
12768
12769 return;
12770 }
12771
12772 /*
12773 * After an early L2 VM-entry failure, we're now back
12774 * in L1 which thinks it just finished a VMLAUNCH or
12775 * VMRESUME instruction, so we need to set the failure
12776 * flag and the VM-instruction error field of the VMCS
12777 * accordingly.
12778 */
12779 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012780
12781 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12782
Jim Mattson4f350c62017-09-14 16:31:44 -070012783 /*
12784 * The emulated instruction was already skipped in
12785 * nested_vmx_run, but the updated RIP was never
12786 * written back to the vmcs01.
12787 */
12788 skip_emulated_instruction(vcpu);
12789 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012790}
12791
Nadav Har'El7c177932011-05-25 23:12:04 +030012792/*
Jan Kiszka42124922014-01-04 18:47:19 +010012793 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12794 */
12795static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12796{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012797 if (is_guest_mode(vcpu)) {
12798 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012799 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012800 }
Jan Kiszka42124922014-01-04 18:47:19 +010012801 free_nested(to_vmx(vcpu));
12802}
12803
12804/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012805 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12806 * 23.7 "VM-entry failures during or after loading guest state" (this also
12807 * lists the acceptable exit-reason and exit-qualification parameters).
12808 * It should only be called before L2 actually succeeded to run, and when
12809 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12810 */
12811static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12812 struct vmcs12 *vmcs12,
12813 u32 reason, unsigned long qualification)
12814{
12815 load_vmcs12_host_state(vcpu, vmcs12);
12816 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12817 vmcs12->exit_qualification = qualification;
12818 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012819 if (enable_shadow_vmcs)
12820 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012821}
12822
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012823static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12824 struct x86_instruction_info *info,
12825 enum x86_intercept_stage stage)
12826{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012827 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12828 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12829
12830 /*
12831 * RDPID causes #UD if disabled through secondary execution controls.
12832 * Because it is marked as EmulateOnUD, we need to intercept it here.
12833 */
12834 if (info->intercept == x86_intercept_rdtscp &&
12835 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12836 ctxt->exception.vector = UD_VECTOR;
12837 ctxt->exception.error_code_valid = false;
12838 return X86EMUL_PROPAGATE_FAULT;
12839 }
12840
12841 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012842 return X86EMUL_CONTINUE;
12843}
12844
Yunhong Jiang64672c92016-06-13 14:19:59 -070012845#ifdef CONFIG_X86_64
12846/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12847static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12848 u64 divisor, u64 *result)
12849{
12850 u64 low = a << shift, high = a >> (64 - shift);
12851
12852 /* To avoid the overflow on divq */
12853 if (high >= divisor)
12854 return 1;
12855
12856 /* Low hold the result, high hold rem which is discarded */
12857 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12858 "rm" (divisor), "0" (low), "1" (high));
12859 *result = low;
12860
12861 return 0;
12862}
12863
12864static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12865{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012866 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012867 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012868
12869 if (kvm_mwait_in_guest(vcpu->kvm))
12870 return -EOPNOTSUPP;
12871
12872 vmx = to_vmx(vcpu);
12873 tscl = rdtsc();
12874 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12875 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012876 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12877
12878 if (delta_tsc > lapic_timer_advance_cycles)
12879 delta_tsc -= lapic_timer_advance_cycles;
12880 else
12881 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012882
12883 /* Convert to host delta tsc if tsc scaling is enabled */
12884 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12885 u64_shl_div_u64(delta_tsc,
12886 kvm_tsc_scaling_ratio_frac_bits,
12887 vcpu->arch.tsc_scaling_ratio,
12888 &delta_tsc))
12889 return -ERANGE;
12890
12891 /*
12892 * If the delta tsc can't fit in the 32 bit after the multi shift,
12893 * we can't use the preemption timer.
12894 * It's possible that it fits on later vmentries, but checking
12895 * on every vmentry is costly so we just use an hrtimer.
12896 */
12897 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12898 return -ERANGE;
12899
12900 vmx->hv_deadline_tsc = tscl + delta_tsc;
12901 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12902 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012903
12904 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012905}
12906
12907static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12908{
12909 struct vcpu_vmx *vmx = to_vmx(vcpu);
12910 vmx->hv_deadline_tsc = -1;
12911 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12912 PIN_BASED_VMX_PREEMPTION_TIMER);
12913}
12914#endif
12915
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012916static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012917{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012918 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012919 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012920}
12921
Kai Huang843e4332015-01-28 10:54:28 +080012922static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12923 struct kvm_memory_slot *slot)
12924{
12925 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12926 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12927}
12928
12929static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12930 struct kvm_memory_slot *slot)
12931{
12932 kvm_mmu_slot_set_dirty(kvm, slot);
12933}
12934
12935static void vmx_flush_log_dirty(struct kvm *kvm)
12936{
12937 kvm_flush_pml_buffers(kvm);
12938}
12939
Bandan Dasc5f983f2017-05-05 15:25:14 -040012940static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12941{
12942 struct vmcs12 *vmcs12;
12943 struct vcpu_vmx *vmx = to_vmx(vcpu);
12944 gpa_t gpa;
12945 struct page *page = NULL;
12946 u64 *pml_address;
12947
12948 if (is_guest_mode(vcpu)) {
12949 WARN_ON_ONCE(vmx->nested.pml_full);
12950
12951 /*
12952 * Check if PML is enabled for the nested guest.
12953 * Whether eptp bit 6 is set is already checked
12954 * as part of A/D emulation.
12955 */
12956 vmcs12 = get_vmcs12(vcpu);
12957 if (!nested_cpu_has_pml(vmcs12))
12958 return 0;
12959
Dan Carpenter47698862017-05-10 22:43:17 +030012960 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012961 vmx->nested.pml_full = true;
12962 return 1;
12963 }
12964
12965 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12966
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012967 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12968 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012969 return 0;
12970
12971 pml_address = kmap(page);
12972 pml_address[vmcs12->guest_pml_index--] = gpa;
12973 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012974 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012975 }
12976
12977 return 0;
12978}
12979
Kai Huang843e4332015-01-28 10:54:28 +080012980static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12981 struct kvm_memory_slot *memslot,
12982 gfn_t offset, unsigned long mask)
12983{
12984 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12985}
12986
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012987static void __pi_post_block(struct kvm_vcpu *vcpu)
12988{
12989 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12990 struct pi_desc old, new;
12991 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012992
12993 do {
12994 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012995 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12996 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012997
12998 dest = cpu_physical_id(vcpu->cpu);
12999
13000 if (x2apic_enabled())
13001 new.ndst = dest;
13002 else
13003 new.ndst = (dest << 8) & 0xFF00;
13004
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013005 /* set 'NV' to 'notification vector' */
13006 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013007 } while (cmpxchg64(&pi_desc->control, old.control,
13008 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013009
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013010 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13011 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013012 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013013 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013014 vcpu->pre_pcpu = -1;
13015 }
13016}
13017
Feng Wuefc64402015-09-18 22:29:51 +080013018/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013019 * This routine does the following things for vCPU which is going
13020 * to be blocked if VT-d PI is enabled.
13021 * - Store the vCPU to the wakeup list, so when interrupts happen
13022 * we can find the right vCPU to wake up.
13023 * - Change the Posted-interrupt descriptor as below:
13024 * 'NDST' <-- vcpu->pre_pcpu
13025 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13026 * - If 'ON' is set during this process, which means at least one
13027 * interrupt is posted for this vCPU, we cannot block it, in
13028 * this case, return 1, otherwise, return 0.
13029 *
13030 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013031static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013032{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013033 unsigned int dest;
13034 struct pi_desc old, new;
13035 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13036
13037 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013038 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13039 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013040 return 0;
13041
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013042 WARN_ON(irqs_disabled());
13043 local_irq_disable();
13044 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13045 vcpu->pre_pcpu = vcpu->cpu;
13046 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13047 list_add_tail(&vcpu->blocked_vcpu_list,
13048 &per_cpu(blocked_vcpu_on_cpu,
13049 vcpu->pre_pcpu));
13050 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13051 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013052
13053 do {
13054 old.control = new.control = pi_desc->control;
13055
Feng Wubf9f6ac2015-09-18 22:29:55 +080013056 WARN((pi_desc->sn == 1),
13057 "Warning: SN field of posted-interrupts "
13058 "is set before blocking\n");
13059
13060 /*
13061 * Since vCPU can be preempted during this process,
13062 * vcpu->cpu could be different with pre_pcpu, we
13063 * need to set pre_pcpu as the destination of wakeup
13064 * notification event, then we can find the right vCPU
13065 * to wakeup in wakeup handler if interrupts happen
13066 * when the vCPU is in blocked state.
13067 */
13068 dest = cpu_physical_id(vcpu->pre_pcpu);
13069
13070 if (x2apic_enabled())
13071 new.ndst = dest;
13072 else
13073 new.ndst = (dest << 8) & 0xFF00;
13074
13075 /* set 'NV' to 'wakeup vector' */
13076 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013077 } while (cmpxchg64(&pi_desc->control, old.control,
13078 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013079
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013080 /* We should not block the vCPU if an interrupt is posted for it. */
13081 if (pi_test_on(pi_desc) == 1)
13082 __pi_post_block(vcpu);
13083
13084 local_irq_enable();
13085 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013086}
13087
Yunhong Jiangbc225122016-06-13 14:19:58 -070013088static int vmx_pre_block(struct kvm_vcpu *vcpu)
13089{
13090 if (pi_pre_block(vcpu))
13091 return 1;
13092
Yunhong Jiang64672c92016-06-13 14:19:59 -070013093 if (kvm_lapic_hv_timer_in_use(vcpu))
13094 kvm_lapic_switch_to_sw_timer(vcpu);
13095
Yunhong Jiangbc225122016-06-13 14:19:58 -070013096 return 0;
13097}
13098
13099static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013100{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013101 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013102 return;
13103
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013104 WARN_ON(irqs_disabled());
13105 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013106 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013107 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013108}
13109
Yunhong Jiangbc225122016-06-13 14:19:58 -070013110static void vmx_post_block(struct kvm_vcpu *vcpu)
13111{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013112 if (kvm_x86_ops->set_hv_timer)
13113 kvm_lapic_switch_to_hv_timer(vcpu);
13114
Yunhong Jiangbc225122016-06-13 14:19:58 -070013115 pi_post_block(vcpu);
13116}
13117
Feng Wubf9f6ac2015-09-18 22:29:55 +080013118/*
Feng Wuefc64402015-09-18 22:29:51 +080013119 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13120 *
13121 * @kvm: kvm
13122 * @host_irq: host irq of the interrupt
13123 * @guest_irq: gsi of the interrupt
13124 * @set: set or unset PI
13125 * returns 0 on success, < 0 on failure
13126 */
13127static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13128 uint32_t guest_irq, bool set)
13129{
13130 struct kvm_kernel_irq_routing_entry *e;
13131 struct kvm_irq_routing_table *irq_rt;
13132 struct kvm_lapic_irq irq;
13133 struct kvm_vcpu *vcpu;
13134 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013135 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013136
13137 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013138 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13139 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013140 return 0;
13141
13142 idx = srcu_read_lock(&kvm->irq_srcu);
13143 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013144 if (guest_irq >= irq_rt->nr_rt_entries ||
13145 hlist_empty(&irq_rt->map[guest_irq])) {
13146 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13147 guest_irq, irq_rt->nr_rt_entries);
13148 goto out;
13149 }
Feng Wuefc64402015-09-18 22:29:51 +080013150
13151 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13152 if (e->type != KVM_IRQ_ROUTING_MSI)
13153 continue;
13154 /*
13155 * VT-d PI cannot support posting multicast/broadcast
13156 * interrupts to a vCPU, we still use interrupt remapping
13157 * for these kind of interrupts.
13158 *
13159 * For lowest-priority interrupts, we only support
13160 * those with single CPU as the destination, e.g. user
13161 * configures the interrupts via /proc/irq or uses
13162 * irqbalance to make the interrupts single-CPU.
13163 *
13164 * We will support full lowest-priority interrupt later.
13165 */
13166
Radim Krčmář371313132016-07-12 22:09:27 +020013167 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013168 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13169 /*
13170 * Make sure the IRTE is in remapped mode if
13171 * we don't handle it in posted mode.
13172 */
13173 ret = irq_set_vcpu_affinity(host_irq, NULL);
13174 if (ret < 0) {
13175 printk(KERN_INFO
13176 "failed to back to remapped mode, irq: %u\n",
13177 host_irq);
13178 goto out;
13179 }
13180
Feng Wuefc64402015-09-18 22:29:51 +080013181 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013182 }
Feng Wuefc64402015-09-18 22:29:51 +080013183
13184 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13185 vcpu_info.vector = irq.vector;
13186
hu huajun2698d822018-04-11 15:16:40 +080013187 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013188 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13189
13190 if (set)
13191 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013192 else
Feng Wuefc64402015-09-18 22:29:51 +080013193 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013194
13195 if (ret < 0) {
13196 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13197 __func__);
13198 goto out;
13199 }
13200 }
13201
13202 ret = 0;
13203out:
13204 srcu_read_unlock(&kvm->irq_srcu, idx);
13205 return ret;
13206}
13207
Ashok Rajc45dcc72016-06-22 14:59:56 +080013208static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13209{
13210 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13211 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13212 FEATURE_CONTROL_LMCE;
13213 else
13214 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13215 ~FEATURE_CONTROL_LMCE;
13216}
13217
Ladi Prosek72d7b372017-10-11 16:54:41 +020013218static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13219{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013220 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13221 if (to_vmx(vcpu)->nested.nested_run_pending)
13222 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013223 return 1;
13224}
13225
Ladi Prosek0234bf82017-10-11 16:54:40 +020013226static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13227{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013228 struct vcpu_vmx *vmx = to_vmx(vcpu);
13229
13230 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13231 if (vmx->nested.smm.guest_mode)
13232 nested_vmx_vmexit(vcpu, -1, 0, 0);
13233
13234 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13235 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013236 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013237 return 0;
13238}
13239
13240static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13241{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013242 struct vcpu_vmx *vmx = to_vmx(vcpu);
13243 int ret;
13244
13245 if (vmx->nested.smm.vmxon) {
13246 vmx->nested.vmxon = true;
13247 vmx->nested.smm.vmxon = false;
13248 }
13249
13250 if (vmx->nested.smm.guest_mode) {
13251 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013252 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013253 vcpu->arch.hflags |= HF_SMM_MASK;
13254 if (ret)
13255 return ret;
13256
13257 vmx->nested.smm.guest_mode = false;
13258 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013259 return 0;
13260}
13261
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013262static int enable_smi_window(struct kvm_vcpu *vcpu)
13263{
13264 return 0;
13265}
13266
Kees Cook404f6aa2016-08-08 16:29:06 -070013267static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013268 .cpu_has_kvm_support = cpu_has_kvm_support,
13269 .disabled_by_bios = vmx_disabled_by_bios,
13270 .hardware_setup = hardware_setup,
13271 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013272 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013273 .hardware_enable = hardware_enable,
13274 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013275 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013276 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013277
Wanpeng Lib31c1142018-03-12 04:53:04 -070013278 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013279 .vm_alloc = vmx_vm_alloc,
13280 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013281
Avi Kivity6aa8b732006-12-10 02:21:36 -080013282 .vcpu_create = vmx_create_vcpu,
13283 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013284 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013285
Avi Kivity04d2cc72007-09-10 18:10:54 +030013286 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013287 .vcpu_load = vmx_vcpu_load,
13288 .vcpu_put = vmx_vcpu_put,
13289
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013290 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013291 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013292 .get_msr = vmx_get_msr,
13293 .set_msr = vmx_set_msr,
13294 .get_segment_base = vmx_get_segment_base,
13295 .get_segment = vmx_get_segment,
13296 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013297 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013298 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013299 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013300 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013301 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013302 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013303 .set_cr3 = vmx_set_cr3,
13304 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013305 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013306 .get_idt = vmx_get_idt,
13307 .set_idt = vmx_set_idt,
13308 .get_gdt = vmx_get_gdt,
13309 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013310 .get_dr6 = vmx_get_dr6,
13311 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013312 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013313 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013314 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013315 .get_rflags = vmx_get_rflags,
13316 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013317
Avi Kivity6aa8b732006-12-10 02:21:36 -080013318 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013319
Avi Kivity6aa8b732006-12-10 02:21:36 -080013320 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013321 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013322 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013323 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13324 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013325 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013326 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013327 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013328 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013329 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013330 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013331 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013332 .get_nmi_mask = vmx_get_nmi_mask,
13333 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013334 .enable_nmi_window = enable_nmi_window,
13335 .enable_irq_window = enable_irq_window,
13336 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013337 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013338 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013339 .get_enable_apicv = vmx_get_enable_apicv,
13340 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013341 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013342 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013343 .hwapic_irr_update = vmx_hwapic_irr_update,
13344 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013345 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13346 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013347
Izik Eiduscbc94022007-10-25 00:29:55 +020013348 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013349 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013350 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013351 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013352
Avi Kivity586f9602010-11-18 13:09:54 +020013353 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013354
Sheng Yang17cc3932010-01-05 19:02:27 +080013355 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013356
13357 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013358
13359 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013360 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013361
13362 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013363
13364 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013365
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013366 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013367 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013368
13369 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013370
13371 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013372 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013373 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013374 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013375 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013376
13377 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013378
13379 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013380
13381 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13382 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13383 .flush_log_dirty = vmx_flush_log_dirty,
13384 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013385 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013386
Feng Wubf9f6ac2015-09-18 22:29:55 +080013387 .pre_block = vmx_pre_block,
13388 .post_block = vmx_post_block,
13389
Wei Huang25462f72015-06-19 15:45:05 +020013390 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013391
13392 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013393
13394#ifdef CONFIG_X86_64
13395 .set_hv_timer = vmx_set_hv_timer,
13396 .cancel_hv_timer = vmx_cancel_hv_timer,
13397#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013398
13399 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013400
Ladi Prosek72d7b372017-10-11 16:54:41 +020013401 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013402 .pre_enter_smm = vmx_pre_enter_smm,
13403 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013404 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013405};
13406
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013407static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013408{
13409 if (vmx_l1d_flush_pages) {
13410 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13411 vmx_l1d_flush_pages = NULL;
13412 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013413 /* Restore state so sysfs ignores VMX */
13414 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013415}
13416
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013417static void vmx_exit(void)
13418{
13419#ifdef CONFIG_KEXEC_CORE
13420 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13421 synchronize_rcu();
13422#endif
13423
13424 kvm_exit();
13425
13426#if IS_ENABLED(CONFIG_HYPERV)
13427 if (static_branch_unlikely(&enable_evmcs)) {
13428 int cpu;
13429 struct hv_vp_assist_page *vp_ap;
13430 /*
13431 * Reset everything to support using non-enlightened VMCS
13432 * access later (e.g. when we reload the module with
13433 * enlightened_vmcs=0)
13434 */
13435 for_each_online_cpu(cpu) {
13436 vp_ap = hv_get_vp_assist_page(cpu);
13437
13438 if (!vp_ap)
13439 continue;
13440
13441 vp_ap->current_nested_vmcs = 0;
13442 vp_ap->enlighten_vmentry = 0;
13443 }
13444
13445 static_branch_disable(&enable_evmcs);
13446 }
13447#endif
13448 vmx_cleanup_l1d_flush();
13449}
13450module_exit(vmx_exit);
13451
Avi Kivity6aa8b732006-12-10 02:21:36 -080013452static int __init vmx_init(void)
13453{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013454 int r;
13455
13456#if IS_ENABLED(CONFIG_HYPERV)
13457 /*
13458 * Enlightened VMCS usage should be recommended and the host needs
13459 * to support eVMCS v1 or above. We can also disable eVMCS support
13460 * with module parameter.
13461 */
13462 if (enlightened_vmcs &&
13463 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13464 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13465 KVM_EVMCS_VERSION) {
13466 int cpu;
13467
13468 /* Check that we have assist pages on all online CPUs */
13469 for_each_online_cpu(cpu) {
13470 if (!hv_get_vp_assist_page(cpu)) {
13471 enlightened_vmcs = false;
13472 break;
13473 }
13474 }
13475
13476 if (enlightened_vmcs) {
13477 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13478 static_branch_enable(&enable_evmcs);
13479 }
13480 } else {
13481 enlightened_vmcs = false;
13482 }
13483#endif
13484
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013485 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13486 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013487 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013488 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013489
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013490 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013491 * Must be called after kvm_init() so enable_ept is properly set
13492 * up. Hand the parameter mitigation value in which was stored in
13493 * the pre module init parser. If no parameter was given, it will
13494 * contain 'auto' which will be turned into the default 'cond'
13495 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013496 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013497 if (boot_cpu_has(X86_BUG_L1TF)) {
13498 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13499 if (r) {
13500 vmx_exit();
13501 return r;
13502 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013503 }
13504
Dave Young2965faa2015-09-09 15:38:55 -070013505#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013506 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13507 crash_vmclear_local_loaded_vmcss);
13508#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013509 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013510
He, Qingfdef3ad2007-04-30 09:45:24 +030013511 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013512}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013513module_init(vmx_init);