blob: c5c0118b126d08a1f032c8cb6cc33e69dd703aed [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);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_always);
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;
214
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200215 if (!enable_ept) {
216 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
217 return 0;
218 }
219
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200220 /* If set to auto use the default l1tf mitigation method */
221 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
222 switch (l1tf_mitigation) {
223 case L1TF_MITIGATION_OFF:
224 l1tf = VMENTER_L1D_FLUSH_NEVER;
225 break;
226 case L1TF_MITIGATION_FLUSH_NOWARN:
227 case L1TF_MITIGATION_FLUSH:
228 case L1TF_MITIGATION_FLUSH_NOSMT:
229 l1tf = VMENTER_L1D_FLUSH_COND;
230 break;
231 case L1TF_MITIGATION_FULL:
232 case L1TF_MITIGATION_FULL_FORCE:
233 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
234 break;
235 }
236 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
237 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
238 }
239
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200240 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
241 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
242 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
243 if (!page)
244 return -ENOMEM;
245 vmx_l1d_flush_pages = page_address(page);
246 }
247
248 l1tf_vmx_mitigation = l1tf;
249
Thomas Gleixner895ae472018-07-13 16:23:22 +0200250 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
251 static_branch_enable(&vmx_l1d_should_flush);
252 else
253 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200254
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200255 if (l1tf == VMENTER_L1D_FLUSH_ALWAYS)
256 static_branch_enable(&vmx_l1d_flush_always);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200257 else
258 static_branch_disable(&vmx_l1d_flush_always);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200259 return 0;
260}
261
262static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200263{
264 unsigned int i;
265
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200266 if (s) {
267 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Thomas Gleixner895ae472018-07-13 16:23:22 +0200268 if (sysfs_streq(s, vmentry_l1d_param[i].option))
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200269 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200270 }
271 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200272 return -EINVAL;
273}
274
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200275static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
276{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200277 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200278
279 if (!boot_cpu_has(X86_BUG_L1TF))
280 return 0;
281
282 l1tf = vmentry_l1d_flush_parse(s);
283 if (l1tf < 0)
284 return l1tf;
285
286 /*
287 * Has vmx_init() run already? If not then this is the pre init
288 * parameter parsing. In that case just store the value and let
289 * vmx_init() do the proper setup after enable_ept has been
290 * established.
291 */
292 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
293 vmentry_l1d_flush_param = l1tf;
294 return 0;
295 }
296
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200297 mutex_lock(&vmx_l1d_flush_mutex);
298 ret = vmx_setup_l1d_flush(l1tf);
299 mutex_unlock(&vmx_l1d_flush_mutex);
300 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200301}
302
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200303static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
304{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200305 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200306}
307
308static const struct kernel_param_ops vmentry_l1d_flush_ops = {
309 .set = vmentry_l1d_flush_set,
310 .get = vmentry_l1d_flush_get,
311};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200312module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200313
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700314struct kvm_vmx {
315 struct kvm kvm;
316
317 unsigned int tss_addr;
318 bool ept_identity_pagetable_done;
319 gpa_t ept_identity_map_addr;
320};
321
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200322#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300323
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400324struct vmcs {
325 u32 revision_id;
326 u32 abort;
327 char data[0];
328};
329
Nadav Har'Eld462b812011-05-24 15:26:10 +0300330/*
331 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
332 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
333 * loaded on this CPU (so we can clear them if the CPU goes down).
334 */
335struct loaded_vmcs {
336 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700337 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300338 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200339 bool launched;
340 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200341 unsigned long vmcs_host_cr3; /* May not match real cr3 */
342 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100343 /* Support for vnmi-less CPUs */
344 int soft_vnmi_blocked;
345 ktime_t entry_time;
346 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100347 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300348 struct list_head loaded_vmcss_on_cpu_link;
349};
350
Avi Kivity26bb0982009-09-07 11:14:12 +0300351struct shared_msr_entry {
352 unsigned index;
353 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200354 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300355};
356
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300357/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300358 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
359 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
360 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
361 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
362 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
363 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600364 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300365 * underlying hardware which will be used to run L2.
366 * This structure is packed to ensure that its layout is identical across
367 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700368 *
369 * IMPORTANT: Changing the layout of existing fields in this structure
370 * will break save/restore compatibility with older kvm releases. When
371 * adding new fields, either use space in the reserved padding* arrays
372 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300373 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300374typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300375struct __packed vmcs12 {
376 /* According to the Intel spec, a VMCS region must start with the
377 * following two fields. Then follow implementation-specific data.
378 */
379 u32 revision_id;
380 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300381
Nadav Har'El27d6c862011-05-25 23:06:59 +0300382 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
383 u32 padding[7]; /* room for future expansion */
384
Nadav Har'El22bd0352011-05-25 23:05:57 +0300385 u64 io_bitmap_a;
386 u64 io_bitmap_b;
387 u64 msr_bitmap;
388 u64 vm_exit_msr_store_addr;
389 u64 vm_exit_msr_load_addr;
390 u64 vm_entry_msr_load_addr;
391 u64 tsc_offset;
392 u64 virtual_apic_page_addr;
393 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800394 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300395 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800396 u64 eoi_exit_bitmap0;
397 u64 eoi_exit_bitmap1;
398 u64 eoi_exit_bitmap2;
399 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800400 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300401 u64 guest_physical_address;
402 u64 vmcs_link_pointer;
403 u64 guest_ia32_debugctl;
404 u64 guest_ia32_pat;
405 u64 guest_ia32_efer;
406 u64 guest_ia32_perf_global_ctrl;
407 u64 guest_pdptr0;
408 u64 guest_pdptr1;
409 u64 guest_pdptr2;
410 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100411 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300412 u64 host_ia32_pat;
413 u64 host_ia32_efer;
414 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700415 u64 vmread_bitmap;
416 u64 vmwrite_bitmap;
417 u64 vm_function_control;
418 u64 eptp_list_address;
419 u64 pml_address;
420 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300421 /*
422 * To allow migration of L1 (complete with its L2 guests) between
423 * machines of different natural widths (32 or 64 bit), we cannot have
424 * unsigned long fields with no explict size. We use u64 (aliased
425 * natural_width) instead. Luckily, x86 is little-endian.
426 */
427 natural_width cr0_guest_host_mask;
428 natural_width cr4_guest_host_mask;
429 natural_width cr0_read_shadow;
430 natural_width cr4_read_shadow;
431 natural_width cr3_target_value0;
432 natural_width cr3_target_value1;
433 natural_width cr3_target_value2;
434 natural_width cr3_target_value3;
435 natural_width exit_qualification;
436 natural_width guest_linear_address;
437 natural_width guest_cr0;
438 natural_width guest_cr3;
439 natural_width guest_cr4;
440 natural_width guest_es_base;
441 natural_width guest_cs_base;
442 natural_width guest_ss_base;
443 natural_width guest_ds_base;
444 natural_width guest_fs_base;
445 natural_width guest_gs_base;
446 natural_width guest_ldtr_base;
447 natural_width guest_tr_base;
448 natural_width guest_gdtr_base;
449 natural_width guest_idtr_base;
450 natural_width guest_dr7;
451 natural_width guest_rsp;
452 natural_width guest_rip;
453 natural_width guest_rflags;
454 natural_width guest_pending_dbg_exceptions;
455 natural_width guest_sysenter_esp;
456 natural_width guest_sysenter_eip;
457 natural_width host_cr0;
458 natural_width host_cr3;
459 natural_width host_cr4;
460 natural_width host_fs_base;
461 natural_width host_gs_base;
462 natural_width host_tr_base;
463 natural_width host_gdtr_base;
464 natural_width host_idtr_base;
465 natural_width host_ia32_sysenter_esp;
466 natural_width host_ia32_sysenter_eip;
467 natural_width host_rsp;
468 natural_width host_rip;
469 natural_width paddingl[8]; /* room for future expansion */
470 u32 pin_based_vm_exec_control;
471 u32 cpu_based_vm_exec_control;
472 u32 exception_bitmap;
473 u32 page_fault_error_code_mask;
474 u32 page_fault_error_code_match;
475 u32 cr3_target_count;
476 u32 vm_exit_controls;
477 u32 vm_exit_msr_store_count;
478 u32 vm_exit_msr_load_count;
479 u32 vm_entry_controls;
480 u32 vm_entry_msr_load_count;
481 u32 vm_entry_intr_info_field;
482 u32 vm_entry_exception_error_code;
483 u32 vm_entry_instruction_len;
484 u32 tpr_threshold;
485 u32 secondary_vm_exec_control;
486 u32 vm_instruction_error;
487 u32 vm_exit_reason;
488 u32 vm_exit_intr_info;
489 u32 vm_exit_intr_error_code;
490 u32 idt_vectoring_info_field;
491 u32 idt_vectoring_error_code;
492 u32 vm_exit_instruction_len;
493 u32 vmx_instruction_info;
494 u32 guest_es_limit;
495 u32 guest_cs_limit;
496 u32 guest_ss_limit;
497 u32 guest_ds_limit;
498 u32 guest_fs_limit;
499 u32 guest_gs_limit;
500 u32 guest_ldtr_limit;
501 u32 guest_tr_limit;
502 u32 guest_gdtr_limit;
503 u32 guest_idtr_limit;
504 u32 guest_es_ar_bytes;
505 u32 guest_cs_ar_bytes;
506 u32 guest_ss_ar_bytes;
507 u32 guest_ds_ar_bytes;
508 u32 guest_fs_ar_bytes;
509 u32 guest_gs_ar_bytes;
510 u32 guest_ldtr_ar_bytes;
511 u32 guest_tr_ar_bytes;
512 u32 guest_interruptibility_info;
513 u32 guest_activity_state;
514 u32 guest_sysenter_cs;
515 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100516 u32 vmx_preemption_timer_value;
517 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300518 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800519 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300520 u16 guest_es_selector;
521 u16 guest_cs_selector;
522 u16 guest_ss_selector;
523 u16 guest_ds_selector;
524 u16 guest_fs_selector;
525 u16 guest_gs_selector;
526 u16 guest_ldtr_selector;
527 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800528 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300529 u16 host_es_selector;
530 u16 host_cs_selector;
531 u16 host_ss_selector;
532 u16 host_ds_selector;
533 u16 host_fs_selector;
534 u16 host_gs_selector;
535 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700536 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300537};
538
539/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700540 * For save/restore compatibility, the vmcs12 field offsets must not change.
541 */
542#define CHECK_OFFSET(field, loc) \
543 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
544 "Offset of " #field " in struct vmcs12 has changed.")
545
546static inline void vmx_check_vmcs12_offsets(void) {
547 CHECK_OFFSET(revision_id, 0);
548 CHECK_OFFSET(abort, 4);
549 CHECK_OFFSET(launch_state, 8);
550 CHECK_OFFSET(io_bitmap_a, 40);
551 CHECK_OFFSET(io_bitmap_b, 48);
552 CHECK_OFFSET(msr_bitmap, 56);
553 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
554 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
555 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
556 CHECK_OFFSET(tsc_offset, 88);
557 CHECK_OFFSET(virtual_apic_page_addr, 96);
558 CHECK_OFFSET(apic_access_addr, 104);
559 CHECK_OFFSET(posted_intr_desc_addr, 112);
560 CHECK_OFFSET(ept_pointer, 120);
561 CHECK_OFFSET(eoi_exit_bitmap0, 128);
562 CHECK_OFFSET(eoi_exit_bitmap1, 136);
563 CHECK_OFFSET(eoi_exit_bitmap2, 144);
564 CHECK_OFFSET(eoi_exit_bitmap3, 152);
565 CHECK_OFFSET(xss_exit_bitmap, 160);
566 CHECK_OFFSET(guest_physical_address, 168);
567 CHECK_OFFSET(vmcs_link_pointer, 176);
568 CHECK_OFFSET(guest_ia32_debugctl, 184);
569 CHECK_OFFSET(guest_ia32_pat, 192);
570 CHECK_OFFSET(guest_ia32_efer, 200);
571 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
572 CHECK_OFFSET(guest_pdptr0, 216);
573 CHECK_OFFSET(guest_pdptr1, 224);
574 CHECK_OFFSET(guest_pdptr2, 232);
575 CHECK_OFFSET(guest_pdptr3, 240);
576 CHECK_OFFSET(guest_bndcfgs, 248);
577 CHECK_OFFSET(host_ia32_pat, 256);
578 CHECK_OFFSET(host_ia32_efer, 264);
579 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
580 CHECK_OFFSET(vmread_bitmap, 280);
581 CHECK_OFFSET(vmwrite_bitmap, 288);
582 CHECK_OFFSET(vm_function_control, 296);
583 CHECK_OFFSET(eptp_list_address, 304);
584 CHECK_OFFSET(pml_address, 312);
585 CHECK_OFFSET(cr0_guest_host_mask, 344);
586 CHECK_OFFSET(cr4_guest_host_mask, 352);
587 CHECK_OFFSET(cr0_read_shadow, 360);
588 CHECK_OFFSET(cr4_read_shadow, 368);
589 CHECK_OFFSET(cr3_target_value0, 376);
590 CHECK_OFFSET(cr3_target_value1, 384);
591 CHECK_OFFSET(cr3_target_value2, 392);
592 CHECK_OFFSET(cr3_target_value3, 400);
593 CHECK_OFFSET(exit_qualification, 408);
594 CHECK_OFFSET(guest_linear_address, 416);
595 CHECK_OFFSET(guest_cr0, 424);
596 CHECK_OFFSET(guest_cr3, 432);
597 CHECK_OFFSET(guest_cr4, 440);
598 CHECK_OFFSET(guest_es_base, 448);
599 CHECK_OFFSET(guest_cs_base, 456);
600 CHECK_OFFSET(guest_ss_base, 464);
601 CHECK_OFFSET(guest_ds_base, 472);
602 CHECK_OFFSET(guest_fs_base, 480);
603 CHECK_OFFSET(guest_gs_base, 488);
604 CHECK_OFFSET(guest_ldtr_base, 496);
605 CHECK_OFFSET(guest_tr_base, 504);
606 CHECK_OFFSET(guest_gdtr_base, 512);
607 CHECK_OFFSET(guest_idtr_base, 520);
608 CHECK_OFFSET(guest_dr7, 528);
609 CHECK_OFFSET(guest_rsp, 536);
610 CHECK_OFFSET(guest_rip, 544);
611 CHECK_OFFSET(guest_rflags, 552);
612 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
613 CHECK_OFFSET(guest_sysenter_esp, 568);
614 CHECK_OFFSET(guest_sysenter_eip, 576);
615 CHECK_OFFSET(host_cr0, 584);
616 CHECK_OFFSET(host_cr3, 592);
617 CHECK_OFFSET(host_cr4, 600);
618 CHECK_OFFSET(host_fs_base, 608);
619 CHECK_OFFSET(host_gs_base, 616);
620 CHECK_OFFSET(host_tr_base, 624);
621 CHECK_OFFSET(host_gdtr_base, 632);
622 CHECK_OFFSET(host_idtr_base, 640);
623 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
624 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
625 CHECK_OFFSET(host_rsp, 664);
626 CHECK_OFFSET(host_rip, 672);
627 CHECK_OFFSET(pin_based_vm_exec_control, 744);
628 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
629 CHECK_OFFSET(exception_bitmap, 752);
630 CHECK_OFFSET(page_fault_error_code_mask, 756);
631 CHECK_OFFSET(page_fault_error_code_match, 760);
632 CHECK_OFFSET(cr3_target_count, 764);
633 CHECK_OFFSET(vm_exit_controls, 768);
634 CHECK_OFFSET(vm_exit_msr_store_count, 772);
635 CHECK_OFFSET(vm_exit_msr_load_count, 776);
636 CHECK_OFFSET(vm_entry_controls, 780);
637 CHECK_OFFSET(vm_entry_msr_load_count, 784);
638 CHECK_OFFSET(vm_entry_intr_info_field, 788);
639 CHECK_OFFSET(vm_entry_exception_error_code, 792);
640 CHECK_OFFSET(vm_entry_instruction_len, 796);
641 CHECK_OFFSET(tpr_threshold, 800);
642 CHECK_OFFSET(secondary_vm_exec_control, 804);
643 CHECK_OFFSET(vm_instruction_error, 808);
644 CHECK_OFFSET(vm_exit_reason, 812);
645 CHECK_OFFSET(vm_exit_intr_info, 816);
646 CHECK_OFFSET(vm_exit_intr_error_code, 820);
647 CHECK_OFFSET(idt_vectoring_info_field, 824);
648 CHECK_OFFSET(idt_vectoring_error_code, 828);
649 CHECK_OFFSET(vm_exit_instruction_len, 832);
650 CHECK_OFFSET(vmx_instruction_info, 836);
651 CHECK_OFFSET(guest_es_limit, 840);
652 CHECK_OFFSET(guest_cs_limit, 844);
653 CHECK_OFFSET(guest_ss_limit, 848);
654 CHECK_OFFSET(guest_ds_limit, 852);
655 CHECK_OFFSET(guest_fs_limit, 856);
656 CHECK_OFFSET(guest_gs_limit, 860);
657 CHECK_OFFSET(guest_ldtr_limit, 864);
658 CHECK_OFFSET(guest_tr_limit, 868);
659 CHECK_OFFSET(guest_gdtr_limit, 872);
660 CHECK_OFFSET(guest_idtr_limit, 876);
661 CHECK_OFFSET(guest_es_ar_bytes, 880);
662 CHECK_OFFSET(guest_cs_ar_bytes, 884);
663 CHECK_OFFSET(guest_ss_ar_bytes, 888);
664 CHECK_OFFSET(guest_ds_ar_bytes, 892);
665 CHECK_OFFSET(guest_fs_ar_bytes, 896);
666 CHECK_OFFSET(guest_gs_ar_bytes, 900);
667 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
668 CHECK_OFFSET(guest_tr_ar_bytes, 908);
669 CHECK_OFFSET(guest_interruptibility_info, 912);
670 CHECK_OFFSET(guest_activity_state, 916);
671 CHECK_OFFSET(guest_sysenter_cs, 920);
672 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
673 CHECK_OFFSET(vmx_preemption_timer_value, 928);
674 CHECK_OFFSET(virtual_processor_id, 960);
675 CHECK_OFFSET(posted_intr_nv, 962);
676 CHECK_OFFSET(guest_es_selector, 964);
677 CHECK_OFFSET(guest_cs_selector, 966);
678 CHECK_OFFSET(guest_ss_selector, 968);
679 CHECK_OFFSET(guest_ds_selector, 970);
680 CHECK_OFFSET(guest_fs_selector, 972);
681 CHECK_OFFSET(guest_gs_selector, 974);
682 CHECK_OFFSET(guest_ldtr_selector, 976);
683 CHECK_OFFSET(guest_tr_selector, 978);
684 CHECK_OFFSET(guest_intr_status, 980);
685 CHECK_OFFSET(host_es_selector, 982);
686 CHECK_OFFSET(host_cs_selector, 984);
687 CHECK_OFFSET(host_ss_selector, 986);
688 CHECK_OFFSET(host_ds_selector, 988);
689 CHECK_OFFSET(host_fs_selector, 990);
690 CHECK_OFFSET(host_gs_selector, 992);
691 CHECK_OFFSET(host_tr_selector, 994);
692 CHECK_OFFSET(guest_pml_index, 996);
693}
694
695/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300696 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
697 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
698 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700699 *
700 * IMPORTANT: Changing this value will break save/restore compatibility with
701 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300702 */
703#define VMCS12_REVISION 0x11e57ed0
704
705/*
706 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
707 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
708 * current implementation, 4K are reserved to avoid future complications.
709 */
710#define VMCS12_SIZE 0x1000
711
712/*
Jim Mattson5b157062017-12-22 12:11:12 -0800713 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
714 * supported VMCS12 field encoding.
715 */
716#define VMCS12_MAX_FIELD_INDEX 0x17
717
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100718struct nested_vmx_msrs {
719 /*
720 * We only store the "true" versions of the VMX capability MSRs. We
721 * generate the "non-true" versions by setting the must-be-1 bits
722 * according to the SDM.
723 */
724 u32 procbased_ctls_low;
725 u32 procbased_ctls_high;
726 u32 secondary_ctls_low;
727 u32 secondary_ctls_high;
728 u32 pinbased_ctls_low;
729 u32 pinbased_ctls_high;
730 u32 exit_ctls_low;
731 u32 exit_ctls_high;
732 u32 entry_ctls_low;
733 u32 entry_ctls_high;
734 u32 misc_low;
735 u32 misc_high;
736 u32 ept_caps;
737 u32 vpid_caps;
738 u64 basic;
739 u64 cr0_fixed0;
740 u64 cr0_fixed1;
741 u64 cr4_fixed0;
742 u64 cr4_fixed1;
743 u64 vmcs_enum;
744 u64 vmfunc_controls;
745};
746
Jim Mattson5b157062017-12-22 12:11:12 -0800747/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300748 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
749 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
750 */
751struct nested_vmx {
752 /* Has the level1 guest done vmxon? */
753 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400754 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400755 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300756
757 /* The guest-physical address of the current VMCS L1 keeps for L2 */
758 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700759 /*
760 * Cache of the guest's VMCS, existing outside of guest memory.
761 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700762 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700763 */
764 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300765 /*
766 * Indicates if the shadow vmcs must be updated with the
767 * data hold by vmcs12
768 */
769 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100770 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300771
Jim Mattson8d860bb2018-05-09 16:56:05 -0400772 bool change_vmcs01_virtual_apic_mode;
773
Nadav Har'El644d7112011-05-25 23:12:35 +0300774 /* L2 must run next, and mustn't decide to exit to L1. */
775 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600776
777 struct loaded_vmcs vmcs02;
778
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300779 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600780 * Guest pages referred to in the vmcs02 with host-physical
781 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300782 */
783 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800784 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800785 struct page *pi_desc_page;
786 struct pi_desc *pi_desc;
787 bool pi_pending;
788 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100789
790 struct hrtimer preemption_timer;
791 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200792
793 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
794 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800795
Wanpeng Li5c614b32015-10-13 09:18:36 -0700796 u16 vpid02;
797 u16 last_vpid;
798
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100799 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200800
801 /* SMM related state */
802 struct {
803 /* in VMX operation on SMM entry? */
804 bool vmxon;
805 /* in guest mode on SMM entry? */
806 bool guest_mode;
807 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300808};
809
Yang Zhang01e439b2013-04-11 19:25:12 +0800810#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800811#define POSTED_INTR_SN 1
812
Yang Zhang01e439b2013-04-11 19:25:12 +0800813/* Posted-Interrupt Descriptor */
814struct pi_desc {
815 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800816 union {
817 struct {
818 /* bit 256 - Outstanding Notification */
819 u16 on : 1,
820 /* bit 257 - Suppress Notification */
821 sn : 1,
822 /* bit 271:258 - Reserved */
823 rsvd_1 : 14;
824 /* bit 279:272 - Notification Vector */
825 u8 nv;
826 /* bit 287:280 - Reserved */
827 u8 rsvd_2;
828 /* bit 319:288 - Notification Destination */
829 u32 ndst;
830 };
831 u64 control;
832 };
833 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800834} __aligned(64);
835
Yang Zhanga20ed542013-04-11 19:25:15 +0800836static bool pi_test_and_set_on(struct pi_desc *pi_desc)
837{
838 return test_and_set_bit(POSTED_INTR_ON,
839 (unsigned long *)&pi_desc->control);
840}
841
842static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
843{
844 return test_and_clear_bit(POSTED_INTR_ON,
845 (unsigned long *)&pi_desc->control);
846}
847
848static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
849{
850 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
851}
852
Feng Wuebbfc762015-09-18 22:29:46 +0800853static inline void pi_clear_sn(struct pi_desc *pi_desc)
854{
855 return clear_bit(POSTED_INTR_SN,
856 (unsigned long *)&pi_desc->control);
857}
858
859static inline void pi_set_sn(struct pi_desc *pi_desc)
860{
861 return set_bit(POSTED_INTR_SN,
862 (unsigned long *)&pi_desc->control);
863}
864
Paolo Bonziniad361092016-09-20 16:15:05 +0200865static inline void pi_clear_on(struct pi_desc *pi_desc)
866{
867 clear_bit(POSTED_INTR_ON,
868 (unsigned long *)&pi_desc->control);
869}
870
Feng Wuebbfc762015-09-18 22:29:46 +0800871static inline int pi_test_on(struct pi_desc *pi_desc)
872{
873 return test_bit(POSTED_INTR_ON,
874 (unsigned long *)&pi_desc->control);
875}
876
877static inline int pi_test_sn(struct pi_desc *pi_desc)
878{
879 return test_bit(POSTED_INTR_SN,
880 (unsigned long *)&pi_desc->control);
881}
882
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400883struct vmx_msrs {
884 unsigned int nr;
885 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
886};
887
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400888struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000889 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300890 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300891 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100892 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300893 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200894 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200895 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300896 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400897 int nmsrs;
898 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800899 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300901 u64 msr_host_kernel_gs_base;
902 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400903#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100904
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100905 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100906 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100907
Gleb Natapov2961e8762013-11-25 15:37:13 +0200908 u32 vm_entry_controls_shadow;
909 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200910 u32 secondary_exec_control;
911
Nadav Har'Eld462b812011-05-24 15:26:10 +0300912 /*
913 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
914 * non-nested (L1) guest, it always points to vmcs01. For a nested
915 * guest (L2), it points to a different VMCS.
916 */
917 struct loaded_vmcs vmcs01;
918 struct loaded_vmcs *loaded_vmcs;
919 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300920 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400921 struct vmx_msrs guest;
922 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300923 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924 struct {
925 int loaded;
926 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300927#ifdef CONFIG_X86_64
928 u16 ds_sel, es_sel;
929#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200930 int gs_ldt_reload_needed;
931 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000932 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400933 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200934 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300935 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300936 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300937 struct kvm_segment segs[8];
938 } rmode;
939 struct {
940 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300941 struct kvm_save_segment {
942 u16 selector;
943 unsigned long base;
944 u32 limit;
945 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300946 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300947 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800948 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300949 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200950
Andi Kleena0861c02009-06-08 17:37:09 +0800951 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800952
Yang Zhang01e439b2013-04-11 19:25:12 +0800953 /* Posted interrupt descriptor */
954 struct pi_desc pi_desc;
955
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300956 /* Support for a guest hypervisor (nested VMX) */
957 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200958
959 /* Dynamic PLE window. */
960 int ple_window;
961 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800962
963 /* Support for PML */
964#define PML_ENTITY_NUM 512
965 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800966
Yunhong Jiang64672c92016-06-13 14:19:59 -0700967 /* apic deadline value in host tsc */
968 u64 hv_deadline_tsc;
969
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800970 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800971
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800972 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800973
Wanpeng Li74c55932017-11-29 01:31:20 -0800974 unsigned long host_debugctlmsr;
975
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800976 /*
977 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
978 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
979 * in msr_ia32_feature_control_valid_bits.
980 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800981 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800982 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400983};
984
Avi Kivity2fb92db2011-04-27 19:42:18 +0300985enum segment_cache_field {
986 SEG_FIELD_SEL = 0,
987 SEG_FIELD_BASE = 1,
988 SEG_FIELD_LIMIT = 2,
989 SEG_FIELD_AR = 3,
990
991 SEG_FIELD_NR = 4
992};
993
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700994static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
995{
996 return container_of(kvm, struct kvm_vmx, kvm);
997}
998
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400999static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1000{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001001 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001002}
1003
Feng Wuefc64402015-09-18 22:29:51 +08001004static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1005{
1006 return &(to_vmx(vcpu)->pi_desc);
1007}
1008
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001009#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001010#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001011#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1012#define FIELD64(number, name) \
1013 FIELD(number, name), \
1014 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001015
Abel Gordon4607c2d2013-04-18 14:35:55 +03001016
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001017static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001018#define SHADOW_FIELD_RO(x) x,
1019#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001020};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001021static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001022 ARRAY_SIZE(shadow_read_only_fields);
1023
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001024static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001025#define SHADOW_FIELD_RW(x) x,
1026#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001027};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001028static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001029 ARRAY_SIZE(shadow_read_write_fields);
1030
Mathias Krause772e0312012-08-30 01:30:19 +02001031static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001032 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001033 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001034 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1035 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1036 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1037 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1038 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1039 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1040 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1041 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001042 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001043 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001044 FIELD(HOST_ES_SELECTOR, host_es_selector),
1045 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1046 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1047 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1048 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1049 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1050 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1051 FIELD64(IO_BITMAP_A, io_bitmap_a),
1052 FIELD64(IO_BITMAP_B, io_bitmap_b),
1053 FIELD64(MSR_BITMAP, msr_bitmap),
1054 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1055 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1056 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001057 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001058 FIELD64(TSC_OFFSET, tsc_offset),
1059 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1060 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001061 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001062 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001063 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001064 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1065 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1066 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1067 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001068 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001069 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1070 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001071 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001072 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1073 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1074 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1075 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1076 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1077 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1078 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1079 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1080 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1081 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001082 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001083 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1084 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1085 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1086 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1087 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1088 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1089 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1090 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1091 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1092 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1093 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1094 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1095 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1096 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1097 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1098 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1099 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1100 FIELD(TPR_THRESHOLD, tpr_threshold),
1101 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1102 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1103 FIELD(VM_EXIT_REASON, vm_exit_reason),
1104 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1105 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1106 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1107 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1108 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1109 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1110 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1111 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1112 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1113 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1114 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1115 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1116 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1117 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1118 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1119 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1120 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1121 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1122 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1123 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1124 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1125 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1126 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1127 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1128 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1129 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1130 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1131 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001132 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001133 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1134 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1135 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1136 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1137 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1138 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1139 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1140 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1141 FIELD(EXIT_QUALIFICATION, exit_qualification),
1142 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1143 FIELD(GUEST_CR0, guest_cr0),
1144 FIELD(GUEST_CR3, guest_cr3),
1145 FIELD(GUEST_CR4, guest_cr4),
1146 FIELD(GUEST_ES_BASE, guest_es_base),
1147 FIELD(GUEST_CS_BASE, guest_cs_base),
1148 FIELD(GUEST_SS_BASE, guest_ss_base),
1149 FIELD(GUEST_DS_BASE, guest_ds_base),
1150 FIELD(GUEST_FS_BASE, guest_fs_base),
1151 FIELD(GUEST_GS_BASE, guest_gs_base),
1152 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1153 FIELD(GUEST_TR_BASE, guest_tr_base),
1154 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1155 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1156 FIELD(GUEST_DR7, guest_dr7),
1157 FIELD(GUEST_RSP, guest_rsp),
1158 FIELD(GUEST_RIP, guest_rip),
1159 FIELD(GUEST_RFLAGS, guest_rflags),
1160 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1161 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1162 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1163 FIELD(HOST_CR0, host_cr0),
1164 FIELD(HOST_CR3, host_cr3),
1165 FIELD(HOST_CR4, host_cr4),
1166 FIELD(HOST_FS_BASE, host_fs_base),
1167 FIELD(HOST_GS_BASE, host_gs_base),
1168 FIELD(HOST_TR_BASE, host_tr_base),
1169 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1170 FIELD(HOST_IDTR_BASE, host_idtr_base),
1171 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1172 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1173 FIELD(HOST_RSP, host_rsp),
1174 FIELD(HOST_RIP, host_rip),
1175};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001176
1177static inline short vmcs_field_to_offset(unsigned long field)
1178{
Dan Williams085331d2018-01-31 17:47:03 -08001179 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1180 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001181 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001182
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001183 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001184 return -ENOENT;
1185
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001186 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001187 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001188 return -ENOENT;
1189
Linus Torvalds15303ba2018-02-10 13:16:35 -08001190 index = array_index_nospec(index, size);
1191 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001192 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001193 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001194 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001195}
1196
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001197static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1198{
David Matlack4f2777b2016-07-13 17:16:37 -07001199 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001200}
1201
Peter Feiner995f00a2017-06-30 17:26:32 -07001202static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001203static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001204static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001205static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001206static void vmx_set_segment(struct kvm_vcpu *vcpu,
1207 struct kvm_segment *var, int seg);
1208static void vmx_get_segment(struct kvm_vcpu *vcpu,
1209 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001210static bool guest_state_valid(struct kvm_vcpu *vcpu);
1211static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001212static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001213static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1214static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1215static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1216 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001217static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001218static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1219 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001220
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1222static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001223/*
1224 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1225 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1226 */
1227static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228
Feng Wubf9f6ac2015-09-18 22:29:55 +08001229/*
1230 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1231 * can find which vCPU should be waken up.
1232 */
1233static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1234static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1235
Radim Krčmář23611332016-09-29 22:41:33 +02001236enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001237 VMX_VMREAD_BITMAP,
1238 VMX_VMWRITE_BITMAP,
1239 VMX_BITMAP_NR
1240};
1241
1242static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1243
Radim Krčmář23611332016-09-29 22:41:33 +02001244#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1245#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001246
Avi Kivity110312c2010-12-21 12:54:20 +02001247static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001248static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001249
Sheng Yang2384d2b2008-01-17 15:14:33 +08001250static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1251static DEFINE_SPINLOCK(vmx_vpid_lock);
1252
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001253static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 int size;
1255 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001256 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001258 u32 pin_based_exec_ctrl;
1259 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001260 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001261 u32 vmexit_ctrl;
1262 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001263 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001264} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265
Hannes Ederefff9e52008-11-28 17:02:06 +01001266static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001267 u32 ept;
1268 u32 vpid;
1269} vmx_capability;
1270
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271#define VMX_SEGMENT_FIELD(seg) \
1272 [VCPU_SREG_##seg] = { \
1273 .selector = GUEST_##seg##_SELECTOR, \
1274 .base = GUEST_##seg##_BASE, \
1275 .limit = GUEST_##seg##_LIMIT, \
1276 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1277 }
1278
Mathias Krause772e0312012-08-30 01:30:19 +02001279static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 unsigned selector;
1281 unsigned base;
1282 unsigned limit;
1283 unsigned ar_bytes;
1284} kvm_vmx_segment_fields[] = {
1285 VMX_SEGMENT_FIELD(CS),
1286 VMX_SEGMENT_FIELD(DS),
1287 VMX_SEGMENT_FIELD(ES),
1288 VMX_SEGMENT_FIELD(FS),
1289 VMX_SEGMENT_FIELD(GS),
1290 VMX_SEGMENT_FIELD(SS),
1291 VMX_SEGMENT_FIELD(TR),
1292 VMX_SEGMENT_FIELD(LDTR),
1293};
1294
Avi Kivity26bb0982009-09-07 11:14:12 +03001295static u64 host_efer;
1296
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001297static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1298
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001299/*
Brian Gerst8c065852010-07-17 09:03:26 -04001300 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001301 * away by decrementing the array size.
1302 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001304#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001305 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001307 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001310DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1311
1312#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1313
1314#define KVM_EVMCS_VERSION 1
1315
1316#if IS_ENABLED(CONFIG_HYPERV)
1317static bool __read_mostly enlightened_vmcs = true;
1318module_param(enlightened_vmcs, bool, 0444);
1319
1320static inline void evmcs_write64(unsigned long field, u64 value)
1321{
1322 u16 clean_field;
1323 int offset = get_evmcs_offset(field, &clean_field);
1324
1325 if (offset < 0)
1326 return;
1327
1328 *(u64 *)((char *)current_evmcs + offset) = value;
1329
1330 current_evmcs->hv_clean_fields &= ~clean_field;
1331}
1332
1333static inline void evmcs_write32(unsigned long field, u32 value)
1334{
1335 u16 clean_field;
1336 int offset = get_evmcs_offset(field, &clean_field);
1337
1338 if (offset < 0)
1339 return;
1340
1341 *(u32 *)((char *)current_evmcs + offset) = value;
1342 current_evmcs->hv_clean_fields &= ~clean_field;
1343}
1344
1345static inline void evmcs_write16(unsigned long field, u16 value)
1346{
1347 u16 clean_field;
1348 int offset = get_evmcs_offset(field, &clean_field);
1349
1350 if (offset < 0)
1351 return;
1352
1353 *(u16 *)((char *)current_evmcs + offset) = value;
1354 current_evmcs->hv_clean_fields &= ~clean_field;
1355}
1356
1357static inline u64 evmcs_read64(unsigned long field)
1358{
1359 int offset = get_evmcs_offset(field, NULL);
1360
1361 if (offset < 0)
1362 return 0;
1363
1364 return *(u64 *)((char *)current_evmcs + offset);
1365}
1366
1367static inline u32 evmcs_read32(unsigned long field)
1368{
1369 int offset = get_evmcs_offset(field, NULL);
1370
1371 if (offset < 0)
1372 return 0;
1373
1374 return *(u32 *)((char *)current_evmcs + offset);
1375}
1376
1377static inline u16 evmcs_read16(unsigned long field)
1378{
1379 int offset = get_evmcs_offset(field, NULL);
1380
1381 if (offset < 0)
1382 return 0;
1383
1384 return *(u16 *)((char *)current_evmcs + offset);
1385}
1386
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001387static inline void evmcs_touch_msr_bitmap(void)
1388{
1389 if (unlikely(!current_evmcs))
1390 return;
1391
1392 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1393 current_evmcs->hv_clean_fields &=
1394 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1395}
1396
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001397static void evmcs_load(u64 phys_addr)
1398{
1399 struct hv_vp_assist_page *vp_ap =
1400 hv_get_vp_assist_page(smp_processor_id());
1401
1402 vp_ap->current_nested_vmcs = phys_addr;
1403 vp_ap->enlighten_vmentry = 1;
1404}
1405
1406static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1407{
1408 /*
1409 * Enlightened VMCSv1 doesn't support these:
1410 *
1411 * POSTED_INTR_NV = 0x00000002,
1412 * GUEST_INTR_STATUS = 0x00000810,
1413 * APIC_ACCESS_ADDR = 0x00002014,
1414 * POSTED_INTR_DESC_ADDR = 0x00002016,
1415 * EOI_EXIT_BITMAP0 = 0x0000201c,
1416 * EOI_EXIT_BITMAP1 = 0x0000201e,
1417 * EOI_EXIT_BITMAP2 = 0x00002020,
1418 * EOI_EXIT_BITMAP3 = 0x00002022,
1419 */
1420 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1421 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1422 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1423 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1424 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1425 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1426 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1427
1428 /*
1429 * GUEST_PML_INDEX = 0x00000812,
1430 * PML_ADDRESS = 0x0000200e,
1431 */
1432 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1433
1434 /* VM_FUNCTION_CONTROL = 0x00002018, */
1435 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1436
1437 /*
1438 * EPTP_LIST_ADDRESS = 0x00002024,
1439 * VMREAD_BITMAP = 0x00002026,
1440 * VMWRITE_BITMAP = 0x00002028,
1441 */
1442 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1443
1444 /*
1445 * TSC_MULTIPLIER = 0x00002032,
1446 */
1447 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1448
1449 /*
1450 * PLE_GAP = 0x00004020,
1451 * PLE_WINDOW = 0x00004022,
1452 */
1453 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1454
1455 /*
1456 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1457 */
1458 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1459
1460 /*
1461 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1462 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1463 */
1464 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1465 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1466
1467 /*
1468 * Currently unsupported in KVM:
1469 * GUEST_IA32_RTIT_CTL = 0x00002814,
1470 */
1471}
1472#else /* !IS_ENABLED(CONFIG_HYPERV) */
1473static inline void evmcs_write64(unsigned long field, u64 value) {}
1474static inline void evmcs_write32(unsigned long field, u32 value) {}
1475static inline void evmcs_write16(unsigned long field, u16 value) {}
1476static inline u64 evmcs_read64(unsigned long field) { return 0; }
1477static inline u32 evmcs_read32(unsigned long field) { return 0; }
1478static inline u16 evmcs_read16(unsigned long field) { return 0; }
1479static inline void evmcs_load(u64 phys_addr) {}
1480static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001481static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001482#endif /* IS_ENABLED(CONFIG_HYPERV) */
1483
Jan Kiszka5bb16012016-02-09 20:14:21 +01001484static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485{
1486 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1487 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001488 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1489}
1490
Jan Kiszka6f054852016-02-09 20:15:18 +01001491static inline bool is_debug(u32 intr_info)
1492{
1493 return is_exception_n(intr_info, DB_VECTOR);
1494}
1495
1496static inline bool is_breakpoint(u32 intr_info)
1497{
1498 return is_exception_n(intr_info, BP_VECTOR);
1499}
1500
Jan Kiszka5bb16012016-02-09 20:14:21 +01001501static inline bool is_page_fault(u32 intr_info)
1502{
1503 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504}
1505
Gui Jianfeng31299942010-03-15 17:29:09 +08001506static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001507{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001508 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001509}
1510
Gui Jianfeng31299942010-03-15 17:29:09 +08001511static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001512{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001513 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001514}
1515
Liran Alon9e869482018-03-12 13:12:51 +02001516static inline bool is_gp_fault(u32 intr_info)
1517{
1518 return is_exception_n(intr_info, GP_VECTOR);
1519}
1520
Gui Jianfeng31299942010-03-15 17:29:09 +08001521static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522{
1523 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1524 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1525}
1526
Gui Jianfeng31299942010-03-15 17:29:09 +08001527static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001528{
1529 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1530 INTR_INFO_VALID_MASK)) ==
1531 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1532}
1533
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001534/* Undocumented: icebp/int1 */
1535static inline bool is_icebp(u32 intr_info)
1536{
1537 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1538 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1539}
1540
Gui Jianfeng31299942010-03-15 17:29:09 +08001541static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001542{
Sheng Yang04547152009-04-01 15:52:31 +08001543 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001544}
1545
Gui Jianfeng31299942010-03-15 17:29:09 +08001546static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001547{
Sheng Yang04547152009-04-01 15:52:31 +08001548 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001549}
1550
Paolo Bonzini35754c92015-07-29 12:05:37 +02001551static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001552{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001553 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001554}
1555
Gui Jianfeng31299942010-03-15 17:29:09 +08001556static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001557{
Sheng Yang04547152009-04-01 15:52:31 +08001558 return vmcs_config.cpu_based_exec_ctrl &
1559 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001560}
1561
Avi Kivity774ead32007-12-26 13:57:04 +02001562static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001563{
Sheng Yang04547152009-04-01 15:52:31 +08001564 return vmcs_config.cpu_based_2nd_exec_ctrl &
1565 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1566}
1567
Yang Zhang8d146952013-01-25 10:18:50 +08001568static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1569{
1570 return vmcs_config.cpu_based_2nd_exec_ctrl &
1571 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1572}
1573
Yang Zhang83d4c282013-01-25 10:18:49 +08001574static inline bool cpu_has_vmx_apic_register_virt(void)
1575{
1576 return vmcs_config.cpu_based_2nd_exec_ctrl &
1577 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1578}
1579
Yang Zhangc7c9c562013-01-25 10:18:51 +08001580static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1581{
1582 return vmcs_config.cpu_based_2nd_exec_ctrl &
1583 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1584}
1585
Yunhong Jiang64672c92016-06-13 14:19:59 -07001586/*
1587 * Comment's format: document - errata name - stepping - processor name.
1588 * Refer from
1589 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1590 */
1591static u32 vmx_preemption_cpu_tfms[] = {
1592/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15930x000206E6,
1594/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1595/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1596/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15970x00020652,
1598/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15990x00020655,
1600/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1601/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1602/*
1603 * 320767.pdf - AAP86 - B1 -
1604 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1605 */
16060x000106E5,
1607/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
16080x000106A0,
1609/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
16100x000106A1,
1611/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
16120x000106A4,
1613 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1614 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1615 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
16160x000106A5,
1617};
1618
1619static inline bool cpu_has_broken_vmx_preemption_timer(void)
1620{
1621 u32 eax = cpuid_eax(0x00000001), i;
1622
1623 /* Clear the reserved bits */
1624 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001625 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001626 if (eax == vmx_preemption_cpu_tfms[i])
1627 return true;
1628
1629 return false;
1630}
1631
1632static inline bool cpu_has_vmx_preemption_timer(void)
1633{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001634 return vmcs_config.pin_based_exec_ctrl &
1635 PIN_BASED_VMX_PREEMPTION_TIMER;
1636}
1637
Yang Zhang01e439b2013-04-11 19:25:12 +08001638static inline bool cpu_has_vmx_posted_intr(void)
1639{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001640 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1641 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001642}
1643
1644static inline bool cpu_has_vmx_apicv(void)
1645{
1646 return cpu_has_vmx_apic_register_virt() &&
1647 cpu_has_vmx_virtual_intr_delivery() &&
1648 cpu_has_vmx_posted_intr();
1649}
1650
Sheng Yang04547152009-04-01 15:52:31 +08001651static inline bool cpu_has_vmx_flexpriority(void)
1652{
1653 return cpu_has_vmx_tpr_shadow() &&
1654 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001655}
1656
Marcelo Tosattie7997942009-06-11 12:07:40 -03001657static inline bool cpu_has_vmx_ept_execute_only(void)
1658{
Gui Jianfeng31299942010-03-15 17:29:09 +08001659 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001660}
1661
Marcelo Tosattie7997942009-06-11 12:07:40 -03001662static inline bool cpu_has_vmx_ept_2m_page(void)
1663{
Gui Jianfeng31299942010-03-15 17:29:09 +08001664 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001665}
1666
Sheng Yang878403b2010-01-05 19:02:29 +08001667static inline bool cpu_has_vmx_ept_1g_page(void)
1668{
Gui Jianfeng31299942010-03-15 17:29:09 +08001669 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001670}
1671
Sheng Yang4bc9b982010-06-02 14:05:24 +08001672static inline bool cpu_has_vmx_ept_4levels(void)
1673{
1674 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1675}
1676
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001677static inline bool cpu_has_vmx_ept_mt_wb(void)
1678{
1679 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1680}
1681
Yu Zhang855feb62017-08-24 20:27:55 +08001682static inline bool cpu_has_vmx_ept_5levels(void)
1683{
1684 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1685}
1686
Xudong Hao83c3a332012-05-28 19:33:35 +08001687static inline bool cpu_has_vmx_ept_ad_bits(void)
1688{
1689 return vmx_capability.ept & VMX_EPT_AD_BIT;
1690}
1691
Gui Jianfeng31299942010-03-15 17:29:09 +08001692static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001693{
Gui Jianfeng31299942010-03-15 17:29:09 +08001694 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001695}
1696
Gui Jianfeng31299942010-03-15 17:29:09 +08001697static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001698{
Gui Jianfeng31299942010-03-15 17:29:09 +08001699 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001700}
1701
Liran Aloncd9a4912018-05-22 17:16:15 +03001702static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1703{
1704 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1705}
1706
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001707static inline bool cpu_has_vmx_invvpid_single(void)
1708{
1709 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1710}
1711
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001712static inline bool cpu_has_vmx_invvpid_global(void)
1713{
1714 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1715}
1716
Wanpeng Li08d839c2017-03-23 05:30:08 -07001717static inline bool cpu_has_vmx_invvpid(void)
1718{
1719 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1720}
1721
Gui Jianfeng31299942010-03-15 17:29:09 +08001722static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001723{
Sheng Yang04547152009-04-01 15:52:31 +08001724 return vmcs_config.cpu_based_2nd_exec_ctrl &
1725 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001726}
1727
Gui Jianfeng31299942010-03-15 17:29:09 +08001728static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001729{
1730 return vmcs_config.cpu_based_2nd_exec_ctrl &
1731 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1732}
1733
Gui Jianfeng31299942010-03-15 17:29:09 +08001734static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001735{
1736 return vmcs_config.cpu_based_2nd_exec_ctrl &
1737 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1738}
1739
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001740static inline bool cpu_has_vmx_basic_inout(void)
1741{
1742 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1743}
1744
Paolo Bonzini35754c92015-07-29 12:05:37 +02001745static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001746{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001747 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001748}
1749
Gui Jianfeng31299942010-03-15 17:29:09 +08001750static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001751{
Sheng Yang04547152009-04-01 15:52:31 +08001752 return vmcs_config.cpu_based_2nd_exec_ctrl &
1753 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001754}
1755
Gui Jianfeng31299942010-03-15 17:29:09 +08001756static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001757{
1758 return vmcs_config.cpu_based_2nd_exec_ctrl &
1759 SECONDARY_EXEC_RDTSCP;
1760}
1761
Mao, Junjiead756a12012-07-02 01:18:48 +00001762static inline bool cpu_has_vmx_invpcid(void)
1763{
1764 return vmcs_config.cpu_based_2nd_exec_ctrl &
1765 SECONDARY_EXEC_ENABLE_INVPCID;
1766}
1767
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001768static inline bool cpu_has_virtual_nmis(void)
1769{
1770 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1771}
1772
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001773static inline bool cpu_has_vmx_wbinvd_exit(void)
1774{
1775 return vmcs_config.cpu_based_2nd_exec_ctrl &
1776 SECONDARY_EXEC_WBINVD_EXITING;
1777}
1778
Abel Gordonabc4fc52013-04-18 14:35:25 +03001779static inline bool cpu_has_vmx_shadow_vmcs(void)
1780{
1781 u64 vmx_msr;
1782 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1783 /* check if the cpu supports writing r/o exit information fields */
1784 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1785 return false;
1786
1787 return vmcs_config.cpu_based_2nd_exec_ctrl &
1788 SECONDARY_EXEC_SHADOW_VMCS;
1789}
1790
Kai Huang843e4332015-01-28 10:54:28 +08001791static inline bool cpu_has_vmx_pml(void)
1792{
1793 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1794}
1795
Haozhong Zhang64903d62015-10-20 15:39:09 +08001796static inline bool cpu_has_vmx_tsc_scaling(void)
1797{
1798 return vmcs_config.cpu_based_2nd_exec_ctrl &
1799 SECONDARY_EXEC_TSC_SCALING;
1800}
1801
Bandan Das2a499e42017-08-03 15:54:41 -04001802static inline bool cpu_has_vmx_vmfunc(void)
1803{
1804 return vmcs_config.cpu_based_2nd_exec_ctrl &
1805 SECONDARY_EXEC_ENABLE_VMFUNC;
1806}
1807
Sean Christopherson64f7a112018-04-30 10:01:06 -07001808static bool vmx_umip_emulated(void)
1809{
1810 return vmcs_config.cpu_based_2nd_exec_ctrl &
1811 SECONDARY_EXEC_DESC;
1812}
1813
Sheng Yang04547152009-04-01 15:52:31 +08001814static inline bool report_flexpriority(void)
1815{
1816 return flexpriority_enabled;
1817}
1818
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001819static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1820{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001821 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001822}
1823
Jim Mattsonf4160e42018-05-29 09:11:33 -07001824/*
1825 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1826 * to modify any valid field of the VMCS, or are the VM-exit
1827 * information fields read-only?
1828 */
1829static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1830{
1831 return to_vmx(vcpu)->nested.msrs.misc_low &
1832 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1833}
1834
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001835static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1836{
1837 return vmcs12->cpu_based_vm_exec_control & bit;
1838}
1839
1840static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1841{
1842 return (vmcs12->cpu_based_vm_exec_control &
1843 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1844 (vmcs12->secondary_vm_exec_control & bit);
1845}
1846
Jan Kiszkaf4124502014-03-07 20:03:13 +01001847static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1848{
1849 return vmcs12->pin_based_vm_exec_control &
1850 PIN_BASED_VMX_PREEMPTION_TIMER;
1851}
1852
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001853static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1854{
1855 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1856}
1857
1858static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1859{
1860 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1861}
1862
Nadav Har'El155a97a2013-08-05 11:07:16 +03001863static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1864{
1865 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1866}
1867
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001868static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1869{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001870 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001871}
1872
Bandan Dasc5f983f2017-05-05 15:25:14 -04001873static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1874{
1875 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1876}
1877
Wincy Vanf2b93282015-02-03 23:56:03 +08001878static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1879{
1880 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1881}
1882
Wanpeng Li5c614b32015-10-13 09:18:36 -07001883static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1884{
1885 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1886}
1887
Wincy Van82f0dd42015-02-03 23:57:18 +08001888static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1889{
1890 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1891}
1892
Wincy Van608406e2015-02-03 23:57:51 +08001893static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1894{
1895 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1896}
1897
Wincy Van705699a2015-02-03 23:58:17 +08001898static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1899{
1900 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1901}
1902
Bandan Das27c42a12017-08-03 15:54:42 -04001903static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1904{
1905 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1906}
1907
Bandan Das41ab9372017-08-03 15:54:43 -04001908static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1909{
1910 return nested_cpu_has_vmfunc(vmcs12) &&
1911 (vmcs12->vm_function_control &
1912 VMX_VMFUNC_EPTP_SWITCHING);
1913}
1914
Jim Mattsonef85b672016-12-12 11:01:37 -08001915static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001916{
1917 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001918 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001919}
1920
Jan Kiszka533558b2014-01-04 18:47:20 +01001921static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1922 u32 exit_intr_info,
1923 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001924static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1925 struct vmcs12 *vmcs12,
1926 u32 reason, unsigned long qualification);
1927
Rusty Russell8b9cf982007-07-30 16:31:43 +10001928static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001929{
1930 int i;
1931
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001932 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001933 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001934 return i;
1935 return -1;
1936}
1937
Sheng Yang2384d2b2008-01-17 15:14:33 +08001938static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1939{
1940 struct {
1941 u64 vpid : 16;
1942 u64 rsvd : 48;
1943 u64 gva;
1944 } operand = { vpid, 0, gva };
1945
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001946 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001947 /* CF==1 or ZF==1 --> rc = -1 */
1948 "; ja 1f ; ud2 ; 1:"
1949 : : "a"(&operand), "c"(ext) : "cc", "memory");
1950}
1951
Sheng Yang14394422008-04-28 12:24:45 +08001952static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1953{
1954 struct {
1955 u64 eptp, gpa;
1956 } operand = {eptp, gpa};
1957
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001958 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001959 /* CF==1 or ZF==1 --> rc = -1 */
1960 "; ja 1f ; ud2 ; 1:\n"
1961 : : "a" (&operand), "c" (ext) : "cc", "memory");
1962}
1963
Avi Kivity26bb0982009-09-07 11:14:12 +03001964static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001965{
1966 int i;
1967
Rusty Russell8b9cf982007-07-30 16:31:43 +10001968 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001969 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001970 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001971 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001972}
1973
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974static void vmcs_clear(struct vmcs *vmcs)
1975{
1976 u64 phys_addr = __pa(vmcs);
1977 u8 error;
1978
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001979 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001980 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981 : "cc", "memory");
1982 if (error)
1983 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1984 vmcs, phys_addr);
1985}
1986
Nadav Har'Eld462b812011-05-24 15:26:10 +03001987static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1988{
1989 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001990 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1991 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001992 loaded_vmcs->cpu = -1;
1993 loaded_vmcs->launched = 0;
1994}
1995
Dongxiao Xu7725b892010-05-11 18:29:38 +08001996static void vmcs_load(struct vmcs *vmcs)
1997{
1998 u64 phys_addr = __pa(vmcs);
1999 u8 error;
2000
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002001 if (static_branch_unlikely(&enable_evmcs))
2002 return evmcs_load(phys_addr);
2003
Dongxiao Xu7725b892010-05-11 18:29:38 +08002004 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02002005 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002006 : "cc", "memory");
2007 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03002008 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002009 vmcs, phys_addr);
2010}
2011
Dave Young2965faa2015-09-09 15:38:55 -07002012#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002013/*
2014 * This bitmap is used to indicate whether the vmclear
2015 * operation is enabled on all cpus. All disabled by
2016 * default.
2017 */
2018static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2019
2020static inline void crash_enable_local_vmclear(int cpu)
2021{
2022 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2023}
2024
2025static inline void crash_disable_local_vmclear(int cpu)
2026{
2027 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2028}
2029
2030static inline int crash_local_vmclear_enabled(int cpu)
2031{
2032 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2033}
2034
2035static void crash_vmclear_local_loaded_vmcss(void)
2036{
2037 int cpu = raw_smp_processor_id();
2038 struct loaded_vmcs *v;
2039
2040 if (!crash_local_vmclear_enabled(cpu))
2041 return;
2042
2043 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2044 loaded_vmcss_on_cpu_link)
2045 vmcs_clear(v->vmcs);
2046}
2047#else
2048static inline void crash_enable_local_vmclear(int cpu) { }
2049static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002050#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002051
Nadav Har'Eld462b812011-05-24 15:26:10 +03002052static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002054 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002055 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056
Nadav Har'Eld462b812011-05-24 15:26:10 +03002057 if (loaded_vmcs->cpu != cpu)
2058 return; /* vcpu migration can race with cpu offline */
2059 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002061 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002062 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002063
2064 /*
2065 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2066 * is before setting loaded_vmcs->vcpu to -1 which is done in
2067 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2068 * then adds the vmcs into percpu list before it is deleted.
2069 */
2070 smp_wmb();
2071
Nadav Har'Eld462b812011-05-24 15:26:10 +03002072 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002073 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074}
2075
Nadav Har'Eld462b812011-05-24 15:26:10 +03002076static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002077{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002078 int cpu = loaded_vmcs->cpu;
2079
2080 if (cpu != -1)
2081 smp_call_function_single(cpu,
2082 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002083}
2084
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002085static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002086{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002087 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002088 return;
2089
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002090 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002091 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002092}
2093
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002094static inline void vpid_sync_vcpu_global(void)
2095{
2096 if (cpu_has_vmx_invvpid_global())
2097 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2098}
2099
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002100static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002101{
2102 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002103 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002104 else
2105 vpid_sync_vcpu_global();
2106}
2107
Sheng Yang14394422008-04-28 12:24:45 +08002108static inline void ept_sync_global(void)
2109{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002110 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002111}
2112
2113static inline void ept_sync_context(u64 eptp)
2114{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002115 if (cpu_has_vmx_invept_context())
2116 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2117 else
2118 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002119}
2120
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002121static __always_inline void vmcs_check16(unsigned long field)
2122{
2123 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2124 "16-bit accessor invalid for 64-bit field");
2125 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2126 "16-bit accessor invalid for 64-bit high field");
2127 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2128 "16-bit accessor invalid for 32-bit high field");
2129 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2130 "16-bit accessor invalid for natural width field");
2131}
2132
2133static __always_inline void vmcs_check32(unsigned long field)
2134{
2135 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2136 "32-bit accessor invalid for 16-bit field");
2137 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2138 "32-bit accessor invalid for natural width field");
2139}
2140
2141static __always_inline void vmcs_check64(unsigned long field)
2142{
2143 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2144 "64-bit accessor invalid for 16-bit field");
2145 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2146 "64-bit accessor invalid for 64-bit high field");
2147 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2148 "64-bit accessor invalid for 32-bit field");
2149 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2150 "64-bit accessor invalid for natural width field");
2151}
2152
2153static __always_inline void vmcs_checkl(unsigned long field)
2154{
2155 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2156 "Natural width accessor invalid for 16-bit field");
2157 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2158 "Natural width accessor invalid for 64-bit field");
2159 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2160 "Natural width accessor invalid for 64-bit high field");
2161 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2162 "Natural width accessor invalid for 32-bit field");
2163}
2164
2165static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166{
Avi Kivity5e520e62011-05-15 10:13:12 -04002167 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168
Avi Kivity5e520e62011-05-15 10:13:12 -04002169 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2170 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171 return value;
2172}
2173
Avi Kivity96304212011-05-15 10:13:13 -04002174static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002176 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002177 if (static_branch_unlikely(&enable_evmcs))
2178 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002179 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180}
2181
Avi Kivity96304212011-05-15 10:13:13 -04002182static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002184 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002185 if (static_branch_unlikely(&enable_evmcs))
2186 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002187 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188}
2189
Avi Kivity96304212011-05-15 10:13:13 -04002190static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002192 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002193 if (static_branch_unlikely(&enable_evmcs))
2194 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002195#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002196 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002198 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199#endif
2200}
2201
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002202static __always_inline unsigned long vmcs_readl(unsigned long field)
2203{
2204 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002205 if (static_branch_unlikely(&enable_evmcs))
2206 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002207 return __vmcs_readl(field);
2208}
2209
Avi Kivitye52de1b2007-01-05 16:36:56 -08002210static noinline void vmwrite_error(unsigned long field, unsigned long value)
2211{
2212 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2213 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2214 dump_stack();
2215}
2216
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002217static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218{
2219 u8 error;
2220
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002221 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002222 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002223 if (unlikely(error))
2224 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225}
2226
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002227static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002229 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002230 if (static_branch_unlikely(&enable_evmcs))
2231 return evmcs_write16(field, value);
2232
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002233 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234}
2235
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002236static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002238 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002239 if (static_branch_unlikely(&enable_evmcs))
2240 return evmcs_write32(field, value);
2241
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002242 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243}
2244
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002245static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002247 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002248 if (static_branch_unlikely(&enable_evmcs))
2249 return evmcs_write64(field, value);
2250
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002251 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002252#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002254 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255#endif
2256}
2257
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002258static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002259{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002260 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002261 if (static_branch_unlikely(&enable_evmcs))
2262 return evmcs_write64(field, value);
2263
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002264 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002265}
2266
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002267static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002268{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002269 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2270 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002271 if (static_branch_unlikely(&enable_evmcs))
2272 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2273
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002274 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2275}
2276
2277static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2278{
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2280 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002281 if (static_branch_unlikely(&enable_evmcs))
2282 return evmcs_write32(field, evmcs_read32(field) | mask);
2283
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002284 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002285}
2286
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002287static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2288{
2289 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2290}
2291
Gleb Natapov2961e8762013-11-25 15:37:13 +02002292static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2293{
2294 vmcs_write32(VM_ENTRY_CONTROLS, val);
2295 vmx->vm_entry_controls_shadow = val;
2296}
2297
2298static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2299{
2300 if (vmx->vm_entry_controls_shadow != val)
2301 vm_entry_controls_init(vmx, val);
2302}
2303
2304static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2305{
2306 return vmx->vm_entry_controls_shadow;
2307}
2308
2309
2310static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2311{
2312 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2313}
2314
2315static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2316{
2317 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2318}
2319
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002320static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2321{
2322 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2323}
2324
Gleb Natapov2961e8762013-11-25 15:37:13 +02002325static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2326{
2327 vmcs_write32(VM_EXIT_CONTROLS, val);
2328 vmx->vm_exit_controls_shadow = val;
2329}
2330
2331static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2332{
2333 if (vmx->vm_exit_controls_shadow != val)
2334 vm_exit_controls_init(vmx, val);
2335}
2336
2337static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2338{
2339 return vmx->vm_exit_controls_shadow;
2340}
2341
2342
2343static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2344{
2345 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2346}
2347
2348static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2349{
2350 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2351}
2352
Avi Kivity2fb92db2011-04-27 19:42:18 +03002353static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2354{
2355 vmx->segment_cache.bitmask = 0;
2356}
2357
2358static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2359 unsigned field)
2360{
2361 bool ret;
2362 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2363
2364 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2365 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2366 vmx->segment_cache.bitmask = 0;
2367 }
2368 ret = vmx->segment_cache.bitmask & mask;
2369 vmx->segment_cache.bitmask |= mask;
2370 return ret;
2371}
2372
2373static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2374{
2375 u16 *p = &vmx->segment_cache.seg[seg].selector;
2376
2377 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2378 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2379 return *p;
2380}
2381
2382static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2383{
2384 ulong *p = &vmx->segment_cache.seg[seg].base;
2385
2386 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2387 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2388 return *p;
2389}
2390
2391static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2392{
2393 u32 *p = &vmx->segment_cache.seg[seg].limit;
2394
2395 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2396 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2397 return *p;
2398}
2399
2400static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2401{
2402 u32 *p = &vmx->segment_cache.seg[seg].ar;
2403
2404 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2405 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2406 return *p;
2407}
2408
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002409static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2410{
2411 u32 eb;
2412
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002413 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002414 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002415 /*
2416 * Guest access to VMware backdoor ports could legitimately
2417 * trigger #GP because of TSS I/O permission bitmap.
2418 * We intercept those #GP and allow access to them anyway
2419 * as VMware does.
2420 */
2421 if (enable_vmware_backdoor)
2422 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002423 if ((vcpu->guest_debug &
2424 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2425 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2426 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002427 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002428 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002429 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002430 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002431
2432 /* When we are running a nested L2 guest and L1 specified for it a
2433 * certain exception bitmap, we must trap the same exceptions and pass
2434 * them to L1. When running L2, we will only handle the exceptions
2435 * specified above if L1 did not want them.
2436 */
2437 if (is_guest_mode(vcpu))
2438 eb |= get_vmcs12(vcpu)->exception_bitmap;
2439
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002440 vmcs_write32(EXCEPTION_BITMAP, eb);
2441}
2442
Ashok Raj15d45072018-02-01 22:59:43 +01002443/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002444 * Check if MSR is intercepted for currently loaded MSR bitmap.
2445 */
2446static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2447{
2448 unsigned long *msr_bitmap;
2449 int f = sizeof(unsigned long);
2450
2451 if (!cpu_has_vmx_msr_bitmap())
2452 return true;
2453
2454 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2455
2456 if (msr <= 0x1fff) {
2457 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2458 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2459 msr &= 0x1fff;
2460 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2461 }
2462
2463 return true;
2464}
2465
2466/*
Ashok Raj15d45072018-02-01 22:59:43 +01002467 * Check if MSR is intercepted for L01 MSR bitmap.
2468 */
2469static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2470{
2471 unsigned long *msr_bitmap;
2472 int f = sizeof(unsigned long);
2473
2474 if (!cpu_has_vmx_msr_bitmap())
2475 return true;
2476
2477 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2478
2479 if (msr <= 0x1fff) {
2480 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2481 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2482 msr &= 0x1fff;
2483 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2484 }
2485
2486 return true;
2487}
2488
Gleb Natapov2961e8762013-11-25 15:37:13 +02002489static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2490 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002491{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002492 vm_entry_controls_clearbit(vmx, entry);
2493 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002494}
2495
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002496static int find_msr(struct vmx_msrs *m, unsigned int msr)
2497{
2498 unsigned int i;
2499
2500 for (i = 0; i < m->nr; ++i) {
2501 if (m->val[i].index == msr)
2502 return i;
2503 }
2504 return -ENOENT;
2505}
2506
Avi Kivity61d2ef22010-04-28 16:40:38 +03002507static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2508{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002509 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002510 struct msr_autoload *m = &vmx->msr_autoload;
2511
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002512 switch (msr) {
2513 case MSR_EFER:
2514 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002515 clear_atomic_switch_msr_special(vmx,
2516 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002517 VM_EXIT_LOAD_IA32_EFER);
2518 return;
2519 }
2520 break;
2521 case MSR_CORE_PERF_GLOBAL_CTRL:
2522 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002523 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002524 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2525 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2526 return;
2527 }
2528 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002529 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002530 i = find_msr(&m->guest, msr);
2531 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002532 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002533 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002534 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002535 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002536
2537skip_guest:
2538 i = find_msr(&m->host, msr);
2539 if (i < 0)
2540 return;
2541
2542 --m->host.nr;
2543 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002544 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002545}
2546
Gleb Natapov2961e8762013-11-25 15:37:13 +02002547static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2548 unsigned long entry, unsigned long exit,
2549 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2550 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002551{
2552 vmcs_write64(guest_val_vmcs, guest_val);
2553 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002554 vm_entry_controls_setbit(vmx, entry);
2555 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002556}
2557
Avi Kivity61d2ef22010-04-28 16:40:38 +03002558static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002559 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002560{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002561 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002562 struct msr_autoload *m = &vmx->msr_autoload;
2563
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002564 switch (msr) {
2565 case MSR_EFER:
2566 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002567 add_atomic_switch_msr_special(vmx,
2568 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002569 VM_EXIT_LOAD_IA32_EFER,
2570 GUEST_IA32_EFER,
2571 HOST_IA32_EFER,
2572 guest_val, host_val);
2573 return;
2574 }
2575 break;
2576 case MSR_CORE_PERF_GLOBAL_CTRL:
2577 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002578 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002579 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2580 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2581 GUEST_IA32_PERF_GLOBAL_CTRL,
2582 HOST_IA32_PERF_GLOBAL_CTRL,
2583 guest_val, host_val);
2584 return;
2585 }
2586 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002587 case MSR_IA32_PEBS_ENABLE:
2588 /* PEBS needs a quiescent period after being disabled (to write
2589 * a record). Disabling PEBS through VMX MSR swapping doesn't
2590 * provide that period, so a CPU could write host's record into
2591 * guest's memory.
2592 */
2593 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002594 }
2595
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002596 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002597 if (!entry_only)
2598 j = find_msr(&m->host, msr);
2599
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002600 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002601 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002602 "Can't add msr %x\n", msr);
2603 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002604 }
2605 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002606 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002607 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002608 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002609 m->guest.val[i].index = msr;
2610 m->guest.val[i].value = guest_val;
2611
2612 if (entry_only)
2613 return;
2614
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002615 if (j < 0) {
2616 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002617 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002618 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002619 m->host.val[j].index = msr;
2620 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002621}
2622
Avi Kivity92c0d902009-10-29 11:00:16 +02002623static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002624{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002625 u64 guest_efer = vmx->vcpu.arch.efer;
2626 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002627
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002628 if (!enable_ept) {
2629 /*
2630 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2631 * host CPUID is more efficient than testing guest CPUID
2632 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2633 */
2634 if (boot_cpu_has(X86_FEATURE_SMEP))
2635 guest_efer |= EFER_NX;
2636 else if (!(guest_efer & EFER_NX))
2637 ignore_bits |= EFER_NX;
2638 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002639
Avi Kivity51c6cf62007-08-29 03:48:05 +03002640 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002641 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002642 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002643 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002644#ifdef CONFIG_X86_64
2645 ignore_bits |= EFER_LMA | EFER_LME;
2646 /* SCE is meaningful only in long mode on Intel */
2647 if (guest_efer & EFER_LMA)
2648 ignore_bits &= ~(u64)EFER_SCE;
2649#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002650
2651 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002652
2653 /*
2654 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2655 * On CPUs that support "load IA32_EFER", always switch EFER
2656 * atomically, since it's faster than switching it manually.
2657 */
2658 if (cpu_has_load_ia32_efer ||
2659 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002660 if (!(guest_efer & EFER_LMA))
2661 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002662 if (guest_efer != host_efer)
2663 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002664 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002665 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002666 } else {
2667 guest_efer &= ~ignore_bits;
2668 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002669
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002670 vmx->guest_msrs[efer_offset].data = guest_efer;
2671 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2672
2673 return true;
2674 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002675}
2676
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002677#ifdef CONFIG_X86_32
2678/*
2679 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2680 * VMCS rather than the segment table. KVM uses this helper to figure
2681 * out the current bases to poke them into the VMCS before entry.
2682 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002683static unsigned long segment_base(u16 selector)
2684{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002685 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002686 unsigned long v;
2687
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002688 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002689 return 0;
2690
Thomas Garnier45fc8752017-03-14 10:05:08 -07002691 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002692
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002693 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002694 u16 ldt_selector = kvm_read_ldt();
2695
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002696 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002697 return 0;
2698
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002699 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002700 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002701 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002702 return v;
2703}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002704#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002705
Avi Kivity04d2cc72007-09-10 18:10:54 +03002706static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002707{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002708 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002709#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002710 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002711#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002712 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002713
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002714 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002715 return;
2716
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002717 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002718 /*
2719 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2720 * allow segment selectors with cpl > 0 or ti == 1.
2721 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002722 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002723 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002724
2725#ifdef CONFIG_X86_64
2726 save_fsgs_for_kvm();
2727 vmx->host_state.fs_sel = current->thread.fsindex;
2728 vmx->host_state.gs_sel = current->thread.gsindex;
2729#else
Avi Kivity9581d442010-10-19 16:46:55 +02002730 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002731 savesegment(gs, vmx->host_state.gs_sel);
2732#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002733 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002734 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002735 vmx->host_state.fs_reload_needed = 0;
2736 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002737 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002738 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002739 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002740 if (!(vmx->host_state.gs_sel & 7))
2741 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002742 else {
2743 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002744 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002745 }
2746
2747#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002748 savesegment(ds, vmx->host_state.ds_sel);
2749 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002750
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002751 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002752 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002753
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002754 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002755 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002756 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002757#else
2758 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2759 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2760#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002761 if (boot_cpu_has(X86_FEATURE_MPX))
2762 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002763 for (i = 0; i < vmx->save_nmsrs; ++i)
2764 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002765 vmx->guest_msrs[i].data,
2766 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002767}
2768
Avi Kivitya9b21b62008-06-24 11:48:49 +03002769static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002770{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002771 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002772 return;
2773
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002774 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002775 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002776#ifdef CONFIG_X86_64
2777 if (is_long_mode(&vmx->vcpu))
2778 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2779#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002780 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002781 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002782#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002783 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002784#else
2785 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002786#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002787 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002788 if (vmx->host_state.fs_reload_needed)
2789 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002790#ifdef CONFIG_X86_64
2791 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2792 loadsegment(ds, vmx->host_state.ds_sel);
2793 loadsegment(es, vmx->host_state.es_sel);
2794 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002795#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002796 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002797#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002798 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002799#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002800 if (vmx->host_state.msr_host_bndcfgs)
2801 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002802 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002803}
2804
Avi Kivitya9b21b62008-06-24 11:48:49 +03002805static void vmx_load_host_state(struct vcpu_vmx *vmx)
2806{
2807 preempt_disable();
2808 __vmx_load_host_state(vmx);
2809 preempt_enable();
2810}
2811
Feng Wu28b835d2015-09-18 22:29:54 +08002812static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2813{
2814 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2815 struct pi_desc old, new;
2816 unsigned int dest;
2817
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002818 /*
2819 * In case of hot-plug or hot-unplug, we may have to undo
2820 * vmx_vcpu_pi_put even if there is no assigned device. And we
2821 * always keep PI.NDST up to date for simplicity: it makes the
2822 * code easier, and CPU migration is not a fast path.
2823 */
2824 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002825 return;
2826
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002827 /*
2828 * First handle the simple case where no cmpxchg is necessary; just
2829 * allow posting non-urgent interrupts.
2830 *
2831 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2832 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2833 * expects the VCPU to be on the blocked_vcpu_list that matches
2834 * PI.NDST.
2835 */
2836 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2837 vcpu->cpu == cpu) {
2838 pi_clear_sn(pi_desc);
2839 return;
2840 }
2841
2842 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002843 do {
2844 old.control = new.control = pi_desc->control;
2845
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002846 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002847
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002848 if (x2apic_enabled())
2849 new.ndst = dest;
2850 else
2851 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002852
Feng Wu28b835d2015-09-18 22:29:54 +08002853 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002854 } while (cmpxchg64(&pi_desc->control, old.control,
2855 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002856}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002857
Peter Feinerc95ba922016-08-17 09:36:47 -07002858static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2859{
2860 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2861 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2862}
2863
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864/*
2865 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2866 * vcpu mutex is already taken.
2867 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002868static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002870 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002871 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002873 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002874 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002875 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002876 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002877
2878 /*
2879 * Read loaded_vmcs->cpu should be before fetching
2880 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2881 * See the comments in __loaded_vmcs_clear().
2882 */
2883 smp_rmb();
2884
Nadav Har'Eld462b812011-05-24 15:26:10 +03002885 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2886 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002887 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002888 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002889 }
2890
2891 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2892 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2893 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002894 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002895 }
2896
2897 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002898 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002899 unsigned long sysenter_esp;
2900
2901 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002902
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 /*
2904 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002905 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002907 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002908 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002909 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002911 /*
2912 * VM exits change the host TR limit to 0x67 after a VM
2913 * exit. This is okay, since 0x67 covers everything except
2914 * the IO bitmap and have have code to handle the IO bitmap
2915 * being lost after a VM exit.
2916 */
2917 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2918
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2920 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002921
Nadav Har'Eld462b812011-05-24 15:26:10 +03002922 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923 }
Feng Wu28b835d2015-09-18 22:29:54 +08002924
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002925 /* Setup TSC multiplier */
2926 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002927 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2928 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002929
Feng Wu28b835d2015-09-18 22:29:54 +08002930 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002931 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002932 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002933}
2934
2935static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2936{
2937 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2938
2939 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002940 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2941 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002942 return;
2943
2944 /* Set SN when the vCPU is preempted */
2945 if (vcpu->preempted)
2946 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947}
2948
2949static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2950{
Feng Wu28b835d2015-09-18 22:29:54 +08002951 vmx_vcpu_pi_put(vcpu);
2952
Avi Kivitya9b21b62008-06-24 11:48:49 +03002953 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954}
2955
Wanpeng Lif244dee2017-07-20 01:11:54 -07002956static bool emulation_required(struct kvm_vcpu *vcpu)
2957{
2958 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2959}
2960
Avi Kivityedcafe32009-12-30 18:07:40 +02002961static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2962
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002963/*
2964 * Return the cr0 value that a nested guest would read. This is a combination
2965 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2966 * its hypervisor (cr0_read_shadow).
2967 */
2968static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2969{
2970 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2971 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2972}
2973static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2974{
2975 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2976 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2977}
2978
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2980{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002981 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002982
Avi Kivity6de12732011-03-07 12:51:22 +02002983 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2984 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2985 rflags = vmcs_readl(GUEST_RFLAGS);
2986 if (to_vmx(vcpu)->rmode.vm86_active) {
2987 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2988 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2989 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2990 }
2991 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002992 }
Avi Kivity6de12732011-03-07 12:51:22 +02002993 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994}
2995
2996static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2997{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002998 unsigned long old_rflags = vmx_get_rflags(vcpu);
2999
Avi Kivity6de12732011-03-07 12:51:22 +02003000 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3001 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003002 if (to_vmx(vcpu)->rmode.vm86_active) {
3003 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003004 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003005 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003007
3008 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3009 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010}
3011
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003012static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003013{
3014 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3015 int ret = 0;
3016
3017 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003018 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003019 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003020 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003021
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003022 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003023}
3024
3025static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3026{
3027 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3028 u32 interruptibility = interruptibility_old;
3029
3030 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3031
Jan Kiszka48005f62010-02-19 19:38:07 +01003032 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003033 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003034 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003035 interruptibility |= GUEST_INTR_STATE_STI;
3036
3037 if ((interruptibility != interruptibility_old))
3038 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3039}
3040
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3042{
3043 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003045 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003047 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048
Glauber Costa2809f5d2009-05-12 16:21:05 -04003049 /* skipping an emulated instruction also counts */
3050 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051}
3052
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003053static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3054 unsigned long exit_qual)
3055{
3056 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3057 unsigned int nr = vcpu->arch.exception.nr;
3058 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3059
3060 if (vcpu->arch.exception.has_error_code) {
3061 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3062 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3063 }
3064
3065 if (kvm_exception_is_soft(nr))
3066 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3067 else
3068 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3069
3070 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3071 vmx_get_nmi_mask(vcpu))
3072 intr_info |= INTR_INFO_UNBLOCK_NMI;
3073
3074 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3075}
3076
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003077/*
3078 * KVM wants to inject page-faults which it got to the guest. This function
3079 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003080 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003081static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003082{
3083 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003084 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003085
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003086 if (nr == PF_VECTOR) {
3087 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003088 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003089 return 1;
3090 }
3091 /*
3092 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3093 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3094 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3095 * can be written only when inject_pending_event runs. This should be
3096 * conditional on a new capability---if the capability is disabled,
3097 * kvm_multiple_exception would write the ancillary information to
3098 * CR2 or DR6, for backwards ABI-compatibility.
3099 */
3100 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3101 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003102 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003103 return 1;
3104 }
3105 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003106 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003107 if (nr == DB_VECTOR)
3108 *exit_qual = vcpu->arch.dr6;
3109 else
3110 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003111 return 1;
3112 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003113 }
3114
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003115 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003116}
3117
Wanpeng Licaa057a2018-03-12 04:53:03 -07003118static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3119{
3120 /*
3121 * Ensure that we clear the HLT state in the VMCS. We don't need to
3122 * explicitly skip the instruction because if the HLT state is set,
3123 * then the instruction is already executing and RIP has already been
3124 * advanced.
3125 */
3126 if (kvm_hlt_in_guest(vcpu->kvm) &&
3127 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3128 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3129}
3130
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003131static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003132{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003133 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003134 unsigned nr = vcpu->arch.exception.nr;
3135 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003136 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003137 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003138
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003139 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003140 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003141 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3142 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003143
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003144 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003145 int inc_eip = 0;
3146 if (kvm_exception_is_soft(nr))
3147 inc_eip = vcpu->arch.event_exit_inst_len;
3148 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003149 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003150 return;
3151 }
3152
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003153 WARN_ON_ONCE(vmx->emulation_required);
3154
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003155 if (kvm_exception_is_soft(nr)) {
3156 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3157 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003158 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3159 } else
3160 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3161
3162 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003163
3164 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003165}
3166
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003167static bool vmx_rdtscp_supported(void)
3168{
3169 return cpu_has_vmx_rdtscp();
3170}
3171
Mao, Junjiead756a12012-07-02 01:18:48 +00003172static bool vmx_invpcid_supported(void)
3173{
3174 return cpu_has_vmx_invpcid() && enable_ept;
3175}
3176
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177/*
Eddie Donga75beee2007-05-17 18:55:15 +03003178 * Swap MSR entry in host/guest MSR entry array.
3179 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003180static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003181{
Avi Kivity26bb0982009-09-07 11:14:12 +03003182 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003183
3184 tmp = vmx->guest_msrs[to];
3185 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3186 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003187}
3188
3189/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003190 * Set up the vmcs to automatically save and restore system
3191 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3192 * mode, as fiddling with msrs is very expensive.
3193 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003194static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003195{
Avi Kivity26bb0982009-09-07 11:14:12 +03003196 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003197
Eddie Donga75beee2007-05-17 18:55:15 +03003198 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003199#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003200 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003201 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003202 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003203 move_msr_up(vmx, index, save_nmsrs++);
3204 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003205 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003206 move_msr_up(vmx, index, save_nmsrs++);
3207 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003208 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003209 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003210 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003211 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003212 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003213 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003214 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003215 * if efer.sce is enabled.
3216 */
Brian Gerst8c065852010-07-17 09:03:26 -04003217 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003218 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003219 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003220 }
Eddie Donga75beee2007-05-17 18:55:15 +03003221#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003222 index = __find_msr_index(vmx, MSR_EFER);
3223 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003224 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003225
Avi Kivity26bb0982009-09-07 11:14:12 +03003226 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003227
Yang Zhang8d146952013-01-25 10:18:50 +08003228 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003229 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003230}
3231
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003232static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003234 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003236 if (is_guest_mode(vcpu) &&
3237 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3238 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3239
3240 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241}
3242
3243/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003244 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003246static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003248 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003249 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003250 * We're here if L1 chose not to trap WRMSR to TSC. According
3251 * to the spec, this should set L1's TSC; The offset that L1
3252 * set for L2 remains unchanged, and still needs to be added
3253 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003254 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003255 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003256 /* recalculate vmcs02.TSC_OFFSET: */
3257 vmcs12 = get_vmcs12(vcpu);
3258 vmcs_write64(TSC_OFFSET, offset +
3259 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3260 vmcs12->tsc_offset : 0));
3261 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003262 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3263 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003264 vmcs_write64(TSC_OFFSET, offset);
3265 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266}
3267
Nadav Har'El801d3422011-05-25 23:02:23 +03003268/*
3269 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3270 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3271 * all guests if the "nested" module option is off, and can also be disabled
3272 * for a single guest by disabling its VMX cpuid bit.
3273 */
3274static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3275{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003276 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003277}
3278
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003280 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3281 * returned for the various VMX controls MSRs when nested VMX is enabled.
3282 * The same values should also be used to verify that vmcs12 control fields are
3283 * valid during nested entry from L1 to L2.
3284 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3285 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3286 * bit in the high half is on if the corresponding bit in the control field
3287 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003288 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003289static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003290{
Paolo Bonzini13893092018-02-26 13:40:09 +01003291 if (!nested) {
3292 memset(msrs, 0, sizeof(*msrs));
3293 return;
3294 }
3295
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003296 /*
3297 * Note that as a general rule, the high half of the MSRs (bits in
3298 * the control fields which may be 1) should be initialized by the
3299 * intersection of the underlying hardware's MSR (i.e., features which
3300 * can be supported) and the list of features we want to expose -
3301 * because they are known to be properly supported in our code.
3302 * Also, usually, the low half of the MSRs (bits which must be 1) can
3303 * be set to 0, meaning that L1 may turn off any of these bits. The
3304 * reason is that if one of these bits is necessary, it will appear
3305 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3306 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003307 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003308 * These rules have exceptions below.
3309 */
3310
3311 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003312 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003313 msrs->pinbased_ctls_low,
3314 msrs->pinbased_ctls_high);
3315 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003316 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003317 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003318 PIN_BASED_EXT_INTR_MASK |
3319 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003320 PIN_BASED_VIRTUAL_NMIS |
3321 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003323 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003324 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003325
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003326 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003327 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->exit_ctls_low,
3329 msrs->exit_ctls_high);
3330 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003331 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003332
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003334#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003335 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003336#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003337 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003338 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003339 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003340 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003341 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3342
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003343 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003344 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003345
Jan Kiszka2996fca2014-06-16 13:59:43 +02003346 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003347 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003348
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003349 /* entry controls */
3350 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003351 msrs->entry_ctls_low,
3352 msrs->entry_ctls_high);
3353 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003354 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003355 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003356#ifdef CONFIG_X86_64
3357 VM_ENTRY_IA32E_MODE |
3358#endif
3359 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003360 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003361 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003362 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003363 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003364
Jan Kiszka2996fca2014-06-16 13:59:43 +02003365 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003366 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003367
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003368 /* cpu-based controls */
3369 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003370 msrs->procbased_ctls_low,
3371 msrs->procbased_ctls_high);
3372 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003373 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003374 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003375 CPU_BASED_VIRTUAL_INTR_PENDING |
3376 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003377 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3378 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3379 CPU_BASED_CR3_STORE_EXITING |
3380#ifdef CONFIG_X86_64
3381 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3382#endif
3383 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003384 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3385 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3386 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3387 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003388 /*
3389 * We can allow some features even when not supported by the
3390 * hardware. For example, L1 can specify an MSR bitmap - and we
3391 * can use it to avoid exits to L1 - even when L0 runs L2
3392 * without MSR bitmaps.
3393 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003394 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003395 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003396 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003397
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003398 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003399 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003400 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3401
Paolo Bonzini80154d72017-08-24 13:55:35 +02003402 /*
3403 * secondary cpu-based controls. Do not include those that
3404 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3405 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003406 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 msrs->secondary_ctls_low,
3408 msrs->secondary_ctls_high);
3409 msrs->secondary_ctls_low = 0;
3410 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003411 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003412 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003413 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003414 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003415 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003416 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003417
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003418 if (enable_ept) {
3419 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003420 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003421 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003422 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003423 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003424 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003425 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003426 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003427 msrs->ept_caps &= vmx_capability.ept;
3428 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003429 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3430 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003431 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003433 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003434 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003435 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003436 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003437
Bandan Das27c42a12017-08-03 15:54:42 -04003438 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003439 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003440 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003441 /*
3442 * Advertise EPTP switching unconditionally
3443 * since we emulate it
3444 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003445 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003446 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003447 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003448 }
3449
Paolo Bonzinief697a72016-03-18 16:58:38 +01003450 /*
3451 * Old versions of KVM use the single-context version without
3452 * checking for support, so declare that it is supported even
3453 * though it is treated as global context. The alternative is
3454 * not failing the single-context invvpid, and it is worse.
3455 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003456 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003457 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003458 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003460 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003461 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003462
Radim Krčmář0790ec12015-03-17 14:02:32 +01003463 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003464 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003465 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3466
Jan Kiszkac18911a2013-03-13 16:06:41 +01003467 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003468 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003469 msrs->misc_low,
3470 msrs->misc_high);
3471 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3472 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003473 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003474 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003475 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003476 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003477
3478 /*
3479 * This MSR reports some information about VMX support. We
3480 * should return information about the VMX we emulate for the
3481 * guest, and the VMCS structure we give it - not about the
3482 * VMX support of the underlying hardware.
3483 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003484 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003485 VMCS12_REVISION |
3486 VMX_BASIC_TRUE_CTLS |
3487 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3488 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3489
3490 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003491 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003492
3493 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003494 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003495 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3496 * We picked the standard core2 setting.
3497 */
3498#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3499#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003500 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3501 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003502
3503 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003504 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3505 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003506
3507 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003508 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003509}
3510
David Matlack38991522016-11-29 18:14:08 -08003511/*
3512 * if fixed0[i] == 1: val[i] must be 1
3513 * if fixed1[i] == 0: val[i] must be 0
3514 */
3515static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3516{
3517 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003518}
3519
3520static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3521{
David Matlack38991522016-11-29 18:14:08 -08003522 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003523}
3524
3525static inline u64 vmx_control_msr(u32 low, u32 high)
3526{
3527 return low | ((u64)high << 32);
3528}
3529
David Matlack62cc6b9d2016-11-29 18:14:07 -08003530static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3531{
3532 superset &= mask;
3533 subset &= mask;
3534
3535 return (superset | subset) == superset;
3536}
3537
3538static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3539{
3540 const u64 feature_and_reserved =
3541 /* feature (except bit 48; see below) */
3542 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3543 /* reserved */
3544 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003546
3547 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3548 return -EINVAL;
3549
3550 /*
3551 * KVM does not emulate a version of VMX that constrains physical
3552 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3553 */
3554 if (data & BIT_ULL(48))
3555 return -EINVAL;
3556
3557 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3558 vmx_basic_vmcs_revision_id(data))
3559 return -EINVAL;
3560
3561 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3562 return -EINVAL;
3563
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003565 return 0;
3566}
3567
3568static int
3569vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3570{
3571 u64 supported;
3572 u32 *lowp, *highp;
3573
3574 switch (msr_index) {
3575 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3577 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003578 break;
3579 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003580 lowp = &vmx->nested.msrs.procbased_ctls_low;
3581 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003582 break;
3583 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003584 lowp = &vmx->nested.msrs.exit_ctls_low;
3585 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003586 break;
3587 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003588 lowp = &vmx->nested.msrs.entry_ctls_low;
3589 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003590 break;
3591 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003592 lowp = &vmx->nested.msrs.secondary_ctls_low;
3593 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003594 break;
3595 default:
3596 BUG();
3597 }
3598
3599 supported = vmx_control_msr(*lowp, *highp);
3600
3601 /* Check must-be-1 bits are still 1. */
3602 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3603 return -EINVAL;
3604
3605 /* Check must-be-0 bits are still 0. */
3606 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3607 return -EINVAL;
3608
3609 *lowp = data;
3610 *highp = data >> 32;
3611 return 0;
3612}
3613
3614static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3615{
3616 const u64 feature_and_reserved_bits =
3617 /* feature */
3618 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3619 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3620 /* reserved */
3621 GENMASK_ULL(13, 9) | BIT_ULL(31);
3622 u64 vmx_misc;
3623
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3625 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003626
3627 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3628 return -EINVAL;
3629
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003630 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003631 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3632 vmx_misc_preemption_timer_rate(data) !=
3633 vmx_misc_preemption_timer_rate(vmx_misc))
3634 return -EINVAL;
3635
3636 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3637 return -EINVAL;
3638
3639 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3640 return -EINVAL;
3641
3642 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3643 return -EINVAL;
3644
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 vmx->nested.msrs.misc_low = data;
3646 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003647
3648 /*
3649 * If L1 has read-only VM-exit information fields, use the
3650 * less permissive vmx_vmwrite_bitmap to specify write
3651 * permissions for the shadow VMCS.
3652 */
3653 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3654 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3655
David Matlack62cc6b9d2016-11-29 18:14:07 -08003656 return 0;
3657}
3658
3659static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3660{
3661 u64 vmx_ept_vpid_cap;
3662
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3664 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003665
3666 /* Every bit is either reserved or a feature bit. */
3667 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3668 return -EINVAL;
3669
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 vmx->nested.msrs.ept_caps = data;
3671 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003672 return 0;
3673}
3674
3675static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3676{
3677 u64 *msr;
3678
3679 switch (msr_index) {
3680 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003681 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003682 break;
3683 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003684 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003685 break;
3686 default:
3687 BUG();
3688 }
3689
3690 /*
3691 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3692 * must be 1 in the restored value.
3693 */
3694 if (!is_bitwise_subset(data, *msr, -1ULL))
3695 return -EINVAL;
3696
3697 *msr = data;
3698 return 0;
3699}
3700
3701/*
3702 * Called when userspace is restoring VMX MSRs.
3703 *
3704 * Returns 0 on success, non-0 otherwise.
3705 */
3706static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3707{
3708 struct vcpu_vmx *vmx = to_vmx(vcpu);
3709
Jim Mattsona943ac52018-05-29 09:11:32 -07003710 /*
3711 * Don't allow changes to the VMX capability MSRs while the vCPU
3712 * is in VMX operation.
3713 */
3714 if (vmx->nested.vmxon)
3715 return -EBUSY;
3716
David Matlack62cc6b9d2016-11-29 18:14:07 -08003717 switch (msr_index) {
3718 case MSR_IA32_VMX_BASIC:
3719 return vmx_restore_vmx_basic(vmx, data);
3720 case MSR_IA32_VMX_PINBASED_CTLS:
3721 case MSR_IA32_VMX_PROCBASED_CTLS:
3722 case MSR_IA32_VMX_EXIT_CTLS:
3723 case MSR_IA32_VMX_ENTRY_CTLS:
3724 /*
3725 * The "non-true" VMX capability MSRs are generated from the
3726 * "true" MSRs, so we do not support restoring them directly.
3727 *
3728 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3729 * should restore the "true" MSRs with the must-be-1 bits
3730 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3731 * DEFAULT SETTINGS".
3732 */
3733 return -EINVAL;
3734 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3735 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3736 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3737 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3738 case MSR_IA32_VMX_PROCBASED_CTLS2:
3739 return vmx_restore_control_msr(vmx, msr_index, data);
3740 case MSR_IA32_VMX_MISC:
3741 return vmx_restore_vmx_misc(vmx, data);
3742 case MSR_IA32_VMX_CR0_FIXED0:
3743 case MSR_IA32_VMX_CR4_FIXED0:
3744 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3745 case MSR_IA32_VMX_CR0_FIXED1:
3746 case MSR_IA32_VMX_CR4_FIXED1:
3747 /*
3748 * These MSRs are generated based on the vCPU's CPUID, so we
3749 * do not support restoring them directly.
3750 */
3751 return -EINVAL;
3752 case MSR_IA32_VMX_EPT_VPID_CAP:
3753 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3754 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003755 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003756 return 0;
3757 default:
3758 /*
3759 * The rest of the VMX capability MSRs do not support restore.
3760 */
3761 return -EINVAL;
3762 }
3763}
3764
Jan Kiszkacae50132014-01-04 18:47:22 +01003765/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003766static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003767{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003768 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003769 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003771 break;
3772 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3773 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003774 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 msrs->pinbased_ctls_low,
3776 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003777 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3778 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003779 break;
3780 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3781 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003782 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003783 msrs->procbased_ctls_low,
3784 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003785 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3786 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003787 break;
3788 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3789 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003790 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003791 msrs->exit_ctls_low,
3792 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003793 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3794 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003795 break;
3796 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3797 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003798 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003799 msrs->entry_ctls_low,
3800 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003801 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3802 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003803 break;
3804 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003805 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 msrs->misc_low,
3807 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003808 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003809 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003811 break;
3812 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003813 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003814 break;
3815 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003816 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003817 break;
3818 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003819 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003820 break;
3821 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003822 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003823 break;
3824 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003825 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003826 msrs->secondary_ctls_low,
3827 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003828 break;
3829 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003830 *pdata = msrs->ept_caps |
3831 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003832 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003833 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003834 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003835 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003836 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003837 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003838 }
3839
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003840 return 0;
3841}
3842
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003843static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3844 uint64_t val)
3845{
3846 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3847
3848 return !(val & ~valid_bits);
3849}
3850
Tom Lendacky801e4592018-02-21 13:39:51 -06003851static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3852{
Paolo Bonzini13893092018-02-26 13:40:09 +01003853 switch (msr->index) {
3854 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3855 if (!nested)
3856 return 1;
3857 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3858 default:
3859 return 1;
3860 }
3861
3862 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003863}
3864
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003865/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 * Reads an msr value (of 'msr_index') into 'pdata'.
3867 * Returns 0 on success, non-0 otherwise.
3868 * Assumes vcpu_load() was already called.
3869 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003870static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003872 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003873 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003875 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003876#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003878 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879 break;
3880 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003881 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003883 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003884 vmx_load_host_state(vmx);
3885 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003886 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003887#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003889 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003890 case MSR_IA32_SPEC_CTRL:
3891 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003892 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3893 return 1;
3894
3895 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3896 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003897 case MSR_IA32_ARCH_CAPABILITIES:
3898 if (!msr_info->host_initiated &&
3899 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3900 return 1;
3901 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3902 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003903 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003904 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905 break;
3906 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003907 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908 break;
3909 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003910 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003912 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003913 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003914 (!msr_info->host_initiated &&
3915 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003916 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003917 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003918 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003919 case MSR_IA32_MCG_EXT_CTL:
3920 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003921 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003922 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003923 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003924 msr_info->data = vcpu->arch.mcg_ext_ctl;
3925 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003926 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003927 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003928 break;
3929 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3930 if (!nested_vmx_allowed(vcpu))
3931 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003932 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3933 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003934 case MSR_IA32_XSS:
3935 if (!vmx_xsaves_supported())
3936 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003937 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003938 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003939 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003940 if (!msr_info->host_initiated &&
3941 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003942 return 1;
3943 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003945 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003946 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003947 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003948 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003950 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 }
3952
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953 return 0;
3954}
3955
Jan Kiszkacae50132014-01-04 18:47:22 +01003956static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3957
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958/*
3959 * Writes msr value into into the appropriate "register".
3960 * Returns 0 on success, non-0 otherwise.
3961 * Assumes vcpu_load() was already called.
3962 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003963static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003965 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003966 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003967 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003968 u32 msr_index = msr_info->index;
3969 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003970
Avi Kivity6aa8b732006-12-10 02:21:36 -08003971 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003972 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003973 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003974 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003975#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003976 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003977 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 vmcs_writel(GUEST_FS_BASE, data);
3979 break;
3980 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003981 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982 vmcs_writel(GUEST_GS_BASE, data);
3983 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003984 case MSR_KERNEL_GS_BASE:
3985 vmx_load_host_state(vmx);
3986 vmx->msr_guest_kernel_gs_base = data;
3987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988#endif
3989 case MSR_IA32_SYSENTER_CS:
3990 vmcs_write32(GUEST_SYSENTER_CS, data);
3991 break;
3992 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003993 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994 break;
3995 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003996 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003998 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003999 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004000 (!msr_info->host_initiated &&
4001 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004002 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004003 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004004 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004006 vmcs_write64(GUEST_BNDCFGS, data);
4007 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004008 case MSR_IA32_SPEC_CTRL:
4009 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004010 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4011 return 1;
4012
4013 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004014 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004015 return 1;
4016
4017 vmx->spec_ctrl = data;
4018
4019 if (!data)
4020 break;
4021
4022 /*
4023 * For non-nested:
4024 * When it's written (to non-zero) for the first time, pass
4025 * it through.
4026 *
4027 * For nested:
4028 * The handling of the MSR bitmap for L2 guests is done in
4029 * nested_vmx_merge_msr_bitmap. We should not touch the
4030 * vmcs02.msr_bitmap here since it gets completely overwritten
4031 * in the merging. We update the vmcs01 here for L1 as well
4032 * since it will end up touching the MSR anyway now.
4033 */
4034 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4035 MSR_IA32_SPEC_CTRL,
4036 MSR_TYPE_RW);
4037 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004038 case MSR_IA32_PRED_CMD:
4039 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004040 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4041 return 1;
4042
4043 if (data & ~PRED_CMD_IBPB)
4044 return 1;
4045
4046 if (!data)
4047 break;
4048
4049 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4050
4051 /*
4052 * For non-nested:
4053 * When it's written (to non-zero) for the first time, pass
4054 * it through.
4055 *
4056 * For nested:
4057 * The handling of the MSR bitmap for L2 guests is done in
4058 * nested_vmx_merge_msr_bitmap. We should not touch the
4059 * vmcs02.msr_bitmap here since it gets completely overwritten
4060 * in the merging.
4061 */
4062 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4063 MSR_TYPE_W);
4064 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004065 case MSR_IA32_ARCH_CAPABILITIES:
4066 if (!msr_info->host_initiated)
4067 return 1;
4068 vmx->arch_capabilities = data;
4069 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004070 case MSR_IA32_CR_PAT:
4071 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004072 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4073 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004074 vmcs_write64(GUEST_IA32_PAT, data);
4075 vcpu->arch.pat = data;
4076 break;
4077 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004078 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004079 break;
Will Auldba904632012-11-29 12:42:50 -08004080 case MSR_IA32_TSC_ADJUST:
4081 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004082 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004083 case MSR_IA32_MCG_EXT_CTL:
4084 if ((!msr_info->host_initiated &&
4085 !(to_vmx(vcpu)->msr_ia32_feature_control &
4086 FEATURE_CONTROL_LMCE)) ||
4087 (data & ~MCG_EXT_CTL_LMCE_EN))
4088 return 1;
4089 vcpu->arch.mcg_ext_ctl = data;
4090 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004091 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004092 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004093 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004094 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4095 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004096 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004097 if (msr_info->host_initiated && data == 0)
4098 vmx_leave_nested(vcpu);
4099 break;
4100 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004101 if (!msr_info->host_initiated)
4102 return 1; /* they are read-only */
4103 if (!nested_vmx_allowed(vcpu))
4104 return 1;
4105 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004106 case MSR_IA32_XSS:
4107 if (!vmx_xsaves_supported())
4108 return 1;
4109 /*
4110 * The only supported bit as of Skylake is bit 8, but
4111 * it is not supported on KVM.
4112 */
4113 if (data != 0)
4114 return 1;
4115 vcpu->arch.ia32_xss = data;
4116 if (vcpu->arch.ia32_xss != host_xss)
4117 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004118 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004119 else
4120 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4121 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004122 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004123 if (!msr_info->host_initiated &&
4124 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004125 return 1;
4126 /* Check reserved bit, higher 32 bits should be zero */
4127 if ((data >> 32) != 0)
4128 return 1;
4129 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004131 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004132 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004133 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004134 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004135 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4136 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004137 ret = kvm_set_shared_msr(msr->index, msr->data,
4138 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004139 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004140 if (ret)
4141 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004142 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004143 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004145 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 }
4147
Eddie Dong2cc51562007-05-21 07:28:09 +03004148 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149}
4150
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004151static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004153 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4154 switch (reg) {
4155 case VCPU_REGS_RSP:
4156 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4157 break;
4158 case VCPU_REGS_RIP:
4159 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4160 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004161 case VCPU_EXREG_PDPTR:
4162 if (enable_ept)
4163 ept_save_pdptrs(vcpu);
4164 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004165 default:
4166 break;
4167 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168}
4169
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170static __init int cpu_has_kvm_support(void)
4171{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004172 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173}
4174
4175static __init int vmx_disabled_by_bios(void)
4176{
4177 u64 msr;
4178
4179 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004180 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004181 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004182 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4183 && tboot_enabled())
4184 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004185 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004186 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004187 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004188 && !tboot_enabled()) {
4189 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004190 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004191 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004192 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004193 /* launched w/o TXT and VMX disabled */
4194 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4195 && !tboot_enabled())
4196 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004197 }
4198
4199 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004200}
4201
Dongxiao Xu7725b892010-05-11 18:29:38 +08004202static void kvm_cpu_vmxon(u64 addr)
4203{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004204 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004205 intel_pt_handle_vmx(1);
4206
Dongxiao Xu7725b892010-05-11 18:29:38 +08004207 asm volatile (ASM_VMX_VMXON_RAX
4208 : : "a"(&addr), "m"(addr)
4209 : "memory", "cc");
4210}
4211
Radim Krčmář13a34e02014-08-28 15:13:03 +02004212static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213{
4214 int cpu = raw_smp_processor_id();
4215 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004216 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004218 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004219 return -EBUSY;
4220
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004221 /*
4222 * This can happen if we hot-added a CPU but failed to allocate
4223 * VP assist page for it.
4224 */
4225 if (static_branch_unlikely(&enable_evmcs) &&
4226 !hv_get_vp_assist_page(cpu))
4227 return -EFAULT;
4228
Nadav Har'Eld462b812011-05-24 15:26:10 +03004229 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004230 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4231 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004232
4233 /*
4234 * Now we can enable the vmclear operation in kdump
4235 * since the loaded_vmcss_on_cpu list on this cpu
4236 * has been initialized.
4237 *
4238 * Though the cpu is not in VMX operation now, there
4239 * is no problem to enable the vmclear operation
4240 * for the loaded_vmcss_on_cpu list is empty!
4241 */
4242 crash_enable_local_vmclear(cpu);
4243
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004245
4246 test_bits = FEATURE_CONTROL_LOCKED;
4247 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4248 if (tboot_enabled())
4249 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4250
4251 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004253 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4254 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004255 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004256 if (enable_ept)
4257 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004258
4259 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260}
4261
Nadav Har'Eld462b812011-05-24 15:26:10 +03004262static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004263{
4264 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004265 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004266
Nadav Har'Eld462b812011-05-24 15:26:10 +03004267 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4268 loaded_vmcss_on_cpu_link)
4269 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004270}
4271
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004272
4273/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4274 * tricks.
4275 */
4276static void kvm_cpu_vmxoff(void)
4277{
4278 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004279
4280 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004281 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004282}
4283
Radim Krčmář13a34e02014-08-28 15:13:03 +02004284static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004285{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004286 vmclear_local_loaded_vmcss();
4287 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004288}
4289
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004290static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004291 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004292{
4293 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004294 u32 ctl = ctl_min | ctl_opt;
4295
4296 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4297
4298 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4299 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4300
4301 /* Ensure minimum (required) set of control bits are supported. */
4302 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004303 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004304
4305 *result = ctl;
4306 return 0;
4307}
4308
Avi Kivity110312c2010-12-21 12:54:20 +02004309static __init bool allow_1_setting(u32 msr, u32 ctl)
4310{
4311 u32 vmx_msr_low, vmx_msr_high;
4312
4313 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4314 return vmx_msr_high & ctl;
4315}
4316
Yang, Sheng002c7f72007-07-31 14:23:01 +03004317static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004318{
4319 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004320 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004321 u32 _pin_based_exec_control = 0;
4322 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004323 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004324 u32 _vmexit_control = 0;
4325 u32 _vmentry_control = 0;
4326
Paolo Bonzini13893092018-02-26 13:40:09 +01004327 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304328 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004329#ifdef CONFIG_X86_64
4330 CPU_BASED_CR8_LOAD_EXITING |
4331 CPU_BASED_CR8_STORE_EXITING |
4332#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004333 CPU_BASED_CR3_LOAD_EXITING |
4334 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004335 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004336 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004337 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004338 CPU_BASED_MWAIT_EXITING |
4339 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004340 CPU_BASED_INVLPG_EXITING |
4341 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004342
Sheng Yangf78e0e22007-10-29 09:40:42 +08004343 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004344 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004345 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004346 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4347 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004348 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004349#ifdef CONFIG_X86_64
4350 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4351 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4352 ~CPU_BASED_CR8_STORE_EXITING;
4353#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004354 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004355 min2 = 0;
4356 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004357 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004358 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004359 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004360 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004361 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004362 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004363 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004364 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004365 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004366 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004367 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004368 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004369 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004370 SECONDARY_EXEC_RDSEED_EXITING |
4371 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004372 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004373 SECONDARY_EXEC_TSC_SCALING |
4374 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004375 if (adjust_vmx_controls(min2, opt2,
4376 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004377 &_cpu_based_2nd_exec_control) < 0)
4378 return -EIO;
4379 }
4380#ifndef CONFIG_X86_64
4381 if (!(_cpu_based_2nd_exec_control &
4382 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4383 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4384#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004385
4386 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4387 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004388 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004389 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4390 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004391
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004392 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4393 &vmx_capability.ept, &vmx_capability.vpid);
4394
Sheng Yangd56f5462008-04-25 10:13:16 +08004395 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004396 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4397 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004398 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4399 CPU_BASED_CR3_STORE_EXITING |
4400 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004401 } else if (vmx_capability.ept) {
4402 vmx_capability.ept = 0;
4403 pr_warn_once("EPT CAP should not exist if not support "
4404 "1-setting enable EPT VM-execution control\n");
4405 }
4406 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4407 vmx_capability.vpid) {
4408 vmx_capability.vpid = 0;
4409 pr_warn_once("VPID CAP should not exist if not support "
4410 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004411 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004412
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004413 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004414#ifdef CONFIG_X86_64
4415 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4416#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004417 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004418 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004419 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4420 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004421 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004422
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004423 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4424 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4425 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004426 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4427 &_pin_based_exec_control) < 0)
4428 return -EIO;
4429
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004430 if (cpu_has_broken_vmx_preemption_timer())
4431 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004432 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004433 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004434 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4435
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004436 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004437 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004438 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4439 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004440 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004442 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004443
4444 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4445 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004446 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004447
4448#ifdef CONFIG_X86_64
4449 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4450 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004451 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004452#endif
4453
4454 /* Require Write-Back (WB) memory type for VMCS accesses. */
4455 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004456 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004457
Yang, Sheng002c7f72007-07-31 14:23:01 +03004458 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004459 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004460 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004461
4462 /* KVM supports Enlightened VMCS v1 only */
4463 if (static_branch_unlikely(&enable_evmcs))
4464 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4465 else
4466 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004467
Yang, Sheng002c7f72007-07-31 14:23:01 +03004468 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4469 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004470 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004471 vmcs_conf->vmexit_ctrl = _vmexit_control;
4472 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004473
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004474 if (static_branch_unlikely(&enable_evmcs))
4475 evmcs_sanitize_exec_ctrls(vmcs_conf);
4476
Avi Kivity110312c2010-12-21 12:54:20 +02004477 cpu_has_load_ia32_efer =
4478 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4479 VM_ENTRY_LOAD_IA32_EFER)
4480 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4481 VM_EXIT_LOAD_IA32_EFER);
4482
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004483 cpu_has_load_perf_global_ctrl =
4484 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4485 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4486 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4487 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4488
4489 /*
4490 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004491 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004492 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4493 *
4494 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4495 *
4496 * AAK155 (model 26)
4497 * AAP115 (model 30)
4498 * AAT100 (model 37)
4499 * BC86,AAY89,BD102 (model 44)
4500 * BA97 (model 46)
4501 *
4502 */
4503 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4504 switch (boot_cpu_data.x86_model) {
4505 case 26:
4506 case 30:
4507 case 37:
4508 case 44:
4509 case 46:
4510 cpu_has_load_perf_global_ctrl = false;
4511 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4512 "does not work properly. Using workaround\n");
4513 break;
4514 default:
4515 break;
4516 }
4517 }
4518
Borislav Petkov782511b2016-04-04 22:25:03 +02004519 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004520 rdmsrl(MSR_IA32_XSS, host_xss);
4521
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004522 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004523}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004524
4525static struct vmcs *alloc_vmcs_cpu(int cpu)
4526{
4527 int node = cpu_to_node(cpu);
4528 struct page *pages;
4529 struct vmcs *vmcs;
4530
Vlastimil Babka96db8002015-09-08 15:03:50 -07004531 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532 if (!pages)
4533 return NULL;
4534 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004535 memset(vmcs, 0, vmcs_config.size);
4536 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004537 return vmcs;
4538}
4539
Avi Kivity6aa8b732006-12-10 02:21:36 -08004540static void free_vmcs(struct vmcs *vmcs)
4541{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004542 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004543}
4544
Nadav Har'Eld462b812011-05-24 15:26:10 +03004545/*
4546 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4547 */
4548static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4549{
4550 if (!loaded_vmcs->vmcs)
4551 return;
4552 loaded_vmcs_clear(loaded_vmcs);
4553 free_vmcs(loaded_vmcs->vmcs);
4554 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004555 if (loaded_vmcs->msr_bitmap)
4556 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004557 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004558}
4559
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004560static struct vmcs *alloc_vmcs(void)
4561{
4562 return alloc_vmcs_cpu(raw_smp_processor_id());
4563}
4564
4565static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4566{
4567 loaded_vmcs->vmcs = alloc_vmcs();
4568 if (!loaded_vmcs->vmcs)
4569 return -ENOMEM;
4570
4571 loaded_vmcs->shadow_vmcs = NULL;
4572 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004573
4574 if (cpu_has_vmx_msr_bitmap()) {
4575 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4576 if (!loaded_vmcs->msr_bitmap)
4577 goto out_vmcs;
4578 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004579
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004580 if (IS_ENABLED(CONFIG_HYPERV) &&
4581 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004582 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4583 struct hv_enlightened_vmcs *evmcs =
4584 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4585
4586 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4587 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004588 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004589 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004590
4591out_vmcs:
4592 free_loaded_vmcs(loaded_vmcs);
4593 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004594}
4595
Sam Ravnborg39959582007-06-01 00:47:13 -07004596static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004597{
4598 int cpu;
4599
Zachary Amsden3230bb42009-09-29 11:38:37 -10004600 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004601 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004602 per_cpu(vmxarea, cpu) = NULL;
4603 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004604}
4605
Jim Mattsond37f4262017-12-22 12:12:16 -08004606enum vmcs_field_width {
4607 VMCS_FIELD_WIDTH_U16 = 0,
4608 VMCS_FIELD_WIDTH_U64 = 1,
4609 VMCS_FIELD_WIDTH_U32 = 2,
4610 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004611};
4612
Jim Mattsond37f4262017-12-22 12:12:16 -08004613static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004614{
4615 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004616 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004617 return (field >> 13) & 0x3 ;
4618}
4619
4620static inline int vmcs_field_readonly(unsigned long field)
4621{
4622 return (((field >> 10) & 0x3) == 1);
4623}
4624
Bandan Dasfe2b2012014-04-21 15:20:14 -04004625static void init_vmcs_shadow_fields(void)
4626{
4627 int i, j;
4628
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004629 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4630 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004631 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004632 (i + 1 == max_shadow_read_only_fields ||
4633 shadow_read_only_fields[i + 1] != field + 1))
4634 pr_err("Missing field from shadow_read_only_field %x\n",
4635 field + 1);
4636
4637 clear_bit(field, vmx_vmread_bitmap);
4638#ifdef CONFIG_X86_64
4639 if (field & 1)
4640 continue;
4641#endif
4642 if (j < i)
4643 shadow_read_only_fields[j] = field;
4644 j++;
4645 }
4646 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004647
4648 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004649 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004650 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004651 (i + 1 == max_shadow_read_write_fields ||
4652 shadow_read_write_fields[i + 1] != field + 1))
4653 pr_err("Missing field from shadow_read_write_field %x\n",
4654 field + 1);
4655
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004656 /*
4657 * PML and the preemption timer can be emulated, but the
4658 * processor cannot vmwrite to fields that don't exist
4659 * on bare metal.
4660 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004661 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004662 case GUEST_PML_INDEX:
4663 if (!cpu_has_vmx_pml())
4664 continue;
4665 break;
4666 case VMX_PREEMPTION_TIMER_VALUE:
4667 if (!cpu_has_vmx_preemption_timer())
4668 continue;
4669 break;
4670 case GUEST_INTR_STATUS:
4671 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004672 continue;
4673 break;
4674 default:
4675 break;
4676 }
4677
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004678 clear_bit(field, vmx_vmwrite_bitmap);
4679 clear_bit(field, vmx_vmread_bitmap);
4680#ifdef CONFIG_X86_64
4681 if (field & 1)
4682 continue;
4683#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004684 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004685 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004686 j++;
4687 }
4688 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004689}
4690
Avi Kivity6aa8b732006-12-10 02:21:36 -08004691static __init int alloc_kvm_area(void)
4692{
4693 int cpu;
4694
Zachary Amsden3230bb42009-09-29 11:38:37 -10004695 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696 struct vmcs *vmcs;
4697
4698 vmcs = alloc_vmcs_cpu(cpu);
4699 if (!vmcs) {
4700 free_kvm_area();
4701 return -ENOMEM;
4702 }
4703
4704 per_cpu(vmxarea, cpu) = vmcs;
4705 }
4706 return 0;
4707}
4708
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004709static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004710 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004712 if (!emulate_invalid_guest_state) {
4713 /*
4714 * CS and SS RPL should be equal during guest entry according
4715 * to VMX spec, but in reality it is not always so. Since vcpu
4716 * is in the middle of the transition from real mode to
4717 * protected mode it is safe to assume that RPL 0 is a good
4718 * default value.
4719 */
4720 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004721 save->selector &= ~SEGMENT_RPL_MASK;
4722 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004723 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004725 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726}
4727
4728static void enter_pmode(struct kvm_vcpu *vcpu)
4729{
4730 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004731 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732
Gleb Natapovd99e4152012-12-20 16:57:45 +02004733 /*
4734 * Update real mode segment cache. It may be not up-to-date if sement
4735 * register was written while vcpu was in a guest mode.
4736 */
4737 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4738 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4739 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4740 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4741 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4742 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4743
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004744 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745
Avi Kivity2fb92db2011-04-27 19:42:18 +03004746 vmx_segment_cache_clear(vmx);
4747
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004748 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004749
4750 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004751 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4752 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004753 vmcs_writel(GUEST_RFLAGS, flags);
4754
Rusty Russell66aee912007-07-17 23:34:16 +10004755 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4756 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757
4758 update_exception_bitmap(vcpu);
4759
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004760 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4761 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4762 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4763 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4764 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4765 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004766}
4767
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004768static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769{
Mathias Krause772e0312012-08-30 01:30:19 +02004770 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004771 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772
Gleb Natapovd99e4152012-12-20 16:57:45 +02004773 var.dpl = 0x3;
4774 if (seg == VCPU_SREG_CS)
4775 var.type = 0x3;
4776
4777 if (!emulate_invalid_guest_state) {
4778 var.selector = var.base >> 4;
4779 var.base = var.base & 0xffff0;
4780 var.limit = 0xffff;
4781 var.g = 0;
4782 var.db = 0;
4783 var.present = 1;
4784 var.s = 1;
4785 var.l = 0;
4786 var.unusable = 0;
4787 var.type = 0x3;
4788 var.avl = 0;
4789 if (save->base & 0xf)
4790 printk_once(KERN_WARNING "kvm: segment base is not "
4791 "paragraph aligned when entering "
4792 "protected mode (seg=%d)", seg);
4793 }
4794
4795 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004796 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004797 vmcs_write32(sf->limit, var.limit);
4798 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799}
4800
4801static void enter_rmode(struct kvm_vcpu *vcpu)
4802{
4803 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004804 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004805 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004807 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4808 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4809 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4810 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4811 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004812 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4813 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004814
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004815 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004816
Gleb Natapov776e58e2011-03-13 12:34:27 +02004817 /*
4818 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004819 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004820 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004821 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004822 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4823 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004824
Avi Kivity2fb92db2011-04-27 19:42:18 +03004825 vmx_segment_cache_clear(vmx);
4826
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004827 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004829 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4830
4831 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004832 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004834 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004835
4836 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004837 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 update_exception_bitmap(vcpu);
4839
Gleb Natapovd99e4152012-12-20 16:57:45 +02004840 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4841 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4842 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4843 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4844 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4845 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004846
Eddie Dong8668a3c2007-10-10 14:26:45 +08004847 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004848}
4849
Amit Shah401d10d2009-02-20 22:53:37 +05304850static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4851{
4852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004853 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4854
4855 if (!msr)
4856 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304857
Avi Kivity44ea2b12009-09-06 15:55:37 +03004858 /*
4859 * Force kernel_gs_base reloading before EFER changes, as control
4860 * of this msr depends on is_long_mode().
4861 */
4862 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004863 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304864 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004865 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304866 msr->data = efer;
4867 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004868 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304869
4870 msr->data = efer & ~EFER_LME;
4871 }
4872 setup_msrs(vmx);
4873}
4874
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004875#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004876
4877static void enter_lmode(struct kvm_vcpu *vcpu)
4878{
4879 u32 guest_tr_ar;
4880
Avi Kivity2fb92db2011-04-27 19:42:18 +03004881 vmx_segment_cache_clear(to_vmx(vcpu));
4882
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004884 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004885 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4886 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004888 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4889 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890 }
Avi Kivityda38f432010-07-06 11:30:49 +03004891 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892}
4893
4894static void exit_lmode(struct kvm_vcpu *vcpu)
4895{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004896 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004897 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898}
4899
4900#endif
4901
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004902static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4903 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004904{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004905 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004906 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4907 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004908 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004909 } else {
4910 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004911 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004912}
4913
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004914static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004915{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004916 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004917}
4918
Avi Kivitye8467fd2009-12-29 18:43:06 +02004919static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4920{
4921 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4922
4923 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4924 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4925}
4926
Avi Kivityaff48ba2010-12-05 18:56:11 +02004927static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4928{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004929 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004930 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4931 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4932}
4933
Anthony Liguori25c4c272007-04-27 09:29:21 +03004934static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004935{
Avi Kivityfc78f512009-12-07 12:16:48 +02004936 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4937
4938 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4939 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004940}
4941
Sheng Yang14394422008-04-28 12:24:45 +08004942static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4943{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004944 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4945
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004946 if (!test_bit(VCPU_EXREG_PDPTR,
4947 (unsigned long *)&vcpu->arch.regs_dirty))
4948 return;
4949
Sheng Yang14394422008-04-28 12:24:45 +08004950 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004951 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4952 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4953 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4954 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004955 }
4956}
4957
Avi Kivity8f5d5492009-05-31 18:41:29 +03004958static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4959{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004960 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4961
Avi Kivity8f5d5492009-05-31 18:41:29 +03004962 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004963 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4964 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4965 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4966 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004967 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004968
4969 __set_bit(VCPU_EXREG_PDPTR,
4970 (unsigned long *)&vcpu->arch.regs_avail);
4971 __set_bit(VCPU_EXREG_PDPTR,
4972 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004973}
4974
David Matlack38991522016-11-29 18:14:08 -08004975static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4976{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004977 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4978 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004979 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4980
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004981 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004982 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4983 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4984 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4985
4986 return fixed_bits_valid(val, fixed0, fixed1);
4987}
4988
4989static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4990{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004991 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4992 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004993
4994 return fixed_bits_valid(val, fixed0, fixed1);
4995}
4996
4997static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4998{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004999 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5000 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005001
5002 return fixed_bits_valid(val, fixed0, fixed1);
5003}
5004
5005/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5006#define nested_guest_cr4_valid nested_cr4_valid
5007#define nested_host_cr4_valid nested_cr4_valid
5008
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005009static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005010
5011static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5012 unsigned long cr0,
5013 struct kvm_vcpu *vcpu)
5014{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005015 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5016 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005017 if (!(cr0 & X86_CR0_PG)) {
5018 /* From paging/starting to nonpaging */
5019 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005020 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005021 (CPU_BASED_CR3_LOAD_EXITING |
5022 CPU_BASED_CR3_STORE_EXITING));
5023 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005024 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005025 } else if (!is_paging(vcpu)) {
5026 /* From nonpaging to paging */
5027 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005028 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005029 ~(CPU_BASED_CR3_LOAD_EXITING |
5030 CPU_BASED_CR3_STORE_EXITING));
5031 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005032 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005033 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005034
5035 if (!(cr0 & X86_CR0_WP))
5036 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005037}
5038
Avi Kivity6aa8b732006-12-10 02:21:36 -08005039static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5040{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005041 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005042 unsigned long hw_cr0;
5043
Gleb Natapov50378782013-02-04 16:00:28 +02005044 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005045 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005046 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005047 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005048 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005049
Gleb Natapov218e7632013-01-21 15:36:45 +02005050 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5051 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005052
Gleb Natapov218e7632013-01-21 15:36:45 +02005053 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5054 enter_rmode(vcpu);
5055 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005057#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005058 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005059 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005060 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005061 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005062 exit_lmode(vcpu);
5063 }
5064#endif
5065
Sean Christophersonb4d18512018-03-05 12:04:40 -08005066 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005067 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5068
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005070 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005071 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005072
5073 /* depends on vcpu->arch.cr0 to be set to a new value */
5074 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075}
5076
Yu Zhang855feb62017-08-24 20:27:55 +08005077static int get_ept_level(struct kvm_vcpu *vcpu)
5078{
5079 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5080 return 5;
5081 return 4;
5082}
5083
Peter Feiner995f00a2017-06-30 17:26:32 -07005084static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005085{
Yu Zhang855feb62017-08-24 20:27:55 +08005086 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005087
Yu Zhang855feb62017-08-24 20:27:55 +08005088 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005089
Peter Feiner995f00a2017-06-30 17:26:32 -07005090 if (enable_ept_ad_bits &&
5091 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005092 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005093 eptp |= (root_hpa & PAGE_MASK);
5094
5095 return eptp;
5096}
5097
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5099{
Sheng Yang14394422008-04-28 12:24:45 +08005100 unsigned long guest_cr3;
5101 u64 eptp;
5102
5103 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005104 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005105 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005106 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005107 if (enable_unrestricted_guest || is_paging(vcpu) ||
5108 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005109 guest_cr3 = kvm_read_cr3(vcpu);
5110 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005111 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005112 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005113 }
5114
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005115 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005116 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005117}
5118
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005119static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005120{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005121 /*
5122 * Pass through host's Machine Check Enable value to hw_cr4, which
5123 * is in force while we are in guest mode. Do not let guests control
5124 * this bit, even if host CR4.MCE == 0.
5125 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005126 unsigned long hw_cr4;
5127
5128 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5129 if (enable_unrestricted_guest)
5130 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5131 else if (to_vmx(vcpu)->rmode.vm86_active)
5132 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5133 else
5134 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005135
Sean Christopherson64f7a112018-04-30 10:01:06 -07005136 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5137 if (cr4 & X86_CR4_UMIP) {
5138 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005139 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005140 hw_cr4 &= ~X86_CR4_UMIP;
5141 } else if (!is_guest_mode(vcpu) ||
5142 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5143 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5144 SECONDARY_EXEC_DESC);
5145 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005146
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005147 if (cr4 & X86_CR4_VMXE) {
5148 /*
5149 * To use VMXON (and later other VMX instructions), a guest
5150 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5151 * So basically the check on whether to allow nested VMX
5152 * is here.
5153 */
5154 if (!nested_vmx_allowed(vcpu))
5155 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005156 }
David Matlack38991522016-11-29 18:14:08 -08005157
5158 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005159 return 1;
5160
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005161 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005162
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005163 if (!enable_unrestricted_guest) {
5164 if (enable_ept) {
5165 if (!is_paging(vcpu)) {
5166 hw_cr4 &= ~X86_CR4_PAE;
5167 hw_cr4 |= X86_CR4_PSE;
5168 } else if (!(cr4 & X86_CR4_PAE)) {
5169 hw_cr4 &= ~X86_CR4_PAE;
5170 }
5171 }
5172
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005173 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005174 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5175 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5176 * to be manually disabled when guest switches to non-paging
5177 * mode.
5178 *
5179 * If !enable_unrestricted_guest, the CPU is always running
5180 * with CR0.PG=1 and CR4 needs to be modified.
5181 * If enable_unrestricted_guest, the CPU automatically
5182 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005183 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005184 if (!is_paging(vcpu))
5185 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5186 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005187
Sheng Yang14394422008-04-28 12:24:45 +08005188 vmcs_writel(CR4_READ_SHADOW, cr4);
5189 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005190 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005191}
5192
Avi Kivity6aa8b732006-12-10 02:21:36 -08005193static void vmx_get_segment(struct kvm_vcpu *vcpu,
5194 struct kvm_segment *var, int seg)
5195{
Avi Kivitya9179492011-01-03 14:28:52 +02005196 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005197 u32 ar;
5198
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005199 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005200 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005201 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005202 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005203 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005204 var->base = vmx_read_guest_seg_base(vmx, seg);
5205 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5206 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005207 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005208 var->base = vmx_read_guest_seg_base(vmx, seg);
5209 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5210 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5211 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005212 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005213 var->type = ar & 15;
5214 var->s = (ar >> 4) & 1;
5215 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005216 /*
5217 * Some userspaces do not preserve unusable property. Since usable
5218 * segment has to be present according to VMX spec we can use present
5219 * property to amend userspace bug by making unusable segment always
5220 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5221 * segment as unusable.
5222 */
5223 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005224 var->avl = (ar >> 12) & 1;
5225 var->l = (ar >> 13) & 1;
5226 var->db = (ar >> 14) & 1;
5227 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228}
5229
Avi Kivitya9179492011-01-03 14:28:52 +02005230static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5231{
Avi Kivitya9179492011-01-03 14:28:52 +02005232 struct kvm_segment s;
5233
5234 if (to_vmx(vcpu)->rmode.vm86_active) {
5235 vmx_get_segment(vcpu, &s, seg);
5236 return s.base;
5237 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005238 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005239}
5240
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005241static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005242{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005243 struct vcpu_vmx *vmx = to_vmx(vcpu);
5244
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005245 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005246 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005247 else {
5248 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005249 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005250 }
Avi Kivity69c73022011-03-07 15:26:44 +02005251}
5252
Avi Kivity653e3102007-05-07 10:55:37 +03005253static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005254{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005255 u32 ar;
5256
Avi Kivityf0495f92012-06-07 17:06:10 +03005257 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005258 ar = 1 << 16;
5259 else {
5260 ar = var->type & 15;
5261 ar |= (var->s & 1) << 4;
5262 ar |= (var->dpl & 3) << 5;
5263 ar |= (var->present & 1) << 7;
5264 ar |= (var->avl & 1) << 12;
5265 ar |= (var->l & 1) << 13;
5266 ar |= (var->db & 1) << 14;
5267 ar |= (var->g & 1) << 15;
5268 }
Avi Kivity653e3102007-05-07 10:55:37 +03005269
5270 return ar;
5271}
5272
5273static void vmx_set_segment(struct kvm_vcpu *vcpu,
5274 struct kvm_segment *var, int seg)
5275{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005276 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005277 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005278
Avi Kivity2fb92db2011-04-27 19:42:18 +03005279 vmx_segment_cache_clear(vmx);
5280
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005281 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5282 vmx->rmode.segs[seg] = *var;
5283 if (seg == VCPU_SREG_TR)
5284 vmcs_write16(sf->selector, var->selector);
5285 else if (var->s)
5286 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005287 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005288 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005289
Avi Kivity653e3102007-05-07 10:55:37 +03005290 vmcs_writel(sf->base, var->base);
5291 vmcs_write32(sf->limit, var->limit);
5292 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005293
5294 /*
5295 * Fix the "Accessed" bit in AR field of segment registers for older
5296 * qemu binaries.
5297 * IA32 arch specifies that at the time of processor reset the
5298 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005299 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005300 * state vmexit when "unrestricted guest" mode is turned on.
5301 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5302 * tree. Newer qemu binaries with that qemu fix would not need this
5303 * kvm hack.
5304 */
5305 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005306 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005307
Gleb Natapovf924d662012-12-12 19:10:55 +02005308 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005309
5310out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005311 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005312}
5313
Avi Kivity6aa8b732006-12-10 02:21:36 -08005314static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5315{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005316 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005317
5318 *db = (ar >> 14) & 1;
5319 *l = (ar >> 13) & 1;
5320}
5321
Gleb Natapov89a27f42010-02-16 10:51:48 +02005322static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005323{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005324 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5325 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005326}
5327
Gleb Natapov89a27f42010-02-16 10:51:48 +02005328static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005329{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005330 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5331 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005332}
5333
Gleb Natapov89a27f42010-02-16 10:51:48 +02005334static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005335{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005336 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5337 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005338}
5339
Gleb Natapov89a27f42010-02-16 10:51:48 +02005340static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005341{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005342 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5343 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005344}
5345
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005346static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5347{
5348 struct kvm_segment var;
5349 u32 ar;
5350
5351 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005352 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005353 if (seg == VCPU_SREG_CS)
5354 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005355 ar = vmx_segment_access_rights(&var);
5356
5357 if (var.base != (var.selector << 4))
5358 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005359 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005361 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005362 return false;
5363
5364 return true;
5365}
5366
5367static bool code_segment_valid(struct kvm_vcpu *vcpu)
5368{
5369 struct kvm_segment cs;
5370 unsigned int cs_rpl;
5371
5372 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005373 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005374
Avi Kivity1872a3f2009-01-04 23:26:52 +02005375 if (cs.unusable)
5376 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005377 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005378 return false;
5379 if (!cs.s)
5380 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005381 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005382 if (cs.dpl > cs_rpl)
5383 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005384 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005385 if (cs.dpl != cs_rpl)
5386 return false;
5387 }
5388 if (!cs.present)
5389 return false;
5390
5391 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5392 return true;
5393}
5394
5395static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5396{
5397 struct kvm_segment ss;
5398 unsigned int ss_rpl;
5399
5400 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005401 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005402
Avi Kivity1872a3f2009-01-04 23:26:52 +02005403 if (ss.unusable)
5404 return true;
5405 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005406 return false;
5407 if (!ss.s)
5408 return false;
5409 if (ss.dpl != ss_rpl) /* DPL != RPL */
5410 return false;
5411 if (!ss.present)
5412 return false;
5413
5414 return true;
5415}
5416
5417static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5418{
5419 struct kvm_segment var;
5420 unsigned int rpl;
5421
5422 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005423 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005424
Avi Kivity1872a3f2009-01-04 23:26:52 +02005425 if (var.unusable)
5426 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005427 if (!var.s)
5428 return false;
5429 if (!var.present)
5430 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005431 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005432 if (var.dpl < rpl) /* DPL < RPL */
5433 return false;
5434 }
5435
5436 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5437 * rights flags
5438 */
5439 return true;
5440}
5441
5442static bool tr_valid(struct kvm_vcpu *vcpu)
5443{
5444 struct kvm_segment tr;
5445
5446 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5447
Avi Kivity1872a3f2009-01-04 23:26:52 +02005448 if (tr.unusable)
5449 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005450 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005451 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005452 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005453 return false;
5454 if (!tr.present)
5455 return false;
5456
5457 return true;
5458}
5459
5460static bool ldtr_valid(struct kvm_vcpu *vcpu)
5461{
5462 struct kvm_segment ldtr;
5463
5464 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5465
Avi Kivity1872a3f2009-01-04 23:26:52 +02005466 if (ldtr.unusable)
5467 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005468 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005469 return false;
5470 if (ldtr.type != 2)
5471 return false;
5472 if (!ldtr.present)
5473 return false;
5474
5475 return true;
5476}
5477
5478static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5479{
5480 struct kvm_segment cs, ss;
5481
5482 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5483 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5484
Nadav Amitb32a9912015-03-29 16:33:04 +03005485 return ((cs.selector & SEGMENT_RPL_MASK) ==
5486 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005487}
5488
5489/*
5490 * Check if guest state is valid. Returns true if valid, false if
5491 * not.
5492 * We assume that registers are always usable
5493 */
5494static bool guest_state_valid(struct kvm_vcpu *vcpu)
5495{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005496 if (enable_unrestricted_guest)
5497 return true;
5498
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005499 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005500 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005501 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5502 return false;
5503 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5504 return false;
5505 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5506 return false;
5507 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5508 return false;
5509 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5510 return false;
5511 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5512 return false;
5513 } else {
5514 /* protected mode guest state checks */
5515 if (!cs_ss_rpl_check(vcpu))
5516 return false;
5517 if (!code_segment_valid(vcpu))
5518 return false;
5519 if (!stack_segment_valid(vcpu))
5520 return false;
5521 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5522 return false;
5523 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5524 return false;
5525 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5526 return false;
5527 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5528 return false;
5529 if (!tr_valid(vcpu))
5530 return false;
5531 if (!ldtr_valid(vcpu))
5532 return false;
5533 }
5534 /* TODO:
5535 * - Add checks on RIP
5536 * - Add checks on RFLAGS
5537 */
5538
5539 return true;
5540}
5541
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005542static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5543{
5544 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5545}
5546
Mike Dayd77c26f2007-10-08 09:02:08 -04005547static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005548{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005549 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005550 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005551 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005553 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005554 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005555 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5556 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005557 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005558 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005559 r = kvm_write_guest_page(kvm, fn++, &data,
5560 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005561 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005562 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005563 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5564 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005565 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005566 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5567 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005568 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005569 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005570 r = kvm_write_guest_page(kvm, fn, &data,
5571 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5572 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005573out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005574 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005575 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005576}
5577
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005578static int init_rmode_identity_map(struct kvm *kvm)
5579{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005580 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005581 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005582 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005583 u32 tmp;
5584
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005585 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005586 mutex_lock(&kvm->slots_lock);
5587
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005588 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005589 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005590
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005591 if (!kvm_vmx->ept_identity_map_addr)
5592 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5593 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005594
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005595 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005596 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005597 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005598 goto out2;
5599
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005600 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005601 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5602 if (r < 0)
5603 goto out;
5604 /* Set up identity-mapping pagetable for EPT in real mode */
5605 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5606 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5607 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5608 r = kvm_write_guest_page(kvm, identity_map_pfn,
5609 &tmp, i * sizeof(tmp), sizeof(tmp));
5610 if (r < 0)
5611 goto out;
5612 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005613 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005614
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005615out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005616 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005617
5618out2:
5619 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005620 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005621}
5622
Avi Kivity6aa8b732006-12-10 02:21:36 -08005623static void seg_setup(int seg)
5624{
Mathias Krause772e0312012-08-30 01:30:19 +02005625 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005626 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005627
5628 vmcs_write16(sf->selector, 0);
5629 vmcs_writel(sf->base, 0);
5630 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005631 ar = 0x93;
5632 if (seg == VCPU_SREG_CS)
5633 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005634
5635 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005636}
5637
Sheng Yangf78e0e22007-10-29 09:40:42 +08005638static int alloc_apic_access_page(struct kvm *kvm)
5639{
Xiao Guangrong44841412012-09-07 14:14:20 +08005640 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005641 int r = 0;
5642
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005643 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005644 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005645 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005646 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5647 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005648 if (r)
5649 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005650
Tang Chen73a6d942014-09-11 13:38:00 +08005651 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005652 if (is_error_page(page)) {
5653 r = -EFAULT;
5654 goto out;
5655 }
5656
Tang Chenc24ae0d2014-09-24 15:57:58 +08005657 /*
5658 * Do not pin the page in memory, so that memory hot-unplug
5659 * is able to migrate it.
5660 */
5661 put_page(page);
5662 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005663out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005664 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005665 return r;
5666}
5667
Wanpeng Li991e7a02015-09-16 17:30:05 +08005668static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005669{
5670 int vpid;
5671
Avi Kivity919818a2009-03-23 18:01:29 +02005672 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005673 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005674 spin_lock(&vmx_vpid_lock);
5675 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005676 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005677 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005678 else
5679 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005680 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005681 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005682}
5683
Wanpeng Li991e7a02015-09-16 17:30:05 +08005684static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005685{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005686 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005687 return;
5688 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005689 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005690 spin_unlock(&vmx_vpid_lock);
5691}
5692
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005693static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5694 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005695{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005696 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005697
5698 if (!cpu_has_vmx_msr_bitmap())
5699 return;
5700
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005701 if (static_branch_unlikely(&enable_evmcs))
5702 evmcs_touch_msr_bitmap();
5703
Sheng Yang25c5f222008-03-28 13:18:56 +08005704 /*
5705 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5706 * have the write-low and read-high bitmap offsets the wrong way round.
5707 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5708 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005709 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005710 if (type & MSR_TYPE_R)
5711 /* read-low */
5712 __clear_bit(msr, msr_bitmap + 0x000 / f);
5713
5714 if (type & MSR_TYPE_W)
5715 /* write-low */
5716 __clear_bit(msr, msr_bitmap + 0x800 / f);
5717
Sheng Yang25c5f222008-03-28 13:18:56 +08005718 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5719 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005720 if (type & MSR_TYPE_R)
5721 /* read-high */
5722 __clear_bit(msr, msr_bitmap + 0x400 / f);
5723
5724 if (type & MSR_TYPE_W)
5725 /* write-high */
5726 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5727
5728 }
5729}
5730
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005731static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5732 u32 msr, int type)
5733{
5734 int f = sizeof(unsigned long);
5735
5736 if (!cpu_has_vmx_msr_bitmap())
5737 return;
5738
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005739 if (static_branch_unlikely(&enable_evmcs))
5740 evmcs_touch_msr_bitmap();
5741
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005742 /*
5743 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5744 * have the write-low and read-high bitmap offsets the wrong way round.
5745 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5746 */
5747 if (msr <= 0x1fff) {
5748 if (type & MSR_TYPE_R)
5749 /* read-low */
5750 __set_bit(msr, msr_bitmap + 0x000 / f);
5751
5752 if (type & MSR_TYPE_W)
5753 /* write-low */
5754 __set_bit(msr, msr_bitmap + 0x800 / f);
5755
5756 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5757 msr &= 0x1fff;
5758 if (type & MSR_TYPE_R)
5759 /* read-high */
5760 __set_bit(msr, msr_bitmap + 0x400 / f);
5761
5762 if (type & MSR_TYPE_W)
5763 /* write-high */
5764 __set_bit(msr, msr_bitmap + 0xc00 / f);
5765
5766 }
5767}
5768
5769static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5770 u32 msr, int type, bool value)
5771{
5772 if (value)
5773 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5774 else
5775 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5776}
5777
Wincy Vanf2b93282015-02-03 23:56:03 +08005778/*
5779 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5780 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5781 */
5782static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5783 unsigned long *msr_bitmap_nested,
5784 u32 msr, int type)
5785{
5786 int f = sizeof(unsigned long);
5787
Wincy Vanf2b93282015-02-03 23:56:03 +08005788 /*
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 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5796 /* read-low */
5797 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5798
5799 if (type & MSR_TYPE_W &&
5800 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5801 /* write-low */
5802 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5803
5804 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5805 msr &= 0x1fff;
5806 if (type & MSR_TYPE_R &&
5807 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5808 /* read-high */
5809 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5810
5811 if (type & MSR_TYPE_W &&
5812 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5813 /* write-high */
5814 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5815
5816 }
5817}
5818
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005819static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005820{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005821 u8 mode = 0;
5822
5823 if (cpu_has_secondary_exec_ctrls() &&
5824 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5825 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5826 mode |= MSR_BITMAP_MODE_X2APIC;
5827 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5828 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5829 }
5830
5831 if (is_long_mode(vcpu))
5832 mode |= MSR_BITMAP_MODE_LM;
5833
5834 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005835}
5836
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005837#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5838
5839static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5840 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005841{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005842 int msr;
5843
5844 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5845 unsigned word = msr / BITS_PER_LONG;
5846 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5847 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005848 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005849
5850 if (mode & MSR_BITMAP_MODE_X2APIC) {
5851 /*
5852 * TPR reads and writes can be virtualized even if virtual interrupt
5853 * delivery is not in use.
5854 */
5855 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5856 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5857 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5858 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5859 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5860 }
5861 }
5862}
5863
5864static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5865{
5866 struct vcpu_vmx *vmx = to_vmx(vcpu);
5867 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5868 u8 mode = vmx_msr_bitmap_mode(vcpu);
5869 u8 changed = mode ^ vmx->msr_bitmap_mode;
5870
5871 if (!changed)
5872 return;
5873
5874 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5875 !(mode & MSR_BITMAP_MODE_LM));
5876
5877 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5878 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5879
5880 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005881}
5882
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005883static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005884{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005885 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005886}
5887
David Matlackc9f04402017-08-01 14:00:40 -07005888static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5889{
5890 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5891 gfn_t gfn;
5892
5893 /*
5894 * Don't need to mark the APIC access page dirty; it is never
5895 * written to by the CPU during APIC virtualization.
5896 */
5897
5898 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5899 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5900 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5901 }
5902
5903 if (nested_cpu_has_posted_intr(vmcs12)) {
5904 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5905 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5906 }
5907}
5908
5909
David Hildenbrand6342c502017-01-25 11:58:58 +01005910static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005911{
5912 struct vcpu_vmx *vmx = to_vmx(vcpu);
5913 int max_irr;
5914 void *vapic_page;
5915 u16 status;
5916
David Matlackc9f04402017-08-01 14:00:40 -07005917 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5918 return;
Wincy Van705699a2015-02-03 23:58:17 +08005919
David Matlackc9f04402017-08-01 14:00:40 -07005920 vmx->nested.pi_pending = false;
5921 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5922 return;
Wincy Van705699a2015-02-03 23:58:17 +08005923
David Matlackc9f04402017-08-01 14:00:40 -07005924 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5925 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005926 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005927 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5928 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005929 kunmap(vmx->nested.virtual_apic_page);
5930
5931 status = vmcs_read16(GUEST_INTR_STATUS);
5932 if ((u8)max_irr > ((u8)status & 0xff)) {
5933 status &= ~0xff;
5934 status |= (u8)max_irr;
5935 vmcs_write16(GUEST_INTR_STATUS, status);
5936 }
5937 }
David Matlackc9f04402017-08-01 14:00:40 -07005938
5939 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005940}
5941
Wincy Van06a55242017-04-28 13:13:59 +08005942static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5943 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005944{
5945#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005946 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5947
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005948 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005949 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005950 * The vector of interrupt to be delivered to vcpu had
5951 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005952 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005953 * Following cases will be reached in this block, and
5954 * we always send a notification event in all cases as
5955 * explained below.
5956 *
5957 * Case 1: vcpu keeps in non-root mode. Sending a
5958 * notification event posts the interrupt to vcpu.
5959 *
5960 * Case 2: vcpu exits to root mode and is still
5961 * runnable. PIR will be synced to vIRR before the
5962 * next vcpu entry. Sending a notification event in
5963 * this case has no effect, as vcpu is not in root
5964 * mode.
5965 *
5966 * Case 3: vcpu exits to root mode and is blocked.
5967 * vcpu_block() has already synced PIR to vIRR and
5968 * never blocks vcpu if vIRR is not cleared. Therefore,
5969 * a blocked vcpu here does not wait for any requested
5970 * interrupts in PIR, and sending a notification event
5971 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005972 */
Feng Wu28b835d2015-09-18 22:29:54 +08005973
Wincy Van06a55242017-04-28 13:13:59 +08005974 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005975 return true;
5976 }
5977#endif
5978 return false;
5979}
5980
Wincy Van705699a2015-02-03 23:58:17 +08005981static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5982 int vector)
5983{
5984 struct vcpu_vmx *vmx = to_vmx(vcpu);
5985
5986 if (is_guest_mode(vcpu) &&
5987 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005988 /*
5989 * If a posted intr is not recognized by hardware,
5990 * we will accomplish it in the next vmentry.
5991 */
5992 vmx->nested.pi_pending = true;
5993 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005994 /* the PIR and ON have been set by L1. */
5995 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5996 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005997 return 0;
5998 }
5999 return -1;
6000}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006001/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006002 * Send interrupt to vcpu via posted interrupt way.
6003 * 1. If target vcpu is running(non-root mode), send posted interrupt
6004 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6005 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6006 * interrupt from PIR in next vmentry.
6007 */
6008static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6009{
6010 struct vcpu_vmx *vmx = to_vmx(vcpu);
6011 int r;
6012
Wincy Van705699a2015-02-03 23:58:17 +08006013 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6014 if (!r)
6015 return;
6016
Yang Zhanga20ed542013-04-11 19:25:15 +08006017 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6018 return;
6019
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006020 /* If a previous notification has sent the IPI, nothing to do. */
6021 if (pi_test_and_set_on(&vmx->pi_desc))
6022 return;
6023
Wincy Van06a55242017-04-28 13:13:59 +08006024 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006025 kvm_vcpu_kick(vcpu);
6026}
6027
Avi Kivity6aa8b732006-12-10 02:21:36 -08006028/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006029 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6030 * will not change in the lifetime of the guest.
6031 * Note that host-state that does change is set elsewhere. E.g., host-state
6032 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6033 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006034static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006035{
6036 u32 low32, high32;
6037 unsigned long tmpl;
6038 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006039 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006040
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006041 cr0 = read_cr0();
6042 WARN_ON(cr0 & X86_CR0_TS);
6043 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006044
6045 /*
6046 * Save the most likely value for this task's CR3 in the VMCS.
6047 * We can't use __get_current_cr3_fast() because we're not atomic.
6048 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006049 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006050 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006051 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006052
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006053 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006054 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006055 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006056 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006057
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006058 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006059#ifdef CONFIG_X86_64
6060 /*
6061 * Load null selectors, so we can avoid reloading them in
6062 * __vmx_load_host_state(), in case userspace uses the null selectors
6063 * too (the expected case).
6064 */
6065 vmcs_write16(HOST_DS_SELECTOR, 0);
6066 vmcs_write16(HOST_ES_SELECTOR, 0);
6067#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006068 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6069 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006070#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006071 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6072 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6073
Juergen Gross87930012017-09-04 12:25:27 +02006074 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006075 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006076 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006077
Avi Kivity83287ea422012-09-16 15:10:57 +03006078 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006079
6080 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6081 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6082 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6083 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6084
6085 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6086 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6087 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6088 }
6089}
6090
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006091static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6092{
6093 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6094 if (enable_ept)
6095 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006096 if (is_guest_mode(&vmx->vcpu))
6097 vmx->vcpu.arch.cr4_guest_owned_bits &=
6098 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006099 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6100}
6101
Yang Zhang01e439b2013-04-11 19:25:12 +08006102static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6103{
6104 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6105
Andrey Smetanind62caab2015-11-10 15:36:33 +03006106 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006107 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006108
6109 if (!enable_vnmi)
6110 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6111
Yunhong Jiang64672c92016-06-13 14:19:59 -07006112 /* Enable the preemption timer dynamically */
6113 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006114 return pin_based_exec_ctrl;
6115}
6116
Andrey Smetanind62caab2015-11-10 15:36:33 +03006117static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6118{
6119 struct vcpu_vmx *vmx = to_vmx(vcpu);
6120
6121 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006122 if (cpu_has_secondary_exec_ctrls()) {
6123 if (kvm_vcpu_apicv_active(vcpu))
6124 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6125 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6126 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6127 else
6128 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6129 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6130 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6131 }
6132
6133 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006134 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006135}
6136
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006137static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6138{
6139 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006140
6141 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6142 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6143
Paolo Bonzini35754c92015-07-29 12:05:37 +02006144 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006145 exec_control &= ~CPU_BASED_TPR_SHADOW;
6146#ifdef CONFIG_X86_64
6147 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6148 CPU_BASED_CR8_LOAD_EXITING;
6149#endif
6150 }
6151 if (!enable_ept)
6152 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6153 CPU_BASED_CR3_LOAD_EXITING |
6154 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006155 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6156 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6157 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006158 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6159 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006160 return exec_control;
6161}
6162
Jim Mattson45ec3682017-08-23 16:32:04 -07006163static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006164{
Jim Mattson45ec3682017-08-23 16:32:04 -07006165 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006166 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006167}
6168
Jim Mattson75f4fc82017-08-23 16:32:03 -07006169static bool vmx_rdseed_supported(void)
6170{
6171 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006172 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006173}
6174
Paolo Bonzini80154d72017-08-24 13:55:35 +02006175static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006176{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006177 struct kvm_vcpu *vcpu = &vmx->vcpu;
6178
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006179 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006180
Paolo Bonzini80154d72017-08-24 13:55:35 +02006181 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006182 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6183 if (vmx->vpid == 0)
6184 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6185 if (!enable_ept) {
6186 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6187 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006188 /* Enable INVPCID for non-ept guests may cause performance regression. */
6189 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006190 }
6191 if (!enable_unrestricted_guest)
6192 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006193 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006194 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006195 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006196 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6197 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006198 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006199
6200 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6201 * in vmx_set_cr4. */
6202 exec_control &= ~SECONDARY_EXEC_DESC;
6203
Abel Gordonabc4fc52013-04-18 14:35:25 +03006204 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6205 (handle_vmptrld).
6206 We can NOT enable shadow_vmcs here because we don't have yet
6207 a current VMCS12
6208 */
6209 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006210
6211 if (!enable_pml)
6212 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006213
Paolo Bonzini3db13482017-08-24 14:48:03 +02006214 if (vmx_xsaves_supported()) {
6215 /* Exposing XSAVES only when XSAVE is exposed */
6216 bool xsaves_enabled =
6217 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6218 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6219
6220 if (!xsaves_enabled)
6221 exec_control &= ~SECONDARY_EXEC_XSAVES;
6222
6223 if (nested) {
6224 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006225 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006226 SECONDARY_EXEC_XSAVES;
6227 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006228 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006229 ~SECONDARY_EXEC_XSAVES;
6230 }
6231 }
6232
Paolo Bonzini80154d72017-08-24 13:55:35 +02006233 if (vmx_rdtscp_supported()) {
6234 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6235 if (!rdtscp_enabled)
6236 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6237
6238 if (nested) {
6239 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006240 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006241 SECONDARY_EXEC_RDTSCP;
6242 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006243 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006244 ~SECONDARY_EXEC_RDTSCP;
6245 }
6246 }
6247
6248 if (vmx_invpcid_supported()) {
6249 /* Exposing INVPCID only when PCID is exposed */
6250 bool invpcid_enabled =
6251 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6252 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6253
6254 if (!invpcid_enabled) {
6255 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6256 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6257 }
6258
6259 if (nested) {
6260 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006261 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006262 SECONDARY_EXEC_ENABLE_INVPCID;
6263 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006264 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006265 ~SECONDARY_EXEC_ENABLE_INVPCID;
6266 }
6267 }
6268
Jim Mattson45ec3682017-08-23 16:32:04 -07006269 if (vmx_rdrand_supported()) {
6270 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6271 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006272 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006273
6274 if (nested) {
6275 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006276 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006277 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006278 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006279 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006280 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006281 }
6282 }
6283
Jim Mattson75f4fc82017-08-23 16:32:03 -07006284 if (vmx_rdseed_supported()) {
6285 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6286 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006287 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006288
6289 if (nested) {
6290 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006291 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006292 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006293 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006294 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006295 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006296 }
6297 }
6298
Paolo Bonzini80154d72017-08-24 13:55:35 +02006299 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006300}
6301
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006302static void ept_set_mmio_spte_mask(void)
6303{
6304 /*
6305 * EPT Misconfigurations can be generated if the value of bits 2:0
6306 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006307 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006308 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6309 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006310}
6311
Wanpeng Lif53cd632014-12-02 19:14:58 +08006312#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006313/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314 * Sets up the vmcs for emulated real mode.
6315 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006316static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006318#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006320#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006321 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322
Abel Gordon4607c2d2013-04-18 14:35:55 +03006323 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006324 /*
6325 * At vCPU creation, "VMWRITE to any supported field
6326 * in the VMCS" is supported, so use the more
6327 * permissive vmx_vmread_bitmap to specify both read
6328 * and write permissions for the shadow VMCS.
6329 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006330 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006331 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006332 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006333 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006334 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006335
Avi Kivity6aa8b732006-12-10 02:21:36 -08006336 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6337
Avi Kivity6aa8b732006-12-10 02:21:36 -08006338 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006339 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006340 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006341
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006342 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006343
Dan Williamsdfa169b2016-06-02 11:17:24 -07006344 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006345 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006346 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006347 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006348 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006349
Andrey Smetanind62caab2015-11-10 15:36:33 +03006350 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006351 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6352 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6353 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6354 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6355
6356 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006357
Li RongQing0bcf2612015-12-03 13:29:34 +08006358 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006359 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006360 }
6361
Wanpeng Lib31c1142018-03-12 04:53:04 -07006362 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006363 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006364 vmx->ple_window = ple_window;
6365 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006366 }
6367
Xiao Guangrongc3707952011-07-12 03:28:04 +08006368 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6369 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006370 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6371
Avi Kivity9581d442010-10-19 16:46:55 +02006372 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6373 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006374 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006375#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006376 rdmsrl(MSR_FS_BASE, a);
6377 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6378 rdmsrl(MSR_GS_BASE, a);
6379 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6380#else
6381 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6382 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6383#endif
6384
Bandan Das2a499e42017-08-03 15:54:41 -04006385 if (cpu_has_vmx_vmfunc())
6386 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6387
Eddie Dong2cc51562007-05-21 07:28:09 +03006388 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6389 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006390 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006391 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006392 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006393
Radim Krčmář74545702015-04-27 15:11:25 +02006394 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6395 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006396
Paolo Bonzini03916db2014-07-24 14:21:57 +02006397 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006398 u32 index = vmx_msr_index[i];
6399 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006400 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006401
6402 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6403 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006404 if (wrmsr_safe(index, data_low, data_high) < 0)
6405 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006406 vmx->guest_msrs[j].index = i;
6407 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006408 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006409 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006410 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006411
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006412 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6413 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006414
6415 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416
6417 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006418 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006419
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006420 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6421 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6422
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006424
Wanpeng Lif53cd632014-12-02 19:14:58 +08006425 if (vmx_xsaves_supported())
6426 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6427
Peter Feiner4e595162016-07-07 14:49:58 -07006428 if (enable_pml) {
6429 ASSERT(vmx->pml_pg);
6430 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6431 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6432 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006433}
6434
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006435static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006436{
6437 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006438 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006439 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006440
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006441 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006442 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006443
Wanpeng Li518e7b92018-02-28 14:03:31 +08006444 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006445 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006446 kvm_set_cr8(vcpu, 0);
6447
6448 if (!init_event) {
6449 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6450 MSR_IA32_APICBASE_ENABLE;
6451 if (kvm_vcpu_is_reset_bsp(vcpu))
6452 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6453 apic_base_msr.host_initiated = true;
6454 kvm_set_apic_base(vcpu, &apic_base_msr);
6455 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006456
Avi Kivity2fb92db2011-04-27 19:42:18 +03006457 vmx_segment_cache_clear(vmx);
6458
Avi Kivity5706be02008-08-20 15:07:31 +03006459 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006460 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006461 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006462
6463 seg_setup(VCPU_SREG_DS);
6464 seg_setup(VCPU_SREG_ES);
6465 seg_setup(VCPU_SREG_FS);
6466 seg_setup(VCPU_SREG_GS);
6467 seg_setup(VCPU_SREG_SS);
6468
6469 vmcs_write16(GUEST_TR_SELECTOR, 0);
6470 vmcs_writel(GUEST_TR_BASE, 0);
6471 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6472 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6473
6474 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6475 vmcs_writel(GUEST_LDTR_BASE, 0);
6476 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6477 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6478
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006479 if (!init_event) {
6480 vmcs_write32(GUEST_SYSENTER_CS, 0);
6481 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6482 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6483 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6484 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006485
Wanpeng Lic37c2872017-11-20 14:52:21 -08006486 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006487 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006488
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006489 vmcs_writel(GUEST_GDTR_BASE, 0);
6490 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6491
6492 vmcs_writel(GUEST_IDTR_BASE, 0);
6493 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6494
Anthony Liguori443381a2010-12-06 10:53:38 -06006495 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006496 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006497 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006498 if (kvm_mpx_supported())
6499 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006500
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006501 setup_msrs(vmx);
6502
Avi Kivity6aa8b732006-12-10 02:21:36 -08006503 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6504
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006505 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006506 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006507 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006508 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006509 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006510 vmcs_write32(TPR_THRESHOLD, 0);
6511 }
6512
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006513 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006514
Sheng Yang2384d2b2008-01-17 15:14:33 +08006515 if (vmx->vpid != 0)
6516 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6517
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006518 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006519 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006520 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006521 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006522 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006523
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006524 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006525
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006526 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006527 if (init_event)
6528 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006529}
6530
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006531/*
6532 * In nested virtualization, check if L1 asked to exit on external interrupts.
6533 * For most existing hypervisors, this will always return true.
6534 */
6535static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6536{
6537 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6538 PIN_BASED_EXT_INTR_MASK;
6539}
6540
Bandan Das77b0f5d2014-04-19 18:17:45 -04006541/*
6542 * In nested virtualization, check if L1 has set
6543 * VM_EXIT_ACK_INTR_ON_EXIT
6544 */
6545static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6546{
6547 return get_vmcs12(vcpu)->vm_exit_controls &
6548 VM_EXIT_ACK_INTR_ON_EXIT;
6549}
6550
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006551static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6552{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006553 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006554}
6555
Jan Kiszkac9a79532014-03-07 20:03:15 +01006556static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006557{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006558 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6559 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006560}
6561
Jan Kiszkac9a79532014-03-07 20:03:15 +01006562static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006563{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006564 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006565 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006566 enable_irq_window(vcpu);
6567 return;
6568 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006569
Paolo Bonzini47c01522016-12-19 11:44:07 +01006570 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6571 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006572}
6573
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006574static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006575{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006576 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006577 uint32_t intr;
6578 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006579
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006580 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006581
Avi Kivityfa89a812008-09-01 15:57:51 +03006582 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006583 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006584 int inc_eip = 0;
6585 if (vcpu->arch.interrupt.soft)
6586 inc_eip = vcpu->arch.event_exit_inst_len;
6587 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006588 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006589 return;
6590 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006591 intr = irq | INTR_INFO_VALID_MASK;
6592 if (vcpu->arch.interrupt.soft) {
6593 intr |= INTR_TYPE_SOFT_INTR;
6594 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6595 vmx->vcpu.arch.event_exit_inst_len);
6596 } else
6597 intr |= INTR_TYPE_EXT_INTR;
6598 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006599
6600 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006601}
6602
Sheng Yangf08864b2008-05-15 18:23:25 +08006603static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6604{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006605 struct vcpu_vmx *vmx = to_vmx(vcpu);
6606
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006607 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006608 /*
6609 * Tracking the NMI-blocked state in software is built upon
6610 * finding the next open IRQ window. This, in turn, depends on
6611 * well-behaving guests: They have to keep IRQs disabled at
6612 * least as long as the NMI handler runs. Otherwise we may
6613 * cause NMI nesting, maybe breaking the guest. But as this is
6614 * highly unlikely, we can live with the residual risk.
6615 */
6616 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6617 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6618 }
6619
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006620 ++vcpu->stat.nmi_injections;
6621 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006622
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006623 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006624 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006625 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006626 return;
6627 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006628
Sheng Yangf08864b2008-05-15 18:23:25 +08006629 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6630 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006631
6632 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006633}
6634
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006635static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6636{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006637 struct vcpu_vmx *vmx = to_vmx(vcpu);
6638 bool masked;
6639
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006640 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006641 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006642 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006643 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006644 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6645 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6646 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006647}
6648
6649static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6650{
6651 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 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6655 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6656 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6657 }
6658 } else {
6659 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6660 if (masked)
6661 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6662 GUEST_INTR_STATE_NMI);
6663 else
6664 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6665 GUEST_INTR_STATE_NMI);
6666 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006667}
6668
Jan Kiszka2505dc92013-04-14 12:12:47 +02006669static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6670{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006671 if (to_vmx(vcpu)->nested.nested_run_pending)
6672 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006673
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006674 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006675 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6676 return 0;
6677
Jan Kiszka2505dc92013-04-14 12:12:47 +02006678 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6679 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6680 | GUEST_INTR_STATE_NMI));
6681}
6682
Gleb Natapov78646122009-03-23 12:12:11 +02006683static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6684{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006685 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6686 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006687 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6688 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006689}
6690
Izik Eiduscbc94022007-10-25 00:29:55 +02006691static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6692{
6693 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006694
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006695 if (enable_unrestricted_guest)
6696 return 0;
6697
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006698 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6699 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006700 if (ret)
6701 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006702 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006703 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006704}
6705
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006706static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6707{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006708 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006709 return 0;
6710}
6711
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006712static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006713{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006714 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006715 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006716 /*
6717 * Update instruction length as we may reinject the exception
6718 * from user space while in guest debugging mode.
6719 */
6720 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6721 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006722 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006723 return false;
6724 /* fall through */
6725 case DB_VECTOR:
6726 if (vcpu->guest_debug &
6727 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6728 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006729 /* fall through */
6730 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006731 case OF_VECTOR:
6732 case BR_VECTOR:
6733 case UD_VECTOR:
6734 case DF_VECTOR:
6735 case SS_VECTOR:
6736 case GP_VECTOR:
6737 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006738 return true;
6739 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006740 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006741 return false;
6742}
6743
6744static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6745 int vec, u32 err_code)
6746{
6747 /*
6748 * Instruction with address size override prefix opcode 0x67
6749 * Cause the #SS fault with 0 error code in VM86 mode.
6750 */
6751 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6752 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6753 if (vcpu->arch.halt_request) {
6754 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006755 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006756 }
6757 return 1;
6758 }
6759 return 0;
6760 }
6761
6762 /*
6763 * Forward all other exceptions that are valid in real mode.
6764 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6765 * the required debugging infrastructure rework.
6766 */
6767 kvm_queue_exception(vcpu, vec);
6768 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006769}
6770
Andi Kleena0861c02009-06-08 17:37:09 +08006771/*
6772 * Trigger machine check on the host. We assume all the MSRs are already set up
6773 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6774 * We pass a fake environment to the machine check handler because we want
6775 * the guest to be always treated like user space, no matter what context
6776 * it used internally.
6777 */
6778static void kvm_machine_check(void)
6779{
6780#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6781 struct pt_regs regs = {
6782 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6783 .flags = X86_EFLAGS_IF,
6784 };
6785
6786 do_machine_check(&regs, 0);
6787#endif
6788}
6789
Avi Kivity851ba692009-08-24 11:10:17 +03006790static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006791{
6792 /* already handled by vcpu_run */
6793 return 1;
6794}
6795
Avi Kivity851ba692009-08-24 11:10:17 +03006796static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006797{
Avi Kivity1155f762007-11-22 11:30:47 +02006798 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006799 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006800 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006801 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006802 u32 vect_info;
6803 enum emulation_result er;
6804
Avi Kivity1155f762007-11-22 11:30:47 +02006805 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006806 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006807
Andi Kleena0861c02009-06-08 17:37:09 +08006808 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006809 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006810
Jim Mattsonef85b672016-12-12 11:01:37 -08006811 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006812 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006813
Wanpeng Li082d06e2018-04-03 16:28:48 -07006814 if (is_invalid_opcode(intr_info))
6815 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006816
Avi Kivity6aa8b732006-12-10 02:21:36 -08006817 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006818 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006819 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006820
Liran Alon9e869482018-03-12 13:12:51 +02006821 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6822 WARN_ON_ONCE(!enable_vmware_backdoor);
6823 er = emulate_instruction(vcpu,
6824 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6825 if (er == EMULATE_USER_EXIT)
6826 return 0;
6827 else if (er != EMULATE_DONE)
6828 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6829 return 1;
6830 }
6831
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006832 /*
6833 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6834 * MMIO, it is better to report an internal error.
6835 * See the comments in vmx_handle_exit.
6836 */
6837 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6838 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6839 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6840 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006841 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006842 vcpu->run->internal.data[0] = vect_info;
6843 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006844 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006845 return 0;
6846 }
6847
Avi Kivity6aa8b732006-12-10 02:21:36 -08006848 if (is_page_fault(intr_info)) {
6849 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006850 /* EPT won't cause page fault directly */
6851 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006852 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006853 }
6854
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006855 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006856
6857 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6858 return handle_rmode_exception(vcpu, ex_no, error_code);
6859
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006860 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006861 case AC_VECTOR:
6862 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6863 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006864 case DB_VECTOR:
6865 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6866 if (!(vcpu->guest_debug &
6867 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006868 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006869 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006870 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006871 skip_emulated_instruction(vcpu);
6872
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006873 kvm_queue_exception(vcpu, DB_VECTOR);
6874 return 1;
6875 }
6876 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6877 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6878 /* fall through */
6879 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006880 /*
6881 * Update instruction length as we may reinject #BP from
6882 * user space while in guest debugging mode. Reading it for
6883 * #DB as well causes no harm, it is not used in that case.
6884 */
6885 vmx->vcpu.arch.event_exit_inst_len =
6886 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006887 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006888 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006889 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6890 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006891 break;
6892 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006893 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6894 kvm_run->ex.exception = ex_no;
6895 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006896 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006897 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006898 return 0;
6899}
6900
Avi Kivity851ba692009-08-24 11:10:17 +03006901static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006902{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006903 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 return 1;
6905}
6906
Avi Kivity851ba692009-08-24 11:10:17 +03006907static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006908{
Avi Kivity851ba692009-08-24 11:10:17 +03006909 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006910 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006911 return 0;
6912}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913
Avi Kivity851ba692009-08-24 11:10:17 +03006914static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006915{
He, Qingbfdaab02007-09-12 14:18:28 +08006916 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006917 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006918 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919
He, Qingbfdaab02007-09-12 14:18:28 +08006920 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006921 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006922
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006923 ++vcpu->stat.io_exits;
6924
Sean Christopherson432baf62018-03-08 08:57:26 -08006925 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006926 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006927
6928 port = exit_qualification >> 16;
6929 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006930 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006931
Sean Christophersondca7f122018-03-08 08:57:27 -08006932 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006933}
6934
Ingo Molnar102d8322007-02-19 14:37:47 +02006935static void
6936vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6937{
6938 /*
6939 * Patch in the VMCALL instruction:
6940 */
6941 hypercall[0] = 0x0f;
6942 hypercall[1] = 0x01;
6943 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006944}
6945
Guo Chao0fa06072012-06-28 15:16:19 +08006946/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006947static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6948{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006949 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006950 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6951 unsigned long orig_val = val;
6952
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006953 /*
6954 * We get here when L2 changed cr0 in a way that did not change
6955 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006956 * but did change L0 shadowed bits. So we first calculate the
6957 * effective cr0 value that L1 would like to write into the
6958 * hardware. It consists of the L2-owned bits from the new
6959 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006960 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006961 val = (val & ~vmcs12->cr0_guest_host_mask) |
6962 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6963
David Matlack38991522016-11-29 18:14:08 -08006964 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006965 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006966
6967 if (kvm_set_cr0(vcpu, val))
6968 return 1;
6969 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006970 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006971 } else {
6972 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006973 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006974 return 1;
David Matlack38991522016-11-29 18:14:08 -08006975
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006976 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006977 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006978}
6979
6980static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6981{
6982 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006983 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6984 unsigned long orig_val = val;
6985
6986 /* analogously to handle_set_cr0 */
6987 val = (val & ~vmcs12->cr4_guest_host_mask) |
6988 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6989 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006990 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006991 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006992 return 0;
6993 } else
6994 return kvm_set_cr4(vcpu, val);
6995}
6996
Paolo Bonzini0367f202016-07-12 10:44:55 +02006997static int handle_desc(struct kvm_vcpu *vcpu)
6998{
6999 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7000 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7001}
7002
Avi Kivity851ba692009-08-24 11:10:17 +03007003static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007004{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007005 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007006 int cr;
7007 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007008 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007009 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007010
He, Qingbfdaab02007-09-12 14:18:28 +08007011 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007012 cr = exit_qualification & 15;
7013 reg = (exit_qualification >> 8) & 15;
7014 switch ((exit_qualification >> 4) & 3) {
7015 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007016 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007017 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007018 switch (cr) {
7019 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007020 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007021 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007022 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007023 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007024 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007025 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007026 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007027 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007028 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007029 case 8: {
7030 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007031 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007032 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007033 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007034 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007035 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007036 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007037 return ret;
7038 /*
7039 * TODO: we might be squashing a
7040 * KVM_GUESTDBG_SINGLESTEP-triggered
7041 * KVM_EXIT_DEBUG here.
7042 */
Avi Kivity851ba692009-08-24 11:10:17 +03007043 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007044 return 0;
7045 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007046 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007047 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007048 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007049 WARN_ONCE(1, "Guest should always own CR0.TS");
7050 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007051 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007052 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007053 case 1: /*mov from cr*/
7054 switch (cr) {
7055 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007056 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007057 val = kvm_read_cr3(vcpu);
7058 kvm_register_write(vcpu, reg, val);
7059 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007060 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007061 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007062 val = kvm_get_cr8(vcpu);
7063 kvm_register_write(vcpu, reg, val);
7064 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007065 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007066 }
7067 break;
7068 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007069 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007070 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007071 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007072
Kyle Huey6affcbe2016-11-29 12:40:40 -08007073 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007074 default:
7075 break;
7076 }
Avi Kivity851ba692009-08-24 11:10:17 +03007077 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007078 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007079 (int)(exit_qualification >> 4) & 3, cr);
7080 return 0;
7081}
7082
Avi Kivity851ba692009-08-24 11:10:17 +03007083static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084{
He, Qingbfdaab02007-09-12 14:18:28 +08007085 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007086 int dr, dr7, reg;
7087
7088 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7089 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7090
7091 /* First, if DR does not exist, trigger UD */
7092 if (!kvm_require_dr(vcpu, dr))
7093 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007094
Jan Kiszkaf2483412010-01-20 18:20:20 +01007095 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007096 if (!kvm_require_cpl(vcpu, 0))
7097 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007098 dr7 = vmcs_readl(GUEST_DR7);
7099 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007100 /*
7101 * As the vm-exit takes precedence over the debug trap, we
7102 * need to emulate the latter, either for the host or the
7103 * guest debugging itself.
7104 */
7105 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007106 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007107 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007108 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007109 vcpu->run->debug.arch.exception = DB_VECTOR;
7110 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007111 return 0;
7112 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007113 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007114 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007115 kvm_queue_exception(vcpu, DB_VECTOR);
7116 return 1;
7117 }
7118 }
7119
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007120 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007121 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7122 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007123
7124 /*
7125 * No more DR vmexits; force a reload of the debug registers
7126 * and reenter on this instruction. The next vmexit will
7127 * retrieve the full state of the debug registers.
7128 */
7129 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7130 return 1;
7131 }
7132
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007133 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7134 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007135 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007136
7137 if (kvm_get_dr(vcpu, dr, &val))
7138 return 1;
7139 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007140 } else
Nadav Amit57773922014-06-18 17:19:23 +03007141 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007142 return 1;
7143
Kyle Huey6affcbe2016-11-29 12:40:40 -08007144 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007145}
7146
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007147static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7148{
7149 return vcpu->arch.dr6;
7150}
7151
7152static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7153{
7154}
7155
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007156static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7157{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007158 get_debugreg(vcpu->arch.db[0], 0);
7159 get_debugreg(vcpu->arch.db[1], 1);
7160 get_debugreg(vcpu->arch.db[2], 2);
7161 get_debugreg(vcpu->arch.db[3], 3);
7162 get_debugreg(vcpu->arch.dr6, 6);
7163 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7164
7165 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007166 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007167}
7168
Gleb Natapov020df072010-04-13 10:05:23 +03007169static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7170{
7171 vmcs_writel(GUEST_DR7, val);
7172}
7173
Avi Kivity851ba692009-08-24 11:10:17 +03007174static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007175{
Kyle Huey6a908b62016-11-29 12:40:37 -08007176 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007177}
7178
Avi Kivity851ba692009-08-24 11:10:17 +03007179static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007181 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007182 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007183
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007184 msr_info.index = ecx;
7185 msr_info.host_initiated = false;
7186 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007187 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007188 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007189 return 1;
7190 }
7191
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007192 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007193
Avi Kivity6aa8b732006-12-10 02:21:36 -08007194 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007195 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7196 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007197 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007198}
7199
Avi Kivity851ba692009-08-24 11:10:17 +03007200static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007201{
Will Auld8fe8ab42012-11-29 12:42:12 -08007202 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007203 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7204 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7205 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007206
Will Auld8fe8ab42012-11-29 12:42:12 -08007207 msr.data = data;
7208 msr.index = ecx;
7209 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007210 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007211 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007212 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007213 return 1;
7214 }
7215
Avi Kivity59200272010-01-25 19:47:02 +02007216 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007217 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007218}
7219
Avi Kivity851ba692009-08-24 11:10:17 +03007220static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007221{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007222 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007223 return 1;
7224}
7225
Avi Kivity851ba692009-08-24 11:10:17 +03007226static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007227{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007228 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7229 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007230
Avi Kivity3842d132010-07-27 12:30:24 +03007231 kvm_make_request(KVM_REQ_EVENT, vcpu);
7232
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007233 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007234 return 1;
7235}
7236
Avi Kivity851ba692009-08-24 11:10:17 +03007237static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007238{
Avi Kivityd3bef152007-06-05 15:53:05 +03007239 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240}
7241
Avi Kivity851ba692009-08-24 11:10:17 +03007242static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007243{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007244 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007245}
7246
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007247static int handle_invd(struct kvm_vcpu *vcpu)
7248{
Andre Przywara51d8b662010-12-21 11:12:02 +01007249 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007250}
7251
Avi Kivity851ba692009-08-24 11:10:17 +03007252static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007253{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007254 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007255
7256 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007258}
7259
Avi Kivityfee84b02011-11-10 14:57:25 +02007260static int handle_rdpmc(struct kvm_vcpu *vcpu)
7261{
7262 int err;
7263
7264 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007265 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007266}
7267
Avi Kivity851ba692009-08-24 11:10:17 +03007268static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007269{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007271}
7272
Dexuan Cui2acf9232010-06-10 11:27:12 +08007273static int handle_xsetbv(struct kvm_vcpu *vcpu)
7274{
7275 u64 new_bv = kvm_read_edx_eax(vcpu);
7276 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7277
7278 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007279 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007280 return 1;
7281}
7282
Wanpeng Lif53cd632014-12-02 19:14:58 +08007283static int handle_xsaves(struct kvm_vcpu *vcpu)
7284{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007285 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007286 WARN(1, "this should never happen\n");
7287 return 1;
7288}
7289
7290static int handle_xrstors(struct kvm_vcpu *vcpu)
7291{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007292 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007293 WARN(1, "this should never happen\n");
7294 return 1;
7295}
7296
Avi Kivity851ba692009-08-24 11:10:17 +03007297static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007298{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007299 if (likely(fasteoi)) {
7300 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7301 int access_type, offset;
7302
7303 access_type = exit_qualification & APIC_ACCESS_TYPE;
7304 offset = exit_qualification & APIC_ACCESS_OFFSET;
7305 /*
7306 * Sane guest uses MOV to write EOI, with written value
7307 * not cared. So make a short-circuit here by avoiding
7308 * heavy instruction emulation.
7309 */
7310 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7311 (offset == APIC_EOI)) {
7312 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007313 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007314 }
7315 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007316 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007317}
7318
Yang Zhangc7c9c562013-01-25 10:18:51 +08007319static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7320{
7321 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7322 int vector = exit_qualification & 0xff;
7323
7324 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7325 kvm_apic_set_eoi_accelerated(vcpu, vector);
7326 return 1;
7327}
7328
Yang Zhang83d4c282013-01-25 10:18:49 +08007329static int handle_apic_write(struct kvm_vcpu *vcpu)
7330{
7331 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7332 u32 offset = exit_qualification & 0xfff;
7333
7334 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7335 kvm_apic_write_nodecode(vcpu, offset);
7336 return 1;
7337}
7338
Avi Kivity851ba692009-08-24 11:10:17 +03007339static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007340{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007341 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007342 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007343 bool has_error_code = false;
7344 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007345 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007346 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007347
7348 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007349 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007350 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007351
7352 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7353
7354 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007355 if (reason == TASK_SWITCH_GATE && idt_v) {
7356 switch (type) {
7357 case INTR_TYPE_NMI_INTR:
7358 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007359 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007360 break;
7361 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007362 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007363 kvm_clear_interrupt_queue(vcpu);
7364 break;
7365 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007366 if (vmx->idt_vectoring_info &
7367 VECTORING_INFO_DELIVER_CODE_MASK) {
7368 has_error_code = true;
7369 error_code =
7370 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7371 }
7372 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007373 case INTR_TYPE_SOFT_EXCEPTION:
7374 kvm_clear_exception_queue(vcpu);
7375 break;
7376 default:
7377 break;
7378 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007379 }
Izik Eidus37817f22008-03-24 23:14:53 +02007380 tss_selector = exit_qualification;
7381
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007382 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7383 type != INTR_TYPE_EXT_INTR &&
7384 type != INTR_TYPE_NMI_INTR))
7385 skip_emulated_instruction(vcpu);
7386
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007387 if (kvm_task_switch(vcpu, tss_selector,
7388 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7389 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007390 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7391 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7392 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007393 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007394 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007395
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007396 /*
7397 * TODO: What about debug traps on tss switch?
7398 * Are we supposed to inject them and update dr6?
7399 */
7400
7401 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007402}
7403
Avi Kivity851ba692009-08-24 11:10:17 +03007404static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007405{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007406 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007407 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007408 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007409
Sheng Yangf9c617f2009-03-25 10:08:52 +08007410 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007411
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007412 /*
7413 * EPT violation happened while executing iret from NMI,
7414 * "blocked by NMI" bit has to be set before next VM entry.
7415 * There are errata that may cause this bit to not be set:
7416 * AAK134, BY25.
7417 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007418 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007419 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007420 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007421 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7422
Sheng Yang14394422008-04-28 12:24:45 +08007423 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007424 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007425
Junaid Shahid27959a42016-12-06 16:46:10 -08007426 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007427 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007428 ? PFERR_USER_MASK : 0;
7429 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007430 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007431 ? PFERR_WRITE_MASK : 0;
7432 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007433 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007434 ? PFERR_FETCH_MASK : 0;
7435 /* ept page table entry is present? */
7436 error_code |= (exit_qualification &
7437 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7438 EPT_VIOLATION_EXECUTABLE))
7439 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007440
Paolo Bonzinieebed242016-11-28 14:39:58 +01007441 error_code |= (exit_qualification & 0x100) != 0 ?
7442 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007443
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007444 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007445 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007446}
7447
Avi Kivity851ba692009-08-24 11:10:17 +03007448static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007449{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007450 gpa_t gpa;
7451
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007452 /*
7453 * A nested guest cannot optimize MMIO vmexits, because we have an
7454 * nGPA here instead of the required GPA.
7455 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007456 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007457 if (!is_guest_mode(vcpu) &&
7458 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007459 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007460 /*
7461 * Doing kvm_skip_emulated_instruction() depends on undefined
7462 * behavior: Intel's manual doesn't mandate
7463 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7464 * occurs and while on real hardware it was observed to be set,
7465 * other hypervisors (namely Hyper-V) don't set it, we end up
7466 * advancing IP with some random value. Disable fast mmio when
7467 * running nested and keep it for real hardware in hope that
7468 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7469 */
7470 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7471 return kvm_skip_emulated_instruction(vcpu);
7472 else
7473 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7474 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007475 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007476
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007477 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007478}
7479
Avi Kivity851ba692009-08-24 11:10:17 +03007480static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007481{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007482 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007483 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7484 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007485 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007486 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007487
7488 return 1;
7489}
7490
Mohammed Gamal80ced182009-09-01 12:48:18 +02007491static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007492{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007493 struct vcpu_vmx *vmx = to_vmx(vcpu);
7494 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007495 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007496 u32 cpu_exec_ctrl;
7497 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007498 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007499
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007500 /*
7501 * We should never reach the point where we are emulating L2
7502 * due to invalid guest state as that means we incorrectly
7503 * allowed a nested VMEntry with an invalid vmcs12.
7504 */
7505 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7506
Avi Kivity49e9d552010-09-19 14:34:08 +02007507 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7508 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007509
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007510 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007511 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007512 return handle_interrupt_window(&vmx->vcpu);
7513
Radim Krčmář72875d82017-04-26 22:32:19 +02007514 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007515 return 1;
7516
Liran Alon9b8ae632017-11-05 16:56:34 +02007517 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007518
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007519 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007520 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007521 ret = 0;
7522 goto out;
7523 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007524
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007525 if (err != EMULATE_DONE)
7526 goto emulation_error;
7527
7528 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7529 vcpu->arch.exception.pending)
7530 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007531
Gleb Natapov8d76c492013-05-08 18:38:44 +03007532 if (vcpu->arch.halt_request) {
7533 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007534 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007535 goto out;
7536 }
7537
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007538 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007539 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007540 if (need_resched())
7541 schedule();
7542 }
7543
Mohammed Gamal80ced182009-09-01 12:48:18 +02007544out:
7545 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007546
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007547emulation_error:
7548 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7549 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7550 vcpu->run->internal.ndata = 0;
7551 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007552}
7553
7554static void grow_ple_window(struct kvm_vcpu *vcpu)
7555{
7556 struct vcpu_vmx *vmx = to_vmx(vcpu);
7557 int old = vmx->ple_window;
7558
Babu Mogerc8e88712018-03-16 16:37:24 -04007559 vmx->ple_window = __grow_ple_window(old, ple_window,
7560 ple_window_grow,
7561 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007562
7563 if (vmx->ple_window != old)
7564 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007565
7566 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007567}
7568
7569static void shrink_ple_window(struct kvm_vcpu *vcpu)
7570{
7571 struct vcpu_vmx *vmx = to_vmx(vcpu);
7572 int old = vmx->ple_window;
7573
Babu Mogerc8e88712018-03-16 16:37:24 -04007574 vmx->ple_window = __shrink_ple_window(old, ple_window,
7575 ple_window_shrink,
7576 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007577
7578 if (vmx->ple_window != old)
7579 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007580
7581 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007582}
7583
7584/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007585 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7586 */
7587static void wakeup_handler(void)
7588{
7589 struct kvm_vcpu *vcpu;
7590 int cpu = smp_processor_id();
7591
7592 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7593 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7594 blocked_vcpu_list) {
7595 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7596
7597 if (pi_test_on(pi_desc) == 1)
7598 kvm_vcpu_kick(vcpu);
7599 }
7600 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7601}
7602
Peng Haoe01bca22018-04-07 05:47:32 +08007603static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007604{
7605 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7606 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7607 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7608 0ull, VMX_EPT_EXECUTABLE_MASK,
7609 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007610 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007611
7612 ept_set_mmio_spte_mask();
7613 kvm_enable_tdp();
7614}
7615
Tiejun Chenf2c76482014-10-28 10:14:47 +08007616static __init int hardware_setup(void)
7617{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007618 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007619
7620 rdmsrl_safe(MSR_EFER, &host_efer);
7621
7622 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7623 kvm_define_shared_msr(i, vmx_msr_index[i]);
7624
Radim Krčmář23611332016-09-29 22:41:33 +02007625 for (i = 0; i < VMX_BITMAP_NR; i++) {
7626 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7627 if (!vmx_bitmap[i])
7628 goto out;
7629 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007630
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007631 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7632 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7633
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007634 if (setup_vmcs_config(&vmcs_config) < 0) {
7635 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007636 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007637 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007638
7639 if (boot_cpu_has(X86_FEATURE_NX))
7640 kvm_enable_efer_bits(EFER_NX);
7641
Wanpeng Li08d839c2017-03-23 05:30:08 -07007642 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7643 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007644 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007645
Tiejun Chenf2c76482014-10-28 10:14:47 +08007646 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007647 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007648 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007649 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007650 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007651
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007652 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007653 enable_ept_ad_bits = 0;
7654
Wanpeng Li8ad81822017-10-09 15:51:53 -07007655 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007656 enable_unrestricted_guest = 0;
7657
Paolo Bonziniad15a292015-01-30 16:18:49 +01007658 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659 flexpriority_enabled = 0;
7660
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007661 if (!cpu_has_virtual_nmis())
7662 enable_vnmi = 0;
7663
Paolo Bonziniad15a292015-01-30 16:18:49 +01007664 /*
7665 * set_apic_access_page_addr() is used to reload apic access
7666 * page upon invalidation. No need to do anything if not
7667 * using the APIC_ACCESS_ADDR VMCS field.
7668 */
7669 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007670 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007671
7672 if (!cpu_has_vmx_tpr_shadow())
7673 kvm_x86_ops->update_cr8_intercept = NULL;
7674
7675 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7676 kvm_disable_largepages();
7677
Wanpeng Li0f107682017-09-28 18:06:24 -07007678 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007679 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007680 ple_window = 0;
7681 ple_window_grow = 0;
7682 ple_window_max = 0;
7683 ple_window_shrink = 0;
7684 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007685
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007686 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007687 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007688 kvm_x86_ops->sync_pir_to_irr = NULL;
7689 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007690
Haozhong Zhang64903d62015-10-20 15:39:09 +08007691 if (cpu_has_vmx_tsc_scaling()) {
7692 kvm_has_tsc_control = true;
7693 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7694 kvm_tsc_scaling_ratio_frac_bits = 48;
7695 }
7696
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007697 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7698
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007699 if (enable_ept)
7700 vmx_enable_tdp();
7701 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007702 kvm_disable_tdp();
7703
Kai Huang843e4332015-01-28 10:54:28 +08007704 /*
7705 * Only enable PML when hardware supports PML feature, and both EPT
7706 * and EPT A/D bit features are enabled -- PML depends on them to work.
7707 */
7708 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7709 enable_pml = 0;
7710
7711 if (!enable_pml) {
7712 kvm_x86_ops->slot_enable_log_dirty = NULL;
7713 kvm_x86_ops->slot_disable_log_dirty = NULL;
7714 kvm_x86_ops->flush_log_dirty = NULL;
7715 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7716 }
7717
Yunhong Jiang64672c92016-06-13 14:19:59 -07007718 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7719 u64 vmx_msr;
7720
7721 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7722 cpu_preemption_timer_multi =
7723 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7724 } else {
7725 kvm_x86_ops->set_hv_timer = NULL;
7726 kvm_x86_ops->cancel_hv_timer = NULL;
7727 }
7728
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007729 if (!cpu_has_vmx_shadow_vmcs())
7730 enable_shadow_vmcs = 0;
7731 if (enable_shadow_vmcs)
7732 init_vmcs_shadow_fields();
7733
Feng Wubf9f6ac2015-09-18 22:29:55 +08007734 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007735 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007736
Ashok Rajc45dcc72016-06-22 14:59:56 +08007737 kvm_mce_cap_supported |= MCG_LMCE_P;
7738
Tiejun Chenf2c76482014-10-28 10:14:47 +08007739 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007740
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007741out:
Radim Krčmář23611332016-09-29 22:41:33 +02007742 for (i = 0; i < VMX_BITMAP_NR; i++)
7743 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007744
7745 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007746}
7747
7748static __exit void hardware_unsetup(void)
7749{
Radim Krčmář23611332016-09-29 22:41:33 +02007750 int i;
7751
7752 for (i = 0; i < VMX_BITMAP_NR; i++)
7753 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007754
Tiejun Chenf2c76482014-10-28 10:14:47 +08007755 free_kvm_area();
7756}
7757
Avi Kivity6aa8b732006-12-10 02:21:36 -08007758/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007759 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7760 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7761 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007762static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007763{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007764 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007765 grow_ple_window(vcpu);
7766
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007767 /*
7768 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7769 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7770 * never set PAUSE_EXITING and just set PLE if supported,
7771 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7772 */
7773 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007774 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007775}
7776
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007777static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007778{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007779 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007780}
7781
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007782static int handle_mwait(struct kvm_vcpu *vcpu)
7783{
7784 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7785 return handle_nop(vcpu);
7786}
7787
Jim Mattson45ec3682017-08-23 16:32:04 -07007788static int handle_invalid_op(struct kvm_vcpu *vcpu)
7789{
7790 kvm_queue_exception(vcpu, UD_VECTOR);
7791 return 1;
7792}
7793
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007794static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7795{
7796 return 1;
7797}
7798
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007799static int handle_monitor(struct kvm_vcpu *vcpu)
7800{
7801 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7802 return handle_nop(vcpu);
7803}
7804
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007805/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007806 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7807 * set the success or error code of an emulated VMX instruction, as specified
7808 * by Vol 2B, VMX Instruction Reference, "Conventions".
7809 */
7810static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7811{
7812 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7813 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7814 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7815}
7816
7817static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7818{
7819 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7820 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7821 X86_EFLAGS_SF | X86_EFLAGS_OF))
7822 | X86_EFLAGS_CF);
7823}
7824
Abel Gordon145c28d2013-04-18 14:36:55 +03007825static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007826 u32 vm_instruction_error)
7827{
7828 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7829 /*
7830 * failValid writes the error number to the current VMCS, which
7831 * can't be done there isn't a current VMCS.
7832 */
7833 nested_vmx_failInvalid(vcpu);
7834 return;
7835 }
7836 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7837 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7838 X86_EFLAGS_SF | X86_EFLAGS_OF))
7839 | X86_EFLAGS_ZF);
7840 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7841 /*
7842 * We don't need to force a shadow sync because
7843 * VM_INSTRUCTION_ERROR is not shadowed
7844 */
7845}
Abel Gordon145c28d2013-04-18 14:36:55 +03007846
Wincy Vanff651cb2014-12-11 08:52:58 +03007847static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7848{
7849 /* TODO: not to reset guest simply here. */
7850 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007851 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007852}
7853
Jan Kiszkaf4124502014-03-07 20:03:13 +01007854static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7855{
7856 struct vcpu_vmx *vmx =
7857 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7858
7859 vmx->nested.preemption_timer_expired = true;
7860 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7861 kvm_vcpu_kick(&vmx->vcpu);
7862
7863 return HRTIMER_NORESTART;
7864}
7865
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007866/*
Bandan Das19677e32014-05-06 02:19:15 -04007867 * Decode the memory-address operand of a vmx instruction, as recorded on an
7868 * exit caused by such an instruction (run by a guest hypervisor).
7869 * On success, returns 0. When the operand is invalid, returns 1 and throws
7870 * #UD or #GP.
7871 */
7872static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7873 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007874 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007875{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007876 gva_t off;
7877 bool exn;
7878 struct kvm_segment s;
7879
Bandan Das19677e32014-05-06 02:19:15 -04007880 /*
7881 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7882 * Execution", on an exit, vmx_instruction_info holds most of the
7883 * addressing components of the operand. Only the displacement part
7884 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7885 * For how an actual address is calculated from all these components,
7886 * refer to Vol. 1, "Operand Addressing".
7887 */
7888 int scaling = vmx_instruction_info & 3;
7889 int addr_size = (vmx_instruction_info >> 7) & 7;
7890 bool is_reg = vmx_instruction_info & (1u << 10);
7891 int seg_reg = (vmx_instruction_info >> 15) & 7;
7892 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7893 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7894 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7895 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7896
7897 if (is_reg) {
7898 kvm_queue_exception(vcpu, UD_VECTOR);
7899 return 1;
7900 }
7901
7902 /* Addr = segment_base + offset */
7903 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007904 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007905 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007906 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007907 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007908 off += kvm_register_read(vcpu, index_reg)<<scaling;
7909 vmx_get_segment(vcpu, &s, seg_reg);
7910 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007911
7912 if (addr_size == 1) /* 32 bit */
7913 *ret &= 0xffffffff;
7914
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007915 /* Checks for #GP/#SS exceptions. */
7916 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007917 if (is_long_mode(vcpu)) {
7918 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7919 * non-canonical form. This is the only check on the memory
7920 * destination for long mode!
7921 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007922 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007923 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007924 /* Protected mode: apply checks for segment validity in the
7925 * following order:
7926 * - segment type check (#GP(0) may be thrown)
7927 * - usability check (#GP(0)/#SS(0))
7928 * - limit check (#GP(0)/#SS(0))
7929 */
7930 if (wr)
7931 /* #GP(0) if the destination operand is located in a
7932 * read-only data segment or any code segment.
7933 */
7934 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7935 else
7936 /* #GP(0) if the source operand is located in an
7937 * execute-only code segment
7938 */
7939 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007940 if (exn) {
7941 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7942 return 1;
7943 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007944 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7945 */
7946 exn = (s.unusable != 0);
7947 /* Protected mode: #GP(0)/#SS(0) if the memory
7948 * operand is outside the segment limit.
7949 */
7950 exn = exn || (off + sizeof(u64) > s.limit);
7951 }
7952 if (exn) {
7953 kvm_queue_exception_e(vcpu,
7954 seg_reg == VCPU_SREG_SS ?
7955 SS_VECTOR : GP_VECTOR,
7956 0);
7957 return 1;
7958 }
7959
Bandan Das19677e32014-05-06 02:19:15 -04007960 return 0;
7961}
7962
Radim Krčmářcbf71272017-05-19 15:48:51 +02007963static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007964{
7965 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007966 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007967
7968 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007969 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007970 return 1;
7971
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007972 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007973 kvm_inject_page_fault(vcpu, &e);
7974 return 1;
7975 }
7976
Bandan Das3573e222014-05-06 02:19:16 -04007977 return 0;
7978}
7979
Jim Mattsone29acc52016-11-30 12:03:43 -08007980static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7981{
7982 struct vcpu_vmx *vmx = to_vmx(vcpu);
7983 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007984 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007985
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007986 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7987 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007988 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007989
7990 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7991 if (!vmx->nested.cached_vmcs12)
7992 goto out_cached_vmcs12;
7993
7994 if (enable_shadow_vmcs) {
7995 shadow_vmcs = alloc_vmcs();
7996 if (!shadow_vmcs)
7997 goto out_shadow_vmcs;
7998 /* mark vmcs as shadow */
7999 shadow_vmcs->revision_id |= (1u << 31);
8000 /* init shadow vmcs */
8001 vmcs_clear(shadow_vmcs);
8002 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
8003 }
8004
Jim Mattsone29acc52016-11-30 12:03:43 -08008005 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8006 HRTIMER_MODE_REL_PINNED);
8007 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8008
8009 vmx->nested.vmxon = true;
8010 return 0;
8011
8012out_shadow_vmcs:
8013 kfree(vmx->nested.cached_vmcs12);
8014
8015out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008016 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008017
Jim Mattsonde3a0022017-11-27 17:22:25 -06008018out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008019 return -ENOMEM;
8020}
8021
Bandan Das3573e222014-05-06 02:19:16 -04008022/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008023 * Emulate the VMXON instruction.
8024 * Currently, we just remember that VMX is active, and do not save or even
8025 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8026 * do not currently need to store anything in that guest-allocated memory
8027 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8028 * argument is different from the VMXON pointer (which the spec says they do).
8029 */
8030static int handle_vmon(struct kvm_vcpu *vcpu)
8031{
Jim Mattsone29acc52016-11-30 12:03:43 -08008032 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008033 gpa_t vmptr;
8034 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008035 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008036 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8037 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008038
Jim Mattson70f3aac2017-04-26 08:53:46 -07008039 /*
8040 * The Intel VMX Instruction Reference lists a bunch of bits that are
8041 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8042 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8043 * Otherwise, we should fail with #UD. But most faulting conditions
8044 * have already been checked by hardware, prior to the VM-exit for
8045 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8046 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008047 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008048 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008049 kvm_queue_exception(vcpu, UD_VECTOR);
8050 return 1;
8051 }
8052
Felix Wilhelm727ba742018-06-11 09:43:44 +02008053 /* CPL=0 must be checked manually. */
8054 if (vmx_get_cpl(vcpu)) {
8055 kvm_queue_exception(vcpu, UD_VECTOR);
8056 return 1;
8057 }
8058
Abel Gordon145c28d2013-04-18 14:36:55 +03008059 if (vmx->nested.vmxon) {
8060 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008061 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008062 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008063
Haozhong Zhang3b840802016-06-22 14:59:54 +08008064 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008065 != VMXON_NEEDED_FEATURES) {
8066 kvm_inject_gp(vcpu, 0);
8067 return 1;
8068 }
8069
Radim Krčmářcbf71272017-05-19 15:48:51 +02008070 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008071 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008072
8073 /*
8074 * SDM 3: 24.11.5
8075 * The first 4 bytes of VMXON region contain the supported
8076 * VMCS revision identifier
8077 *
8078 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8079 * which replaces physical address width with 32
8080 */
8081 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8082 nested_vmx_failInvalid(vcpu);
8083 return kvm_skip_emulated_instruction(vcpu);
8084 }
8085
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008086 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8087 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008088 nested_vmx_failInvalid(vcpu);
8089 return kvm_skip_emulated_instruction(vcpu);
8090 }
8091 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8092 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008093 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008094 nested_vmx_failInvalid(vcpu);
8095 return kvm_skip_emulated_instruction(vcpu);
8096 }
8097 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008098 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008099
8100 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008101 ret = enter_vmx_operation(vcpu);
8102 if (ret)
8103 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008104
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008105 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008106 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008107}
8108
8109/*
8110 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8111 * for running VMX instructions (except VMXON, whose prerequisites are
8112 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008113 * Note that many of these exceptions have priority over VM exits, so they
8114 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008115 */
8116static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8117{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008118 if (vmx_get_cpl(vcpu)) {
8119 kvm_queue_exception(vcpu, UD_VECTOR);
8120 return 0;
8121 }
8122
Jim Mattson70f3aac2017-04-26 08:53:46 -07008123 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008124 kvm_queue_exception(vcpu, UD_VECTOR);
8125 return 0;
8126 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008127 return 1;
8128}
8129
David Matlack8ca44e82017-08-01 14:00:39 -07008130static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8131{
8132 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8133 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8134}
8135
Abel Gordone7953d72013-04-18 14:37:55 +03008136static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8137{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008138 if (vmx->nested.current_vmptr == -1ull)
8139 return;
8140
Abel Gordon012f83c2013-04-18 14:39:25 +03008141 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008142 /* copy to memory all shadowed fields in case
8143 they were modified */
8144 copy_shadow_to_vmcs12(vmx);
8145 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008146 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008147 }
Wincy Van705699a2015-02-03 23:58:17 +08008148 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008149
8150 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008151 kvm_vcpu_write_guest_page(&vmx->vcpu,
8152 vmx->nested.current_vmptr >> PAGE_SHIFT,
8153 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008154
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008155 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008156}
8157
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008158/*
8159 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8160 * just stops using VMX.
8161 */
8162static void free_nested(struct vcpu_vmx *vmx)
8163{
Wanpeng Lib7455822017-11-22 14:04:00 -08008164 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008165 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008166
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008167 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008168 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008169 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008170 vmx->nested.posted_intr_nv = -1;
8171 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008172 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008173 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008174 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8175 free_vmcs(vmx->vmcs01.shadow_vmcs);
8176 vmx->vmcs01.shadow_vmcs = NULL;
8177 }
David Matlack4f2777b2016-07-13 17:16:37 -07008178 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008179 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008180 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008181 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008182 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008183 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008184 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008185 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008186 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008187 }
Wincy Van705699a2015-02-03 23:58:17 +08008188 if (vmx->nested.pi_desc_page) {
8189 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008190 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008191 vmx->nested.pi_desc_page = NULL;
8192 vmx->nested.pi_desc = NULL;
8193 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008194
Jim Mattsonde3a0022017-11-27 17:22:25 -06008195 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008196}
8197
8198/* Emulate the VMXOFF instruction */
8199static int handle_vmoff(struct kvm_vcpu *vcpu)
8200{
8201 if (!nested_vmx_check_permission(vcpu))
8202 return 1;
8203 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008204 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008205 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008206}
8207
Nadav Har'El27d6c862011-05-25 23:06:59 +03008208/* Emulate the VMCLEAR instruction */
8209static int handle_vmclear(struct kvm_vcpu *vcpu)
8210{
8211 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008212 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008213 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008214
8215 if (!nested_vmx_check_permission(vcpu))
8216 return 1;
8217
Radim Krčmářcbf71272017-05-19 15:48:51 +02008218 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008219 return 1;
8220
Radim Krčmářcbf71272017-05-19 15:48:51 +02008221 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8222 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8223 return kvm_skip_emulated_instruction(vcpu);
8224 }
8225
8226 if (vmptr == vmx->nested.vmxon_ptr) {
8227 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8228 return kvm_skip_emulated_instruction(vcpu);
8229 }
8230
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008231 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008232 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008233
Jim Mattson587d7e722017-03-02 12:41:48 -08008234 kvm_vcpu_write_guest(vcpu,
8235 vmptr + offsetof(struct vmcs12, launch_state),
8236 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008237
Nadav Har'El27d6c862011-05-25 23:06:59 +03008238 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008239 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008240}
8241
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008242static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8243
8244/* Emulate the VMLAUNCH instruction */
8245static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8246{
8247 return nested_vmx_run(vcpu, true);
8248}
8249
8250/* Emulate the VMRESUME instruction */
8251static int handle_vmresume(struct kvm_vcpu *vcpu)
8252{
8253
8254 return nested_vmx_run(vcpu, false);
8255}
8256
Nadav Har'El49f705c2011-05-25 23:08:30 +03008257/*
8258 * Read a vmcs12 field. Since these can have varying lengths and we return
8259 * one type, we chose the biggest type (u64) and zero-extend the return value
8260 * to that size. Note that the caller, handle_vmread, might need to use only
8261 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8262 * 64-bit fields are to be returned).
8263 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008264static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8265 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008266{
8267 short offset = vmcs_field_to_offset(field);
8268 char *p;
8269
8270 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008271 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008272
8273 p = ((char *)(get_vmcs12(vcpu))) + offset;
8274
Jim Mattsond37f4262017-12-22 12:12:16 -08008275 switch (vmcs_field_width(field)) {
8276 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008277 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008278 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008279 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008280 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008281 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008282 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008283 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008284 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008285 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008286 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008287 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008288 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008289 WARN_ON(1);
8290 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008291 }
8292}
8293
Abel Gordon20b97fe2013-04-18 14:36:25 +03008294
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008295static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8296 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008297 short offset = vmcs_field_to_offset(field);
8298 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8299 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008300 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008301
Jim Mattsond37f4262017-12-22 12:12:16 -08008302 switch (vmcs_field_width(field)) {
8303 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008304 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008305 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008306 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008307 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008308 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008309 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008310 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008311 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008312 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008313 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008314 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008315 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008316 WARN_ON(1);
8317 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008318 }
8319
8320}
8321
Jim Mattsonf4160e42018-05-29 09:11:33 -07008322/*
8323 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8324 * they have been modified by the L1 guest. Note that the "read-only"
8325 * VM-exit information fields are actually writable if the vCPU is
8326 * configured to support "VMWRITE to any supported field in the VMCS."
8327 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008328static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8329{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008330 const u16 *fields[] = {
8331 shadow_read_write_fields,
8332 shadow_read_only_fields
8333 };
8334 const int max_fields[] = {
8335 max_shadow_read_write_fields,
8336 max_shadow_read_only_fields
8337 };
8338 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008339 unsigned long field;
8340 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008341 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008342
Jan Kiszka282da872014-10-08 18:05:39 +02008343 preempt_disable();
8344
Abel Gordon16f5b902013-04-18 14:38:25 +03008345 vmcs_load(shadow_vmcs);
8346
Jim Mattsonf4160e42018-05-29 09:11:33 -07008347 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8348 for (i = 0; i < max_fields[q]; i++) {
8349 field = fields[q][i];
8350 field_value = __vmcs_readl(field);
8351 vmcs12_write_any(&vmx->vcpu, field, field_value);
8352 }
8353 /*
8354 * Skip the VM-exit information fields if they are read-only.
8355 */
8356 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8357 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008358 }
8359
8360 vmcs_clear(shadow_vmcs);
8361 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008362
8363 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008364}
8365
Abel Gordonc3114422013-04-18 14:38:55 +03008366static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8367{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008368 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008369 shadow_read_write_fields,
8370 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008371 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008372 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008373 max_shadow_read_write_fields,
8374 max_shadow_read_only_fields
8375 };
8376 int i, q;
8377 unsigned long field;
8378 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008379 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008380
8381 vmcs_load(shadow_vmcs);
8382
Mathias Krausec2bae892013-06-26 20:36:21 +02008383 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008384 for (i = 0; i < max_fields[q]; i++) {
8385 field = fields[q][i];
8386 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008387 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008388 }
8389 }
8390
8391 vmcs_clear(shadow_vmcs);
8392 vmcs_load(vmx->loaded_vmcs->vmcs);
8393}
8394
Nadav Har'El49f705c2011-05-25 23:08:30 +03008395/*
8396 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8397 * used before) all generate the same failure when it is missing.
8398 */
8399static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8400{
8401 struct vcpu_vmx *vmx = to_vmx(vcpu);
8402 if (vmx->nested.current_vmptr == -1ull) {
8403 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008404 return 0;
8405 }
8406 return 1;
8407}
8408
8409static int handle_vmread(struct kvm_vcpu *vcpu)
8410{
8411 unsigned long field;
8412 u64 field_value;
8413 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8414 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8415 gva_t gva = 0;
8416
Kyle Hueyeb277562016-11-29 12:40:39 -08008417 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008418 return 1;
8419
Kyle Huey6affcbe2016-11-29 12:40:40 -08008420 if (!nested_vmx_check_vmcs12(vcpu))
8421 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008422
Nadav Har'El49f705c2011-05-25 23:08:30 +03008423 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008424 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008426 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008427 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008428 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008429 }
8430 /*
8431 * Now copy part of this value to register or memory, as requested.
8432 * Note that the number of bits actually copied is 32 or 64 depending
8433 * on the guest's mode (32 or 64 bit), not on the given field's length.
8434 */
8435 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008436 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008437 field_value);
8438 } else {
8439 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008440 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008442 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008443 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8444 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008445 }
8446
8447 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008448 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008449}
8450
8451
8452static int handle_vmwrite(struct kvm_vcpu *vcpu)
8453{
8454 unsigned long field;
8455 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008456 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008457 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8458 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008459
Nadav Har'El49f705c2011-05-25 23:08:30 +03008460 /* The value to write might be 32 or 64 bits, depending on L1's long
8461 * mode, and eventually we need to write that into a field of several
8462 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008463 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008464 * bits into the vmcs12 field.
8465 */
8466 u64 field_value = 0;
8467 struct x86_exception e;
8468
Kyle Hueyeb277562016-11-29 12:40:39 -08008469 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008470 return 1;
8471
Kyle Huey6affcbe2016-11-29 12:40:40 -08008472 if (!nested_vmx_check_vmcs12(vcpu))
8473 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008474
Nadav Har'El49f705c2011-05-25 23:08:30 +03008475 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008476 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008477 (((vmx_instruction_info) >> 3) & 0xf));
8478 else {
8479 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008480 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008481 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008482 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8483 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008484 kvm_inject_page_fault(vcpu, &e);
8485 return 1;
8486 }
8487 }
8488
8489
Nadav Amit27e6fb52014-06-18 17:19:26 +03008490 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008491 /*
8492 * If the vCPU supports "VMWRITE to any supported field in the
8493 * VMCS," then the "read-only" fields are actually read/write.
8494 */
8495 if (vmcs_field_readonly(field) &&
8496 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008497 nested_vmx_failValid(vcpu,
8498 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008499 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008500 }
8501
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008502 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008503 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008504 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008505 }
8506
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008507 switch (field) {
8508#define SHADOW_FIELD_RW(x) case x:
8509#include "vmx_shadow_fields.h"
8510 /*
8511 * The fields that can be updated by L1 without a vmexit are
8512 * always updated in the vmcs02, the others go down the slow
8513 * path of prepare_vmcs02.
8514 */
8515 break;
8516 default:
8517 vmx->nested.dirty_vmcs12 = true;
8518 break;
8519 }
8520
Nadav Har'El49f705c2011-05-25 23:08:30 +03008521 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008522 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008523}
8524
Jim Mattsona8bc2842016-11-30 12:03:44 -08008525static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8526{
8527 vmx->nested.current_vmptr = vmptr;
8528 if (enable_shadow_vmcs) {
8529 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8530 SECONDARY_EXEC_SHADOW_VMCS);
8531 vmcs_write64(VMCS_LINK_POINTER,
8532 __pa(vmx->vmcs01.shadow_vmcs));
8533 vmx->nested.sync_shadow_vmcs = true;
8534 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008535 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008536}
8537
Nadav Har'El63846662011-05-25 23:07:29 +03008538/* Emulate the VMPTRLD instruction */
8539static int handle_vmptrld(struct kvm_vcpu *vcpu)
8540{
8541 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008542 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008543
8544 if (!nested_vmx_check_permission(vcpu))
8545 return 1;
8546
Radim Krčmářcbf71272017-05-19 15:48:51 +02008547 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008548 return 1;
8549
Radim Krčmářcbf71272017-05-19 15:48:51 +02008550 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8551 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8552 return kvm_skip_emulated_instruction(vcpu);
8553 }
8554
8555 if (vmptr == vmx->nested.vmxon_ptr) {
8556 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8557 return kvm_skip_emulated_instruction(vcpu);
8558 }
8559
Nadav Har'El63846662011-05-25 23:07:29 +03008560 if (vmx->nested.current_vmptr != vmptr) {
8561 struct vmcs12 *new_vmcs12;
8562 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008563 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8564 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008565 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008566 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008567 }
8568 new_vmcs12 = kmap(page);
8569 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8570 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008571 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008572 nested_vmx_failValid(vcpu,
8573 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008574 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008575 }
Nadav Har'El63846662011-05-25 23:07:29 +03008576
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008577 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008578 /*
8579 * Load VMCS12 from guest memory since it is not already
8580 * cached.
8581 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008582 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8583 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008584 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008585
Jim Mattsona8bc2842016-11-30 12:03:44 -08008586 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008587 }
8588
8589 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008590 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008591}
8592
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008593/* Emulate the VMPTRST instruction */
8594static int handle_vmptrst(struct kvm_vcpu *vcpu)
8595{
8596 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8597 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8598 gva_t vmcs_gva;
8599 struct x86_exception e;
8600
8601 if (!nested_vmx_check_permission(vcpu))
8602 return 1;
8603
8604 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008605 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008606 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008607 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008608 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8609 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8610 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008611 kvm_inject_page_fault(vcpu, &e);
8612 return 1;
8613 }
8614 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008615 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008616}
8617
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008618/* Emulate the INVEPT instruction */
8619static int handle_invept(struct kvm_vcpu *vcpu)
8620{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008621 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008622 u32 vmx_instruction_info, types;
8623 unsigned long type;
8624 gva_t gva;
8625 struct x86_exception e;
8626 struct {
8627 u64 eptp, gpa;
8628 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008629
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008630 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008631 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008632 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008633 kvm_queue_exception(vcpu, UD_VECTOR);
8634 return 1;
8635 }
8636
8637 if (!nested_vmx_check_permission(vcpu))
8638 return 1;
8639
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008640 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008641 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008642
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008643 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008644
Jim Mattson85c856b2016-10-26 08:38:38 -07008645 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008646 nested_vmx_failValid(vcpu,
8647 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008648 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008649 }
8650
8651 /* According to the Intel VMX instruction reference, the memory
8652 * operand is read even if it isn't needed (e.g., for type==global)
8653 */
8654 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008655 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008656 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008657 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008658 kvm_inject_page_fault(vcpu, &e);
8659 return 1;
8660 }
8661
8662 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008663 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008664 /*
8665 * TODO: track mappings and invalidate
8666 * single context requests appropriately
8667 */
8668 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008669 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008670 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008671 nested_vmx_succeed(vcpu);
8672 break;
8673 default:
8674 BUG_ON(1);
8675 break;
8676 }
8677
Kyle Huey6affcbe2016-11-29 12:40:40 -08008678 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008679}
8680
Petr Matouseka642fc32014-09-23 20:22:30 +02008681static int handle_invvpid(struct kvm_vcpu *vcpu)
8682{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008683 struct vcpu_vmx *vmx = to_vmx(vcpu);
8684 u32 vmx_instruction_info;
8685 unsigned long type, types;
8686 gva_t gva;
8687 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008688 struct {
8689 u64 vpid;
8690 u64 gla;
8691 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008692
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008693 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008694 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008695 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008696 kvm_queue_exception(vcpu, UD_VECTOR);
8697 return 1;
8698 }
8699
8700 if (!nested_vmx_check_permission(vcpu))
8701 return 1;
8702
8703 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8704 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8705
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008706 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008707 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008708
Jim Mattson85c856b2016-10-26 08:38:38 -07008709 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008710 nested_vmx_failValid(vcpu,
8711 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008712 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008713 }
8714
8715 /* according to the intel vmx instruction reference, the memory
8716 * operand is read even if it isn't needed (e.g., for type==global)
8717 */
8718 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8719 vmx_instruction_info, false, &gva))
8720 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008721 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008722 kvm_inject_page_fault(vcpu, &e);
8723 return 1;
8724 }
Jim Mattson40352602017-06-28 09:37:37 -07008725 if (operand.vpid >> 16) {
8726 nested_vmx_failValid(vcpu,
8727 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8728 return kvm_skip_emulated_instruction(vcpu);
8729 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008730
8731 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008732 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008733 if (!operand.vpid ||
8734 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008735 nested_vmx_failValid(vcpu,
8736 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8737 return kvm_skip_emulated_instruction(vcpu);
8738 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008739 if (cpu_has_vmx_invvpid_individual_addr() &&
8740 vmx->nested.vpid02) {
8741 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8742 vmx->nested.vpid02, operand.gla);
8743 } else
8744 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8745 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008746 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008747 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008748 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008749 nested_vmx_failValid(vcpu,
8750 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008751 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008752 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008753 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008754 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008755 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008756 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008757 break;
8758 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008759 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008760 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008761 }
8762
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008763 nested_vmx_succeed(vcpu);
8764
Kyle Huey6affcbe2016-11-29 12:40:40 -08008765 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008766}
8767
Kai Huang843e4332015-01-28 10:54:28 +08008768static int handle_pml_full(struct kvm_vcpu *vcpu)
8769{
8770 unsigned long exit_qualification;
8771
8772 trace_kvm_pml_full(vcpu->vcpu_id);
8773
8774 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8775
8776 /*
8777 * PML buffer FULL happened while executing iret from NMI,
8778 * "blocked by NMI" bit has to be set before next VM entry.
8779 */
8780 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008781 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008782 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8783 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8784 GUEST_INTR_STATE_NMI);
8785
8786 /*
8787 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8788 * here.., and there's no userspace involvement needed for PML.
8789 */
8790 return 1;
8791}
8792
Yunhong Jiang64672c92016-06-13 14:19:59 -07008793static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8794{
8795 kvm_lapic_expired_hv_timer(vcpu);
8796 return 1;
8797}
8798
Bandan Das41ab9372017-08-03 15:54:43 -04008799static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8800{
8801 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008802 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8803
8804 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008805 switch (address & VMX_EPTP_MT_MASK) {
8806 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008807 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008808 return false;
8809 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008810 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008811 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008812 return false;
8813 break;
8814 default:
8815 return false;
8816 }
8817
David Hildenbrandbb97a012017-08-10 23:15:28 +02008818 /* only 4 levels page-walk length are valid */
8819 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008820 return false;
8821
8822 /* Reserved bits should not be set */
8823 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8824 return false;
8825
8826 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008827 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008828 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008829 return false;
8830 }
8831
8832 return true;
8833}
8834
8835static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8836 struct vmcs12 *vmcs12)
8837{
8838 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8839 u64 address;
8840 bool accessed_dirty;
8841 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8842
8843 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8844 !nested_cpu_has_ept(vmcs12))
8845 return 1;
8846
8847 if (index >= VMFUNC_EPTP_ENTRIES)
8848 return 1;
8849
8850
8851 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8852 &address, index * 8, 8))
8853 return 1;
8854
David Hildenbrandbb97a012017-08-10 23:15:28 +02008855 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008856
8857 /*
8858 * If the (L2) guest does a vmfunc to the currently
8859 * active ept pointer, we don't have to do anything else
8860 */
8861 if (vmcs12->ept_pointer != address) {
8862 if (!valid_ept_address(vcpu, address))
8863 return 1;
8864
8865 kvm_mmu_unload(vcpu);
8866 mmu->ept_ad = accessed_dirty;
8867 mmu->base_role.ad_disabled = !accessed_dirty;
8868 vmcs12->ept_pointer = address;
8869 /*
8870 * TODO: Check what's the correct approach in case
8871 * mmu reload fails. Currently, we just let the next
8872 * reload potentially fail
8873 */
8874 kvm_mmu_reload(vcpu);
8875 }
8876
8877 return 0;
8878}
8879
Bandan Das2a499e42017-08-03 15:54:41 -04008880static int handle_vmfunc(struct kvm_vcpu *vcpu)
8881{
Bandan Das27c42a12017-08-03 15:54:42 -04008882 struct vcpu_vmx *vmx = to_vmx(vcpu);
8883 struct vmcs12 *vmcs12;
8884 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8885
8886 /*
8887 * VMFUNC is only supported for nested guests, but we always enable the
8888 * secondary control for simplicity; for non-nested mode, fake that we
8889 * didn't by injecting #UD.
8890 */
8891 if (!is_guest_mode(vcpu)) {
8892 kvm_queue_exception(vcpu, UD_VECTOR);
8893 return 1;
8894 }
8895
8896 vmcs12 = get_vmcs12(vcpu);
8897 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8898 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008899
8900 switch (function) {
8901 case 0:
8902 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8903 goto fail;
8904 break;
8905 default:
8906 goto fail;
8907 }
8908 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008909
8910fail:
8911 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8912 vmcs_read32(VM_EXIT_INTR_INFO),
8913 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008914 return 1;
8915}
8916
Nadav Har'El0140cae2011-05-25 23:06:28 +03008917/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008918 * The exit handlers return 1 if the exit was handled fully and guest execution
8919 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8920 * to be done to userspace and return 0.
8921 */
Mathias Krause772e0312012-08-30 01:30:19 +02008922static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008923 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8924 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008925 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008926 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008927 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008928 [EXIT_REASON_CR_ACCESS] = handle_cr,
8929 [EXIT_REASON_DR_ACCESS] = handle_dr,
8930 [EXIT_REASON_CPUID] = handle_cpuid,
8931 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8932 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8933 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8934 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008935 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008936 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008937 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008938 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008939 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008940 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008941 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008942 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008943 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008944 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008945 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008946 [EXIT_REASON_VMOFF] = handle_vmoff,
8947 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008948 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8949 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008950 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008951 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008952 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008953 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008954 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008955 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008956 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8957 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008958 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8959 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008960 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008961 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008962 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008963 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008964 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008965 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008966 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008967 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008968 [EXIT_REASON_XSAVES] = handle_xsaves,
8969 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008970 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008971 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008972 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008973};
8974
8975static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008976 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008977
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008978static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8979 struct vmcs12 *vmcs12)
8980{
8981 unsigned long exit_qualification;
8982 gpa_t bitmap, last_bitmap;
8983 unsigned int port;
8984 int size;
8985 u8 b;
8986
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008987 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008988 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008989
8990 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8991
8992 port = exit_qualification >> 16;
8993 size = (exit_qualification & 7) + 1;
8994
8995 last_bitmap = (gpa_t)-1;
8996 b = -1;
8997
8998 while (size > 0) {
8999 if (port < 0x8000)
9000 bitmap = vmcs12->io_bitmap_a;
9001 else if (port < 0x10000)
9002 bitmap = vmcs12->io_bitmap_b;
9003 else
Joe Perches1d804d02015-03-30 16:46:09 -07009004 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009005 bitmap += (port & 0x7fff) / 8;
9006
9007 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009008 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009009 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009010 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009011 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009012
9013 port++;
9014 size--;
9015 last_bitmap = bitmap;
9016 }
9017
Joe Perches1d804d02015-03-30 16:46:09 -07009018 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009019}
9020
Nadav Har'El644d7112011-05-25 23:12:35 +03009021/*
9022 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9023 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9024 * disinterest in the current event (read or write a specific MSR) by using an
9025 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9026 */
9027static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9028 struct vmcs12 *vmcs12, u32 exit_reason)
9029{
9030 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9031 gpa_t bitmap;
9032
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009033 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009034 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009035
9036 /*
9037 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9038 * for the four combinations of read/write and low/high MSR numbers.
9039 * First we need to figure out which of the four to use:
9040 */
9041 bitmap = vmcs12->msr_bitmap;
9042 if (exit_reason == EXIT_REASON_MSR_WRITE)
9043 bitmap += 2048;
9044 if (msr_index >= 0xc0000000) {
9045 msr_index -= 0xc0000000;
9046 bitmap += 1024;
9047 }
9048
9049 /* Then read the msr_index'th bit from this bitmap: */
9050 if (msr_index < 1024*8) {
9051 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009052 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009053 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009054 return 1 & (b >> (msr_index & 7));
9055 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009056 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009057}
9058
9059/*
9060 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9061 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9062 * intercept (via guest_host_mask etc.) the current event.
9063 */
9064static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9065 struct vmcs12 *vmcs12)
9066{
9067 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9068 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009069 int reg;
9070 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009071
9072 switch ((exit_qualification >> 4) & 3) {
9073 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009074 reg = (exit_qualification >> 8) & 15;
9075 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009076 switch (cr) {
9077 case 0:
9078 if (vmcs12->cr0_guest_host_mask &
9079 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009080 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009081 break;
9082 case 3:
9083 if ((vmcs12->cr3_target_count >= 1 &&
9084 vmcs12->cr3_target_value0 == val) ||
9085 (vmcs12->cr3_target_count >= 2 &&
9086 vmcs12->cr3_target_value1 == val) ||
9087 (vmcs12->cr3_target_count >= 3 &&
9088 vmcs12->cr3_target_value2 == val) ||
9089 (vmcs12->cr3_target_count >= 4 &&
9090 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009091 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009092 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009093 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009094 break;
9095 case 4:
9096 if (vmcs12->cr4_guest_host_mask &
9097 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009098 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009099 break;
9100 case 8:
9101 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009102 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009103 break;
9104 }
9105 break;
9106 case 2: /* clts */
9107 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9108 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009110 break;
9111 case 1: /* mov from cr */
9112 switch (cr) {
9113 case 3:
9114 if (vmcs12->cpu_based_vm_exec_control &
9115 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009116 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009117 break;
9118 case 8:
9119 if (vmcs12->cpu_based_vm_exec_control &
9120 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009121 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009122 break;
9123 }
9124 break;
9125 case 3: /* lmsw */
9126 /*
9127 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9128 * cr0. Other attempted changes are ignored, with no exit.
9129 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009130 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009131 if (vmcs12->cr0_guest_host_mask & 0xe &
9132 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009133 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009134 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9135 !(vmcs12->cr0_read_shadow & 0x1) &&
9136 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009137 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009138 break;
9139 }
Joe Perches1d804d02015-03-30 16:46:09 -07009140 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009141}
9142
9143/*
9144 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9145 * should handle it ourselves in L0 (and then continue L2). Only call this
9146 * when in is_guest_mode (L2).
9147 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009148static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009149{
Nadav Har'El644d7112011-05-25 23:12:35 +03009150 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9151 struct vcpu_vmx *vmx = to_vmx(vcpu);
9152 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9153
Jim Mattson4f350c62017-09-14 16:31:44 -07009154 if (vmx->nested.nested_run_pending)
9155 return false;
9156
9157 if (unlikely(vmx->fail)) {
9158 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9159 vmcs_read32(VM_INSTRUCTION_ERROR));
9160 return true;
9161 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009162
David Matlackc9f04402017-08-01 14:00:40 -07009163 /*
9164 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009165 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9166 * Page). The CPU may write to these pages via their host
9167 * physical address while L2 is running, bypassing any
9168 * address-translation-based dirty tracking (e.g. EPT write
9169 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009170 *
9171 * Mark them dirty on every exit from L2 to prevent them from
9172 * getting out of sync with dirty tracking.
9173 */
9174 nested_mark_vmcs12_pages_dirty(vcpu);
9175
Jim Mattson4f350c62017-09-14 16:31:44 -07009176 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9177 vmcs_readl(EXIT_QUALIFICATION),
9178 vmx->idt_vectoring_info,
9179 intr_info,
9180 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9181 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009182
9183 switch (exit_reason) {
9184 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009185 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009186 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009188 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009189 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009190 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009191 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009192 else if (is_debug(intr_info) &&
9193 vcpu->guest_debug &
9194 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9195 return false;
9196 else if (is_breakpoint(intr_info) &&
9197 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9198 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009199 return vmcs12->exception_bitmap &
9200 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9201 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009202 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009203 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009204 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009205 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009206 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009207 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009208 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009209 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009210 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009211 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009212 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009213 case EXIT_REASON_HLT:
9214 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9215 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009216 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009217 case EXIT_REASON_INVLPG:
9218 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9219 case EXIT_REASON_RDPMC:
9220 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009221 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009222 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009223 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009224 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009225 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009226 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9227 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9228 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9229 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9230 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9231 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009232 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009233 /*
9234 * VMX instructions trap unconditionally. This allows L1 to
9235 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9236 */
Joe Perches1d804d02015-03-30 16:46:09 -07009237 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009238 case EXIT_REASON_CR_ACCESS:
9239 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9240 case EXIT_REASON_DR_ACCESS:
9241 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9242 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009243 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009244 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9245 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009246 case EXIT_REASON_MSR_READ:
9247 case EXIT_REASON_MSR_WRITE:
9248 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9249 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009250 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009251 case EXIT_REASON_MWAIT_INSTRUCTION:
9252 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009253 case EXIT_REASON_MONITOR_TRAP_FLAG:
9254 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009255 case EXIT_REASON_MONITOR_INSTRUCTION:
9256 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9257 case EXIT_REASON_PAUSE_INSTRUCTION:
9258 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9259 nested_cpu_has2(vmcs12,
9260 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9261 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009262 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009263 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009264 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009265 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009266 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009267 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009268 /*
9269 * The controls for "virtualize APIC accesses," "APIC-
9270 * register virtualization," and "virtual-interrupt
9271 * delivery" only come from vmcs12.
9272 */
Joe Perches1d804d02015-03-30 16:46:09 -07009273 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009274 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009275 /*
9276 * L0 always deals with the EPT violation. If nested EPT is
9277 * used, and the nested mmu code discovers that the address is
9278 * missing in the guest EPT table (EPT12), the EPT violation
9279 * will be injected with nested_ept_inject_page_fault()
9280 */
Joe Perches1d804d02015-03-30 16:46:09 -07009281 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009282 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009283 /*
9284 * L2 never uses directly L1's EPT, but rather L0's own EPT
9285 * table (shadow on EPT) or a merged EPT table that L0 built
9286 * (EPT on EPT). So any problems with the structure of the
9287 * table is L0's fault.
9288 */
Joe Perches1d804d02015-03-30 16:46:09 -07009289 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009290 case EXIT_REASON_INVPCID:
9291 return
9292 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9293 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009294 case EXIT_REASON_WBINVD:
9295 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9296 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009297 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009298 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9299 /*
9300 * This should never happen, since it is not possible to
9301 * set XSS to a non-zero value---neither in L1 nor in L2.
9302 * If if it were, XSS would have to be checked against
9303 * the XSS exit bitmap in vmcs12.
9304 */
9305 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009306 case EXIT_REASON_PREEMPTION_TIMER:
9307 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009308 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009309 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009310 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009311 case EXIT_REASON_VMFUNC:
9312 /* VM functions are emulated through L2->L0 vmexits. */
9313 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009314 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009315 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009316 }
9317}
9318
Paolo Bonzini7313c692017-07-27 10:31:25 +02009319static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9320{
9321 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9322
9323 /*
9324 * At this point, the exit interruption info in exit_intr_info
9325 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9326 * we need to query the in-kernel LAPIC.
9327 */
9328 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9329 if ((exit_intr_info &
9330 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9331 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9332 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9333 vmcs12->vm_exit_intr_error_code =
9334 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9335 }
9336
9337 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9338 vmcs_readl(EXIT_QUALIFICATION));
9339 return 1;
9340}
9341
Avi Kivity586f9602010-11-18 13:09:54 +02009342static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9343{
9344 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9345 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9346}
9347
Kai Huanga3eaa862015-11-04 13:46:05 +08009348static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009349{
Kai Huanga3eaa862015-11-04 13:46:05 +08009350 if (vmx->pml_pg) {
9351 __free_page(vmx->pml_pg);
9352 vmx->pml_pg = NULL;
9353 }
Kai Huang843e4332015-01-28 10:54:28 +08009354}
9355
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009356static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009357{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009358 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009359 u64 *pml_buf;
9360 u16 pml_idx;
9361
9362 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9363
9364 /* Do nothing if PML buffer is empty */
9365 if (pml_idx == (PML_ENTITY_NUM - 1))
9366 return;
9367
9368 /* PML index always points to next available PML buffer entity */
9369 if (pml_idx >= PML_ENTITY_NUM)
9370 pml_idx = 0;
9371 else
9372 pml_idx++;
9373
9374 pml_buf = page_address(vmx->pml_pg);
9375 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9376 u64 gpa;
9377
9378 gpa = pml_buf[pml_idx];
9379 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009380 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009381 }
9382
9383 /* reset PML index */
9384 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9385}
9386
9387/*
9388 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9389 * Called before reporting dirty_bitmap to userspace.
9390 */
9391static void kvm_flush_pml_buffers(struct kvm *kvm)
9392{
9393 int i;
9394 struct kvm_vcpu *vcpu;
9395 /*
9396 * We only need to kick vcpu out of guest mode here, as PML buffer
9397 * is flushed at beginning of all VMEXITs, and it's obvious that only
9398 * vcpus running in guest are possible to have unflushed GPAs in PML
9399 * buffer.
9400 */
9401 kvm_for_each_vcpu(i, vcpu, kvm)
9402 kvm_vcpu_kick(vcpu);
9403}
9404
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009405static void vmx_dump_sel(char *name, uint32_t sel)
9406{
9407 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009408 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009409 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9410 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9411 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9412}
9413
9414static void vmx_dump_dtsel(char *name, uint32_t limit)
9415{
9416 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9417 name, vmcs_read32(limit),
9418 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9419}
9420
9421static void dump_vmcs(void)
9422{
9423 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9424 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9425 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9426 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9427 u32 secondary_exec_control = 0;
9428 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009429 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009430 int i, n;
9431
9432 if (cpu_has_secondary_exec_ctrls())
9433 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9434
9435 pr_err("*** Guest State ***\n");
9436 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9437 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9438 vmcs_readl(CR0_GUEST_HOST_MASK));
9439 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9440 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9441 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9442 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9443 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9444 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009445 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9446 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9447 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9448 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009449 }
9450 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9451 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9452 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9453 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9454 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9455 vmcs_readl(GUEST_SYSENTER_ESP),
9456 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9457 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9458 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9459 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9460 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9461 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9462 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9463 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9464 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9465 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9466 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9467 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9468 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009469 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9470 efer, vmcs_read64(GUEST_IA32_PAT));
9471 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9472 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009473 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009474 if (cpu_has_load_perf_global_ctrl &&
9475 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009476 pr_err("PerfGlobCtl = 0x%016llx\n",
9477 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009478 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009479 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009480 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9481 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9482 vmcs_read32(GUEST_ACTIVITY_STATE));
9483 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9484 pr_err("InterruptStatus = %04x\n",
9485 vmcs_read16(GUEST_INTR_STATUS));
9486
9487 pr_err("*** Host State ***\n");
9488 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9489 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9490 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9491 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9492 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9493 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9494 vmcs_read16(HOST_TR_SELECTOR));
9495 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9496 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9497 vmcs_readl(HOST_TR_BASE));
9498 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9499 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9500 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9501 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9502 vmcs_readl(HOST_CR4));
9503 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9504 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9505 vmcs_read32(HOST_IA32_SYSENTER_CS),
9506 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9507 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009508 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9509 vmcs_read64(HOST_IA32_EFER),
9510 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009511 if (cpu_has_load_perf_global_ctrl &&
9512 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009513 pr_err("PerfGlobCtl = 0x%016llx\n",
9514 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009515
9516 pr_err("*** Control State ***\n");
9517 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9518 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9519 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9520 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9521 vmcs_read32(EXCEPTION_BITMAP),
9522 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9523 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9524 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9525 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9526 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9527 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9528 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9529 vmcs_read32(VM_EXIT_INTR_INFO),
9530 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9531 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9532 pr_err(" reason=%08x qualification=%016lx\n",
9533 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9534 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9535 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9536 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009537 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009538 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009539 pr_err("TSC Multiplier = 0x%016llx\n",
9540 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009541 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9542 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9543 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9544 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9545 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009546 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009547 n = vmcs_read32(CR3_TARGET_COUNT);
9548 for (i = 0; i + 1 < n; i += 4)
9549 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9550 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9551 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9552 if (i < n)
9553 pr_err("CR3 target%u=%016lx\n",
9554 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9555 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9556 pr_err("PLE Gap=%08x Window=%08x\n",
9557 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9558 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9559 pr_err("Virtual processor ID = 0x%04x\n",
9560 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9561}
9562
Avi Kivity6aa8b732006-12-10 02:21:36 -08009563/*
9564 * The guest has exited. See if we can fix it or if we need userspace
9565 * assistance.
9566 */
Avi Kivity851ba692009-08-24 11:10:17 +03009567static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009568{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009569 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009570 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009571 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009572
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009573 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9574
Kai Huang843e4332015-01-28 10:54:28 +08009575 /*
9576 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9577 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9578 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9579 * mode as if vcpus is in root mode, the PML buffer must has been
9580 * flushed already.
9581 */
9582 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009583 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009584
Mohammed Gamal80ced182009-09-01 12:48:18 +02009585 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009586 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009587 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009588
Paolo Bonzini7313c692017-07-27 10:31:25 +02009589 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9590 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009591
Mohammed Gamal51207022010-05-31 22:40:54 +03009592 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009593 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009594 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9595 vcpu->run->fail_entry.hardware_entry_failure_reason
9596 = exit_reason;
9597 return 0;
9598 }
9599
Avi Kivity29bd8a72007-09-10 17:27:03 +03009600 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009601 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9602 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009603 = vmcs_read32(VM_INSTRUCTION_ERROR);
9604 return 0;
9605 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009606
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009607 /*
9608 * Note:
9609 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9610 * delivery event since it indicates guest is accessing MMIO.
9611 * The vm-exit can be triggered again after return to guest that
9612 * will cause infinite loop.
9613 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009614 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009615 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009616 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009617 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009618 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9619 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9620 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009621 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009622 vcpu->run->internal.data[0] = vectoring_info;
9623 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009624 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9625 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9626 vcpu->run->internal.ndata++;
9627 vcpu->run->internal.data[3] =
9628 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9629 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009630 return 0;
9631 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009632
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009633 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009634 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9635 if (vmx_interrupt_allowed(vcpu)) {
9636 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9637 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9638 vcpu->arch.nmi_pending) {
9639 /*
9640 * This CPU don't support us in finding the end of an
9641 * NMI-blocked window if the guest runs with IRQs
9642 * disabled. So we pull the trigger after 1 s of
9643 * futile waiting, but inform the user about this.
9644 */
9645 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9646 "state on VCPU %d after 1 s timeout\n",
9647 __func__, vcpu->vcpu_id);
9648 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9649 }
9650 }
9651
Avi Kivity6aa8b732006-12-10 02:21:36 -08009652 if (exit_reason < kvm_vmx_max_exit_handlers
9653 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009654 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009655 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009656 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9657 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009658 kvm_queue_exception(vcpu, UD_VECTOR);
9659 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009660 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009661}
9662
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009663/*
9664 * Software based L1D cache flush which is used when microcode providing
9665 * the cache control MSR is not loaded.
9666 *
9667 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9668 * flush it is required to read in 64 KiB because the replacement algorithm
9669 * is not exactly LRU. This could be sized at runtime via topology
9670 * information but as all relevant affected CPUs have 32KiB L1D cache size
9671 * there is no point in doing so.
9672 */
9673#define L1D_CACHE_ORDER 4
9674static void *vmx_l1d_flush_pages;
9675
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009676static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009677{
9678 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009679
9680 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009681 * This code is only executed when the the flush mode is 'cond' or
9682 * 'always'
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009683 *
Thomas Gleixner2f055942018-07-13 16:23:17 +02009684 * If 'flush always', keep the flush bit set, otherwise clear
9685 * it. The flush bit gets set again either from vcpu_run() or from
9686 * one of the unsafe VMEXIT handlers.
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009687 */
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02009688 if (static_branch_unlikely(&vmx_l1d_flush_always))
9689 vcpu->arch.l1tf_flush_l1d = true;
9690 else
9691 vcpu->arch.l1tf_flush_l1d = false;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009692
9693 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009694
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009695 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9696 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9697 return;
9698 }
9699
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009700 asm volatile(
9701 /* First ensure the pages are in the TLB */
9702 "xorl %%eax, %%eax\n"
9703 ".Lpopulate_tlb:\n\t"
9704 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9705 "addl $4096, %%eax\n\t"
9706 "cmpl %%eax, %[size]\n\t"
9707 "jne .Lpopulate_tlb\n\t"
9708 "xorl %%eax, %%eax\n\t"
9709 "cpuid\n\t"
9710 /* Now fill the cache */
9711 "xorl %%eax, %%eax\n"
9712 ".Lfill_cache:\n"
9713 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9714 "addl $64, %%eax\n\t"
9715 "cmpl %%eax, %[size]\n\t"
9716 "jne .Lfill_cache\n\t"
9717 "lfence\n"
9718 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9719 [size] "r" (size)
9720 : "eax", "ebx", "ecx", "edx");
9721}
9722
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009723static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009724{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009725 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9726
9727 if (is_guest_mode(vcpu) &&
9728 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9729 return;
9730
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009731 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009732 vmcs_write32(TPR_THRESHOLD, 0);
9733 return;
9734 }
9735
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009736 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009737}
9738
Jim Mattson8d860bb2018-05-09 16:56:05 -04009739static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009740{
9741 u32 sec_exec_control;
9742
Jim Mattson8d860bb2018-05-09 16:56:05 -04009743 if (!lapic_in_kernel(vcpu))
9744 return;
9745
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009746 /* Postpone execution until vmcs01 is the current VMCS. */
9747 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009748 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009749 return;
9750 }
9751
Paolo Bonzini35754c92015-07-29 12:05:37 +02009752 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009753 return;
9754
9755 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009756 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9757 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009758
Jim Mattson8d860bb2018-05-09 16:56:05 -04009759 switch (kvm_get_apic_mode(vcpu)) {
9760 case LAPIC_MODE_INVALID:
9761 WARN_ONCE(true, "Invalid local APIC state");
9762 case LAPIC_MODE_DISABLED:
9763 break;
9764 case LAPIC_MODE_XAPIC:
9765 if (flexpriority_enabled) {
9766 sec_exec_control |=
9767 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9768 vmx_flush_tlb(vcpu, true);
9769 }
9770 break;
9771 case LAPIC_MODE_X2APIC:
9772 if (cpu_has_vmx_virtualize_x2apic_mode())
9773 sec_exec_control |=
9774 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9775 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009776 }
9777 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9778
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009779 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009780}
9781
Tang Chen38b99172014-09-24 15:57:54 +08009782static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9783{
Jim Mattsonab5df312018-05-09 17:02:03 -04009784 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009785 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009786 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009787 }
Tang Chen38b99172014-09-24 15:57:54 +08009788}
9789
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009790static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009791{
9792 u16 status;
9793 u8 old;
9794
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009795 if (max_isr == -1)
9796 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009797
9798 status = vmcs_read16(GUEST_INTR_STATUS);
9799 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009800 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009801 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009802 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009803 vmcs_write16(GUEST_INTR_STATUS, status);
9804 }
9805}
9806
9807static void vmx_set_rvi(int vector)
9808{
9809 u16 status;
9810 u8 old;
9811
Wei Wang4114c272014-11-05 10:53:43 +08009812 if (vector == -1)
9813 vector = 0;
9814
Yang Zhangc7c9c562013-01-25 10:18:51 +08009815 status = vmcs_read16(GUEST_INTR_STATUS);
9816 old = (u8)status & 0xff;
9817 if ((u8)vector != old) {
9818 status &= ~0xff;
9819 status |= (u8)vector;
9820 vmcs_write16(GUEST_INTR_STATUS, status);
9821 }
9822}
9823
9824static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9825{
Liran Alon851c1a182017-12-24 18:12:56 +02009826 /*
9827 * When running L2, updating RVI is only relevant when
9828 * vmcs12 virtual-interrupt-delivery enabled.
9829 * However, it can be enabled only when L1 also
9830 * intercepts external-interrupts and in that case
9831 * we should not update vmcs02 RVI but instead intercept
9832 * interrupt. Therefore, do nothing when running L2.
9833 */
9834 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009835 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009836}
9837
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009838static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009839{
9840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009841 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009842 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009843
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009844 WARN_ON(!vcpu->arch.apicv_active);
9845 if (pi_test_on(&vmx->pi_desc)) {
9846 pi_clear_on(&vmx->pi_desc);
9847 /*
9848 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9849 * But on x86 this is just a compiler barrier anyway.
9850 */
9851 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009852 max_irr_updated =
9853 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9854
9855 /*
9856 * If we are running L2 and L1 has a new pending interrupt
9857 * which can be injected, we should re-evaluate
9858 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009859 * If L1 intercepts external-interrupts, we should
9860 * exit from L2 to L1. Otherwise, interrupt should be
9861 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009862 */
Liran Alon851c1a182017-12-24 18:12:56 +02009863 if (is_guest_mode(vcpu) && max_irr_updated) {
9864 if (nested_exit_on_intr(vcpu))
9865 kvm_vcpu_exiting_guest_mode(vcpu);
9866 else
9867 kvm_make_request(KVM_REQ_EVENT, vcpu);
9868 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009869 } else {
9870 max_irr = kvm_lapic_find_highest_irr(vcpu);
9871 }
9872 vmx_hwapic_irr_update(vcpu, max_irr);
9873 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009874}
9875
Andrey Smetanin63086302015-11-10 15:36:32 +03009876static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009877{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009878 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009879 return;
9880
Yang Zhangc7c9c562013-01-25 10:18:51 +08009881 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9882 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9883 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9884 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9885}
9886
Paolo Bonzini967235d2016-12-19 14:03:45 +01009887static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9888{
9889 struct vcpu_vmx *vmx = to_vmx(vcpu);
9890
9891 pi_clear_on(&vmx->pi_desc);
9892 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9893}
9894
Avi Kivity51aa01d2010-07-20 14:31:20 +03009895static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009896{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009897 u32 exit_intr_info = 0;
9898 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009899
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009900 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9901 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009902 return;
9903
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009904 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9905 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9906 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009907
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009908 /* if exit due to PF check for async PF */
9909 if (is_page_fault(exit_intr_info))
9910 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9911
Andi Kleena0861c02009-06-08 17:37:09 +08009912 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009913 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9914 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009915 kvm_machine_check();
9916
Gleb Natapov20f65982009-05-11 13:35:55 +03009917 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009918 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009919 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009920 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009921 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009922 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009923}
Gleb Natapov20f65982009-05-11 13:35:55 +03009924
Yang Zhanga547c6d2013-04-11 19:25:10 +08009925static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9926{
9927 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9928
Yang Zhanga547c6d2013-04-11 19:25:10 +08009929 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9930 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9931 unsigned int vector;
9932 unsigned long entry;
9933 gate_desc *desc;
9934 struct vcpu_vmx *vmx = to_vmx(vcpu);
9935#ifdef CONFIG_X86_64
9936 unsigned long tmp;
9937#endif
9938
9939 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9940 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009941 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009942 asm volatile(
9943#ifdef CONFIG_X86_64
9944 "mov %%" _ASM_SP ", %[sp]\n\t"
9945 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9946 "push $%c[ss]\n\t"
9947 "push %[sp]\n\t"
9948#endif
9949 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009950 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009951 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009952 :
9953#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009954 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009955#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009956 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009957 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009958 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009959 [ss]"i"(__KERNEL_DS),
9960 [cs]"i"(__KERNEL_CS)
9961 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009962 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009963 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009964}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009965STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009966
Tom Lendackybc226f02018-05-10 22:06:39 +02009967static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009968{
Tom Lendackybc226f02018-05-10 22:06:39 +02009969 switch (index) {
9970 case MSR_IA32_SMBASE:
9971 /*
9972 * We cannot do SMM unless we can run the guest in big
9973 * real mode.
9974 */
9975 return enable_unrestricted_guest || emulate_invalid_guest_state;
9976 case MSR_AMD64_VIRT_SPEC_CTRL:
9977 /* This is AMD only. */
9978 return false;
9979 default:
9980 return true;
9981 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009982}
9983
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009984static bool vmx_mpx_supported(void)
9985{
9986 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9987 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9988}
9989
Wanpeng Li55412b22014-12-02 19:21:30 +08009990static bool vmx_xsaves_supported(void)
9991{
9992 return vmcs_config.cpu_based_2nd_exec_ctrl &
9993 SECONDARY_EXEC_XSAVES;
9994}
9995
Avi Kivity51aa01d2010-07-20 14:31:20 +03009996static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9997{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009998 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009999 bool unblock_nmi;
10000 u8 vector;
10001 bool idtv_info_valid;
10002
10003 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010004
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010005 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010006 if (vmx->loaded_vmcs->nmi_known_unmasked)
10007 return;
10008 /*
10009 * Can't use vmx->exit_intr_info since we're not sure what
10010 * the exit reason is.
10011 */
10012 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10013 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10014 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10015 /*
10016 * SDM 3: 27.7.1.2 (September 2008)
10017 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10018 * a guest IRET fault.
10019 * SDM 3: 23.2.2 (September 2008)
10020 * Bit 12 is undefined in any of the following cases:
10021 * If the VM exit sets the valid bit in the IDT-vectoring
10022 * information field.
10023 * If the VM exit is due to a double fault.
10024 */
10025 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10026 vector != DF_VECTOR && !idtv_info_valid)
10027 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10028 GUEST_INTR_STATE_NMI);
10029 else
10030 vmx->loaded_vmcs->nmi_known_unmasked =
10031 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10032 & GUEST_INTR_STATE_NMI);
10033 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10034 vmx->loaded_vmcs->vnmi_blocked_time +=
10035 ktime_to_ns(ktime_sub(ktime_get(),
10036 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010037}
10038
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010039static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010040 u32 idt_vectoring_info,
10041 int instr_len_field,
10042 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010043{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010044 u8 vector;
10045 int type;
10046 bool idtv_info_valid;
10047
10048 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010049
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010050 vcpu->arch.nmi_injected = false;
10051 kvm_clear_exception_queue(vcpu);
10052 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010053
10054 if (!idtv_info_valid)
10055 return;
10056
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010057 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010058
Avi Kivity668f6122008-07-02 09:28:55 +030010059 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10060 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010061
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010062 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010063 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010064 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010065 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010066 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010067 * Clear bit "block by NMI" before VM entry if a NMI
10068 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010069 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010070 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010071 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010072 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010073 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010074 /* fall through */
10075 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010076 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010077 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010078 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010079 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010080 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010081 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010082 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010083 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010084 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010085 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010086 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010087 break;
10088 default:
10089 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010090 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010091}
10092
Avi Kivity83422e12010-07-20 14:43:23 +030010093static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10094{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010095 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010096 VM_EXIT_INSTRUCTION_LEN,
10097 IDT_VECTORING_ERROR_CODE);
10098}
10099
Avi Kivityb463a6f2010-07-20 15:06:17 +030010100static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10101{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010102 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010103 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10104 VM_ENTRY_INSTRUCTION_LEN,
10105 VM_ENTRY_EXCEPTION_ERROR_CODE);
10106
10107 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10108}
10109
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010110static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10111{
10112 int i, nr_msrs;
10113 struct perf_guest_switch_msr *msrs;
10114
10115 msrs = perf_guest_get_msrs(&nr_msrs);
10116
10117 if (!msrs)
10118 return;
10119
10120 for (i = 0; i < nr_msrs; i++)
10121 if (msrs[i].host == msrs[i].guest)
10122 clear_atomic_switch_msr(vmx, msrs[i].msr);
10123 else
10124 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010125 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010126}
10127
Jiang Biao33365e72016-11-03 15:03:37 +080010128static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010129{
10130 struct vcpu_vmx *vmx = to_vmx(vcpu);
10131 u64 tscl;
10132 u32 delta_tsc;
10133
10134 if (vmx->hv_deadline_tsc == -1)
10135 return;
10136
10137 tscl = rdtsc();
10138 if (vmx->hv_deadline_tsc > tscl)
10139 /* sure to be 32 bit only because checked on set_hv_timer */
10140 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10141 cpu_preemption_timer_multi);
10142 else
10143 delta_tsc = 0;
10144
10145 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10146}
10147
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010148static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010149{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010150 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010151 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010152
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010153 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010154 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010155 vmx->loaded_vmcs->soft_vnmi_blocked))
10156 vmx->loaded_vmcs->entry_time = ktime_get();
10157
Avi Kivity104f2262010-11-18 13:12:52 +020010158 /* Don't enter VMX if guest state is invalid, let the exit handler
10159 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010160 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010161 return;
10162
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010163 if (vmx->ple_window_dirty) {
10164 vmx->ple_window_dirty = false;
10165 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10166 }
10167
Abel Gordon012f83c2013-04-18 14:39:25 +030010168 if (vmx->nested.sync_shadow_vmcs) {
10169 copy_vmcs12_to_shadow(vmx);
10170 vmx->nested.sync_shadow_vmcs = false;
10171 }
10172
Avi Kivity104f2262010-11-18 13:12:52 +020010173 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10174 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10175 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10176 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10177
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010178 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010179 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010180 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010181 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010182 }
10183
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010184 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010185 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010186 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010187 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010188 }
10189
Avi Kivity104f2262010-11-18 13:12:52 +020010190 /* When single-stepping over STI and MOV SS, we must clear the
10191 * corresponding interruptibility bits in the guest state. Otherwise
10192 * vmentry fails as it then expects bit 14 (BS) in pending debug
10193 * exceptions being set, but that's not correct for the guest debugging
10194 * case. */
10195 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10196 vmx_set_interrupt_shadow(vcpu, 0);
10197
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010198 if (static_cpu_has(X86_FEATURE_PKU) &&
10199 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10200 vcpu->arch.pkru != vmx->host_pkru)
10201 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010202
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010203 atomic_switch_perf_msrs(vmx);
10204
Yunhong Jiang64672c92016-06-13 14:19:59 -070010205 vmx_arm_hv_timer(vcpu);
10206
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010207 /*
10208 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10209 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10210 * is no need to worry about the conditional branch over the wrmsr
10211 * being speculatively taken.
10212 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010213 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010214
Nadav Har'Eld462b812011-05-24 15:26:10 +030010215 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010216
10217 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10218 (unsigned long)&current_evmcs->host_rsp : 0;
10219
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010220 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10221 if (vcpu->arch.l1tf_flush_l1d)
10222 vmx_l1d_flush(vcpu);
10223 }
10224
Avi Kivity104f2262010-11-18 13:12:52 +020010225 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010226 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010227 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10228 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10229 "push %%" _ASM_CX " \n\t"
10230 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010231 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010232 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010233 /* Avoid VMWRITE when Enlightened VMCS is in use */
10234 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10235 "jz 2f \n\t"
10236 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10237 "jmp 1f \n\t"
10238 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010239 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010240 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010241 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010242 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10243 "mov %%cr2, %%" _ASM_DX " \n\t"
10244 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010245 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010246 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010247 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010248 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010249 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010250 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010251 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10252 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10253 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10254 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10255 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10256 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010257#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010258 "mov %c[r8](%0), %%r8 \n\t"
10259 "mov %c[r9](%0), %%r9 \n\t"
10260 "mov %c[r10](%0), %%r10 \n\t"
10261 "mov %c[r11](%0), %%r11 \n\t"
10262 "mov %c[r12](%0), %%r12 \n\t"
10263 "mov %c[r13](%0), %%r13 \n\t"
10264 "mov %c[r14](%0), %%r14 \n\t"
10265 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010266#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010267 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010268
Avi Kivity6aa8b732006-12-10 02:21:36 -080010269 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010270 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010271 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010272 "jmp 2f \n\t"
10273 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10274 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010275 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010276 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010277 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010278 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010279 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10280 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10281 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10282 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10283 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10284 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10285 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010286#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010287 "mov %%r8, %c[r8](%0) \n\t"
10288 "mov %%r9, %c[r9](%0) \n\t"
10289 "mov %%r10, %c[r10](%0) \n\t"
10290 "mov %%r11, %c[r11](%0) \n\t"
10291 "mov %%r12, %c[r12](%0) \n\t"
10292 "mov %%r13, %c[r13](%0) \n\t"
10293 "mov %%r14, %c[r14](%0) \n\t"
10294 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010295 "xor %%r8d, %%r8d \n\t"
10296 "xor %%r9d, %%r9d \n\t"
10297 "xor %%r10d, %%r10d \n\t"
10298 "xor %%r11d, %%r11d \n\t"
10299 "xor %%r12d, %%r12d \n\t"
10300 "xor %%r13d, %%r13d \n\t"
10301 "xor %%r14d, %%r14d \n\t"
10302 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010303#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010304 "mov %%cr2, %%" _ASM_AX " \n\t"
10305 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010306
Jim Mattson0cb5b302018-01-03 14:31:38 -080010307 "xor %%eax, %%eax \n\t"
10308 "xor %%ebx, %%ebx \n\t"
10309 "xor %%esi, %%esi \n\t"
10310 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010311 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010312 ".pushsection .rodata \n\t"
10313 ".global vmx_return \n\t"
10314 "vmx_return: " _ASM_PTR " 2b \n\t"
10315 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010316 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010317 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010318 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010319 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010320 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10321 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10322 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10323 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10324 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10325 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10326 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010327#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010328 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10329 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10330 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10331 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10332 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10333 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10334 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10335 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010336#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010337 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10338 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010339 : "cc", "memory"
10340#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010341 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010342 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010343#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010344 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010345#endif
10346 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010347
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010348 /*
10349 * We do not use IBRS in the kernel. If this vCPU has used the
10350 * SPEC_CTRL MSR it may have left it on; save the value and
10351 * turn it off. This is much more efficient than blindly adding
10352 * it to the atomic save/restore list. Especially as the former
10353 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10354 *
10355 * For non-nested case:
10356 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10357 * save it.
10358 *
10359 * For nested case:
10360 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10361 * save it.
10362 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010363 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010364 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010365
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010366 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010367
David Woodhouse117cc7a2018-01-12 11:11:27 +000010368 /* Eliminate branch target predictions from guest mode */
10369 vmexit_fill_RSB();
10370
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010371 /* All fields are clean at this point */
10372 if (static_branch_unlikely(&enable_evmcs))
10373 current_evmcs->hv_clean_fields |=
10374 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10375
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010376 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010377 if (vmx->host_debugctlmsr)
10378 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010379
Avi Kivityaa67f602012-08-01 16:48:03 +030010380#ifndef CONFIG_X86_64
10381 /*
10382 * The sysexit path does not restore ds/es, so we must set them to
10383 * a reasonable value ourselves.
10384 *
10385 * We can't defer this to vmx_load_host_state() since that function
10386 * may be executed in interrupt context, which saves and restore segments
10387 * around it, nullifying its effect.
10388 */
10389 loadsegment(ds, __USER_DS);
10390 loadsegment(es, __USER_DS);
10391#endif
10392
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010393 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010394 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010395 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010396 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010397 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010398 vcpu->arch.regs_dirty = 0;
10399
Gleb Natapove0b890d2013-09-25 12:51:33 +030010400 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010401 * eager fpu is enabled if PKEY is supported and CR4 is switched
10402 * back on host, so it is safe to read guest PKRU from current
10403 * XSAVE.
10404 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010405 if (static_cpu_has(X86_FEATURE_PKU) &&
10406 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10407 vcpu->arch.pkru = __read_pkru();
10408 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010409 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010410 }
10411
Gleb Natapove0b890d2013-09-25 12:51:33 +030010412 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010413 vmx->idt_vectoring_info = 0;
10414
10415 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10416 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10417 return;
10418
10419 vmx->loaded_vmcs->launched = 1;
10420 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010421
Avi Kivity51aa01d2010-07-20 14:31:20 +030010422 vmx_complete_atomic_exit(vmx);
10423 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010424 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010425}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010426STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010427
Sean Christopherson434a1e92018-03-20 12:17:18 -070010428static struct kvm *vmx_vm_alloc(void)
10429{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010430 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010431 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010432}
10433
10434static void vmx_vm_free(struct kvm *kvm)
10435{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010436 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010437}
10438
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010439static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010440{
10441 struct vcpu_vmx *vmx = to_vmx(vcpu);
10442 int cpu;
10443
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010444 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010445 return;
10446
10447 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010448 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010449 vmx_vcpu_put(vcpu);
10450 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010451 put_cpu();
10452}
10453
Jim Mattson2f1fe812016-07-08 15:36:06 -070010454/*
10455 * Ensure that the current vmcs of the logical processor is the
10456 * vmcs01 of the vcpu before calling free_nested().
10457 */
10458static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10459{
10460 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010461
Christoffer Dallec7660c2017-12-04 21:35:23 +010010462 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010463 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010464 free_nested(vmx);
10465 vcpu_put(vcpu);
10466}
10467
Avi Kivity6aa8b732006-12-10 02:21:36 -080010468static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10469{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010470 struct vcpu_vmx *vmx = to_vmx(vcpu);
10471
Kai Huang843e4332015-01-28 10:54:28 +080010472 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010473 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010474 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010475 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010476 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010477 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010478 kfree(vmx->guest_msrs);
10479 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010480 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010481}
10482
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010483static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010484{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010485 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010486 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010487 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010488 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010489
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010490 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010491 return ERR_PTR(-ENOMEM);
10492
Wanpeng Li991e7a02015-09-16 17:30:05 +080010493 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010494
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010495 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10496 if (err)
10497 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010498
Peter Feiner4e595162016-07-07 14:49:58 -070010499 err = -ENOMEM;
10500
10501 /*
10502 * If PML is turned on, failure on enabling PML just results in failure
10503 * of creating the vcpu, therefore we can simplify PML logic (by
10504 * avoiding dealing with cases, such as enabling PML partially on vcpus
10505 * for the guest, etc.
10506 */
10507 if (enable_pml) {
10508 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10509 if (!vmx->pml_pg)
10510 goto uninit_vcpu;
10511 }
10512
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010513 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010514 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10515 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010516
Peter Feiner4e595162016-07-07 14:49:58 -070010517 if (!vmx->guest_msrs)
10518 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010519
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010520 err = alloc_loaded_vmcs(&vmx->vmcs01);
10521 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010522 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010523
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010524 msr_bitmap = vmx->vmcs01.msr_bitmap;
10525 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10526 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10527 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10528 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10529 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10530 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10531 vmx->msr_bitmap_mode = 0;
10532
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010533 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010534 cpu = get_cpu();
10535 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010536 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010537 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010538 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010539 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010540 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010541 err = alloc_apic_access_page(kvm);
10542 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010543 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010544 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010545
Sean Christophersone90008d2018-03-05 12:04:37 -080010546 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010547 err = init_rmode_identity_map(kvm);
10548 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010549 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010550 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010551
Wanpeng Li5c614b32015-10-13 09:18:36 -070010552 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010553 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10554 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010555 vmx->nested.vpid02 = allocate_vpid();
10556 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010557
Wincy Van705699a2015-02-03 23:58:17 +080010558 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010559 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010560
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010561 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10562
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010563 /*
10564 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10565 * or POSTED_INTR_WAKEUP_VECTOR.
10566 */
10567 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10568 vmx->pi_desc.sn = 1;
10569
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010570 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010571
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010572free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010573 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010574 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010575free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010576 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010577free_pml:
10578 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010579uninit_vcpu:
10580 kvm_vcpu_uninit(&vmx->vcpu);
10581free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010582 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010583 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010584 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010585}
10586
Jiri Kosinad90a7a02018-07-13 16:23:25 +020010587#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"
10588#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 -040010589
Wanpeng Lib31c1142018-03-12 04:53:04 -070010590static int vmx_vm_init(struct kvm *kvm)
10591{
10592 if (!ple_gap)
10593 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010594
Jiri Kosinad90a7a02018-07-13 16:23:25 +020010595 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
10596 switch (l1tf_mitigation) {
10597 case L1TF_MITIGATION_OFF:
10598 case L1TF_MITIGATION_FLUSH_NOWARN:
10599 /* 'I explicitly don't care' is set */
10600 break;
10601 case L1TF_MITIGATION_FLUSH:
10602 case L1TF_MITIGATION_FLUSH_NOSMT:
10603 case L1TF_MITIGATION_FULL:
10604 /*
10605 * Warn upon starting the first VM in a potentially
10606 * insecure environment.
10607 */
10608 if (cpu_smt_control == CPU_SMT_ENABLED)
10609 pr_warn_once(L1TF_MSG_SMT);
10610 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
10611 pr_warn_once(L1TF_MSG_L1D);
10612 break;
10613 case L1TF_MITIGATION_FULL_FORCE:
10614 /* Flush is enforced */
10615 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010616 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010617 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010618 return 0;
10619}
10620
Yang, Sheng002c7f72007-07-31 14:23:01 +030010621static void __init vmx_check_processor_compat(void *rtn)
10622{
10623 struct vmcs_config vmcs_conf;
10624
10625 *(int *)rtn = 0;
10626 if (setup_vmcs_config(&vmcs_conf) < 0)
10627 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010628 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010629 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10630 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10631 smp_processor_id());
10632 *(int *)rtn = -EIO;
10633 }
10634}
10635
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010636static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010637{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010638 u8 cache;
10639 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010640
Sheng Yang522c68c2009-04-27 20:35:43 +080010641 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010642 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010643 * 2. EPT with VT-d:
10644 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010645 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010646 * b. VT-d with snooping control feature: snooping control feature of
10647 * VT-d engine can guarantee the cache correctness. Just set it
10648 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010649 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010650 * consistent with host MTRR
10651 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010652 if (is_mmio) {
10653 cache = MTRR_TYPE_UNCACHABLE;
10654 goto exit;
10655 }
10656
10657 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010658 ipat = VMX_EPT_IPAT_BIT;
10659 cache = MTRR_TYPE_WRBACK;
10660 goto exit;
10661 }
10662
10663 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10664 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010665 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010666 cache = MTRR_TYPE_WRBACK;
10667 else
10668 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010669 goto exit;
10670 }
10671
Xiao Guangrongff536042015-06-15 16:55:22 +080010672 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010673
10674exit:
10675 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010676}
10677
Sheng Yang17cc3932010-01-05 19:02:27 +080010678static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010679{
Sheng Yang878403b2010-01-05 19:02:29 +080010680 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10681 return PT_DIRECTORY_LEVEL;
10682 else
10683 /* For shadow and EPT supported 1GB page */
10684 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010685}
10686
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010687static void vmcs_set_secondary_exec_control(u32 new_ctl)
10688{
10689 /*
10690 * These bits in the secondary execution controls field
10691 * are dynamic, the others are mostly based on the hypervisor
10692 * architecture and the guest's CPUID. Do not touch the
10693 * dynamic bits.
10694 */
10695 u32 mask =
10696 SECONDARY_EXEC_SHADOW_VMCS |
10697 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010698 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10699 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010700
10701 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10702
10703 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10704 (new_ctl & ~mask) | (cur_ctl & mask));
10705}
10706
David Matlack8322ebb2016-11-29 18:14:09 -080010707/*
10708 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10709 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10710 */
10711static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10712{
10713 struct vcpu_vmx *vmx = to_vmx(vcpu);
10714 struct kvm_cpuid_entry2 *entry;
10715
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010716 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10717 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010718
10719#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10720 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010721 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010722} while (0)
10723
10724 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10725 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10726 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10727 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10728 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10729 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10730 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10731 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10732 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10733 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10734 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10735 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10736 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10737 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10738 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10739
10740 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10741 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10742 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10743 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10744 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010745 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010746
10747#undef cr4_fixed1_update
10748}
10749
Sheng Yang0e851882009-12-18 16:48:46 +080010750static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10751{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010752 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010753
Paolo Bonzini80154d72017-08-24 13:55:35 +020010754 if (cpu_has_secondary_exec_ctrls()) {
10755 vmx_compute_secondary_exec_control(vmx);
10756 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010757 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010758
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010759 if (nested_vmx_allowed(vcpu))
10760 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10761 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10762 else
10763 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10764 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010765
10766 if (nested_vmx_allowed(vcpu))
10767 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010768}
10769
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010770static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10771{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010772 if (func == 1 && nested)
10773 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010774}
10775
Yang Zhang25d92082013-08-06 12:00:32 +030010776static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10777 struct x86_exception *fault)
10778{
Jan Kiszka533558b2014-01-04 18:47:20 +010010779 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010781 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010782 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010783
Bandan Dasc5f983f2017-05-05 15:25:14 -040010784 if (vmx->nested.pml_full) {
10785 exit_reason = EXIT_REASON_PML_FULL;
10786 vmx->nested.pml_full = false;
10787 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10788 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010789 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010790 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010791 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010792
10793 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010794 vmcs12->guest_physical_address = fault->address;
10795}
10796
Peter Feiner995f00a2017-06-30 17:26:32 -070010797static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10798{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010799 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010800}
10801
Nadav Har'El155a97a2013-08-05 11:07:16 +030010802/* Callbacks for nested_ept_init_mmu_context: */
10803
10804static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10805{
10806 /* return the page table to be shadowed - in our case, EPT12 */
10807 return get_vmcs12(vcpu)->ept_pointer;
10808}
10809
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010810static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010811{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010812 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010813 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010814 return 1;
10815
10816 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010817 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010818 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010819 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010820 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010821 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10822 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10823 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10824
10825 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010826 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010827}
10828
10829static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10830{
10831 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10832}
10833
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010834static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10835 u16 error_code)
10836{
10837 bool inequality, bit;
10838
10839 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10840 inequality =
10841 (error_code & vmcs12->page_fault_error_code_mask) !=
10842 vmcs12->page_fault_error_code_match;
10843 return inequality ^ bit;
10844}
10845
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010846static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10847 struct x86_exception *fault)
10848{
10849 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10850
10851 WARN_ON(!is_guest_mode(vcpu));
10852
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010853 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10854 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010855 vmcs12->vm_exit_intr_error_code = fault->error_code;
10856 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10857 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10858 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10859 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010860 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010861 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010862 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010863}
10864
Paolo Bonzinic9923842017-12-13 14:16:30 +010010865static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10866 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010867
10868static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010869 struct vmcs12 *vmcs12)
10870{
10871 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010872 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010873 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010874
10875 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010876 /*
10877 * Translate L1 physical address to host physical
10878 * address for vmcs02. Keep the page pinned, so this
10879 * physical address remains valid. We keep a reference
10880 * to it so we can release it later.
10881 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010882 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010883 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010884 vmx->nested.apic_access_page = NULL;
10885 }
10886 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010887 /*
10888 * If translation failed, no matter: This feature asks
10889 * to exit when accessing the given address, and if it
10890 * can never be accessed, this feature won't do
10891 * anything anyway.
10892 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010893 if (!is_error_page(page)) {
10894 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010895 hpa = page_to_phys(vmx->nested.apic_access_page);
10896 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10897 } else {
10898 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10899 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10900 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010901 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010902
10903 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010904 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010905 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010906 vmx->nested.virtual_apic_page = NULL;
10907 }
10908 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010909
10910 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010911 * If translation failed, VM entry will fail because
10912 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10913 * Failing the vm entry is _not_ what the processor
10914 * does but it's basically the only possibility we
10915 * have. We could still enter the guest if CR8 load
10916 * exits are enabled, CR8 store exits are enabled, and
10917 * virtualize APIC access is disabled; in this case
10918 * the processor would never use the TPR shadow and we
10919 * could simply clear the bit from the execution
10920 * control. But such a configuration is useless, so
10921 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010922 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010923 if (!is_error_page(page)) {
10924 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010925 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10926 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10927 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010928 }
10929
Wincy Van705699a2015-02-03 23:58:17 +080010930 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010931 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10932 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010933 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010934 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010935 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010936 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10937 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010938 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010939 vmx->nested.pi_desc_page = page;
10940 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010941 vmx->nested.pi_desc =
10942 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10943 (unsigned long)(vmcs12->posted_intr_desc_addr &
10944 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010945 vmcs_write64(POSTED_INTR_DESC_ADDR,
10946 page_to_phys(vmx->nested.pi_desc_page) +
10947 (unsigned long)(vmcs12->posted_intr_desc_addr &
10948 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010949 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010950 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010951 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10952 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010953 else
10954 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10955 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010956}
10957
Jan Kiszkaf4124502014-03-07 20:03:13 +010010958static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10959{
10960 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10961 struct vcpu_vmx *vmx = to_vmx(vcpu);
10962
10963 if (vcpu->arch.virtual_tsc_khz == 0)
10964 return;
10965
10966 /* Make sure short timeouts reliably trigger an immediate vmexit.
10967 * hrtimer_start does not guarantee this. */
10968 if (preemption_timeout <= 1) {
10969 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10970 return;
10971 }
10972
10973 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10974 preemption_timeout *= 1000000;
10975 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10976 hrtimer_start(&vmx->nested.preemption_timer,
10977 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10978}
10979
Jim Mattson56a20512017-07-06 16:33:06 -070010980static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10981 struct vmcs12 *vmcs12)
10982{
10983 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10984 return 0;
10985
10986 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10987 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10988 return -EINVAL;
10989
10990 return 0;
10991}
10992
Wincy Van3af18d92015-02-03 23:49:31 +080010993static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10994 struct vmcs12 *vmcs12)
10995{
Wincy Van3af18d92015-02-03 23:49:31 +080010996 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10997 return 0;
10998
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010999 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011000 return -EINVAL;
11001
11002 return 0;
11003}
11004
Jim Mattson712b12d2017-08-24 13:24:47 -070011005static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11006 struct vmcs12 *vmcs12)
11007{
11008 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11009 return 0;
11010
11011 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11012 return -EINVAL;
11013
11014 return 0;
11015}
11016
Wincy Van3af18d92015-02-03 23:49:31 +080011017/*
11018 * Merge L0's and L1's MSR bitmap, return false to indicate that
11019 * we do not use the hardware.
11020 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011021static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11022 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011023{
Wincy Van82f0dd42015-02-03 23:57:18 +080011024 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011025 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011026 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011027 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011028 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011029 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011030 *
11031 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11032 * ensures that we do not accidentally generate an L02 MSR bitmap
11033 * from the L12 MSR bitmap that is too permissive.
11034 * 2. That L1 or L2s have actually used the MSR. This avoids
11035 * unnecessarily merging of the bitmap if the MSR is unused. This
11036 * works properly because we only update the L01 MSR bitmap lazily.
11037 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11038 * updated to reflect this when L1 (or its L2s) actually write to
11039 * the MSR.
11040 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011041 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11042 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011043
Paolo Bonzinic9923842017-12-13 14:16:30 +010011044 /* Nothing to do if the MSR bitmap is not in use. */
11045 if (!cpu_has_vmx_msr_bitmap() ||
11046 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11047 return false;
11048
Ashok Raj15d45072018-02-01 22:59:43 +010011049 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011050 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011051 return false;
11052
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011053 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11054 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011055 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011056
Radim Krčmářd048c092016-08-08 20:16:22 +020011057 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011058 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11059 /*
11060 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11061 * just lets the processor take the value from the virtual-APIC page;
11062 * take those 256 bits directly from the L1 bitmap.
11063 */
11064 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11065 unsigned word = msr / BITS_PER_LONG;
11066 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11067 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011068 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011069 } else {
11070 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11071 unsigned word = msr / BITS_PER_LONG;
11072 msr_bitmap_l0[word] = ~0;
11073 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11074 }
11075 }
11076
11077 nested_vmx_disable_intercept_for_msr(
11078 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011079 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011080 MSR_TYPE_W);
11081
11082 if (nested_cpu_has_vid(vmcs12)) {
11083 nested_vmx_disable_intercept_for_msr(
11084 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011085 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011086 MSR_TYPE_W);
11087 nested_vmx_disable_intercept_for_msr(
11088 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011089 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011090 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011091 }
Ashok Raj15d45072018-02-01 22:59:43 +010011092
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011093 if (spec_ctrl)
11094 nested_vmx_disable_intercept_for_msr(
11095 msr_bitmap_l1, msr_bitmap_l0,
11096 MSR_IA32_SPEC_CTRL,
11097 MSR_TYPE_R | MSR_TYPE_W);
11098
Ashok Raj15d45072018-02-01 22:59:43 +010011099 if (pred_cmd)
11100 nested_vmx_disable_intercept_for_msr(
11101 msr_bitmap_l1, msr_bitmap_l0,
11102 MSR_IA32_PRED_CMD,
11103 MSR_TYPE_W);
11104
Wincy Vanf2b93282015-02-03 23:56:03 +080011105 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011106 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011107
11108 return true;
11109}
11110
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011111static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11112 struct vmcs12 *vmcs12)
11113{
11114 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11115 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11116 return -EINVAL;
11117 else
11118 return 0;
11119}
11120
Wincy Vanf2b93282015-02-03 23:56:03 +080011121static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11122 struct vmcs12 *vmcs12)
11123{
Wincy Van82f0dd42015-02-03 23:57:18 +080011124 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011125 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011126 !nested_cpu_has_vid(vmcs12) &&
11127 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011128 return 0;
11129
11130 /*
11131 * If virtualize x2apic mode is enabled,
11132 * virtualize apic access must be disabled.
11133 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011134 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11135 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011136 return -EINVAL;
11137
Wincy Van608406e2015-02-03 23:57:51 +080011138 /*
11139 * If virtual interrupt delivery is enabled,
11140 * we must exit on external interrupts.
11141 */
11142 if (nested_cpu_has_vid(vmcs12) &&
11143 !nested_exit_on_intr(vcpu))
11144 return -EINVAL;
11145
Wincy Van705699a2015-02-03 23:58:17 +080011146 /*
11147 * bits 15:8 should be zero in posted_intr_nv,
11148 * the descriptor address has been already checked
11149 * in nested_get_vmcs12_pages.
11150 */
11151 if (nested_cpu_has_posted_intr(vmcs12) &&
11152 (!nested_cpu_has_vid(vmcs12) ||
11153 !nested_exit_intr_ack_set(vcpu) ||
11154 vmcs12->posted_intr_nv & 0xff00))
11155 return -EINVAL;
11156
Wincy Vanf2b93282015-02-03 23:56:03 +080011157 /* tpr shadow is needed by all apicv features. */
11158 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11159 return -EINVAL;
11160
11161 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011162}
11163
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011164static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11165 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011166 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011167{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011168 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011169 u64 count, addr;
11170
11171 if (vmcs12_read_any(vcpu, count_field, &count) ||
11172 vmcs12_read_any(vcpu, addr_field, &addr)) {
11173 WARN_ON(1);
11174 return -EINVAL;
11175 }
11176 if (count == 0)
11177 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011178 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011179 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11180 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011181 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011182 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11183 addr_field, maxphyaddr, count, addr);
11184 return -EINVAL;
11185 }
11186 return 0;
11187}
11188
11189static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11190 struct vmcs12 *vmcs12)
11191{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011192 if (vmcs12->vm_exit_msr_load_count == 0 &&
11193 vmcs12->vm_exit_msr_store_count == 0 &&
11194 vmcs12->vm_entry_msr_load_count == 0)
11195 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011196 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011197 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011198 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011199 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011200 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011201 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011202 return -EINVAL;
11203 return 0;
11204}
11205
Bandan Dasc5f983f2017-05-05 15:25:14 -040011206static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11207 struct vmcs12 *vmcs12)
11208{
11209 u64 address = vmcs12->pml_address;
11210 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11211
11212 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11213 if (!nested_cpu_has_ept(vmcs12) ||
11214 !IS_ALIGNED(address, 4096) ||
11215 address >> maxphyaddr)
11216 return -EINVAL;
11217 }
11218
11219 return 0;
11220}
11221
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011222static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11223 struct vmx_msr_entry *e)
11224{
11225 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011226 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011227 return -EINVAL;
11228 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11229 e->index == MSR_IA32_UCODE_REV)
11230 return -EINVAL;
11231 if (e->reserved != 0)
11232 return -EINVAL;
11233 return 0;
11234}
11235
11236static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11237 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011238{
11239 if (e->index == MSR_FS_BASE ||
11240 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011241 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11242 nested_vmx_msr_check_common(vcpu, e))
11243 return -EINVAL;
11244 return 0;
11245}
11246
11247static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11248 struct vmx_msr_entry *e)
11249{
11250 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11251 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011252 return -EINVAL;
11253 return 0;
11254}
11255
11256/*
11257 * Load guest's/host's msr at nested entry/exit.
11258 * return 0 for success, entry index for failure.
11259 */
11260static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11261{
11262 u32 i;
11263 struct vmx_msr_entry e;
11264 struct msr_data msr;
11265
11266 msr.host_initiated = false;
11267 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011268 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11269 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011270 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011271 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11272 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011273 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011274 }
11275 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011276 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011277 "%s check failed (%u, 0x%x, 0x%x)\n",
11278 __func__, i, e.index, e.reserved);
11279 goto fail;
11280 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011281 msr.index = e.index;
11282 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011283 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011284 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011285 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11286 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011287 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011288 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011289 }
11290 return 0;
11291fail:
11292 return i + 1;
11293}
11294
11295static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11296{
11297 u32 i;
11298 struct vmx_msr_entry e;
11299
11300 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011301 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011302 if (kvm_vcpu_read_guest(vcpu,
11303 gpa + i * sizeof(e),
11304 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011305 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011306 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11307 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011308 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011309 }
11310 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011311 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011312 "%s check failed (%u, 0x%x, 0x%x)\n",
11313 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011314 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011315 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011316 msr_info.host_initiated = false;
11317 msr_info.index = e.index;
11318 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011319 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011320 "%s cannot read MSR (%u, 0x%x)\n",
11321 __func__, i, e.index);
11322 return -EINVAL;
11323 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011324 if (kvm_vcpu_write_guest(vcpu,
11325 gpa + i * sizeof(e) +
11326 offsetof(struct vmx_msr_entry, value),
11327 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011328 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011329 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011330 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011331 return -EINVAL;
11332 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011333 }
11334 return 0;
11335}
11336
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011337static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11338{
11339 unsigned long invalid_mask;
11340
11341 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11342 return (val & invalid_mask) == 0;
11343}
11344
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011345/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011346 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11347 * emulating VM entry into a guest with EPT enabled.
11348 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11349 * is assigned to entry_failure_code on failure.
11350 */
11351static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011352 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011353{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011354 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011355 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011356 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11357 return 1;
11358 }
11359
11360 /*
11361 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11362 * must not be dereferenced.
11363 */
11364 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11365 !nested_ept) {
11366 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11367 *entry_failure_code = ENTRY_FAIL_PDPTE;
11368 return 1;
11369 }
11370 }
11371
11372 vcpu->arch.cr3 = cr3;
11373 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11374 }
11375
11376 kvm_mmu_reset_context(vcpu);
11377 return 0;
11378}
11379
Jim Mattson6514dc32018-04-26 16:09:12 -070011380static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011381{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011382 struct vcpu_vmx *vmx = to_vmx(vcpu);
11383
11384 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11385 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11386 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11387 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11388 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11389 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11390 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11391 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11392 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11393 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11394 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11395 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11396 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11397 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11398 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11399 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11400 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11401 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11402 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11403 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11404 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11405 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11406 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11407 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11408 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11409 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11410 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11411 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11412 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11413 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11414 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011415
11416 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11417 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11418 vmcs12->guest_pending_dbg_exceptions);
11419 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11420 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11421
11422 if (nested_cpu_has_xsaves(vmcs12))
11423 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11424 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11425
11426 if (cpu_has_vmx_posted_intr())
11427 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11428
11429 /*
11430 * Whether page-faults are trapped is determined by a combination of
11431 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11432 * If enable_ept, L0 doesn't care about page faults and we should
11433 * set all of these to L1's desires. However, if !enable_ept, L0 does
11434 * care about (at least some) page faults, and because it is not easy
11435 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11436 * to exit on each and every L2 page fault. This is done by setting
11437 * MASK=MATCH=0 and (see below) EB.PF=1.
11438 * Note that below we don't need special code to set EB.PF beyond the
11439 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11440 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11441 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11442 */
11443 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11444 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11445 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11446 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11447
11448 /* All VMFUNCs are currently emulated through L0 vmexits. */
11449 if (cpu_has_vmx_vmfunc())
11450 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11451
11452 if (cpu_has_vmx_apicv()) {
11453 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11454 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11455 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11456 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11457 }
11458
11459 /*
11460 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11461 * Some constant fields are set here by vmx_set_constant_host_state().
11462 * Other fields are different per CPU, and will be set later when
11463 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11464 */
11465 vmx_set_constant_host_state(vmx);
11466
11467 /*
11468 * Set the MSR load/store lists to match L0's settings.
11469 */
11470 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011471 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11472 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11473 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11474 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011475
11476 set_cr4_guest_host_mask(vmx);
11477
11478 if (vmx_mpx_supported())
11479 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11480
11481 if (enable_vpid) {
11482 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11483 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11484 else
11485 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11486 }
11487
11488 /*
11489 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11490 */
11491 if (enable_ept) {
11492 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11493 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11494 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11495 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11496 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011497
11498 if (cpu_has_vmx_msr_bitmap())
11499 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011500}
11501
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011502/*
11503 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11504 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011505 * 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 +030011506 * guest in a way that will both be appropriate to L1's requests, and our
11507 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11508 * function also has additional necessary side-effects, like setting various
11509 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011510 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11511 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011512 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011513static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011514 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011515{
11516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011517 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011518
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011519 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011520 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011521 vmx->nested.dirty_vmcs12 = false;
11522 }
11523
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011524 /*
11525 * First, the fields that are shadowed. This must be kept in sync
11526 * with vmx_shadow_fields.h.
11527 */
11528
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011529 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011530 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011531 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011532 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11533 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011534
11535 /*
11536 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11537 * HOST_FS_BASE, HOST_GS_BASE.
11538 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011539
Jim Mattson6514dc32018-04-26 16:09:12 -070011540 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011541 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011542 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11543 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11544 } else {
11545 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11546 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11547 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011548 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011549 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11550 vmcs12->vm_entry_intr_info_field);
11551 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11552 vmcs12->vm_entry_exception_error_code);
11553 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11554 vmcs12->vm_entry_instruction_len);
11555 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11556 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011557 vmx->loaded_vmcs->nmi_known_unmasked =
11558 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011559 } else {
11560 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11561 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011562 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011563
Jan Kiszkaf4124502014-03-07 20:03:13 +010011564 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011565
Paolo Bonzini93140062016-07-06 13:23:51 +020011566 /* Preemption timer setting is only taken from vmcs01. */
11567 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11568 exec_control |= vmcs_config.pin_based_exec_ctrl;
11569 if (vmx->hv_deadline_tsc == -1)
11570 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11571
11572 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011573 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011574 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11575 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011576 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011577 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011578 }
Wincy Van705699a2015-02-03 23:58:17 +080011579
Jan Kiszkaf4124502014-03-07 20:03:13 +010011580 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011581
Jan Kiszkaf4124502014-03-07 20:03:13 +010011582 vmx->nested.preemption_timer_expired = false;
11583 if (nested_cpu_has_preemption_timer(vmcs12))
11584 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011585
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011586 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011587 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011588
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011589 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011590 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011591 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011592 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011593 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011594 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011595 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11596 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011597 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011598 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11599 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11600 ~SECONDARY_EXEC_ENABLE_PML;
11601 exec_control |= vmcs12_exec_ctrl;
11602 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011603
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011604 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011605 vmcs_write16(GUEST_INTR_STATUS,
11606 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011607
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011608 /*
11609 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11610 * nested_get_vmcs12_pages will either fix it up or
11611 * remove the VM execution control.
11612 */
11613 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11614 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11615
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011616 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11617 }
11618
Jim Mattson83bafef2016-10-04 10:48:38 -070011619 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011620 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11621 * entry, but only if the current (host) sp changed from the value
11622 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11623 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11624 * here we just force the write to happen on entry.
11625 */
11626 vmx->host_rsp = 0;
11627
11628 exec_control = vmx_exec_control(vmx); /* L0's desires */
11629 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11630 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11631 exec_control &= ~CPU_BASED_TPR_SHADOW;
11632 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011633
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011634 /*
11635 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11636 * nested_get_vmcs12_pages can't fix it up, the illegal value
11637 * will result in a VM entry failure.
11638 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011639 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011640 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011641 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011642 } else {
11643#ifdef CONFIG_X86_64
11644 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11645 CPU_BASED_CR8_STORE_EXITING;
11646#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011647 }
11648
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011649 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011650 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11651 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011652 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011653 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11654 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11655
11656 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11657
11658 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11659 * bitwise-or of what L1 wants to trap for L2, and what we want to
11660 * trap. Note that CR0.TS also needs updating - we do this later.
11661 */
11662 update_exception_bitmap(vcpu);
11663 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11664 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11665
Nadav Har'El8049d652013-08-05 11:07:06 +030011666 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11667 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11668 * bits are further modified by vmx_set_efer() below.
11669 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011670 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011671
11672 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11673 * emulated by vmx_set_efer(), below.
11674 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011675 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011676 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11677 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011678 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11679
Jim Mattson6514dc32018-04-26 16:09:12 -070011680 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011681 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011682 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011683 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011684 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011685 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011686 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011687
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011688 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11689
Peter Feinerc95ba922016-08-17 09:36:47 -070011690 if (kvm_has_tsc_control)
11691 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011692
11693 if (enable_vpid) {
11694 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011695 * There is no direct mapping between vpid02 and vpid12, the
11696 * vpid02 is per-vCPU for L0 and reused while the value of
11697 * vpid12 is changed w/ one invvpid during nested vmentry.
11698 * The vpid12 is allocated by L1 for L2, so it will not
11699 * influence global bitmap(for vpid01 and vpid02 allocation)
11700 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011701 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011702 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011703 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11704 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011705 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011706 }
11707 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011708 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011709 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011710 }
11711
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011712 if (enable_pml) {
11713 /*
11714 * Conceptually we want to copy the PML address and index from
11715 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11716 * since we always flush the log on each vmexit, this happens
11717 * to be equivalent to simply resetting the fields in vmcs02.
11718 */
11719 ASSERT(vmx->pml_pg);
11720 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11721 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11722 }
11723
Nadav Har'El155a97a2013-08-05 11:07:16 +030011724 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011725 if (nested_ept_init_mmu_context(vcpu)) {
11726 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11727 return 1;
11728 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011729 } else if (nested_cpu_has2(vmcs12,
11730 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011731 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011732 }
11733
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011734 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011735 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11736 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011737 * The CR0_READ_SHADOW is what L2 should have expected to read given
11738 * the specifications by L1; It's not enough to take
11739 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11740 * have more bits than L1 expected.
11741 */
11742 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11743 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11744
11745 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11746 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11747
Jim Mattson6514dc32018-04-26 16:09:12 -070011748 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011749 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011750 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11751 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11752 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11753 else
11754 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11755 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11756 vmx_set_efer(vcpu, vcpu->arch.efer);
11757
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011758 /*
11759 * Guest state is invalid and unrestricted guest is disabled,
11760 * which means L1 attempted VMEntry to L2 with invalid state.
11761 * Fail the VMEntry.
11762 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011763 if (vmx->emulation_required) {
11764 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011765 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011766 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011767
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011768 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011769 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011770 entry_failure_code))
11771 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011772
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011773 if (!enable_ept)
11774 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11775
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011776 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11777 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011778 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011779}
11780
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011781static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11782{
11783 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11784 nested_cpu_has_virtual_nmis(vmcs12))
11785 return -EINVAL;
11786
11787 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11788 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11789 return -EINVAL;
11790
11791 return 0;
11792}
11793
Jim Mattsonca0bde22016-11-30 12:03:46 -080011794static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11795{
11796 struct vcpu_vmx *vmx = to_vmx(vcpu);
11797
11798 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11799 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11800 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11801
Jim Mattson56a20512017-07-06 16:33:06 -070011802 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11803 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11804
Jim Mattsonca0bde22016-11-30 12:03:46 -080011805 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11806 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11807
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011808 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11809 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11810
Jim Mattson712b12d2017-08-24 13:24:47 -070011811 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11812 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11813
Jim Mattsonca0bde22016-11-30 12:03:46 -080011814 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11815 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11816
11817 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11818 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11819
Bandan Dasc5f983f2017-05-05 15:25:14 -040011820 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11821 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11822
Jim Mattsonca0bde22016-11-30 12:03:46 -080011823 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011824 vmx->nested.msrs.procbased_ctls_low,
11825 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011826 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11827 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011828 vmx->nested.msrs.secondary_ctls_low,
11829 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011830 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011831 vmx->nested.msrs.pinbased_ctls_low,
11832 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011833 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011834 vmx->nested.msrs.exit_ctls_low,
11835 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011836 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011837 vmx->nested.msrs.entry_ctls_low,
11838 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011839 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11840
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011841 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011842 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11843
Bandan Das41ab9372017-08-03 15:54:43 -040011844 if (nested_cpu_has_vmfunc(vmcs12)) {
11845 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011846 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011847 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11848
11849 if (nested_cpu_has_eptp_switching(vmcs12)) {
11850 if (!nested_cpu_has_ept(vmcs12) ||
11851 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11852 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11853 }
11854 }
Bandan Das27c42a12017-08-03 15:54:42 -040011855
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011856 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11857 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11858
Jim Mattsonca0bde22016-11-30 12:03:46 -080011859 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11860 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11861 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11862 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11863
11864 return 0;
11865}
11866
11867static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11868 u32 *exit_qual)
11869{
11870 bool ia32e;
11871
11872 *exit_qual = ENTRY_FAIL_DEFAULT;
11873
11874 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11875 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11876 return 1;
11877
11878 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11879 vmcs12->vmcs_link_pointer != -1ull) {
11880 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11881 return 1;
11882 }
11883
11884 /*
11885 * If the load IA32_EFER VM-entry control is 1, the following checks
11886 * are performed on the field for the IA32_EFER MSR:
11887 * - Bits reserved in the IA32_EFER MSR must be 0.
11888 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11889 * the IA-32e mode guest VM-exit control. It must also be identical
11890 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11891 * CR0.PG) is 1.
11892 */
11893 if (to_vmx(vcpu)->nested.nested_run_pending &&
11894 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11895 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11896 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11897 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11898 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11899 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11900 return 1;
11901 }
11902
11903 /*
11904 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11905 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11906 * the values of the LMA and LME bits in the field must each be that of
11907 * the host address-space size VM-exit control.
11908 */
11909 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11910 ia32e = (vmcs12->vm_exit_controls &
11911 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11912 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11913 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11914 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11915 return 1;
11916 }
11917
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011918 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11919 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11920 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11921 return 1;
11922
Jim Mattsonca0bde22016-11-30 12:03:46 -080011923 return 0;
11924}
11925
Jim Mattson6514dc32018-04-26 16:09:12 -070011926static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011927{
11928 struct vcpu_vmx *vmx = to_vmx(vcpu);
11929 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011930 u32 msr_entry_idx;
11931 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011932 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011933
Jim Mattson858e25c2016-11-30 12:03:47 -080011934 enter_guest_mode(vcpu);
11935
11936 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11937 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11938
Jim Mattsonde3a0022017-11-27 17:22:25 -060011939 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011940 vmx_segment_cache_clear(vmx);
11941
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011942 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11943 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11944
11945 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011946 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011947 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011948
11949 nested_get_vmcs12_pages(vcpu, vmcs12);
11950
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011951 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011952 msr_entry_idx = nested_vmx_load_msr(vcpu,
11953 vmcs12->vm_entry_msr_load_addr,
11954 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011955 if (msr_entry_idx)
11956 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011957
Jim Mattson858e25c2016-11-30 12:03:47 -080011958 /*
11959 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11960 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11961 * returned as far as L1 is concerned. It will only return (and set
11962 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11963 */
11964 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011965
11966fail:
11967 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11968 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11969 leave_guest_mode(vcpu);
11970 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11971 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11972 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011973}
11974
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011975/*
11976 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11977 * for running an L2 nested guest.
11978 */
11979static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11980{
11981 struct vmcs12 *vmcs12;
11982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011983 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011984 u32 exit_qual;
11985 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011986
Kyle Hueyeb277562016-11-29 12:40:39 -080011987 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011988 return 1;
11989
Kyle Hueyeb277562016-11-29 12:40:39 -080011990 if (!nested_vmx_check_vmcs12(vcpu))
11991 goto out;
11992
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011993 vmcs12 = get_vmcs12(vcpu);
11994
Abel Gordon012f83c2013-04-18 14:39:25 +030011995 if (enable_shadow_vmcs)
11996 copy_shadow_to_vmcs12(vmx);
11997
Nadav Har'El7c177932011-05-25 23:12:04 +030011998 /*
11999 * The nested entry process starts with enforcing various prerequisites
12000 * on vmcs12 as required by the Intel SDM, and act appropriately when
12001 * they fail: As the SDM explains, some conditions should cause the
12002 * instruction to fail, while others will cause the instruction to seem
12003 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12004 * To speed up the normal (success) code path, we should avoid checking
12005 * for misconfigurations which will anyway be caught by the processor
12006 * when using the merged vmcs02.
12007 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012008 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12009 nested_vmx_failValid(vcpu,
12010 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12011 goto out;
12012 }
12013
Nadav Har'El7c177932011-05-25 23:12:04 +030012014 if (vmcs12->launch_state == launch) {
12015 nested_vmx_failValid(vcpu,
12016 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12017 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012018 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012019 }
12020
Jim Mattsonca0bde22016-11-30 12:03:46 -080012021 ret = check_vmentry_prereqs(vcpu, vmcs12);
12022 if (ret) {
12023 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012024 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012025 }
12026
Nadav Har'El7c177932011-05-25 23:12:04 +030012027 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012028 * After this point, the trap flag no longer triggers a singlestep trap
12029 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12030 * This is not 100% correct; for performance reasons, we delegate most
12031 * of the checks on host state to the processor. If those fail,
12032 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012033 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012034 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012035
Jim Mattsonca0bde22016-11-30 12:03:46 -080012036 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12037 if (ret) {
12038 nested_vmx_entry_failure(vcpu, vmcs12,
12039 EXIT_REASON_INVALID_STATE, exit_qual);
12040 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012041 }
12042
12043 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012044 * We're finally done with prerequisite checking, and can start with
12045 * the nested entry.
12046 */
12047
Jim Mattson6514dc32018-04-26 16:09:12 -070012048 vmx->nested.nested_run_pending = 1;
12049 ret = enter_vmx_non_root_mode(vcpu);
12050 if (ret) {
12051 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012052 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070012053 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012054
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012055 /* Hide L1D cache contents from the nested guest. */
12056 vmx->vcpu.arch.l1tf_flush_l1d = true;
12057
Chao Gao135a06c2018-02-11 10:06:30 +080012058 /*
12059 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12060 * by event injection, halt vcpu.
12061 */
12062 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012063 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12064 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012065 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012066 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012067 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012068
12069out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012070 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012071}
12072
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012073/*
12074 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12075 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12076 * This function returns the new value we should put in vmcs12.guest_cr0.
12077 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12078 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12079 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12080 * didn't trap the bit, because if L1 did, so would L0).
12081 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12082 * been modified by L2, and L1 knows it. So just leave the old value of
12083 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12084 * isn't relevant, because if L0 traps this bit it can set it to anything.
12085 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12086 * changed these bits, and therefore they need to be updated, but L0
12087 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12088 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12089 */
12090static inline unsigned long
12091vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12092{
12093 return
12094 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12095 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12096 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12097 vcpu->arch.cr0_guest_owned_bits));
12098}
12099
12100static inline unsigned long
12101vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12102{
12103 return
12104 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12105 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12106 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12107 vcpu->arch.cr4_guest_owned_bits));
12108}
12109
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012110static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12111 struct vmcs12 *vmcs12)
12112{
12113 u32 idt_vectoring;
12114 unsigned int nr;
12115
Wanpeng Li664f8e22017-08-24 03:35:09 -070012116 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012117 nr = vcpu->arch.exception.nr;
12118 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12119
12120 if (kvm_exception_is_soft(nr)) {
12121 vmcs12->vm_exit_instruction_len =
12122 vcpu->arch.event_exit_inst_len;
12123 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12124 } else
12125 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12126
12127 if (vcpu->arch.exception.has_error_code) {
12128 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12129 vmcs12->idt_vectoring_error_code =
12130 vcpu->arch.exception.error_code;
12131 }
12132
12133 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012134 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012135 vmcs12->idt_vectoring_info_field =
12136 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012137 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012138 nr = vcpu->arch.interrupt.nr;
12139 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12140
12141 if (vcpu->arch.interrupt.soft) {
12142 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12143 vmcs12->vm_entry_instruction_len =
12144 vcpu->arch.event_exit_inst_len;
12145 } else
12146 idt_vectoring |= INTR_TYPE_EXT_INTR;
12147
12148 vmcs12->idt_vectoring_info_field = idt_vectoring;
12149 }
12150}
12151
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012152static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12153{
12154 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012155 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012156 bool block_nested_events =
12157 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012158
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012159 if (vcpu->arch.exception.pending &&
12160 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012161 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012162 return -EBUSY;
12163 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012164 return 0;
12165 }
12166
Jan Kiszkaf4124502014-03-07 20:03:13 +010012167 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12168 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012169 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012170 return -EBUSY;
12171 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12172 return 0;
12173 }
12174
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012175 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012176 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012177 return -EBUSY;
12178 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12179 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12180 INTR_INFO_VALID_MASK, 0);
12181 /*
12182 * The NMI-triggered VM exit counts as injection:
12183 * clear this one and block further NMIs.
12184 */
12185 vcpu->arch.nmi_pending = 0;
12186 vmx_set_nmi_mask(vcpu, true);
12187 return 0;
12188 }
12189
12190 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12191 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012192 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012193 return -EBUSY;
12194 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012195 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012196 }
12197
David Hildenbrand6342c502017-01-25 11:58:58 +010012198 vmx_complete_nested_posted_interrupt(vcpu);
12199 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012200}
12201
Jan Kiszkaf4124502014-03-07 20:03:13 +010012202static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12203{
12204 ktime_t remaining =
12205 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12206 u64 value;
12207
12208 if (ktime_to_ns(remaining) <= 0)
12209 return 0;
12210
12211 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12212 do_div(value, 1000000);
12213 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12214}
12215
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012216/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012217 * Update the guest state fields of vmcs12 to reflect changes that
12218 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12219 * VM-entry controls is also updated, since this is really a guest
12220 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012221 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012222static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012223{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012224 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12225 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12226
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012227 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12228 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12229 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12230
12231 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12232 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12233 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12234 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12235 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12236 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12237 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12238 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12239 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12240 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12241 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12242 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12243 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12244 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12245 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12246 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12247 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12248 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12249 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12250 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12251 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12252 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12253 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12254 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12255 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12256 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12257 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12258 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12259 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12260 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12261 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12262 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12263 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12264 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12265 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12266 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12267
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012268 vmcs12->guest_interruptibility_info =
12269 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12270 vmcs12->guest_pending_dbg_exceptions =
12271 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012272 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12273 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12274 else
12275 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012276
Jan Kiszkaf4124502014-03-07 20:03:13 +010012277 if (nested_cpu_has_preemption_timer(vmcs12)) {
12278 if (vmcs12->vm_exit_controls &
12279 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12280 vmcs12->vmx_preemption_timer_value =
12281 vmx_get_preemption_timer_value(vcpu);
12282 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12283 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012284
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012285 /*
12286 * In some cases (usually, nested EPT), L2 is allowed to change its
12287 * own CR3 without exiting. If it has changed it, we must keep it.
12288 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12289 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12290 *
12291 * Additionally, restore L2's PDPTR to vmcs12.
12292 */
12293 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012294 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012295 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12296 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12297 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12298 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12299 }
12300
Jim Mattsond281e132017-06-01 12:44:46 -070012301 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012302
Wincy Van608406e2015-02-03 23:57:51 +080012303 if (nested_cpu_has_vid(vmcs12))
12304 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12305
Jan Kiszkac18911a2013-03-13 16:06:41 +010012306 vmcs12->vm_entry_controls =
12307 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012308 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012309
Jan Kiszka2996fca2014-06-16 13:59:43 +020012310 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12311 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12312 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12313 }
12314
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012315 /* TODO: These cannot have changed unless we have MSR bitmaps and
12316 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012317 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012318 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012319 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12320 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012321 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12322 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12323 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012324 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012325 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012326}
12327
12328/*
12329 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12330 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12331 * and this function updates it to reflect the changes to the guest state while
12332 * L2 was running (and perhaps made some exits which were handled directly by L0
12333 * without going back to L1), and to reflect the exit reason.
12334 * Note that we do not have to copy here all VMCS fields, just those that
12335 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12336 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12337 * which already writes to vmcs12 directly.
12338 */
12339static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12340 u32 exit_reason, u32 exit_intr_info,
12341 unsigned long exit_qualification)
12342{
12343 /* update guest state fields: */
12344 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012345
12346 /* update exit information fields: */
12347
Jan Kiszka533558b2014-01-04 18:47:20 +010012348 vmcs12->vm_exit_reason = exit_reason;
12349 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012350 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012351
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012352 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012353 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12354 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12355
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012356 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012357 vmcs12->launch_state = 1;
12358
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012359 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12360 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012361 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012362
12363 /*
12364 * Transfer the event that L0 or L1 may wanted to inject into
12365 * L2 to IDT_VECTORING_INFO_FIELD.
12366 */
12367 vmcs12_save_pending_event(vcpu, vmcs12);
12368 }
12369
12370 /*
12371 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12372 * preserved above and would only end up incorrectly in L1.
12373 */
12374 vcpu->arch.nmi_injected = false;
12375 kvm_clear_exception_queue(vcpu);
12376 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012377}
12378
Wanpeng Li5af41572017-11-05 16:54:49 -080012379static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12380 struct vmcs12 *vmcs12)
12381{
12382 u32 entry_failure_code;
12383
12384 nested_ept_uninit_mmu_context(vcpu);
12385
12386 /*
12387 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12388 * couldn't have changed.
12389 */
12390 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12391 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12392
12393 if (!enable_ept)
12394 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12395}
12396
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012397/*
12398 * A part of what we need to when the nested L2 guest exits and we want to
12399 * run its L1 parent, is to reset L1's guest state to the host state specified
12400 * in vmcs12.
12401 * This function is to be called not only on normal nested exit, but also on
12402 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12403 * Failures During or After Loading Guest State").
12404 * This function should be called when the active VMCS is L1's (vmcs01).
12405 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012406static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12407 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012408{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012409 struct kvm_segment seg;
12410
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012411 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12412 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012413 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012414 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12415 else
12416 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12417 vmx_set_efer(vcpu, vcpu->arch.efer);
12418
12419 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12420 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012421 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012422 /*
12423 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012424 * actually changed, because vmx_set_cr0 refers to efer set above.
12425 *
12426 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12427 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012428 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012429 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012430 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012431
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012432 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012433 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012434 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012435
Wanpeng Li5af41572017-11-05 16:54:49 -080012436 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012437
Liran Alon6f1e03b2018-05-22 17:16:14 +030012438 /*
12439 * If vmcs01 don't use VPID, CPU flushes TLB on every
12440 * VMEntry/VMExit. Thus, no need to flush TLB.
12441 *
12442 * If vmcs12 uses VPID, TLB entries populated by L2 are
12443 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12444 * with vmx->vpid. Thus, no need to flush TLB.
12445 *
12446 * Therefore, flush TLB only in case vmcs01 uses VPID and
12447 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12448 * are both tagged with vmx->vpid.
12449 */
12450 if (enable_vpid &&
12451 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012452 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012453 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012454
12455 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12456 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12457 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12458 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12459 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012460 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12461 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012462
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012463 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12464 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12465 vmcs_write64(GUEST_BNDCFGS, 0);
12466
Jan Kiszka44811c02013-08-04 17:17:27 +020012467 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012468 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012469 vcpu->arch.pat = vmcs12->host_ia32_pat;
12470 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012471 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12472 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12473 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012474
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012475 /* Set L1 segment info according to Intel SDM
12476 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12477 seg = (struct kvm_segment) {
12478 .base = 0,
12479 .limit = 0xFFFFFFFF,
12480 .selector = vmcs12->host_cs_selector,
12481 .type = 11,
12482 .present = 1,
12483 .s = 1,
12484 .g = 1
12485 };
12486 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12487 seg.l = 1;
12488 else
12489 seg.db = 1;
12490 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12491 seg = (struct kvm_segment) {
12492 .base = 0,
12493 .limit = 0xFFFFFFFF,
12494 .type = 3,
12495 .present = 1,
12496 .s = 1,
12497 .db = 1,
12498 .g = 1
12499 };
12500 seg.selector = vmcs12->host_ds_selector;
12501 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12502 seg.selector = vmcs12->host_es_selector;
12503 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12504 seg.selector = vmcs12->host_ss_selector;
12505 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12506 seg.selector = vmcs12->host_fs_selector;
12507 seg.base = vmcs12->host_fs_base;
12508 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12509 seg.selector = vmcs12->host_gs_selector;
12510 seg.base = vmcs12->host_gs_base;
12511 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12512 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012513 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012514 .limit = 0x67,
12515 .selector = vmcs12->host_tr_selector,
12516 .type = 11,
12517 .present = 1
12518 };
12519 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12520
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012521 kvm_set_dr(vcpu, 7, 0x400);
12522 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012523
Wincy Van3af18d92015-02-03 23:49:31 +080012524 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012525 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012526
Wincy Vanff651cb2014-12-11 08:52:58 +030012527 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12528 vmcs12->vm_exit_msr_load_count))
12529 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012530}
12531
12532/*
12533 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12534 * and modify vmcs12 to make it see what it would expect to see there if
12535 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12536 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012537static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12538 u32 exit_intr_info,
12539 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012540{
12541 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012542 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12543
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012544 /* trying to cancel vmlaunch/vmresume is a bug */
12545 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12546
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012547 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012548 * The only expected VM-instruction error is "VM entry with
12549 * invalid control field(s)." Anything else indicates a
12550 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012551 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012552 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12553 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12554
12555 leave_guest_mode(vcpu);
12556
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012557 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12558 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12559
Jim Mattson4f350c62017-09-14 16:31:44 -070012560 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012561 if (exit_reason == -1)
12562 sync_vmcs12(vcpu, vmcs12);
12563 else
12564 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12565 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012566
12567 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12568 vmcs12->vm_exit_msr_store_count))
12569 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012570 }
12571
Jim Mattson4f350c62017-09-14 16:31:44 -070012572 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012573 vm_entry_controls_reset_shadow(vmx);
12574 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012575 vmx_segment_cache_clear(vmx);
12576
Paolo Bonzini93140062016-07-06 13:23:51 +020012577 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012578 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12579 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012580 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012581 if (vmx->hv_deadline_tsc == -1)
12582 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12583 PIN_BASED_VMX_PREEMPTION_TIMER);
12584 else
12585 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12586 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012587 if (kvm_has_tsc_control)
12588 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012589
Jim Mattson8d860bb2018-05-09 16:56:05 -040012590 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12591 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12592 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012593 } else if (!nested_cpu_has_ept(vmcs12) &&
12594 nested_cpu_has2(vmcs12,
12595 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012596 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012597 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012598
12599 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12600 vmx->host_rsp = 0;
12601
12602 /* Unpin physical memory we referred to in vmcs02 */
12603 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012604 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012605 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012606 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012607 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012608 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012609 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012610 }
Wincy Van705699a2015-02-03 23:58:17 +080012611 if (vmx->nested.pi_desc_page) {
12612 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012613 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012614 vmx->nested.pi_desc_page = NULL;
12615 vmx->nested.pi_desc = NULL;
12616 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012617
12618 /*
Tang Chen38b99172014-09-24 15:57:54 +080012619 * We are now running in L2, mmu_notifier will force to reload the
12620 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12621 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012622 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012623
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012624 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012625 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012626
12627 /* in case we halted in L2 */
12628 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012629
12630 if (likely(!vmx->fail)) {
12631 /*
12632 * TODO: SDM says that with acknowledge interrupt on
12633 * exit, bit 31 of the VM-exit interrupt information
12634 * (valid interrupt) is always set to 1 on
12635 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12636 * need kvm_cpu_has_interrupt(). See the commit
12637 * message for details.
12638 */
12639 if (nested_exit_intr_ack_set(vcpu) &&
12640 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12641 kvm_cpu_has_interrupt(vcpu)) {
12642 int irq = kvm_cpu_get_interrupt(vcpu);
12643 WARN_ON(irq < 0);
12644 vmcs12->vm_exit_intr_info = irq |
12645 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12646 }
12647
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012648 if (exit_reason != -1)
12649 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12650 vmcs12->exit_qualification,
12651 vmcs12->idt_vectoring_info_field,
12652 vmcs12->vm_exit_intr_info,
12653 vmcs12->vm_exit_intr_error_code,
12654 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012655
12656 load_vmcs12_host_state(vcpu, vmcs12);
12657
12658 return;
12659 }
12660
12661 /*
12662 * After an early L2 VM-entry failure, we're now back
12663 * in L1 which thinks it just finished a VMLAUNCH or
12664 * VMRESUME instruction, so we need to set the failure
12665 * flag and the VM-instruction error field of the VMCS
12666 * accordingly.
12667 */
12668 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012669
12670 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12671
Jim Mattson4f350c62017-09-14 16:31:44 -070012672 /*
12673 * The emulated instruction was already skipped in
12674 * nested_vmx_run, but the updated RIP was never
12675 * written back to the vmcs01.
12676 */
12677 skip_emulated_instruction(vcpu);
12678 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012679}
12680
Nadav Har'El7c177932011-05-25 23:12:04 +030012681/*
Jan Kiszka42124922014-01-04 18:47:19 +010012682 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12683 */
12684static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12685{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012686 if (is_guest_mode(vcpu)) {
12687 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012688 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012689 }
Jan Kiszka42124922014-01-04 18:47:19 +010012690 free_nested(to_vmx(vcpu));
12691}
12692
12693/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012694 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12695 * 23.7 "VM-entry failures during or after loading guest state" (this also
12696 * lists the acceptable exit-reason and exit-qualification parameters).
12697 * It should only be called before L2 actually succeeded to run, and when
12698 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12699 */
12700static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12701 struct vmcs12 *vmcs12,
12702 u32 reason, unsigned long qualification)
12703{
12704 load_vmcs12_host_state(vcpu, vmcs12);
12705 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12706 vmcs12->exit_qualification = qualification;
12707 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012708 if (enable_shadow_vmcs)
12709 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012710}
12711
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012712static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12713 struct x86_instruction_info *info,
12714 enum x86_intercept_stage stage)
12715{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012716 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12717 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12718
12719 /*
12720 * RDPID causes #UD if disabled through secondary execution controls.
12721 * Because it is marked as EmulateOnUD, we need to intercept it here.
12722 */
12723 if (info->intercept == x86_intercept_rdtscp &&
12724 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12725 ctxt->exception.vector = UD_VECTOR;
12726 ctxt->exception.error_code_valid = false;
12727 return X86EMUL_PROPAGATE_FAULT;
12728 }
12729
12730 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012731 return X86EMUL_CONTINUE;
12732}
12733
Yunhong Jiang64672c92016-06-13 14:19:59 -070012734#ifdef CONFIG_X86_64
12735/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12736static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12737 u64 divisor, u64 *result)
12738{
12739 u64 low = a << shift, high = a >> (64 - shift);
12740
12741 /* To avoid the overflow on divq */
12742 if (high >= divisor)
12743 return 1;
12744
12745 /* Low hold the result, high hold rem which is discarded */
12746 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12747 "rm" (divisor), "0" (low), "1" (high));
12748 *result = low;
12749
12750 return 0;
12751}
12752
12753static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12754{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012755 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012756 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012757
12758 if (kvm_mwait_in_guest(vcpu->kvm))
12759 return -EOPNOTSUPP;
12760
12761 vmx = to_vmx(vcpu);
12762 tscl = rdtsc();
12763 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12764 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012765 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12766
12767 if (delta_tsc > lapic_timer_advance_cycles)
12768 delta_tsc -= lapic_timer_advance_cycles;
12769 else
12770 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012771
12772 /* Convert to host delta tsc if tsc scaling is enabled */
12773 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12774 u64_shl_div_u64(delta_tsc,
12775 kvm_tsc_scaling_ratio_frac_bits,
12776 vcpu->arch.tsc_scaling_ratio,
12777 &delta_tsc))
12778 return -ERANGE;
12779
12780 /*
12781 * If the delta tsc can't fit in the 32 bit after the multi shift,
12782 * we can't use the preemption timer.
12783 * It's possible that it fits on later vmentries, but checking
12784 * on every vmentry is costly so we just use an hrtimer.
12785 */
12786 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12787 return -ERANGE;
12788
12789 vmx->hv_deadline_tsc = tscl + delta_tsc;
12790 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12791 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012792
12793 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012794}
12795
12796static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12797{
12798 struct vcpu_vmx *vmx = to_vmx(vcpu);
12799 vmx->hv_deadline_tsc = -1;
12800 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12801 PIN_BASED_VMX_PREEMPTION_TIMER);
12802}
12803#endif
12804
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012805static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012806{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012807 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012808 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012809}
12810
Kai Huang843e4332015-01-28 10:54:28 +080012811static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12812 struct kvm_memory_slot *slot)
12813{
12814 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12815 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12816}
12817
12818static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12819 struct kvm_memory_slot *slot)
12820{
12821 kvm_mmu_slot_set_dirty(kvm, slot);
12822}
12823
12824static void vmx_flush_log_dirty(struct kvm *kvm)
12825{
12826 kvm_flush_pml_buffers(kvm);
12827}
12828
Bandan Dasc5f983f2017-05-05 15:25:14 -040012829static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12830{
12831 struct vmcs12 *vmcs12;
12832 struct vcpu_vmx *vmx = to_vmx(vcpu);
12833 gpa_t gpa;
12834 struct page *page = NULL;
12835 u64 *pml_address;
12836
12837 if (is_guest_mode(vcpu)) {
12838 WARN_ON_ONCE(vmx->nested.pml_full);
12839
12840 /*
12841 * Check if PML is enabled for the nested guest.
12842 * Whether eptp bit 6 is set is already checked
12843 * as part of A/D emulation.
12844 */
12845 vmcs12 = get_vmcs12(vcpu);
12846 if (!nested_cpu_has_pml(vmcs12))
12847 return 0;
12848
Dan Carpenter47698862017-05-10 22:43:17 +030012849 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012850 vmx->nested.pml_full = true;
12851 return 1;
12852 }
12853
12854 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12855
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012856 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12857 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012858 return 0;
12859
12860 pml_address = kmap(page);
12861 pml_address[vmcs12->guest_pml_index--] = gpa;
12862 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012863 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012864 }
12865
12866 return 0;
12867}
12868
Kai Huang843e4332015-01-28 10:54:28 +080012869static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12870 struct kvm_memory_slot *memslot,
12871 gfn_t offset, unsigned long mask)
12872{
12873 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12874}
12875
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012876static void __pi_post_block(struct kvm_vcpu *vcpu)
12877{
12878 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12879 struct pi_desc old, new;
12880 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012881
12882 do {
12883 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012884 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12885 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012886
12887 dest = cpu_physical_id(vcpu->cpu);
12888
12889 if (x2apic_enabled())
12890 new.ndst = dest;
12891 else
12892 new.ndst = (dest << 8) & 0xFF00;
12893
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012894 /* set 'NV' to 'notification vector' */
12895 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012896 } while (cmpxchg64(&pi_desc->control, old.control,
12897 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012898
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012899 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12900 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012901 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012902 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012903 vcpu->pre_pcpu = -1;
12904 }
12905}
12906
Feng Wuefc64402015-09-18 22:29:51 +080012907/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012908 * This routine does the following things for vCPU which is going
12909 * to be blocked if VT-d PI is enabled.
12910 * - Store the vCPU to the wakeup list, so when interrupts happen
12911 * we can find the right vCPU to wake up.
12912 * - Change the Posted-interrupt descriptor as below:
12913 * 'NDST' <-- vcpu->pre_pcpu
12914 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12915 * - If 'ON' is set during this process, which means at least one
12916 * interrupt is posted for this vCPU, we cannot block it, in
12917 * this case, return 1, otherwise, return 0.
12918 *
12919 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012920static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012921{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012922 unsigned int dest;
12923 struct pi_desc old, new;
12924 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12925
12926 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012927 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12928 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012929 return 0;
12930
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012931 WARN_ON(irqs_disabled());
12932 local_irq_disable();
12933 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12934 vcpu->pre_pcpu = vcpu->cpu;
12935 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12936 list_add_tail(&vcpu->blocked_vcpu_list,
12937 &per_cpu(blocked_vcpu_on_cpu,
12938 vcpu->pre_pcpu));
12939 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12940 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012941
12942 do {
12943 old.control = new.control = pi_desc->control;
12944
Feng Wubf9f6ac2015-09-18 22:29:55 +080012945 WARN((pi_desc->sn == 1),
12946 "Warning: SN field of posted-interrupts "
12947 "is set before blocking\n");
12948
12949 /*
12950 * Since vCPU can be preempted during this process,
12951 * vcpu->cpu could be different with pre_pcpu, we
12952 * need to set pre_pcpu as the destination of wakeup
12953 * notification event, then we can find the right vCPU
12954 * to wakeup in wakeup handler if interrupts happen
12955 * when the vCPU is in blocked state.
12956 */
12957 dest = cpu_physical_id(vcpu->pre_pcpu);
12958
12959 if (x2apic_enabled())
12960 new.ndst = dest;
12961 else
12962 new.ndst = (dest << 8) & 0xFF00;
12963
12964 /* set 'NV' to 'wakeup vector' */
12965 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012966 } while (cmpxchg64(&pi_desc->control, old.control,
12967 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012968
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012969 /* We should not block the vCPU if an interrupt is posted for it. */
12970 if (pi_test_on(pi_desc) == 1)
12971 __pi_post_block(vcpu);
12972
12973 local_irq_enable();
12974 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012975}
12976
Yunhong Jiangbc225122016-06-13 14:19:58 -070012977static int vmx_pre_block(struct kvm_vcpu *vcpu)
12978{
12979 if (pi_pre_block(vcpu))
12980 return 1;
12981
Yunhong Jiang64672c92016-06-13 14:19:59 -070012982 if (kvm_lapic_hv_timer_in_use(vcpu))
12983 kvm_lapic_switch_to_sw_timer(vcpu);
12984
Yunhong Jiangbc225122016-06-13 14:19:58 -070012985 return 0;
12986}
12987
12988static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012989{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012990 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012991 return;
12992
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012993 WARN_ON(irqs_disabled());
12994 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012995 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012996 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012997}
12998
Yunhong Jiangbc225122016-06-13 14:19:58 -070012999static void vmx_post_block(struct kvm_vcpu *vcpu)
13000{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013001 if (kvm_x86_ops->set_hv_timer)
13002 kvm_lapic_switch_to_hv_timer(vcpu);
13003
Yunhong Jiangbc225122016-06-13 14:19:58 -070013004 pi_post_block(vcpu);
13005}
13006
Feng Wubf9f6ac2015-09-18 22:29:55 +080013007/*
Feng Wuefc64402015-09-18 22:29:51 +080013008 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13009 *
13010 * @kvm: kvm
13011 * @host_irq: host irq of the interrupt
13012 * @guest_irq: gsi of the interrupt
13013 * @set: set or unset PI
13014 * returns 0 on success, < 0 on failure
13015 */
13016static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13017 uint32_t guest_irq, bool set)
13018{
13019 struct kvm_kernel_irq_routing_entry *e;
13020 struct kvm_irq_routing_table *irq_rt;
13021 struct kvm_lapic_irq irq;
13022 struct kvm_vcpu *vcpu;
13023 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013024 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013025
13026 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013027 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13028 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013029 return 0;
13030
13031 idx = srcu_read_lock(&kvm->irq_srcu);
13032 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013033 if (guest_irq >= irq_rt->nr_rt_entries ||
13034 hlist_empty(&irq_rt->map[guest_irq])) {
13035 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13036 guest_irq, irq_rt->nr_rt_entries);
13037 goto out;
13038 }
Feng Wuefc64402015-09-18 22:29:51 +080013039
13040 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13041 if (e->type != KVM_IRQ_ROUTING_MSI)
13042 continue;
13043 /*
13044 * VT-d PI cannot support posting multicast/broadcast
13045 * interrupts to a vCPU, we still use interrupt remapping
13046 * for these kind of interrupts.
13047 *
13048 * For lowest-priority interrupts, we only support
13049 * those with single CPU as the destination, e.g. user
13050 * configures the interrupts via /proc/irq or uses
13051 * irqbalance to make the interrupts single-CPU.
13052 *
13053 * We will support full lowest-priority interrupt later.
13054 */
13055
Radim Krčmář371313132016-07-12 22:09:27 +020013056 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013057 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13058 /*
13059 * Make sure the IRTE is in remapped mode if
13060 * we don't handle it in posted mode.
13061 */
13062 ret = irq_set_vcpu_affinity(host_irq, NULL);
13063 if (ret < 0) {
13064 printk(KERN_INFO
13065 "failed to back to remapped mode, irq: %u\n",
13066 host_irq);
13067 goto out;
13068 }
13069
Feng Wuefc64402015-09-18 22:29:51 +080013070 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013071 }
Feng Wuefc64402015-09-18 22:29:51 +080013072
13073 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13074 vcpu_info.vector = irq.vector;
13075
hu huajun2698d822018-04-11 15:16:40 +080013076 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013077 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13078
13079 if (set)
13080 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013081 else
Feng Wuefc64402015-09-18 22:29:51 +080013082 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013083
13084 if (ret < 0) {
13085 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13086 __func__);
13087 goto out;
13088 }
13089 }
13090
13091 ret = 0;
13092out:
13093 srcu_read_unlock(&kvm->irq_srcu, idx);
13094 return ret;
13095}
13096
Ashok Rajc45dcc72016-06-22 14:59:56 +080013097static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13098{
13099 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13100 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13101 FEATURE_CONTROL_LMCE;
13102 else
13103 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13104 ~FEATURE_CONTROL_LMCE;
13105}
13106
Ladi Prosek72d7b372017-10-11 16:54:41 +020013107static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13108{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013109 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13110 if (to_vmx(vcpu)->nested.nested_run_pending)
13111 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013112 return 1;
13113}
13114
Ladi Prosek0234bf82017-10-11 16:54:40 +020013115static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13116{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013117 struct vcpu_vmx *vmx = to_vmx(vcpu);
13118
13119 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13120 if (vmx->nested.smm.guest_mode)
13121 nested_vmx_vmexit(vcpu, -1, 0, 0);
13122
13123 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13124 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013125 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013126 return 0;
13127}
13128
13129static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13130{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013131 struct vcpu_vmx *vmx = to_vmx(vcpu);
13132 int ret;
13133
13134 if (vmx->nested.smm.vmxon) {
13135 vmx->nested.vmxon = true;
13136 vmx->nested.smm.vmxon = false;
13137 }
13138
13139 if (vmx->nested.smm.guest_mode) {
13140 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013141 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013142 vcpu->arch.hflags |= HF_SMM_MASK;
13143 if (ret)
13144 return ret;
13145
13146 vmx->nested.smm.guest_mode = false;
13147 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013148 return 0;
13149}
13150
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013151static int enable_smi_window(struct kvm_vcpu *vcpu)
13152{
13153 return 0;
13154}
13155
Kees Cook404f6aa2016-08-08 16:29:06 -070013156static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013157 .cpu_has_kvm_support = cpu_has_kvm_support,
13158 .disabled_by_bios = vmx_disabled_by_bios,
13159 .hardware_setup = hardware_setup,
13160 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013161 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013162 .hardware_enable = hardware_enable,
13163 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013164 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013165 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013166
Wanpeng Lib31c1142018-03-12 04:53:04 -070013167 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013168 .vm_alloc = vmx_vm_alloc,
13169 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013170
Avi Kivity6aa8b732006-12-10 02:21:36 -080013171 .vcpu_create = vmx_create_vcpu,
13172 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013173 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013174
Avi Kivity04d2cc72007-09-10 18:10:54 +030013175 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013176 .vcpu_load = vmx_vcpu_load,
13177 .vcpu_put = vmx_vcpu_put,
13178
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013179 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013180 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013181 .get_msr = vmx_get_msr,
13182 .set_msr = vmx_set_msr,
13183 .get_segment_base = vmx_get_segment_base,
13184 .get_segment = vmx_get_segment,
13185 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013186 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013187 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013188 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013189 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013190 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013191 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013192 .set_cr3 = vmx_set_cr3,
13193 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013194 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013195 .get_idt = vmx_get_idt,
13196 .set_idt = vmx_set_idt,
13197 .get_gdt = vmx_get_gdt,
13198 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013199 .get_dr6 = vmx_get_dr6,
13200 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013201 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013202 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013203 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013204 .get_rflags = vmx_get_rflags,
13205 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013206
Avi Kivity6aa8b732006-12-10 02:21:36 -080013207 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013208
Avi Kivity6aa8b732006-12-10 02:21:36 -080013209 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013210 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013211 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013212 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13213 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013214 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013215 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013216 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013217 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013218 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013219 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013220 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013221 .get_nmi_mask = vmx_get_nmi_mask,
13222 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013223 .enable_nmi_window = enable_nmi_window,
13224 .enable_irq_window = enable_irq_window,
13225 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013226 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013227 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013228 .get_enable_apicv = vmx_get_enable_apicv,
13229 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013230 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013231 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013232 .hwapic_irr_update = vmx_hwapic_irr_update,
13233 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013234 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13235 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013236
Izik Eiduscbc94022007-10-25 00:29:55 +020013237 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013238 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013239 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013240 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013241
Avi Kivity586f9602010-11-18 13:09:54 +020013242 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013243
Sheng Yang17cc3932010-01-05 19:02:27 +080013244 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013245
13246 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013247
13248 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013249 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013250
13251 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013252
13253 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013254
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013255 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013256 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013257
13258 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013259
13260 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013261 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013262 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013263 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013264 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013265
13266 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013267
13268 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013269
13270 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13271 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13272 .flush_log_dirty = vmx_flush_log_dirty,
13273 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013274 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013275
Feng Wubf9f6ac2015-09-18 22:29:55 +080013276 .pre_block = vmx_pre_block,
13277 .post_block = vmx_post_block,
13278
Wei Huang25462f72015-06-19 15:45:05 +020013279 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013280
13281 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013282
13283#ifdef CONFIG_X86_64
13284 .set_hv_timer = vmx_set_hv_timer,
13285 .cancel_hv_timer = vmx_cancel_hv_timer,
13286#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013287
13288 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013289
Ladi Prosek72d7b372017-10-11 16:54:41 +020013290 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013291 .pre_enter_smm = vmx_pre_enter_smm,
13292 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013293 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013294};
13295
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013296static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013297{
13298 if (vmx_l1d_flush_pages) {
13299 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13300 vmx_l1d_flush_pages = NULL;
13301 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013302 /* Restore state so sysfs ignores VMX */
13303 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013304}
13305
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013306static void vmx_exit(void)
13307{
13308#ifdef CONFIG_KEXEC_CORE
13309 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13310 synchronize_rcu();
13311#endif
13312
13313 kvm_exit();
13314
13315#if IS_ENABLED(CONFIG_HYPERV)
13316 if (static_branch_unlikely(&enable_evmcs)) {
13317 int cpu;
13318 struct hv_vp_assist_page *vp_ap;
13319 /*
13320 * Reset everything to support using non-enlightened VMCS
13321 * access later (e.g. when we reload the module with
13322 * enlightened_vmcs=0)
13323 */
13324 for_each_online_cpu(cpu) {
13325 vp_ap = hv_get_vp_assist_page(cpu);
13326
13327 if (!vp_ap)
13328 continue;
13329
13330 vp_ap->current_nested_vmcs = 0;
13331 vp_ap->enlighten_vmentry = 0;
13332 }
13333
13334 static_branch_disable(&enable_evmcs);
13335 }
13336#endif
13337 vmx_cleanup_l1d_flush();
13338}
13339module_exit(vmx_exit);
13340
Avi Kivity6aa8b732006-12-10 02:21:36 -080013341static int __init vmx_init(void)
13342{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013343 int r;
13344
13345#if IS_ENABLED(CONFIG_HYPERV)
13346 /*
13347 * Enlightened VMCS usage should be recommended and the host needs
13348 * to support eVMCS v1 or above. We can also disable eVMCS support
13349 * with module parameter.
13350 */
13351 if (enlightened_vmcs &&
13352 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13353 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13354 KVM_EVMCS_VERSION) {
13355 int cpu;
13356
13357 /* Check that we have assist pages on all online CPUs */
13358 for_each_online_cpu(cpu) {
13359 if (!hv_get_vp_assist_page(cpu)) {
13360 enlightened_vmcs = false;
13361 break;
13362 }
13363 }
13364
13365 if (enlightened_vmcs) {
13366 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13367 static_branch_enable(&enable_evmcs);
13368 }
13369 } else {
13370 enlightened_vmcs = false;
13371 }
13372#endif
13373
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013374 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13375 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013376 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013377 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013378
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013379 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013380 * Must be called after kvm_init() so enable_ept is properly set
13381 * up. Hand the parameter mitigation value in which was stored in
13382 * the pre module init parser. If no parameter was given, it will
13383 * contain 'auto' which will be turned into the default 'cond'
13384 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013385 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013386 if (boot_cpu_has(X86_BUG_L1TF)) {
13387 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13388 if (r) {
13389 vmx_exit();
13390 return r;
13391 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013392 }
13393
Dave Young2965faa2015-09-09 15:38:55 -070013394#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013395 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13396 crash_vmclear_local_loaded_vmcss);
13397#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013398 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013399
He, Qingfdef3ad2007-04-30 09:45:24 +030013400 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013401}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013402module_init(vmx_init);