blob: 29d6eab6920e69bc2c64ad1c3f85585b7af4bd8c [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200193static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200195/* Storage for pre module init parameter parsing */
196static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197
198static const struct {
199 const char *option;
200 enum vmx_l1d_flush_state cmd;
201} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200202 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203 {"never", VMENTER_L1D_FLUSH_NEVER},
204 {"cond", VMENTER_L1D_FLUSH_COND},
205 {"always", VMENTER_L1D_FLUSH_ALWAYS},
206};
207
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200208#define L1D_CACHE_ORDER 4
209static void *vmx_l1d_flush_pages;
210
211static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
212{
213 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200214 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200215
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200216 if (!enable_ept) {
217 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
218 return 0;
219 }
220
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200221 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
222 u64 msr;
223
224 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
225 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
226 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
227 return 0;
228 }
229 }
230
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200231 /* If set to auto use the default l1tf mitigation method */
232 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
233 switch (l1tf_mitigation) {
234 case L1TF_MITIGATION_OFF:
235 l1tf = VMENTER_L1D_FLUSH_NEVER;
236 break;
237 case L1TF_MITIGATION_FLUSH_NOWARN:
238 case L1TF_MITIGATION_FLUSH:
239 case L1TF_MITIGATION_FLUSH_NOSMT:
240 l1tf = VMENTER_L1D_FLUSH_COND;
241 break;
242 case L1TF_MITIGATION_FULL:
243 case L1TF_MITIGATION_FULL_FORCE:
244 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
245 break;
246 }
247 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
248 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
249 }
250
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200251 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
252 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
253 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
254 if (!page)
255 return -ENOMEM;
256 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200257
258 /*
259 * Initialize each page with a different pattern in
260 * order to protect against KSM in the nested
261 * virtualization case.
262 */
263 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
264 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
265 PAGE_SIZE);
266 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200267 }
268
269 l1tf_vmx_mitigation = l1tf;
270
Thomas Gleixner895ae472018-07-13 16:23:22 +0200271 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
272 static_branch_enable(&vmx_l1d_should_flush);
273 else
274 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200275
Nicolai Stange427362a2018-07-21 22:25:00 +0200276 if (l1tf == VMENTER_L1D_FLUSH_COND)
277 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200278 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200279 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200280 return 0;
281}
282
283static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200284{
285 unsigned int i;
286
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200287 if (s) {
288 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Thomas Gleixner895ae472018-07-13 16:23:22 +0200289 if (sysfs_streq(s, vmentry_l1d_param[i].option))
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200290 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200291 }
292 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200293 return -EINVAL;
294}
295
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200296static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
297{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200298 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200299
300 if (!boot_cpu_has(X86_BUG_L1TF))
301 return 0;
302
303 l1tf = vmentry_l1d_flush_parse(s);
304 if (l1tf < 0)
305 return l1tf;
306
307 /*
308 * Has vmx_init() run already? If not then this is the pre init
309 * parameter parsing. In that case just store the value and let
310 * vmx_init() do the proper setup after enable_ept has been
311 * established.
312 */
313 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
314 vmentry_l1d_flush_param = l1tf;
315 return 0;
316 }
317
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200318 mutex_lock(&vmx_l1d_flush_mutex);
319 ret = vmx_setup_l1d_flush(l1tf);
320 mutex_unlock(&vmx_l1d_flush_mutex);
321 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200322}
323
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200324static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
325{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200326 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200327}
328
329static const struct kernel_param_ops vmentry_l1d_flush_ops = {
330 .set = vmentry_l1d_flush_set,
331 .get = vmentry_l1d_flush_get,
332};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200333module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200334
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700335struct kvm_vmx {
336 struct kvm kvm;
337
338 unsigned int tss_addr;
339 bool ept_identity_pagetable_done;
340 gpa_t ept_identity_map_addr;
341};
342
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200343#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300344
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400345struct vmcs {
346 u32 revision_id;
347 u32 abort;
348 char data[0];
349};
350
Nadav Har'Eld462b812011-05-24 15:26:10 +0300351/*
352 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
353 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
354 * loaded on this CPU (so we can clear them if the CPU goes down).
355 */
356struct loaded_vmcs {
357 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700358 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300359 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200360 bool launched;
361 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200362 unsigned long vmcs_host_cr3; /* May not match real cr3 */
363 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100364 /* Support for vnmi-less CPUs */
365 int soft_vnmi_blocked;
366 ktime_t entry_time;
367 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100368 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300369 struct list_head loaded_vmcss_on_cpu_link;
370};
371
Avi Kivity26bb0982009-09-07 11:14:12 +0300372struct shared_msr_entry {
373 unsigned index;
374 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200375 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300376};
377
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300378/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300379 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
380 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
381 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
382 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
383 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
384 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600385 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300386 * underlying hardware which will be used to run L2.
387 * This structure is packed to ensure that its layout is identical across
388 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700389 *
390 * IMPORTANT: Changing the layout of existing fields in this structure
391 * will break save/restore compatibility with older kvm releases. When
392 * adding new fields, either use space in the reserved padding* arrays
393 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300394 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300395typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300396struct __packed vmcs12 {
397 /* According to the Intel spec, a VMCS region must start with the
398 * following two fields. Then follow implementation-specific data.
399 */
400 u32 revision_id;
401 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300402
Nadav Har'El27d6c862011-05-25 23:06:59 +0300403 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
404 u32 padding[7]; /* room for future expansion */
405
Nadav Har'El22bd0352011-05-25 23:05:57 +0300406 u64 io_bitmap_a;
407 u64 io_bitmap_b;
408 u64 msr_bitmap;
409 u64 vm_exit_msr_store_addr;
410 u64 vm_exit_msr_load_addr;
411 u64 vm_entry_msr_load_addr;
412 u64 tsc_offset;
413 u64 virtual_apic_page_addr;
414 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800415 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300416 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800417 u64 eoi_exit_bitmap0;
418 u64 eoi_exit_bitmap1;
419 u64 eoi_exit_bitmap2;
420 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800421 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300422 u64 guest_physical_address;
423 u64 vmcs_link_pointer;
424 u64 guest_ia32_debugctl;
425 u64 guest_ia32_pat;
426 u64 guest_ia32_efer;
427 u64 guest_ia32_perf_global_ctrl;
428 u64 guest_pdptr0;
429 u64 guest_pdptr1;
430 u64 guest_pdptr2;
431 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100432 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300433 u64 host_ia32_pat;
434 u64 host_ia32_efer;
435 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700436 u64 vmread_bitmap;
437 u64 vmwrite_bitmap;
438 u64 vm_function_control;
439 u64 eptp_list_address;
440 u64 pml_address;
441 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300442 /*
443 * To allow migration of L1 (complete with its L2 guests) between
444 * machines of different natural widths (32 or 64 bit), we cannot have
445 * unsigned long fields with no explict size. We use u64 (aliased
446 * natural_width) instead. Luckily, x86 is little-endian.
447 */
448 natural_width cr0_guest_host_mask;
449 natural_width cr4_guest_host_mask;
450 natural_width cr0_read_shadow;
451 natural_width cr4_read_shadow;
452 natural_width cr3_target_value0;
453 natural_width cr3_target_value1;
454 natural_width cr3_target_value2;
455 natural_width cr3_target_value3;
456 natural_width exit_qualification;
457 natural_width guest_linear_address;
458 natural_width guest_cr0;
459 natural_width guest_cr3;
460 natural_width guest_cr4;
461 natural_width guest_es_base;
462 natural_width guest_cs_base;
463 natural_width guest_ss_base;
464 natural_width guest_ds_base;
465 natural_width guest_fs_base;
466 natural_width guest_gs_base;
467 natural_width guest_ldtr_base;
468 natural_width guest_tr_base;
469 natural_width guest_gdtr_base;
470 natural_width guest_idtr_base;
471 natural_width guest_dr7;
472 natural_width guest_rsp;
473 natural_width guest_rip;
474 natural_width guest_rflags;
475 natural_width guest_pending_dbg_exceptions;
476 natural_width guest_sysenter_esp;
477 natural_width guest_sysenter_eip;
478 natural_width host_cr0;
479 natural_width host_cr3;
480 natural_width host_cr4;
481 natural_width host_fs_base;
482 natural_width host_gs_base;
483 natural_width host_tr_base;
484 natural_width host_gdtr_base;
485 natural_width host_idtr_base;
486 natural_width host_ia32_sysenter_esp;
487 natural_width host_ia32_sysenter_eip;
488 natural_width host_rsp;
489 natural_width host_rip;
490 natural_width paddingl[8]; /* room for future expansion */
491 u32 pin_based_vm_exec_control;
492 u32 cpu_based_vm_exec_control;
493 u32 exception_bitmap;
494 u32 page_fault_error_code_mask;
495 u32 page_fault_error_code_match;
496 u32 cr3_target_count;
497 u32 vm_exit_controls;
498 u32 vm_exit_msr_store_count;
499 u32 vm_exit_msr_load_count;
500 u32 vm_entry_controls;
501 u32 vm_entry_msr_load_count;
502 u32 vm_entry_intr_info_field;
503 u32 vm_entry_exception_error_code;
504 u32 vm_entry_instruction_len;
505 u32 tpr_threshold;
506 u32 secondary_vm_exec_control;
507 u32 vm_instruction_error;
508 u32 vm_exit_reason;
509 u32 vm_exit_intr_info;
510 u32 vm_exit_intr_error_code;
511 u32 idt_vectoring_info_field;
512 u32 idt_vectoring_error_code;
513 u32 vm_exit_instruction_len;
514 u32 vmx_instruction_info;
515 u32 guest_es_limit;
516 u32 guest_cs_limit;
517 u32 guest_ss_limit;
518 u32 guest_ds_limit;
519 u32 guest_fs_limit;
520 u32 guest_gs_limit;
521 u32 guest_ldtr_limit;
522 u32 guest_tr_limit;
523 u32 guest_gdtr_limit;
524 u32 guest_idtr_limit;
525 u32 guest_es_ar_bytes;
526 u32 guest_cs_ar_bytes;
527 u32 guest_ss_ar_bytes;
528 u32 guest_ds_ar_bytes;
529 u32 guest_fs_ar_bytes;
530 u32 guest_gs_ar_bytes;
531 u32 guest_ldtr_ar_bytes;
532 u32 guest_tr_ar_bytes;
533 u32 guest_interruptibility_info;
534 u32 guest_activity_state;
535 u32 guest_sysenter_cs;
536 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100537 u32 vmx_preemption_timer_value;
538 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300539 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800540 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300541 u16 guest_es_selector;
542 u16 guest_cs_selector;
543 u16 guest_ss_selector;
544 u16 guest_ds_selector;
545 u16 guest_fs_selector;
546 u16 guest_gs_selector;
547 u16 guest_ldtr_selector;
548 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800549 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300550 u16 host_es_selector;
551 u16 host_cs_selector;
552 u16 host_ss_selector;
553 u16 host_ds_selector;
554 u16 host_fs_selector;
555 u16 host_gs_selector;
556 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700557 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300558};
559
560/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700561 * For save/restore compatibility, the vmcs12 field offsets must not change.
562 */
563#define CHECK_OFFSET(field, loc) \
564 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
565 "Offset of " #field " in struct vmcs12 has changed.")
566
567static inline void vmx_check_vmcs12_offsets(void) {
568 CHECK_OFFSET(revision_id, 0);
569 CHECK_OFFSET(abort, 4);
570 CHECK_OFFSET(launch_state, 8);
571 CHECK_OFFSET(io_bitmap_a, 40);
572 CHECK_OFFSET(io_bitmap_b, 48);
573 CHECK_OFFSET(msr_bitmap, 56);
574 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
575 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
576 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
577 CHECK_OFFSET(tsc_offset, 88);
578 CHECK_OFFSET(virtual_apic_page_addr, 96);
579 CHECK_OFFSET(apic_access_addr, 104);
580 CHECK_OFFSET(posted_intr_desc_addr, 112);
581 CHECK_OFFSET(ept_pointer, 120);
582 CHECK_OFFSET(eoi_exit_bitmap0, 128);
583 CHECK_OFFSET(eoi_exit_bitmap1, 136);
584 CHECK_OFFSET(eoi_exit_bitmap2, 144);
585 CHECK_OFFSET(eoi_exit_bitmap3, 152);
586 CHECK_OFFSET(xss_exit_bitmap, 160);
587 CHECK_OFFSET(guest_physical_address, 168);
588 CHECK_OFFSET(vmcs_link_pointer, 176);
589 CHECK_OFFSET(guest_ia32_debugctl, 184);
590 CHECK_OFFSET(guest_ia32_pat, 192);
591 CHECK_OFFSET(guest_ia32_efer, 200);
592 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
593 CHECK_OFFSET(guest_pdptr0, 216);
594 CHECK_OFFSET(guest_pdptr1, 224);
595 CHECK_OFFSET(guest_pdptr2, 232);
596 CHECK_OFFSET(guest_pdptr3, 240);
597 CHECK_OFFSET(guest_bndcfgs, 248);
598 CHECK_OFFSET(host_ia32_pat, 256);
599 CHECK_OFFSET(host_ia32_efer, 264);
600 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
601 CHECK_OFFSET(vmread_bitmap, 280);
602 CHECK_OFFSET(vmwrite_bitmap, 288);
603 CHECK_OFFSET(vm_function_control, 296);
604 CHECK_OFFSET(eptp_list_address, 304);
605 CHECK_OFFSET(pml_address, 312);
606 CHECK_OFFSET(cr0_guest_host_mask, 344);
607 CHECK_OFFSET(cr4_guest_host_mask, 352);
608 CHECK_OFFSET(cr0_read_shadow, 360);
609 CHECK_OFFSET(cr4_read_shadow, 368);
610 CHECK_OFFSET(cr3_target_value0, 376);
611 CHECK_OFFSET(cr3_target_value1, 384);
612 CHECK_OFFSET(cr3_target_value2, 392);
613 CHECK_OFFSET(cr3_target_value3, 400);
614 CHECK_OFFSET(exit_qualification, 408);
615 CHECK_OFFSET(guest_linear_address, 416);
616 CHECK_OFFSET(guest_cr0, 424);
617 CHECK_OFFSET(guest_cr3, 432);
618 CHECK_OFFSET(guest_cr4, 440);
619 CHECK_OFFSET(guest_es_base, 448);
620 CHECK_OFFSET(guest_cs_base, 456);
621 CHECK_OFFSET(guest_ss_base, 464);
622 CHECK_OFFSET(guest_ds_base, 472);
623 CHECK_OFFSET(guest_fs_base, 480);
624 CHECK_OFFSET(guest_gs_base, 488);
625 CHECK_OFFSET(guest_ldtr_base, 496);
626 CHECK_OFFSET(guest_tr_base, 504);
627 CHECK_OFFSET(guest_gdtr_base, 512);
628 CHECK_OFFSET(guest_idtr_base, 520);
629 CHECK_OFFSET(guest_dr7, 528);
630 CHECK_OFFSET(guest_rsp, 536);
631 CHECK_OFFSET(guest_rip, 544);
632 CHECK_OFFSET(guest_rflags, 552);
633 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
634 CHECK_OFFSET(guest_sysenter_esp, 568);
635 CHECK_OFFSET(guest_sysenter_eip, 576);
636 CHECK_OFFSET(host_cr0, 584);
637 CHECK_OFFSET(host_cr3, 592);
638 CHECK_OFFSET(host_cr4, 600);
639 CHECK_OFFSET(host_fs_base, 608);
640 CHECK_OFFSET(host_gs_base, 616);
641 CHECK_OFFSET(host_tr_base, 624);
642 CHECK_OFFSET(host_gdtr_base, 632);
643 CHECK_OFFSET(host_idtr_base, 640);
644 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
645 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
646 CHECK_OFFSET(host_rsp, 664);
647 CHECK_OFFSET(host_rip, 672);
648 CHECK_OFFSET(pin_based_vm_exec_control, 744);
649 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
650 CHECK_OFFSET(exception_bitmap, 752);
651 CHECK_OFFSET(page_fault_error_code_mask, 756);
652 CHECK_OFFSET(page_fault_error_code_match, 760);
653 CHECK_OFFSET(cr3_target_count, 764);
654 CHECK_OFFSET(vm_exit_controls, 768);
655 CHECK_OFFSET(vm_exit_msr_store_count, 772);
656 CHECK_OFFSET(vm_exit_msr_load_count, 776);
657 CHECK_OFFSET(vm_entry_controls, 780);
658 CHECK_OFFSET(vm_entry_msr_load_count, 784);
659 CHECK_OFFSET(vm_entry_intr_info_field, 788);
660 CHECK_OFFSET(vm_entry_exception_error_code, 792);
661 CHECK_OFFSET(vm_entry_instruction_len, 796);
662 CHECK_OFFSET(tpr_threshold, 800);
663 CHECK_OFFSET(secondary_vm_exec_control, 804);
664 CHECK_OFFSET(vm_instruction_error, 808);
665 CHECK_OFFSET(vm_exit_reason, 812);
666 CHECK_OFFSET(vm_exit_intr_info, 816);
667 CHECK_OFFSET(vm_exit_intr_error_code, 820);
668 CHECK_OFFSET(idt_vectoring_info_field, 824);
669 CHECK_OFFSET(idt_vectoring_error_code, 828);
670 CHECK_OFFSET(vm_exit_instruction_len, 832);
671 CHECK_OFFSET(vmx_instruction_info, 836);
672 CHECK_OFFSET(guest_es_limit, 840);
673 CHECK_OFFSET(guest_cs_limit, 844);
674 CHECK_OFFSET(guest_ss_limit, 848);
675 CHECK_OFFSET(guest_ds_limit, 852);
676 CHECK_OFFSET(guest_fs_limit, 856);
677 CHECK_OFFSET(guest_gs_limit, 860);
678 CHECK_OFFSET(guest_ldtr_limit, 864);
679 CHECK_OFFSET(guest_tr_limit, 868);
680 CHECK_OFFSET(guest_gdtr_limit, 872);
681 CHECK_OFFSET(guest_idtr_limit, 876);
682 CHECK_OFFSET(guest_es_ar_bytes, 880);
683 CHECK_OFFSET(guest_cs_ar_bytes, 884);
684 CHECK_OFFSET(guest_ss_ar_bytes, 888);
685 CHECK_OFFSET(guest_ds_ar_bytes, 892);
686 CHECK_OFFSET(guest_fs_ar_bytes, 896);
687 CHECK_OFFSET(guest_gs_ar_bytes, 900);
688 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
689 CHECK_OFFSET(guest_tr_ar_bytes, 908);
690 CHECK_OFFSET(guest_interruptibility_info, 912);
691 CHECK_OFFSET(guest_activity_state, 916);
692 CHECK_OFFSET(guest_sysenter_cs, 920);
693 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
694 CHECK_OFFSET(vmx_preemption_timer_value, 928);
695 CHECK_OFFSET(virtual_processor_id, 960);
696 CHECK_OFFSET(posted_intr_nv, 962);
697 CHECK_OFFSET(guest_es_selector, 964);
698 CHECK_OFFSET(guest_cs_selector, 966);
699 CHECK_OFFSET(guest_ss_selector, 968);
700 CHECK_OFFSET(guest_ds_selector, 970);
701 CHECK_OFFSET(guest_fs_selector, 972);
702 CHECK_OFFSET(guest_gs_selector, 974);
703 CHECK_OFFSET(guest_ldtr_selector, 976);
704 CHECK_OFFSET(guest_tr_selector, 978);
705 CHECK_OFFSET(guest_intr_status, 980);
706 CHECK_OFFSET(host_es_selector, 982);
707 CHECK_OFFSET(host_cs_selector, 984);
708 CHECK_OFFSET(host_ss_selector, 986);
709 CHECK_OFFSET(host_ds_selector, 988);
710 CHECK_OFFSET(host_fs_selector, 990);
711 CHECK_OFFSET(host_gs_selector, 992);
712 CHECK_OFFSET(host_tr_selector, 994);
713 CHECK_OFFSET(guest_pml_index, 996);
714}
715
716/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300717 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
718 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
719 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700720 *
721 * IMPORTANT: Changing this value will break save/restore compatibility with
722 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300723 */
724#define VMCS12_REVISION 0x11e57ed0
725
726/*
727 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
728 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
729 * current implementation, 4K are reserved to avoid future complications.
730 */
731#define VMCS12_SIZE 0x1000
732
733/*
Jim Mattson5b157062017-12-22 12:11:12 -0800734 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
735 * supported VMCS12 field encoding.
736 */
737#define VMCS12_MAX_FIELD_INDEX 0x17
738
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100739struct nested_vmx_msrs {
740 /*
741 * We only store the "true" versions of the VMX capability MSRs. We
742 * generate the "non-true" versions by setting the must-be-1 bits
743 * according to the SDM.
744 */
745 u32 procbased_ctls_low;
746 u32 procbased_ctls_high;
747 u32 secondary_ctls_low;
748 u32 secondary_ctls_high;
749 u32 pinbased_ctls_low;
750 u32 pinbased_ctls_high;
751 u32 exit_ctls_low;
752 u32 exit_ctls_high;
753 u32 entry_ctls_low;
754 u32 entry_ctls_high;
755 u32 misc_low;
756 u32 misc_high;
757 u32 ept_caps;
758 u32 vpid_caps;
759 u64 basic;
760 u64 cr0_fixed0;
761 u64 cr0_fixed1;
762 u64 cr4_fixed0;
763 u64 cr4_fixed1;
764 u64 vmcs_enum;
765 u64 vmfunc_controls;
766};
767
Jim Mattson5b157062017-12-22 12:11:12 -0800768/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300769 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
770 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
771 */
772struct nested_vmx {
773 /* Has the level1 guest done vmxon? */
774 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400775 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400776 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300777
778 /* The guest-physical address of the current VMCS L1 keeps for L2 */
779 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700780 /*
781 * Cache of the guest's VMCS, existing outside of guest memory.
782 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700783 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700784 */
785 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300786 /*
787 * Indicates if the shadow vmcs must be updated with the
788 * data hold by vmcs12
789 */
790 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100791 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300792
Jim Mattson8d860bb2018-05-09 16:56:05 -0400793 bool change_vmcs01_virtual_apic_mode;
794
Nadav Har'El644d7112011-05-25 23:12:35 +0300795 /* L2 must run next, and mustn't decide to exit to L1. */
796 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600797
798 struct loaded_vmcs vmcs02;
799
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300800 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600801 * Guest pages referred to in the vmcs02 with host-physical
802 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300803 */
804 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800805 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800806 struct page *pi_desc_page;
807 struct pi_desc *pi_desc;
808 bool pi_pending;
809 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100810
811 struct hrtimer preemption_timer;
812 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200813
814 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
815 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800816
Wanpeng Li5c614b32015-10-13 09:18:36 -0700817 u16 vpid02;
818 u16 last_vpid;
819
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100820 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200821
822 /* SMM related state */
823 struct {
824 /* in VMX operation on SMM entry? */
825 bool vmxon;
826 /* in guest mode on SMM entry? */
827 bool guest_mode;
828 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300829};
830
Yang Zhang01e439b2013-04-11 19:25:12 +0800831#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800832#define POSTED_INTR_SN 1
833
Yang Zhang01e439b2013-04-11 19:25:12 +0800834/* Posted-Interrupt Descriptor */
835struct pi_desc {
836 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800837 union {
838 struct {
839 /* bit 256 - Outstanding Notification */
840 u16 on : 1,
841 /* bit 257 - Suppress Notification */
842 sn : 1,
843 /* bit 271:258 - Reserved */
844 rsvd_1 : 14;
845 /* bit 279:272 - Notification Vector */
846 u8 nv;
847 /* bit 287:280 - Reserved */
848 u8 rsvd_2;
849 /* bit 319:288 - Notification Destination */
850 u32 ndst;
851 };
852 u64 control;
853 };
854 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800855} __aligned(64);
856
Yang Zhanga20ed542013-04-11 19:25:15 +0800857static bool pi_test_and_set_on(struct pi_desc *pi_desc)
858{
859 return test_and_set_bit(POSTED_INTR_ON,
860 (unsigned long *)&pi_desc->control);
861}
862
863static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
864{
865 return test_and_clear_bit(POSTED_INTR_ON,
866 (unsigned long *)&pi_desc->control);
867}
868
869static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
870{
871 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
872}
873
Feng Wuebbfc762015-09-18 22:29:46 +0800874static inline void pi_clear_sn(struct pi_desc *pi_desc)
875{
876 return clear_bit(POSTED_INTR_SN,
877 (unsigned long *)&pi_desc->control);
878}
879
880static inline void pi_set_sn(struct pi_desc *pi_desc)
881{
882 return set_bit(POSTED_INTR_SN,
883 (unsigned long *)&pi_desc->control);
884}
885
Paolo Bonziniad361092016-09-20 16:15:05 +0200886static inline void pi_clear_on(struct pi_desc *pi_desc)
887{
888 clear_bit(POSTED_INTR_ON,
889 (unsigned long *)&pi_desc->control);
890}
891
Feng Wuebbfc762015-09-18 22:29:46 +0800892static inline int pi_test_on(struct pi_desc *pi_desc)
893{
894 return test_bit(POSTED_INTR_ON,
895 (unsigned long *)&pi_desc->control);
896}
897
898static inline int pi_test_sn(struct pi_desc *pi_desc)
899{
900 return test_bit(POSTED_INTR_SN,
901 (unsigned long *)&pi_desc->control);
902}
903
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400904struct vmx_msrs {
905 unsigned int nr;
906 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
907};
908
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400909struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000910 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300911 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300912 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100913 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300914 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200915 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200916 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300917 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400918 int nmsrs;
919 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800920 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400921#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300922 u64 msr_host_kernel_gs_base;
923 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100925
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100926 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100927 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100928
Gleb Natapov2961e8762013-11-25 15:37:13 +0200929 u32 vm_entry_controls_shadow;
930 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200931 u32 secondary_exec_control;
932
Nadav Har'Eld462b812011-05-24 15:26:10 +0300933 /*
934 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
935 * non-nested (L1) guest, it always points to vmcs01. For a nested
936 * guest (L2), it points to a different VMCS.
937 */
938 struct loaded_vmcs vmcs01;
939 struct loaded_vmcs *loaded_vmcs;
940 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300941 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400942 struct vmx_msrs guest;
943 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300944 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400945 struct {
946 int loaded;
947 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300948#ifdef CONFIG_X86_64
949 u16 ds_sel, es_sel;
950#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200951 int gs_ldt_reload_needed;
952 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000953 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400954 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200955 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300956 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300957 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300958 struct kvm_segment segs[8];
959 } rmode;
960 struct {
961 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300962 struct kvm_save_segment {
963 u16 selector;
964 unsigned long base;
965 u32 limit;
966 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300967 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300968 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800969 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300970 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200971
Andi Kleena0861c02009-06-08 17:37:09 +0800972 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800973
Yang Zhang01e439b2013-04-11 19:25:12 +0800974 /* Posted interrupt descriptor */
975 struct pi_desc pi_desc;
976
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300977 /* Support for a guest hypervisor (nested VMX) */
978 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200979
980 /* Dynamic PLE window. */
981 int ple_window;
982 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800983
984 /* Support for PML */
985#define PML_ENTITY_NUM 512
986 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800987
Yunhong Jiang64672c92016-06-13 14:19:59 -0700988 /* apic deadline value in host tsc */
989 u64 hv_deadline_tsc;
990
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800991 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800992
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800993 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800994
Wanpeng Li74c55932017-11-29 01:31:20 -0800995 unsigned long host_debugctlmsr;
996
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800997 /*
998 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
999 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1000 * in msr_ia32_feature_control_valid_bits.
1001 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001002 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001003 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001004};
1005
Avi Kivity2fb92db2011-04-27 19:42:18 +03001006enum segment_cache_field {
1007 SEG_FIELD_SEL = 0,
1008 SEG_FIELD_BASE = 1,
1009 SEG_FIELD_LIMIT = 2,
1010 SEG_FIELD_AR = 3,
1011
1012 SEG_FIELD_NR = 4
1013};
1014
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001015static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1016{
1017 return container_of(kvm, struct kvm_vmx, kvm);
1018}
1019
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001020static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1021{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001022 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001023}
1024
Feng Wuefc64402015-09-18 22:29:51 +08001025static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1026{
1027 return &(to_vmx(vcpu)->pi_desc);
1028}
1029
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001030#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001031#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001032#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1033#define FIELD64(number, name) \
1034 FIELD(number, name), \
1035 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001036
Abel Gordon4607c2d2013-04-18 14:35:55 +03001037
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001038static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001039#define SHADOW_FIELD_RO(x) x,
1040#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001041};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001042static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001043 ARRAY_SIZE(shadow_read_only_fields);
1044
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001045static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001046#define SHADOW_FIELD_RW(x) x,
1047#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001048};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001049static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001050 ARRAY_SIZE(shadow_read_write_fields);
1051
Mathias Krause772e0312012-08-30 01:30:19 +02001052static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001053 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001054 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001055 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1056 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1057 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1058 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1059 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1060 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1061 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1062 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001063 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001064 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001065 FIELD(HOST_ES_SELECTOR, host_es_selector),
1066 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1067 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1068 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1069 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1070 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1071 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1072 FIELD64(IO_BITMAP_A, io_bitmap_a),
1073 FIELD64(IO_BITMAP_B, io_bitmap_b),
1074 FIELD64(MSR_BITMAP, msr_bitmap),
1075 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1076 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1077 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001078 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001079 FIELD64(TSC_OFFSET, tsc_offset),
1080 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1081 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001082 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001083 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001084 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001085 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1086 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1087 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1088 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001089 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001090 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1091 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001092 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001093 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1094 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1095 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1096 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1097 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1098 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1099 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1100 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1101 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1102 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001103 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001104 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1105 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1106 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1107 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1108 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1109 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1110 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1111 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1112 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1113 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1114 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1115 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1116 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1117 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1118 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1119 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1120 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1121 FIELD(TPR_THRESHOLD, tpr_threshold),
1122 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1123 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1124 FIELD(VM_EXIT_REASON, vm_exit_reason),
1125 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1126 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1127 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1128 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1129 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1130 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1131 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1132 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1133 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1134 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1135 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1136 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1137 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1138 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1139 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1140 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1141 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1142 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1143 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1144 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1145 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1146 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1147 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1148 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1149 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1150 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1151 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1152 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001153 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001154 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1155 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1156 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1157 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1158 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1159 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1160 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1161 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1162 FIELD(EXIT_QUALIFICATION, exit_qualification),
1163 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1164 FIELD(GUEST_CR0, guest_cr0),
1165 FIELD(GUEST_CR3, guest_cr3),
1166 FIELD(GUEST_CR4, guest_cr4),
1167 FIELD(GUEST_ES_BASE, guest_es_base),
1168 FIELD(GUEST_CS_BASE, guest_cs_base),
1169 FIELD(GUEST_SS_BASE, guest_ss_base),
1170 FIELD(GUEST_DS_BASE, guest_ds_base),
1171 FIELD(GUEST_FS_BASE, guest_fs_base),
1172 FIELD(GUEST_GS_BASE, guest_gs_base),
1173 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1174 FIELD(GUEST_TR_BASE, guest_tr_base),
1175 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1176 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1177 FIELD(GUEST_DR7, guest_dr7),
1178 FIELD(GUEST_RSP, guest_rsp),
1179 FIELD(GUEST_RIP, guest_rip),
1180 FIELD(GUEST_RFLAGS, guest_rflags),
1181 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1182 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1183 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1184 FIELD(HOST_CR0, host_cr0),
1185 FIELD(HOST_CR3, host_cr3),
1186 FIELD(HOST_CR4, host_cr4),
1187 FIELD(HOST_FS_BASE, host_fs_base),
1188 FIELD(HOST_GS_BASE, host_gs_base),
1189 FIELD(HOST_TR_BASE, host_tr_base),
1190 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1191 FIELD(HOST_IDTR_BASE, host_idtr_base),
1192 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1193 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1194 FIELD(HOST_RSP, host_rsp),
1195 FIELD(HOST_RIP, host_rip),
1196};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001197
1198static inline short vmcs_field_to_offset(unsigned long field)
1199{
Dan Williams085331d2018-01-31 17:47:03 -08001200 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1201 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001202 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001203
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001204 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001205 return -ENOENT;
1206
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001207 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001208 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001209 return -ENOENT;
1210
Linus Torvalds15303ba2018-02-10 13:16:35 -08001211 index = array_index_nospec(index, size);
1212 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001213 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001214 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001215 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001216}
1217
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001218static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1219{
David Matlack4f2777b2016-07-13 17:16:37 -07001220 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001221}
1222
Peter Feiner995f00a2017-06-30 17:26:32 -07001223static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001224static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001225static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001226static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001227static void vmx_set_segment(struct kvm_vcpu *vcpu,
1228 struct kvm_segment *var, int seg);
1229static void vmx_get_segment(struct kvm_vcpu *vcpu,
1230 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001231static bool guest_state_valid(struct kvm_vcpu *vcpu);
1232static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001233static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001234static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1235static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1236static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1237 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001238static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001239static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1240 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001241
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1243static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001244/*
1245 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1246 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1247 */
1248static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249
Feng Wubf9f6ac2015-09-18 22:29:55 +08001250/*
1251 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1252 * can find which vCPU should be waken up.
1253 */
1254static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1255static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1256
Radim Krčmář23611332016-09-29 22:41:33 +02001257enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001258 VMX_VMREAD_BITMAP,
1259 VMX_VMWRITE_BITMAP,
1260 VMX_BITMAP_NR
1261};
1262
1263static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1264
Radim Krčmář23611332016-09-29 22:41:33 +02001265#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1266#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001267
Avi Kivity110312c2010-12-21 12:54:20 +02001268static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001269static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001270
Sheng Yang2384d2b2008-01-17 15:14:33 +08001271static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1272static DEFINE_SPINLOCK(vmx_vpid_lock);
1273
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001274static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 int size;
1276 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001277 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001279 u32 pin_based_exec_ctrl;
1280 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001281 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001282 u32 vmexit_ctrl;
1283 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001284 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001285} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286
Hannes Ederefff9e52008-11-28 17:02:06 +01001287static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001288 u32 ept;
1289 u32 vpid;
1290} vmx_capability;
1291
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292#define VMX_SEGMENT_FIELD(seg) \
1293 [VCPU_SREG_##seg] = { \
1294 .selector = GUEST_##seg##_SELECTOR, \
1295 .base = GUEST_##seg##_BASE, \
1296 .limit = GUEST_##seg##_LIMIT, \
1297 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1298 }
1299
Mathias Krause772e0312012-08-30 01:30:19 +02001300static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 unsigned selector;
1302 unsigned base;
1303 unsigned limit;
1304 unsigned ar_bytes;
1305} kvm_vmx_segment_fields[] = {
1306 VMX_SEGMENT_FIELD(CS),
1307 VMX_SEGMENT_FIELD(DS),
1308 VMX_SEGMENT_FIELD(ES),
1309 VMX_SEGMENT_FIELD(FS),
1310 VMX_SEGMENT_FIELD(GS),
1311 VMX_SEGMENT_FIELD(SS),
1312 VMX_SEGMENT_FIELD(TR),
1313 VMX_SEGMENT_FIELD(LDTR),
1314};
1315
Avi Kivity26bb0982009-09-07 11:14:12 +03001316static u64 host_efer;
1317
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001318static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1319
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001320/*
Brian Gerst8c065852010-07-17 09:03:26 -04001321 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001322 * away by decrementing the array size.
1323 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001325#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001326 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001328 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001331DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1332
1333#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1334
1335#define KVM_EVMCS_VERSION 1
1336
1337#if IS_ENABLED(CONFIG_HYPERV)
1338static bool __read_mostly enlightened_vmcs = true;
1339module_param(enlightened_vmcs, bool, 0444);
1340
1341static inline void evmcs_write64(unsigned long field, u64 value)
1342{
1343 u16 clean_field;
1344 int offset = get_evmcs_offset(field, &clean_field);
1345
1346 if (offset < 0)
1347 return;
1348
1349 *(u64 *)((char *)current_evmcs + offset) = value;
1350
1351 current_evmcs->hv_clean_fields &= ~clean_field;
1352}
1353
1354static inline void evmcs_write32(unsigned long field, u32 value)
1355{
1356 u16 clean_field;
1357 int offset = get_evmcs_offset(field, &clean_field);
1358
1359 if (offset < 0)
1360 return;
1361
1362 *(u32 *)((char *)current_evmcs + offset) = value;
1363 current_evmcs->hv_clean_fields &= ~clean_field;
1364}
1365
1366static inline void evmcs_write16(unsigned long field, u16 value)
1367{
1368 u16 clean_field;
1369 int offset = get_evmcs_offset(field, &clean_field);
1370
1371 if (offset < 0)
1372 return;
1373
1374 *(u16 *)((char *)current_evmcs + offset) = value;
1375 current_evmcs->hv_clean_fields &= ~clean_field;
1376}
1377
1378static inline u64 evmcs_read64(unsigned long field)
1379{
1380 int offset = get_evmcs_offset(field, NULL);
1381
1382 if (offset < 0)
1383 return 0;
1384
1385 return *(u64 *)((char *)current_evmcs + offset);
1386}
1387
1388static inline u32 evmcs_read32(unsigned long field)
1389{
1390 int offset = get_evmcs_offset(field, NULL);
1391
1392 if (offset < 0)
1393 return 0;
1394
1395 return *(u32 *)((char *)current_evmcs + offset);
1396}
1397
1398static inline u16 evmcs_read16(unsigned long field)
1399{
1400 int offset = get_evmcs_offset(field, NULL);
1401
1402 if (offset < 0)
1403 return 0;
1404
1405 return *(u16 *)((char *)current_evmcs + offset);
1406}
1407
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001408static inline void evmcs_touch_msr_bitmap(void)
1409{
1410 if (unlikely(!current_evmcs))
1411 return;
1412
1413 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1414 current_evmcs->hv_clean_fields &=
1415 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1416}
1417
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001418static void evmcs_load(u64 phys_addr)
1419{
1420 struct hv_vp_assist_page *vp_ap =
1421 hv_get_vp_assist_page(smp_processor_id());
1422
1423 vp_ap->current_nested_vmcs = phys_addr;
1424 vp_ap->enlighten_vmentry = 1;
1425}
1426
1427static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1428{
1429 /*
1430 * Enlightened VMCSv1 doesn't support these:
1431 *
1432 * POSTED_INTR_NV = 0x00000002,
1433 * GUEST_INTR_STATUS = 0x00000810,
1434 * APIC_ACCESS_ADDR = 0x00002014,
1435 * POSTED_INTR_DESC_ADDR = 0x00002016,
1436 * EOI_EXIT_BITMAP0 = 0x0000201c,
1437 * EOI_EXIT_BITMAP1 = 0x0000201e,
1438 * EOI_EXIT_BITMAP2 = 0x00002020,
1439 * EOI_EXIT_BITMAP3 = 0x00002022,
1440 */
1441 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1442 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1443 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1444 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1445 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1446 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1447 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1448
1449 /*
1450 * GUEST_PML_INDEX = 0x00000812,
1451 * PML_ADDRESS = 0x0000200e,
1452 */
1453 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1454
1455 /* VM_FUNCTION_CONTROL = 0x00002018, */
1456 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1457
1458 /*
1459 * EPTP_LIST_ADDRESS = 0x00002024,
1460 * VMREAD_BITMAP = 0x00002026,
1461 * VMWRITE_BITMAP = 0x00002028,
1462 */
1463 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1464
1465 /*
1466 * TSC_MULTIPLIER = 0x00002032,
1467 */
1468 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1469
1470 /*
1471 * PLE_GAP = 0x00004020,
1472 * PLE_WINDOW = 0x00004022,
1473 */
1474 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1475
1476 /*
1477 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1478 */
1479 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1480
1481 /*
1482 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1483 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1484 */
1485 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1486 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1487
1488 /*
1489 * Currently unsupported in KVM:
1490 * GUEST_IA32_RTIT_CTL = 0x00002814,
1491 */
1492}
1493#else /* !IS_ENABLED(CONFIG_HYPERV) */
1494static inline void evmcs_write64(unsigned long field, u64 value) {}
1495static inline void evmcs_write32(unsigned long field, u32 value) {}
1496static inline void evmcs_write16(unsigned long field, u16 value) {}
1497static inline u64 evmcs_read64(unsigned long field) { return 0; }
1498static inline u32 evmcs_read32(unsigned long field) { return 0; }
1499static inline u16 evmcs_read16(unsigned long field) { return 0; }
1500static inline void evmcs_load(u64 phys_addr) {}
1501static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001502static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001503#endif /* IS_ENABLED(CONFIG_HYPERV) */
1504
Jan Kiszka5bb16012016-02-09 20:14:21 +01001505static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506{
1507 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1508 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001509 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1510}
1511
Jan Kiszka6f054852016-02-09 20:15:18 +01001512static inline bool is_debug(u32 intr_info)
1513{
1514 return is_exception_n(intr_info, DB_VECTOR);
1515}
1516
1517static inline bool is_breakpoint(u32 intr_info)
1518{
1519 return is_exception_n(intr_info, BP_VECTOR);
1520}
1521
Jan Kiszka5bb16012016-02-09 20:14:21 +01001522static inline bool is_page_fault(u32 intr_info)
1523{
1524 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525}
1526
Gui Jianfeng31299942010-03-15 17:29:09 +08001527static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001528{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001529 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001530}
1531
Gui Jianfeng31299942010-03-15 17:29:09 +08001532static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001533{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001534 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001535}
1536
Liran Alon9e869482018-03-12 13:12:51 +02001537static inline bool is_gp_fault(u32 intr_info)
1538{
1539 return is_exception_n(intr_info, GP_VECTOR);
1540}
1541
Gui Jianfeng31299942010-03-15 17:29:09 +08001542static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543{
1544 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1545 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1546}
1547
Gui Jianfeng31299942010-03-15 17:29:09 +08001548static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001549{
1550 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1551 INTR_INFO_VALID_MASK)) ==
1552 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1553}
1554
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001555/* Undocumented: icebp/int1 */
1556static inline bool is_icebp(u32 intr_info)
1557{
1558 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1559 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1560}
1561
Gui Jianfeng31299942010-03-15 17:29:09 +08001562static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001563{
Sheng Yang04547152009-04-01 15:52:31 +08001564 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001565}
1566
Gui Jianfeng31299942010-03-15 17:29:09 +08001567static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001568{
Sheng Yang04547152009-04-01 15:52:31 +08001569 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001570}
1571
Paolo Bonzini35754c92015-07-29 12:05:37 +02001572static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001573{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001574 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001575}
1576
Gui Jianfeng31299942010-03-15 17:29:09 +08001577static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001578{
Sheng Yang04547152009-04-01 15:52:31 +08001579 return vmcs_config.cpu_based_exec_ctrl &
1580 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001581}
1582
Avi Kivity774ead32007-12-26 13:57:04 +02001583static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001584{
Sheng Yang04547152009-04-01 15:52:31 +08001585 return vmcs_config.cpu_based_2nd_exec_ctrl &
1586 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1587}
1588
Yang Zhang8d146952013-01-25 10:18:50 +08001589static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1590{
1591 return vmcs_config.cpu_based_2nd_exec_ctrl &
1592 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1593}
1594
Yang Zhang83d4c282013-01-25 10:18:49 +08001595static inline bool cpu_has_vmx_apic_register_virt(void)
1596{
1597 return vmcs_config.cpu_based_2nd_exec_ctrl &
1598 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1599}
1600
Yang Zhangc7c9c562013-01-25 10:18:51 +08001601static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1602{
1603 return vmcs_config.cpu_based_2nd_exec_ctrl &
1604 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1605}
1606
Yunhong Jiang64672c92016-06-13 14:19:59 -07001607/*
1608 * Comment's format: document - errata name - stepping - processor name.
1609 * Refer from
1610 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1611 */
1612static u32 vmx_preemption_cpu_tfms[] = {
1613/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
16140x000206E6,
1615/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1616/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1617/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
16180x00020652,
1619/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
16200x00020655,
1621/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1622/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1623/*
1624 * 320767.pdf - AAP86 - B1 -
1625 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1626 */
16270x000106E5,
1628/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
16290x000106A0,
1630/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
16310x000106A1,
1632/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
16330x000106A4,
1634 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1635 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1636 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
16370x000106A5,
1638};
1639
1640static inline bool cpu_has_broken_vmx_preemption_timer(void)
1641{
1642 u32 eax = cpuid_eax(0x00000001), i;
1643
1644 /* Clear the reserved bits */
1645 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001646 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001647 if (eax == vmx_preemption_cpu_tfms[i])
1648 return true;
1649
1650 return false;
1651}
1652
1653static inline bool cpu_has_vmx_preemption_timer(void)
1654{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001655 return vmcs_config.pin_based_exec_ctrl &
1656 PIN_BASED_VMX_PREEMPTION_TIMER;
1657}
1658
Yang Zhang01e439b2013-04-11 19:25:12 +08001659static inline bool cpu_has_vmx_posted_intr(void)
1660{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001661 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1662 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001663}
1664
1665static inline bool cpu_has_vmx_apicv(void)
1666{
1667 return cpu_has_vmx_apic_register_virt() &&
1668 cpu_has_vmx_virtual_intr_delivery() &&
1669 cpu_has_vmx_posted_intr();
1670}
1671
Sheng Yang04547152009-04-01 15:52:31 +08001672static inline bool cpu_has_vmx_flexpriority(void)
1673{
1674 return cpu_has_vmx_tpr_shadow() &&
1675 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001676}
1677
Marcelo Tosattie7997942009-06-11 12:07:40 -03001678static inline bool cpu_has_vmx_ept_execute_only(void)
1679{
Gui Jianfeng31299942010-03-15 17:29:09 +08001680 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001681}
1682
Marcelo Tosattie7997942009-06-11 12:07:40 -03001683static inline bool cpu_has_vmx_ept_2m_page(void)
1684{
Gui Jianfeng31299942010-03-15 17:29:09 +08001685 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001686}
1687
Sheng Yang878403b2010-01-05 19:02:29 +08001688static inline bool cpu_has_vmx_ept_1g_page(void)
1689{
Gui Jianfeng31299942010-03-15 17:29:09 +08001690 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001691}
1692
Sheng Yang4bc9b982010-06-02 14:05:24 +08001693static inline bool cpu_has_vmx_ept_4levels(void)
1694{
1695 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1696}
1697
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001698static inline bool cpu_has_vmx_ept_mt_wb(void)
1699{
1700 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1701}
1702
Yu Zhang855feb62017-08-24 20:27:55 +08001703static inline bool cpu_has_vmx_ept_5levels(void)
1704{
1705 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1706}
1707
Xudong Hao83c3a332012-05-28 19:33:35 +08001708static inline bool cpu_has_vmx_ept_ad_bits(void)
1709{
1710 return vmx_capability.ept & VMX_EPT_AD_BIT;
1711}
1712
Gui Jianfeng31299942010-03-15 17:29:09 +08001713static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001714{
Gui Jianfeng31299942010-03-15 17:29:09 +08001715 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001716}
1717
Gui Jianfeng31299942010-03-15 17:29:09 +08001718static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001719{
Gui Jianfeng31299942010-03-15 17:29:09 +08001720 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001721}
1722
Liran Aloncd9a4912018-05-22 17:16:15 +03001723static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1724{
1725 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1726}
1727
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001728static inline bool cpu_has_vmx_invvpid_single(void)
1729{
1730 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1731}
1732
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001733static inline bool cpu_has_vmx_invvpid_global(void)
1734{
1735 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1736}
1737
Wanpeng Li08d839c2017-03-23 05:30:08 -07001738static inline bool cpu_has_vmx_invvpid(void)
1739{
1740 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1741}
1742
Gui Jianfeng31299942010-03-15 17:29:09 +08001743static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001744{
Sheng Yang04547152009-04-01 15:52:31 +08001745 return vmcs_config.cpu_based_2nd_exec_ctrl &
1746 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001747}
1748
Gui Jianfeng31299942010-03-15 17:29:09 +08001749static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001750{
1751 return vmcs_config.cpu_based_2nd_exec_ctrl &
1752 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1753}
1754
Gui Jianfeng31299942010-03-15 17:29:09 +08001755static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001756{
1757 return vmcs_config.cpu_based_2nd_exec_ctrl &
1758 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1759}
1760
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001761static inline bool cpu_has_vmx_basic_inout(void)
1762{
1763 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1764}
1765
Paolo Bonzini35754c92015-07-29 12:05:37 +02001766static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001767{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001768 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001769}
1770
Gui Jianfeng31299942010-03-15 17:29:09 +08001771static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001772{
Sheng Yang04547152009-04-01 15:52:31 +08001773 return vmcs_config.cpu_based_2nd_exec_ctrl &
1774 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001775}
1776
Gui Jianfeng31299942010-03-15 17:29:09 +08001777static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001778{
1779 return vmcs_config.cpu_based_2nd_exec_ctrl &
1780 SECONDARY_EXEC_RDTSCP;
1781}
1782
Mao, Junjiead756a12012-07-02 01:18:48 +00001783static inline bool cpu_has_vmx_invpcid(void)
1784{
1785 return vmcs_config.cpu_based_2nd_exec_ctrl &
1786 SECONDARY_EXEC_ENABLE_INVPCID;
1787}
1788
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001789static inline bool cpu_has_virtual_nmis(void)
1790{
1791 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1792}
1793
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001794static inline bool cpu_has_vmx_wbinvd_exit(void)
1795{
1796 return vmcs_config.cpu_based_2nd_exec_ctrl &
1797 SECONDARY_EXEC_WBINVD_EXITING;
1798}
1799
Abel Gordonabc4fc52013-04-18 14:35:25 +03001800static inline bool cpu_has_vmx_shadow_vmcs(void)
1801{
1802 u64 vmx_msr;
1803 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1804 /* check if the cpu supports writing r/o exit information fields */
1805 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1806 return false;
1807
1808 return vmcs_config.cpu_based_2nd_exec_ctrl &
1809 SECONDARY_EXEC_SHADOW_VMCS;
1810}
1811
Kai Huang843e4332015-01-28 10:54:28 +08001812static inline bool cpu_has_vmx_pml(void)
1813{
1814 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1815}
1816
Haozhong Zhang64903d62015-10-20 15:39:09 +08001817static inline bool cpu_has_vmx_tsc_scaling(void)
1818{
1819 return vmcs_config.cpu_based_2nd_exec_ctrl &
1820 SECONDARY_EXEC_TSC_SCALING;
1821}
1822
Bandan Das2a499e42017-08-03 15:54:41 -04001823static inline bool cpu_has_vmx_vmfunc(void)
1824{
1825 return vmcs_config.cpu_based_2nd_exec_ctrl &
1826 SECONDARY_EXEC_ENABLE_VMFUNC;
1827}
1828
Sean Christopherson64f7a112018-04-30 10:01:06 -07001829static bool vmx_umip_emulated(void)
1830{
1831 return vmcs_config.cpu_based_2nd_exec_ctrl &
1832 SECONDARY_EXEC_DESC;
1833}
1834
Sheng Yang04547152009-04-01 15:52:31 +08001835static inline bool report_flexpriority(void)
1836{
1837 return flexpriority_enabled;
1838}
1839
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001840static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1841{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001842 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001843}
1844
Jim Mattsonf4160e42018-05-29 09:11:33 -07001845/*
1846 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1847 * to modify any valid field of the VMCS, or are the VM-exit
1848 * information fields read-only?
1849 */
1850static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1851{
1852 return to_vmx(vcpu)->nested.msrs.misc_low &
1853 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1854}
1855
Marc Orr04473782018-06-20 17:21:29 -07001856static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1857{
1858 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1859}
1860
1861static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1862{
1863 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1864 CPU_BASED_MONITOR_TRAP_FLAG;
1865}
1866
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001867static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1868{
1869 return vmcs12->cpu_based_vm_exec_control & bit;
1870}
1871
1872static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1873{
1874 return (vmcs12->cpu_based_vm_exec_control &
1875 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1876 (vmcs12->secondary_vm_exec_control & bit);
1877}
1878
Jan Kiszkaf4124502014-03-07 20:03:13 +01001879static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1880{
1881 return vmcs12->pin_based_vm_exec_control &
1882 PIN_BASED_VMX_PREEMPTION_TIMER;
1883}
1884
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001885static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1886{
1887 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1888}
1889
1890static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1891{
1892 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1893}
1894
Nadav Har'El155a97a2013-08-05 11:07:16 +03001895static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1896{
1897 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1898}
1899
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001900static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1901{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001902 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001903}
1904
Bandan Dasc5f983f2017-05-05 15:25:14 -04001905static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1906{
1907 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1908}
1909
Wincy Vanf2b93282015-02-03 23:56:03 +08001910static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1911{
1912 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1913}
1914
Wanpeng Li5c614b32015-10-13 09:18:36 -07001915static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1916{
1917 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1918}
1919
Wincy Van82f0dd42015-02-03 23:57:18 +08001920static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1921{
1922 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1923}
1924
Wincy Van608406e2015-02-03 23:57:51 +08001925static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1926{
1927 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1928}
1929
Wincy Van705699a2015-02-03 23:58:17 +08001930static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1931{
1932 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1933}
1934
Bandan Das27c42a12017-08-03 15:54:42 -04001935static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1936{
1937 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1938}
1939
Bandan Das41ab9372017-08-03 15:54:43 -04001940static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1941{
1942 return nested_cpu_has_vmfunc(vmcs12) &&
1943 (vmcs12->vm_function_control &
1944 VMX_VMFUNC_EPTP_SWITCHING);
1945}
1946
Jim Mattsonef85b672016-12-12 11:01:37 -08001947static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001948{
1949 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001950 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001951}
1952
Jan Kiszka533558b2014-01-04 18:47:20 +01001953static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1954 u32 exit_intr_info,
1955 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001956static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1957 struct vmcs12 *vmcs12,
1958 u32 reason, unsigned long qualification);
1959
Rusty Russell8b9cf982007-07-30 16:31:43 +10001960static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001961{
1962 int i;
1963
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001964 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001965 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001966 return i;
1967 return -1;
1968}
1969
Sheng Yang2384d2b2008-01-17 15:14:33 +08001970static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1971{
1972 struct {
1973 u64 vpid : 16;
1974 u64 rsvd : 48;
1975 u64 gva;
1976 } operand = { vpid, 0, gva };
1977
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001978 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001979 /* CF==1 or ZF==1 --> rc = -1 */
1980 "; ja 1f ; ud2 ; 1:"
1981 : : "a"(&operand), "c"(ext) : "cc", "memory");
1982}
1983
Sheng Yang14394422008-04-28 12:24:45 +08001984static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1985{
1986 struct {
1987 u64 eptp, gpa;
1988 } operand = {eptp, gpa};
1989
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001990 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001991 /* CF==1 or ZF==1 --> rc = -1 */
1992 "; ja 1f ; ud2 ; 1:\n"
1993 : : "a" (&operand), "c" (ext) : "cc", "memory");
1994}
1995
Avi Kivity26bb0982009-09-07 11:14:12 +03001996static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001997{
1998 int i;
1999
Rusty Russell8b9cf982007-07-30 16:31:43 +10002000 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002001 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002002 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002003 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002004}
2005
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006static void vmcs_clear(struct vmcs *vmcs)
2007{
2008 u64 phys_addr = __pa(vmcs);
2009 u8 error;
2010
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002011 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02002012 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013 : "cc", "memory");
2014 if (error)
2015 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2016 vmcs, phys_addr);
2017}
2018
Nadav Har'Eld462b812011-05-24 15:26:10 +03002019static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2020{
2021 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002022 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2023 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002024 loaded_vmcs->cpu = -1;
2025 loaded_vmcs->launched = 0;
2026}
2027
Dongxiao Xu7725b892010-05-11 18:29:38 +08002028static void vmcs_load(struct vmcs *vmcs)
2029{
2030 u64 phys_addr = __pa(vmcs);
2031 u8 error;
2032
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002033 if (static_branch_unlikely(&enable_evmcs))
2034 return evmcs_load(phys_addr);
2035
Dongxiao Xu7725b892010-05-11 18:29:38 +08002036 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02002037 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002038 : "cc", "memory");
2039 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03002040 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002041 vmcs, phys_addr);
2042}
2043
Dave Young2965faa2015-09-09 15:38:55 -07002044#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002045/*
2046 * This bitmap is used to indicate whether the vmclear
2047 * operation is enabled on all cpus. All disabled by
2048 * default.
2049 */
2050static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2051
2052static inline void crash_enable_local_vmclear(int cpu)
2053{
2054 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2055}
2056
2057static inline void crash_disable_local_vmclear(int cpu)
2058{
2059 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2060}
2061
2062static inline int crash_local_vmclear_enabled(int cpu)
2063{
2064 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2065}
2066
2067static void crash_vmclear_local_loaded_vmcss(void)
2068{
2069 int cpu = raw_smp_processor_id();
2070 struct loaded_vmcs *v;
2071
2072 if (!crash_local_vmclear_enabled(cpu))
2073 return;
2074
2075 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2076 loaded_vmcss_on_cpu_link)
2077 vmcs_clear(v->vmcs);
2078}
2079#else
2080static inline void crash_enable_local_vmclear(int cpu) { }
2081static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002082#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002083
Nadav Har'Eld462b812011-05-24 15:26:10 +03002084static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002086 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002087 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088
Nadav Har'Eld462b812011-05-24 15:26:10 +03002089 if (loaded_vmcs->cpu != cpu)
2090 return; /* vcpu migration can race with cpu offline */
2091 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002093 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002094 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002095
2096 /*
2097 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2098 * is before setting loaded_vmcs->vcpu to -1 which is done in
2099 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2100 * then adds the vmcs into percpu list before it is deleted.
2101 */
2102 smp_wmb();
2103
Nadav Har'Eld462b812011-05-24 15:26:10 +03002104 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002105 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106}
2107
Nadav Har'Eld462b812011-05-24 15:26:10 +03002108static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002109{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002110 int cpu = loaded_vmcs->cpu;
2111
2112 if (cpu != -1)
2113 smp_call_function_single(cpu,
2114 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002115}
2116
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002117static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002118{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002119 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002120 return;
2121
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002122 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002123 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002124}
2125
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002126static inline void vpid_sync_vcpu_global(void)
2127{
2128 if (cpu_has_vmx_invvpid_global())
2129 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2130}
2131
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002132static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002133{
2134 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002135 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002136 else
2137 vpid_sync_vcpu_global();
2138}
2139
Sheng Yang14394422008-04-28 12:24:45 +08002140static inline void ept_sync_global(void)
2141{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002142 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002143}
2144
2145static inline void ept_sync_context(u64 eptp)
2146{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002147 if (cpu_has_vmx_invept_context())
2148 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2149 else
2150 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002151}
2152
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002153static __always_inline void vmcs_check16(unsigned long field)
2154{
2155 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2156 "16-bit accessor invalid for 64-bit field");
2157 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2158 "16-bit accessor invalid for 64-bit high field");
2159 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2160 "16-bit accessor invalid for 32-bit high field");
2161 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2162 "16-bit accessor invalid for natural width field");
2163}
2164
2165static __always_inline void vmcs_check32(unsigned long field)
2166{
2167 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2168 "32-bit accessor invalid for 16-bit field");
2169 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2170 "32-bit accessor invalid for natural width field");
2171}
2172
2173static __always_inline void vmcs_check64(unsigned long field)
2174{
2175 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2176 "64-bit accessor invalid for 16-bit field");
2177 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2178 "64-bit accessor invalid for 64-bit high field");
2179 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2180 "64-bit accessor invalid for 32-bit field");
2181 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2182 "64-bit accessor invalid for natural width field");
2183}
2184
2185static __always_inline void vmcs_checkl(unsigned long field)
2186{
2187 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2188 "Natural width accessor invalid for 16-bit field");
2189 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2190 "Natural width accessor invalid for 64-bit field");
2191 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2192 "Natural width accessor invalid for 64-bit high field");
2193 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2194 "Natural width accessor invalid for 32-bit field");
2195}
2196
2197static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198{
Avi Kivity5e520e62011-05-15 10:13:12 -04002199 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200
Avi Kivity5e520e62011-05-15 10:13:12 -04002201 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2202 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 return value;
2204}
2205
Avi Kivity96304212011-05-15 10:13:13 -04002206static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002208 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002209 if (static_branch_unlikely(&enable_evmcs))
2210 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002211 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212}
2213
Avi Kivity96304212011-05-15 10:13:13 -04002214static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002216 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002217 if (static_branch_unlikely(&enable_evmcs))
2218 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002219 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220}
2221
Avi Kivity96304212011-05-15 10:13:13 -04002222static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002224 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002225 if (static_branch_unlikely(&enable_evmcs))
2226 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002227#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002228 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002230 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231#endif
2232}
2233
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002234static __always_inline unsigned long vmcs_readl(unsigned long field)
2235{
2236 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002237 if (static_branch_unlikely(&enable_evmcs))
2238 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002239 return __vmcs_readl(field);
2240}
2241
Avi Kivitye52de1b2007-01-05 16:36:56 -08002242static noinline void vmwrite_error(unsigned long field, unsigned long value)
2243{
2244 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2245 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2246 dump_stack();
2247}
2248
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002249static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
2251 u8 error;
2252
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002253 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002254 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002255 if (unlikely(error))
2256 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257}
2258
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002259static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002261 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002262 if (static_branch_unlikely(&enable_evmcs))
2263 return evmcs_write16(field, value);
2264
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002265 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266}
2267
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002268static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002270 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002271 if (static_branch_unlikely(&enable_evmcs))
2272 return evmcs_write32(field, value);
2273
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002274 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275}
2276
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002277static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002279 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002280 if (static_branch_unlikely(&enable_evmcs))
2281 return evmcs_write64(field, value);
2282
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002283 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002284#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002286 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287#endif
2288}
2289
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002290static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002291{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002292 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002293 if (static_branch_unlikely(&enable_evmcs))
2294 return evmcs_write64(field, value);
2295
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002296 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002297}
2298
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002299static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002300{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002301 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2302 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002303 if (static_branch_unlikely(&enable_evmcs))
2304 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2305
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002306 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2307}
2308
2309static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2310{
2311 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2312 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002313 if (static_branch_unlikely(&enable_evmcs))
2314 return evmcs_write32(field, evmcs_read32(field) | mask);
2315
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002316 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002317}
2318
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002319static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2320{
2321 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2322}
2323
Gleb Natapov2961e8762013-11-25 15:37:13 +02002324static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2325{
2326 vmcs_write32(VM_ENTRY_CONTROLS, val);
2327 vmx->vm_entry_controls_shadow = val;
2328}
2329
2330static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2331{
2332 if (vmx->vm_entry_controls_shadow != val)
2333 vm_entry_controls_init(vmx, val);
2334}
2335
2336static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2337{
2338 return vmx->vm_entry_controls_shadow;
2339}
2340
2341
2342static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2343{
2344 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2345}
2346
2347static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2348{
2349 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2350}
2351
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002352static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2353{
2354 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2355}
2356
Gleb Natapov2961e8762013-11-25 15:37:13 +02002357static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2358{
2359 vmcs_write32(VM_EXIT_CONTROLS, val);
2360 vmx->vm_exit_controls_shadow = val;
2361}
2362
2363static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2364{
2365 if (vmx->vm_exit_controls_shadow != val)
2366 vm_exit_controls_init(vmx, val);
2367}
2368
2369static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2370{
2371 return vmx->vm_exit_controls_shadow;
2372}
2373
2374
2375static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2376{
2377 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2378}
2379
2380static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2381{
2382 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2383}
2384
Avi Kivity2fb92db2011-04-27 19:42:18 +03002385static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2386{
2387 vmx->segment_cache.bitmask = 0;
2388}
2389
2390static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2391 unsigned field)
2392{
2393 bool ret;
2394 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2395
2396 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2397 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2398 vmx->segment_cache.bitmask = 0;
2399 }
2400 ret = vmx->segment_cache.bitmask & mask;
2401 vmx->segment_cache.bitmask |= mask;
2402 return ret;
2403}
2404
2405static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2406{
2407 u16 *p = &vmx->segment_cache.seg[seg].selector;
2408
2409 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2410 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2411 return *p;
2412}
2413
2414static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2415{
2416 ulong *p = &vmx->segment_cache.seg[seg].base;
2417
2418 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2419 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2420 return *p;
2421}
2422
2423static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2424{
2425 u32 *p = &vmx->segment_cache.seg[seg].limit;
2426
2427 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2428 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2429 return *p;
2430}
2431
2432static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2433{
2434 u32 *p = &vmx->segment_cache.seg[seg].ar;
2435
2436 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2437 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2438 return *p;
2439}
2440
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002441static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2442{
2443 u32 eb;
2444
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002445 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002446 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002447 /*
2448 * Guest access to VMware backdoor ports could legitimately
2449 * trigger #GP because of TSS I/O permission bitmap.
2450 * We intercept those #GP and allow access to them anyway
2451 * as VMware does.
2452 */
2453 if (enable_vmware_backdoor)
2454 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002455 if ((vcpu->guest_debug &
2456 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2457 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2458 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002459 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002460 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002461 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002462 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002463
2464 /* When we are running a nested L2 guest and L1 specified for it a
2465 * certain exception bitmap, we must trap the same exceptions and pass
2466 * them to L1. When running L2, we will only handle the exceptions
2467 * specified above if L1 did not want them.
2468 */
2469 if (is_guest_mode(vcpu))
2470 eb |= get_vmcs12(vcpu)->exception_bitmap;
2471
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002472 vmcs_write32(EXCEPTION_BITMAP, eb);
2473}
2474
Ashok Raj15d45072018-02-01 22:59:43 +01002475/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002476 * Check if MSR is intercepted for currently loaded MSR bitmap.
2477 */
2478static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2479{
2480 unsigned long *msr_bitmap;
2481 int f = sizeof(unsigned long);
2482
2483 if (!cpu_has_vmx_msr_bitmap())
2484 return true;
2485
2486 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2487
2488 if (msr <= 0x1fff) {
2489 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2490 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2491 msr &= 0x1fff;
2492 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2493 }
2494
2495 return true;
2496}
2497
2498/*
Ashok Raj15d45072018-02-01 22:59:43 +01002499 * Check if MSR is intercepted for L01 MSR bitmap.
2500 */
2501static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2502{
2503 unsigned long *msr_bitmap;
2504 int f = sizeof(unsigned long);
2505
2506 if (!cpu_has_vmx_msr_bitmap())
2507 return true;
2508
2509 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2510
2511 if (msr <= 0x1fff) {
2512 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2513 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2514 msr &= 0x1fff;
2515 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2516 }
2517
2518 return true;
2519}
2520
Gleb Natapov2961e8762013-11-25 15:37:13 +02002521static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2522 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002523{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002524 vm_entry_controls_clearbit(vmx, entry);
2525 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002526}
2527
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002528static int find_msr(struct vmx_msrs *m, unsigned int msr)
2529{
2530 unsigned int i;
2531
2532 for (i = 0; i < m->nr; ++i) {
2533 if (m->val[i].index == msr)
2534 return i;
2535 }
2536 return -ENOENT;
2537}
2538
Avi Kivity61d2ef22010-04-28 16:40:38 +03002539static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2540{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002541 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002542 struct msr_autoload *m = &vmx->msr_autoload;
2543
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002544 switch (msr) {
2545 case MSR_EFER:
2546 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002547 clear_atomic_switch_msr_special(vmx,
2548 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002549 VM_EXIT_LOAD_IA32_EFER);
2550 return;
2551 }
2552 break;
2553 case MSR_CORE_PERF_GLOBAL_CTRL:
2554 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002555 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002556 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2557 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2558 return;
2559 }
2560 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002561 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002562 i = find_msr(&m->guest, msr);
2563 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002564 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002565 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002566 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002567 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002568
2569skip_guest:
2570 i = find_msr(&m->host, msr);
2571 if (i < 0)
2572 return;
2573
2574 --m->host.nr;
2575 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002576 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002577}
2578
Gleb Natapov2961e8762013-11-25 15:37:13 +02002579static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2580 unsigned long entry, unsigned long exit,
2581 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2582 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002583{
2584 vmcs_write64(guest_val_vmcs, guest_val);
2585 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002586 vm_entry_controls_setbit(vmx, entry);
2587 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002588}
2589
Avi Kivity61d2ef22010-04-28 16:40:38 +03002590static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002591 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002592{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002593 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002594 struct msr_autoload *m = &vmx->msr_autoload;
2595
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002596 switch (msr) {
2597 case MSR_EFER:
2598 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002599 add_atomic_switch_msr_special(vmx,
2600 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002601 VM_EXIT_LOAD_IA32_EFER,
2602 GUEST_IA32_EFER,
2603 HOST_IA32_EFER,
2604 guest_val, host_val);
2605 return;
2606 }
2607 break;
2608 case MSR_CORE_PERF_GLOBAL_CTRL:
2609 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002610 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002611 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2612 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2613 GUEST_IA32_PERF_GLOBAL_CTRL,
2614 HOST_IA32_PERF_GLOBAL_CTRL,
2615 guest_val, host_val);
2616 return;
2617 }
2618 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002619 case MSR_IA32_PEBS_ENABLE:
2620 /* PEBS needs a quiescent period after being disabled (to write
2621 * a record). Disabling PEBS through VMX MSR swapping doesn't
2622 * provide that period, so a CPU could write host's record into
2623 * guest's memory.
2624 */
2625 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002626 }
2627
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002628 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002629 if (!entry_only)
2630 j = find_msr(&m->host, msr);
2631
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002632 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002633 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002634 "Can't add msr %x\n", msr);
2635 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002636 }
2637 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002638 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002639 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002640 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002641 m->guest.val[i].index = msr;
2642 m->guest.val[i].value = guest_val;
2643
2644 if (entry_only)
2645 return;
2646
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002647 if (j < 0) {
2648 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002649 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002650 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002651 m->host.val[j].index = msr;
2652 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002653}
2654
Avi Kivity92c0d902009-10-29 11:00:16 +02002655static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002656{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002657 u64 guest_efer = vmx->vcpu.arch.efer;
2658 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002659
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002660 if (!enable_ept) {
2661 /*
2662 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2663 * host CPUID is more efficient than testing guest CPUID
2664 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2665 */
2666 if (boot_cpu_has(X86_FEATURE_SMEP))
2667 guest_efer |= EFER_NX;
2668 else if (!(guest_efer & EFER_NX))
2669 ignore_bits |= EFER_NX;
2670 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002671
Avi Kivity51c6cf62007-08-29 03:48:05 +03002672 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002673 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002674 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002675 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002676#ifdef CONFIG_X86_64
2677 ignore_bits |= EFER_LMA | EFER_LME;
2678 /* SCE is meaningful only in long mode on Intel */
2679 if (guest_efer & EFER_LMA)
2680 ignore_bits &= ~(u64)EFER_SCE;
2681#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002682
2683 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002684
2685 /*
2686 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2687 * On CPUs that support "load IA32_EFER", always switch EFER
2688 * atomically, since it's faster than switching it manually.
2689 */
2690 if (cpu_has_load_ia32_efer ||
2691 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002692 if (!(guest_efer & EFER_LMA))
2693 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002694 if (guest_efer != host_efer)
2695 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002696 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002697 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002698 } else {
2699 guest_efer &= ~ignore_bits;
2700 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002701
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002702 vmx->guest_msrs[efer_offset].data = guest_efer;
2703 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2704
2705 return true;
2706 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002707}
2708
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002709#ifdef CONFIG_X86_32
2710/*
2711 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2712 * VMCS rather than the segment table. KVM uses this helper to figure
2713 * out the current bases to poke them into the VMCS before entry.
2714 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002715static unsigned long segment_base(u16 selector)
2716{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002717 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002718 unsigned long v;
2719
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002720 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002721 return 0;
2722
Thomas Garnier45fc8752017-03-14 10:05:08 -07002723 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002724
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002725 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002726 u16 ldt_selector = kvm_read_ldt();
2727
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002728 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002729 return 0;
2730
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002731 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002732 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002733 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002734 return v;
2735}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002736#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002737
Avi Kivity04d2cc72007-09-10 18:10:54 +03002738static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002739{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002740 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002741#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002742 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002743 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002744#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002745 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002746
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002747 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002748 return;
2749
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002750 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002751 /*
2752 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2753 * allow segment selectors with cpl > 0 or ti == 1.
2754 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002755 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002756 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002757
2758#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002759 if (likely(is_64bit_mm(current->mm))) {
2760 save_fsgs_for_kvm();
2761 vmx->host_state.fs_sel = current->thread.fsindex;
2762 vmx->host_state.gs_sel = current->thread.gsindex;
2763 fs_base = current->thread.fsbase;
2764 kernel_gs_base = current->thread.gsbase;
2765 } else {
2766#endif
2767 savesegment(fs, vmx->host_state.fs_sel);
2768 savesegment(gs, vmx->host_state.gs_sel);
2769#ifdef CONFIG_X86_64
2770 fs_base = read_msr(MSR_FS_BASE);
2771 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2772 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002773#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002774 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002775 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002776 vmx->host_state.fs_reload_needed = 0;
2777 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002778 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002779 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002780 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002781 if (!(vmx->host_state.gs_sel & 7))
2782 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002783 else {
2784 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002785 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002786 }
2787
2788#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002789 savesegment(ds, vmx->host_state.ds_sel);
2790 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002791
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002792 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002793 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002794
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002795 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002796 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002797 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002798#else
2799 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2800 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2801#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002802 if (boot_cpu_has(X86_FEATURE_MPX))
2803 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002804 for (i = 0; i < vmx->save_nmsrs; ++i)
2805 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002806 vmx->guest_msrs[i].data,
2807 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002808}
2809
Avi Kivitya9b21b62008-06-24 11:48:49 +03002810static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002811{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002812 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002813 return;
2814
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002815 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002816 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002817#ifdef CONFIG_X86_64
2818 if (is_long_mode(&vmx->vcpu))
2819 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2820#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002821 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002822 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002823#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002824 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002825#else
2826 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002827#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002828 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002829 if (vmx->host_state.fs_reload_needed)
2830 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002831#ifdef CONFIG_X86_64
2832 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2833 loadsegment(ds, vmx->host_state.ds_sel);
2834 loadsegment(es, vmx->host_state.es_sel);
2835 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002836#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002837 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002838#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002839 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002840#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002841 if (vmx->host_state.msr_host_bndcfgs)
2842 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002843 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002844}
2845
Avi Kivitya9b21b62008-06-24 11:48:49 +03002846static void vmx_load_host_state(struct vcpu_vmx *vmx)
2847{
2848 preempt_disable();
2849 __vmx_load_host_state(vmx);
2850 preempt_enable();
2851}
2852
Feng Wu28b835d2015-09-18 22:29:54 +08002853static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2854{
2855 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2856 struct pi_desc old, new;
2857 unsigned int dest;
2858
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002859 /*
2860 * In case of hot-plug or hot-unplug, we may have to undo
2861 * vmx_vcpu_pi_put even if there is no assigned device. And we
2862 * always keep PI.NDST up to date for simplicity: it makes the
2863 * code easier, and CPU migration is not a fast path.
2864 */
2865 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002866 return;
2867
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002868 /*
2869 * First handle the simple case where no cmpxchg is necessary; just
2870 * allow posting non-urgent interrupts.
2871 *
2872 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2873 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2874 * expects the VCPU to be on the blocked_vcpu_list that matches
2875 * PI.NDST.
2876 */
2877 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2878 vcpu->cpu == cpu) {
2879 pi_clear_sn(pi_desc);
2880 return;
2881 }
2882
2883 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002884 do {
2885 old.control = new.control = pi_desc->control;
2886
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002887 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002888
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002889 if (x2apic_enabled())
2890 new.ndst = dest;
2891 else
2892 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002893
Feng Wu28b835d2015-09-18 22:29:54 +08002894 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002895 } while (cmpxchg64(&pi_desc->control, old.control,
2896 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002897}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002898
Peter Feinerc95ba922016-08-17 09:36:47 -07002899static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2900{
2901 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2902 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2903}
2904
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905/*
2906 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2907 * vcpu mutex is already taken.
2908 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002909static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002911 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002912 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002914 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002915 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002916 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002917 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002918
2919 /*
2920 * Read loaded_vmcs->cpu should be before fetching
2921 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2922 * See the comments in __loaded_vmcs_clear().
2923 */
2924 smp_rmb();
2925
Nadav Har'Eld462b812011-05-24 15:26:10 +03002926 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2927 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002928 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002929 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002930 }
2931
2932 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2933 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2934 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002935 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002936 }
2937
2938 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002939 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002940 unsigned long sysenter_esp;
2941
2942 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002943
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 /*
2945 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002946 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002948 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002949 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002950 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002952 /*
2953 * VM exits change the host TR limit to 0x67 after a VM
2954 * exit. This is okay, since 0x67 covers everything except
2955 * the IO bitmap and have have code to handle the IO bitmap
2956 * being lost after a VM exit.
2957 */
2958 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2959
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2961 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002962
Nadav Har'Eld462b812011-05-24 15:26:10 +03002963 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 }
Feng Wu28b835d2015-09-18 22:29:54 +08002965
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002966 /* Setup TSC multiplier */
2967 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002968 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2969 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002970
Feng Wu28b835d2015-09-18 22:29:54 +08002971 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002972 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002973 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002974}
2975
2976static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2977{
2978 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2979
2980 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002981 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2982 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002983 return;
2984
2985 /* Set SN when the vCPU is preempted */
2986 if (vcpu->preempted)
2987 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988}
2989
2990static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2991{
Feng Wu28b835d2015-09-18 22:29:54 +08002992 vmx_vcpu_pi_put(vcpu);
2993
Avi Kivitya9b21b62008-06-24 11:48:49 +03002994 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995}
2996
Wanpeng Lif244dee2017-07-20 01:11:54 -07002997static bool emulation_required(struct kvm_vcpu *vcpu)
2998{
2999 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3000}
3001
Avi Kivityedcafe32009-12-30 18:07:40 +02003002static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3003
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003004/*
3005 * Return the cr0 value that a nested guest would read. This is a combination
3006 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3007 * its hypervisor (cr0_read_shadow).
3008 */
3009static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3010{
3011 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3012 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3013}
3014static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3015{
3016 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3017 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3018}
3019
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3021{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003022 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003023
Avi Kivity6de12732011-03-07 12:51:22 +02003024 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3025 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3026 rflags = vmcs_readl(GUEST_RFLAGS);
3027 if (to_vmx(vcpu)->rmode.vm86_active) {
3028 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3029 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3030 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3031 }
3032 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003033 }
Avi Kivity6de12732011-03-07 12:51:22 +02003034 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035}
3036
3037static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3038{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003039 unsigned long old_rflags = vmx_get_rflags(vcpu);
3040
Avi Kivity6de12732011-03-07 12:51:22 +02003041 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3042 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003043 if (to_vmx(vcpu)->rmode.vm86_active) {
3044 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003045 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003046 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003048
3049 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3050 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051}
3052
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003053static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003054{
3055 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3056 int ret = 0;
3057
3058 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003059 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003060 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003061 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003062
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003063 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003064}
3065
3066static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3067{
3068 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3069 u32 interruptibility = interruptibility_old;
3070
3071 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3072
Jan Kiszka48005f62010-02-19 19:38:07 +01003073 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003074 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003075 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003076 interruptibility |= GUEST_INTR_STATE_STI;
3077
3078 if ((interruptibility != interruptibility_old))
3079 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3080}
3081
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3083{
3084 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003086 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003088 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089
Glauber Costa2809f5d2009-05-12 16:21:05 -04003090 /* skipping an emulated instruction also counts */
3091 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092}
3093
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003094static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3095 unsigned long exit_qual)
3096{
3097 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3098 unsigned int nr = vcpu->arch.exception.nr;
3099 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3100
3101 if (vcpu->arch.exception.has_error_code) {
3102 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3103 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3104 }
3105
3106 if (kvm_exception_is_soft(nr))
3107 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3108 else
3109 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3110
3111 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3112 vmx_get_nmi_mask(vcpu))
3113 intr_info |= INTR_INFO_UNBLOCK_NMI;
3114
3115 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3116}
3117
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003118/*
3119 * KVM wants to inject page-faults which it got to the guest. This function
3120 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003121 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003122static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003123{
3124 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003125 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003126
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003127 if (nr == PF_VECTOR) {
3128 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003129 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003130 return 1;
3131 }
3132 /*
3133 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3134 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3135 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3136 * can be written only when inject_pending_event runs. This should be
3137 * conditional on a new capability---if the capability is disabled,
3138 * kvm_multiple_exception would write the ancillary information to
3139 * CR2 or DR6, for backwards ABI-compatibility.
3140 */
3141 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3142 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003143 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003144 return 1;
3145 }
3146 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003147 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003148 if (nr == DB_VECTOR)
3149 *exit_qual = vcpu->arch.dr6;
3150 else
3151 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003152 return 1;
3153 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003154 }
3155
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003156 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003157}
3158
Wanpeng Licaa057a2018-03-12 04:53:03 -07003159static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3160{
3161 /*
3162 * Ensure that we clear the HLT state in the VMCS. We don't need to
3163 * explicitly skip the instruction because if the HLT state is set,
3164 * then the instruction is already executing and RIP has already been
3165 * advanced.
3166 */
3167 if (kvm_hlt_in_guest(vcpu->kvm) &&
3168 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3169 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3170}
3171
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003172static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003173{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003174 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003175 unsigned nr = vcpu->arch.exception.nr;
3176 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003177 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003178 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003179
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003180 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003181 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003182 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3183 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003184
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003185 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003186 int inc_eip = 0;
3187 if (kvm_exception_is_soft(nr))
3188 inc_eip = vcpu->arch.event_exit_inst_len;
3189 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003190 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003191 return;
3192 }
3193
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003194 WARN_ON_ONCE(vmx->emulation_required);
3195
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003196 if (kvm_exception_is_soft(nr)) {
3197 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3198 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003199 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3200 } else
3201 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3202
3203 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003204
3205 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003206}
3207
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003208static bool vmx_rdtscp_supported(void)
3209{
3210 return cpu_has_vmx_rdtscp();
3211}
3212
Mao, Junjiead756a12012-07-02 01:18:48 +00003213static bool vmx_invpcid_supported(void)
3214{
3215 return cpu_has_vmx_invpcid() && enable_ept;
3216}
3217
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218/*
Eddie Donga75beee2007-05-17 18:55:15 +03003219 * Swap MSR entry in host/guest MSR entry array.
3220 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003221static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003222{
Avi Kivity26bb0982009-09-07 11:14:12 +03003223 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003224
3225 tmp = vmx->guest_msrs[to];
3226 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3227 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003228}
3229
3230/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003231 * Set up the vmcs to automatically save and restore system
3232 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3233 * mode, as fiddling with msrs is very expensive.
3234 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003235static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003236{
Avi Kivity26bb0982009-09-07 11:14:12 +03003237 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003238
Eddie Donga75beee2007-05-17 18:55:15 +03003239 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003240#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003241 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003242 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003243 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003244 move_msr_up(vmx, index, save_nmsrs++);
3245 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003246 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003247 move_msr_up(vmx, index, save_nmsrs++);
3248 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003249 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003250 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003251 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003252 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003253 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003254 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003255 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003256 * if efer.sce is enabled.
3257 */
Brian Gerst8c065852010-07-17 09:03:26 -04003258 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003259 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003260 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003261 }
Eddie Donga75beee2007-05-17 18:55:15 +03003262#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003263 index = __find_msr_index(vmx, MSR_EFER);
3264 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003265 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003266
Avi Kivity26bb0982009-09-07 11:14:12 +03003267 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003268
Yang Zhang8d146952013-01-25 10:18:50 +08003269 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003270 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003271}
3272
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003273static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003275 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003277 if (is_guest_mode(vcpu) &&
3278 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3279 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3280
3281 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282}
3283
3284/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003285 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003286 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003287static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003289 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003290 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003291 * We're here if L1 chose not to trap WRMSR to TSC. According
3292 * to the spec, this should set L1's TSC; The offset that L1
3293 * set for L2 remains unchanged, and still needs to be added
3294 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003295 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003296 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003297 /* recalculate vmcs02.TSC_OFFSET: */
3298 vmcs12 = get_vmcs12(vcpu);
3299 vmcs_write64(TSC_OFFSET, offset +
3300 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3301 vmcs12->tsc_offset : 0));
3302 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003303 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3304 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003305 vmcs_write64(TSC_OFFSET, offset);
3306 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003307}
3308
Nadav Har'El801d3422011-05-25 23:02:23 +03003309/*
3310 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3311 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3312 * all guests if the "nested" module option is off, and can also be disabled
3313 * for a single guest by disabling its VMX cpuid bit.
3314 */
3315static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3316{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003317 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003318}
3319
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003321 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3322 * returned for the various VMX controls MSRs when nested VMX is enabled.
3323 * The same values should also be used to verify that vmcs12 control fields are
3324 * valid during nested entry from L1 to L2.
3325 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3326 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3327 * bit in the high half is on if the corresponding bit in the control field
3328 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003329 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003330static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003331{
Paolo Bonzini13893092018-02-26 13:40:09 +01003332 if (!nested) {
3333 memset(msrs, 0, sizeof(*msrs));
3334 return;
3335 }
3336
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003337 /*
3338 * Note that as a general rule, the high half of the MSRs (bits in
3339 * the control fields which may be 1) should be initialized by the
3340 * intersection of the underlying hardware's MSR (i.e., features which
3341 * can be supported) and the list of features we want to expose -
3342 * because they are known to be properly supported in our code.
3343 * Also, usually, the low half of the MSRs (bits which must be 1) can
3344 * be set to 0, meaning that L1 may turn off any of these bits. The
3345 * reason is that if one of these bits is necessary, it will appear
3346 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3347 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003348 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003349 * These rules have exceptions below.
3350 */
3351
3352 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003353 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003354 msrs->pinbased_ctls_low,
3355 msrs->pinbased_ctls_high);
3356 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003357 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003359 PIN_BASED_EXT_INTR_MASK |
3360 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003361 PIN_BASED_VIRTUAL_NMIS |
3362 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003363 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003364 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003365 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003366
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003367 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003368 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 msrs->exit_ctls_low,
3370 msrs->exit_ctls_high);
3371 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003372 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003373
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003374 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003375#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003376 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003377#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003378 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003379 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003380 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003381 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003382 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3383
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003384 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003385 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003386
Jan Kiszka2996fca2014-06-16 13:59:43 +02003387 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003388 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003389
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003390 /* entry controls */
3391 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003392 msrs->entry_ctls_low,
3393 msrs->entry_ctls_high);
3394 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003395 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003396 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003397#ifdef CONFIG_X86_64
3398 VM_ENTRY_IA32E_MODE |
3399#endif
3400 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003401 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003402 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003403 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003405
Jan Kiszka2996fca2014-06-16 13:59:43 +02003406 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003408
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003409 /* cpu-based controls */
3410 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003411 msrs->procbased_ctls_low,
3412 msrs->procbased_ctls_high);
3413 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003414 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003415 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003416 CPU_BASED_VIRTUAL_INTR_PENDING |
3417 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003418 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3419 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3420 CPU_BASED_CR3_STORE_EXITING |
3421#ifdef CONFIG_X86_64
3422 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3423#endif
3424 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003425 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3426 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3427 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3428 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003429 /*
3430 * We can allow some features even when not supported by the
3431 * hardware. For example, L1 can specify an MSR bitmap - and we
3432 * can use it to avoid exits to L1 - even when L0 runs L2
3433 * without MSR bitmaps.
3434 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003435 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003436 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003437 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003438
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003439 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003440 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003441 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3442
Paolo Bonzini80154d72017-08-24 13:55:35 +02003443 /*
3444 * secondary cpu-based controls. Do not include those that
3445 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3446 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003447 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003448 msrs->secondary_ctls_low,
3449 msrs->secondary_ctls_high);
3450 msrs->secondary_ctls_low = 0;
3451 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003452 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003453 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003454 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003455 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003456 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003457 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003458
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003459 if (enable_ept) {
3460 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003461 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003462 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003463 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003464 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003465 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003466 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003467 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003468 msrs->ept_caps &= vmx_capability.ept;
3469 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003470 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3471 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003472 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003473 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003474 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003476 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003477 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003478
Bandan Das27c42a12017-08-03 15:54:42 -04003479 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003480 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003481 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003482 /*
3483 * Advertise EPTP switching unconditionally
3484 * since we emulate it
3485 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003486 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003487 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003488 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003489 }
3490
Paolo Bonzinief697a72016-03-18 16:58:38 +01003491 /*
3492 * Old versions of KVM use the single-context version without
3493 * checking for support, so declare that it is supported even
3494 * though it is treated as global context. The alternative is
3495 * not failing the single-context invvpid, and it is worse.
3496 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003497 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003498 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003499 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003500 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003501 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003502 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003503
Radim Krčmář0790ec12015-03-17 14:02:32 +01003504 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003505 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003506 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3507
Jan Kiszkac18911a2013-03-13 16:06:41 +01003508 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003509 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msrs->misc_low,
3511 msrs->misc_high);
3512 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3513 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003514 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003515 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003516 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003517 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003518
3519 /*
3520 * This MSR reports some information about VMX support. We
3521 * should return information about the VMX we emulate for the
3522 * guest, and the VMCS structure we give it - not about the
3523 * VMX support of the underlying hardware.
3524 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003525 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003526 VMCS12_REVISION |
3527 VMX_BASIC_TRUE_CTLS |
3528 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3529 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3530
3531 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003532 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003533
3534 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003535 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003536 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3537 * We picked the standard core2 setting.
3538 */
3539#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3540#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3542 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003543
3544 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3546 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003547
3548 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003549 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003550}
3551
David Matlack38991522016-11-29 18:14:08 -08003552/*
3553 * if fixed0[i] == 1: val[i] must be 1
3554 * if fixed1[i] == 0: val[i] must be 0
3555 */
3556static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3557{
3558 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003559}
3560
3561static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3562{
David Matlack38991522016-11-29 18:14:08 -08003563 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003564}
3565
3566static inline u64 vmx_control_msr(u32 low, u32 high)
3567{
3568 return low | ((u64)high << 32);
3569}
3570
David Matlack62cc6b9d2016-11-29 18:14:07 -08003571static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3572{
3573 superset &= mask;
3574 subset &= mask;
3575
3576 return (superset | subset) == superset;
3577}
3578
3579static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3580{
3581 const u64 feature_and_reserved =
3582 /* feature (except bit 48; see below) */
3583 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3584 /* reserved */
3585 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003586 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003587
3588 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3589 return -EINVAL;
3590
3591 /*
3592 * KVM does not emulate a version of VMX that constrains physical
3593 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3594 */
3595 if (data & BIT_ULL(48))
3596 return -EINVAL;
3597
3598 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3599 vmx_basic_vmcs_revision_id(data))
3600 return -EINVAL;
3601
3602 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3603 return -EINVAL;
3604
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003605 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003606 return 0;
3607}
3608
3609static int
3610vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3611{
3612 u64 supported;
3613 u32 *lowp, *highp;
3614
3615 switch (msr_index) {
3616 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003617 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3618 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003619 break;
3620 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 lowp = &vmx->nested.msrs.procbased_ctls_low;
3622 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003623 break;
3624 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003625 lowp = &vmx->nested.msrs.exit_ctls_low;
3626 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003627 break;
3628 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003629 lowp = &vmx->nested.msrs.entry_ctls_low;
3630 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003631 break;
3632 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 lowp = &vmx->nested.msrs.secondary_ctls_low;
3634 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003635 break;
3636 default:
3637 BUG();
3638 }
3639
3640 supported = vmx_control_msr(*lowp, *highp);
3641
3642 /* Check must-be-1 bits are still 1. */
3643 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3644 return -EINVAL;
3645
3646 /* Check must-be-0 bits are still 0. */
3647 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3648 return -EINVAL;
3649
3650 *lowp = data;
3651 *highp = data >> 32;
3652 return 0;
3653}
3654
3655static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3656{
3657 const u64 feature_and_reserved_bits =
3658 /* feature */
3659 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3660 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3661 /* reserved */
3662 GENMASK_ULL(13, 9) | BIT_ULL(31);
3663 u64 vmx_misc;
3664
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003665 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3666 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003667
3668 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3669 return -EINVAL;
3670
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003671 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003672 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3673 vmx_misc_preemption_timer_rate(data) !=
3674 vmx_misc_preemption_timer_rate(vmx_misc))
3675 return -EINVAL;
3676
3677 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3678 return -EINVAL;
3679
3680 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3681 return -EINVAL;
3682
3683 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3684 return -EINVAL;
3685
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 vmx->nested.msrs.misc_low = data;
3687 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003688
3689 /*
3690 * If L1 has read-only VM-exit information fields, use the
3691 * less permissive vmx_vmwrite_bitmap to specify write
3692 * permissions for the shadow VMCS.
3693 */
3694 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3695 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3696
David Matlack62cc6b9d2016-11-29 18:14:07 -08003697 return 0;
3698}
3699
3700static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3701{
3702 u64 vmx_ept_vpid_cap;
3703
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003704 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3705 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003706
3707 /* Every bit is either reserved or a feature bit. */
3708 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3709 return -EINVAL;
3710
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003711 vmx->nested.msrs.ept_caps = data;
3712 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003713 return 0;
3714}
3715
3716static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3717{
3718 u64 *msr;
3719
3720 switch (msr_index) {
3721 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003722 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003723 break;
3724 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003725 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003726 break;
3727 default:
3728 BUG();
3729 }
3730
3731 /*
3732 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3733 * must be 1 in the restored value.
3734 */
3735 if (!is_bitwise_subset(data, *msr, -1ULL))
3736 return -EINVAL;
3737
3738 *msr = data;
3739 return 0;
3740}
3741
3742/*
3743 * Called when userspace is restoring VMX MSRs.
3744 *
3745 * Returns 0 on success, non-0 otherwise.
3746 */
3747static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3748{
3749 struct vcpu_vmx *vmx = to_vmx(vcpu);
3750
Jim Mattsona943ac52018-05-29 09:11:32 -07003751 /*
3752 * Don't allow changes to the VMX capability MSRs while the vCPU
3753 * is in VMX operation.
3754 */
3755 if (vmx->nested.vmxon)
3756 return -EBUSY;
3757
David Matlack62cc6b9d2016-11-29 18:14:07 -08003758 switch (msr_index) {
3759 case MSR_IA32_VMX_BASIC:
3760 return vmx_restore_vmx_basic(vmx, data);
3761 case MSR_IA32_VMX_PINBASED_CTLS:
3762 case MSR_IA32_VMX_PROCBASED_CTLS:
3763 case MSR_IA32_VMX_EXIT_CTLS:
3764 case MSR_IA32_VMX_ENTRY_CTLS:
3765 /*
3766 * The "non-true" VMX capability MSRs are generated from the
3767 * "true" MSRs, so we do not support restoring them directly.
3768 *
3769 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3770 * should restore the "true" MSRs with the must-be-1 bits
3771 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3772 * DEFAULT SETTINGS".
3773 */
3774 return -EINVAL;
3775 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3776 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3777 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3778 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3779 case MSR_IA32_VMX_PROCBASED_CTLS2:
3780 return vmx_restore_control_msr(vmx, msr_index, data);
3781 case MSR_IA32_VMX_MISC:
3782 return vmx_restore_vmx_misc(vmx, data);
3783 case MSR_IA32_VMX_CR0_FIXED0:
3784 case MSR_IA32_VMX_CR4_FIXED0:
3785 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3786 case MSR_IA32_VMX_CR0_FIXED1:
3787 case MSR_IA32_VMX_CR4_FIXED1:
3788 /*
3789 * These MSRs are generated based on the vCPU's CPUID, so we
3790 * do not support restoring them directly.
3791 */
3792 return -EINVAL;
3793 case MSR_IA32_VMX_EPT_VPID_CAP:
3794 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3795 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003796 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003797 return 0;
3798 default:
3799 /*
3800 * The rest of the VMX capability MSRs do not support restore.
3801 */
3802 return -EINVAL;
3803 }
3804}
3805
Jan Kiszkacae50132014-01-04 18:47:22 +01003806/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003807static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003808{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003809 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003810 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003811 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003812 break;
3813 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3814 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003815 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003816 msrs->pinbased_ctls_low,
3817 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003818 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3819 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003820 break;
3821 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3822 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003823 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003824 msrs->procbased_ctls_low,
3825 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003826 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3827 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003828 break;
3829 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3830 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003831 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003832 msrs->exit_ctls_low,
3833 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003834 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3835 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003836 break;
3837 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3838 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003839 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003840 msrs->entry_ctls_low,
3841 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003842 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3843 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003844 break;
3845 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003846 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003847 msrs->misc_low,
3848 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003849 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003850 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003851 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003852 break;
3853 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003854 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003855 break;
3856 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003857 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003858 break;
3859 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003860 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003861 break;
3862 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003863 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003864 break;
3865 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003866 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003867 msrs->secondary_ctls_low,
3868 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003869 break;
3870 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003871 *pdata = msrs->ept_caps |
3872 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003873 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003874 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003876 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003877 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003878 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003879 }
3880
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003881 return 0;
3882}
3883
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003884static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3885 uint64_t val)
3886{
3887 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3888
3889 return !(val & ~valid_bits);
3890}
3891
Tom Lendacky801e4592018-02-21 13:39:51 -06003892static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3893{
Paolo Bonzini13893092018-02-26 13:40:09 +01003894 switch (msr->index) {
3895 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3896 if (!nested)
3897 return 1;
3898 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3899 default:
3900 return 1;
3901 }
3902
3903 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003904}
3905
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003906/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 * Reads an msr value (of 'msr_index') into 'pdata'.
3908 * Returns 0 on success, non-0 otherwise.
3909 * Assumes vcpu_load() was already called.
3910 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003911static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003913 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003914 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003916 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003917#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003919 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 break;
3921 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003922 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003924 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003925 vmx_load_host_state(vmx);
3926 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003927 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003928#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003930 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003931 case MSR_IA32_SPEC_CTRL:
3932 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003933 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3934 return 1;
3935
3936 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3937 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003938 case MSR_IA32_ARCH_CAPABILITIES:
3939 if (!msr_info->host_initiated &&
3940 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3941 return 1;
3942 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3943 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003945 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 break;
3947 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003948 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 break;
3950 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003951 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003953 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003954 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003955 (!msr_info->host_initiated &&
3956 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003957 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003958 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003959 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003960 case MSR_IA32_MCG_EXT_CTL:
3961 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003962 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003963 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003964 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003965 msr_info->data = vcpu->arch.mcg_ext_ctl;
3966 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003967 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003968 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003969 break;
3970 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3971 if (!nested_vmx_allowed(vcpu))
3972 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003973 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3974 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003975 case MSR_IA32_XSS:
3976 if (!vmx_xsaves_supported())
3977 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003978 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003979 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003980 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003981 if (!msr_info->host_initiated &&
3982 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003983 return 1;
3984 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003986 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003987 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003988 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003989 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003991 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003992 }
3993
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994 return 0;
3995}
3996
Jan Kiszkacae50132014-01-04 18:47:22 +01003997static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3998
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999/*
4000 * Writes msr value into into the appropriate "register".
4001 * Returns 0 on success, non-0 otherwise.
4002 * Assumes vcpu_load() was already called.
4003 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004004static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004006 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004007 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004008 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004009 u32 msr_index = msr_info->index;
4010 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004011
Avi Kivity6aa8b732006-12-10 02:21:36 -08004012 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004013 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004014 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004015 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004016#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004018 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004019 vmcs_writel(GUEST_FS_BASE, data);
4020 break;
4021 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004022 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023 vmcs_writel(GUEST_GS_BASE, data);
4024 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004025 case MSR_KERNEL_GS_BASE:
4026 vmx_load_host_state(vmx);
4027 vmx->msr_guest_kernel_gs_base = data;
4028 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004029#endif
4030 case MSR_IA32_SYSENTER_CS:
4031 vmcs_write32(GUEST_SYSENTER_CS, data);
4032 break;
4033 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004034 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035 break;
4036 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004037 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004039 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004040 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004041 (!msr_info->host_initiated &&
4042 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004043 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004044 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004045 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004047 vmcs_write64(GUEST_BNDCFGS, data);
4048 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004049 case MSR_IA32_SPEC_CTRL:
4050 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004051 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4052 return 1;
4053
4054 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004055 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004056 return 1;
4057
4058 vmx->spec_ctrl = data;
4059
4060 if (!data)
4061 break;
4062
4063 /*
4064 * For non-nested:
4065 * When it's written (to non-zero) for the first time, pass
4066 * it through.
4067 *
4068 * For nested:
4069 * The handling of the MSR bitmap for L2 guests is done in
4070 * nested_vmx_merge_msr_bitmap. We should not touch the
4071 * vmcs02.msr_bitmap here since it gets completely overwritten
4072 * in the merging. We update the vmcs01 here for L1 as well
4073 * since it will end up touching the MSR anyway now.
4074 */
4075 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4076 MSR_IA32_SPEC_CTRL,
4077 MSR_TYPE_RW);
4078 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004079 case MSR_IA32_PRED_CMD:
4080 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004081 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4082 return 1;
4083
4084 if (data & ~PRED_CMD_IBPB)
4085 return 1;
4086
4087 if (!data)
4088 break;
4089
4090 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4091
4092 /*
4093 * For non-nested:
4094 * When it's written (to non-zero) for the first time, pass
4095 * it through.
4096 *
4097 * For nested:
4098 * The handling of the MSR bitmap for L2 guests is done in
4099 * nested_vmx_merge_msr_bitmap. We should not touch the
4100 * vmcs02.msr_bitmap here since it gets completely overwritten
4101 * in the merging.
4102 */
4103 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4104 MSR_TYPE_W);
4105 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004106 case MSR_IA32_ARCH_CAPABILITIES:
4107 if (!msr_info->host_initiated)
4108 return 1;
4109 vmx->arch_capabilities = data;
4110 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004111 case MSR_IA32_CR_PAT:
4112 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004113 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4114 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004115 vmcs_write64(GUEST_IA32_PAT, data);
4116 vcpu->arch.pat = data;
4117 break;
4118 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004119 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004120 break;
Will Auldba904632012-11-29 12:42:50 -08004121 case MSR_IA32_TSC_ADJUST:
4122 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004123 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004124 case MSR_IA32_MCG_EXT_CTL:
4125 if ((!msr_info->host_initiated &&
4126 !(to_vmx(vcpu)->msr_ia32_feature_control &
4127 FEATURE_CONTROL_LMCE)) ||
4128 (data & ~MCG_EXT_CTL_LMCE_EN))
4129 return 1;
4130 vcpu->arch.mcg_ext_ctl = data;
4131 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004132 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004133 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004134 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004135 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4136 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004137 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004138 if (msr_info->host_initiated && data == 0)
4139 vmx_leave_nested(vcpu);
4140 break;
4141 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004142 if (!msr_info->host_initiated)
4143 return 1; /* they are read-only */
4144 if (!nested_vmx_allowed(vcpu))
4145 return 1;
4146 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004147 case MSR_IA32_XSS:
4148 if (!vmx_xsaves_supported())
4149 return 1;
4150 /*
4151 * The only supported bit as of Skylake is bit 8, but
4152 * it is not supported on KVM.
4153 */
4154 if (data != 0)
4155 return 1;
4156 vcpu->arch.ia32_xss = data;
4157 if (vcpu->arch.ia32_xss != host_xss)
4158 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004159 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004160 else
4161 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4162 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004163 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004164 if (!msr_info->host_initiated &&
4165 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004166 return 1;
4167 /* Check reserved bit, higher 32 bits should be zero */
4168 if ((data >> 32) != 0)
4169 return 1;
4170 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004172 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004173 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004174 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004175 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004176 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4177 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004178 ret = kvm_set_shared_msr(msr->index, msr->data,
4179 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004180 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004181 if (ret)
4182 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004183 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004184 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004186 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187 }
4188
Eddie Dong2cc51562007-05-21 07:28:09 +03004189 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190}
4191
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004192static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004194 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4195 switch (reg) {
4196 case VCPU_REGS_RSP:
4197 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4198 break;
4199 case VCPU_REGS_RIP:
4200 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4201 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004202 case VCPU_EXREG_PDPTR:
4203 if (enable_ept)
4204 ept_save_pdptrs(vcpu);
4205 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004206 default:
4207 break;
4208 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209}
4210
Avi Kivity6aa8b732006-12-10 02:21:36 -08004211static __init int cpu_has_kvm_support(void)
4212{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004213 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004214}
4215
4216static __init int vmx_disabled_by_bios(void)
4217{
4218 u64 msr;
4219
4220 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004221 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004222 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004223 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4224 && tboot_enabled())
4225 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004226 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004227 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004228 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004229 && !tboot_enabled()) {
4230 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004231 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004232 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004233 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004234 /* launched w/o TXT and VMX disabled */
4235 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4236 && !tboot_enabled())
4237 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004238 }
4239
4240 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241}
4242
Dongxiao Xu7725b892010-05-11 18:29:38 +08004243static void kvm_cpu_vmxon(u64 addr)
4244{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004245 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004246 intel_pt_handle_vmx(1);
4247
Dongxiao Xu7725b892010-05-11 18:29:38 +08004248 asm volatile (ASM_VMX_VMXON_RAX
4249 : : "a"(&addr), "m"(addr)
4250 : "memory", "cc");
4251}
4252
Radim Krčmář13a34e02014-08-28 15:13:03 +02004253static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254{
4255 int cpu = raw_smp_processor_id();
4256 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004257 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004259 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004260 return -EBUSY;
4261
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004262 /*
4263 * This can happen if we hot-added a CPU but failed to allocate
4264 * VP assist page for it.
4265 */
4266 if (static_branch_unlikely(&enable_evmcs) &&
4267 !hv_get_vp_assist_page(cpu))
4268 return -EFAULT;
4269
Nadav Har'Eld462b812011-05-24 15:26:10 +03004270 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004271 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4272 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004273
4274 /*
4275 * Now we can enable the vmclear operation in kdump
4276 * since the loaded_vmcss_on_cpu list on this cpu
4277 * has been initialized.
4278 *
4279 * Though the cpu is not in VMX operation now, there
4280 * is no problem to enable the vmclear operation
4281 * for the loaded_vmcss_on_cpu list is empty!
4282 */
4283 crash_enable_local_vmclear(cpu);
4284
Avi Kivity6aa8b732006-12-10 02:21:36 -08004285 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004286
4287 test_bits = FEATURE_CONTROL_LOCKED;
4288 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4289 if (tboot_enabled())
4290 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4291
4292 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004294 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4295 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004296 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004297 if (enable_ept)
4298 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004299
4300 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004301}
4302
Nadav Har'Eld462b812011-05-24 15:26:10 +03004303static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004304{
4305 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004306 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004307
Nadav Har'Eld462b812011-05-24 15:26:10 +03004308 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4309 loaded_vmcss_on_cpu_link)
4310 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004311}
4312
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004313
4314/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4315 * tricks.
4316 */
4317static void kvm_cpu_vmxoff(void)
4318{
4319 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004320
4321 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004322 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004323}
4324
Radim Krčmář13a34e02014-08-28 15:13:03 +02004325static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004327 vmclear_local_loaded_vmcss();
4328 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329}
4330
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004331static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004332 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333{
4334 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004335 u32 ctl = ctl_min | ctl_opt;
4336
4337 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4338
4339 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4340 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4341
4342 /* Ensure minimum (required) set of control bits are supported. */
4343 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004344 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004345
4346 *result = ctl;
4347 return 0;
4348}
4349
Avi Kivity110312c2010-12-21 12:54:20 +02004350static __init bool allow_1_setting(u32 msr, u32 ctl)
4351{
4352 u32 vmx_msr_low, vmx_msr_high;
4353
4354 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4355 return vmx_msr_high & ctl;
4356}
4357
Yang, Sheng002c7f72007-07-31 14:23:01 +03004358static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004359{
4360 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004361 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004362 u32 _pin_based_exec_control = 0;
4363 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004364 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004365 u32 _vmexit_control = 0;
4366 u32 _vmentry_control = 0;
4367
Paolo Bonzini13893092018-02-26 13:40:09 +01004368 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304369 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004370#ifdef CONFIG_X86_64
4371 CPU_BASED_CR8_LOAD_EXITING |
4372 CPU_BASED_CR8_STORE_EXITING |
4373#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004374 CPU_BASED_CR3_LOAD_EXITING |
4375 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004376 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004377 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004378 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004379 CPU_BASED_MWAIT_EXITING |
4380 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004381 CPU_BASED_INVLPG_EXITING |
4382 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004383
Sheng Yangf78e0e22007-10-29 09:40:42 +08004384 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004385 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004386 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004387 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4388 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004389 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004390#ifdef CONFIG_X86_64
4391 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4392 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4393 ~CPU_BASED_CR8_STORE_EXITING;
4394#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004395 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004396 min2 = 0;
4397 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004398 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004399 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004400 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004401 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004402 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004403 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004404 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004405 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004406 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004407 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004408 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004409 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004410 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004411 SECONDARY_EXEC_RDSEED_EXITING |
4412 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004413 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004414 SECONDARY_EXEC_TSC_SCALING |
4415 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004416 if (adjust_vmx_controls(min2, opt2,
4417 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004418 &_cpu_based_2nd_exec_control) < 0)
4419 return -EIO;
4420 }
4421#ifndef CONFIG_X86_64
4422 if (!(_cpu_based_2nd_exec_control &
4423 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4424 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4425#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004426
4427 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4428 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004429 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004430 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4431 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004432
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004433 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4434 &vmx_capability.ept, &vmx_capability.vpid);
4435
Sheng Yangd56f5462008-04-25 10:13:16 +08004436 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004437 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4438 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004439 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4440 CPU_BASED_CR3_STORE_EXITING |
4441 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004442 } else if (vmx_capability.ept) {
4443 vmx_capability.ept = 0;
4444 pr_warn_once("EPT CAP should not exist if not support "
4445 "1-setting enable EPT VM-execution control\n");
4446 }
4447 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4448 vmx_capability.vpid) {
4449 vmx_capability.vpid = 0;
4450 pr_warn_once("VPID CAP should not exist if not support "
4451 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004452 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004453
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004454 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004455#ifdef CONFIG_X86_64
4456 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4457#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004458 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004459 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004460 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4461 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004462 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004463
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004464 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4465 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4466 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004467 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4468 &_pin_based_exec_control) < 0)
4469 return -EIO;
4470
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004471 if (cpu_has_broken_vmx_preemption_timer())
4472 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004473 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004474 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004475 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4476
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004477 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004478 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004479 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4480 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004481 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004483 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004484
4485 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4486 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004487 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004488
4489#ifdef CONFIG_X86_64
4490 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4491 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004492 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004493#endif
4494
4495 /* Require Write-Back (WB) memory type for VMCS accesses. */
4496 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004497 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004498
Yang, Sheng002c7f72007-07-31 14:23:01 +03004499 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004500 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004501 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004502
Liran Alon2307af12018-06-29 22:59:04 +03004503 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004504
Yang, Sheng002c7f72007-07-31 14:23:01 +03004505 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4506 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004507 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004508 vmcs_conf->vmexit_ctrl = _vmexit_control;
4509 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004511 if (static_branch_unlikely(&enable_evmcs))
4512 evmcs_sanitize_exec_ctrls(vmcs_conf);
4513
Avi Kivity110312c2010-12-21 12:54:20 +02004514 cpu_has_load_ia32_efer =
4515 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4516 VM_ENTRY_LOAD_IA32_EFER)
4517 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4518 VM_EXIT_LOAD_IA32_EFER);
4519
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004520 cpu_has_load_perf_global_ctrl =
4521 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4522 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4523 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4524 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4525
4526 /*
4527 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004528 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004529 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4530 *
4531 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4532 *
4533 * AAK155 (model 26)
4534 * AAP115 (model 30)
4535 * AAT100 (model 37)
4536 * BC86,AAY89,BD102 (model 44)
4537 * BA97 (model 46)
4538 *
4539 */
4540 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4541 switch (boot_cpu_data.x86_model) {
4542 case 26:
4543 case 30:
4544 case 37:
4545 case 44:
4546 case 46:
4547 cpu_has_load_perf_global_ctrl = false;
4548 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4549 "does not work properly. Using workaround\n");
4550 break;
4551 default:
4552 break;
4553 }
4554 }
4555
Borislav Petkov782511b2016-04-04 22:25:03 +02004556 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004557 rdmsrl(MSR_IA32_XSS, host_xss);
4558
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004559 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004560}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561
4562static struct vmcs *alloc_vmcs_cpu(int cpu)
4563{
4564 int node = cpu_to_node(cpu);
4565 struct page *pages;
4566 struct vmcs *vmcs;
4567
Vlastimil Babka96db8002015-09-08 15:03:50 -07004568 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004569 if (!pages)
4570 return NULL;
4571 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004572 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004573
4574 /* KVM supports Enlightened VMCS v1 only */
4575 if (static_branch_unlikely(&enable_evmcs))
4576 vmcs->revision_id = KVM_EVMCS_VERSION;
4577 else
4578 vmcs->revision_id = vmcs_config.revision_id;
4579
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580 return vmcs;
4581}
4582
Avi Kivity6aa8b732006-12-10 02:21:36 -08004583static void free_vmcs(struct vmcs *vmcs)
4584{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004585 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586}
4587
Nadav Har'Eld462b812011-05-24 15:26:10 +03004588/*
4589 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4590 */
4591static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4592{
4593 if (!loaded_vmcs->vmcs)
4594 return;
4595 loaded_vmcs_clear(loaded_vmcs);
4596 free_vmcs(loaded_vmcs->vmcs);
4597 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004598 if (loaded_vmcs->msr_bitmap)
4599 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004600 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004601}
4602
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004603static struct vmcs *alloc_vmcs(void)
4604{
4605 return alloc_vmcs_cpu(raw_smp_processor_id());
4606}
4607
4608static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4609{
4610 loaded_vmcs->vmcs = alloc_vmcs();
4611 if (!loaded_vmcs->vmcs)
4612 return -ENOMEM;
4613
4614 loaded_vmcs->shadow_vmcs = NULL;
4615 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004616
4617 if (cpu_has_vmx_msr_bitmap()) {
4618 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4619 if (!loaded_vmcs->msr_bitmap)
4620 goto out_vmcs;
4621 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004622
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004623 if (IS_ENABLED(CONFIG_HYPERV) &&
4624 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004625 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4626 struct hv_enlightened_vmcs *evmcs =
4627 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4628
4629 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4630 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004631 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004632 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004633
4634out_vmcs:
4635 free_loaded_vmcs(loaded_vmcs);
4636 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004637}
4638
Sam Ravnborg39959582007-06-01 00:47:13 -07004639static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004640{
4641 int cpu;
4642
Zachary Amsden3230bb42009-09-29 11:38:37 -10004643 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004644 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004645 per_cpu(vmxarea, cpu) = NULL;
4646 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004647}
4648
Jim Mattsond37f4262017-12-22 12:12:16 -08004649enum vmcs_field_width {
4650 VMCS_FIELD_WIDTH_U16 = 0,
4651 VMCS_FIELD_WIDTH_U64 = 1,
4652 VMCS_FIELD_WIDTH_U32 = 2,
4653 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004654};
4655
Jim Mattsond37f4262017-12-22 12:12:16 -08004656static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004657{
4658 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004659 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004660 return (field >> 13) & 0x3 ;
4661}
4662
4663static inline int vmcs_field_readonly(unsigned long field)
4664{
4665 return (((field >> 10) & 0x3) == 1);
4666}
4667
Bandan Dasfe2b2012014-04-21 15:20:14 -04004668static void init_vmcs_shadow_fields(void)
4669{
4670 int i, j;
4671
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004672 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4673 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004674 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004675 (i + 1 == max_shadow_read_only_fields ||
4676 shadow_read_only_fields[i + 1] != field + 1))
4677 pr_err("Missing field from shadow_read_only_field %x\n",
4678 field + 1);
4679
4680 clear_bit(field, vmx_vmread_bitmap);
4681#ifdef CONFIG_X86_64
4682 if (field & 1)
4683 continue;
4684#endif
4685 if (j < i)
4686 shadow_read_only_fields[j] = field;
4687 j++;
4688 }
4689 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004690
4691 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004692 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004693 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004694 (i + 1 == max_shadow_read_write_fields ||
4695 shadow_read_write_fields[i + 1] != field + 1))
4696 pr_err("Missing field from shadow_read_write_field %x\n",
4697 field + 1);
4698
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004699 /*
4700 * PML and the preemption timer can be emulated, but the
4701 * processor cannot vmwrite to fields that don't exist
4702 * on bare metal.
4703 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004704 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004705 case GUEST_PML_INDEX:
4706 if (!cpu_has_vmx_pml())
4707 continue;
4708 break;
4709 case VMX_PREEMPTION_TIMER_VALUE:
4710 if (!cpu_has_vmx_preemption_timer())
4711 continue;
4712 break;
4713 case GUEST_INTR_STATUS:
4714 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004715 continue;
4716 break;
4717 default:
4718 break;
4719 }
4720
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004721 clear_bit(field, vmx_vmwrite_bitmap);
4722 clear_bit(field, vmx_vmread_bitmap);
4723#ifdef CONFIG_X86_64
4724 if (field & 1)
4725 continue;
4726#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004727 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004728 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004729 j++;
4730 }
4731 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004732}
4733
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734static __init int alloc_kvm_area(void)
4735{
4736 int cpu;
4737
Zachary Amsden3230bb42009-09-29 11:38:37 -10004738 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739 struct vmcs *vmcs;
4740
4741 vmcs = alloc_vmcs_cpu(cpu);
4742 if (!vmcs) {
4743 free_kvm_area();
4744 return -ENOMEM;
4745 }
4746
Liran Alon2307af12018-06-29 22:59:04 +03004747 /*
4748 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4749 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4750 * revision_id reported by MSR_IA32_VMX_BASIC.
4751 *
4752 * However, even though not explictly documented by
4753 * TLFS, VMXArea passed as VMXON argument should
4754 * still be marked with revision_id reported by
4755 * physical CPU.
4756 */
4757 if (static_branch_unlikely(&enable_evmcs))
4758 vmcs->revision_id = vmcs_config.revision_id;
4759
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760 per_cpu(vmxarea, cpu) = vmcs;
4761 }
4762 return 0;
4763}
4764
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004765static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004766 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004767{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004768 if (!emulate_invalid_guest_state) {
4769 /*
4770 * CS and SS RPL should be equal during guest entry according
4771 * to VMX spec, but in reality it is not always so. Since vcpu
4772 * is in the middle of the transition from real mode to
4773 * protected mode it is safe to assume that RPL 0 is a good
4774 * default value.
4775 */
4776 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004777 save->selector &= ~SEGMENT_RPL_MASK;
4778 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004779 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004781 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004782}
4783
4784static void enter_pmode(struct kvm_vcpu *vcpu)
4785{
4786 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004787 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788
Gleb Natapovd99e4152012-12-20 16:57:45 +02004789 /*
4790 * Update real mode segment cache. It may be not up-to-date if sement
4791 * register was written while vcpu was in a guest mode.
4792 */
4793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4796 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4797 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4798 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4799
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004800 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801
Avi Kivity2fb92db2011-04-27 19:42:18 +03004802 vmx_segment_cache_clear(vmx);
4803
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004804 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805
4806 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004807 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4808 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809 vmcs_writel(GUEST_RFLAGS, flags);
4810
Rusty Russell66aee912007-07-17 23:34:16 +10004811 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4812 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813
4814 update_exception_bitmap(vcpu);
4815
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004816 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4817 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4818 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4819 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4820 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4821 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822}
4823
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004824static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825{
Mathias Krause772e0312012-08-30 01:30:19 +02004826 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004827 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828
Gleb Natapovd99e4152012-12-20 16:57:45 +02004829 var.dpl = 0x3;
4830 if (seg == VCPU_SREG_CS)
4831 var.type = 0x3;
4832
4833 if (!emulate_invalid_guest_state) {
4834 var.selector = var.base >> 4;
4835 var.base = var.base & 0xffff0;
4836 var.limit = 0xffff;
4837 var.g = 0;
4838 var.db = 0;
4839 var.present = 1;
4840 var.s = 1;
4841 var.l = 0;
4842 var.unusable = 0;
4843 var.type = 0x3;
4844 var.avl = 0;
4845 if (save->base & 0xf)
4846 printk_once(KERN_WARNING "kvm: segment base is not "
4847 "paragraph aligned when entering "
4848 "protected mode (seg=%d)", seg);
4849 }
4850
4851 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004852 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004853 vmcs_write32(sf->limit, var.limit);
4854 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004855}
4856
4857static void enter_rmode(struct kvm_vcpu *vcpu)
4858{
4859 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004860 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004861 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004863 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4864 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4865 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4866 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4867 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004868 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4869 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004870
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004871 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872
Gleb Natapov776e58e2011-03-13 12:34:27 +02004873 /*
4874 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004875 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004876 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004877 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004878 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4879 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004880
Avi Kivity2fb92db2011-04-27 19:42:18 +03004881 vmx_segment_cache_clear(vmx);
4882
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004883 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4886
4887 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004888 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004889
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004890 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891
4892 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004893 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 update_exception_bitmap(vcpu);
4895
Gleb Natapovd99e4152012-12-20 16:57:45 +02004896 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4897 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4898 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4899 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4900 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4901 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004902
Eddie Dong8668a3c2007-10-10 14:26:45 +08004903 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904}
4905
Amit Shah401d10d2009-02-20 22:53:37 +05304906static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4907{
4908 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004909 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4910
4911 if (!msr)
4912 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304913
Avi Kivity44ea2b12009-09-06 15:55:37 +03004914 /*
4915 * Force kernel_gs_base reloading before EFER changes, as control
4916 * of this msr depends on is_long_mode().
4917 */
4918 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004919 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304920 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004921 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304922 msr->data = efer;
4923 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004924 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304925
4926 msr->data = efer & ~EFER_LME;
4927 }
4928 setup_msrs(vmx);
4929}
4930
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004931#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004932
4933static void enter_lmode(struct kvm_vcpu *vcpu)
4934{
4935 u32 guest_tr_ar;
4936
Avi Kivity2fb92db2011-04-27 19:42:18 +03004937 vmx_segment_cache_clear(to_vmx(vcpu));
4938
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004940 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004941 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4942 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004943 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004944 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4945 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004946 }
Avi Kivityda38f432010-07-06 11:30:49 +03004947 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004948}
4949
4950static void exit_lmode(struct kvm_vcpu *vcpu)
4951{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004952 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004953 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954}
4955
4956#endif
4957
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004958static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4959 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004960{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004961 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004962 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4963 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004964 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004965 } else {
4966 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004967 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004968}
4969
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004970static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004971{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004972 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004973}
4974
Avi Kivitye8467fd2009-12-29 18:43:06 +02004975static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4976{
4977 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4978
4979 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4980 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4981}
4982
Avi Kivityaff48ba2010-12-05 18:56:11 +02004983static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4984{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004985 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004986 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4987 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4988}
4989
Anthony Liguori25c4c272007-04-27 09:29:21 +03004990static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004991{
Avi Kivityfc78f512009-12-07 12:16:48 +02004992 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4993
4994 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4995 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004996}
4997
Sheng Yang14394422008-04-28 12:24:45 +08004998static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4999{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005000 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5001
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005002 if (!test_bit(VCPU_EXREG_PDPTR,
5003 (unsigned long *)&vcpu->arch.regs_dirty))
5004 return;
5005
Sheng Yang14394422008-04-28 12:24:45 +08005006 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005007 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5008 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5009 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5010 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005011 }
5012}
5013
Avi Kivity8f5d5492009-05-31 18:41:29 +03005014static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5015{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005016 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5017
Avi Kivity8f5d5492009-05-31 18:41:29 +03005018 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005019 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5020 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5021 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5022 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005023 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005024
5025 __set_bit(VCPU_EXREG_PDPTR,
5026 (unsigned long *)&vcpu->arch.regs_avail);
5027 __set_bit(VCPU_EXREG_PDPTR,
5028 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005029}
5030
David Matlack38991522016-11-29 18:14:08 -08005031static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5032{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005033 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5034 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005035 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5036
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005037 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005038 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5039 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5040 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5041
5042 return fixed_bits_valid(val, fixed0, fixed1);
5043}
5044
5045static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5046{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005047 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5048 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005049
5050 return fixed_bits_valid(val, fixed0, fixed1);
5051}
5052
5053static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5054{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005055 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5056 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005057
5058 return fixed_bits_valid(val, fixed0, fixed1);
5059}
5060
5061/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5062#define nested_guest_cr4_valid nested_cr4_valid
5063#define nested_host_cr4_valid nested_cr4_valid
5064
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005065static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005066
5067static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5068 unsigned long cr0,
5069 struct kvm_vcpu *vcpu)
5070{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005071 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5072 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005073 if (!(cr0 & X86_CR0_PG)) {
5074 /* From paging/starting to nonpaging */
5075 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005076 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005077 (CPU_BASED_CR3_LOAD_EXITING |
5078 CPU_BASED_CR3_STORE_EXITING));
5079 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005080 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005081 } else if (!is_paging(vcpu)) {
5082 /* From nonpaging to paging */
5083 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005084 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005085 ~(CPU_BASED_CR3_LOAD_EXITING |
5086 CPU_BASED_CR3_STORE_EXITING));
5087 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005088 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005089 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005090
5091 if (!(cr0 & X86_CR0_WP))
5092 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005093}
5094
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5096{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005097 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005098 unsigned long hw_cr0;
5099
Gleb Natapov50378782013-02-04 16:00:28 +02005100 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005101 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005102 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005103 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005104 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005105
Gleb Natapov218e7632013-01-21 15:36:45 +02005106 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5107 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005108
Gleb Natapov218e7632013-01-21 15:36:45 +02005109 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5110 enter_rmode(vcpu);
5111 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005112
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005113#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005114 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005115 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005116 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005117 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005118 exit_lmode(vcpu);
5119 }
5120#endif
5121
Sean Christophersonb4d18512018-03-05 12:04:40 -08005122 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005123 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5124
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005126 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005127 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005128
5129 /* depends on vcpu->arch.cr0 to be set to a new value */
5130 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005131}
5132
Yu Zhang855feb62017-08-24 20:27:55 +08005133static int get_ept_level(struct kvm_vcpu *vcpu)
5134{
5135 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5136 return 5;
5137 return 4;
5138}
5139
Peter Feiner995f00a2017-06-30 17:26:32 -07005140static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005141{
Yu Zhang855feb62017-08-24 20:27:55 +08005142 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005143
Yu Zhang855feb62017-08-24 20:27:55 +08005144 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005145
Peter Feiner995f00a2017-06-30 17:26:32 -07005146 if (enable_ept_ad_bits &&
5147 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005148 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005149 eptp |= (root_hpa & PAGE_MASK);
5150
5151 return eptp;
5152}
5153
Avi Kivity6aa8b732006-12-10 02:21:36 -08005154static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5155{
Sheng Yang14394422008-04-28 12:24:45 +08005156 unsigned long guest_cr3;
5157 u64 eptp;
5158
5159 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005160 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005161 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005162 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005163 if (enable_unrestricted_guest || is_paging(vcpu) ||
5164 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005165 guest_cr3 = kvm_read_cr3(vcpu);
5166 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005167 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005168 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005169 }
5170
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005171 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005172 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005173}
5174
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005175static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005176{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005177 /*
5178 * Pass through host's Machine Check Enable value to hw_cr4, which
5179 * is in force while we are in guest mode. Do not let guests control
5180 * this bit, even if host CR4.MCE == 0.
5181 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005182 unsigned long hw_cr4;
5183
5184 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5185 if (enable_unrestricted_guest)
5186 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5187 else if (to_vmx(vcpu)->rmode.vm86_active)
5188 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5189 else
5190 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005191
Sean Christopherson64f7a112018-04-30 10:01:06 -07005192 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5193 if (cr4 & X86_CR4_UMIP) {
5194 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005195 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005196 hw_cr4 &= ~X86_CR4_UMIP;
5197 } else if (!is_guest_mode(vcpu) ||
5198 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5199 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5200 SECONDARY_EXEC_DESC);
5201 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005202
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005203 if (cr4 & X86_CR4_VMXE) {
5204 /*
5205 * To use VMXON (and later other VMX instructions), a guest
5206 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5207 * So basically the check on whether to allow nested VMX
5208 * is here.
5209 */
5210 if (!nested_vmx_allowed(vcpu))
5211 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005212 }
David Matlack38991522016-11-29 18:14:08 -08005213
5214 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005215 return 1;
5216
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005217 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005218
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005219 if (!enable_unrestricted_guest) {
5220 if (enable_ept) {
5221 if (!is_paging(vcpu)) {
5222 hw_cr4 &= ~X86_CR4_PAE;
5223 hw_cr4 |= X86_CR4_PSE;
5224 } else if (!(cr4 & X86_CR4_PAE)) {
5225 hw_cr4 &= ~X86_CR4_PAE;
5226 }
5227 }
5228
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005229 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005230 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5231 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5232 * to be manually disabled when guest switches to non-paging
5233 * mode.
5234 *
5235 * If !enable_unrestricted_guest, the CPU is always running
5236 * with CR0.PG=1 and CR4 needs to be modified.
5237 * If enable_unrestricted_guest, the CPU automatically
5238 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005239 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005240 if (!is_paging(vcpu))
5241 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5242 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005243
Sheng Yang14394422008-04-28 12:24:45 +08005244 vmcs_writel(CR4_READ_SHADOW, cr4);
5245 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005246 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005247}
5248
Avi Kivity6aa8b732006-12-10 02:21:36 -08005249static void vmx_get_segment(struct kvm_vcpu *vcpu,
5250 struct kvm_segment *var, int seg)
5251{
Avi Kivitya9179492011-01-03 14:28:52 +02005252 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005253 u32 ar;
5254
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005255 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005256 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005257 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005258 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005259 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005260 var->base = vmx_read_guest_seg_base(vmx, seg);
5261 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5262 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005263 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005264 var->base = vmx_read_guest_seg_base(vmx, seg);
5265 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5266 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5267 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005268 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005269 var->type = ar & 15;
5270 var->s = (ar >> 4) & 1;
5271 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005272 /*
5273 * Some userspaces do not preserve unusable property. Since usable
5274 * segment has to be present according to VMX spec we can use present
5275 * property to amend userspace bug by making unusable segment always
5276 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5277 * segment as unusable.
5278 */
5279 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005280 var->avl = (ar >> 12) & 1;
5281 var->l = (ar >> 13) & 1;
5282 var->db = (ar >> 14) & 1;
5283 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005284}
5285
Avi Kivitya9179492011-01-03 14:28:52 +02005286static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5287{
Avi Kivitya9179492011-01-03 14:28:52 +02005288 struct kvm_segment s;
5289
5290 if (to_vmx(vcpu)->rmode.vm86_active) {
5291 vmx_get_segment(vcpu, &s, seg);
5292 return s.base;
5293 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005294 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005295}
5296
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005297static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005298{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005299 struct vcpu_vmx *vmx = to_vmx(vcpu);
5300
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005301 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005302 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005303 else {
5304 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005305 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005306 }
Avi Kivity69c73022011-03-07 15:26:44 +02005307}
5308
Avi Kivity653e3102007-05-07 10:55:37 +03005309static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005311 u32 ar;
5312
Avi Kivityf0495f92012-06-07 17:06:10 +03005313 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005314 ar = 1 << 16;
5315 else {
5316 ar = var->type & 15;
5317 ar |= (var->s & 1) << 4;
5318 ar |= (var->dpl & 3) << 5;
5319 ar |= (var->present & 1) << 7;
5320 ar |= (var->avl & 1) << 12;
5321 ar |= (var->l & 1) << 13;
5322 ar |= (var->db & 1) << 14;
5323 ar |= (var->g & 1) << 15;
5324 }
Avi Kivity653e3102007-05-07 10:55:37 +03005325
5326 return ar;
5327}
5328
5329static void vmx_set_segment(struct kvm_vcpu *vcpu,
5330 struct kvm_segment *var, int seg)
5331{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005332 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005333 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005334
Avi Kivity2fb92db2011-04-27 19:42:18 +03005335 vmx_segment_cache_clear(vmx);
5336
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005337 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5338 vmx->rmode.segs[seg] = *var;
5339 if (seg == VCPU_SREG_TR)
5340 vmcs_write16(sf->selector, var->selector);
5341 else if (var->s)
5342 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005343 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005344 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005345
Avi Kivity653e3102007-05-07 10:55:37 +03005346 vmcs_writel(sf->base, var->base);
5347 vmcs_write32(sf->limit, var->limit);
5348 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005349
5350 /*
5351 * Fix the "Accessed" bit in AR field of segment registers for older
5352 * qemu binaries.
5353 * IA32 arch specifies that at the time of processor reset the
5354 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005355 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005356 * state vmexit when "unrestricted guest" mode is turned on.
5357 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5358 * tree. Newer qemu binaries with that qemu fix would not need this
5359 * kvm hack.
5360 */
5361 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005362 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005363
Gleb Natapovf924d662012-12-12 19:10:55 +02005364 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005365
5366out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005367 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005368}
5369
Avi Kivity6aa8b732006-12-10 02:21:36 -08005370static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5371{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005372 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005373
5374 *db = (ar >> 14) & 1;
5375 *l = (ar >> 13) & 1;
5376}
5377
Gleb Natapov89a27f42010-02-16 10:51:48 +02005378static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005380 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5381 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005382}
5383
Gleb Natapov89a27f42010-02-16 10:51:48 +02005384static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005385{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005386 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5387 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388}
5389
Gleb Natapov89a27f42010-02-16 10:51:48 +02005390static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005391{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005392 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5393 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005394}
5395
Gleb Natapov89a27f42010-02-16 10:51:48 +02005396static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005397{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005398 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5399 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005400}
5401
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005402static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5403{
5404 struct kvm_segment var;
5405 u32 ar;
5406
5407 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005408 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005409 if (seg == VCPU_SREG_CS)
5410 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005411 ar = vmx_segment_access_rights(&var);
5412
5413 if (var.base != (var.selector << 4))
5414 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005415 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005416 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005417 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005418 return false;
5419
5420 return true;
5421}
5422
5423static bool code_segment_valid(struct kvm_vcpu *vcpu)
5424{
5425 struct kvm_segment cs;
5426 unsigned int cs_rpl;
5427
5428 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005429 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005430
Avi Kivity1872a3f2009-01-04 23:26:52 +02005431 if (cs.unusable)
5432 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005433 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005434 return false;
5435 if (!cs.s)
5436 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005437 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005438 if (cs.dpl > cs_rpl)
5439 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005440 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005441 if (cs.dpl != cs_rpl)
5442 return false;
5443 }
5444 if (!cs.present)
5445 return false;
5446
5447 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5448 return true;
5449}
5450
5451static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5452{
5453 struct kvm_segment ss;
5454 unsigned int ss_rpl;
5455
5456 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005457 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005458
Avi Kivity1872a3f2009-01-04 23:26:52 +02005459 if (ss.unusable)
5460 return true;
5461 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005462 return false;
5463 if (!ss.s)
5464 return false;
5465 if (ss.dpl != ss_rpl) /* DPL != RPL */
5466 return false;
5467 if (!ss.present)
5468 return false;
5469
5470 return true;
5471}
5472
5473static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5474{
5475 struct kvm_segment var;
5476 unsigned int rpl;
5477
5478 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005479 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005480
Avi Kivity1872a3f2009-01-04 23:26:52 +02005481 if (var.unusable)
5482 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005483 if (!var.s)
5484 return false;
5485 if (!var.present)
5486 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005487 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005488 if (var.dpl < rpl) /* DPL < RPL */
5489 return false;
5490 }
5491
5492 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5493 * rights flags
5494 */
5495 return true;
5496}
5497
5498static bool tr_valid(struct kvm_vcpu *vcpu)
5499{
5500 struct kvm_segment tr;
5501
5502 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5503
Avi Kivity1872a3f2009-01-04 23:26:52 +02005504 if (tr.unusable)
5505 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005506 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005507 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005508 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005509 return false;
5510 if (!tr.present)
5511 return false;
5512
5513 return true;
5514}
5515
5516static bool ldtr_valid(struct kvm_vcpu *vcpu)
5517{
5518 struct kvm_segment ldtr;
5519
5520 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5521
Avi Kivity1872a3f2009-01-04 23:26:52 +02005522 if (ldtr.unusable)
5523 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005524 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005525 return false;
5526 if (ldtr.type != 2)
5527 return false;
5528 if (!ldtr.present)
5529 return false;
5530
5531 return true;
5532}
5533
5534static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5535{
5536 struct kvm_segment cs, ss;
5537
5538 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5539 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5540
Nadav Amitb32a9912015-03-29 16:33:04 +03005541 return ((cs.selector & SEGMENT_RPL_MASK) ==
5542 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005543}
5544
5545/*
5546 * Check if guest state is valid. Returns true if valid, false if
5547 * not.
5548 * We assume that registers are always usable
5549 */
5550static bool guest_state_valid(struct kvm_vcpu *vcpu)
5551{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005552 if (enable_unrestricted_guest)
5553 return true;
5554
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005555 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005556 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005557 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5558 return false;
5559 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5560 return false;
5561 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5562 return false;
5563 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5564 return false;
5565 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5566 return false;
5567 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5568 return false;
5569 } else {
5570 /* protected mode guest state checks */
5571 if (!cs_ss_rpl_check(vcpu))
5572 return false;
5573 if (!code_segment_valid(vcpu))
5574 return false;
5575 if (!stack_segment_valid(vcpu))
5576 return false;
5577 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5578 return false;
5579 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5580 return false;
5581 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5582 return false;
5583 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5584 return false;
5585 if (!tr_valid(vcpu))
5586 return false;
5587 if (!ldtr_valid(vcpu))
5588 return false;
5589 }
5590 /* TODO:
5591 * - Add checks on RIP
5592 * - Add checks on RFLAGS
5593 */
5594
5595 return true;
5596}
5597
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005598static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5599{
5600 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5601}
5602
Mike Dayd77c26f2007-10-08 09:02:08 -04005603static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005605 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005606 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005607 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005608
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005609 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005610 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005611 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5612 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005613 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005614 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005615 r = kvm_write_guest_page(kvm, fn++, &data,
5616 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005617 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005618 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005619 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5620 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005621 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005622 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5623 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005624 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005625 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005626 r = kvm_write_guest_page(kvm, fn, &data,
5627 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5628 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005629out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005630 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005631 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005632}
5633
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005634static int init_rmode_identity_map(struct kvm *kvm)
5635{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005636 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005637 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005638 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005639 u32 tmp;
5640
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005641 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005642 mutex_lock(&kvm->slots_lock);
5643
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005644 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005645 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005646
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005647 if (!kvm_vmx->ept_identity_map_addr)
5648 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5649 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005650
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005651 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005652 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005653 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005654 goto out2;
5655
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005656 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005657 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5658 if (r < 0)
5659 goto out;
5660 /* Set up identity-mapping pagetable for EPT in real mode */
5661 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5662 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5663 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5664 r = kvm_write_guest_page(kvm, identity_map_pfn,
5665 &tmp, i * sizeof(tmp), sizeof(tmp));
5666 if (r < 0)
5667 goto out;
5668 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005669 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005670
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005671out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005672 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005673
5674out2:
5675 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005676 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005677}
5678
Avi Kivity6aa8b732006-12-10 02:21:36 -08005679static void seg_setup(int seg)
5680{
Mathias Krause772e0312012-08-30 01:30:19 +02005681 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005682 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005683
5684 vmcs_write16(sf->selector, 0);
5685 vmcs_writel(sf->base, 0);
5686 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005687 ar = 0x93;
5688 if (seg == VCPU_SREG_CS)
5689 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005690
5691 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005692}
5693
Sheng Yangf78e0e22007-10-29 09:40:42 +08005694static int alloc_apic_access_page(struct kvm *kvm)
5695{
Xiao Guangrong44841412012-09-07 14:14:20 +08005696 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005697 int r = 0;
5698
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005699 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005700 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005701 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005702 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5703 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005704 if (r)
5705 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005706
Tang Chen73a6d942014-09-11 13:38:00 +08005707 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005708 if (is_error_page(page)) {
5709 r = -EFAULT;
5710 goto out;
5711 }
5712
Tang Chenc24ae0d2014-09-24 15:57:58 +08005713 /*
5714 * Do not pin the page in memory, so that memory hot-unplug
5715 * is able to migrate it.
5716 */
5717 put_page(page);
5718 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005719out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005720 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005721 return r;
5722}
5723
Wanpeng Li991e7a02015-09-16 17:30:05 +08005724static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005725{
5726 int vpid;
5727
Avi Kivity919818a2009-03-23 18:01:29 +02005728 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005729 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005730 spin_lock(&vmx_vpid_lock);
5731 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005732 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005733 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005734 else
5735 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005736 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005737 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005738}
5739
Wanpeng Li991e7a02015-09-16 17:30:05 +08005740static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005741{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005742 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005743 return;
5744 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005745 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005746 spin_unlock(&vmx_vpid_lock);
5747}
5748
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005749static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5750 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005751{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005752 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005753
5754 if (!cpu_has_vmx_msr_bitmap())
5755 return;
5756
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005757 if (static_branch_unlikely(&enable_evmcs))
5758 evmcs_touch_msr_bitmap();
5759
Sheng Yang25c5f222008-03-28 13:18:56 +08005760 /*
5761 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5762 * have the write-low and read-high bitmap offsets the wrong way round.
5763 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5764 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005765 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005766 if (type & MSR_TYPE_R)
5767 /* read-low */
5768 __clear_bit(msr, msr_bitmap + 0x000 / f);
5769
5770 if (type & MSR_TYPE_W)
5771 /* write-low */
5772 __clear_bit(msr, msr_bitmap + 0x800 / f);
5773
Sheng Yang25c5f222008-03-28 13:18:56 +08005774 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5775 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005776 if (type & MSR_TYPE_R)
5777 /* read-high */
5778 __clear_bit(msr, msr_bitmap + 0x400 / f);
5779
5780 if (type & MSR_TYPE_W)
5781 /* write-high */
5782 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5783
5784 }
5785}
5786
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005787static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5788 u32 msr, int type)
5789{
5790 int f = sizeof(unsigned long);
5791
5792 if (!cpu_has_vmx_msr_bitmap())
5793 return;
5794
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005795 if (static_branch_unlikely(&enable_evmcs))
5796 evmcs_touch_msr_bitmap();
5797
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005798 /*
5799 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5800 * have the write-low and read-high bitmap offsets the wrong way round.
5801 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5802 */
5803 if (msr <= 0x1fff) {
5804 if (type & MSR_TYPE_R)
5805 /* read-low */
5806 __set_bit(msr, msr_bitmap + 0x000 / f);
5807
5808 if (type & MSR_TYPE_W)
5809 /* write-low */
5810 __set_bit(msr, msr_bitmap + 0x800 / f);
5811
5812 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5813 msr &= 0x1fff;
5814 if (type & MSR_TYPE_R)
5815 /* read-high */
5816 __set_bit(msr, msr_bitmap + 0x400 / f);
5817
5818 if (type & MSR_TYPE_W)
5819 /* write-high */
5820 __set_bit(msr, msr_bitmap + 0xc00 / f);
5821
5822 }
5823}
5824
5825static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5826 u32 msr, int type, bool value)
5827{
5828 if (value)
5829 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5830 else
5831 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5832}
5833
Wincy Vanf2b93282015-02-03 23:56:03 +08005834/*
5835 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5836 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5837 */
5838static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5839 unsigned long *msr_bitmap_nested,
5840 u32 msr, int type)
5841{
5842 int f = sizeof(unsigned long);
5843
Wincy Vanf2b93282015-02-03 23:56:03 +08005844 /*
5845 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5846 * have the write-low and read-high bitmap offsets the wrong way round.
5847 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5848 */
5849 if (msr <= 0x1fff) {
5850 if (type & MSR_TYPE_R &&
5851 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5852 /* read-low */
5853 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5854
5855 if (type & MSR_TYPE_W &&
5856 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5857 /* write-low */
5858 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5859
5860 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5861 msr &= 0x1fff;
5862 if (type & MSR_TYPE_R &&
5863 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5864 /* read-high */
5865 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5866
5867 if (type & MSR_TYPE_W &&
5868 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5869 /* write-high */
5870 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5871
5872 }
5873}
5874
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005875static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005876{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005877 u8 mode = 0;
5878
5879 if (cpu_has_secondary_exec_ctrls() &&
5880 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5881 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5882 mode |= MSR_BITMAP_MODE_X2APIC;
5883 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5884 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5885 }
5886
5887 if (is_long_mode(vcpu))
5888 mode |= MSR_BITMAP_MODE_LM;
5889
5890 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005891}
5892
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005893#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5894
5895static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5896 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005897{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005898 int msr;
5899
5900 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5901 unsigned word = msr / BITS_PER_LONG;
5902 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5903 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005904 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005905
5906 if (mode & MSR_BITMAP_MODE_X2APIC) {
5907 /*
5908 * TPR reads and writes can be virtualized even if virtual interrupt
5909 * delivery is not in use.
5910 */
5911 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5912 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5913 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5914 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5915 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5916 }
5917 }
5918}
5919
5920static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5921{
5922 struct vcpu_vmx *vmx = to_vmx(vcpu);
5923 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5924 u8 mode = vmx_msr_bitmap_mode(vcpu);
5925 u8 changed = mode ^ vmx->msr_bitmap_mode;
5926
5927 if (!changed)
5928 return;
5929
5930 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5931 !(mode & MSR_BITMAP_MODE_LM));
5932
5933 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5934 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5935
5936 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005937}
5938
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005939static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005940{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005941 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005942}
5943
David Matlackc9f04402017-08-01 14:00:40 -07005944static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5945{
5946 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5947 gfn_t gfn;
5948
5949 /*
5950 * Don't need to mark the APIC access page dirty; it is never
5951 * written to by the CPU during APIC virtualization.
5952 */
5953
5954 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5955 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5956 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5957 }
5958
5959 if (nested_cpu_has_posted_intr(vmcs12)) {
5960 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5961 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5962 }
5963}
5964
5965
David Hildenbrand6342c502017-01-25 11:58:58 +01005966static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005967{
5968 struct vcpu_vmx *vmx = to_vmx(vcpu);
5969 int max_irr;
5970 void *vapic_page;
5971 u16 status;
5972
David Matlackc9f04402017-08-01 14:00:40 -07005973 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5974 return;
Wincy Van705699a2015-02-03 23:58:17 +08005975
David Matlackc9f04402017-08-01 14:00:40 -07005976 vmx->nested.pi_pending = false;
5977 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5978 return;
Wincy Van705699a2015-02-03 23:58:17 +08005979
David Matlackc9f04402017-08-01 14:00:40 -07005980 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5981 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005982 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005983 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5984 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005985 kunmap(vmx->nested.virtual_apic_page);
5986
5987 status = vmcs_read16(GUEST_INTR_STATUS);
5988 if ((u8)max_irr > ((u8)status & 0xff)) {
5989 status &= ~0xff;
5990 status |= (u8)max_irr;
5991 vmcs_write16(GUEST_INTR_STATUS, status);
5992 }
5993 }
David Matlackc9f04402017-08-01 14:00:40 -07005994
5995 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005996}
5997
Wincy Van06a55242017-04-28 13:13:59 +08005998static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5999 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006000{
6001#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006002 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6003
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006004 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006005 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006006 * The vector of interrupt to be delivered to vcpu had
6007 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006008 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006009 * Following cases will be reached in this block, and
6010 * we always send a notification event in all cases as
6011 * explained below.
6012 *
6013 * Case 1: vcpu keeps in non-root mode. Sending a
6014 * notification event posts the interrupt to vcpu.
6015 *
6016 * Case 2: vcpu exits to root mode and is still
6017 * runnable. PIR will be synced to vIRR before the
6018 * next vcpu entry. Sending a notification event in
6019 * this case has no effect, as vcpu is not in root
6020 * mode.
6021 *
6022 * Case 3: vcpu exits to root mode and is blocked.
6023 * vcpu_block() has already synced PIR to vIRR and
6024 * never blocks vcpu if vIRR is not cleared. Therefore,
6025 * a blocked vcpu here does not wait for any requested
6026 * interrupts in PIR, and sending a notification event
6027 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006028 */
Feng Wu28b835d2015-09-18 22:29:54 +08006029
Wincy Van06a55242017-04-28 13:13:59 +08006030 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006031 return true;
6032 }
6033#endif
6034 return false;
6035}
6036
Wincy Van705699a2015-02-03 23:58:17 +08006037static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6038 int vector)
6039{
6040 struct vcpu_vmx *vmx = to_vmx(vcpu);
6041
6042 if (is_guest_mode(vcpu) &&
6043 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006044 /*
6045 * If a posted intr is not recognized by hardware,
6046 * we will accomplish it in the next vmentry.
6047 */
6048 vmx->nested.pi_pending = true;
6049 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006050 /* the PIR and ON have been set by L1. */
6051 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6052 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006053 return 0;
6054 }
6055 return -1;
6056}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006057/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006058 * Send interrupt to vcpu via posted interrupt way.
6059 * 1. If target vcpu is running(non-root mode), send posted interrupt
6060 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6061 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6062 * interrupt from PIR in next vmentry.
6063 */
6064static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6065{
6066 struct vcpu_vmx *vmx = to_vmx(vcpu);
6067 int r;
6068
Wincy Van705699a2015-02-03 23:58:17 +08006069 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6070 if (!r)
6071 return;
6072
Yang Zhanga20ed542013-04-11 19:25:15 +08006073 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6074 return;
6075
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006076 /* If a previous notification has sent the IPI, nothing to do. */
6077 if (pi_test_and_set_on(&vmx->pi_desc))
6078 return;
6079
Wincy Van06a55242017-04-28 13:13:59 +08006080 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006081 kvm_vcpu_kick(vcpu);
6082}
6083
Avi Kivity6aa8b732006-12-10 02:21:36 -08006084/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006085 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6086 * will not change in the lifetime of the guest.
6087 * Note that host-state that does change is set elsewhere. E.g., host-state
6088 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6089 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006090static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006091{
6092 u32 low32, high32;
6093 unsigned long tmpl;
6094 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006095 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006096
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006097 cr0 = read_cr0();
6098 WARN_ON(cr0 & X86_CR0_TS);
6099 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006100
6101 /*
6102 * Save the most likely value for this task's CR3 in the VMCS.
6103 * We can't use __get_current_cr3_fast() because we're not atomic.
6104 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006105 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006106 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006107 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006108
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006109 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006110 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006111 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006112 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006113
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006114 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006115#ifdef CONFIG_X86_64
6116 /*
6117 * Load null selectors, so we can avoid reloading them in
6118 * __vmx_load_host_state(), in case userspace uses the null selectors
6119 * too (the expected case).
6120 */
6121 vmcs_write16(HOST_DS_SELECTOR, 0);
6122 vmcs_write16(HOST_ES_SELECTOR, 0);
6123#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006124 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6125 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006126#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006127 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6128 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6129
Juergen Gross87930012017-09-04 12:25:27 +02006130 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006131 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006132 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006133
Avi Kivity83287ea422012-09-16 15:10:57 +03006134 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006135
6136 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6137 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6138 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6139 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6140
6141 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6142 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6143 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6144 }
6145}
6146
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006147static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6148{
6149 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6150 if (enable_ept)
6151 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006152 if (is_guest_mode(&vmx->vcpu))
6153 vmx->vcpu.arch.cr4_guest_owned_bits &=
6154 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006155 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6156}
6157
Yang Zhang01e439b2013-04-11 19:25:12 +08006158static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6159{
6160 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6161
Andrey Smetanind62caab2015-11-10 15:36:33 +03006162 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006163 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006164
6165 if (!enable_vnmi)
6166 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6167
Yunhong Jiang64672c92016-06-13 14:19:59 -07006168 /* Enable the preemption timer dynamically */
6169 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006170 return pin_based_exec_ctrl;
6171}
6172
Andrey Smetanind62caab2015-11-10 15:36:33 +03006173static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6174{
6175 struct vcpu_vmx *vmx = to_vmx(vcpu);
6176
6177 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006178 if (cpu_has_secondary_exec_ctrls()) {
6179 if (kvm_vcpu_apicv_active(vcpu))
6180 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6181 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6182 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6183 else
6184 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6185 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6186 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6187 }
6188
6189 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006190 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006191}
6192
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006193static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6194{
6195 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006196
6197 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6198 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6199
Paolo Bonzini35754c92015-07-29 12:05:37 +02006200 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006201 exec_control &= ~CPU_BASED_TPR_SHADOW;
6202#ifdef CONFIG_X86_64
6203 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6204 CPU_BASED_CR8_LOAD_EXITING;
6205#endif
6206 }
6207 if (!enable_ept)
6208 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6209 CPU_BASED_CR3_LOAD_EXITING |
6210 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006211 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6212 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6213 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006214 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6215 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006216 return exec_control;
6217}
6218
Jim Mattson45ec3682017-08-23 16:32:04 -07006219static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006220{
Jim Mattson45ec3682017-08-23 16:32:04 -07006221 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006222 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006223}
6224
Jim Mattson75f4fc82017-08-23 16:32:03 -07006225static bool vmx_rdseed_supported(void)
6226{
6227 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006228 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006229}
6230
Paolo Bonzini80154d72017-08-24 13:55:35 +02006231static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006232{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006233 struct kvm_vcpu *vcpu = &vmx->vcpu;
6234
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006235 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006236
Paolo Bonzini80154d72017-08-24 13:55:35 +02006237 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006238 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6239 if (vmx->vpid == 0)
6240 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6241 if (!enable_ept) {
6242 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6243 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006244 /* Enable INVPCID for non-ept guests may cause performance regression. */
6245 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006246 }
6247 if (!enable_unrestricted_guest)
6248 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006249 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006250 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006251 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006252 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6253 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006254 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006255
6256 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6257 * in vmx_set_cr4. */
6258 exec_control &= ~SECONDARY_EXEC_DESC;
6259
Abel Gordonabc4fc52013-04-18 14:35:25 +03006260 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6261 (handle_vmptrld).
6262 We can NOT enable shadow_vmcs here because we don't have yet
6263 a current VMCS12
6264 */
6265 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006266
6267 if (!enable_pml)
6268 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006269
Paolo Bonzini3db13482017-08-24 14:48:03 +02006270 if (vmx_xsaves_supported()) {
6271 /* Exposing XSAVES only when XSAVE is exposed */
6272 bool xsaves_enabled =
6273 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6274 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6275
6276 if (!xsaves_enabled)
6277 exec_control &= ~SECONDARY_EXEC_XSAVES;
6278
6279 if (nested) {
6280 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006281 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006282 SECONDARY_EXEC_XSAVES;
6283 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006284 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006285 ~SECONDARY_EXEC_XSAVES;
6286 }
6287 }
6288
Paolo Bonzini80154d72017-08-24 13:55:35 +02006289 if (vmx_rdtscp_supported()) {
6290 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6291 if (!rdtscp_enabled)
6292 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6293
6294 if (nested) {
6295 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006296 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006297 SECONDARY_EXEC_RDTSCP;
6298 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006299 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006300 ~SECONDARY_EXEC_RDTSCP;
6301 }
6302 }
6303
6304 if (vmx_invpcid_supported()) {
6305 /* Exposing INVPCID only when PCID is exposed */
6306 bool invpcid_enabled =
6307 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6308 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6309
6310 if (!invpcid_enabled) {
6311 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6312 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6313 }
6314
6315 if (nested) {
6316 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006317 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006318 SECONDARY_EXEC_ENABLE_INVPCID;
6319 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006320 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006321 ~SECONDARY_EXEC_ENABLE_INVPCID;
6322 }
6323 }
6324
Jim Mattson45ec3682017-08-23 16:32:04 -07006325 if (vmx_rdrand_supported()) {
6326 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6327 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006328 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006329
6330 if (nested) {
6331 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006332 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006333 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006334 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006335 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006336 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006337 }
6338 }
6339
Jim Mattson75f4fc82017-08-23 16:32:03 -07006340 if (vmx_rdseed_supported()) {
6341 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6342 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006343 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006344
6345 if (nested) {
6346 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006347 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006348 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006349 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006350 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006351 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006352 }
6353 }
6354
Paolo Bonzini80154d72017-08-24 13:55:35 +02006355 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006356}
6357
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006358static void ept_set_mmio_spte_mask(void)
6359{
6360 /*
6361 * EPT Misconfigurations can be generated if the value of bits 2:0
6362 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006363 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006364 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6365 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006366}
6367
Wanpeng Lif53cd632014-12-02 19:14:58 +08006368#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006369/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006370 * Sets up the vmcs for emulated real mode.
6371 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006372static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006373{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006374#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006375 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006376#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006377 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006378
Abel Gordon4607c2d2013-04-18 14:35:55 +03006379 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006380 /*
6381 * At vCPU creation, "VMWRITE to any supported field
6382 * in the VMCS" is supported, so use the more
6383 * permissive vmx_vmread_bitmap to specify both read
6384 * and write permissions for the shadow VMCS.
6385 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006386 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006387 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006388 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006389 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006390 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006391
Avi Kivity6aa8b732006-12-10 02:21:36 -08006392 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6393
Avi Kivity6aa8b732006-12-10 02:21:36 -08006394 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006395 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006396 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006397
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006398 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006399
Dan Williamsdfa169b2016-06-02 11:17:24 -07006400 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006401 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006402 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006403 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006404 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006405
Andrey Smetanind62caab2015-11-10 15:36:33 +03006406 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006407 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6408 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6409 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6410 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6411
6412 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006413
Li RongQing0bcf2612015-12-03 13:29:34 +08006414 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006415 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006416 }
6417
Wanpeng Lib31c1142018-03-12 04:53:04 -07006418 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006419 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006420 vmx->ple_window = ple_window;
6421 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006422 }
6423
Xiao Guangrongc3707952011-07-12 03:28:04 +08006424 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6425 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006426 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6427
Avi Kivity9581d442010-10-19 16:46:55 +02006428 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6429 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006430 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006431#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006432 rdmsrl(MSR_FS_BASE, a);
6433 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6434 rdmsrl(MSR_GS_BASE, a);
6435 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6436#else
6437 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6438 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6439#endif
6440
Bandan Das2a499e42017-08-03 15:54:41 -04006441 if (cpu_has_vmx_vmfunc())
6442 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6443
Eddie Dong2cc51562007-05-21 07:28:09 +03006444 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6445 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006446 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006447 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006448 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006449
Radim Krčmář74545702015-04-27 15:11:25 +02006450 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6451 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006452
Paolo Bonzini03916db2014-07-24 14:21:57 +02006453 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006454 u32 index = vmx_msr_index[i];
6455 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006456 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006457
6458 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6459 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006460 if (wrmsr_safe(index, data_low, data_high) < 0)
6461 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006462 vmx->guest_msrs[j].index = i;
6463 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006464 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006465 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006466 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006467
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006468 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6469 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006470
6471 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006472
6473 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006474 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006475
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006476 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6477 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6478
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006479 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006480
Wanpeng Lif53cd632014-12-02 19:14:58 +08006481 if (vmx_xsaves_supported())
6482 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6483
Peter Feiner4e595162016-07-07 14:49:58 -07006484 if (enable_pml) {
6485 ASSERT(vmx->pml_pg);
6486 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6487 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6488 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006489}
6490
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006491static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006492{
6493 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006494 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006495 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006496
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006497 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006498 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006499
Wanpeng Li518e7b92018-02-28 14:03:31 +08006500 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006501 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006502 kvm_set_cr8(vcpu, 0);
6503
6504 if (!init_event) {
6505 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6506 MSR_IA32_APICBASE_ENABLE;
6507 if (kvm_vcpu_is_reset_bsp(vcpu))
6508 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6509 apic_base_msr.host_initiated = true;
6510 kvm_set_apic_base(vcpu, &apic_base_msr);
6511 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006512
Avi Kivity2fb92db2011-04-27 19:42:18 +03006513 vmx_segment_cache_clear(vmx);
6514
Avi Kivity5706be02008-08-20 15:07:31 +03006515 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006516 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006517 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006518
6519 seg_setup(VCPU_SREG_DS);
6520 seg_setup(VCPU_SREG_ES);
6521 seg_setup(VCPU_SREG_FS);
6522 seg_setup(VCPU_SREG_GS);
6523 seg_setup(VCPU_SREG_SS);
6524
6525 vmcs_write16(GUEST_TR_SELECTOR, 0);
6526 vmcs_writel(GUEST_TR_BASE, 0);
6527 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6528 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6529
6530 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6531 vmcs_writel(GUEST_LDTR_BASE, 0);
6532 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6533 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6534
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006535 if (!init_event) {
6536 vmcs_write32(GUEST_SYSENTER_CS, 0);
6537 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6538 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6539 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6540 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006541
Wanpeng Lic37c2872017-11-20 14:52:21 -08006542 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006543 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006544
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006545 vmcs_writel(GUEST_GDTR_BASE, 0);
6546 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6547
6548 vmcs_writel(GUEST_IDTR_BASE, 0);
6549 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6550
Anthony Liguori443381a2010-12-06 10:53:38 -06006551 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006552 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006553 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006554 if (kvm_mpx_supported())
6555 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006556
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006557 setup_msrs(vmx);
6558
Avi Kivity6aa8b732006-12-10 02:21:36 -08006559 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6560
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006561 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006562 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006563 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006564 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006565 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006566 vmcs_write32(TPR_THRESHOLD, 0);
6567 }
6568
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006569 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006570
Sheng Yang2384d2b2008-01-17 15:14:33 +08006571 if (vmx->vpid != 0)
6572 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6573
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006574 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006575 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006576 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006577 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006578 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006579
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006580 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006581
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006582 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006583 if (init_event)
6584 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006585}
6586
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006587/*
6588 * In nested virtualization, check if L1 asked to exit on external interrupts.
6589 * For most existing hypervisors, this will always return true.
6590 */
6591static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6592{
6593 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6594 PIN_BASED_EXT_INTR_MASK;
6595}
6596
Bandan Das77b0f5d2014-04-19 18:17:45 -04006597/*
6598 * In nested virtualization, check if L1 has set
6599 * VM_EXIT_ACK_INTR_ON_EXIT
6600 */
6601static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6602{
6603 return get_vmcs12(vcpu)->vm_exit_controls &
6604 VM_EXIT_ACK_INTR_ON_EXIT;
6605}
6606
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006607static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6608{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006609 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006610}
6611
Jan Kiszkac9a79532014-03-07 20:03:15 +01006612static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006613{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006614 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6615 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006616}
6617
Jan Kiszkac9a79532014-03-07 20:03:15 +01006618static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006619{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006620 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006621 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006622 enable_irq_window(vcpu);
6623 return;
6624 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006625
Paolo Bonzini47c01522016-12-19 11:44:07 +01006626 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6627 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006628}
6629
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006630static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006631{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006632 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006633 uint32_t intr;
6634 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006635
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006636 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006637
Avi Kivityfa89a812008-09-01 15:57:51 +03006638 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006639 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006640 int inc_eip = 0;
6641 if (vcpu->arch.interrupt.soft)
6642 inc_eip = vcpu->arch.event_exit_inst_len;
6643 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006644 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006645 return;
6646 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006647 intr = irq | INTR_INFO_VALID_MASK;
6648 if (vcpu->arch.interrupt.soft) {
6649 intr |= INTR_TYPE_SOFT_INTR;
6650 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6651 vmx->vcpu.arch.event_exit_inst_len);
6652 } else
6653 intr |= INTR_TYPE_EXT_INTR;
6654 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006655
6656 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006657}
6658
Sheng Yangf08864b2008-05-15 18:23:25 +08006659static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6660{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006661 struct vcpu_vmx *vmx = to_vmx(vcpu);
6662
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006663 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006664 /*
6665 * Tracking the NMI-blocked state in software is built upon
6666 * finding the next open IRQ window. This, in turn, depends on
6667 * well-behaving guests: They have to keep IRQs disabled at
6668 * least as long as the NMI handler runs. Otherwise we may
6669 * cause NMI nesting, maybe breaking the guest. But as this is
6670 * highly unlikely, we can live with the residual risk.
6671 */
6672 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6673 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6674 }
6675
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006676 ++vcpu->stat.nmi_injections;
6677 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006678
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006679 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006680 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006681 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006682 return;
6683 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006684
Sheng Yangf08864b2008-05-15 18:23:25 +08006685 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6686 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006687
6688 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006689}
6690
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006691static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6692{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006693 struct vcpu_vmx *vmx = to_vmx(vcpu);
6694 bool masked;
6695
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006696 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006697 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006698 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006699 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006700 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6701 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6702 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006703}
6704
6705static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6706{
6707 struct vcpu_vmx *vmx = to_vmx(vcpu);
6708
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006709 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006710 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6711 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6712 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6713 }
6714 } else {
6715 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6716 if (masked)
6717 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6718 GUEST_INTR_STATE_NMI);
6719 else
6720 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6721 GUEST_INTR_STATE_NMI);
6722 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006723}
6724
Jan Kiszka2505dc92013-04-14 12:12:47 +02006725static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6726{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006727 if (to_vmx(vcpu)->nested.nested_run_pending)
6728 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006729
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006730 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006731 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6732 return 0;
6733
Jan Kiszka2505dc92013-04-14 12:12:47 +02006734 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6735 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6736 | GUEST_INTR_STATE_NMI));
6737}
6738
Gleb Natapov78646122009-03-23 12:12:11 +02006739static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6740{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006741 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6742 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006743 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6744 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006745}
6746
Izik Eiduscbc94022007-10-25 00:29:55 +02006747static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6748{
6749 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006750
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006751 if (enable_unrestricted_guest)
6752 return 0;
6753
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006754 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6755 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006756 if (ret)
6757 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006758 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006759 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006760}
6761
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006762static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6763{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006764 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006765 return 0;
6766}
6767
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006768static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006769{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006770 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006771 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006772 /*
6773 * Update instruction length as we may reinject the exception
6774 * from user space while in guest debugging mode.
6775 */
6776 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6777 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006778 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006779 return false;
6780 /* fall through */
6781 case DB_VECTOR:
6782 if (vcpu->guest_debug &
6783 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6784 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006785 /* fall through */
6786 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006787 case OF_VECTOR:
6788 case BR_VECTOR:
6789 case UD_VECTOR:
6790 case DF_VECTOR:
6791 case SS_VECTOR:
6792 case GP_VECTOR:
6793 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006794 return true;
6795 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006796 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006797 return false;
6798}
6799
6800static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6801 int vec, u32 err_code)
6802{
6803 /*
6804 * Instruction with address size override prefix opcode 0x67
6805 * Cause the #SS fault with 0 error code in VM86 mode.
6806 */
6807 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6808 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6809 if (vcpu->arch.halt_request) {
6810 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006811 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006812 }
6813 return 1;
6814 }
6815 return 0;
6816 }
6817
6818 /*
6819 * Forward all other exceptions that are valid in real mode.
6820 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6821 * the required debugging infrastructure rework.
6822 */
6823 kvm_queue_exception(vcpu, vec);
6824 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006825}
6826
Andi Kleena0861c02009-06-08 17:37:09 +08006827/*
6828 * Trigger machine check on the host. We assume all the MSRs are already set up
6829 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6830 * We pass a fake environment to the machine check handler because we want
6831 * the guest to be always treated like user space, no matter what context
6832 * it used internally.
6833 */
6834static void kvm_machine_check(void)
6835{
6836#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6837 struct pt_regs regs = {
6838 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6839 .flags = X86_EFLAGS_IF,
6840 };
6841
6842 do_machine_check(&regs, 0);
6843#endif
6844}
6845
Avi Kivity851ba692009-08-24 11:10:17 +03006846static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006847{
6848 /* already handled by vcpu_run */
6849 return 1;
6850}
6851
Avi Kivity851ba692009-08-24 11:10:17 +03006852static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006853{
Avi Kivity1155f762007-11-22 11:30:47 +02006854 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006855 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006856 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006857 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006858 u32 vect_info;
6859 enum emulation_result er;
6860
Avi Kivity1155f762007-11-22 11:30:47 +02006861 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006862 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863
Andi Kleena0861c02009-06-08 17:37:09 +08006864 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006865 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006866
Jim Mattsonef85b672016-12-12 11:01:37 -08006867 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006868 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006869
Wanpeng Li082d06e2018-04-03 16:28:48 -07006870 if (is_invalid_opcode(intr_info))
6871 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006872
Avi Kivity6aa8b732006-12-10 02:21:36 -08006873 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006874 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006875 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006876
Liran Alon9e869482018-03-12 13:12:51 +02006877 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6878 WARN_ON_ONCE(!enable_vmware_backdoor);
6879 er = emulate_instruction(vcpu,
6880 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6881 if (er == EMULATE_USER_EXIT)
6882 return 0;
6883 else if (er != EMULATE_DONE)
6884 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6885 return 1;
6886 }
6887
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006888 /*
6889 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6890 * MMIO, it is better to report an internal error.
6891 * See the comments in vmx_handle_exit.
6892 */
6893 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6894 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6895 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6896 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006897 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006898 vcpu->run->internal.data[0] = vect_info;
6899 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006900 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006901 return 0;
6902 }
6903
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 if (is_page_fault(intr_info)) {
6905 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006906 /* EPT won't cause page fault directly */
6907 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006908 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006909 }
6910
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006911 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006912
6913 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6914 return handle_rmode_exception(vcpu, ex_no, error_code);
6915
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006916 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006917 case AC_VECTOR:
6918 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6919 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006920 case DB_VECTOR:
6921 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6922 if (!(vcpu->guest_debug &
6923 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006924 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006925 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006926 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006927 skip_emulated_instruction(vcpu);
6928
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006929 kvm_queue_exception(vcpu, DB_VECTOR);
6930 return 1;
6931 }
6932 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6933 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6934 /* fall through */
6935 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006936 /*
6937 * Update instruction length as we may reinject #BP from
6938 * user space while in guest debugging mode. Reading it for
6939 * #DB as well causes no harm, it is not used in that case.
6940 */
6941 vmx->vcpu.arch.event_exit_inst_len =
6942 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006943 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006944 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006945 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6946 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006947 break;
6948 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006949 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6950 kvm_run->ex.exception = ex_no;
6951 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006952 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006953 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006954 return 0;
6955}
6956
Avi Kivity851ba692009-08-24 11:10:17 +03006957static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006958{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006959 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006960 return 1;
6961}
6962
Avi Kivity851ba692009-08-24 11:10:17 +03006963static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006964{
Avi Kivity851ba692009-08-24 11:10:17 +03006965 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006966 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006967 return 0;
6968}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006969
Avi Kivity851ba692009-08-24 11:10:17 +03006970static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006971{
He, Qingbfdaab02007-09-12 14:18:28 +08006972 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006973 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006974 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006975
He, Qingbfdaab02007-09-12 14:18:28 +08006976 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006977 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006978
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006979 ++vcpu->stat.io_exits;
6980
Sean Christopherson432baf62018-03-08 08:57:26 -08006981 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006982 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006983
6984 port = exit_qualification >> 16;
6985 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006986 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006987
Sean Christophersondca7f122018-03-08 08:57:27 -08006988 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006989}
6990
Ingo Molnar102d8322007-02-19 14:37:47 +02006991static void
6992vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6993{
6994 /*
6995 * Patch in the VMCALL instruction:
6996 */
6997 hypercall[0] = 0x0f;
6998 hypercall[1] = 0x01;
6999 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007000}
7001
Guo Chao0fa06072012-06-28 15:16:19 +08007002/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007003static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7004{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007005 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007006 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7007 unsigned long orig_val = val;
7008
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007009 /*
7010 * We get here when L2 changed cr0 in a way that did not change
7011 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007012 * but did change L0 shadowed bits. So we first calculate the
7013 * effective cr0 value that L1 would like to write into the
7014 * hardware. It consists of the L2-owned bits from the new
7015 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007016 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007017 val = (val & ~vmcs12->cr0_guest_host_mask) |
7018 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7019
David Matlack38991522016-11-29 18:14:08 -08007020 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007021 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007022
7023 if (kvm_set_cr0(vcpu, val))
7024 return 1;
7025 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007026 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007027 } else {
7028 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007029 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007030 return 1;
David Matlack38991522016-11-29 18:14:08 -08007031
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007032 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007033 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007034}
7035
7036static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7037{
7038 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007039 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7040 unsigned long orig_val = val;
7041
7042 /* analogously to handle_set_cr0 */
7043 val = (val & ~vmcs12->cr4_guest_host_mask) |
7044 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7045 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007046 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007047 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007048 return 0;
7049 } else
7050 return kvm_set_cr4(vcpu, val);
7051}
7052
Paolo Bonzini0367f202016-07-12 10:44:55 +02007053static int handle_desc(struct kvm_vcpu *vcpu)
7054{
7055 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7056 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7057}
7058
Avi Kivity851ba692009-08-24 11:10:17 +03007059static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007060{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007061 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007062 int cr;
7063 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007064 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007065 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007066
He, Qingbfdaab02007-09-12 14:18:28 +08007067 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007068 cr = exit_qualification & 15;
7069 reg = (exit_qualification >> 8) & 15;
7070 switch ((exit_qualification >> 4) & 3) {
7071 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007072 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007073 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007074 switch (cr) {
7075 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007076 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007077 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007079 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007080 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007081 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007083 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007084 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007085 case 8: {
7086 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007087 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007088 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007089 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007090 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007091 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007092 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007093 return ret;
7094 /*
7095 * TODO: we might be squashing a
7096 * KVM_GUESTDBG_SINGLESTEP-triggered
7097 * KVM_EXIT_DEBUG here.
7098 */
Avi Kivity851ba692009-08-24 11:10:17 +03007099 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007100 return 0;
7101 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007102 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007103 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007104 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007105 WARN_ONCE(1, "Guest should always own CR0.TS");
7106 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007107 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007108 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007109 case 1: /*mov from cr*/
7110 switch (cr) {
7111 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007112 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007113 val = kvm_read_cr3(vcpu);
7114 kvm_register_write(vcpu, reg, val);
7115 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007116 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007117 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007118 val = kvm_get_cr8(vcpu);
7119 kvm_register_write(vcpu, reg, val);
7120 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007121 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007122 }
7123 break;
7124 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007125 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007126 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007127 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007128
Kyle Huey6affcbe2016-11-29 12:40:40 -08007129 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130 default:
7131 break;
7132 }
Avi Kivity851ba692009-08-24 11:10:17 +03007133 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007134 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007135 (int)(exit_qualification >> 4) & 3, cr);
7136 return 0;
7137}
7138
Avi Kivity851ba692009-08-24 11:10:17 +03007139static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007140{
He, Qingbfdaab02007-09-12 14:18:28 +08007141 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007142 int dr, dr7, reg;
7143
7144 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7145 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7146
7147 /* First, if DR does not exist, trigger UD */
7148 if (!kvm_require_dr(vcpu, dr))
7149 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007150
Jan Kiszkaf2483412010-01-20 18:20:20 +01007151 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007152 if (!kvm_require_cpl(vcpu, 0))
7153 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007154 dr7 = vmcs_readl(GUEST_DR7);
7155 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007156 /*
7157 * As the vm-exit takes precedence over the debug trap, we
7158 * need to emulate the latter, either for the host or the
7159 * guest debugging itself.
7160 */
7161 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007162 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007163 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007164 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007165 vcpu->run->debug.arch.exception = DB_VECTOR;
7166 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007167 return 0;
7168 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007169 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007170 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007171 kvm_queue_exception(vcpu, DB_VECTOR);
7172 return 1;
7173 }
7174 }
7175
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007176 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007177 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7178 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007179
7180 /*
7181 * No more DR vmexits; force a reload of the debug registers
7182 * and reenter on this instruction. The next vmexit will
7183 * retrieve the full state of the debug registers.
7184 */
7185 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7186 return 1;
7187 }
7188
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007189 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7190 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007191 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007192
7193 if (kvm_get_dr(vcpu, dr, &val))
7194 return 1;
7195 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007196 } else
Nadav Amit57773922014-06-18 17:19:23 +03007197 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007198 return 1;
7199
Kyle Huey6affcbe2016-11-29 12:40:40 -08007200 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007201}
7202
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007203static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7204{
7205 return vcpu->arch.dr6;
7206}
7207
7208static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7209{
7210}
7211
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007212static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7213{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007214 get_debugreg(vcpu->arch.db[0], 0);
7215 get_debugreg(vcpu->arch.db[1], 1);
7216 get_debugreg(vcpu->arch.db[2], 2);
7217 get_debugreg(vcpu->arch.db[3], 3);
7218 get_debugreg(vcpu->arch.dr6, 6);
7219 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7220
7221 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007222 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007223}
7224
Gleb Natapov020df072010-04-13 10:05:23 +03007225static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7226{
7227 vmcs_writel(GUEST_DR7, val);
7228}
7229
Avi Kivity851ba692009-08-24 11:10:17 +03007230static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007231{
Kyle Huey6a908b62016-11-29 12:40:37 -08007232 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007233}
7234
Avi Kivity851ba692009-08-24 11:10:17 +03007235static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007236{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007237 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007238 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007239
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007240 msr_info.index = ecx;
7241 msr_info.host_initiated = false;
7242 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007243 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007244 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007245 return 1;
7246 }
7247
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007248 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007249
Avi Kivity6aa8b732006-12-10 02:21:36 -08007250 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007251 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7252 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007253 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007254}
7255
Avi Kivity851ba692009-08-24 11:10:17 +03007256static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007257{
Will Auld8fe8ab42012-11-29 12:42:12 -08007258 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007259 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7260 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7261 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007262
Will Auld8fe8ab42012-11-29 12:42:12 -08007263 msr.data = data;
7264 msr.index = ecx;
7265 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007266 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007267 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007268 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007269 return 1;
7270 }
7271
Avi Kivity59200272010-01-25 19:47:02 +02007272 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007273 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007274}
7275
Avi Kivity851ba692009-08-24 11:10:17 +03007276static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007277{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007278 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007279 return 1;
7280}
7281
Avi Kivity851ba692009-08-24 11:10:17 +03007282static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007283{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007284 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7285 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007286
Avi Kivity3842d132010-07-27 12:30:24 +03007287 kvm_make_request(KVM_REQ_EVENT, vcpu);
7288
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007289 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007290 return 1;
7291}
7292
Avi Kivity851ba692009-08-24 11:10:17 +03007293static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007294{
Avi Kivityd3bef152007-06-05 15:53:05 +03007295 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007296}
7297
Avi Kivity851ba692009-08-24 11:10:17 +03007298static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007299{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007300 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007301}
7302
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007303static int handle_invd(struct kvm_vcpu *vcpu)
7304{
Andre Przywara51d8b662010-12-21 11:12:02 +01007305 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007306}
7307
Avi Kivity851ba692009-08-24 11:10:17 +03007308static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007309{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007310 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007311
7312 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007313 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007314}
7315
Avi Kivityfee84b02011-11-10 14:57:25 +02007316static int handle_rdpmc(struct kvm_vcpu *vcpu)
7317{
7318 int err;
7319
7320 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007321 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007322}
7323
Avi Kivity851ba692009-08-24 11:10:17 +03007324static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007325{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007326 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007327}
7328
Dexuan Cui2acf9232010-06-10 11:27:12 +08007329static int handle_xsetbv(struct kvm_vcpu *vcpu)
7330{
7331 u64 new_bv = kvm_read_edx_eax(vcpu);
7332 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7333
7334 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007335 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007336 return 1;
7337}
7338
Wanpeng Lif53cd632014-12-02 19:14:58 +08007339static int handle_xsaves(struct kvm_vcpu *vcpu)
7340{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007341 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007342 WARN(1, "this should never happen\n");
7343 return 1;
7344}
7345
7346static int handle_xrstors(struct kvm_vcpu *vcpu)
7347{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007348 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007349 WARN(1, "this should never happen\n");
7350 return 1;
7351}
7352
Avi Kivity851ba692009-08-24 11:10:17 +03007353static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007354{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007355 if (likely(fasteoi)) {
7356 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7357 int access_type, offset;
7358
7359 access_type = exit_qualification & APIC_ACCESS_TYPE;
7360 offset = exit_qualification & APIC_ACCESS_OFFSET;
7361 /*
7362 * Sane guest uses MOV to write EOI, with written value
7363 * not cared. So make a short-circuit here by avoiding
7364 * heavy instruction emulation.
7365 */
7366 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7367 (offset == APIC_EOI)) {
7368 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007369 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007370 }
7371 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007372 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007373}
7374
Yang Zhangc7c9c562013-01-25 10:18:51 +08007375static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7376{
7377 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7378 int vector = exit_qualification & 0xff;
7379
7380 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7381 kvm_apic_set_eoi_accelerated(vcpu, vector);
7382 return 1;
7383}
7384
Yang Zhang83d4c282013-01-25 10:18:49 +08007385static int handle_apic_write(struct kvm_vcpu *vcpu)
7386{
7387 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7388 u32 offset = exit_qualification & 0xfff;
7389
7390 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7391 kvm_apic_write_nodecode(vcpu, offset);
7392 return 1;
7393}
7394
Avi Kivity851ba692009-08-24 11:10:17 +03007395static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007396{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007397 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007398 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007399 bool has_error_code = false;
7400 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007401 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007402 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007403
7404 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007405 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007406 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007407
7408 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7409
7410 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007411 if (reason == TASK_SWITCH_GATE && idt_v) {
7412 switch (type) {
7413 case INTR_TYPE_NMI_INTR:
7414 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007415 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007416 break;
7417 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007418 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007419 kvm_clear_interrupt_queue(vcpu);
7420 break;
7421 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007422 if (vmx->idt_vectoring_info &
7423 VECTORING_INFO_DELIVER_CODE_MASK) {
7424 has_error_code = true;
7425 error_code =
7426 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7427 }
7428 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007429 case INTR_TYPE_SOFT_EXCEPTION:
7430 kvm_clear_exception_queue(vcpu);
7431 break;
7432 default:
7433 break;
7434 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007435 }
Izik Eidus37817f22008-03-24 23:14:53 +02007436 tss_selector = exit_qualification;
7437
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007438 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7439 type != INTR_TYPE_EXT_INTR &&
7440 type != INTR_TYPE_NMI_INTR))
7441 skip_emulated_instruction(vcpu);
7442
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007443 if (kvm_task_switch(vcpu, tss_selector,
7444 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7445 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007446 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7447 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7448 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007449 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007450 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007451
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007452 /*
7453 * TODO: What about debug traps on tss switch?
7454 * Are we supposed to inject them and update dr6?
7455 */
7456
7457 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007458}
7459
Avi Kivity851ba692009-08-24 11:10:17 +03007460static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007461{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007462 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007463 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007464 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007465
Sheng Yangf9c617f2009-03-25 10:08:52 +08007466 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007467
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007468 /*
7469 * EPT violation happened while executing iret from NMI,
7470 * "blocked by NMI" bit has to be set before next VM entry.
7471 * There are errata that may cause this bit to not be set:
7472 * AAK134, BY25.
7473 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007474 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007475 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007476 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007477 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7478
Sheng Yang14394422008-04-28 12:24:45 +08007479 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007480 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007481
Junaid Shahid27959a42016-12-06 16:46:10 -08007482 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007483 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007484 ? PFERR_USER_MASK : 0;
7485 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007486 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007487 ? PFERR_WRITE_MASK : 0;
7488 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007489 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007490 ? PFERR_FETCH_MASK : 0;
7491 /* ept page table entry is present? */
7492 error_code |= (exit_qualification &
7493 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7494 EPT_VIOLATION_EXECUTABLE))
7495 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007496
Paolo Bonzinieebed242016-11-28 14:39:58 +01007497 error_code |= (exit_qualification & 0x100) != 0 ?
7498 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007499
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007500 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007501 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007502}
7503
Avi Kivity851ba692009-08-24 11:10:17 +03007504static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007505{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007506 gpa_t gpa;
7507
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007508 /*
7509 * A nested guest cannot optimize MMIO vmexits, because we have an
7510 * nGPA here instead of the required GPA.
7511 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007512 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007513 if (!is_guest_mode(vcpu) &&
7514 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007515 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007516 /*
7517 * Doing kvm_skip_emulated_instruction() depends on undefined
7518 * behavior: Intel's manual doesn't mandate
7519 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7520 * occurs and while on real hardware it was observed to be set,
7521 * other hypervisors (namely Hyper-V) don't set it, we end up
7522 * advancing IP with some random value. Disable fast mmio when
7523 * running nested and keep it for real hardware in hope that
7524 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7525 */
7526 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7527 return kvm_skip_emulated_instruction(vcpu);
7528 else
7529 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7530 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007531 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007532
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007533 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007534}
7535
Avi Kivity851ba692009-08-24 11:10:17 +03007536static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007537{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007538 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007539 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7540 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007541 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007542 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007543
7544 return 1;
7545}
7546
Mohammed Gamal80ced182009-09-01 12:48:18 +02007547static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007548{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007549 struct vcpu_vmx *vmx = to_vmx(vcpu);
7550 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007551 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007552 u32 cpu_exec_ctrl;
7553 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007554 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007555
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007556 /*
7557 * We should never reach the point where we are emulating L2
7558 * due to invalid guest state as that means we incorrectly
7559 * allowed a nested VMEntry with an invalid vmcs12.
7560 */
7561 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7562
Avi Kivity49e9d552010-09-19 14:34:08 +02007563 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7564 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007565
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007566 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007567 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007568 return handle_interrupt_window(&vmx->vcpu);
7569
Radim Krčmář72875d82017-04-26 22:32:19 +02007570 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007571 return 1;
7572
Liran Alon9b8ae632017-11-05 16:56:34 +02007573 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007574
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007575 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007576 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007577 ret = 0;
7578 goto out;
7579 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007580
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007581 if (err != EMULATE_DONE)
7582 goto emulation_error;
7583
7584 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7585 vcpu->arch.exception.pending)
7586 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007587
Gleb Natapov8d76c492013-05-08 18:38:44 +03007588 if (vcpu->arch.halt_request) {
7589 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007590 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007591 goto out;
7592 }
7593
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007594 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007595 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007596 if (need_resched())
7597 schedule();
7598 }
7599
Mohammed Gamal80ced182009-09-01 12:48:18 +02007600out:
7601 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007602
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007603emulation_error:
7604 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7605 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7606 vcpu->run->internal.ndata = 0;
7607 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007608}
7609
7610static void grow_ple_window(struct kvm_vcpu *vcpu)
7611{
7612 struct vcpu_vmx *vmx = to_vmx(vcpu);
7613 int old = vmx->ple_window;
7614
Babu Mogerc8e88712018-03-16 16:37:24 -04007615 vmx->ple_window = __grow_ple_window(old, ple_window,
7616 ple_window_grow,
7617 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007618
7619 if (vmx->ple_window != old)
7620 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007621
7622 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007623}
7624
7625static void shrink_ple_window(struct kvm_vcpu *vcpu)
7626{
7627 struct vcpu_vmx *vmx = to_vmx(vcpu);
7628 int old = vmx->ple_window;
7629
Babu Mogerc8e88712018-03-16 16:37:24 -04007630 vmx->ple_window = __shrink_ple_window(old, ple_window,
7631 ple_window_shrink,
7632 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007633
7634 if (vmx->ple_window != old)
7635 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007636
7637 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007638}
7639
7640/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007641 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7642 */
7643static void wakeup_handler(void)
7644{
7645 struct kvm_vcpu *vcpu;
7646 int cpu = smp_processor_id();
7647
7648 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7649 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7650 blocked_vcpu_list) {
7651 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7652
7653 if (pi_test_on(pi_desc) == 1)
7654 kvm_vcpu_kick(vcpu);
7655 }
7656 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7657}
7658
Peng Haoe01bca22018-04-07 05:47:32 +08007659static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007660{
7661 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7662 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7663 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7664 0ull, VMX_EPT_EXECUTABLE_MASK,
7665 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007666 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007667
7668 ept_set_mmio_spte_mask();
7669 kvm_enable_tdp();
7670}
7671
Tiejun Chenf2c76482014-10-28 10:14:47 +08007672static __init int hardware_setup(void)
7673{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007674 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007675
7676 rdmsrl_safe(MSR_EFER, &host_efer);
7677
7678 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7679 kvm_define_shared_msr(i, vmx_msr_index[i]);
7680
Radim Krčmář23611332016-09-29 22:41:33 +02007681 for (i = 0; i < VMX_BITMAP_NR; i++) {
7682 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7683 if (!vmx_bitmap[i])
7684 goto out;
7685 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007686
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007687 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7688 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7689
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007690 if (setup_vmcs_config(&vmcs_config) < 0) {
7691 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007692 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007693 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007694
7695 if (boot_cpu_has(X86_FEATURE_NX))
7696 kvm_enable_efer_bits(EFER_NX);
7697
Wanpeng Li08d839c2017-03-23 05:30:08 -07007698 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7699 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007700 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007701
Tiejun Chenf2c76482014-10-28 10:14:47 +08007702 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007703 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007704 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007705 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007706 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007707
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007708 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007709 enable_ept_ad_bits = 0;
7710
Wanpeng Li8ad81822017-10-09 15:51:53 -07007711 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007712 enable_unrestricted_guest = 0;
7713
Paolo Bonziniad15a292015-01-30 16:18:49 +01007714 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007715 flexpriority_enabled = 0;
7716
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007717 if (!cpu_has_virtual_nmis())
7718 enable_vnmi = 0;
7719
Paolo Bonziniad15a292015-01-30 16:18:49 +01007720 /*
7721 * set_apic_access_page_addr() is used to reload apic access
7722 * page upon invalidation. No need to do anything if not
7723 * using the APIC_ACCESS_ADDR VMCS field.
7724 */
7725 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007726 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007727
7728 if (!cpu_has_vmx_tpr_shadow())
7729 kvm_x86_ops->update_cr8_intercept = NULL;
7730
7731 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7732 kvm_disable_largepages();
7733
Wanpeng Li0f107682017-09-28 18:06:24 -07007734 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007735 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007736 ple_window = 0;
7737 ple_window_grow = 0;
7738 ple_window_max = 0;
7739 ple_window_shrink = 0;
7740 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007741
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007742 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007743 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007744 kvm_x86_ops->sync_pir_to_irr = NULL;
7745 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007746
Haozhong Zhang64903d62015-10-20 15:39:09 +08007747 if (cpu_has_vmx_tsc_scaling()) {
7748 kvm_has_tsc_control = true;
7749 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7750 kvm_tsc_scaling_ratio_frac_bits = 48;
7751 }
7752
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007753 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7754
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007755 if (enable_ept)
7756 vmx_enable_tdp();
7757 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007758 kvm_disable_tdp();
7759
Kai Huang843e4332015-01-28 10:54:28 +08007760 /*
7761 * Only enable PML when hardware supports PML feature, and both EPT
7762 * and EPT A/D bit features are enabled -- PML depends on them to work.
7763 */
7764 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7765 enable_pml = 0;
7766
7767 if (!enable_pml) {
7768 kvm_x86_ops->slot_enable_log_dirty = NULL;
7769 kvm_x86_ops->slot_disable_log_dirty = NULL;
7770 kvm_x86_ops->flush_log_dirty = NULL;
7771 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7772 }
7773
Yunhong Jiang64672c92016-06-13 14:19:59 -07007774 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7775 u64 vmx_msr;
7776
7777 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7778 cpu_preemption_timer_multi =
7779 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7780 } else {
7781 kvm_x86_ops->set_hv_timer = NULL;
7782 kvm_x86_ops->cancel_hv_timer = NULL;
7783 }
7784
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007785 if (!cpu_has_vmx_shadow_vmcs())
7786 enable_shadow_vmcs = 0;
7787 if (enable_shadow_vmcs)
7788 init_vmcs_shadow_fields();
7789
Feng Wubf9f6ac2015-09-18 22:29:55 +08007790 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007791 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007792
Ashok Rajc45dcc72016-06-22 14:59:56 +08007793 kvm_mce_cap_supported |= MCG_LMCE_P;
7794
Tiejun Chenf2c76482014-10-28 10:14:47 +08007795 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007796
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007797out:
Radim Krčmář23611332016-09-29 22:41:33 +02007798 for (i = 0; i < VMX_BITMAP_NR; i++)
7799 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007800
7801 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007802}
7803
7804static __exit void hardware_unsetup(void)
7805{
Radim Krčmář23611332016-09-29 22:41:33 +02007806 int i;
7807
7808 for (i = 0; i < VMX_BITMAP_NR; i++)
7809 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007810
Tiejun Chenf2c76482014-10-28 10:14:47 +08007811 free_kvm_area();
7812}
7813
Avi Kivity6aa8b732006-12-10 02:21:36 -08007814/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007815 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7816 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7817 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007818static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007819{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007820 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007821 grow_ple_window(vcpu);
7822
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007823 /*
7824 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7825 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7826 * never set PAUSE_EXITING and just set PLE if supported,
7827 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7828 */
7829 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007830 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007831}
7832
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007833static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007834{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007835 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007836}
7837
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007838static int handle_mwait(struct kvm_vcpu *vcpu)
7839{
7840 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7841 return handle_nop(vcpu);
7842}
7843
Jim Mattson45ec3682017-08-23 16:32:04 -07007844static int handle_invalid_op(struct kvm_vcpu *vcpu)
7845{
7846 kvm_queue_exception(vcpu, UD_VECTOR);
7847 return 1;
7848}
7849
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007850static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7851{
7852 return 1;
7853}
7854
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007855static int handle_monitor(struct kvm_vcpu *vcpu)
7856{
7857 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7858 return handle_nop(vcpu);
7859}
7860
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007861/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007862 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7863 * set the success or error code of an emulated VMX instruction, as specified
7864 * by Vol 2B, VMX Instruction Reference, "Conventions".
7865 */
7866static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7867{
7868 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7869 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7870 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7871}
7872
7873static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7874{
7875 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7876 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7877 X86_EFLAGS_SF | X86_EFLAGS_OF))
7878 | X86_EFLAGS_CF);
7879}
7880
Abel Gordon145c28d2013-04-18 14:36:55 +03007881static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007882 u32 vm_instruction_error)
7883{
7884 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7885 /*
7886 * failValid writes the error number to the current VMCS, which
7887 * can't be done there isn't a current VMCS.
7888 */
7889 nested_vmx_failInvalid(vcpu);
7890 return;
7891 }
7892 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7893 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7894 X86_EFLAGS_SF | X86_EFLAGS_OF))
7895 | X86_EFLAGS_ZF);
7896 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7897 /*
7898 * We don't need to force a shadow sync because
7899 * VM_INSTRUCTION_ERROR is not shadowed
7900 */
7901}
Abel Gordon145c28d2013-04-18 14:36:55 +03007902
Wincy Vanff651cb2014-12-11 08:52:58 +03007903static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7904{
7905 /* TODO: not to reset guest simply here. */
7906 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007907 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007908}
7909
Jan Kiszkaf4124502014-03-07 20:03:13 +01007910static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7911{
7912 struct vcpu_vmx *vmx =
7913 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7914
7915 vmx->nested.preemption_timer_expired = true;
7916 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7917 kvm_vcpu_kick(&vmx->vcpu);
7918
7919 return HRTIMER_NORESTART;
7920}
7921
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007922/*
Bandan Das19677e32014-05-06 02:19:15 -04007923 * Decode the memory-address operand of a vmx instruction, as recorded on an
7924 * exit caused by such an instruction (run by a guest hypervisor).
7925 * On success, returns 0. When the operand is invalid, returns 1 and throws
7926 * #UD or #GP.
7927 */
7928static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7929 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007930 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007931{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007932 gva_t off;
7933 bool exn;
7934 struct kvm_segment s;
7935
Bandan Das19677e32014-05-06 02:19:15 -04007936 /*
7937 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7938 * Execution", on an exit, vmx_instruction_info holds most of the
7939 * addressing components of the operand. Only the displacement part
7940 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7941 * For how an actual address is calculated from all these components,
7942 * refer to Vol. 1, "Operand Addressing".
7943 */
7944 int scaling = vmx_instruction_info & 3;
7945 int addr_size = (vmx_instruction_info >> 7) & 7;
7946 bool is_reg = vmx_instruction_info & (1u << 10);
7947 int seg_reg = (vmx_instruction_info >> 15) & 7;
7948 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7949 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7950 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7951 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7952
7953 if (is_reg) {
7954 kvm_queue_exception(vcpu, UD_VECTOR);
7955 return 1;
7956 }
7957
7958 /* Addr = segment_base + offset */
7959 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007960 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007961 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007962 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007963 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007964 off += kvm_register_read(vcpu, index_reg)<<scaling;
7965 vmx_get_segment(vcpu, &s, seg_reg);
7966 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007967
7968 if (addr_size == 1) /* 32 bit */
7969 *ret &= 0xffffffff;
7970
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007971 /* Checks for #GP/#SS exceptions. */
7972 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007973 if (is_long_mode(vcpu)) {
7974 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7975 * non-canonical form. This is the only check on the memory
7976 * destination for long mode!
7977 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007978 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007979 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007980 /* Protected mode: apply checks for segment validity in the
7981 * following order:
7982 * - segment type check (#GP(0) may be thrown)
7983 * - usability check (#GP(0)/#SS(0))
7984 * - limit check (#GP(0)/#SS(0))
7985 */
7986 if (wr)
7987 /* #GP(0) if the destination operand is located in a
7988 * read-only data segment or any code segment.
7989 */
7990 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7991 else
7992 /* #GP(0) if the source operand is located in an
7993 * execute-only code segment
7994 */
7995 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007996 if (exn) {
7997 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7998 return 1;
7999 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008000 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8001 */
8002 exn = (s.unusable != 0);
8003 /* Protected mode: #GP(0)/#SS(0) if the memory
8004 * operand is outside the segment limit.
8005 */
8006 exn = exn || (off + sizeof(u64) > s.limit);
8007 }
8008 if (exn) {
8009 kvm_queue_exception_e(vcpu,
8010 seg_reg == VCPU_SREG_SS ?
8011 SS_VECTOR : GP_VECTOR,
8012 0);
8013 return 1;
8014 }
8015
Bandan Das19677e32014-05-06 02:19:15 -04008016 return 0;
8017}
8018
Radim Krčmářcbf71272017-05-19 15:48:51 +02008019static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008020{
8021 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008022 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008023
8024 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008025 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008026 return 1;
8027
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008028 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008029 kvm_inject_page_fault(vcpu, &e);
8030 return 1;
8031 }
8032
Bandan Das3573e222014-05-06 02:19:16 -04008033 return 0;
8034}
8035
Jim Mattsone29acc52016-11-30 12:03:43 -08008036static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8037{
8038 struct vcpu_vmx *vmx = to_vmx(vcpu);
8039 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008040 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008041
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008042 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8043 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008044 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008045
8046 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8047 if (!vmx->nested.cached_vmcs12)
8048 goto out_cached_vmcs12;
8049
8050 if (enable_shadow_vmcs) {
8051 shadow_vmcs = alloc_vmcs();
8052 if (!shadow_vmcs)
8053 goto out_shadow_vmcs;
8054 /* mark vmcs as shadow */
8055 shadow_vmcs->revision_id |= (1u << 31);
8056 /* init shadow vmcs */
8057 vmcs_clear(shadow_vmcs);
8058 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
8059 }
8060
Jim Mattsone29acc52016-11-30 12:03:43 -08008061 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8062 HRTIMER_MODE_REL_PINNED);
8063 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8064
8065 vmx->nested.vmxon = true;
8066 return 0;
8067
8068out_shadow_vmcs:
8069 kfree(vmx->nested.cached_vmcs12);
8070
8071out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008072 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008073
Jim Mattsonde3a0022017-11-27 17:22:25 -06008074out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008075 return -ENOMEM;
8076}
8077
Bandan Das3573e222014-05-06 02:19:16 -04008078/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008079 * Emulate the VMXON instruction.
8080 * Currently, we just remember that VMX is active, and do not save or even
8081 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8082 * do not currently need to store anything in that guest-allocated memory
8083 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8084 * argument is different from the VMXON pointer (which the spec says they do).
8085 */
8086static int handle_vmon(struct kvm_vcpu *vcpu)
8087{
Jim Mattsone29acc52016-11-30 12:03:43 -08008088 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008089 gpa_t vmptr;
8090 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008091 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008092 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8093 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008094
Jim Mattson70f3aac2017-04-26 08:53:46 -07008095 /*
8096 * The Intel VMX Instruction Reference lists a bunch of bits that are
8097 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8098 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8099 * Otherwise, we should fail with #UD. But most faulting conditions
8100 * have already been checked by hardware, prior to the VM-exit for
8101 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8102 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008103 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008104 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008105 kvm_queue_exception(vcpu, UD_VECTOR);
8106 return 1;
8107 }
8108
Felix Wilhelm727ba742018-06-11 09:43:44 +02008109 /* CPL=0 must be checked manually. */
8110 if (vmx_get_cpl(vcpu)) {
8111 kvm_queue_exception(vcpu, UD_VECTOR);
8112 return 1;
8113 }
8114
Abel Gordon145c28d2013-04-18 14:36:55 +03008115 if (vmx->nested.vmxon) {
8116 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008117 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008118 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008119
Haozhong Zhang3b840802016-06-22 14:59:54 +08008120 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008121 != VMXON_NEEDED_FEATURES) {
8122 kvm_inject_gp(vcpu, 0);
8123 return 1;
8124 }
8125
Radim Krčmářcbf71272017-05-19 15:48:51 +02008126 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008127 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008128
8129 /*
8130 * SDM 3: 24.11.5
8131 * The first 4 bytes of VMXON region contain the supported
8132 * VMCS revision identifier
8133 *
8134 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8135 * which replaces physical address width with 32
8136 */
8137 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8138 nested_vmx_failInvalid(vcpu);
8139 return kvm_skip_emulated_instruction(vcpu);
8140 }
8141
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008142 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8143 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008144 nested_vmx_failInvalid(vcpu);
8145 return kvm_skip_emulated_instruction(vcpu);
8146 }
8147 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8148 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008149 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008150 nested_vmx_failInvalid(vcpu);
8151 return kvm_skip_emulated_instruction(vcpu);
8152 }
8153 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008154 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008155
8156 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008157 ret = enter_vmx_operation(vcpu);
8158 if (ret)
8159 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008160
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008161 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008162 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008163}
8164
8165/*
8166 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8167 * for running VMX instructions (except VMXON, whose prerequisites are
8168 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008169 * Note that many of these exceptions have priority over VM exits, so they
8170 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008171 */
8172static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8173{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008174 if (vmx_get_cpl(vcpu)) {
8175 kvm_queue_exception(vcpu, UD_VECTOR);
8176 return 0;
8177 }
8178
Jim Mattson70f3aac2017-04-26 08:53:46 -07008179 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008180 kvm_queue_exception(vcpu, UD_VECTOR);
8181 return 0;
8182 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008183 return 1;
8184}
8185
David Matlack8ca44e82017-08-01 14:00:39 -07008186static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8187{
8188 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8189 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8190}
8191
Abel Gordone7953d72013-04-18 14:37:55 +03008192static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8193{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008194 if (vmx->nested.current_vmptr == -1ull)
8195 return;
8196
Abel Gordon012f83c2013-04-18 14:39:25 +03008197 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008198 /* copy to memory all shadowed fields in case
8199 they were modified */
8200 copy_shadow_to_vmcs12(vmx);
8201 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008202 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008203 }
Wincy Van705699a2015-02-03 23:58:17 +08008204 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008205
8206 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008207 kvm_vcpu_write_guest_page(&vmx->vcpu,
8208 vmx->nested.current_vmptr >> PAGE_SHIFT,
8209 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008210
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008211 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008212}
8213
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008214/*
8215 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8216 * just stops using VMX.
8217 */
8218static void free_nested(struct vcpu_vmx *vmx)
8219{
Wanpeng Lib7455822017-11-22 14:04:00 -08008220 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008221 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008222
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008223 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008224 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008225 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008226 vmx->nested.posted_intr_nv = -1;
8227 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008228 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008229 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008230 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8231 free_vmcs(vmx->vmcs01.shadow_vmcs);
8232 vmx->vmcs01.shadow_vmcs = NULL;
8233 }
David Matlack4f2777b2016-07-13 17:16:37 -07008234 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008235 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008236 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008237 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008238 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008239 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008240 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008241 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008242 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008243 }
Wincy Van705699a2015-02-03 23:58:17 +08008244 if (vmx->nested.pi_desc_page) {
8245 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008246 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008247 vmx->nested.pi_desc_page = NULL;
8248 vmx->nested.pi_desc = NULL;
8249 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008250
Jim Mattsonde3a0022017-11-27 17:22:25 -06008251 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008252}
8253
8254/* Emulate the VMXOFF instruction */
8255static int handle_vmoff(struct kvm_vcpu *vcpu)
8256{
8257 if (!nested_vmx_check_permission(vcpu))
8258 return 1;
8259 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008260 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008261 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008262}
8263
Nadav Har'El27d6c862011-05-25 23:06:59 +03008264/* Emulate the VMCLEAR instruction */
8265static int handle_vmclear(struct kvm_vcpu *vcpu)
8266{
8267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008268 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008269 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008270
8271 if (!nested_vmx_check_permission(vcpu))
8272 return 1;
8273
Radim Krčmářcbf71272017-05-19 15:48:51 +02008274 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008275 return 1;
8276
Radim Krčmářcbf71272017-05-19 15:48:51 +02008277 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8278 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8279 return kvm_skip_emulated_instruction(vcpu);
8280 }
8281
8282 if (vmptr == vmx->nested.vmxon_ptr) {
8283 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8284 return kvm_skip_emulated_instruction(vcpu);
8285 }
8286
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008287 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008288 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008289
Jim Mattson587d7e722017-03-02 12:41:48 -08008290 kvm_vcpu_write_guest(vcpu,
8291 vmptr + offsetof(struct vmcs12, launch_state),
8292 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008293
Nadav Har'El27d6c862011-05-25 23:06:59 +03008294 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008295 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008296}
8297
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008298static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8299
8300/* Emulate the VMLAUNCH instruction */
8301static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8302{
8303 return nested_vmx_run(vcpu, true);
8304}
8305
8306/* Emulate the VMRESUME instruction */
8307static int handle_vmresume(struct kvm_vcpu *vcpu)
8308{
8309
8310 return nested_vmx_run(vcpu, false);
8311}
8312
Nadav Har'El49f705c2011-05-25 23:08:30 +03008313/*
8314 * Read a vmcs12 field. Since these can have varying lengths and we return
8315 * one type, we chose the biggest type (u64) and zero-extend the return value
8316 * to that size. Note that the caller, handle_vmread, might need to use only
8317 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8318 * 64-bit fields are to be returned).
8319 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008320static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8321 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008322{
8323 short offset = vmcs_field_to_offset(field);
8324 char *p;
8325
8326 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008327 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008328
8329 p = ((char *)(get_vmcs12(vcpu))) + offset;
8330
Jim Mattsond37f4262017-12-22 12:12:16 -08008331 switch (vmcs_field_width(field)) {
8332 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008333 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008334 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008335 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008336 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008337 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008338 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008339 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008340 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008341 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008342 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008343 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008344 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008345 WARN_ON(1);
8346 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008347 }
8348}
8349
Abel Gordon20b97fe2013-04-18 14:36:25 +03008350
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008351static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8352 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008353 short offset = vmcs_field_to_offset(field);
8354 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8355 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008356 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008357
Jim Mattsond37f4262017-12-22 12:12:16 -08008358 switch (vmcs_field_width(field)) {
8359 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008360 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008361 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008362 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008363 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008364 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008365 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008366 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008367 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008368 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008369 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008370 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008371 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008372 WARN_ON(1);
8373 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008374 }
8375
8376}
8377
Jim Mattsonf4160e42018-05-29 09:11:33 -07008378/*
8379 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8380 * they have been modified by the L1 guest. Note that the "read-only"
8381 * VM-exit information fields are actually writable if the vCPU is
8382 * configured to support "VMWRITE to any supported field in the VMCS."
8383 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008384static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8385{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008386 const u16 *fields[] = {
8387 shadow_read_write_fields,
8388 shadow_read_only_fields
8389 };
8390 const int max_fields[] = {
8391 max_shadow_read_write_fields,
8392 max_shadow_read_only_fields
8393 };
8394 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008395 unsigned long field;
8396 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008397 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008398
Jan Kiszka282da872014-10-08 18:05:39 +02008399 preempt_disable();
8400
Abel Gordon16f5b902013-04-18 14:38:25 +03008401 vmcs_load(shadow_vmcs);
8402
Jim Mattsonf4160e42018-05-29 09:11:33 -07008403 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8404 for (i = 0; i < max_fields[q]; i++) {
8405 field = fields[q][i];
8406 field_value = __vmcs_readl(field);
8407 vmcs12_write_any(&vmx->vcpu, field, field_value);
8408 }
8409 /*
8410 * Skip the VM-exit information fields if they are read-only.
8411 */
8412 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8413 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008414 }
8415
8416 vmcs_clear(shadow_vmcs);
8417 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008418
8419 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008420}
8421
Abel Gordonc3114422013-04-18 14:38:55 +03008422static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8423{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008424 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008425 shadow_read_write_fields,
8426 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008427 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008428 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008429 max_shadow_read_write_fields,
8430 max_shadow_read_only_fields
8431 };
8432 int i, q;
8433 unsigned long field;
8434 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008435 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008436
8437 vmcs_load(shadow_vmcs);
8438
Mathias Krausec2bae892013-06-26 20:36:21 +02008439 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008440 for (i = 0; i < max_fields[q]; i++) {
8441 field = fields[q][i];
8442 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008443 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008444 }
8445 }
8446
8447 vmcs_clear(shadow_vmcs);
8448 vmcs_load(vmx->loaded_vmcs->vmcs);
8449}
8450
Nadav Har'El49f705c2011-05-25 23:08:30 +03008451/*
8452 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8453 * used before) all generate the same failure when it is missing.
8454 */
8455static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8456{
8457 struct vcpu_vmx *vmx = to_vmx(vcpu);
8458 if (vmx->nested.current_vmptr == -1ull) {
8459 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008460 return 0;
8461 }
8462 return 1;
8463}
8464
8465static int handle_vmread(struct kvm_vcpu *vcpu)
8466{
8467 unsigned long field;
8468 u64 field_value;
8469 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8470 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8471 gva_t gva = 0;
8472
Kyle Hueyeb277562016-11-29 12:40:39 -08008473 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008474 return 1;
8475
Kyle Huey6affcbe2016-11-29 12:40:40 -08008476 if (!nested_vmx_check_vmcs12(vcpu))
8477 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008478
Nadav Har'El49f705c2011-05-25 23:08:30 +03008479 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008480 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008481 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008482 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008483 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008484 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008485 }
8486 /*
8487 * Now copy part of this value to register or memory, as requested.
8488 * Note that the number of bits actually copied is 32 or 64 depending
8489 * on the guest's mode (32 or 64 bit), not on the given field's length.
8490 */
8491 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008492 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008493 field_value);
8494 } else {
8495 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008496 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008497 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008498 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008499 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8500 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008501 }
8502
8503 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008504 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008505}
8506
8507
8508static int handle_vmwrite(struct kvm_vcpu *vcpu)
8509{
8510 unsigned long field;
8511 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008512 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008513 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8514 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008515
Nadav Har'El49f705c2011-05-25 23:08:30 +03008516 /* The value to write might be 32 or 64 bits, depending on L1's long
8517 * mode, and eventually we need to write that into a field of several
8518 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008519 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008520 * bits into the vmcs12 field.
8521 */
8522 u64 field_value = 0;
8523 struct x86_exception e;
8524
Kyle Hueyeb277562016-11-29 12:40:39 -08008525 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008526 return 1;
8527
Kyle Huey6affcbe2016-11-29 12:40:40 -08008528 if (!nested_vmx_check_vmcs12(vcpu))
8529 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008530
Nadav Har'El49f705c2011-05-25 23:08:30 +03008531 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008532 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008533 (((vmx_instruction_info) >> 3) & 0xf));
8534 else {
8535 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008536 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008537 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008538 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8539 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008540 kvm_inject_page_fault(vcpu, &e);
8541 return 1;
8542 }
8543 }
8544
8545
Nadav Amit27e6fb52014-06-18 17:19:26 +03008546 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008547 /*
8548 * If the vCPU supports "VMWRITE to any supported field in the
8549 * VMCS," then the "read-only" fields are actually read/write.
8550 */
8551 if (vmcs_field_readonly(field) &&
8552 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553 nested_vmx_failValid(vcpu,
8554 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008555 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556 }
8557
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008558 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008560 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008561 }
8562
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008563 switch (field) {
8564#define SHADOW_FIELD_RW(x) case x:
8565#include "vmx_shadow_fields.h"
8566 /*
8567 * The fields that can be updated by L1 without a vmexit are
8568 * always updated in the vmcs02, the others go down the slow
8569 * path of prepare_vmcs02.
8570 */
8571 break;
8572 default:
8573 vmx->nested.dirty_vmcs12 = true;
8574 break;
8575 }
8576
Nadav Har'El49f705c2011-05-25 23:08:30 +03008577 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008578 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008579}
8580
Jim Mattsona8bc2842016-11-30 12:03:44 -08008581static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8582{
8583 vmx->nested.current_vmptr = vmptr;
8584 if (enable_shadow_vmcs) {
8585 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8586 SECONDARY_EXEC_SHADOW_VMCS);
8587 vmcs_write64(VMCS_LINK_POINTER,
8588 __pa(vmx->vmcs01.shadow_vmcs));
8589 vmx->nested.sync_shadow_vmcs = true;
8590 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008591 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008592}
8593
Nadav Har'El63846662011-05-25 23:07:29 +03008594/* Emulate the VMPTRLD instruction */
8595static int handle_vmptrld(struct kvm_vcpu *vcpu)
8596{
8597 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008598 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008599
8600 if (!nested_vmx_check_permission(vcpu))
8601 return 1;
8602
Radim Krčmářcbf71272017-05-19 15:48:51 +02008603 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008604 return 1;
8605
Radim Krčmářcbf71272017-05-19 15:48:51 +02008606 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8607 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8608 return kvm_skip_emulated_instruction(vcpu);
8609 }
8610
8611 if (vmptr == vmx->nested.vmxon_ptr) {
8612 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8613 return kvm_skip_emulated_instruction(vcpu);
8614 }
8615
Nadav Har'El63846662011-05-25 23:07:29 +03008616 if (vmx->nested.current_vmptr != vmptr) {
8617 struct vmcs12 *new_vmcs12;
8618 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008619 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8620 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008621 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008622 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008623 }
8624 new_vmcs12 = kmap(page);
8625 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8626 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008627 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008628 nested_vmx_failValid(vcpu,
8629 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008630 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008631 }
Nadav Har'El63846662011-05-25 23:07:29 +03008632
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008633 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008634 /*
8635 * Load VMCS12 from guest memory since it is not already
8636 * cached.
8637 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008638 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8639 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008640 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008641
Jim Mattsona8bc2842016-11-30 12:03:44 -08008642 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008643 }
8644
8645 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008646 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008647}
8648
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008649/* Emulate the VMPTRST instruction */
8650static int handle_vmptrst(struct kvm_vcpu *vcpu)
8651{
8652 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8653 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8654 gva_t vmcs_gva;
8655 struct x86_exception e;
8656
8657 if (!nested_vmx_check_permission(vcpu))
8658 return 1;
8659
8660 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008661 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008662 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008663 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008664 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8665 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8666 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008667 kvm_inject_page_fault(vcpu, &e);
8668 return 1;
8669 }
8670 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008671 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008672}
8673
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008674/* Emulate the INVEPT instruction */
8675static int handle_invept(struct kvm_vcpu *vcpu)
8676{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008677 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008678 u32 vmx_instruction_info, types;
8679 unsigned long type;
8680 gva_t gva;
8681 struct x86_exception e;
8682 struct {
8683 u64 eptp, gpa;
8684 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008685
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008686 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008687 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008688 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008689 kvm_queue_exception(vcpu, UD_VECTOR);
8690 return 1;
8691 }
8692
8693 if (!nested_vmx_check_permission(vcpu))
8694 return 1;
8695
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008696 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008697 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008698
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008699 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008700
Jim Mattson85c856b2016-10-26 08:38:38 -07008701 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008702 nested_vmx_failValid(vcpu,
8703 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008704 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008705 }
8706
8707 /* According to the Intel VMX instruction reference, the memory
8708 * operand is read even if it isn't needed (e.g., for type==global)
8709 */
8710 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008711 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008712 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008713 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008714 kvm_inject_page_fault(vcpu, &e);
8715 return 1;
8716 }
8717
8718 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008719 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008720 /*
8721 * TODO: track mappings and invalidate
8722 * single context requests appropriately
8723 */
8724 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008725 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008726 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008727 nested_vmx_succeed(vcpu);
8728 break;
8729 default:
8730 BUG_ON(1);
8731 break;
8732 }
8733
Kyle Huey6affcbe2016-11-29 12:40:40 -08008734 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008735}
8736
Petr Matouseka642fc32014-09-23 20:22:30 +02008737static int handle_invvpid(struct kvm_vcpu *vcpu)
8738{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008739 struct vcpu_vmx *vmx = to_vmx(vcpu);
8740 u32 vmx_instruction_info;
8741 unsigned long type, types;
8742 gva_t gva;
8743 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008744 struct {
8745 u64 vpid;
8746 u64 gla;
8747 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008748
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008749 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008750 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008751 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008752 kvm_queue_exception(vcpu, UD_VECTOR);
8753 return 1;
8754 }
8755
8756 if (!nested_vmx_check_permission(vcpu))
8757 return 1;
8758
8759 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8760 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8761
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008762 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008763 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008764
Jim Mattson85c856b2016-10-26 08:38:38 -07008765 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008766 nested_vmx_failValid(vcpu,
8767 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008768 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008769 }
8770
8771 /* according to the intel vmx instruction reference, the memory
8772 * operand is read even if it isn't needed (e.g., for type==global)
8773 */
8774 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8775 vmx_instruction_info, false, &gva))
8776 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008777 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008778 kvm_inject_page_fault(vcpu, &e);
8779 return 1;
8780 }
Jim Mattson40352602017-06-28 09:37:37 -07008781 if (operand.vpid >> 16) {
8782 nested_vmx_failValid(vcpu,
8783 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8784 return kvm_skip_emulated_instruction(vcpu);
8785 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008786
8787 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008788 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008789 if (!operand.vpid ||
8790 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008791 nested_vmx_failValid(vcpu,
8792 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8793 return kvm_skip_emulated_instruction(vcpu);
8794 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008795 if (cpu_has_vmx_invvpid_individual_addr() &&
8796 vmx->nested.vpid02) {
8797 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8798 vmx->nested.vpid02, operand.gla);
8799 } else
8800 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8801 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008802 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008803 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008804 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008805 nested_vmx_failValid(vcpu,
8806 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008807 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008808 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008809 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008810 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008811 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008812 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008813 break;
8814 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008815 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008816 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008817 }
8818
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008819 nested_vmx_succeed(vcpu);
8820
Kyle Huey6affcbe2016-11-29 12:40:40 -08008821 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008822}
8823
Kai Huang843e4332015-01-28 10:54:28 +08008824static int handle_pml_full(struct kvm_vcpu *vcpu)
8825{
8826 unsigned long exit_qualification;
8827
8828 trace_kvm_pml_full(vcpu->vcpu_id);
8829
8830 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8831
8832 /*
8833 * PML buffer FULL happened while executing iret from NMI,
8834 * "blocked by NMI" bit has to be set before next VM entry.
8835 */
8836 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008837 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008838 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8839 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8840 GUEST_INTR_STATE_NMI);
8841
8842 /*
8843 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8844 * here.., and there's no userspace involvement needed for PML.
8845 */
8846 return 1;
8847}
8848
Yunhong Jiang64672c92016-06-13 14:19:59 -07008849static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8850{
8851 kvm_lapic_expired_hv_timer(vcpu);
8852 return 1;
8853}
8854
Bandan Das41ab9372017-08-03 15:54:43 -04008855static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8856{
8857 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008858 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8859
8860 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008861 switch (address & VMX_EPTP_MT_MASK) {
8862 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008863 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008864 return false;
8865 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008866 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008867 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008868 return false;
8869 break;
8870 default:
8871 return false;
8872 }
8873
David Hildenbrandbb97a012017-08-10 23:15:28 +02008874 /* only 4 levels page-walk length are valid */
8875 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008876 return false;
8877
8878 /* Reserved bits should not be set */
8879 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8880 return false;
8881
8882 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008883 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008884 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008885 return false;
8886 }
8887
8888 return true;
8889}
8890
8891static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8892 struct vmcs12 *vmcs12)
8893{
8894 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8895 u64 address;
8896 bool accessed_dirty;
8897 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8898
8899 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8900 !nested_cpu_has_ept(vmcs12))
8901 return 1;
8902
8903 if (index >= VMFUNC_EPTP_ENTRIES)
8904 return 1;
8905
8906
8907 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8908 &address, index * 8, 8))
8909 return 1;
8910
David Hildenbrandbb97a012017-08-10 23:15:28 +02008911 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008912
8913 /*
8914 * If the (L2) guest does a vmfunc to the currently
8915 * active ept pointer, we don't have to do anything else
8916 */
8917 if (vmcs12->ept_pointer != address) {
8918 if (!valid_ept_address(vcpu, address))
8919 return 1;
8920
8921 kvm_mmu_unload(vcpu);
8922 mmu->ept_ad = accessed_dirty;
8923 mmu->base_role.ad_disabled = !accessed_dirty;
8924 vmcs12->ept_pointer = address;
8925 /*
8926 * TODO: Check what's the correct approach in case
8927 * mmu reload fails. Currently, we just let the next
8928 * reload potentially fail
8929 */
8930 kvm_mmu_reload(vcpu);
8931 }
8932
8933 return 0;
8934}
8935
Bandan Das2a499e42017-08-03 15:54:41 -04008936static int handle_vmfunc(struct kvm_vcpu *vcpu)
8937{
Bandan Das27c42a12017-08-03 15:54:42 -04008938 struct vcpu_vmx *vmx = to_vmx(vcpu);
8939 struct vmcs12 *vmcs12;
8940 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8941
8942 /*
8943 * VMFUNC is only supported for nested guests, but we always enable the
8944 * secondary control for simplicity; for non-nested mode, fake that we
8945 * didn't by injecting #UD.
8946 */
8947 if (!is_guest_mode(vcpu)) {
8948 kvm_queue_exception(vcpu, UD_VECTOR);
8949 return 1;
8950 }
8951
8952 vmcs12 = get_vmcs12(vcpu);
8953 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8954 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008955
8956 switch (function) {
8957 case 0:
8958 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8959 goto fail;
8960 break;
8961 default:
8962 goto fail;
8963 }
8964 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008965
8966fail:
8967 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8968 vmcs_read32(VM_EXIT_INTR_INFO),
8969 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008970 return 1;
8971}
8972
Nadav Har'El0140cae2011-05-25 23:06:28 +03008973/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008974 * The exit handlers return 1 if the exit was handled fully and guest execution
8975 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8976 * to be done to userspace and return 0.
8977 */
Mathias Krause772e0312012-08-30 01:30:19 +02008978static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008979 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8980 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008981 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008982 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008983 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008984 [EXIT_REASON_CR_ACCESS] = handle_cr,
8985 [EXIT_REASON_DR_ACCESS] = handle_dr,
8986 [EXIT_REASON_CPUID] = handle_cpuid,
8987 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8988 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8989 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8990 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008991 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008992 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008993 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008994 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008995 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008996 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008997 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008998 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008999 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009000 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009001 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009002 [EXIT_REASON_VMOFF] = handle_vmoff,
9003 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009004 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9005 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009006 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009007 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009008 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009009 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009010 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009011 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009012 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9013 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009014 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9015 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009016 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009017 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009018 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009019 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009020 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009021 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009022 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009023 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009024 [EXIT_REASON_XSAVES] = handle_xsaves,
9025 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009026 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04009027 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009028 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009029};
9030
9031static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009032 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009033
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009034static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9035 struct vmcs12 *vmcs12)
9036{
9037 unsigned long exit_qualification;
9038 gpa_t bitmap, last_bitmap;
9039 unsigned int port;
9040 int size;
9041 u8 b;
9042
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009043 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009044 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009045
9046 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9047
9048 port = exit_qualification >> 16;
9049 size = (exit_qualification & 7) + 1;
9050
9051 last_bitmap = (gpa_t)-1;
9052 b = -1;
9053
9054 while (size > 0) {
9055 if (port < 0x8000)
9056 bitmap = vmcs12->io_bitmap_a;
9057 else if (port < 0x10000)
9058 bitmap = vmcs12->io_bitmap_b;
9059 else
Joe Perches1d804d02015-03-30 16:46:09 -07009060 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009061 bitmap += (port & 0x7fff) / 8;
9062
9063 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009064 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009065 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009066 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009067 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009068
9069 port++;
9070 size--;
9071 last_bitmap = bitmap;
9072 }
9073
Joe Perches1d804d02015-03-30 16:46:09 -07009074 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009075}
9076
Nadav Har'El644d7112011-05-25 23:12:35 +03009077/*
9078 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9079 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9080 * disinterest in the current event (read or write a specific MSR) by using an
9081 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9082 */
9083static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9084 struct vmcs12 *vmcs12, u32 exit_reason)
9085{
9086 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9087 gpa_t bitmap;
9088
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009089 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009090 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009091
9092 /*
9093 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9094 * for the four combinations of read/write and low/high MSR numbers.
9095 * First we need to figure out which of the four to use:
9096 */
9097 bitmap = vmcs12->msr_bitmap;
9098 if (exit_reason == EXIT_REASON_MSR_WRITE)
9099 bitmap += 2048;
9100 if (msr_index >= 0xc0000000) {
9101 msr_index -= 0xc0000000;
9102 bitmap += 1024;
9103 }
9104
9105 /* Then read the msr_index'th bit from this bitmap: */
9106 if (msr_index < 1024*8) {
9107 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009108 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009110 return 1 & (b >> (msr_index & 7));
9111 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009112 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009113}
9114
9115/*
9116 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9117 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9118 * intercept (via guest_host_mask etc.) the current event.
9119 */
9120static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9121 struct vmcs12 *vmcs12)
9122{
9123 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9124 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009125 int reg;
9126 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009127
9128 switch ((exit_qualification >> 4) & 3) {
9129 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009130 reg = (exit_qualification >> 8) & 15;
9131 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009132 switch (cr) {
9133 case 0:
9134 if (vmcs12->cr0_guest_host_mask &
9135 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009136 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009137 break;
9138 case 3:
9139 if ((vmcs12->cr3_target_count >= 1 &&
9140 vmcs12->cr3_target_value0 == val) ||
9141 (vmcs12->cr3_target_count >= 2 &&
9142 vmcs12->cr3_target_value1 == val) ||
9143 (vmcs12->cr3_target_count >= 3 &&
9144 vmcs12->cr3_target_value2 == val) ||
9145 (vmcs12->cr3_target_count >= 4 &&
9146 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009147 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009148 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009149 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009150 break;
9151 case 4:
9152 if (vmcs12->cr4_guest_host_mask &
9153 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009154 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009155 break;
9156 case 8:
9157 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009158 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009159 break;
9160 }
9161 break;
9162 case 2: /* clts */
9163 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9164 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009165 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 break;
9167 case 1: /* mov from cr */
9168 switch (cr) {
9169 case 3:
9170 if (vmcs12->cpu_based_vm_exec_control &
9171 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009172 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009173 break;
9174 case 8:
9175 if (vmcs12->cpu_based_vm_exec_control &
9176 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009177 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009178 break;
9179 }
9180 break;
9181 case 3: /* lmsw */
9182 /*
9183 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9184 * cr0. Other attempted changes are ignored, with no exit.
9185 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009186 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 if (vmcs12->cr0_guest_host_mask & 0xe &
9188 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009189 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009190 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9191 !(vmcs12->cr0_read_shadow & 0x1) &&
9192 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009193 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009194 break;
9195 }
Joe Perches1d804d02015-03-30 16:46:09 -07009196 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009197}
9198
9199/*
9200 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9201 * should handle it ourselves in L0 (and then continue L2). Only call this
9202 * when in is_guest_mode (L2).
9203 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009204static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009205{
Nadav Har'El644d7112011-05-25 23:12:35 +03009206 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9207 struct vcpu_vmx *vmx = to_vmx(vcpu);
9208 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9209
Jim Mattson4f350c62017-09-14 16:31:44 -07009210 if (vmx->nested.nested_run_pending)
9211 return false;
9212
9213 if (unlikely(vmx->fail)) {
9214 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9215 vmcs_read32(VM_INSTRUCTION_ERROR));
9216 return true;
9217 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009218
David Matlackc9f04402017-08-01 14:00:40 -07009219 /*
9220 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009221 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9222 * Page). The CPU may write to these pages via their host
9223 * physical address while L2 is running, bypassing any
9224 * address-translation-based dirty tracking (e.g. EPT write
9225 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009226 *
9227 * Mark them dirty on every exit from L2 to prevent them from
9228 * getting out of sync with dirty tracking.
9229 */
9230 nested_mark_vmcs12_pages_dirty(vcpu);
9231
Jim Mattson4f350c62017-09-14 16:31:44 -07009232 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9233 vmcs_readl(EXIT_QUALIFICATION),
9234 vmx->idt_vectoring_info,
9235 intr_info,
9236 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9237 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009238
9239 switch (exit_reason) {
9240 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009241 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009242 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009243 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009244 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009245 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009246 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009247 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009248 else if (is_debug(intr_info) &&
9249 vcpu->guest_debug &
9250 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9251 return false;
9252 else if (is_breakpoint(intr_info) &&
9253 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9254 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009255 return vmcs12->exception_bitmap &
9256 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9257 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009258 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009259 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009260 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009261 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009262 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009263 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009264 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009265 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009266 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009267 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009268 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009269 case EXIT_REASON_HLT:
9270 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9271 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009272 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009273 case EXIT_REASON_INVLPG:
9274 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9275 case EXIT_REASON_RDPMC:
9276 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009277 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009278 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009279 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009280 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009281 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009282 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9283 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9284 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9285 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9286 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9287 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009288 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009289 /*
9290 * VMX instructions trap unconditionally. This allows L1 to
9291 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9292 */
Joe Perches1d804d02015-03-30 16:46:09 -07009293 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009294 case EXIT_REASON_CR_ACCESS:
9295 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9296 case EXIT_REASON_DR_ACCESS:
9297 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9298 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009299 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009300 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9301 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009302 case EXIT_REASON_MSR_READ:
9303 case EXIT_REASON_MSR_WRITE:
9304 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9305 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009306 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009307 case EXIT_REASON_MWAIT_INSTRUCTION:
9308 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009309 case EXIT_REASON_MONITOR_TRAP_FLAG:
9310 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009311 case EXIT_REASON_MONITOR_INSTRUCTION:
9312 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9313 case EXIT_REASON_PAUSE_INSTRUCTION:
9314 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9315 nested_cpu_has2(vmcs12,
9316 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9317 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009318 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009319 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009320 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009321 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009322 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009323 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009324 /*
9325 * The controls for "virtualize APIC accesses," "APIC-
9326 * register virtualization," and "virtual-interrupt
9327 * delivery" only come from vmcs12.
9328 */
Joe Perches1d804d02015-03-30 16:46:09 -07009329 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009330 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009331 /*
9332 * L0 always deals with the EPT violation. If nested EPT is
9333 * used, and the nested mmu code discovers that the address is
9334 * missing in the guest EPT table (EPT12), the EPT violation
9335 * will be injected with nested_ept_inject_page_fault()
9336 */
Joe Perches1d804d02015-03-30 16:46:09 -07009337 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009338 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009339 /*
9340 * L2 never uses directly L1's EPT, but rather L0's own EPT
9341 * table (shadow on EPT) or a merged EPT table that L0 built
9342 * (EPT on EPT). So any problems with the structure of the
9343 * table is L0's fault.
9344 */
Joe Perches1d804d02015-03-30 16:46:09 -07009345 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009346 case EXIT_REASON_INVPCID:
9347 return
9348 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9349 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009350 case EXIT_REASON_WBINVD:
9351 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9352 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009353 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009354 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9355 /*
9356 * This should never happen, since it is not possible to
9357 * set XSS to a non-zero value---neither in L1 nor in L2.
9358 * If if it were, XSS would have to be checked against
9359 * the XSS exit bitmap in vmcs12.
9360 */
9361 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009362 case EXIT_REASON_PREEMPTION_TIMER:
9363 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009364 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009365 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009366 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009367 case EXIT_REASON_VMFUNC:
9368 /* VM functions are emulated through L2->L0 vmexits. */
9369 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009370 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009371 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009372 }
9373}
9374
Paolo Bonzini7313c692017-07-27 10:31:25 +02009375static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9376{
9377 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9378
9379 /*
9380 * At this point, the exit interruption info in exit_intr_info
9381 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9382 * we need to query the in-kernel LAPIC.
9383 */
9384 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9385 if ((exit_intr_info &
9386 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9387 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9388 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9389 vmcs12->vm_exit_intr_error_code =
9390 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9391 }
9392
9393 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9394 vmcs_readl(EXIT_QUALIFICATION));
9395 return 1;
9396}
9397
Avi Kivity586f9602010-11-18 13:09:54 +02009398static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9399{
9400 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9401 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9402}
9403
Kai Huanga3eaa862015-11-04 13:46:05 +08009404static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009405{
Kai Huanga3eaa862015-11-04 13:46:05 +08009406 if (vmx->pml_pg) {
9407 __free_page(vmx->pml_pg);
9408 vmx->pml_pg = NULL;
9409 }
Kai Huang843e4332015-01-28 10:54:28 +08009410}
9411
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009412static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009413{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009414 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009415 u64 *pml_buf;
9416 u16 pml_idx;
9417
9418 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9419
9420 /* Do nothing if PML buffer is empty */
9421 if (pml_idx == (PML_ENTITY_NUM - 1))
9422 return;
9423
9424 /* PML index always points to next available PML buffer entity */
9425 if (pml_idx >= PML_ENTITY_NUM)
9426 pml_idx = 0;
9427 else
9428 pml_idx++;
9429
9430 pml_buf = page_address(vmx->pml_pg);
9431 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9432 u64 gpa;
9433
9434 gpa = pml_buf[pml_idx];
9435 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009436 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009437 }
9438
9439 /* reset PML index */
9440 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9441}
9442
9443/*
9444 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9445 * Called before reporting dirty_bitmap to userspace.
9446 */
9447static void kvm_flush_pml_buffers(struct kvm *kvm)
9448{
9449 int i;
9450 struct kvm_vcpu *vcpu;
9451 /*
9452 * We only need to kick vcpu out of guest mode here, as PML buffer
9453 * is flushed at beginning of all VMEXITs, and it's obvious that only
9454 * vcpus running in guest are possible to have unflushed GPAs in PML
9455 * buffer.
9456 */
9457 kvm_for_each_vcpu(i, vcpu, kvm)
9458 kvm_vcpu_kick(vcpu);
9459}
9460
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009461static void vmx_dump_sel(char *name, uint32_t sel)
9462{
9463 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009464 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009465 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9466 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9467 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9468}
9469
9470static void vmx_dump_dtsel(char *name, uint32_t limit)
9471{
9472 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9473 name, vmcs_read32(limit),
9474 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9475}
9476
9477static void dump_vmcs(void)
9478{
9479 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9480 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9481 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9482 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9483 u32 secondary_exec_control = 0;
9484 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009485 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009486 int i, n;
9487
9488 if (cpu_has_secondary_exec_ctrls())
9489 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9490
9491 pr_err("*** Guest State ***\n");
9492 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9493 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9494 vmcs_readl(CR0_GUEST_HOST_MASK));
9495 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9496 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9497 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9498 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9499 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9500 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009501 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9502 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9503 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9504 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009505 }
9506 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9507 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9508 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9509 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9510 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9511 vmcs_readl(GUEST_SYSENTER_ESP),
9512 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9513 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9514 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9515 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9516 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9517 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9518 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9519 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9520 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9521 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9522 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9523 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9524 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009525 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9526 efer, vmcs_read64(GUEST_IA32_PAT));
9527 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9528 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009529 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009530 if (cpu_has_load_perf_global_ctrl &&
9531 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009532 pr_err("PerfGlobCtl = 0x%016llx\n",
9533 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009534 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009535 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009536 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9537 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9538 vmcs_read32(GUEST_ACTIVITY_STATE));
9539 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9540 pr_err("InterruptStatus = %04x\n",
9541 vmcs_read16(GUEST_INTR_STATUS));
9542
9543 pr_err("*** Host State ***\n");
9544 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9545 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9546 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9547 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9548 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9549 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9550 vmcs_read16(HOST_TR_SELECTOR));
9551 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9552 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9553 vmcs_readl(HOST_TR_BASE));
9554 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9555 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9556 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9557 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9558 vmcs_readl(HOST_CR4));
9559 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9560 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9561 vmcs_read32(HOST_IA32_SYSENTER_CS),
9562 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9563 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009564 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9565 vmcs_read64(HOST_IA32_EFER),
9566 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009567 if (cpu_has_load_perf_global_ctrl &&
9568 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009569 pr_err("PerfGlobCtl = 0x%016llx\n",
9570 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009571
9572 pr_err("*** Control State ***\n");
9573 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9574 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9575 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9576 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9577 vmcs_read32(EXCEPTION_BITMAP),
9578 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9579 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9580 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9581 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9582 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9583 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9584 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9585 vmcs_read32(VM_EXIT_INTR_INFO),
9586 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9587 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9588 pr_err(" reason=%08x qualification=%016lx\n",
9589 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9590 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9591 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9592 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009593 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009594 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009595 pr_err("TSC Multiplier = 0x%016llx\n",
9596 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009597 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9598 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9599 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9600 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9601 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009602 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009603 n = vmcs_read32(CR3_TARGET_COUNT);
9604 for (i = 0; i + 1 < n; i += 4)
9605 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9606 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9607 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9608 if (i < n)
9609 pr_err("CR3 target%u=%016lx\n",
9610 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9611 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9612 pr_err("PLE Gap=%08x Window=%08x\n",
9613 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9614 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9615 pr_err("Virtual processor ID = 0x%04x\n",
9616 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9617}
9618
Avi Kivity6aa8b732006-12-10 02:21:36 -08009619/*
9620 * The guest has exited. See if we can fix it or if we need userspace
9621 * assistance.
9622 */
Avi Kivity851ba692009-08-24 11:10:17 +03009623static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009624{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009625 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009626 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009627 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009628
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009629 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9630
Kai Huang843e4332015-01-28 10:54:28 +08009631 /*
9632 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9633 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9634 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9635 * mode as if vcpus is in root mode, the PML buffer must has been
9636 * flushed already.
9637 */
9638 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009639 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009640
Mohammed Gamal80ced182009-09-01 12:48:18 +02009641 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009642 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009643 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009644
Paolo Bonzini7313c692017-07-27 10:31:25 +02009645 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9646 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009647
Mohammed Gamal51207022010-05-31 22:40:54 +03009648 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009649 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009650 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9651 vcpu->run->fail_entry.hardware_entry_failure_reason
9652 = exit_reason;
9653 return 0;
9654 }
9655
Avi Kivity29bd8a72007-09-10 17:27:03 +03009656 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009657 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9658 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009659 = vmcs_read32(VM_INSTRUCTION_ERROR);
9660 return 0;
9661 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009662
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009663 /*
9664 * Note:
9665 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9666 * delivery event since it indicates guest is accessing MMIO.
9667 * The vm-exit can be triggered again after return to guest that
9668 * will cause infinite loop.
9669 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009670 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009671 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009672 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009673 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009674 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9675 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9676 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009677 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009678 vcpu->run->internal.data[0] = vectoring_info;
9679 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009680 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9681 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9682 vcpu->run->internal.ndata++;
9683 vcpu->run->internal.data[3] =
9684 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9685 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009686 return 0;
9687 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009688
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009689 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009690 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9691 if (vmx_interrupt_allowed(vcpu)) {
9692 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9693 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9694 vcpu->arch.nmi_pending) {
9695 /*
9696 * This CPU don't support us in finding the end of an
9697 * NMI-blocked window if the guest runs with IRQs
9698 * disabled. So we pull the trigger after 1 s of
9699 * futile waiting, but inform the user about this.
9700 */
9701 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9702 "state on VCPU %d after 1 s timeout\n",
9703 __func__, vcpu->vcpu_id);
9704 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9705 }
9706 }
9707
Avi Kivity6aa8b732006-12-10 02:21:36 -08009708 if (exit_reason < kvm_vmx_max_exit_handlers
9709 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009710 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009711 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009712 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9713 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009714 kvm_queue_exception(vcpu, UD_VECTOR);
9715 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009716 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009717}
9718
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009719/*
9720 * Software based L1D cache flush which is used when microcode providing
9721 * the cache control MSR is not loaded.
9722 *
9723 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9724 * flush it is required to read in 64 KiB because the replacement algorithm
9725 * is not exactly LRU. This could be sized at runtime via topology
9726 * information but as all relevant affected CPUs have 32KiB L1D cache size
9727 * there is no point in doing so.
9728 */
9729#define L1D_CACHE_ORDER 4
9730static void *vmx_l1d_flush_pages;
9731
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009732static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009733{
9734 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009735
9736 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009737 * This code is only executed when the the flush mode is 'cond' or
9738 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009739 */
Nicolai Stange427362a2018-07-21 22:25:00 +02009740 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02009741 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02009742
Nicolai Stange379fd0c2018-07-21 22:16:56 +02009743 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02009744 * Clear the per-vcpu flush bit, it gets set again
9745 * either from vcpu_run() or from one of the unsafe
9746 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02009747 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02009748 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02009749 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02009750
9751 /*
9752 * Clear the per-cpu flush bit, it gets set again from
9753 * the interrupt handlers.
9754 */
9755 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
9756 kvm_clear_cpu_l1tf_flush_l1d();
9757
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02009758 if (!flush_l1d)
9759 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02009760 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009761
9762 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009763
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009764 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9765 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9766 return;
9767 }
9768
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009769 asm volatile(
9770 /* First ensure the pages are in the TLB */
9771 "xorl %%eax, %%eax\n"
9772 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02009773 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009774 "addl $4096, %%eax\n\t"
9775 "cmpl %%eax, %[size]\n\t"
9776 "jne .Lpopulate_tlb\n\t"
9777 "xorl %%eax, %%eax\n\t"
9778 "cpuid\n\t"
9779 /* Now fill the cache */
9780 "xorl %%eax, %%eax\n"
9781 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02009782 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009783 "addl $64, %%eax\n\t"
9784 "cmpl %%eax, %[size]\n\t"
9785 "jne .Lfill_cache\n\t"
9786 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02009787 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009788 [size] "r" (size)
9789 : "eax", "ebx", "ecx", "edx");
9790}
9791
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009792static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009793{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009794 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9795
9796 if (is_guest_mode(vcpu) &&
9797 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9798 return;
9799
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009800 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009801 vmcs_write32(TPR_THRESHOLD, 0);
9802 return;
9803 }
9804
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009805 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009806}
9807
Jim Mattson8d860bb2018-05-09 16:56:05 -04009808static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009809{
9810 u32 sec_exec_control;
9811
Jim Mattson8d860bb2018-05-09 16:56:05 -04009812 if (!lapic_in_kernel(vcpu))
9813 return;
9814
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009815 /* Postpone execution until vmcs01 is the current VMCS. */
9816 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009817 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009818 return;
9819 }
9820
Paolo Bonzini35754c92015-07-29 12:05:37 +02009821 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009822 return;
9823
9824 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009825 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9826 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009827
Jim Mattson8d860bb2018-05-09 16:56:05 -04009828 switch (kvm_get_apic_mode(vcpu)) {
9829 case LAPIC_MODE_INVALID:
9830 WARN_ONCE(true, "Invalid local APIC state");
9831 case LAPIC_MODE_DISABLED:
9832 break;
9833 case LAPIC_MODE_XAPIC:
9834 if (flexpriority_enabled) {
9835 sec_exec_control |=
9836 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9837 vmx_flush_tlb(vcpu, true);
9838 }
9839 break;
9840 case LAPIC_MODE_X2APIC:
9841 if (cpu_has_vmx_virtualize_x2apic_mode())
9842 sec_exec_control |=
9843 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9844 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009845 }
9846 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9847
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009848 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009849}
9850
Tang Chen38b99172014-09-24 15:57:54 +08009851static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9852{
Jim Mattsonab5df312018-05-09 17:02:03 -04009853 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009854 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009855 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009856 }
Tang Chen38b99172014-09-24 15:57:54 +08009857}
9858
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009859static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009860{
9861 u16 status;
9862 u8 old;
9863
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009864 if (max_isr == -1)
9865 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009866
9867 status = vmcs_read16(GUEST_INTR_STATUS);
9868 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009869 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009870 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009871 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009872 vmcs_write16(GUEST_INTR_STATUS, status);
9873 }
9874}
9875
9876static void vmx_set_rvi(int vector)
9877{
9878 u16 status;
9879 u8 old;
9880
Wei Wang4114c272014-11-05 10:53:43 +08009881 if (vector == -1)
9882 vector = 0;
9883
Yang Zhangc7c9c562013-01-25 10:18:51 +08009884 status = vmcs_read16(GUEST_INTR_STATUS);
9885 old = (u8)status & 0xff;
9886 if ((u8)vector != old) {
9887 status &= ~0xff;
9888 status |= (u8)vector;
9889 vmcs_write16(GUEST_INTR_STATUS, status);
9890 }
9891}
9892
9893static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9894{
Liran Alon851c1a182017-12-24 18:12:56 +02009895 /*
9896 * When running L2, updating RVI is only relevant when
9897 * vmcs12 virtual-interrupt-delivery enabled.
9898 * However, it can be enabled only when L1 also
9899 * intercepts external-interrupts and in that case
9900 * we should not update vmcs02 RVI but instead intercept
9901 * interrupt. Therefore, do nothing when running L2.
9902 */
9903 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009904 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009905}
9906
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009907static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009908{
9909 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009910 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009911 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009912
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009913 WARN_ON(!vcpu->arch.apicv_active);
9914 if (pi_test_on(&vmx->pi_desc)) {
9915 pi_clear_on(&vmx->pi_desc);
9916 /*
9917 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9918 * But on x86 this is just a compiler barrier anyway.
9919 */
9920 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009921 max_irr_updated =
9922 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9923
9924 /*
9925 * If we are running L2 and L1 has a new pending interrupt
9926 * which can be injected, we should re-evaluate
9927 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009928 * If L1 intercepts external-interrupts, we should
9929 * exit from L2 to L1. Otherwise, interrupt should be
9930 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009931 */
Liran Alon851c1a182017-12-24 18:12:56 +02009932 if (is_guest_mode(vcpu) && max_irr_updated) {
9933 if (nested_exit_on_intr(vcpu))
9934 kvm_vcpu_exiting_guest_mode(vcpu);
9935 else
9936 kvm_make_request(KVM_REQ_EVENT, vcpu);
9937 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009938 } else {
9939 max_irr = kvm_lapic_find_highest_irr(vcpu);
9940 }
9941 vmx_hwapic_irr_update(vcpu, max_irr);
9942 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009943}
9944
Andrey Smetanin63086302015-11-10 15:36:32 +03009945static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009946{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009947 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009948 return;
9949
Yang Zhangc7c9c562013-01-25 10:18:51 +08009950 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9951 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9952 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9953 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9954}
9955
Paolo Bonzini967235d2016-12-19 14:03:45 +01009956static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9957{
9958 struct vcpu_vmx *vmx = to_vmx(vcpu);
9959
9960 pi_clear_on(&vmx->pi_desc);
9961 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9962}
9963
Avi Kivity51aa01d2010-07-20 14:31:20 +03009964static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009965{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009966 u32 exit_intr_info = 0;
9967 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009968
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009969 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9970 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009971 return;
9972
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009973 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9974 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9975 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009976
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009977 /* if exit due to PF check for async PF */
9978 if (is_page_fault(exit_intr_info))
9979 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9980
Andi Kleena0861c02009-06-08 17:37:09 +08009981 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009982 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9983 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009984 kvm_machine_check();
9985
Gleb Natapov20f65982009-05-11 13:35:55 +03009986 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009987 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009988 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009989 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009990 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009991 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009992}
Gleb Natapov20f65982009-05-11 13:35:55 +03009993
Yang Zhanga547c6d2013-04-11 19:25:10 +08009994static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9995{
9996 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9997
Yang Zhanga547c6d2013-04-11 19:25:10 +08009998 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9999 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10000 unsigned int vector;
10001 unsigned long entry;
10002 gate_desc *desc;
10003 struct vcpu_vmx *vmx = to_vmx(vcpu);
10004#ifdef CONFIG_X86_64
10005 unsigned long tmp;
10006#endif
10007
10008 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10009 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010010 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010011 asm volatile(
10012#ifdef CONFIG_X86_64
10013 "mov %%" _ASM_SP ", %[sp]\n\t"
10014 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10015 "push $%c[ss]\n\t"
10016 "push %[sp]\n\t"
10017#endif
10018 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010019 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010020 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010021 :
10022#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010023 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010024#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010025 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010026 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010027 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010028 [ss]"i"(__KERNEL_DS),
10029 [cs]"i"(__KERNEL_CS)
10030 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010031 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010032}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010033STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010034
Tom Lendackybc226f02018-05-10 22:06:39 +020010035static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010036{
Tom Lendackybc226f02018-05-10 22:06:39 +020010037 switch (index) {
10038 case MSR_IA32_SMBASE:
10039 /*
10040 * We cannot do SMM unless we can run the guest in big
10041 * real mode.
10042 */
10043 return enable_unrestricted_guest || emulate_invalid_guest_state;
10044 case MSR_AMD64_VIRT_SPEC_CTRL:
10045 /* This is AMD only. */
10046 return false;
10047 default:
10048 return true;
10049 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010050}
10051
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010052static bool vmx_mpx_supported(void)
10053{
10054 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10055 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10056}
10057
Wanpeng Li55412b22014-12-02 19:21:30 +080010058static bool vmx_xsaves_supported(void)
10059{
10060 return vmcs_config.cpu_based_2nd_exec_ctrl &
10061 SECONDARY_EXEC_XSAVES;
10062}
10063
Avi Kivity51aa01d2010-07-20 14:31:20 +030010064static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10065{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010066 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010067 bool unblock_nmi;
10068 u8 vector;
10069 bool idtv_info_valid;
10070
10071 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010072
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010073 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010074 if (vmx->loaded_vmcs->nmi_known_unmasked)
10075 return;
10076 /*
10077 * Can't use vmx->exit_intr_info since we're not sure what
10078 * the exit reason is.
10079 */
10080 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10081 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10082 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10083 /*
10084 * SDM 3: 27.7.1.2 (September 2008)
10085 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10086 * a guest IRET fault.
10087 * SDM 3: 23.2.2 (September 2008)
10088 * Bit 12 is undefined in any of the following cases:
10089 * If the VM exit sets the valid bit in the IDT-vectoring
10090 * information field.
10091 * If the VM exit is due to a double fault.
10092 */
10093 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10094 vector != DF_VECTOR && !idtv_info_valid)
10095 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10096 GUEST_INTR_STATE_NMI);
10097 else
10098 vmx->loaded_vmcs->nmi_known_unmasked =
10099 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10100 & GUEST_INTR_STATE_NMI);
10101 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10102 vmx->loaded_vmcs->vnmi_blocked_time +=
10103 ktime_to_ns(ktime_sub(ktime_get(),
10104 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010105}
10106
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010107static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010108 u32 idt_vectoring_info,
10109 int instr_len_field,
10110 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010111{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010112 u8 vector;
10113 int type;
10114 bool idtv_info_valid;
10115
10116 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010117
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010118 vcpu->arch.nmi_injected = false;
10119 kvm_clear_exception_queue(vcpu);
10120 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010121
10122 if (!idtv_info_valid)
10123 return;
10124
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010125 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010126
Avi Kivity668f6122008-07-02 09:28:55 +030010127 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10128 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010129
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010130 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010131 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010132 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010133 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010134 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010135 * Clear bit "block by NMI" before VM entry if a NMI
10136 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010137 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010138 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010139 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010140 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010141 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010142 /* fall through */
10143 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010144 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010145 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010146 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010147 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010148 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010149 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010150 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010151 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010152 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010153 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010154 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010155 break;
10156 default:
10157 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010158 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010159}
10160
Avi Kivity83422e12010-07-20 14:43:23 +030010161static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10162{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010163 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010164 VM_EXIT_INSTRUCTION_LEN,
10165 IDT_VECTORING_ERROR_CODE);
10166}
10167
Avi Kivityb463a6f2010-07-20 15:06:17 +030010168static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10169{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010170 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010171 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10172 VM_ENTRY_INSTRUCTION_LEN,
10173 VM_ENTRY_EXCEPTION_ERROR_CODE);
10174
10175 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10176}
10177
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010178static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10179{
10180 int i, nr_msrs;
10181 struct perf_guest_switch_msr *msrs;
10182
10183 msrs = perf_guest_get_msrs(&nr_msrs);
10184
10185 if (!msrs)
10186 return;
10187
10188 for (i = 0; i < nr_msrs; i++)
10189 if (msrs[i].host == msrs[i].guest)
10190 clear_atomic_switch_msr(vmx, msrs[i].msr);
10191 else
10192 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010193 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010194}
10195
Jiang Biao33365e72016-11-03 15:03:37 +080010196static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010197{
10198 struct vcpu_vmx *vmx = to_vmx(vcpu);
10199 u64 tscl;
10200 u32 delta_tsc;
10201
10202 if (vmx->hv_deadline_tsc == -1)
10203 return;
10204
10205 tscl = rdtsc();
10206 if (vmx->hv_deadline_tsc > tscl)
10207 /* sure to be 32 bit only because checked on set_hv_timer */
10208 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10209 cpu_preemption_timer_multi);
10210 else
10211 delta_tsc = 0;
10212
10213 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10214}
10215
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010216static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010217{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010218 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010219 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010220
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010221 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010222 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010223 vmx->loaded_vmcs->soft_vnmi_blocked))
10224 vmx->loaded_vmcs->entry_time = ktime_get();
10225
Avi Kivity104f2262010-11-18 13:12:52 +020010226 /* Don't enter VMX if guest state is invalid, let the exit handler
10227 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010228 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010229 return;
10230
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010231 if (vmx->ple_window_dirty) {
10232 vmx->ple_window_dirty = false;
10233 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10234 }
10235
Abel Gordon012f83c2013-04-18 14:39:25 +030010236 if (vmx->nested.sync_shadow_vmcs) {
10237 copy_vmcs12_to_shadow(vmx);
10238 vmx->nested.sync_shadow_vmcs = false;
10239 }
10240
Avi Kivity104f2262010-11-18 13:12:52 +020010241 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10242 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10243 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10244 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10245
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010246 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010247 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010248 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010249 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010250 }
10251
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010252 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010253 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010254 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010255 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010256 }
10257
Avi Kivity104f2262010-11-18 13:12:52 +020010258 /* When single-stepping over STI and MOV SS, we must clear the
10259 * corresponding interruptibility bits in the guest state. Otherwise
10260 * vmentry fails as it then expects bit 14 (BS) in pending debug
10261 * exceptions being set, but that's not correct for the guest debugging
10262 * case. */
10263 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10264 vmx_set_interrupt_shadow(vcpu, 0);
10265
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010266 if (static_cpu_has(X86_FEATURE_PKU) &&
10267 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10268 vcpu->arch.pkru != vmx->host_pkru)
10269 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010270
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010271 atomic_switch_perf_msrs(vmx);
10272
Yunhong Jiang64672c92016-06-13 14:19:59 -070010273 vmx_arm_hv_timer(vcpu);
10274
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010275 /*
10276 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10277 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10278 * is no need to worry about the conditional branch over the wrmsr
10279 * being speculatively taken.
10280 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010281 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010282
Nadav Har'Eld462b812011-05-24 15:26:10 +030010283 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010284
10285 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10286 (unsigned long)&current_evmcs->host_rsp : 0;
10287
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010288 if (static_branch_unlikely(&vmx_l1d_should_flush))
10289 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010290
Avi Kivity104f2262010-11-18 13:12:52 +020010291 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010292 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010293 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10294 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10295 "push %%" _ASM_CX " \n\t"
10296 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010297 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010298 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010299 /* Avoid VMWRITE when Enlightened VMCS is in use */
10300 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10301 "jz 2f \n\t"
10302 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10303 "jmp 1f \n\t"
10304 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010305 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010306 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010307 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010308 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10309 "mov %%cr2, %%" _ASM_DX " \n\t"
10310 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010311 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010312 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010313 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010314 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010315 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010316 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010317 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10318 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10319 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10320 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10321 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10322 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010323#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010324 "mov %c[r8](%0), %%r8 \n\t"
10325 "mov %c[r9](%0), %%r9 \n\t"
10326 "mov %c[r10](%0), %%r10 \n\t"
10327 "mov %c[r11](%0), %%r11 \n\t"
10328 "mov %c[r12](%0), %%r12 \n\t"
10329 "mov %c[r13](%0), %%r13 \n\t"
10330 "mov %c[r14](%0), %%r14 \n\t"
10331 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010332#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010333 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010334
Avi Kivity6aa8b732006-12-10 02:21:36 -080010335 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010336 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010337 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010338 "jmp 2f \n\t"
10339 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10340 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010341 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010342 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010343 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010344 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010345 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10346 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10347 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10348 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10349 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10350 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10351 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010352#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010353 "mov %%r8, %c[r8](%0) \n\t"
10354 "mov %%r9, %c[r9](%0) \n\t"
10355 "mov %%r10, %c[r10](%0) \n\t"
10356 "mov %%r11, %c[r11](%0) \n\t"
10357 "mov %%r12, %c[r12](%0) \n\t"
10358 "mov %%r13, %c[r13](%0) \n\t"
10359 "mov %%r14, %c[r14](%0) \n\t"
10360 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010361 "xor %%r8d, %%r8d \n\t"
10362 "xor %%r9d, %%r9d \n\t"
10363 "xor %%r10d, %%r10d \n\t"
10364 "xor %%r11d, %%r11d \n\t"
10365 "xor %%r12d, %%r12d \n\t"
10366 "xor %%r13d, %%r13d \n\t"
10367 "xor %%r14d, %%r14d \n\t"
10368 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010369#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010370 "mov %%cr2, %%" _ASM_AX " \n\t"
10371 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010372
Jim Mattson0cb5b302018-01-03 14:31:38 -080010373 "xor %%eax, %%eax \n\t"
10374 "xor %%ebx, %%ebx \n\t"
10375 "xor %%esi, %%esi \n\t"
10376 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010377 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010378 ".pushsection .rodata \n\t"
10379 ".global vmx_return \n\t"
10380 "vmx_return: " _ASM_PTR " 2b \n\t"
10381 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010382 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010383 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010384 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010385 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010386 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10387 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10388 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10389 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10390 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10391 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10392 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010393#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010394 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10395 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10396 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10397 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10398 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10399 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10400 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10401 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010402#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010403 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10404 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010405 : "cc", "memory"
10406#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010407 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010408 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010409#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010410 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010411#endif
10412 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010413
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010414 /*
10415 * We do not use IBRS in the kernel. If this vCPU has used the
10416 * SPEC_CTRL MSR it may have left it on; save the value and
10417 * turn it off. This is much more efficient than blindly adding
10418 * it to the atomic save/restore list. Especially as the former
10419 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10420 *
10421 * For non-nested case:
10422 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10423 * save it.
10424 *
10425 * For nested case:
10426 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10427 * save it.
10428 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010429 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010430 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010431
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010432 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010433
David Woodhouse117cc7a2018-01-12 11:11:27 +000010434 /* Eliminate branch target predictions from guest mode */
10435 vmexit_fill_RSB();
10436
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010437 /* All fields are clean at this point */
10438 if (static_branch_unlikely(&enable_evmcs))
10439 current_evmcs->hv_clean_fields |=
10440 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10441
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010442 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010443 if (vmx->host_debugctlmsr)
10444 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010445
Avi Kivityaa67f602012-08-01 16:48:03 +030010446#ifndef CONFIG_X86_64
10447 /*
10448 * The sysexit path does not restore ds/es, so we must set them to
10449 * a reasonable value ourselves.
10450 *
10451 * We can't defer this to vmx_load_host_state() since that function
10452 * may be executed in interrupt context, which saves and restore segments
10453 * around it, nullifying its effect.
10454 */
10455 loadsegment(ds, __USER_DS);
10456 loadsegment(es, __USER_DS);
10457#endif
10458
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010459 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010460 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010461 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010462 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010463 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010464 vcpu->arch.regs_dirty = 0;
10465
Gleb Natapove0b890d2013-09-25 12:51:33 +030010466 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010467 * eager fpu is enabled if PKEY is supported and CR4 is switched
10468 * back on host, so it is safe to read guest PKRU from current
10469 * XSAVE.
10470 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010471 if (static_cpu_has(X86_FEATURE_PKU) &&
10472 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10473 vcpu->arch.pkru = __read_pkru();
10474 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010475 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010476 }
10477
Gleb Natapove0b890d2013-09-25 12:51:33 +030010478 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010479 vmx->idt_vectoring_info = 0;
10480
10481 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10482 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10483 return;
10484
10485 vmx->loaded_vmcs->launched = 1;
10486 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010487
Avi Kivity51aa01d2010-07-20 14:31:20 +030010488 vmx_complete_atomic_exit(vmx);
10489 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010490 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010491}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010492STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010493
Sean Christopherson434a1e92018-03-20 12:17:18 -070010494static struct kvm *vmx_vm_alloc(void)
10495{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010496 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010497 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010498}
10499
10500static void vmx_vm_free(struct kvm *kvm)
10501{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010502 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010503}
10504
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010505static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010506{
10507 struct vcpu_vmx *vmx = to_vmx(vcpu);
10508 int cpu;
10509
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010510 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010511 return;
10512
10513 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010514 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010515 vmx_vcpu_put(vcpu);
10516 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010517 put_cpu();
10518}
10519
Jim Mattson2f1fe812016-07-08 15:36:06 -070010520/*
10521 * Ensure that the current vmcs of the logical processor is the
10522 * vmcs01 of the vcpu before calling free_nested().
10523 */
10524static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10525{
10526 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010527
Christoffer Dallec7660c2017-12-04 21:35:23 +010010528 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010529 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010530 free_nested(vmx);
10531 vcpu_put(vcpu);
10532}
10533
Avi Kivity6aa8b732006-12-10 02:21:36 -080010534static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10535{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010536 struct vcpu_vmx *vmx = to_vmx(vcpu);
10537
Kai Huang843e4332015-01-28 10:54:28 +080010538 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010539 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010540 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010541 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010542 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010543 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010544 kfree(vmx->guest_msrs);
10545 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010546 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010547}
10548
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010549static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010550{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010551 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010552 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010553 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010554 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010555
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010556 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010557 return ERR_PTR(-ENOMEM);
10558
Wanpeng Li991e7a02015-09-16 17:30:05 +080010559 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010560
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010561 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10562 if (err)
10563 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010564
Peter Feiner4e595162016-07-07 14:49:58 -070010565 err = -ENOMEM;
10566
10567 /*
10568 * If PML is turned on, failure on enabling PML just results in failure
10569 * of creating the vcpu, therefore we can simplify PML logic (by
10570 * avoiding dealing with cases, such as enabling PML partially on vcpus
10571 * for the guest, etc.
10572 */
10573 if (enable_pml) {
10574 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10575 if (!vmx->pml_pg)
10576 goto uninit_vcpu;
10577 }
10578
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010579 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010580 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10581 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010582
Peter Feiner4e595162016-07-07 14:49:58 -070010583 if (!vmx->guest_msrs)
10584 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010585
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010586 err = alloc_loaded_vmcs(&vmx->vmcs01);
10587 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010588 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010589
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010590 msr_bitmap = vmx->vmcs01.msr_bitmap;
10591 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10592 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10593 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10594 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10595 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10596 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10597 vmx->msr_bitmap_mode = 0;
10598
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010599 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010600 cpu = get_cpu();
10601 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010602 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010603 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010604 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010605 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010606 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010607 err = alloc_apic_access_page(kvm);
10608 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010609 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010610 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010611
Sean Christophersone90008d2018-03-05 12:04:37 -080010612 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010613 err = init_rmode_identity_map(kvm);
10614 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010615 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010616 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010617
Wanpeng Li5c614b32015-10-13 09:18:36 -070010618 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010619 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10620 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010621 vmx->nested.vpid02 = allocate_vpid();
10622 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010623
Wincy Van705699a2015-02-03 23:58:17 +080010624 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010625 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010626
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010627 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10628
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010629 /*
10630 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10631 * or POSTED_INTR_WAKEUP_VECTOR.
10632 */
10633 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10634 vmx->pi_desc.sn = 1;
10635
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010636 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010637
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010638free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010639 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010640 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010641free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010642 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010643free_pml:
10644 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010645uninit_vcpu:
10646 kvm_vcpu_uninit(&vmx->vcpu);
10647free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010648 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010649 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010650 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010651}
10652
Jiri Kosinad90a7a02018-07-13 16:23:25 +020010653#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"
10654#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 -040010655
Wanpeng Lib31c1142018-03-12 04:53:04 -070010656static int vmx_vm_init(struct kvm *kvm)
10657{
10658 if (!ple_gap)
10659 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010660
Jiri Kosinad90a7a02018-07-13 16:23:25 +020010661 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
10662 switch (l1tf_mitigation) {
10663 case L1TF_MITIGATION_OFF:
10664 case L1TF_MITIGATION_FLUSH_NOWARN:
10665 /* 'I explicitly don't care' is set */
10666 break;
10667 case L1TF_MITIGATION_FLUSH:
10668 case L1TF_MITIGATION_FLUSH_NOSMT:
10669 case L1TF_MITIGATION_FULL:
10670 /*
10671 * Warn upon starting the first VM in a potentially
10672 * insecure environment.
10673 */
10674 if (cpu_smt_control == CPU_SMT_ENABLED)
10675 pr_warn_once(L1TF_MSG_SMT);
10676 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
10677 pr_warn_once(L1TF_MSG_L1D);
10678 break;
10679 case L1TF_MITIGATION_FULL_FORCE:
10680 /* Flush is enforced */
10681 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010682 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010683 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010684 return 0;
10685}
10686
Yang, Sheng002c7f72007-07-31 14:23:01 +030010687static void __init vmx_check_processor_compat(void *rtn)
10688{
10689 struct vmcs_config vmcs_conf;
10690
10691 *(int *)rtn = 0;
10692 if (setup_vmcs_config(&vmcs_conf) < 0)
10693 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010694 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010695 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10696 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10697 smp_processor_id());
10698 *(int *)rtn = -EIO;
10699 }
10700}
10701
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010702static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010703{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010704 u8 cache;
10705 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010706
Sheng Yang522c68c2009-04-27 20:35:43 +080010707 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010708 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010709 * 2. EPT with VT-d:
10710 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010711 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010712 * b. VT-d with snooping control feature: snooping control feature of
10713 * VT-d engine can guarantee the cache correctness. Just set it
10714 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010715 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010716 * consistent with host MTRR
10717 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010718 if (is_mmio) {
10719 cache = MTRR_TYPE_UNCACHABLE;
10720 goto exit;
10721 }
10722
10723 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010724 ipat = VMX_EPT_IPAT_BIT;
10725 cache = MTRR_TYPE_WRBACK;
10726 goto exit;
10727 }
10728
10729 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10730 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010731 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010732 cache = MTRR_TYPE_WRBACK;
10733 else
10734 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010735 goto exit;
10736 }
10737
Xiao Guangrongff536042015-06-15 16:55:22 +080010738 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010739
10740exit:
10741 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010742}
10743
Sheng Yang17cc3932010-01-05 19:02:27 +080010744static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010745{
Sheng Yang878403b2010-01-05 19:02:29 +080010746 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10747 return PT_DIRECTORY_LEVEL;
10748 else
10749 /* For shadow and EPT supported 1GB page */
10750 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010751}
10752
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010753static void vmcs_set_secondary_exec_control(u32 new_ctl)
10754{
10755 /*
10756 * These bits in the secondary execution controls field
10757 * are dynamic, the others are mostly based on the hypervisor
10758 * architecture and the guest's CPUID. Do not touch the
10759 * dynamic bits.
10760 */
10761 u32 mask =
10762 SECONDARY_EXEC_SHADOW_VMCS |
10763 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010764 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10765 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010766
10767 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10768
10769 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10770 (new_ctl & ~mask) | (cur_ctl & mask));
10771}
10772
David Matlack8322ebb2016-11-29 18:14:09 -080010773/*
10774 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10775 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10776 */
10777static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10778{
10779 struct vcpu_vmx *vmx = to_vmx(vcpu);
10780 struct kvm_cpuid_entry2 *entry;
10781
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010782 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10783 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010784
10785#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10786 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010787 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010788} while (0)
10789
10790 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10791 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10792 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10793 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10794 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10795 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10796 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10797 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10798 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10799 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10800 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10801 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10802 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10803 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10804 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10805
10806 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10807 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10808 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10809 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10810 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010811 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010812
10813#undef cr4_fixed1_update
10814}
10815
Sheng Yang0e851882009-12-18 16:48:46 +080010816static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10817{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010819
Paolo Bonzini80154d72017-08-24 13:55:35 +020010820 if (cpu_has_secondary_exec_ctrls()) {
10821 vmx_compute_secondary_exec_control(vmx);
10822 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010823 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010824
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010825 if (nested_vmx_allowed(vcpu))
10826 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10827 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10828 else
10829 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10830 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010831
10832 if (nested_vmx_allowed(vcpu))
10833 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010834}
10835
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010836static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10837{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010838 if (func == 1 && nested)
10839 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010840}
10841
Yang Zhang25d92082013-08-06 12:00:32 +030010842static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10843 struct x86_exception *fault)
10844{
Jan Kiszka533558b2014-01-04 18:47:20 +010010845 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010846 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010847 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010848 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010849
Bandan Dasc5f983f2017-05-05 15:25:14 -040010850 if (vmx->nested.pml_full) {
10851 exit_reason = EXIT_REASON_PML_FULL;
10852 vmx->nested.pml_full = false;
10853 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10854 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010855 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010856 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010857 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010858
10859 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010860 vmcs12->guest_physical_address = fault->address;
10861}
10862
Peter Feiner995f00a2017-06-30 17:26:32 -070010863static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10864{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010865 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010866}
10867
Nadav Har'El155a97a2013-08-05 11:07:16 +030010868/* Callbacks for nested_ept_init_mmu_context: */
10869
10870static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10871{
10872 /* return the page table to be shadowed - in our case, EPT12 */
10873 return get_vmcs12(vcpu)->ept_pointer;
10874}
10875
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010876static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010877{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010878 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010879 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010880 return 1;
10881
10882 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010883 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010884 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010885 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010886 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010887 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10888 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10889 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10890
10891 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010892 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010893}
10894
10895static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10896{
10897 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10898}
10899
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010900static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10901 u16 error_code)
10902{
10903 bool inequality, bit;
10904
10905 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10906 inequality =
10907 (error_code & vmcs12->page_fault_error_code_mask) !=
10908 vmcs12->page_fault_error_code_match;
10909 return inequality ^ bit;
10910}
10911
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010912static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10913 struct x86_exception *fault)
10914{
10915 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10916
10917 WARN_ON(!is_guest_mode(vcpu));
10918
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010919 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10920 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010921 vmcs12->vm_exit_intr_error_code = fault->error_code;
10922 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10923 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10924 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10925 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010926 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010927 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010928 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010929}
10930
Paolo Bonzinic9923842017-12-13 14:16:30 +010010931static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10932 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010933
10934static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010935 struct vmcs12 *vmcs12)
10936{
10937 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010938 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010939 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010940
10941 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010942 /*
10943 * Translate L1 physical address to host physical
10944 * address for vmcs02. Keep the page pinned, so this
10945 * physical address remains valid. We keep a reference
10946 * to it so we can release it later.
10947 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010948 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010949 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010950 vmx->nested.apic_access_page = NULL;
10951 }
10952 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010953 /*
10954 * If translation failed, no matter: This feature asks
10955 * to exit when accessing the given address, and if it
10956 * can never be accessed, this feature won't do
10957 * anything anyway.
10958 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010959 if (!is_error_page(page)) {
10960 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010961 hpa = page_to_phys(vmx->nested.apic_access_page);
10962 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10963 } else {
10964 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10965 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10966 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010967 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010968
10969 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010970 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010971 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010972 vmx->nested.virtual_apic_page = NULL;
10973 }
10974 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010975
10976 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010977 * If translation failed, VM entry will fail because
10978 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10979 * Failing the vm entry is _not_ what the processor
10980 * does but it's basically the only possibility we
10981 * have. We could still enter the guest if CR8 load
10982 * exits are enabled, CR8 store exits are enabled, and
10983 * virtualize APIC access is disabled; in this case
10984 * the processor would never use the TPR shadow and we
10985 * could simply clear the bit from the execution
10986 * control. But such a configuration is useless, so
10987 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010988 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010989 if (!is_error_page(page)) {
10990 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010991 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10992 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10993 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010994 }
10995
Wincy Van705699a2015-02-03 23:58:17 +080010996 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010997 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10998 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010999 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011000 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011001 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011002 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11003 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011004 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011005 vmx->nested.pi_desc_page = page;
11006 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011007 vmx->nested.pi_desc =
11008 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11009 (unsigned long)(vmcs12->posted_intr_desc_addr &
11010 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011011 vmcs_write64(POSTED_INTR_DESC_ADDR,
11012 page_to_phys(vmx->nested.pi_desc_page) +
11013 (unsigned long)(vmcs12->posted_intr_desc_addr &
11014 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011015 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011016 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011017 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11018 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011019 else
11020 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11021 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011022}
11023
Jan Kiszkaf4124502014-03-07 20:03:13 +010011024static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11025{
11026 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11027 struct vcpu_vmx *vmx = to_vmx(vcpu);
11028
11029 if (vcpu->arch.virtual_tsc_khz == 0)
11030 return;
11031
11032 /* Make sure short timeouts reliably trigger an immediate vmexit.
11033 * hrtimer_start does not guarantee this. */
11034 if (preemption_timeout <= 1) {
11035 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11036 return;
11037 }
11038
11039 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11040 preemption_timeout *= 1000000;
11041 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11042 hrtimer_start(&vmx->nested.preemption_timer,
11043 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11044}
11045
Jim Mattson56a20512017-07-06 16:33:06 -070011046static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11047 struct vmcs12 *vmcs12)
11048{
11049 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11050 return 0;
11051
11052 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11053 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11054 return -EINVAL;
11055
11056 return 0;
11057}
11058
Wincy Van3af18d92015-02-03 23:49:31 +080011059static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11060 struct vmcs12 *vmcs12)
11061{
Wincy Van3af18d92015-02-03 23:49:31 +080011062 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11063 return 0;
11064
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011065 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011066 return -EINVAL;
11067
11068 return 0;
11069}
11070
Jim Mattson712b12d2017-08-24 13:24:47 -070011071static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11072 struct vmcs12 *vmcs12)
11073{
11074 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11075 return 0;
11076
11077 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11078 return -EINVAL;
11079
11080 return 0;
11081}
11082
Wincy Van3af18d92015-02-03 23:49:31 +080011083/*
11084 * Merge L0's and L1's MSR bitmap, return false to indicate that
11085 * we do not use the hardware.
11086 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011087static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11088 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011089{
Wincy Van82f0dd42015-02-03 23:57:18 +080011090 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011091 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011092 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011093 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011094 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011095 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011096 *
11097 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11098 * ensures that we do not accidentally generate an L02 MSR bitmap
11099 * from the L12 MSR bitmap that is too permissive.
11100 * 2. That L1 or L2s have actually used the MSR. This avoids
11101 * unnecessarily merging of the bitmap if the MSR is unused. This
11102 * works properly because we only update the L01 MSR bitmap lazily.
11103 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11104 * updated to reflect this when L1 (or its L2s) actually write to
11105 * the MSR.
11106 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011107 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11108 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011109
Paolo Bonzinic9923842017-12-13 14:16:30 +010011110 /* Nothing to do if the MSR bitmap is not in use. */
11111 if (!cpu_has_vmx_msr_bitmap() ||
11112 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11113 return false;
11114
Ashok Raj15d45072018-02-01 22:59:43 +010011115 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011116 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011117 return false;
11118
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011119 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11120 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011121 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011122
Radim Krčmářd048c092016-08-08 20:16:22 +020011123 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011124 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11125 /*
11126 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11127 * just lets the processor take the value from the virtual-APIC page;
11128 * take those 256 bits directly from the L1 bitmap.
11129 */
11130 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11131 unsigned word = msr / BITS_PER_LONG;
11132 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11133 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011134 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011135 } else {
11136 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11137 unsigned word = msr / BITS_PER_LONG;
11138 msr_bitmap_l0[word] = ~0;
11139 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11140 }
11141 }
11142
11143 nested_vmx_disable_intercept_for_msr(
11144 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011145 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011146 MSR_TYPE_W);
11147
11148 if (nested_cpu_has_vid(vmcs12)) {
11149 nested_vmx_disable_intercept_for_msr(
11150 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011151 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011152 MSR_TYPE_W);
11153 nested_vmx_disable_intercept_for_msr(
11154 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011155 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011156 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011157 }
Ashok Raj15d45072018-02-01 22:59:43 +010011158
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011159 if (spec_ctrl)
11160 nested_vmx_disable_intercept_for_msr(
11161 msr_bitmap_l1, msr_bitmap_l0,
11162 MSR_IA32_SPEC_CTRL,
11163 MSR_TYPE_R | MSR_TYPE_W);
11164
Ashok Raj15d45072018-02-01 22:59:43 +010011165 if (pred_cmd)
11166 nested_vmx_disable_intercept_for_msr(
11167 msr_bitmap_l1, msr_bitmap_l0,
11168 MSR_IA32_PRED_CMD,
11169 MSR_TYPE_W);
11170
Wincy Vanf2b93282015-02-03 23:56:03 +080011171 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011172 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011173
11174 return true;
11175}
11176
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011177static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11178 struct vmcs12 *vmcs12)
11179{
11180 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11181 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11182 return -EINVAL;
11183 else
11184 return 0;
11185}
11186
Wincy Vanf2b93282015-02-03 23:56:03 +080011187static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11188 struct vmcs12 *vmcs12)
11189{
Wincy Van82f0dd42015-02-03 23:57:18 +080011190 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011191 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011192 !nested_cpu_has_vid(vmcs12) &&
11193 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011194 return 0;
11195
11196 /*
11197 * If virtualize x2apic mode is enabled,
11198 * virtualize apic access must be disabled.
11199 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011200 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11201 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011202 return -EINVAL;
11203
Wincy Van608406e2015-02-03 23:57:51 +080011204 /*
11205 * If virtual interrupt delivery is enabled,
11206 * we must exit on external interrupts.
11207 */
11208 if (nested_cpu_has_vid(vmcs12) &&
11209 !nested_exit_on_intr(vcpu))
11210 return -EINVAL;
11211
Wincy Van705699a2015-02-03 23:58:17 +080011212 /*
11213 * bits 15:8 should be zero in posted_intr_nv,
11214 * the descriptor address has been already checked
11215 * in nested_get_vmcs12_pages.
11216 */
11217 if (nested_cpu_has_posted_intr(vmcs12) &&
11218 (!nested_cpu_has_vid(vmcs12) ||
11219 !nested_exit_intr_ack_set(vcpu) ||
11220 vmcs12->posted_intr_nv & 0xff00))
11221 return -EINVAL;
11222
Wincy Vanf2b93282015-02-03 23:56:03 +080011223 /* tpr shadow is needed by all apicv features. */
11224 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11225 return -EINVAL;
11226
11227 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011228}
11229
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011230static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11231 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011232 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011233{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011234 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011235 u64 count, addr;
11236
11237 if (vmcs12_read_any(vcpu, count_field, &count) ||
11238 vmcs12_read_any(vcpu, addr_field, &addr)) {
11239 WARN_ON(1);
11240 return -EINVAL;
11241 }
11242 if (count == 0)
11243 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011244 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011245 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11246 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011247 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011248 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11249 addr_field, maxphyaddr, count, addr);
11250 return -EINVAL;
11251 }
11252 return 0;
11253}
11254
11255static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11256 struct vmcs12 *vmcs12)
11257{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011258 if (vmcs12->vm_exit_msr_load_count == 0 &&
11259 vmcs12->vm_exit_msr_store_count == 0 &&
11260 vmcs12->vm_entry_msr_load_count == 0)
11261 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011262 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011263 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011264 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011265 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011266 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011267 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011268 return -EINVAL;
11269 return 0;
11270}
11271
Bandan Dasc5f983f2017-05-05 15:25:14 -040011272static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11273 struct vmcs12 *vmcs12)
11274{
11275 u64 address = vmcs12->pml_address;
11276 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11277
11278 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11279 if (!nested_cpu_has_ept(vmcs12) ||
11280 !IS_ALIGNED(address, 4096) ||
11281 address >> maxphyaddr)
11282 return -EINVAL;
11283 }
11284
11285 return 0;
11286}
11287
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011288static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11289 struct vmx_msr_entry *e)
11290{
11291 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011292 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011293 return -EINVAL;
11294 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11295 e->index == MSR_IA32_UCODE_REV)
11296 return -EINVAL;
11297 if (e->reserved != 0)
11298 return -EINVAL;
11299 return 0;
11300}
11301
11302static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11303 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011304{
11305 if (e->index == MSR_FS_BASE ||
11306 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011307 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11308 nested_vmx_msr_check_common(vcpu, e))
11309 return -EINVAL;
11310 return 0;
11311}
11312
11313static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11314 struct vmx_msr_entry *e)
11315{
11316 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11317 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011318 return -EINVAL;
11319 return 0;
11320}
11321
11322/*
11323 * Load guest's/host's msr at nested entry/exit.
11324 * return 0 for success, entry index for failure.
11325 */
11326static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11327{
11328 u32 i;
11329 struct vmx_msr_entry e;
11330 struct msr_data msr;
11331
11332 msr.host_initiated = false;
11333 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011334 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11335 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011336 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011337 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11338 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011339 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011340 }
11341 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011342 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011343 "%s check failed (%u, 0x%x, 0x%x)\n",
11344 __func__, i, e.index, e.reserved);
11345 goto fail;
11346 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011347 msr.index = e.index;
11348 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011349 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011350 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011351 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11352 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011353 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011354 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011355 }
11356 return 0;
11357fail:
11358 return i + 1;
11359}
11360
11361static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11362{
11363 u32 i;
11364 struct vmx_msr_entry e;
11365
11366 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011367 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011368 if (kvm_vcpu_read_guest(vcpu,
11369 gpa + i * sizeof(e),
11370 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011371 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011372 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11373 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011374 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011375 }
11376 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011377 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011378 "%s check failed (%u, 0x%x, 0x%x)\n",
11379 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011380 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011381 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011382 msr_info.host_initiated = false;
11383 msr_info.index = e.index;
11384 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011385 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011386 "%s cannot read MSR (%u, 0x%x)\n",
11387 __func__, i, e.index);
11388 return -EINVAL;
11389 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011390 if (kvm_vcpu_write_guest(vcpu,
11391 gpa + i * sizeof(e) +
11392 offsetof(struct vmx_msr_entry, value),
11393 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011394 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011395 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011396 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011397 return -EINVAL;
11398 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011399 }
11400 return 0;
11401}
11402
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011403static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11404{
11405 unsigned long invalid_mask;
11406
11407 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11408 return (val & invalid_mask) == 0;
11409}
11410
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011411/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011412 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11413 * emulating VM entry into a guest with EPT enabled.
11414 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11415 * is assigned to entry_failure_code on failure.
11416 */
11417static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011418 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011419{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011420 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011421 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011422 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11423 return 1;
11424 }
11425
11426 /*
11427 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11428 * must not be dereferenced.
11429 */
11430 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11431 !nested_ept) {
11432 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11433 *entry_failure_code = ENTRY_FAIL_PDPTE;
11434 return 1;
11435 }
11436 }
11437
11438 vcpu->arch.cr3 = cr3;
11439 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11440 }
11441
11442 kvm_mmu_reset_context(vcpu);
11443 return 0;
11444}
11445
Jim Mattson6514dc32018-04-26 16:09:12 -070011446static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011447{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011448 struct vcpu_vmx *vmx = to_vmx(vcpu);
11449
11450 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11451 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11452 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11453 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11454 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11455 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11456 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11457 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11458 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11459 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11460 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11461 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11462 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11463 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11464 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11465 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11466 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11467 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11468 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11469 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11470 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11471 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11472 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11473 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11474 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11475 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11476 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11477 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11478 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11479 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11480 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011481
11482 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11483 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11484 vmcs12->guest_pending_dbg_exceptions);
11485 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11486 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11487
11488 if (nested_cpu_has_xsaves(vmcs12))
11489 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11490 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11491
11492 if (cpu_has_vmx_posted_intr())
11493 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11494
11495 /*
11496 * Whether page-faults are trapped is determined by a combination of
11497 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11498 * If enable_ept, L0 doesn't care about page faults and we should
11499 * set all of these to L1's desires. However, if !enable_ept, L0 does
11500 * care about (at least some) page faults, and because it is not easy
11501 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11502 * to exit on each and every L2 page fault. This is done by setting
11503 * MASK=MATCH=0 and (see below) EB.PF=1.
11504 * Note that below we don't need special code to set EB.PF beyond the
11505 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11506 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11507 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11508 */
11509 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11510 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11511 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11512 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11513
11514 /* All VMFUNCs are currently emulated through L0 vmexits. */
11515 if (cpu_has_vmx_vmfunc())
11516 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11517
11518 if (cpu_has_vmx_apicv()) {
11519 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11520 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11521 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11522 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11523 }
11524
11525 /*
11526 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11527 * Some constant fields are set here by vmx_set_constant_host_state().
11528 * Other fields are different per CPU, and will be set later when
11529 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11530 */
11531 vmx_set_constant_host_state(vmx);
11532
11533 /*
11534 * Set the MSR load/store lists to match L0's settings.
11535 */
11536 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011537 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11538 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11539 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11540 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011541
11542 set_cr4_guest_host_mask(vmx);
11543
11544 if (vmx_mpx_supported())
11545 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11546
11547 if (enable_vpid) {
11548 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11549 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11550 else
11551 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11552 }
11553
11554 /*
11555 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11556 */
11557 if (enable_ept) {
11558 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11559 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11560 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11561 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11562 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011563
11564 if (cpu_has_vmx_msr_bitmap())
11565 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011566}
11567
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011568/*
11569 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11570 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011571 * 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 +030011572 * guest in a way that will both be appropriate to L1's requests, and our
11573 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11574 * function also has additional necessary side-effects, like setting various
11575 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011576 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11577 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011578 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011579static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011580 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011581{
11582 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011583 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011584
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011585 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011586 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011587 vmx->nested.dirty_vmcs12 = false;
11588 }
11589
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011590 /*
11591 * First, the fields that are shadowed. This must be kept in sync
11592 * with vmx_shadow_fields.h.
11593 */
11594
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011595 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011596 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011597 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011598 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11599 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011600
11601 /*
11602 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11603 * HOST_FS_BASE, HOST_GS_BASE.
11604 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011605
Jim Mattson6514dc32018-04-26 16:09:12 -070011606 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011607 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011608 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11609 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11610 } else {
11611 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11612 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11613 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011614 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011615 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11616 vmcs12->vm_entry_intr_info_field);
11617 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11618 vmcs12->vm_entry_exception_error_code);
11619 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11620 vmcs12->vm_entry_instruction_len);
11621 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11622 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011623 vmx->loaded_vmcs->nmi_known_unmasked =
11624 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011625 } else {
11626 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11627 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011628 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011629
Jan Kiszkaf4124502014-03-07 20:03:13 +010011630 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011631
Paolo Bonzini93140062016-07-06 13:23:51 +020011632 /* Preemption timer setting is only taken from vmcs01. */
11633 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11634 exec_control |= vmcs_config.pin_based_exec_ctrl;
11635 if (vmx->hv_deadline_tsc == -1)
11636 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11637
11638 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011639 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011640 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11641 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011642 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011643 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011644 }
Wincy Van705699a2015-02-03 23:58:17 +080011645
Jan Kiszkaf4124502014-03-07 20:03:13 +010011646 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011647
Jan Kiszkaf4124502014-03-07 20:03:13 +010011648 vmx->nested.preemption_timer_expired = false;
11649 if (nested_cpu_has_preemption_timer(vmcs12))
11650 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011651
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011652 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011653 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011654
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011655 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011656 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011657 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011658 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011659 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011660 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011661 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11662 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011663 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011664 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11665 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11666 ~SECONDARY_EXEC_ENABLE_PML;
11667 exec_control |= vmcs12_exec_ctrl;
11668 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011669
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011670 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011671 vmcs_write16(GUEST_INTR_STATUS,
11672 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011673
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011674 /*
11675 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11676 * nested_get_vmcs12_pages will either fix it up or
11677 * remove the VM execution control.
11678 */
11679 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11680 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11681
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011682 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11683 }
11684
Jim Mattson83bafef2016-10-04 10:48:38 -070011685 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011686 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11687 * entry, but only if the current (host) sp changed from the value
11688 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11689 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11690 * here we just force the write to happen on entry.
11691 */
11692 vmx->host_rsp = 0;
11693
11694 exec_control = vmx_exec_control(vmx); /* L0's desires */
11695 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11696 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11697 exec_control &= ~CPU_BASED_TPR_SHADOW;
11698 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011699
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011700 /*
11701 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11702 * nested_get_vmcs12_pages can't fix it up, the illegal value
11703 * will result in a VM entry failure.
11704 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011705 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011706 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011707 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011708 } else {
11709#ifdef CONFIG_X86_64
11710 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11711 CPU_BASED_CR8_STORE_EXITING;
11712#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011713 }
11714
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011715 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011716 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11717 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011718 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011719 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11720 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11721
11722 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11723
11724 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11725 * bitwise-or of what L1 wants to trap for L2, and what we want to
11726 * trap. Note that CR0.TS also needs updating - we do this later.
11727 */
11728 update_exception_bitmap(vcpu);
11729 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11730 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11731
Nadav Har'El8049d652013-08-05 11:07:06 +030011732 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11733 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11734 * bits are further modified by vmx_set_efer() below.
11735 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011736 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011737
11738 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11739 * emulated by vmx_set_efer(), below.
11740 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011741 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011742 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11743 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011744 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11745
Jim Mattson6514dc32018-04-26 16:09:12 -070011746 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011747 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011748 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011749 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011750 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011751 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011752 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011753
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011754 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11755
Peter Feinerc95ba922016-08-17 09:36:47 -070011756 if (kvm_has_tsc_control)
11757 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011758
11759 if (enable_vpid) {
11760 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011761 * There is no direct mapping between vpid02 and vpid12, the
11762 * vpid02 is per-vCPU for L0 and reused while the value of
11763 * vpid12 is changed w/ one invvpid during nested vmentry.
11764 * The vpid12 is allocated by L1 for L2, so it will not
11765 * influence global bitmap(for vpid01 and vpid02 allocation)
11766 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011767 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011768 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011769 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11770 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011771 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011772 }
11773 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011774 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011775 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011776 }
11777
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011778 if (enable_pml) {
11779 /*
11780 * Conceptually we want to copy the PML address and index from
11781 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11782 * since we always flush the log on each vmexit, this happens
11783 * to be equivalent to simply resetting the fields in vmcs02.
11784 */
11785 ASSERT(vmx->pml_pg);
11786 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11787 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11788 }
11789
Nadav Har'El155a97a2013-08-05 11:07:16 +030011790 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011791 if (nested_ept_init_mmu_context(vcpu)) {
11792 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11793 return 1;
11794 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011795 } else if (nested_cpu_has2(vmcs12,
11796 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011797 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011798 }
11799
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011800 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011801 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11802 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011803 * The CR0_READ_SHADOW is what L2 should have expected to read given
11804 * the specifications by L1; It's not enough to take
11805 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11806 * have more bits than L1 expected.
11807 */
11808 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11809 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11810
11811 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11812 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11813
Jim Mattson6514dc32018-04-26 16:09:12 -070011814 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011815 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011816 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11817 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11818 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11819 else
11820 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11821 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11822 vmx_set_efer(vcpu, vcpu->arch.efer);
11823
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011824 /*
11825 * Guest state is invalid and unrestricted guest is disabled,
11826 * which means L1 attempted VMEntry to L2 with invalid state.
11827 * Fail the VMEntry.
11828 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011829 if (vmx->emulation_required) {
11830 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011831 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011832 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011833
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011834 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011835 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011836 entry_failure_code))
11837 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011838
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011839 if (!enable_ept)
11840 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11841
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011842 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11843 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011844 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011845}
11846
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011847static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11848{
11849 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11850 nested_cpu_has_virtual_nmis(vmcs12))
11851 return -EINVAL;
11852
11853 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11854 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11855 return -EINVAL;
11856
11857 return 0;
11858}
11859
Jim Mattsonca0bde22016-11-30 12:03:46 -080011860static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11861{
11862 struct vcpu_vmx *vmx = to_vmx(vcpu);
11863
11864 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11865 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11866 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11867
Jim Mattson56a20512017-07-06 16:33:06 -070011868 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11869 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11870
Jim Mattsonca0bde22016-11-30 12:03:46 -080011871 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11872 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11873
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011874 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11875 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11876
Jim Mattson712b12d2017-08-24 13:24:47 -070011877 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11878 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11879
Jim Mattsonca0bde22016-11-30 12:03:46 -080011880 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11881 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11882
11883 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11884 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11885
Bandan Dasc5f983f2017-05-05 15:25:14 -040011886 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11887 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11888
Jim Mattsonca0bde22016-11-30 12:03:46 -080011889 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011890 vmx->nested.msrs.procbased_ctls_low,
11891 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011892 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11893 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011894 vmx->nested.msrs.secondary_ctls_low,
11895 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011896 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011897 vmx->nested.msrs.pinbased_ctls_low,
11898 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011899 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011900 vmx->nested.msrs.exit_ctls_low,
11901 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011902 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011903 vmx->nested.msrs.entry_ctls_low,
11904 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011905 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11906
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011907 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011908 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11909
Bandan Das41ab9372017-08-03 15:54:43 -040011910 if (nested_cpu_has_vmfunc(vmcs12)) {
11911 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011912 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011913 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11914
11915 if (nested_cpu_has_eptp_switching(vmcs12)) {
11916 if (!nested_cpu_has_ept(vmcs12) ||
11917 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11918 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11919 }
11920 }
Bandan Das27c42a12017-08-03 15:54:42 -040011921
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011922 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11923 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11924
Jim Mattsonca0bde22016-11-30 12:03:46 -080011925 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11926 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11927 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11928 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11929
Marc Orr04473782018-06-20 17:21:29 -070011930 /*
11931 * From the Intel SDM, volume 3:
11932 * Fields relevant to VM-entry event injection must be set properly.
11933 * These fields are the VM-entry interruption-information field, the
11934 * VM-entry exception error code, and the VM-entry instruction length.
11935 */
11936 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11937 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11938 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11939 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11940 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11941 bool should_have_error_code;
11942 bool urg = nested_cpu_has2(vmcs12,
11943 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11944 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11945
11946 /* VM-entry interruption-info field: interruption type */
11947 if (intr_type == INTR_TYPE_RESERVED ||
11948 (intr_type == INTR_TYPE_OTHER_EVENT &&
11949 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11950 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11951
11952 /* VM-entry interruption-info field: vector */
11953 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11954 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11955 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11956 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11957
11958 /* VM-entry interruption-info field: deliver error code */
11959 should_have_error_code =
11960 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11961 x86_exception_has_error_code(vector);
11962 if (has_error_code != should_have_error_code)
11963 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11964
11965 /* VM-entry exception error code */
11966 if (has_error_code &&
11967 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11968 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11969
11970 /* VM-entry interruption-info field: reserved bits */
11971 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11972 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11973
11974 /* VM-entry instruction length */
11975 switch (intr_type) {
11976 case INTR_TYPE_SOFT_EXCEPTION:
11977 case INTR_TYPE_SOFT_INTR:
11978 case INTR_TYPE_PRIV_SW_EXCEPTION:
11979 if ((vmcs12->vm_entry_instruction_len > 15) ||
11980 (vmcs12->vm_entry_instruction_len == 0 &&
11981 !nested_cpu_has_zero_length_injection(vcpu)))
11982 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11983 }
11984 }
11985
Jim Mattsonca0bde22016-11-30 12:03:46 -080011986 return 0;
11987}
11988
11989static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11990 u32 *exit_qual)
11991{
11992 bool ia32e;
11993
11994 *exit_qual = ENTRY_FAIL_DEFAULT;
11995
11996 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11997 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11998 return 1;
11999
12000 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
12001 vmcs12->vmcs_link_pointer != -1ull) {
12002 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12003 return 1;
12004 }
12005
12006 /*
12007 * If the load IA32_EFER VM-entry control is 1, the following checks
12008 * are performed on the field for the IA32_EFER MSR:
12009 * - Bits reserved in the IA32_EFER MSR must be 0.
12010 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12011 * the IA-32e mode guest VM-exit control. It must also be identical
12012 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12013 * CR0.PG) is 1.
12014 */
12015 if (to_vmx(vcpu)->nested.nested_run_pending &&
12016 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12017 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12018 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12019 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12020 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12021 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12022 return 1;
12023 }
12024
12025 /*
12026 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12027 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12028 * the values of the LMA and LME bits in the field must each be that of
12029 * the host address-space size VM-exit control.
12030 */
12031 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12032 ia32e = (vmcs12->vm_exit_controls &
12033 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12034 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12035 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12036 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12037 return 1;
12038 }
12039
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012040 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12041 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12042 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12043 return 1;
12044
Jim Mattsonca0bde22016-11-30 12:03:46 -080012045 return 0;
12046}
12047
Jim Mattson6514dc32018-04-26 16:09:12 -070012048static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080012049{
12050 struct vcpu_vmx *vmx = to_vmx(vcpu);
12051 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012052 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012053 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012054
Jim Mattson858e25c2016-11-30 12:03:47 -080012055 enter_guest_mode(vcpu);
12056
12057 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12058 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12059
Jim Mattsonde3a0022017-11-27 17:22:25 -060012060 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012061 vmx_segment_cache_clear(vmx);
12062
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012063 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12064 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12065
12066 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070012067 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012068 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012069
12070 nested_get_vmcs12_pages(vcpu, vmcs12);
12071
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012072 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson0b88abd2018-05-30 16:00:02 -070012073 exit_qual = nested_vmx_load_msr(vcpu,
12074 vmcs12->vm_entry_msr_load_addr,
12075 vmcs12->vm_entry_msr_load_count);
12076 if (exit_qual)
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012077 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012078
Jim Mattson858e25c2016-11-30 12:03:47 -080012079 /*
12080 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12081 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12082 * returned as far as L1 is concerned. It will only return (and set
12083 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12084 */
12085 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012086
12087fail:
12088 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12089 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12090 leave_guest_mode(vcpu);
12091 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12092 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
12093 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012094}
12095
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012096/*
12097 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12098 * for running an L2 nested guest.
12099 */
12100static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12101{
12102 struct vmcs12 *vmcs12;
12103 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012104 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012105 u32 exit_qual;
12106 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012107
Kyle Hueyeb277562016-11-29 12:40:39 -080012108 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012109 return 1;
12110
Kyle Hueyeb277562016-11-29 12:40:39 -080012111 if (!nested_vmx_check_vmcs12(vcpu))
12112 goto out;
12113
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012114 vmcs12 = get_vmcs12(vcpu);
12115
Abel Gordon012f83c2013-04-18 14:39:25 +030012116 if (enable_shadow_vmcs)
12117 copy_shadow_to_vmcs12(vmx);
12118
Nadav Har'El7c177932011-05-25 23:12:04 +030012119 /*
12120 * The nested entry process starts with enforcing various prerequisites
12121 * on vmcs12 as required by the Intel SDM, and act appropriately when
12122 * they fail: As the SDM explains, some conditions should cause the
12123 * instruction to fail, while others will cause the instruction to seem
12124 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12125 * To speed up the normal (success) code path, we should avoid checking
12126 * for misconfigurations which will anyway be caught by the processor
12127 * when using the merged vmcs02.
12128 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012129 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12130 nested_vmx_failValid(vcpu,
12131 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12132 goto out;
12133 }
12134
Nadav Har'El7c177932011-05-25 23:12:04 +030012135 if (vmcs12->launch_state == launch) {
12136 nested_vmx_failValid(vcpu,
12137 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12138 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012139 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012140 }
12141
Jim Mattsonca0bde22016-11-30 12:03:46 -080012142 ret = check_vmentry_prereqs(vcpu, vmcs12);
12143 if (ret) {
12144 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012145 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012146 }
12147
Nadav Har'El7c177932011-05-25 23:12:04 +030012148 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012149 * After this point, the trap flag no longer triggers a singlestep trap
12150 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12151 * This is not 100% correct; for performance reasons, we delegate most
12152 * of the checks on host state to the processor. If those fail,
12153 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012154 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012155 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012156
Jim Mattsonca0bde22016-11-30 12:03:46 -080012157 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12158 if (ret) {
12159 nested_vmx_entry_failure(vcpu, vmcs12,
12160 EXIT_REASON_INVALID_STATE, exit_qual);
12161 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012162 }
12163
12164 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012165 * We're finally done with prerequisite checking, and can start with
12166 * the nested entry.
12167 */
12168
Jim Mattson6514dc32018-04-26 16:09:12 -070012169 vmx->nested.nested_run_pending = 1;
12170 ret = enter_vmx_non_root_mode(vcpu);
12171 if (ret) {
12172 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012173 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070012174 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012175
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012176 /* Hide L1D cache contents from the nested guest. */
12177 vmx->vcpu.arch.l1tf_flush_l1d = true;
12178
Chao Gao135a06c2018-02-11 10:06:30 +080012179 /*
12180 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12181 * by event injection, halt vcpu.
12182 */
12183 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012184 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12185 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012186 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012187 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012188 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012189
12190out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012191 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012192}
12193
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012194/*
12195 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12196 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12197 * This function returns the new value we should put in vmcs12.guest_cr0.
12198 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12199 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12200 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12201 * didn't trap the bit, because if L1 did, so would L0).
12202 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12203 * been modified by L2, and L1 knows it. So just leave the old value of
12204 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12205 * isn't relevant, because if L0 traps this bit it can set it to anything.
12206 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12207 * changed these bits, and therefore they need to be updated, but L0
12208 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12209 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12210 */
12211static inline unsigned long
12212vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12213{
12214 return
12215 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12216 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12217 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12218 vcpu->arch.cr0_guest_owned_bits));
12219}
12220
12221static inline unsigned long
12222vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12223{
12224 return
12225 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12226 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12227 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12228 vcpu->arch.cr4_guest_owned_bits));
12229}
12230
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012231static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12232 struct vmcs12 *vmcs12)
12233{
12234 u32 idt_vectoring;
12235 unsigned int nr;
12236
Wanpeng Li664f8e22017-08-24 03:35:09 -070012237 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012238 nr = vcpu->arch.exception.nr;
12239 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12240
12241 if (kvm_exception_is_soft(nr)) {
12242 vmcs12->vm_exit_instruction_len =
12243 vcpu->arch.event_exit_inst_len;
12244 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12245 } else
12246 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12247
12248 if (vcpu->arch.exception.has_error_code) {
12249 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12250 vmcs12->idt_vectoring_error_code =
12251 vcpu->arch.exception.error_code;
12252 }
12253
12254 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012255 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012256 vmcs12->idt_vectoring_info_field =
12257 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012258 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012259 nr = vcpu->arch.interrupt.nr;
12260 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12261
12262 if (vcpu->arch.interrupt.soft) {
12263 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12264 vmcs12->vm_entry_instruction_len =
12265 vcpu->arch.event_exit_inst_len;
12266 } else
12267 idt_vectoring |= INTR_TYPE_EXT_INTR;
12268
12269 vmcs12->idt_vectoring_info_field = idt_vectoring;
12270 }
12271}
12272
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012273static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12274{
12275 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012276 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012277 bool block_nested_events =
12278 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012279
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012280 if (vcpu->arch.exception.pending &&
12281 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012282 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012283 return -EBUSY;
12284 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012285 return 0;
12286 }
12287
Jan Kiszkaf4124502014-03-07 20:03:13 +010012288 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12289 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012290 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012291 return -EBUSY;
12292 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12293 return 0;
12294 }
12295
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012296 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012297 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012298 return -EBUSY;
12299 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12300 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12301 INTR_INFO_VALID_MASK, 0);
12302 /*
12303 * The NMI-triggered VM exit counts as injection:
12304 * clear this one and block further NMIs.
12305 */
12306 vcpu->arch.nmi_pending = 0;
12307 vmx_set_nmi_mask(vcpu, true);
12308 return 0;
12309 }
12310
12311 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12312 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012313 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012314 return -EBUSY;
12315 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012316 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012317 }
12318
David Hildenbrand6342c502017-01-25 11:58:58 +010012319 vmx_complete_nested_posted_interrupt(vcpu);
12320 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012321}
12322
Jan Kiszkaf4124502014-03-07 20:03:13 +010012323static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12324{
12325 ktime_t remaining =
12326 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12327 u64 value;
12328
12329 if (ktime_to_ns(remaining) <= 0)
12330 return 0;
12331
12332 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12333 do_div(value, 1000000);
12334 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12335}
12336
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012337/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012338 * Update the guest state fields of vmcs12 to reflect changes that
12339 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12340 * VM-entry controls is also updated, since this is really a guest
12341 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012342 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012343static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012344{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012345 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12346 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12347
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012348 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12349 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12350 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12351
12352 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12353 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12354 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12355 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12356 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12357 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12358 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12359 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12360 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12361 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12362 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12363 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12364 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12365 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12366 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12367 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12368 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12369 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12370 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12371 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12372 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12373 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12374 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12375 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12376 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12377 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12378 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12379 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12380 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12381 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12382 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12383 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12384 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12385 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12386 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12387 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12388
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012389 vmcs12->guest_interruptibility_info =
12390 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12391 vmcs12->guest_pending_dbg_exceptions =
12392 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012393 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12394 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12395 else
12396 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012397
Jan Kiszkaf4124502014-03-07 20:03:13 +010012398 if (nested_cpu_has_preemption_timer(vmcs12)) {
12399 if (vmcs12->vm_exit_controls &
12400 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12401 vmcs12->vmx_preemption_timer_value =
12402 vmx_get_preemption_timer_value(vcpu);
12403 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12404 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012405
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012406 /*
12407 * In some cases (usually, nested EPT), L2 is allowed to change its
12408 * own CR3 without exiting. If it has changed it, we must keep it.
12409 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12410 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12411 *
12412 * Additionally, restore L2's PDPTR to vmcs12.
12413 */
12414 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012415 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012416 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12417 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12418 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12419 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12420 }
12421
Jim Mattsond281e132017-06-01 12:44:46 -070012422 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012423
Wincy Van608406e2015-02-03 23:57:51 +080012424 if (nested_cpu_has_vid(vmcs12))
12425 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12426
Jan Kiszkac18911a2013-03-13 16:06:41 +010012427 vmcs12->vm_entry_controls =
12428 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012429 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012430
Jan Kiszka2996fca2014-06-16 13:59:43 +020012431 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12432 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12433 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12434 }
12435
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012436 /* TODO: These cannot have changed unless we have MSR bitmaps and
12437 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012438 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012439 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012440 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12441 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012442 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12443 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12444 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012445 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012446 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012447}
12448
12449/*
12450 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12451 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12452 * and this function updates it to reflect the changes to the guest state while
12453 * L2 was running (and perhaps made some exits which were handled directly by L0
12454 * without going back to L1), and to reflect the exit reason.
12455 * Note that we do not have to copy here all VMCS fields, just those that
12456 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12457 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12458 * which already writes to vmcs12 directly.
12459 */
12460static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12461 u32 exit_reason, u32 exit_intr_info,
12462 unsigned long exit_qualification)
12463{
12464 /* update guest state fields: */
12465 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012466
12467 /* update exit information fields: */
12468
Jan Kiszka533558b2014-01-04 18:47:20 +010012469 vmcs12->vm_exit_reason = exit_reason;
12470 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012471 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012472
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012473 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012474 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12475 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12476
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012477 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012478 vmcs12->launch_state = 1;
12479
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012480 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12481 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012482 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012483
12484 /*
12485 * Transfer the event that L0 or L1 may wanted to inject into
12486 * L2 to IDT_VECTORING_INFO_FIELD.
12487 */
12488 vmcs12_save_pending_event(vcpu, vmcs12);
12489 }
12490
12491 /*
12492 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12493 * preserved above and would only end up incorrectly in L1.
12494 */
12495 vcpu->arch.nmi_injected = false;
12496 kvm_clear_exception_queue(vcpu);
12497 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012498}
12499
Wanpeng Li5af41572017-11-05 16:54:49 -080012500static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12501 struct vmcs12 *vmcs12)
12502{
12503 u32 entry_failure_code;
12504
12505 nested_ept_uninit_mmu_context(vcpu);
12506
12507 /*
12508 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12509 * couldn't have changed.
12510 */
12511 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12512 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12513
12514 if (!enable_ept)
12515 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12516}
12517
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012518/*
12519 * A part of what we need to when the nested L2 guest exits and we want to
12520 * run its L1 parent, is to reset L1's guest state to the host state specified
12521 * in vmcs12.
12522 * This function is to be called not only on normal nested exit, but also on
12523 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12524 * Failures During or After Loading Guest State").
12525 * This function should be called when the active VMCS is L1's (vmcs01).
12526 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012527static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12528 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012529{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012530 struct kvm_segment seg;
12531
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012532 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12533 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012534 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012535 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12536 else
12537 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12538 vmx_set_efer(vcpu, vcpu->arch.efer);
12539
12540 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12541 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012542 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012543 /*
12544 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012545 * actually changed, because vmx_set_cr0 refers to efer set above.
12546 *
12547 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12548 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012549 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012550 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012551 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012552
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012553 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012554 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012555 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012556
Wanpeng Li5af41572017-11-05 16:54:49 -080012557 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012558
Liran Alon6f1e03b2018-05-22 17:16:14 +030012559 /*
12560 * If vmcs01 don't use VPID, CPU flushes TLB on every
12561 * VMEntry/VMExit. Thus, no need to flush TLB.
12562 *
12563 * If vmcs12 uses VPID, TLB entries populated by L2 are
12564 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12565 * with vmx->vpid. Thus, no need to flush TLB.
12566 *
12567 * Therefore, flush TLB only in case vmcs01 uses VPID and
12568 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12569 * are both tagged with vmx->vpid.
12570 */
12571 if (enable_vpid &&
12572 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012573 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012574 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012575
12576 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12577 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12578 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12579 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12580 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012581 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12582 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012583
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012584 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12585 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12586 vmcs_write64(GUEST_BNDCFGS, 0);
12587
Jan Kiszka44811c02013-08-04 17:17:27 +020012588 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012589 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012590 vcpu->arch.pat = vmcs12->host_ia32_pat;
12591 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012592 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12593 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12594 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012595
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012596 /* Set L1 segment info according to Intel SDM
12597 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12598 seg = (struct kvm_segment) {
12599 .base = 0,
12600 .limit = 0xFFFFFFFF,
12601 .selector = vmcs12->host_cs_selector,
12602 .type = 11,
12603 .present = 1,
12604 .s = 1,
12605 .g = 1
12606 };
12607 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12608 seg.l = 1;
12609 else
12610 seg.db = 1;
12611 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12612 seg = (struct kvm_segment) {
12613 .base = 0,
12614 .limit = 0xFFFFFFFF,
12615 .type = 3,
12616 .present = 1,
12617 .s = 1,
12618 .db = 1,
12619 .g = 1
12620 };
12621 seg.selector = vmcs12->host_ds_selector;
12622 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12623 seg.selector = vmcs12->host_es_selector;
12624 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12625 seg.selector = vmcs12->host_ss_selector;
12626 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12627 seg.selector = vmcs12->host_fs_selector;
12628 seg.base = vmcs12->host_fs_base;
12629 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12630 seg.selector = vmcs12->host_gs_selector;
12631 seg.base = vmcs12->host_gs_base;
12632 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12633 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012634 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012635 .limit = 0x67,
12636 .selector = vmcs12->host_tr_selector,
12637 .type = 11,
12638 .present = 1
12639 };
12640 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12641
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012642 kvm_set_dr(vcpu, 7, 0x400);
12643 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012644
Wincy Van3af18d92015-02-03 23:49:31 +080012645 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012646 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012647
Wincy Vanff651cb2014-12-11 08:52:58 +030012648 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12649 vmcs12->vm_exit_msr_load_count))
12650 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012651}
12652
12653/*
12654 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12655 * and modify vmcs12 to make it see what it would expect to see there if
12656 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12657 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012658static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12659 u32 exit_intr_info,
12660 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012661{
12662 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012663 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12664
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012665 /* trying to cancel vmlaunch/vmresume is a bug */
12666 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12667
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012668 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012669 * The only expected VM-instruction error is "VM entry with
12670 * invalid control field(s)." Anything else indicates a
12671 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012672 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012673 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12674 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12675
12676 leave_guest_mode(vcpu);
12677
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012678 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12679 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12680
Jim Mattson4f350c62017-09-14 16:31:44 -070012681 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012682 if (exit_reason == -1)
12683 sync_vmcs12(vcpu, vmcs12);
12684 else
12685 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12686 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012687
12688 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12689 vmcs12->vm_exit_msr_store_count))
12690 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012691 }
12692
Jim Mattson4f350c62017-09-14 16:31:44 -070012693 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012694 vm_entry_controls_reset_shadow(vmx);
12695 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012696 vmx_segment_cache_clear(vmx);
12697
Paolo Bonzini93140062016-07-06 13:23:51 +020012698 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012699 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12700 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012701 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012702 if (vmx->hv_deadline_tsc == -1)
12703 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12704 PIN_BASED_VMX_PREEMPTION_TIMER);
12705 else
12706 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12707 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012708 if (kvm_has_tsc_control)
12709 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012710
Jim Mattson8d860bb2018-05-09 16:56:05 -040012711 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12712 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12713 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012714 } else if (!nested_cpu_has_ept(vmcs12) &&
12715 nested_cpu_has2(vmcs12,
12716 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012717 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012718 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012719
12720 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12721 vmx->host_rsp = 0;
12722
12723 /* Unpin physical memory we referred to in vmcs02 */
12724 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012725 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012726 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012727 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012728 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012729 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012730 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012731 }
Wincy Van705699a2015-02-03 23:58:17 +080012732 if (vmx->nested.pi_desc_page) {
12733 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012734 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012735 vmx->nested.pi_desc_page = NULL;
12736 vmx->nested.pi_desc = NULL;
12737 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012738
12739 /*
Tang Chen38b99172014-09-24 15:57:54 +080012740 * We are now running in L2, mmu_notifier will force to reload the
12741 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12742 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012743 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012744
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012745 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012746 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012747
12748 /* in case we halted in L2 */
12749 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012750
12751 if (likely(!vmx->fail)) {
12752 /*
12753 * TODO: SDM says that with acknowledge interrupt on
12754 * exit, bit 31 of the VM-exit interrupt information
12755 * (valid interrupt) is always set to 1 on
12756 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12757 * need kvm_cpu_has_interrupt(). See the commit
12758 * message for details.
12759 */
12760 if (nested_exit_intr_ack_set(vcpu) &&
12761 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12762 kvm_cpu_has_interrupt(vcpu)) {
12763 int irq = kvm_cpu_get_interrupt(vcpu);
12764 WARN_ON(irq < 0);
12765 vmcs12->vm_exit_intr_info = irq |
12766 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12767 }
12768
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012769 if (exit_reason != -1)
12770 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12771 vmcs12->exit_qualification,
12772 vmcs12->idt_vectoring_info_field,
12773 vmcs12->vm_exit_intr_info,
12774 vmcs12->vm_exit_intr_error_code,
12775 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012776
12777 load_vmcs12_host_state(vcpu, vmcs12);
12778
12779 return;
12780 }
12781
12782 /*
12783 * After an early L2 VM-entry failure, we're now back
12784 * in L1 which thinks it just finished a VMLAUNCH or
12785 * VMRESUME instruction, so we need to set the failure
12786 * flag and the VM-instruction error field of the VMCS
12787 * accordingly.
12788 */
12789 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012790
12791 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12792
Jim Mattson4f350c62017-09-14 16:31:44 -070012793 /*
12794 * The emulated instruction was already skipped in
12795 * nested_vmx_run, but the updated RIP was never
12796 * written back to the vmcs01.
12797 */
12798 skip_emulated_instruction(vcpu);
12799 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012800}
12801
Nadav Har'El7c177932011-05-25 23:12:04 +030012802/*
Jan Kiszka42124922014-01-04 18:47:19 +010012803 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12804 */
12805static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12806{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012807 if (is_guest_mode(vcpu)) {
12808 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012809 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012810 }
Jan Kiszka42124922014-01-04 18:47:19 +010012811 free_nested(to_vmx(vcpu));
12812}
12813
12814/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012815 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12816 * 23.7 "VM-entry failures during or after loading guest state" (this also
12817 * lists the acceptable exit-reason and exit-qualification parameters).
12818 * It should only be called before L2 actually succeeded to run, and when
12819 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12820 */
12821static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12822 struct vmcs12 *vmcs12,
12823 u32 reason, unsigned long qualification)
12824{
12825 load_vmcs12_host_state(vcpu, vmcs12);
12826 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12827 vmcs12->exit_qualification = qualification;
12828 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012829 if (enable_shadow_vmcs)
12830 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012831}
12832
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012833static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12834 struct x86_instruction_info *info,
12835 enum x86_intercept_stage stage)
12836{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012837 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12838 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12839
12840 /*
12841 * RDPID causes #UD if disabled through secondary execution controls.
12842 * Because it is marked as EmulateOnUD, we need to intercept it here.
12843 */
12844 if (info->intercept == x86_intercept_rdtscp &&
12845 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12846 ctxt->exception.vector = UD_VECTOR;
12847 ctxt->exception.error_code_valid = false;
12848 return X86EMUL_PROPAGATE_FAULT;
12849 }
12850
12851 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012852 return X86EMUL_CONTINUE;
12853}
12854
Yunhong Jiang64672c92016-06-13 14:19:59 -070012855#ifdef CONFIG_X86_64
12856/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12857static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12858 u64 divisor, u64 *result)
12859{
12860 u64 low = a << shift, high = a >> (64 - shift);
12861
12862 /* To avoid the overflow on divq */
12863 if (high >= divisor)
12864 return 1;
12865
12866 /* Low hold the result, high hold rem which is discarded */
12867 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12868 "rm" (divisor), "0" (low), "1" (high));
12869 *result = low;
12870
12871 return 0;
12872}
12873
12874static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12875{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012876 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012877 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012878
12879 if (kvm_mwait_in_guest(vcpu->kvm))
12880 return -EOPNOTSUPP;
12881
12882 vmx = to_vmx(vcpu);
12883 tscl = rdtsc();
12884 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12885 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012886 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12887
12888 if (delta_tsc > lapic_timer_advance_cycles)
12889 delta_tsc -= lapic_timer_advance_cycles;
12890 else
12891 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012892
12893 /* Convert to host delta tsc if tsc scaling is enabled */
12894 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12895 u64_shl_div_u64(delta_tsc,
12896 kvm_tsc_scaling_ratio_frac_bits,
12897 vcpu->arch.tsc_scaling_ratio,
12898 &delta_tsc))
12899 return -ERANGE;
12900
12901 /*
12902 * If the delta tsc can't fit in the 32 bit after the multi shift,
12903 * we can't use the preemption timer.
12904 * It's possible that it fits on later vmentries, but checking
12905 * on every vmentry is costly so we just use an hrtimer.
12906 */
12907 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12908 return -ERANGE;
12909
12910 vmx->hv_deadline_tsc = tscl + delta_tsc;
12911 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12912 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012913
12914 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012915}
12916
12917static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12918{
12919 struct vcpu_vmx *vmx = to_vmx(vcpu);
12920 vmx->hv_deadline_tsc = -1;
12921 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12922 PIN_BASED_VMX_PREEMPTION_TIMER);
12923}
12924#endif
12925
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012926static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012927{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012928 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012929 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012930}
12931
Kai Huang843e4332015-01-28 10:54:28 +080012932static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12933 struct kvm_memory_slot *slot)
12934{
12935 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12936 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12937}
12938
12939static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12940 struct kvm_memory_slot *slot)
12941{
12942 kvm_mmu_slot_set_dirty(kvm, slot);
12943}
12944
12945static void vmx_flush_log_dirty(struct kvm *kvm)
12946{
12947 kvm_flush_pml_buffers(kvm);
12948}
12949
Bandan Dasc5f983f2017-05-05 15:25:14 -040012950static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12951{
12952 struct vmcs12 *vmcs12;
12953 struct vcpu_vmx *vmx = to_vmx(vcpu);
12954 gpa_t gpa;
12955 struct page *page = NULL;
12956 u64 *pml_address;
12957
12958 if (is_guest_mode(vcpu)) {
12959 WARN_ON_ONCE(vmx->nested.pml_full);
12960
12961 /*
12962 * Check if PML is enabled for the nested guest.
12963 * Whether eptp bit 6 is set is already checked
12964 * as part of A/D emulation.
12965 */
12966 vmcs12 = get_vmcs12(vcpu);
12967 if (!nested_cpu_has_pml(vmcs12))
12968 return 0;
12969
Dan Carpenter47698862017-05-10 22:43:17 +030012970 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012971 vmx->nested.pml_full = true;
12972 return 1;
12973 }
12974
12975 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12976
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012977 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12978 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012979 return 0;
12980
12981 pml_address = kmap(page);
12982 pml_address[vmcs12->guest_pml_index--] = gpa;
12983 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012984 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012985 }
12986
12987 return 0;
12988}
12989
Kai Huang843e4332015-01-28 10:54:28 +080012990static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12991 struct kvm_memory_slot *memslot,
12992 gfn_t offset, unsigned long mask)
12993{
12994 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12995}
12996
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012997static void __pi_post_block(struct kvm_vcpu *vcpu)
12998{
12999 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13000 struct pi_desc old, new;
13001 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013002
13003 do {
13004 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013005 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13006 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013007
13008 dest = cpu_physical_id(vcpu->cpu);
13009
13010 if (x2apic_enabled())
13011 new.ndst = dest;
13012 else
13013 new.ndst = (dest << 8) & 0xFF00;
13014
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013015 /* set 'NV' to 'notification vector' */
13016 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013017 } while (cmpxchg64(&pi_desc->control, old.control,
13018 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013019
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013020 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13021 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013022 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013023 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013024 vcpu->pre_pcpu = -1;
13025 }
13026}
13027
Feng Wuefc64402015-09-18 22:29:51 +080013028/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013029 * This routine does the following things for vCPU which is going
13030 * to be blocked if VT-d PI is enabled.
13031 * - Store the vCPU to the wakeup list, so when interrupts happen
13032 * we can find the right vCPU to wake up.
13033 * - Change the Posted-interrupt descriptor as below:
13034 * 'NDST' <-- vcpu->pre_pcpu
13035 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13036 * - If 'ON' is set during this process, which means at least one
13037 * interrupt is posted for this vCPU, we cannot block it, in
13038 * this case, return 1, otherwise, return 0.
13039 *
13040 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013041static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013042{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013043 unsigned int dest;
13044 struct pi_desc old, new;
13045 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13046
13047 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013048 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13049 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013050 return 0;
13051
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013052 WARN_ON(irqs_disabled());
13053 local_irq_disable();
13054 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13055 vcpu->pre_pcpu = vcpu->cpu;
13056 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13057 list_add_tail(&vcpu->blocked_vcpu_list,
13058 &per_cpu(blocked_vcpu_on_cpu,
13059 vcpu->pre_pcpu));
13060 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13061 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013062
13063 do {
13064 old.control = new.control = pi_desc->control;
13065
Feng Wubf9f6ac2015-09-18 22:29:55 +080013066 WARN((pi_desc->sn == 1),
13067 "Warning: SN field of posted-interrupts "
13068 "is set before blocking\n");
13069
13070 /*
13071 * Since vCPU can be preempted during this process,
13072 * vcpu->cpu could be different with pre_pcpu, we
13073 * need to set pre_pcpu as the destination of wakeup
13074 * notification event, then we can find the right vCPU
13075 * to wakeup in wakeup handler if interrupts happen
13076 * when the vCPU is in blocked state.
13077 */
13078 dest = cpu_physical_id(vcpu->pre_pcpu);
13079
13080 if (x2apic_enabled())
13081 new.ndst = dest;
13082 else
13083 new.ndst = (dest << 8) & 0xFF00;
13084
13085 /* set 'NV' to 'wakeup vector' */
13086 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013087 } while (cmpxchg64(&pi_desc->control, old.control,
13088 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013089
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013090 /* We should not block the vCPU if an interrupt is posted for it. */
13091 if (pi_test_on(pi_desc) == 1)
13092 __pi_post_block(vcpu);
13093
13094 local_irq_enable();
13095 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013096}
13097
Yunhong Jiangbc225122016-06-13 14:19:58 -070013098static int vmx_pre_block(struct kvm_vcpu *vcpu)
13099{
13100 if (pi_pre_block(vcpu))
13101 return 1;
13102
Yunhong Jiang64672c92016-06-13 14:19:59 -070013103 if (kvm_lapic_hv_timer_in_use(vcpu))
13104 kvm_lapic_switch_to_sw_timer(vcpu);
13105
Yunhong Jiangbc225122016-06-13 14:19:58 -070013106 return 0;
13107}
13108
13109static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013110{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013111 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013112 return;
13113
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013114 WARN_ON(irqs_disabled());
13115 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013116 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013117 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013118}
13119
Yunhong Jiangbc225122016-06-13 14:19:58 -070013120static void vmx_post_block(struct kvm_vcpu *vcpu)
13121{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013122 if (kvm_x86_ops->set_hv_timer)
13123 kvm_lapic_switch_to_hv_timer(vcpu);
13124
Yunhong Jiangbc225122016-06-13 14:19:58 -070013125 pi_post_block(vcpu);
13126}
13127
Feng Wubf9f6ac2015-09-18 22:29:55 +080013128/*
Feng Wuefc64402015-09-18 22:29:51 +080013129 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13130 *
13131 * @kvm: kvm
13132 * @host_irq: host irq of the interrupt
13133 * @guest_irq: gsi of the interrupt
13134 * @set: set or unset PI
13135 * returns 0 on success, < 0 on failure
13136 */
13137static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13138 uint32_t guest_irq, bool set)
13139{
13140 struct kvm_kernel_irq_routing_entry *e;
13141 struct kvm_irq_routing_table *irq_rt;
13142 struct kvm_lapic_irq irq;
13143 struct kvm_vcpu *vcpu;
13144 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013145 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013146
13147 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013148 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13149 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013150 return 0;
13151
13152 idx = srcu_read_lock(&kvm->irq_srcu);
13153 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013154 if (guest_irq >= irq_rt->nr_rt_entries ||
13155 hlist_empty(&irq_rt->map[guest_irq])) {
13156 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13157 guest_irq, irq_rt->nr_rt_entries);
13158 goto out;
13159 }
Feng Wuefc64402015-09-18 22:29:51 +080013160
13161 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13162 if (e->type != KVM_IRQ_ROUTING_MSI)
13163 continue;
13164 /*
13165 * VT-d PI cannot support posting multicast/broadcast
13166 * interrupts to a vCPU, we still use interrupt remapping
13167 * for these kind of interrupts.
13168 *
13169 * For lowest-priority interrupts, we only support
13170 * those with single CPU as the destination, e.g. user
13171 * configures the interrupts via /proc/irq or uses
13172 * irqbalance to make the interrupts single-CPU.
13173 *
13174 * We will support full lowest-priority interrupt later.
13175 */
13176
Radim Krčmář371313132016-07-12 22:09:27 +020013177 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013178 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13179 /*
13180 * Make sure the IRTE is in remapped mode if
13181 * we don't handle it in posted mode.
13182 */
13183 ret = irq_set_vcpu_affinity(host_irq, NULL);
13184 if (ret < 0) {
13185 printk(KERN_INFO
13186 "failed to back to remapped mode, irq: %u\n",
13187 host_irq);
13188 goto out;
13189 }
13190
Feng Wuefc64402015-09-18 22:29:51 +080013191 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013192 }
Feng Wuefc64402015-09-18 22:29:51 +080013193
13194 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13195 vcpu_info.vector = irq.vector;
13196
hu huajun2698d822018-04-11 15:16:40 +080013197 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013198 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13199
13200 if (set)
13201 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013202 else
Feng Wuefc64402015-09-18 22:29:51 +080013203 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013204
13205 if (ret < 0) {
13206 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13207 __func__);
13208 goto out;
13209 }
13210 }
13211
13212 ret = 0;
13213out:
13214 srcu_read_unlock(&kvm->irq_srcu, idx);
13215 return ret;
13216}
13217
Ashok Rajc45dcc72016-06-22 14:59:56 +080013218static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13219{
13220 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13221 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13222 FEATURE_CONTROL_LMCE;
13223 else
13224 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13225 ~FEATURE_CONTROL_LMCE;
13226}
13227
Ladi Prosek72d7b372017-10-11 16:54:41 +020013228static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13229{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013230 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13231 if (to_vmx(vcpu)->nested.nested_run_pending)
13232 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013233 return 1;
13234}
13235
Ladi Prosek0234bf82017-10-11 16:54:40 +020013236static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13237{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013238 struct vcpu_vmx *vmx = to_vmx(vcpu);
13239
13240 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13241 if (vmx->nested.smm.guest_mode)
13242 nested_vmx_vmexit(vcpu, -1, 0, 0);
13243
13244 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13245 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013246 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013247 return 0;
13248}
13249
13250static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13251{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013252 struct vcpu_vmx *vmx = to_vmx(vcpu);
13253 int ret;
13254
13255 if (vmx->nested.smm.vmxon) {
13256 vmx->nested.vmxon = true;
13257 vmx->nested.smm.vmxon = false;
13258 }
13259
13260 if (vmx->nested.smm.guest_mode) {
13261 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013262 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013263 vcpu->arch.hflags |= HF_SMM_MASK;
13264 if (ret)
13265 return ret;
13266
13267 vmx->nested.smm.guest_mode = false;
13268 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013269 return 0;
13270}
13271
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013272static int enable_smi_window(struct kvm_vcpu *vcpu)
13273{
13274 return 0;
13275}
13276
Kees Cook404f6aa2016-08-08 16:29:06 -070013277static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013278 .cpu_has_kvm_support = cpu_has_kvm_support,
13279 .disabled_by_bios = vmx_disabled_by_bios,
13280 .hardware_setup = hardware_setup,
13281 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013282 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013283 .hardware_enable = hardware_enable,
13284 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013285 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013286 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013287
Wanpeng Lib31c1142018-03-12 04:53:04 -070013288 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013289 .vm_alloc = vmx_vm_alloc,
13290 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013291
Avi Kivity6aa8b732006-12-10 02:21:36 -080013292 .vcpu_create = vmx_create_vcpu,
13293 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013294 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013295
Avi Kivity04d2cc72007-09-10 18:10:54 +030013296 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013297 .vcpu_load = vmx_vcpu_load,
13298 .vcpu_put = vmx_vcpu_put,
13299
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013300 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013301 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013302 .get_msr = vmx_get_msr,
13303 .set_msr = vmx_set_msr,
13304 .get_segment_base = vmx_get_segment_base,
13305 .get_segment = vmx_get_segment,
13306 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013307 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013308 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013309 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013310 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013311 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013312 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013313 .set_cr3 = vmx_set_cr3,
13314 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013315 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013316 .get_idt = vmx_get_idt,
13317 .set_idt = vmx_set_idt,
13318 .get_gdt = vmx_get_gdt,
13319 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013320 .get_dr6 = vmx_get_dr6,
13321 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013322 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013323 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013324 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013325 .get_rflags = vmx_get_rflags,
13326 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013327
Avi Kivity6aa8b732006-12-10 02:21:36 -080013328 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013329
Avi Kivity6aa8b732006-12-10 02:21:36 -080013330 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013331 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013332 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013333 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13334 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013335 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013336 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013337 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013338 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013339 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013340 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013341 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013342 .get_nmi_mask = vmx_get_nmi_mask,
13343 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013344 .enable_nmi_window = enable_nmi_window,
13345 .enable_irq_window = enable_irq_window,
13346 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013347 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013348 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013349 .get_enable_apicv = vmx_get_enable_apicv,
13350 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013351 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013352 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013353 .hwapic_irr_update = vmx_hwapic_irr_update,
13354 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013355 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13356 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013357
Izik Eiduscbc94022007-10-25 00:29:55 +020013358 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013359 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013360 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013361 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013362
Avi Kivity586f9602010-11-18 13:09:54 +020013363 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013364
Sheng Yang17cc3932010-01-05 19:02:27 +080013365 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013366
13367 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013368
13369 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013370 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013371
13372 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013373
13374 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013375
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013376 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013377 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013378
13379 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013380
13381 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013382 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013383 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013384 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013385 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013386
13387 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013388
13389 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013390
13391 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13392 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13393 .flush_log_dirty = vmx_flush_log_dirty,
13394 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013395 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013396
Feng Wubf9f6ac2015-09-18 22:29:55 +080013397 .pre_block = vmx_pre_block,
13398 .post_block = vmx_post_block,
13399
Wei Huang25462f72015-06-19 15:45:05 +020013400 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013401
13402 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013403
13404#ifdef CONFIG_X86_64
13405 .set_hv_timer = vmx_set_hv_timer,
13406 .cancel_hv_timer = vmx_cancel_hv_timer,
13407#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013408
13409 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013410
Ladi Prosek72d7b372017-10-11 16:54:41 +020013411 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013412 .pre_enter_smm = vmx_pre_enter_smm,
13413 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013414 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013415};
13416
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013417static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013418{
13419 if (vmx_l1d_flush_pages) {
13420 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13421 vmx_l1d_flush_pages = NULL;
13422 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013423 /* Restore state so sysfs ignores VMX */
13424 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013425}
13426
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013427static void vmx_exit(void)
13428{
13429#ifdef CONFIG_KEXEC_CORE
13430 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13431 synchronize_rcu();
13432#endif
13433
13434 kvm_exit();
13435
13436#if IS_ENABLED(CONFIG_HYPERV)
13437 if (static_branch_unlikely(&enable_evmcs)) {
13438 int cpu;
13439 struct hv_vp_assist_page *vp_ap;
13440 /*
13441 * Reset everything to support using non-enlightened VMCS
13442 * access later (e.g. when we reload the module with
13443 * enlightened_vmcs=0)
13444 */
13445 for_each_online_cpu(cpu) {
13446 vp_ap = hv_get_vp_assist_page(cpu);
13447
13448 if (!vp_ap)
13449 continue;
13450
13451 vp_ap->current_nested_vmcs = 0;
13452 vp_ap->enlighten_vmentry = 0;
13453 }
13454
13455 static_branch_disable(&enable_evmcs);
13456 }
13457#endif
13458 vmx_cleanup_l1d_flush();
13459}
13460module_exit(vmx_exit);
13461
Avi Kivity6aa8b732006-12-10 02:21:36 -080013462static int __init vmx_init(void)
13463{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013464 int r;
13465
13466#if IS_ENABLED(CONFIG_HYPERV)
13467 /*
13468 * Enlightened VMCS usage should be recommended and the host needs
13469 * to support eVMCS v1 or above. We can also disable eVMCS support
13470 * with module parameter.
13471 */
13472 if (enlightened_vmcs &&
13473 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13474 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13475 KVM_EVMCS_VERSION) {
13476 int cpu;
13477
13478 /* Check that we have assist pages on all online CPUs */
13479 for_each_online_cpu(cpu) {
13480 if (!hv_get_vp_assist_page(cpu)) {
13481 enlightened_vmcs = false;
13482 break;
13483 }
13484 }
13485
13486 if (enlightened_vmcs) {
13487 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13488 static_branch_enable(&enable_evmcs);
13489 }
13490 } else {
13491 enlightened_vmcs = false;
13492 }
13493#endif
13494
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013495 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13496 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013497 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013498 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013499
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013500 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013501 * Must be called after kvm_init() so enable_ept is properly set
13502 * up. Hand the parameter mitigation value in which was stored in
13503 * the pre module init parser. If no parameter was given, it will
13504 * contain 'auto' which will be turned into the default 'cond'
13505 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013506 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013507 if (boot_cpu_has(X86_BUG_L1TF)) {
13508 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13509 if (r) {
13510 vmx_exit();
13511 return r;
13512 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013513 }
13514
Dave Young2965faa2015-09-09 15:38:55 -070013515#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013516 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13517 crash_vmclear_local_loaded_vmcss);
13518#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013519 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013520
He, Qingfdef3ad2007-04-30 09:45:24 +030013521 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013522}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013523module_init(vmx_init);