blob: 57379c88fcbd076e9b075b4d679d74834a044f81 [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
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100124
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
Sean Christopherson3de63472018-07-13 08:42:30 -0700134#define KVM_VM_CR0_ALWAYS_OFF (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;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200200 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200201} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200202 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
203 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
204 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
205 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
206 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
207 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200208};
209
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200210#define L1D_CACHE_ORDER 4
211static void *vmx_l1d_flush_pages;
212
213static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
214{
215 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200216 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200217
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200218 if (!enable_ept) {
219 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
220 return 0;
221 }
222
Yi Wangd806afa2018-08-16 13:42:39 +0800223 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
224 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200225
Yi Wangd806afa2018-08-16 13:42:39 +0800226 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
227 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
228 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
229 return 0;
230 }
231 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200232
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200233 /* If set to auto use the default l1tf mitigation method */
234 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
235 switch (l1tf_mitigation) {
236 case L1TF_MITIGATION_OFF:
237 l1tf = VMENTER_L1D_FLUSH_NEVER;
238 break;
239 case L1TF_MITIGATION_FLUSH_NOWARN:
240 case L1TF_MITIGATION_FLUSH:
241 case L1TF_MITIGATION_FLUSH_NOSMT:
242 l1tf = VMENTER_L1D_FLUSH_COND;
243 break;
244 case L1TF_MITIGATION_FULL:
245 case L1TF_MITIGATION_FULL_FORCE:
246 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
247 break;
248 }
249 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
250 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
251 }
252
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200253 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
254 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
255 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
256 if (!page)
257 return -ENOMEM;
258 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200259
260 /*
261 * Initialize each page with a different pattern in
262 * order to protect against KSM in the nested
263 * virtualization case.
264 */
265 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
266 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
267 PAGE_SIZE);
268 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200269 }
270
271 l1tf_vmx_mitigation = l1tf;
272
Thomas Gleixner895ae472018-07-13 16:23:22 +0200273 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
274 static_branch_enable(&vmx_l1d_should_flush);
275 else
276 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200277
Nicolai Stange427362a2018-07-21 22:25:00 +0200278 if (l1tf == VMENTER_L1D_FLUSH_COND)
279 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200280 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200281 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200282 return 0;
283}
284
285static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200286{
287 unsigned int i;
288
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200289 if (s) {
290 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200291 if (vmentry_l1d_param[i].for_parse &&
292 sysfs_streq(s, vmentry_l1d_param[i].option))
293 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200294 }
295 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200296 return -EINVAL;
297}
298
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200299static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
300{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200301 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200302
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303 l1tf = vmentry_l1d_flush_parse(s);
304 if (l1tf < 0)
305 return l1tf;
306
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200307 if (!boot_cpu_has(X86_BUG_L1TF))
308 return 0;
309
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200310 /*
311 * Has vmx_init() run already? If not then this is the pre init
312 * parameter parsing. In that case just store the value and let
313 * vmx_init() do the proper setup after enable_ept has been
314 * established.
315 */
316 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
317 vmentry_l1d_flush_param = l1tf;
318 return 0;
319 }
320
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200321 mutex_lock(&vmx_l1d_flush_mutex);
322 ret = vmx_setup_l1d_flush(l1tf);
323 mutex_unlock(&vmx_l1d_flush_mutex);
324 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200325}
326
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200327static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
328{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200329 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
330 return sprintf(s, "???\n");
331
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200332 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200333}
334
335static const struct kernel_param_ops vmentry_l1d_flush_ops = {
336 .set = vmentry_l1d_flush_set,
337 .get = vmentry_l1d_flush_get,
338};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200339module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200340
Tianyu Lan877ad952018-07-19 08:40:23 +0000341enum ept_pointers_status {
342 EPT_POINTERS_CHECK = 0,
343 EPT_POINTERS_MATCH = 1,
344 EPT_POINTERS_MISMATCH = 2
345};
346
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700347struct kvm_vmx {
348 struct kvm kvm;
349
350 unsigned int tss_addr;
351 bool ept_identity_pagetable_done;
352 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000353
354 enum ept_pointers_status ept_pointers_match;
355 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700356};
357
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200358#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300359
Liran Alon392b2f22018-06-23 02:35:01 +0300360struct vmcs_hdr {
361 u32 revision_id:31;
362 u32 shadow_vmcs:1;
363};
364
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400365struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300366 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400367 u32 abort;
368 char data[0];
369};
370
Nadav Har'Eld462b812011-05-24 15:26:10 +0300371/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700372 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
373 * and whose values change infrequently, but are not constant. I.e. this is
374 * used as a write-through cache of the corresponding VMCS fields.
375 */
376struct vmcs_host_state {
377 unsigned long cr3; /* May not match real cr3 */
378 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700379 unsigned long gs_base;
380 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700381
382 u16 fs_sel, gs_sel, ldt_sel;
383#ifdef CONFIG_X86_64
384 u16 ds_sel, es_sel;
385#endif
386};
387
388/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300389 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
390 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
391 * loaded on this CPU (so we can clear them if the CPU goes down).
392 */
393struct loaded_vmcs {
394 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700395 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300396 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200397 bool launched;
398 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700399 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100400 /* Support for vnmi-less CPUs */
401 int soft_vnmi_blocked;
402 ktime_t entry_time;
403 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100404 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300405 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700406 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300407};
408
Avi Kivity26bb0982009-09-07 11:14:12 +0300409struct shared_msr_entry {
410 unsigned index;
411 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200412 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300413};
414
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300415/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300416 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
417 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
418 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
419 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
420 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
421 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600422 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300423 * underlying hardware which will be used to run L2.
424 * This structure is packed to ensure that its layout is identical across
425 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700426 *
427 * IMPORTANT: Changing the layout of existing fields in this structure
428 * will break save/restore compatibility with older kvm releases. When
429 * adding new fields, either use space in the reserved padding* arrays
430 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300431 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300432typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300433struct __packed vmcs12 {
434 /* According to the Intel spec, a VMCS region must start with the
435 * following two fields. Then follow implementation-specific data.
436 */
Liran Alon392b2f22018-06-23 02:35:01 +0300437 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300438 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300439
Nadav Har'El27d6c862011-05-25 23:06:59 +0300440 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
441 u32 padding[7]; /* room for future expansion */
442
Nadav Har'El22bd0352011-05-25 23:05:57 +0300443 u64 io_bitmap_a;
444 u64 io_bitmap_b;
445 u64 msr_bitmap;
446 u64 vm_exit_msr_store_addr;
447 u64 vm_exit_msr_load_addr;
448 u64 vm_entry_msr_load_addr;
449 u64 tsc_offset;
450 u64 virtual_apic_page_addr;
451 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800452 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300453 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800454 u64 eoi_exit_bitmap0;
455 u64 eoi_exit_bitmap1;
456 u64 eoi_exit_bitmap2;
457 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800458 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300459 u64 guest_physical_address;
460 u64 vmcs_link_pointer;
461 u64 guest_ia32_debugctl;
462 u64 guest_ia32_pat;
463 u64 guest_ia32_efer;
464 u64 guest_ia32_perf_global_ctrl;
465 u64 guest_pdptr0;
466 u64 guest_pdptr1;
467 u64 guest_pdptr2;
468 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100469 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300470 u64 host_ia32_pat;
471 u64 host_ia32_efer;
472 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700473 u64 vmread_bitmap;
474 u64 vmwrite_bitmap;
475 u64 vm_function_control;
476 u64 eptp_list_address;
477 u64 pml_address;
478 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300479 /*
480 * To allow migration of L1 (complete with its L2 guests) between
481 * machines of different natural widths (32 or 64 bit), we cannot have
482 * unsigned long fields with no explict size. We use u64 (aliased
483 * natural_width) instead. Luckily, x86 is little-endian.
484 */
485 natural_width cr0_guest_host_mask;
486 natural_width cr4_guest_host_mask;
487 natural_width cr0_read_shadow;
488 natural_width cr4_read_shadow;
489 natural_width cr3_target_value0;
490 natural_width cr3_target_value1;
491 natural_width cr3_target_value2;
492 natural_width cr3_target_value3;
493 natural_width exit_qualification;
494 natural_width guest_linear_address;
495 natural_width guest_cr0;
496 natural_width guest_cr3;
497 natural_width guest_cr4;
498 natural_width guest_es_base;
499 natural_width guest_cs_base;
500 natural_width guest_ss_base;
501 natural_width guest_ds_base;
502 natural_width guest_fs_base;
503 natural_width guest_gs_base;
504 natural_width guest_ldtr_base;
505 natural_width guest_tr_base;
506 natural_width guest_gdtr_base;
507 natural_width guest_idtr_base;
508 natural_width guest_dr7;
509 natural_width guest_rsp;
510 natural_width guest_rip;
511 natural_width guest_rflags;
512 natural_width guest_pending_dbg_exceptions;
513 natural_width guest_sysenter_esp;
514 natural_width guest_sysenter_eip;
515 natural_width host_cr0;
516 natural_width host_cr3;
517 natural_width host_cr4;
518 natural_width host_fs_base;
519 natural_width host_gs_base;
520 natural_width host_tr_base;
521 natural_width host_gdtr_base;
522 natural_width host_idtr_base;
523 natural_width host_ia32_sysenter_esp;
524 natural_width host_ia32_sysenter_eip;
525 natural_width host_rsp;
526 natural_width host_rip;
527 natural_width paddingl[8]; /* room for future expansion */
528 u32 pin_based_vm_exec_control;
529 u32 cpu_based_vm_exec_control;
530 u32 exception_bitmap;
531 u32 page_fault_error_code_mask;
532 u32 page_fault_error_code_match;
533 u32 cr3_target_count;
534 u32 vm_exit_controls;
535 u32 vm_exit_msr_store_count;
536 u32 vm_exit_msr_load_count;
537 u32 vm_entry_controls;
538 u32 vm_entry_msr_load_count;
539 u32 vm_entry_intr_info_field;
540 u32 vm_entry_exception_error_code;
541 u32 vm_entry_instruction_len;
542 u32 tpr_threshold;
543 u32 secondary_vm_exec_control;
544 u32 vm_instruction_error;
545 u32 vm_exit_reason;
546 u32 vm_exit_intr_info;
547 u32 vm_exit_intr_error_code;
548 u32 idt_vectoring_info_field;
549 u32 idt_vectoring_error_code;
550 u32 vm_exit_instruction_len;
551 u32 vmx_instruction_info;
552 u32 guest_es_limit;
553 u32 guest_cs_limit;
554 u32 guest_ss_limit;
555 u32 guest_ds_limit;
556 u32 guest_fs_limit;
557 u32 guest_gs_limit;
558 u32 guest_ldtr_limit;
559 u32 guest_tr_limit;
560 u32 guest_gdtr_limit;
561 u32 guest_idtr_limit;
562 u32 guest_es_ar_bytes;
563 u32 guest_cs_ar_bytes;
564 u32 guest_ss_ar_bytes;
565 u32 guest_ds_ar_bytes;
566 u32 guest_fs_ar_bytes;
567 u32 guest_gs_ar_bytes;
568 u32 guest_ldtr_ar_bytes;
569 u32 guest_tr_ar_bytes;
570 u32 guest_interruptibility_info;
571 u32 guest_activity_state;
572 u32 guest_sysenter_cs;
573 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100574 u32 vmx_preemption_timer_value;
575 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300576 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800577 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300578 u16 guest_es_selector;
579 u16 guest_cs_selector;
580 u16 guest_ss_selector;
581 u16 guest_ds_selector;
582 u16 guest_fs_selector;
583 u16 guest_gs_selector;
584 u16 guest_ldtr_selector;
585 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800586 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300587 u16 host_es_selector;
588 u16 host_cs_selector;
589 u16 host_ss_selector;
590 u16 host_ds_selector;
591 u16 host_fs_selector;
592 u16 host_gs_selector;
593 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700594 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300595};
596
597/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700598 * For save/restore compatibility, the vmcs12 field offsets must not change.
599 */
600#define CHECK_OFFSET(field, loc) \
601 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
602 "Offset of " #field " in struct vmcs12 has changed.")
603
604static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300605 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700606 CHECK_OFFSET(abort, 4);
607 CHECK_OFFSET(launch_state, 8);
608 CHECK_OFFSET(io_bitmap_a, 40);
609 CHECK_OFFSET(io_bitmap_b, 48);
610 CHECK_OFFSET(msr_bitmap, 56);
611 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
612 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
613 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
614 CHECK_OFFSET(tsc_offset, 88);
615 CHECK_OFFSET(virtual_apic_page_addr, 96);
616 CHECK_OFFSET(apic_access_addr, 104);
617 CHECK_OFFSET(posted_intr_desc_addr, 112);
618 CHECK_OFFSET(ept_pointer, 120);
619 CHECK_OFFSET(eoi_exit_bitmap0, 128);
620 CHECK_OFFSET(eoi_exit_bitmap1, 136);
621 CHECK_OFFSET(eoi_exit_bitmap2, 144);
622 CHECK_OFFSET(eoi_exit_bitmap3, 152);
623 CHECK_OFFSET(xss_exit_bitmap, 160);
624 CHECK_OFFSET(guest_physical_address, 168);
625 CHECK_OFFSET(vmcs_link_pointer, 176);
626 CHECK_OFFSET(guest_ia32_debugctl, 184);
627 CHECK_OFFSET(guest_ia32_pat, 192);
628 CHECK_OFFSET(guest_ia32_efer, 200);
629 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
630 CHECK_OFFSET(guest_pdptr0, 216);
631 CHECK_OFFSET(guest_pdptr1, 224);
632 CHECK_OFFSET(guest_pdptr2, 232);
633 CHECK_OFFSET(guest_pdptr3, 240);
634 CHECK_OFFSET(guest_bndcfgs, 248);
635 CHECK_OFFSET(host_ia32_pat, 256);
636 CHECK_OFFSET(host_ia32_efer, 264);
637 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
638 CHECK_OFFSET(vmread_bitmap, 280);
639 CHECK_OFFSET(vmwrite_bitmap, 288);
640 CHECK_OFFSET(vm_function_control, 296);
641 CHECK_OFFSET(eptp_list_address, 304);
642 CHECK_OFFSET(pml_address, 312);
643 CHECK_OFFSET(cr0_guest_host_mask, 344);
644 CHECK_OFFSET(cr4_guest_host_mask, 352);
645 CHECK_OFFSET(cr0_read_shadow, 360);
646 CHECK_OFFSET(cr4_read_shadow, 368);
647 CHECK_OFFSET(cr3_target_value0, 376);
648 CHECK_OFFSET(cr3_target_value1, 384);
649 CHECK_OFFSET(cr3_target_value2, 392);
650 CHECK_OFFSET(cr3_target_value3, 400);
651 CHECK_OFFSET(exit_qualification, 408);
652 CHECK_OFFSET(guest_linear_address, 416);
653 CHECK_OFFSET(guest_cr0, 424);
654 CHECK_OFFSET(guest_cr3, 432);
655 CHECK_OFFSET(guest_cr4, 440);
656 CHECK_OFFSET(guest_es_base, 448);
657 CHECK_OFFSET(guest_cs_base, 456);
658 CHECK_OFFSET(guest_ss_base, 464);
659 CHECK_OFFSET(guest_ds_base, 472);
660 CHECK_OFFSET(guest_fs_base, 480);
661 CHECK_OFFSET(guest_gs_base, 488);
662 CHECK_OFFSET(guest_ldtr_base, 496);
663 CHECK_OFFSET(guest_tr_base, 504);
664 CHECK_OFFSET(guest_gdtr_base, 512);
665 CHECK_OFFSET(guest_idtr_base, 520);
666 CHECK_OFFSET(guest_dr7, 528);
667 CHECK_OFFSET(guest_rsp, 536);
668 CHECK_OFFSET(guest_rip, 544);
669 CHECK_OFFSET(guest_rflags, 552);
670 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
671 CHECK_OFFSET(guest_sysenter_esp, 568);
672 CHECK_OFFSET(guest_sysenter_eip, 576);
673 CHECK_OFFSET(host_cr0, 584);
674 CHECK_OFFSET(host_cr3, 592);
675 CHECK_OFFSET(host_cr4, 600);
676 CHECK_OFFSET(host_fs_base, 608);
677 CHECK_OFFSET(host_gs_base, 616);
678 CHECK_OFFSET(host_tr_base, 624);
679 CHECK_OFFSET(host_gdtr_base, 632);
680 CHECK_OFFSET(host_idtr_base, 640);
681 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
682 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
683 CHECK_OFFSET(host_rsp, 664);
684 CHECK_OFFSET(host_rip, 672);
685 CHECK_OFFSET(pin_based_vm_exec_control, 744);
686 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
687 CHECK_OFFSET(exception_bitmap, 752);
688 CHECK_OFFSET(page_fault_error_code_mask, 756);
689 CHECK_OFFSET(page_fault_error_code_match, 760);
690 CHECK_OFFSET(cr3_target_count, 764);
691 CHECK_OFFSET(vm_exit_controls, 768);
692 CHECK_OFFSET(vm_exit_msr_store_count, 772);
693 CHECK_OFFSET(vm_exit_msr_load_count, 776);
694 CHECK_OFFSET(vm_entry_controls, 780);
695 CHECK_OFFSET(vm_entry_msr_load_count, 784);
696 CHECK_OFFSET(vm_entry_intr_info_field, 788);
697 CHECK_OFFSET(vm_entry_exception_error_code, 792);
698 CHECK_OFFSET(vm_entry_instruction_len, 796);
699 CHECK_OFFSET(tpr_threshold, 800);
700 CHECK_OFFSET(secondary_vm_exec_control, 804);
701 CHECK_OFFSET(vm_instruction_error, 808);
702 CHECK_OFFSET(vm_exit_reason, 812);
703 CHECK_OFFSET(vm_exit_intr_info, 816);
704 CHECK_OFFSET(vm_exit_intr_error_code, 820);
705 CHECK_OFFSET(idt_vectoring_info_field, 824);
706 CHECK_OFFSET(idt_vectoring_error_code, 828);
707 CHECK_OFFSET(vm_exit_instruction_len, 832);
708 CHECK_OFFSET(vmx_instruction_info, 836);
709 CHECK_OFFSET(guest_es_limit, 840);
710 CHECK_OFFSET(guest_cs_limit, 844);
711 CHECK_OFFSET(guest_ss_limit, 848);
712 CHECK_OFFSET(guest_ds_limit, 852);
713 CHECK_OFFSET(guest_fs_limit, 856);
714 CHECK_OFFSET(guest_gs_limit, 860);
715 CHECK_OFFSET(guest_ldtr_limit, 864);
716 CHECK_OFFSET(guest_tr_limit, 868);
717 CHECK_OFFSET(guest_gdtr_limit, 872);
718 CHECK_OFFSET(guest_idtr_limit, 876);
719 CHECK_OFFSET(guest_es_ar_bytes, 880);
720 CHECK_OFFSET(guest_cs_ar_bytes, 884);
721 CHECK_OFFSET(guest_ss_ar_bytes, 888);
722 CHECK_OFFSET(guest_ds_ar_bytes, 892);
723 CHECK_OFFSET(guest_fs_ar_bytes, 896);
724 CHECK_OFFSET(guest_gs_ar_bytes, 900);
725 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
726 CHECK_OFFSET(guest_tr_ar_bytes, 908);
727 CHECK_OFFSET(guest_interruptibility_info, 912);
728 CHECK_OFFSET(guest_activity_state, 916);
729 CHECK_OFFSET(guest_sysenter_cs, 920);
730 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
731 CHECK_OFFSET(vmx_preemption_timer_value, 928);
732 CHECK_OFFSET(virtual_processor_id, 960);
733 CHECK_OFFSET(posted_intr_nv, 962);
734 CHECK_OFFSET(guest_es_selector, 964);
735 CHECK_OFFSET(guest_cs_selector, 966);
736 CHECK_OFFSET(guest_ss_selector, 968);
737 CHECK_OFFSET(guest_ds_selector, 970);
738 CHECK_OFFSET(guest_fs_selector, 972);
739 CHECK_OFFSET(guest_gs_selector, 974);
740 CHECK_OFFSET(guest_ldtr_selector, 976);
741 CHECK_OFFSET(guest_tr_selector, 978);
742 CHECK_OFFSET(guest_intr_status, 980);
743 CHECK_OFFSET(host_es_selector, 982);
744 CHECK_OFFSET(host_cs_selector, 984);
745 CHECK_OFFSET(host_ss_selector, 986);
746 CHECK_OFFSET(host_ds_selector, 988);
747 CHECK_OFFSET(host_fs_selector, 990);
748 CHECK_OFFSET(host_gs_selector, 992);
749 CHECK_OFFSET(host_tr_selector, 994);
750 CHECK_OFFSET(guest_pml_index, 996);
751}
752
753/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300754 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
755 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
756 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700757 *
758 * IMPORTANT: Changing this value will break save/restore compatibility with
759 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300760 */
761#define VMCS12_REVISION 0x11e57ed0
762
763/*
764 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
765 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
766 * current implementation, 4K are reserved to avoid future complications.
767 */
768#define VMCS12_SIZE 0x1000
769
770/*
Jim Mattson5b157062017-12-22 12:11:12 -0800771 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
772 * supported VMCS12 field encoding.
773 */
774#define VMCS12_MAX_FIELD_INDEX 0x17
775
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100776struct nested_vmx_msrs {
777 /*
778 * We only store the "true" versions of the VMX capability MSRs. We
779 * generate the "non-true" versions by setting the must-be-1 bits
780 * according to the SDM.
781 */
782 u32 procbased_ctls_low;
783 u32 procbased_ctls_high;
784 u32 secondary_ctls_low;
785 u32 secondary_ctls_high;
786 u32 pinbased_ctls_low;
787 u32 pinbased_ctls_high;
788 u32 exit_ctls_low;
789 u32 exit_ctls_high;
790 u32 entry_ctls_low;
791 u32 entry_ctls_high;
792 u32 misc_low;
793 u32 misc_high;
794 u32 ept_caps;
795 u32 vpid_caps;
796 u64 basic;
797 u64 cr0_fixed0;
798 u64 cr0_fixed1;
799 u64 cr4_fixed0;
800 u64 cr4_fixed1;
801 u64 vmcs_enum;
802 u64 vmfunc_controls;
803};
804
Jim Mattson5b157062017-12-22 12:11:12 -0800805/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300806 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
807 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
808 */
809struct nested_vmx {
810 /* Has the level1 guest done vmxon? */
811 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400812 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400813 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300814
815 /* The guest-physical address of the current VMCS L1 keeps for L2 */
816 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700817 /*
818 * Cache of the guest's VMCS, existing outside of guest memory.
819 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700820 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700821 */
822 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300823 /*
Liran Alon61ada742018-06-23 02:35:08 +0300824 * Cache of the guest's shadow VMCS, existing outside of guest
825 * memory. Loaded from guest memory during VM entry. Flushed
826 * to guest memory during VM exit.
827 */
828 struct vmcs12 *cached_shadow_vmcs12;
829 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300830 * Indicates if the shadow vmcs must be updated with the
831 * data hold by vmcs12
832 */
833 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100834 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300835
Sean Christopherson9d6105b22018-09-26 09:23:51 -0700836 /*
837 * vmcs02 has been initialized, i.e. state that is constant for
838 * vmcs02 has been written to the backing VMCS. Initialization
839 * is delayed until L1 actually attempts to run a nested VM.
840 */
841 bool vmcs02_initialized;
842
Jim Mattson8d860bb2018-05-09 16:56:05 -0400843 bool change_vmcs01_virtual_apic_mode;
844
Nadav Har'El644d7112011-05-25 23:12:35 +0300845 /* L2 must run next, and mustn't decide to exit to L1. */
846 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600847
848 struct loaded_vmcs vmcs02;
849
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300850 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600851 * Guest pages referred to in the vmcs02 with host-physical
852 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300853 */
854 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800855 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800856 struct page *pi_desc_page;
857 struct pi_desc *pi_desc;
858 bool pi_pending;
859 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100860
861 struct hrtimer preemption_timer;
862 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200863
864 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
865 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300866 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800867
Wanpeng Li5c614b32015-10-13 09:18:36 -0700868 u16 vpid02;
869 u16 last_vpid;
870
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100871 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200872
873 /* SMM related state */
874 struct {
875 /* in VMX operation on SMM entry? */
876 bool vmxon;
877 /* in guest mode on SMM entry? */
878 bool guest_mode;
879 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300880};
881
Yang Zhang01e439b2013-04-11 19:25:12 +0800882#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800883#define POSTED_INTR_SN 1
884
Yang Zhang01e439b2013-04-11 19:25:12 +0800885/* Posted-Interrupt Descriptor */
886struct pi_desc {
887 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800888 union {
889 struct {
890 /* bit 256 - Outstanding Notification */
891 u16 on : 1,
892 /* bit 257 - Suppress Notification */
893 sn : 1,
894 /* bit 271:258 - Reserved */
895 rsvd_1 : 14;
896 /* bit 279:272 - Notification Vector */
897 u8 nv;
898 /* bit 287:280 - Reserved */
899 u8 rsvd_2;
900 /* bit 319:288 - Notification Destination */
901 u32 ndst;
902 };
903 u64 control;
904 };
905 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800906} __aligned(64);
907
Yang Zhanga20ed542013-04-11 19:25:15 +0800908static bool pi_test_and_set_on(struct pi_desc *pi_desc)
909{
910 return test_and_set_bit(POSTED_INTR_ON,
911 (unsigned long *)&pi_desc->control);
912}
913
914static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
915{
916 return test_and_clear_bit(POSTED_INTR_ON,
917 (unsigned long *)&pi_desc->control);
918}
919
920static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
921{
922 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
923}
924
Feng Wuebbfc762015-09-18 22:29:46 +0800925static inline void pi_clear_sn(struct pi_desc *pi_desc)
926{
927 return clear_bit(POSTED_INTR_SN,
928 (unsigned long *)&pi_desc->control);
929}
930
931static inline void pi_set_sn(struct pi_desc *pi_desc)
932{
933 return set_bit(POSTED_INTR_SN,
934 (unsigned long *)&pi_desc->control);
935}
936
Paolo Bonziniad361092016-09-20 16:15:05 +0200937static inline void pi_clear_on(struct pi_desc *pi_desc)
938{
939 clear_bit(POSTED_INTR_ON,
940 (unsigned long *)&pi_desc->control);
941}
942
Feng Wuebbfc762015-09-18 22:29:46 +0800943static inline int pi_test_on(struct pi_desc *pi_desc)
944{
945 return test_bit(POSTED_INTR_ON,
946 (unsigned long *)&pi_desc->control);
947}
948
949static inline int pi_test_sn(struct pi_desc *pi_desc)
950{
951 return test_bit(POSTED_INTR_SN,
952 (unsigned long *)&pi_desc->control);
953}
954
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400955struct vmx_msrs {
956 unsigned int nr;
957 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
958};
959
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400960struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000961 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300962 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300963 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100964 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300965 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200966 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200967 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300968 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400969 int nmsrs;
970 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800971 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400972#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300973 u64 msr_host_kernel_gs_base;
974 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400975#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100976
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100977 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100978 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100979
Gleb Natapov2961e8762013-11-25 15:37:13 +0200980 u32 vm_entry_controls_shadow;
981 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200982 u32 secondary_exec_control;
983
Nadav Har'Eld462b812011-05-24 15:26:10 +0300984 /*
985 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
986 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700987 * guest (L2), it points to a different VMCS. loaded_cpu_state points
988 * to the VMCS whose state is loaded into the CPU registers that only
989 * need to be switched when transitioning to/from the kernel; a NULL
990 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300991 */
992 struct loaded_vmcs vmcs01;
993 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700994 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300995 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300996 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400997 struct vmx_msrs guest;
998 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300999 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001000
Avi Kivity9c8cba32007-11-22 11:42:59 +02001001 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001002 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001003 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001004 struct kvm_segment segs[8];
1005 } rmode;
1006 struct {
1007 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001008 struct kvm_save_segment {
1009 u16 selector;
1010 unsigned long base;
1011 u32 limit;
1012 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001013 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001014 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001015 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001016 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001017
Andi Kleena0861c02009-06-08 17:37:09 +08001018 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001019
Yang Zhang01e439b2013-04-11 19:25:12 +08001020 /* Posted interrupt descriptor */
1021 struct pi_desc pi_desc;
1022
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001023 /* Support for a guest hypervisor (nested VMX) */
1024 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001025
1026 /* Dynamic PLE window. */
1027 int ple_window;
1028 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001029
Sean Christophersond264ee02018-08-27 15:21:12 -07001030 bool req_immediate_exit;
1031
Kai Huang843e4332015-01-28 10:54:28 +08001032 /* Support for PML */
1033#define PML_ENTITY_NUM 512
1034 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001035
Yunhong Jiang64672c92016-06-13 14:19:59 -07001036 /* apic deadline value in host tsc */
1037 u64 hv_deadline_tsc;
1038
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001039 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001040
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001041 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001042
Wanpeng Li74c55932017-11-29 01:31:20 -08001043 unsigned long host_debugctlmsr;
1044
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001045 /*
1046 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1047 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1048 * in msr_ia32_feature_control_valid_bits.
1049 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001050 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001051 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001052 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001053};
1054
Avi Kivity2fb92db2011-04-27 19:42:18 +03001055enum segment_cache_field {
1056 SEG_FIELD_SEL = 0,
1057 SEG_FIELD_BASE = 1,
1058 SEG_FIELD_LIMIT = 2,
1059 SEG_FIELD_AR = 3,
1060
1061 SEG_FIELD_NR = 4
1062};
1063
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001064static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1065{
1066 return container_of(kvm, struct kvm_vmx, kvm);
1067}
1068
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001069static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1070{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001071 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001072}
1073
Feng Wuefc64402015-09-18 22:29:51 +08001074static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1075{
1076 return &(to_vmx(vcpu)->pi_desc);
1077}
1078
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001079#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001080#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001081#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1082#define FIELD64(number, name) \
1083 FIELD(number, name), \
1084 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001085
Abel Gordon4607c2d2013-04-18 14:35:55 +03001086
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001087static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001088#define SHADOW_FIELD_RO(x) x,
1089#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001090};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001091static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001092 ARRAY_SIZE(shadow_read_only_fields);
1093
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001094static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001095#define SHADOW_FIELD_RW(x) x,
1096#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001097};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001098static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001099 ARRAY_SIZE(shadow_read_write_fields);
1100
Mathias Krause772e0312012-08-30 01:30:19 +02001101static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001102 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001103 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001104 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1105 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1106 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1107 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1108 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1109 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1110 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1111 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001112 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001113 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001114 FIELD(HOST_ES_SELECTOR, host_es_selector),
1115 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1116 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1117 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1118 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1119 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1120 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1121 FIELD64(IO_BITMAP_A, io_bitmap_a),
1122 FIELD64(IO_BITMAP_B, io_bitmap_b),
1123 FIELD64(MSR_BITMAP, msr_bitmap),
1124 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1125 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1126 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001127 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001128 FIELD64(TSC_OFFSET, tsc_offset),
1129 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1130 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001131 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001132 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001133 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001134 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1135 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1136 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1137 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001138 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001139 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1140 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001141 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001142 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1143 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1144 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1145 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1146 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1147 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1148 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1149 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1150 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1151 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001152 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001153 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1154 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1155 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1156 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1157 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1158 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1159 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1160 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1161 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1162 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1163 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1164 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1165 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1166 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1167 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1168 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1169 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1170 FIELD(TPR_THRESHOLD, tpr_threshold),
1171 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1172 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1173 FIELD(VM_EXIT_REASON, vm_exit_reason),
1174 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1175 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1176 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1177 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1178 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1179 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1180 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1181 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1182 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1183 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1184 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1185 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1186 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1187 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1188 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1189 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1190 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1191 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1192 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1193 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1194 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1195 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1196 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1197 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1198 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1199 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1200 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1201 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001202 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001203 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1204 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1205 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1206 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1207 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1208 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1209 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1210 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1211 FIELD(EXIT_QUALIFICATION, exit_qualification),
1212 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1213 FIELD(GUEST_CR0, guest_cr0),
1214 FIELD(GUEST_CR3, guest_cr3),
1215 FIELD(GUEST_CR4, guest_cr4),
1216 FIELD(GUEST_ES_BASE, guest_es_base),
1217 FIELD(GUEST_CS_BASE, guest_cs_base),
1218 FIELD(GUEST_SS_BASE, guest_ss_base),
1219 FIELD(GUEST_DS_BASE, guest_ds_base),
1220 FIELD(GUEST_FS_BASE, guest_fs_base),
1221 FIELD(GUEST_GS_BASE, guest_gs_base),
1222 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1223 FIELD(GUEST_TR_BASE, guest_tr_base),
1224 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1225 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1226 FIELD(GUEST_DR7, guest_dr7),
1227 FIELD(GUEST_RSP, guest_rsp),
1228 FIELD(GUEST_RIP, guest_rip),
1229 FIELD(GUEST_RFLAGS, guest_rflags),
1230 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1231 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1232 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1233 FIELD(HOST_CR0, host_cr0),
1234 FIELD(HOST_CR3, host_cr3),
1235 FIELD(HOST_CR4, host_cr4),
1236 FIELD(HOST_FS_BASE, host_fs_base),
1237 FIELD(HOST_GS_BASE, host_gs_base),
1238 FIELD(HOST_TR_BASE, host_tr_base),
1239 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1240 FIELD(HOST_IDTR_BASE, host_idtr_base),
1241 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1242 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1243 FIELD(HOST_RSP, host_rsp),
1244 FIELD(HOST_RIP, host_rip),
1245};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001246
1247static inline short vmcs_field_to_offset(unsigned long field)
1248{
Dan Williams085331d2018-01-31 17:47:03 -08001249 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1250 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001251 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001252
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001253 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001254 return -ENOENT;
1255
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001256 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001257 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001258 return -ENOENT;
1259
Linus Torvalds15303ba2018-02-10 13:16:35 -08001260 index = array_index_nospec(index, size);
1261 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001262 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001263 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001264 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001265}
1266
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001267static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1268{
David Matlack4f2777b2016-07-13 17:16:37 -07001269 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001270}
1271
Liran Alon61ada742018-06-23 02:35:08 +03001272static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1273{
1274 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1275}
1276
Peter Feiner995f00a2017-06-30 17:26:32 -07001277static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001278static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001279static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001280static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001281static void vmx_set_segment(struct kvm_vcpu *vcpu,
1282 struct kvm_segment *var, int seg);
1283static void vmx_get_segment(struct kvm_vcpu *vcpu,
1284 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001285static bool guest_state_valid(struct kvm_vcpu *vcpu);
1286static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001287static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001288static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1289static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1290static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1291 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001292static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001293static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1294 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001295
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1297static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001298/*
1299 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1300 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1301 */
1302static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303
Feng Wubf9f6ac2015-09-18 22:29:55 +08001304/*
1305 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1306 * can find which vCPU should be waken up.
1307 */
1308static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1309static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1310
Radim Krčmář23611332016-09-29 22:41:33 +02001311enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001312 VMX_VMREAD_BITMAP,
1313 VMX_VMWRITE_BITMAP,
1314 VMX_BITMAP_NR
1315};
1316
1317static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1318
Radim Krčmář23611332016-09-29 22:41:33 +02001319#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1320#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001321
Avi Kivity110312c2010-12-21 12:54:20 +02001322static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001323static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001324
Sheng Yang2384d2b2008-01-17 15:14:33 +08001325static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1326static DEFINE_SPINLOCK(vmx_vpid_lock);
1327
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001328static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329 int size;
1330 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001331 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001333 u32 pin_based_exec_ctrl;
1334 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001335 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001336 u32 vmexit_ctrl;
1337 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001338 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001339} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Hannes Ederefff9e52008-11-28 17:02:06 +01001341static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001342 u32 ept;
1343 u32 vpid;
1344} vmx_capability;
1345
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346#define VMX_SEGMENT_FIELD(seg) \
1347 [VCPU_SREG_##seg] = { \
1348 .selector = GUEST_##seg##_SELECTOR, \
1349 .base = GUEST_##seg##_BASE, \
1350 .limit = GUEST_##seg##_LIMIT, \
1351 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1352 }
1353
Mathias Krause772e0312012-08-30 01:30:19 +02001354static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 unsigned selector;
1356 unsigned base;
1357 unsigned limit;
1358 unsigned ar_bytes;
1359} kvm_vmx_segment_fields[] = {
1360 VMX_SEGMENT_FIELD(CS),
1361 VMX_SEGMENT_FIELD(DS),
1362 VMX_SEGMENT_FIELD(ES),
1363 VMX_SEGMENT_FIELD(FS),
1364 VMX_SEGMENT_FIELD(GS),
1365 VMX_SEGMENT_FIELD(SS),
1366 VMX_SEGMENT_FIELD(TR),
1367 VMX_SEGMENT_FIELD(LDTR),
1368};
1369
Avi Kivity26bb0982009-09-07 11:14:12 +03001370static u64 host_efer;
1371
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001372static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1373
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001374/*
Brian Gerst8c065852010-07-17 09:03:26 -04001375 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001376 * away by decrementing the array size.
1377 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001379#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001380 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001382 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001385DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1386
1387#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1388
1389#define KVM_EVMCS_VERSION 1
1390
1391#if IS_ENABLED(CONFIG_HYPERV)
1392static bool __read_mostly enlightened_vmcs = true;
1393module_param(enlightened_vmcs, bool, 0444);
1394
1395static inline void evmcs_write64(unsigned long field, u64 value)
1396{
1397 u16 clean_field;
1398 int offset = get_evmcs_offset(field, &clean_field);
1399
1400 if (offset < 0)
1401 return;
1402
1403 *(u64 *)((char *)current_evmcs + offset) = value;
1404
1405 current_evmcs->hv_clean_fields &= ~clean_field;
1406}
1407
1408static inline void evmcs_write32(unsigned long field, u32 value)
1409{
1410 u16 clean_field;
1411 int offset = get_evmcs_offset(field, &clean_field);
1412
1413 if (offset < 0)
1414 return;
1415
1416 *(u32 *)((char *)current_evmcs + offset) = value;
1417 current_evmcs->hv_clean_fields &= ~clean_field;
1418}
1419
1420static inline void evmcs_write16(unsigned long field, u16 value)
1421{
1422 u16 clean_field;
1423 int offset = get_evmcs_offset(field, &clean_field);
1424
1425 if (offset < 0)
1426 return;
1427
1428 *(u16 *)((char *)current_evmcs + offset) = value;
1429 current_evmcs->hv_clean_fields &= ~clean_field;
1430}
1431
1432static inline u64 evmcs_read64(unsigned long field)
1433{
1434 int offset = get_evmcs_offset(field, NULL);
1435
1436 if (offset < 0)
1437 return 0;
1438
1439 return *(u64 *)((char *)current_evmcs + offset);
1440}
1441
1442static inline u32 evmcs_read32(unsigned long field)
1443{
1444 int offset = get_evmcs_offset(field, NULL);
1445
1446 if (offset < 0)
1447 return 0;
1448
1449 return *(u32 *)((char *)current_evmcs + offset);
1450}
1451
1452static inline u16 evmcs_read16(unsigned long field)
1453{
1454 int offset = get_evmcs_offset(field, NULL);
1455
1456 if (offset < 0)
1457 return 0;
1458
1459 return *(u16 *)((char *)current_evmcs + offset);
1460}
1461
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001462static inline void evmcs_touch_msr_bitmap(void)
1463{
1464 if (unlikely(!current_evmcs))
1465 return;
1466
1467 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1468 current_evmcs->hv_clean_fields &=
1469 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1470}
1471
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001472static void evmcs_load(u64 phys_addr)
1473{
1474 struct hv_vp_assist_page *vp_ap =
1475 hv_get_vp_assist_page(smp_processor_id());
1476
1477 vp_ap->current_nested_vmcs = phys_addr;
1478 vp_ap->enlighten_vmentry = 1;
1479}
1480
1481static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1482{
1483 /*
1484 * Enlightened VMCSv1 doesn't support these:
1485 *
1486 * POSTED_INTR_NV = 0x00000002,
1487 * GUEST_INTR_STATUS = 0x00000810,
1488 * APIC_ACCESS_ADDR = 0x00002014,
1489 * POSTED_INTR_DESC_ADDR = 0x00002016,
1490 * EOI_EXIT_BITMAP0 = 0x0000201c,
1491 * EOI_EXIT_BITMAP1 = 0x0000201e,
1492 * EOI_EXIT_BITMAP2 = 0x00002020,
1493 * EOI_EXIT_BITMAP3 = 0x00002022,
1494 */
1495 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1496 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1497 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1498 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1499 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1501 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1502
1503 /*
1504 * GUEST_PML_INDEX = 0x00000812,
1505 * PML_ADDRESS = 0x0000200e,
1506 */
1507 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1508
1509 /* VM_FUNCTION_CONTROL = 0x00002018, */
1510 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1511
1512 /*
1513 * EPTP_LIST_ADDRESS = 0x00002024,
1514 * VMREAD_BITMAP = 0x00002026,
1515 * VMWRITE_BITMAP = 0x00002028,
1516 */
1517 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1518
1519 /*
1520 * TSC_MULTIPLIER = 0x00002032,
1521 */
1522 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1523
1524 /*
1525 * PLE_GAP = 0x00004020,
1526 * PLE_WINDOW = 0x00004022,
1527 */
1528 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1529
1530 /*
1531 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1532 */
1533 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1534
1535 /*
1536 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1537 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1538 */
1539 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1540 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1541
1542 /*
1543 * Currently unsupported in KVM:
1544 * GUEST_IA32_RTIT_CTL = 0x00002814,
1545 */
1546}
Tianyu Lan877ad952018-07-19 08:40:23 +00001547
1548/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1549static void check_ept_pointer_match(struct kvm *kvm)
1550{
1551 struct kvm_vcpu *vcpu;
1552 u64 tmp_eptp = INVALID_PAGE;
1553 int i;
1554
1555 kvm_for_each_vcpu(i, vcpu, kvm) {
1556 if (!VALID_PAGE(tmp_eptp)) {
1557 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1558 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1559 to_kvm_vmx(kvm)->ept_pointers_match
1560 = EPT_POINTERS_MISMATCH;
1561 return;
1562 }
1563 }
1564
1565 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1566}
1567
1568static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1569{
1570 int ret;
1571
1572 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1573
1574 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1575 check_ept_pointer_match(kvm);
1576
1577 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1578 ret = -ENOTSUPP;
1579 goto out;
1580 }
1581
1582 ret = hyperv_flush_guest_mapping(
1583 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1584
1585out:
1586 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1587 return ret;
1588}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001589#else /* !IS_ENABLED(CONFIG_HYPERV) */
1590static inline void evmcs_write64(unsigned long field, u64 value) {}
1591static inline void evmcs_write32(unsigned long field, u32 value) {}
1592static inline void evmcs_write16(unsigned long field, u16 value) {}
1593static inline u64 evmcs_read64(unsigned long field) { return 0; }
1594static inline u32 evmcs_read32(unsigned long field) { return 0; }
1595static inline u16 evmcs_read16(unsigned long field) { return 0; }
1596static inline void evmcs_load(u64 phys_addr) {}
1597static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001598static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001599#endif /* IS_ENABLED(CONFIG_HYPERV) */
1600
Jan Kiszka5bb16012016-02-09 20:14:21 +01001601static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602{
1603 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1604 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001605 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1606}
1607
Jan Kiszka6f054852016-02-09 20:15:18 +01001608static inline bool is_debug(u32 intr_info)
1609{
1610 return is_exception_n(intr_info, DB_VECTOR);
1611}
1612
1613static inline bool is_breakpoint(u32 intr_info)
1614{
1615 return is_exception_n(intr_info, BP_VECTOR);
1616}
1617
Jan Kiszka5bb16012016-02-09 20:14:21 +01001618static inline bool is_page_fault(u32 intr_info)
1619{
1620 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621}
1622
Gui Jianfeng31299942010-03-15 17:29:09 +08001623static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001624{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001625 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001626}
1627
Liran Alon9e869482018-03-12 13:12:51 +02001628static inline bool is_gp_fault(u32 intr_info)
1629{
1630 return is_exception_n(intr_info, GP_VECTOR);
1631}
1632
Gui Jianfeng31299942010-03-15 17:29:09 +08001633static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001634{
1635 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1636 INTR_INFO_VALID_MASK)) ==
1637 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1638}
1639
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001640/* Undocumented: icebp/int1 */
1641static inline bool is_icebp(u32 intr_info)
1642{
1643 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1644 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1645}
1646
Gui Jianfeng31299942010-03-15 17:29:09 +08001647static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001648{
Sheng Yang04547152009-04-01 15:52:31 +08001649 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001650}
1651
Gui Jianfeng31299942010-03-15 17:29:09 +08001652static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001653{
Sheng Yang04547152009-04-01 15:52:31 +08001654 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001655}
1656
Paolo Bonzini35754c92015-07-29 12:05:37 +02001657static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001658{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001659 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001660}
1661
Gui Jianfeng31299942010-03-15 17:29:09 +08001662static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001663{
Sheng Yang04547152009-04-01 15:52:31 +08001664 return vmcs_config.cpu_based_exec_ctrl &
1665 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001666}
1667
Avi Kivity774ead32007-12-26 13:57:04 +02001668static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001669{
Sheng Yang04547152009-04-01 15:52:31 +08001670 return vmcs_config.cpu_based_2nd_exec_ctrl &
1671 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1672}
1673
Yang Zhang8d146952013-01-25 10:18:50 +08001674static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1675{
1676 return vmcs_config.cpu_based_2nd_exec_ctrl &
1677 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1678}
1679
Yang Zhang83d4c282013-01-25 10:18:49 +08001680static inline bool cpu_has_vmx_apic_register_virt(void)
1681{
1682 return vmcs_config.cpu_based_2nd_exec_ctrl &
1683 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1684}
1685
Yang Zhangc7c9c562013-01-25 10:18:51 +08001686static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1687{
1688 return vmcs_config.cpu_based_2nd_exec_ctrl &
1689 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1690}
1691
Sean Christopherson0b665d32018-08-14 09:33:34 -07001692static inline bool cpu_has_vmx_encls_vmexit(void)
1693{
1694 return vmcs_config.cpu_based_2nd_exec_ctrl &
1695 SECONDARY_EXEC_ENCLS_EXITING;
1696}
1697
Yunhong Jiang64672c92016-06-13 14:19:59 -07001698/*
1699 * Comment's format: document - errata name - stepping - processor name.
1700 * Refer from
1701 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1702 */
1703static u32 vmx_preemption_cpu_tfms[] = {
1704/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17050x000206E6,
1706/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1707/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1708/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17090x00020652,
1710/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17110x00020655,
1712/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1713/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1714/*
1715 * 320767.pdf - AAP86 - B1 -
1716 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1717 */
17180x000106E5,
1719/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17200x000106A0,
1721/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17220x000106A1,
1723/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17240x000106A4,
1725 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1726 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1727 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17280x000106A5,
1729};
1730
1731static inline bool cpu_has_broken_vmx_preemption_timer(void)
1732{
1733 u32 eax = cpuid_eax(0x00000001), i;
1734
1735 /* Clear the reserved bits */
1736 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001737 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001738 if (eax == vmx_preemption_cpu_tfms[i])
1739 return true;
1740
1741 return false;
1742}
1743
1744static inline bool cpu_has_vmx_preemption_timer(void)
1745{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001746 return vmcs_config.pin_based_exec_ctrl &
1747 PIN_BASED_VMX_PREEMPTION_TIMER;
1748}
1749
Yang Zhang01e439b2013-04-11 19:25:12 +08001750static inline bool cpu_has_vmx_posted_intr(void)
1751{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001752 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1753 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001754}
1755
1756static inline bool cpu_has_vmx_apicv(void)
1757{
1758 return cpu_has_vmx_apic_register_virt() &&
1759 cpu_has_vmx_virtual_intr_delivery() &&
1760 cpu_has_vmx_posted_intr();
1761}
1762
Sheng Yang04547152009-04-01 15:52:31 +08001763static inline bool cpu_has_vmx_flexpriority(void)
1764{
1765 return cpu_has_vmx_tpr_shadow() &&
1766 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001767}
1768
Marcelo Tosattie7997942009-06-11 12:07:40 -03001769static inline bool cpu_has_vmx_ept_execute_only(void)
1770{
Gui Jianfeng31299942010-03-15 17:29:09 +08001771 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001772}
1773
Marcelo Tosattie7997942009-06-11 12:07:40 -03001774static inline bool cpu_has_vmx_ept_2m_page(void)
1775{
Gui Jianfeng31299942010-03-15 17:29:09 +08001776 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001777}
1778
Sheng Yang878403b2010-01-05 19:02:29 +08001779static inline bool cpu_has_vmx_ept_1g_page(void)
1780{
Gui Jianfeng31299942010-03-15 17:29:09 +08001781 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001782}
1783
Sheng Yang4bc9b982010-06-02 14:05:24 +08001784static inline bool cpu_has_vmx_ept_4levels(void)
1785{
1786 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1787}
1788
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001789static inline bool cpu_has_vmx_ept_mt_wb(void)
1790{
1791 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1792}
1793
Yu Zhang855feb62017-08-24 20:27:55 +08001794static inline bool cpu_has_vmx_ept_5levels(void)
1795{
1796 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1797}
1798
Xudong Hao83c3a332012-05-28 19:33:35 +08001799static inline bool cpu_has_vmx_ept_ad_bits(void)
1800{
1801 return vmx_capability.ept & VMX_EPT_AD_BIT;
1802}
1803
Gui Jianfeng31299942010-03-15 17:29:09 +08001804static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001805{
Gui Jianfeng31299942010-03-15 17:29:09 +08001806 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001807}
1808
Gui Jianfeng31299942010-03-15 17:29:09 +08001809static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001810{
Gui Jianfeng31299942010-03-15 17:29:09 +08001811 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001812}
1813
Liran Aloncd9a4912018-05-22 17:16:15 +03001814static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1815{
1816 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1817}
1818
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001819static inline bool cpu_has_vmx_invvpid_single(void)
1820{
1821 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1822}
1823
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001824static inline bool cpu_has_vmx_invvpid_global(void)
1825{
1826 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1827}
1828
Wanpeng Li08d839c2017-03-23 05:30:08 -07001829static inline bool cpu_has_vmx_invvpid(void)
1830{
1831 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1832}
1833
Gui Jianfeng31299942010-03-15 17:29:09 +08001834static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001835{
Sheng Yang04547152009-04-01 15:52:31 +08001836 return vmcs_config.cpu_based_2nd_exec_ctrl &
1837 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001838}
1839
Gui Jianfeng31299942010-03-15 17:29:09 +08001840static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001841{
1842 return vmcs_config.cpu_based_2nd_exec_ctrl &
1843 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1844}
1845
Gui Jianfeng31299942010-03-15 17:29:09 +08001846static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001847{
1848 return vmcs_config.cpu_based_2nd_exec_ctrl &
1849 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1850}
1851
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001852static inline bool cpu_has_vmx_basic_inout(void)
1853{
1854 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1855}
1856
Paolo Bonzini35754c92015-07-29 12:05:37 +02001857static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001858{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001859 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001860}
1861
Gui Jianfeng31299942010-03-15 17:29:09 +08001862static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001863{
Sheng Yang04547152009-04-01 15:52:31 +08001864 return vmcs_config.cpu_based_2nd_exec_ctrl &
1865 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001866}
1867
Gui Jianfeng31299942010-03-15 17:29:09 +08001868static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001869{
1870 return vmcs_config.cpu_based_2nd_exec_ctrl &
1871 SECONDARY_EXEC_RDTSCP;
1872}
1873
Mao, Junjiead756a12012-07-02 01:18:48 +00001874static inline bool cpu_has_vmx_invpcid(void)
1875{
1876 return vmcs_config.cpu_based_2nd_exec_ctrl &
1877 SECONDARY_EXEC_ENABLE_INVPCID;
1878}
1879
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001880static inline bool cpu_has_virtual_nmis(void)
1881{
1882 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1883}
1884
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001885static inline bool cpu_has_vmx_wbinvd_exit(void)
1886{
1887 return vmcs_config.cpu_based_2nd_exec_ctrl &
1888 SECONDARY_EXEC_WBINVD_EXITING;
1889}
1890
Abel Gordonabc4fc52013-04-18 14:35:25 +03001891static inline bool cpu_has_vmx_shadow_vmcs(void)
1892{
1893 u64 vmx_msr;
1894 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1895 /* check if the cpu supports writing r/o exit information fields */
1896 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1897 return false;
1898
1899 return vmcs_config.cpu_based_2nd_exec_ctrl &
1900 SECONDARY_EXEC_SHADOW_VMCS;
1901}
1902
Kai Huang843e4332015-01-28 10:54:28 +08001903static inline bool cpu_has_vmx_pml(void)
1904{
1905 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1906}
1907
Haozhong Zhang64903d62015-10-20 15:39:09 +08001908static inline bool cpu_has_vmx_tsc_scaling(void)
1909{
1910 return vmcs_config.cpu_based_2nd_exec_ctrl &
1911 SECONDARY_EXEC_TSC_SCALING;
1912}
1913
Bandan Das2a499e42017-08-03 15:54:41 -04001914static inline bool cpu_has_vmx_vmfunc(void)
1915{
1916 return vmcs_config.cpu_based_2nd_exec_ctrl &
1917 SECONDARY_EXEC_ENABLE_VMFUNC;
1918}
1919
Sean Christopherson64f7a112018-04-30 10:01:06 -07001920static bool vmx_umip_emulated(void)
1921{
1922 return vmcs_config.cpu_based_2nd_exec_ctrl &
1923 SECONDARY_EXEC_DESC;
1924}
1925
Sheng Yang04547152009-04-01 15:52:31 +08001926static inline bool report_flexpriority(void)
1927{
1928 return flexpriority_enabled;
1929}
1930
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001931static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1932{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001933 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001934}
1935
Jim Mattsonf4160e42018-05-29 09:11:33 -07001936/*
1937 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1938 * to modify any valid field of the VMCS, or are the VM-exit
1939 * information fields read-only?
1940 */
1941static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1942{
1943 return to_vmx(vcpu)->nested.msrs.misc_low &
1944 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1945}
1946
Marc Orr04473782018-06-20 17:21:29 -07001947static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1948{
1949 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1950}
1951
1952static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1953{
1954 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1955 CPU_BASED_MONITOR_TRAP_FLAG;
1956}
1957
Liran Alonfa97d7d2018-07-18 14:07:59 +02001958static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1959{
1960 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1961 SECONDARY_EXEC_SHADOW_VMCS;
1962}
1963
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001964static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1965{
1966 return vmcs12->cpu_based_vm_exec_control & bit;
1967}
1968
1969static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1970{
1971 return (vmcs12->cpu_based_vm_exec_control &
1972 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1973 (vmcs12->secondary_vm_exec_control & bit);
1974}
1975
Jan Kiszkaf4124502014-03-07 20:03:13 +01001976static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1977{
1978 return vmcs12->pin_based_vm_exec_control &
1979 PIN_BASED_VMX_PREEMPTION_TIMER;
1980}
1981
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001982static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1983{
1984 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1985}
1986
1987static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1988{
1989 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1990}
1991
Nadav Har'El155a97a2013-08-05 11:07:16 +03001992static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1993{
1994 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1995}
1996
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001997static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1998{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001999 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002000}
2001
Bandan Dasc5f983f2017-05-05 15:25:14 -04002002static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2003{
2004 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2005}
2006
Wincy Vanf2b93282015-02-03 23:56:03 +08002007static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2008{
2009 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2010}
2011
Wanpeng Li5c614b32015-10-13 09:18:36 -07002012static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2013{
2014 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2015}
2016
Wincy Van82f0dd42015-02-03 23:57:18 +08002017static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2018{
2019 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2020}
2021
Wincy Van608406e2015-02-03 23:57:51 +08002022static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2023{
2024 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2025}
2026
Wincy Van705699a2015-02-03 23:58:17 +08002027static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2028{
2029 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2030}
2031
Bandan Das27c42a12017-08-03 15:54:42 -04002032static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2033{
2034 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2035}
2036
Bandan Das41ab9372017-08-03 15:54:43 -04002037static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2038{
2039 return nested_cpu_has_vmfunc(vmcs12) &&
2040 (vmcs12->vm_function_control &
2041 VMX_VMFUNC_EPTP_SWITCHING);
2042}
2043
Liran Alonf792d272018-06-23 02:35:05 +03002044static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2045{
2046 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2047}
2048
Jim Mattsonef85b672016-12-12 11:01:37 -08002049static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002050{
2051 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002052 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002053}
2054
Jan Kiszka533558b2014-01-04 18:47:20 +01002055static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2056 u32 exit_intr_info,
2057 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002058
Rusty Russell8b9cf982007-07-30 16:31:43 +10002059static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002060{
2061 int i;
2062
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002063 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002064 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002065 return i;
2066 return -1;
2067}
2068
Sheng Yang2384d2b2008-01-17 15:14:33 +08002069static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2070{
2071 struct {
2072 u64 vpid : 16;
2073 u64 rsvd : 48;
2074 u64 gva;
2075 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002076 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002077
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002078 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2079 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2080 : "memory");
2081 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002082}
2083
Sheng Yang14394422008-04-28 12:24:45 +08002084static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2085{
2086 struct {
2087 u64 eptp, gpa;
2088 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002089 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002090
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002091 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2092 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2093 : "memory");
2094 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002095}
2096
Avi Kivity26bb0982009-09-07 11:14:12 +03002097static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002098{
2099 int i;
2100
Rusty Russell8b9cf982007-07-30 16:31:43 +10002101 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002102 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002103 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002104 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002105}
2106
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107static void vmcs_clear(struct vmcs *vmcs)
2108{
2109 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002110 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002112 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2113 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2114 : "memory");
2115 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2117 vmcs, phys_addr);
2118}
2119
Nadav Har'Eld462b812011-05-24 15:26:10 +03002120static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2121{
2122 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002123 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2124 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002125 loaded_vmcs->cpu = -1;
2126 loaded_vmcs->launched = 0;
2127}
2128
Dongxiao Xu7725b892010-05-11 18:29:38 +08002129static void vmcs_load(struct vmcs *vmcs)
2130{
2131 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002132 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002133
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002134 if (static_branch_unlikely(&enable_evmcs))
2135 return evmcs_load(phys_addr);
2136
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002137 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2138 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2139 : "memory");
2140 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002141 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002142 vmcs, phys_addr);
2143}
2144
Dave Young2965faa2015-09-09 15:38:55 -07002145#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002146/*
2147 * This bitmap is used to indicate whether the vmclear
2148 * operation is enabled on all cpus. All disabled by
2149 * default.
2150 */
2151static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2152
2153static inline void crash_enable_local_vmclear(int cpu)
2154{
2155 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2156}
2157
2158static inline void crash_disable_local_vmclear(int cpu)
2159{
2160 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2161}
2162
2163static inline int crash_local_vmclear_enabled(int cpu)
2164{
2165 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2166}
2167
2168static void crash_vmclear_local_loaded_vmcss(void)
2169{
2170 int cpu = raw_smp_processor_id();
2171 struct loaded_vmcs *v;
2172
2173 if (!crash_local_vmclear_enabled(cpu))
2174 return;
2175
2176 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2177 loaded_vmcss_on_cpu_link)
2178 vmcs_clear(v->vmcs);
2179}
2180#else
2181static inline void crash_enable_local_vmclear(int cpu) { }
2182static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002183#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002184
Nadav Har'Eld462b812011-05-24 15:26:10 +03002185static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002187 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002188 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189
Nadav Har'Eld462b812011-05-24 15:26:10 +03002190 if (loaded_vmcs->cpu != cpu)
2191 return; /* vcpu migration can race with cpu offline */
2192 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002194 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002195 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002196
2197 /*
2198 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2199 * is before setting loaded_vmcs->vcpu to -1 which is done in
2200 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2201 * then adds the vmcs into percpu list before it is deleted.
2202 */
2203 smp_wmb();
2204
Nadav Har'Eld462b812011-05-24 15:26:10 +03002205 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002206 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207}
2208
Nadav Har'Eld462b812011-05-24 15:26:10 +03002209static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002210{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002211 int cpu = loaded_vmcs->cpu;
2212
2213 if (cpu != -1)
2214 smp_call_function_single(cpu,
2215 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002216}
2217
Junaid Shahidfaff8752018-06-29 13:10:05 -07002218static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2219{
2220 if (vpid == 0)
2221 return true;
2222
2223 if (cpu_has_vmx_invvpid_individual_addr()) {
2224 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2225 return true;
2226 }
2227
2228 return false;
2229}
2230
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002231static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002232{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002233 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002234 return;
2235
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002236 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002237 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002238}
2239
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002240static inline void vpid_sync_vcpu_global(void)
2241{
2242 if (cpu_has_vmx_invvpid_global())
2243 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2244}
2245
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002246static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002247{
2248 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002249 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002250 else
2251 vpid_sync_vcpu_global();
2252}
2253
Sheng Yang14394422008-04-28 12:24:45 +08002254static inline void ept_sync_global(void)
2255{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002256 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002257}
2258
2259static inline void ept_sync_context(u64 eptp)
2260{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002261 if (cpu_has_vmx_invept_context())
2262 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2263 else
2264 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002265}
2266
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002267static __always_inline void vmcs_check16(unsigned long field)
2268{
2269 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2270 "16-bit accessor invalid for 64-bit field");
2271 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2272 "16-bit accessor invalid for 64-bit high field");
2273 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2274 "16-bit accessor invalid for 32-bit high field");
2275 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2276 "16-bit accessor invalid for natural width field");
2277}
2278
2279static __always_inline void vmcs_check32(unsigned long field)
2280{
2281 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2282 "32-bit accessor invalid for 16-bit field");
2283 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2284 "32-bit accessor invalid for natural width field");
2285}
2286
2287static __always_inline void vmcs_check64(unsigned long field)
2288{
2289 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2290 "64-bit accessor invalid for 16-bit field");
2291 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2292 "64-bit accessor invalid for 64-bit high field");
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2294 "64-bit accessor invalid for 32-bit field");
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2296 "64-bit accessor invalid for natural width field");
2297}
2298
2299static __always_inline void vmcs_checkl(unsigned long field)
2300{
2301 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2302 "Natural width accessor invalid for 16-bit field");
2303 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2304 "Natural width accessor invalid for 64-bit field");
2305 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2306 "Natural width accessor invalid for 64-bit high field");
2307 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2308 "Natural width accessor invalid for 32-bit field");
2309}
2310
2311static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312{
Avi Kivity5e520e62011-05-15 10:13:12 -04002313 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314
Avi Kivity5e520e62011-05-15 10:13:12 -04002315 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2316 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 return value;
2318}
2319
Avi Kivity96304212011-05-15 10:13:13 -04002320static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002322 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002323 if (static_branch_unlikely(&enable_evmcs))
2324 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002325 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326}
2327
Avi Kivity96304212011-05-15 10:13:13 -04002328static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002330 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002331 if (static_branch_unlikely(&enable_evmcs))
2332 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002333 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334}
2335
Avi Kivity96304212011-05-15 10:13:13 -04002336static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002338 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002339 if (static_branch_unlikely(&enable_evmcs))
2340 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002341#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002342 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002344 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345#endif
2346}
2347
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002348static __always_inline unsigned long vmcs_readl(unsigned long field)
2349{
2350 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002351 if (static_branch_unlikely(&enable_evmcs))
2352 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002353 return __vmcs_readl(field);
2354}
2355
Avi Kivitye52de1b2007-01-05 16:36:56 -08002356static noinline void vmwrite_error(unsigned long field, unsigned long value)
2357{
2358 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2359 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2360 dump_stack();
2361}
2362
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002363static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002365 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002367 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2368 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002369 if (unlikely(error))
2370 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371}
2372
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002373static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002375 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002376 if (static_branch_unlikely(&enable_evmcs))
2377 return evmcs_write16(field, value);
2378
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002379 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380}
2381
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002382static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002384 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002385 if (static_branch_unlikely(&enable_evmcs))
2386 return evmcs_write32(field, value);
2387
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002388 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389}
2390
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002391static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002393 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002394 if (static_branch_unlikely(&enable_evmcs))
2395 return evmcs_write64(field, value);
2396
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002398#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002400 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401#endif
2402}
2403
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002405{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002406 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002407 if (static_branch_unlikely(&enable_evmcs))
2408 return evmcs_write64(field, value);
2409
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002410 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002411}
2412
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002414{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002415 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2416 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002417 if (static_branch_unlikely(&enable_evmcs))
2418 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2419
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002420 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2421}
2422
2423static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2424{
2425 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2426 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002427 if (static_branch_unlikely(&enable_evmcs))
2428 return evmcs_write32(field, evmcs_read32(field) | mask);
2429
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002430 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002431}
2432
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002433static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2434{
2435 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2436}
2437
Gleb Natapov2961e8762013-11-25 15:37:13 +02002438static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2439{
2440 vmcs_write32(VM_ENTRY_CONTROLS, val);
2441 vmx->vm_entry_controls_shadow = val;
2442}
2443
2444static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2445{
2446 if (vmx->vm_entry_controls_shadow != val)
2447 vm_entry_controls_init(vmx, val);
2448}
2449
2450static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2451{
2452 return vmx->vm_entry_controls_shadow;
2453}
2454
2455
2456static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2457{
2458 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2459}
2460
2461static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2462{
2463 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2464}
2465
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002466static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2467{
2468 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2469}
2470
Gleb Natapov2961e8762013-11-25 15:37:13 +02002471static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2472{
2473 vmcs_write32(VM_EXIT_CONTROLS, val);
2474 vmx->vm_exit_controls_shadow = val;
2475}
2476
2477static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2478{
2479 if (vmx->vm_exit_controls_shadow != val)
2480 vm_exit_controls_init(vmx, val);
2481}
2482
2483static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2484{
2485 return vmx->vm_exit_controls_shadow;
2486}
2487
2488
2489static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2490{
2491 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2492}
2493
2494static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2495{
2496 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2497}
2498
Avi Kivity2fb92db2011-04-27 19:42:18 +03002499static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2500{
2501 vmx->segment_cache.bitmask = 0;
2502}
2503
2504static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2505 unsigned field)
2506{
2507 bool ret;
2508 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2509
2510 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2511 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2512 vmx->segment_cache.bitmask = 0;
2513 }
2514 ret = vmx->segment_cache.bitmask & mask;
2515 vmx->segment_cache.bitmask |= mask;
2516 return ret;
2517}
2518
2519static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2520{
2521 u16 *p = &vmx->segment_cache.seg[seg].selector;
2522
2523 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2524 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2525 return *p;
2526}
2527
2528static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2529{
2530 ulong *p = &vmx->segment_cache.seg[seg].base;
2531
2532 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2533 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2534 return *p;
2535}
2536
2537static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2538{
2539 u32 *p = &vmx->segment_cache.seg[seg].limit;
2540
2541 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2542 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2543 return *p;
2544}
2545
2546static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2547{
2548 u32 *p = &vmx->segment_cache.seg[seg].ar;
2549
2550 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2551 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2552 return *p;
2553}
2554
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002555static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2556{
2557 u32 eb;
2558
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002559 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002560 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002561 /*
2562 * Guest access to VMware backdoor ports could legitimately
2563 * trigger #GP because of TSS I/O permission bitmap.
2564 * We intercept those #GP and allow access to them anyway
2565 * as VMware does.
2566 */
2567 if (enable_vmware_backdoor)
2568 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002569 if ((vcpu->guest_debug &
2570 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2571 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2572 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002573 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002574 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002575 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002576 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002577
2578 /* When we are running a nested L2 guest and L1 specified for it a
2579 * certain exception bitmap, we must trap the same exceptions and pass
2580 * them to L1. When running L2, we will only handle the exceptions
2581 * specified above if L1 did not want them.
2582 */
2583 if (is_guest_mode(vcpu))
2584 eb |= get_vmcs12(vcpu)->exception_bitmap;
2585
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002586 vmcs_write32(EXCEPTION_BITMAP, eb);
2587}
2588
Ashok Raj15d45072018-02-01 22:59:43 +01002589/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002590 * Check if MSR is intercepted for currently loaded MSR bitmap.
2591 */
2592static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2593{
2594 unsigned long *msr_bitmap;
2595 int f = sizeof(unsigned long);
2596
2597 if (!cpu_has_vmx_msr_bitmap())
2598 return true;
2599
2600 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2601
2602 if (msr <= 0x1fff) {
2603 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2604 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2605 msr &= 0x1fff;
2606 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2607 }
2608
2609 return true;
2610}
2611
2612/*
Ashok Raj15d45072018-02-01 22:59:43 +01002613 * Check if MSR is intercepted for L01 MSR bitmap.
2614 */
2615static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2616{
2617 unsigned long *msr_bitmap;
2618 int f = sizeof(unsigned long);
2619
2620 if (!cpu_has_vmx_msr_bitmap())
2621 return true;
2622
2623 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2624
2625 if (msr <= 0x1fff) {
2626 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2627 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2628 msr &= 0x1fff;
2629 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2630 }
2631
2632 return true;
2633}
2634
Gleb Natapov2961e8762013-11-25 15:37:13 +02002635static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2636 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002637{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002638 vm_entry_controls_clearbit(vmx, entry);
2639 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002640}
2641
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002642static int find_msr(struct vmx_msrs *m, unsigned int msr)
2643{
2644 unsigned int i;
2645
2646 for (i = 0; i < m->nr; ++i) {
2647 if (m->val[i].index == msr)
2648 return i;
2649 }
2650 return -ENOENT;
2651}
2652
Avi Kivity61d2ef22010-04-28 16:40:38 +03002653static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2654{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002655 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002656 struct msr_autoload *m = &vmx->msr_autoload;
2657
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002658 switch (msr) {
2659 case MSR_EFER:
2660 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002661 clear_atomic_switch_msr_special(vmx,
2662 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002663 VM_EXIT_LOAD_IA32_EFER);
2664 return;
2665 }
2666 break;
2667 case MSR_CORE_PERF_GLOBAL_CTRL:
2668 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002669 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002670 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2671 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2672 return;
2673 }
2674 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002675 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002676 i = find_msr(&m->guest, msr);
2677 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002678 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002679 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002680 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002681 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002682
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002683skip_guest:
2684 i = find_msr(&m->host, msr);
2685 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002686 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002687
2688 --m->host.nr;
2689 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002690 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002691}
2692
Gleb Natapov2961e8762013-11-25 15:37:13 +02002693static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2694 unsigned long entry, unsigned long exit,
2695 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2696 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002697{
2698 vmcs_write64(guest_val_vmcs, guest_val);
2699 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002700 vm_entry_controls_setbit(vmx, entry);
2701 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002702}
2703
Avi Kivity61d2ef22010-04-28 16:40:38 +03002704static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002705 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002706{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002707 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002708 struct msr_autoload *m = &vmx->msr_autoload;
2709
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002710 switch (msr) {
2711 case MSR_EFER:
2712 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002713 add_atomic_switch_msr_special(vmx,
2714 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002715 VM_EXIT_LOAD_IA32_EFER,
2716 GUEST_IA32_EFER,
2717 HOST_IA32_EFER,
2718 guest_val, host_val);
2719 return;
2720 }
2721 break;
2722 case MSR_CORE_PERF_GLOBAL_CTRL:
2723 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002724 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002725 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2726 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2727 GUEST_IA32_PERF_GLOBAL_CTRL,
2728 HOST_IA32_PERF_GLOBAL_CTRL,
2729 guest_val, host_val);
2730 return;
2731 }
2732 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002733 case MSR_IA32_PEBS_ENABLE:
2734 /* PEBS needs a quiescent period after being disabled (to write
2735 * a record). Disabling PEBS through VMX MSR swapping doesn't
2736 * provide that period, so a CPU could write host's record into
2737 * guest's memory.
2738 */
2739 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002740 }
2741
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002742 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002743 if (!entry_only)
2744 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002745
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002746 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002747 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002748 "Can't add msr %x\n", msr);
2749 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002750 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002751 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002752 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002753 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002754 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002755 m->guest.val[i].index = msr;
2756 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002757
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002758 if (entry_only)
2759 return;
2760
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002761 if (j < 0) {
2762 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002763 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002764 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002765 m->host.val[j].index = msr;
2766 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002767}
2768
Avi Kivity92c0d902009-10-29 11:00:16 +02002769static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002770{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002771 u64 guest_efer = vmx->vcpu.arch.efer;
2772 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002773
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002774 if (!enable_ept) {
2775 /*
2776 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2777 * host CPUID is more efficient than testing guest CPUID
2778 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2779 */
2780 if (boot_cpu_has(X86_FEATURE_SMEP))
2781 guest_efer |= EFER_NX;
2782 else if (!(guest_efer & EFER_NX))
2783 ignore_bits |= EFER_NX;
2784 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002785
Avi Kivity51c6cf62007-08-29 03:48:05 +03002786 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002787 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002788 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002789 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002790#ifdef CONFIG_X86_64
2791 ignore_bits |= EFER_LMA | EFER_LME;
2792 /* SCE is meaningful only in long mode on Intel */
2793 if (guest_efer & EFER_LMA)
2794 ignore_bits &= ~(u64)EFER_SCE;
2795#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002796
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002797 /*
2798 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2799 * On CPUs that support "load IA32_EFER", always switch EFER
2800 * atomically, since it's faster than switching it manually.
2801 */
2802 if (cpu_has_load_ia32_efer ||
2803 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002804 if (!(guest_efer & EFER_LMA))
2805 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002806 if (guest_efer != host_efer)
2807 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002808 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002809 else
2810 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002811 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002812 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002813 clear_atomic_switch_msr(vmx, MSR_EFER);
2814
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002815 guest_efer &= ~ignore_bits;
2816 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002817
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002818 vmx->guest_msrs[efer_offset].data = guest_efer;
2819 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2820
2821 return true;
2822 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002823}
2824
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002825#ifdef CONFIG_X86_32
2826/*
2827 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2828 * VMCS rather than the segment table. KVM uses this helper to figure
2829 * out the current bases to poke them into the VMCS before entry.
2830 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002831static unsigned long segment_base(u16 selector)
2832{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002833 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002834 unsigned long v;
2835
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002836 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002837 return 0;
2838
Thomas Garnier45fc8752017-03-14 10:05:08 -07002839 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002840
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002841 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842 u16 ldt_selector = kvm_read_ldt();
2843
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002844 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002845 return 0;
2846
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002847 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002848 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 return v;
2851}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002852#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002853
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002854static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002855{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002856 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002857 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002858#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002859 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002860#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002861 unsigned long fs_base, gs_base;
2862 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002863 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002864
Sean Christophersond264ee02018-08-27 15:21:12 -07002865 vmx->req_immediate_exit = false;
2866
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002867 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002868 return;
2869
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002870 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002871 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872
Avi Kivity33ed6322007-05-02 16:54:03 +03002873 /*
2874 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2875 * allow segment selectors with cpl > 0 or ti == 1.
2876 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002877 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002878
2879#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002880 savesegment(ds, host_state->ds_sel);
2881 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002882
2883 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002884 if (likely(is_64bit_mm(current->mm))) {
2885 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002886 fs_sel = current->thread.fsindex;
2887 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002888 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002889 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002890 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002891 savesegment(fs, fs_sel);
2892 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002893 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002894 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002895 }
2896
Paolo Bonzini4679b612018-09-24 17:23:01 +02002897 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002898#else
Sean Christophersone368b872018-07-23 12:32:41 -07002899 savesegment(fs, fs_sel);
2900 savesegment(gs, gs_sel);
2901 fs_base = segment_base(fs_sel);
2902 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002903#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002904
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002905 if (unlikely(fs_sel != host_state->fs_sel)) {
2906 if (!(fs_sel & 7))
2907 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2908 else
2909 vmcs_write16(HOST_FS_SELECTOR, 0);
2910 host_state->fs_sel = fs_sel;
2911 }
2912 if (unlikely(gs_sel != host_state->gs_sel)) {
2913 if (!(gs_sel & 7))
2914 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2915 else
2916 vmcs_write16(HOST_GS_SELECTOR, 0);
2917 host_state->gs_sel = gs_sel;
2918 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002919 if (unlikely(fs_base != host_state->fs_base)) {
2920 vmcs_writel(HOST_FS_BASE, fs_base);
2921 host_state->fs_base = fs_base;
2922 }
2923 if (unlikely(gs_base != host_state->gs_base)) {
2924 vmcs_writel(HOST_GS_BASE, gs_base);
2925 host_state->gs_base = gs_base;
2926 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002927
Avi Kivity26bb0982009-09-07 11:14:12 +03002928 for (i = 0; i < vmx->save_nmsrs; ++i)
2929 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002930 vmx->guest_msrs[i].data,
2931 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002932}
2933
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002934static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002935{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002936 struct vmcs_host_state *host_state;
2937
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002938 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002939 return;
2940
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002941 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002942 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002943
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002944 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002945 vmx->loaded_cpu_state = NULL;
2946
Avi Kivityc8770e72010-11-11 12:37:26 +02002947#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002948 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002949#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002950 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2951 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002952#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002953 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002954#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002956#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002957 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002958 if (host_state->fs_sel & 7)
2959 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002960#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002961 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2962 loadsegment(ds, host_state->ds_sel);
2963 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002964 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002965#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002966 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002967#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002968 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002969#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002970 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002971}
2972
Sean Christopherson678e3152018-07-23 12:32:43 -07002973#ifdef CONFIG_X86_64
2974static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002975{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002976 preempt_disable();
2977 if (vmx->loaded_cpu_state)
2978 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2979 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002980 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002981}
2982
Sean Christopherson678e3152018-07-23 12:32:43 -07002983static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2984{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002985 preempt_disable();
2986 if (vmx->loaded_cpu_state)
2987 wrmsrl(MSR_KERNEL_GS_BASE, data);
2988 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002989 vmx->msr_guest_kernel_gs_base = data;
2990}
2991#endif
2992
Feng Wu28b835d2015-09-18 22:29:54 +08002993static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2994{
2995 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2996 struct pi_desc old, new;
2997 unsigned int dest;
2998
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002999 /*
3000 * In case of hot-plug or hot-unplug, we may have to undo
3001 * vmx_vcpu_pi_put even if there is no assigned device. And we
3002 * always keep PI.NDST up to date for simplicity: it makes the
3003 * code easier, and CPU migration is not a fast path.
3004 */
3005 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003006 return;
3007
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003008 /*
3009 * First handle the simple case where no cmpxchg is necessary; just
3010 * allow posting non-urgent interrupts.
3011 *
3012 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3013 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3014 * expects the VCPU to be on the blocked_vcpu_list that matches
3015 * PI.NDST.
3016 */
3017 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3018 vcpu->cpu == cpu) {
3019 pi_clear_sn(pi_desc);
3020 return;
3021 }
3022
3023 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003024 do {
3025 old.control = new.control = pi_desc->control;
3026
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003027 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003028
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003029 if (x2apic_enabled())
3030 new.ndst = dest;
3031 else
3032 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003033
Feng Wu28b835d2015-09-18 22:29:54 +08003034 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003035 } while (cmpxchg64(&pi_desc->control, old.control,
3036 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003037}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003038
Peter Feinerc95ba922016-08-17 09:36:47 -07003039static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3040{
3041 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3042 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3043}
3044
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045/*
3046 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3047 * vcpu mutex is already taken.
3048 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003049static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003051 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003052 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003054 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003055 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003056 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003057 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003058
3059 /*
3060 * Read loaded_vmcs->cpu should be before fetching
3061 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3062 * See the comments in __loaded_vmcs_clear().
3063 */
3064 smp_rmb();
3065
Nadav Har'Eld462b812011-05-24 15:26:10 +03003066 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3067 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003068 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003069 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003070 }
3071
3072 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3073 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3074 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003075 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003076 }
3077
3078 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003079 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003080 unsigned long sysenter_esp;
3081
3082 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003083
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 /*
3085 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003086 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003088 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003089 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003090 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003092 /*
3093 * VM exits change the host TR limit to 0x67 after a VM
3094 * exit. This is okay, since 0x67 covers everything except
3095 * the IO bitmap and have have code to handle the IO bitmap
3096 * being lost after a VM exit.
3097 */
3098 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3099
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3101 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003102
Nadav Har'Eld462b812011-05-24 15:26:10 +03003103 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 }
Feng Wu28b835d2015-09-18 22:29:54 +08003105
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003106 /* Setup TSC multiplier */
3107 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003108 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3109 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003110
Feng Wu28b835d2015-09-18 22:29:54 +08003111 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003112 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003113 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003114}
3115
3116static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3117{
3118 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3119
3120 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003121 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3122 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003123 return;
3124
3125 /* Set SN when the vCPU is preempted */
3126 if (vcpu->preempted)
3127 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128}
3129
3130static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3131{
Feng Wu28b835d2015-09-18 22:29:54 +08003132 vmx_vcpu_pi_put(vcpu);
3133
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003134 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135}
3136
Wanpeng Lif244dee2017-07-20 01:11:54 -07003137static bool emulation_required(struct kvm_vcpu *vcpu)
3138{
3139 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3140}
3141
Avi Kivityedcafe32009-12-30 18:07:40 +02003142static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3143
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003144/*
3145 * Return the cr0 value that a nested guest would read. This is a combination
3146 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3147 * its hypervisor (cr0_read_shadow).
3148 */
3149static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3150{
3151 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3152 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3153}
3154static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3155{
3156 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3157 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3158}
3159
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3161{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003162 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003163
Avi Kivity6de12732011-03-07 12:51:22 +02003164 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3165 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3166 rflags = vmcs_readl(GUEST_RFLAGS);
3167 if (to_vmx(vcpu)->rmode.vm86_active) {
3168 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3169 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3170 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3171 }
3172 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003173 }
Avi Kivity6de12732011-03-07 12:51:22 +02003174 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175}
3176
3177static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3178{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003179 unsigned long old_rflags = vmx_get_rflags(vcpu);
3180
Avi Kivity6de12732011-03-07 12:51:22 +02003181 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3182 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003183 if (to_vmx(vcpu)->rmode.vm86_active) {
3184 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003185 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003186 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003188
3189 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3190 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191}
3192
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003193static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003194{
3195 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3196 int ret = 0;
3197
3198 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003199 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003200 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003201 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003202
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003203 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003204}
3205
3206static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3207{
3208 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3209 u32 interruptibility = interruptibility_old;
3210
3211 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3212
Jan Kiszka48005f62010-02-19 19:38:07 +01003213 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003214 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003215 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003216 interruptibility |= GUEST_INTR_STATE_STI;
3217
3218 if ((interruptibility != interruptibility_old))
3219 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3220}
3221
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3223{
3224 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003226 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003228 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229
Glauber Costa2809f5d2009-05-12 16:21:05 -04003230 /* skipping an emulated instruction also counts */
3231 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232}
3233
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003234static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3235 unsigned long exit_qual)
3236{
3237 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3238 unsigned int nr = vcpu->arch.exception.nr;
3239 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3240
3241 if (vcpu->arch.exception.has_error_code) {
3242 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3243 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3244 }
3245
3246 if (kvm_exception_is_soft(nr))
3247 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3248 else
3249 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3250
3251 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3252 vmx_get_nmi_mask(vcpu))
3253 intr_info |= INTR_INFO_UNBLOCK_NMI;
3254
3255 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3256}
3257
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003258/*
3259 * KVM wants to inject page-faults which it got to the guest. This function
3260 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003261 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003262static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003263{
3264 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003265 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003266
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003267 if (nr == PF_VECTOR) {
3268 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003269 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003270 return 1;
3271 }
3272 /*
3273 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3274 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3275 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3276 * can be written only when inject_pending_event runs. This should be
3277 * conditional on a new capability---if the capability is disabled,
3278 * kvm_multiple_exception would write the ancillary information to
3279 * CR2 or DR6, for backwards ABI-compatibility.
3280 */
3281 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3282 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003283 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003284 return 1;
3285 }
3286 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003287 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003288 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003289 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003290 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3291 *exit_qual ^= DR6_RTM;
3292 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003293 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003294 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003295 return 1;
3296 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003297 }
3298
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003299 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003300}
3301
Wanpeng Licaa057a2018-03-12 04:53:03 -07003302static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3303{
3304 /*
3305 * Ensure that we clear the HLT state in the VMCS. We don't need to
3306 * explicitly skip the instruction because if the HLT state is set,
3307 * then the instruction is already executing and RIP has already been
3308 * advanced.
3309 */
3310 if (kvm_hlt_in_guest(vcpu->kvm) &&
3311 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3312 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3313}
3314
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003315static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003316{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003317 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003318 unsigned nr = vcpu->arch.exception.nr;
3319 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003320 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003321 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003322
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003323 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003324 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003325 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3326 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003327
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003328 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003329 int inc_eip = 0;
3330 if (kvm_exception_is_soft(nr))
3331 inc_eip = vcpu->arch.event_exit_inst_len;
3332 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003333 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003334 return;
3335 }
3336
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003337 WARN_ON_ONCE(vmx->emulation_required);
3338
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003339 if (kvm_exception_is_soft(nr)) {
3340 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3341 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003342 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3343 } else
3344 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3345
3346 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003347
3348 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003349}
3350
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003351static bool vmx_rdtscp_supported(void)
3352{
3353 return cpu_has_vmx_rdtscp();
3354}
3355
Mao, Junjiead756a12012-07-02 01:18:48 +00003356static bool vmx_invpcid_supported(void)
3357{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003358 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003359}
3360
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361/*
Eddie Donga75beee2007-05-17 18:55:15 +03003362 * Swap MSR entry in host/guest MSR entry array.
3363 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003364static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003365{
Avi Kivity26bb0982009-09-07 11:14:12 +03003366 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003367
3368 tmp = vmx->guest_msrs[to];
3369 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3370 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003371}
3372
3373/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003374 * Set up the vmcs to automatically save and restore system
3375 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3376 * mode, as fiddling with msrs is very expensive.
3377 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003378static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003379{
Avi Kivity26bb0982009-09-07 11:14:12 +03003380 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003381
Eddie Donga75beee2007-05-17 18:55:15 +03003382 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003383#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003384 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003385 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003386 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003387 move_msr_up(vmx, index, save_nmsrs++);
3388 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003389 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003390 move_msr_up(vmx, index, save_nmsrs++);
3391 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003392 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003393 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003394 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003395 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003396 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003397 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003398 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003399 * if efer.sce is enabled.
3400 */
Brian Gerst8c065852010-07-17 09:03:26 -04003401 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003402 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003403 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003404 }
Eddie Donga75beee2007-05-17 18:55:15 +03003405#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003406 index = __find_msr_index(vmx, MSR_EFER);
3407 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003408 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003409
Avi Kivity26bb0982009-09-07 11:14:12 +03003410 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003411
Yang Zhang8d146952013-01-25 10:18:50 +08003412 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003413 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003414}
3415
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003416static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003418 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003420 if (is_guest_mode(vcpu) &&
3421 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3422 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3423
3424 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425}
3426
3427/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003428 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003430static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003432 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003433 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003434 * We're here if L1 chose not to trap WRMSR to TSC. According
3435 * to the spec, this should set L1's TSC; The offset that L1
3436 * set for L2 remains unchanged, and still needs to be added
3437 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003438 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003439 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003440 /* recalculate vmcs02.TSC_OFFSET: */
3441 vmcs12 = get_vmcs12(vcpu);
3442 vmcs_write64(TSC_OFFSET, offset +
3443 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3444 vmcs12->tsc_offset : 0));
3445 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003446 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3447 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003448 vmcs_write64(TSC_OFFSET, offset);
3449 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450}
3451
Nadav Har'El801d3422011-05-25 23:02:23 +03003452/*
3453 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3454 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3455 * all guests if the "nested" module option is off, and can also be disabled
3456 * for a single guest by disabling its VMX cpuid bit.
3457 */
3458static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3459{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003460 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003461}
3462
Avi Kivity6aa8b732006-12-10 02:21:36 -08003463/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003464 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3465 * returned for the various VMX controls MSRs when nested VMX is enabled.
3466 * The same values should also be used to verify that vmcs12 control fields are
3467 * valid during nested entry from L1 to L2.
3468 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3469 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3470 * bit in the high half is on if the corresponding bit in the control field
3471 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003473static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474{
Paolo Bonzini13893092018-02-26 13:40:09 +01003475 if (!nested) {
3476 memset(msrs, 0, sizeof(*msrs));
3477 return;
3478 }
3479
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003480 /*
3481 * Note that as a general rule, the high half of the MSRs (bits in
3482 * the control fields which may be 1) should be initialized by the
3483 * intersection of the underlying hardware's MSR (i.e., features which
3484 * can be supported) and the list of features we want to expose -
3485 * because they are known to be properly supported in our code.
3486 * Also, usually, the low half of the MSRs (bits which must be 1) can
3487 * be set to 0, meaning that L1 may turn off any of these bits. The
3488 * reason is that if one of these bits is necessary, it will appear
3489 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3490 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003491 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003492 * These rules have exceptions below.
3493 */
3494
3495 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003496 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 msrs->pinbased_ctls_low,
3498 msrs->pinbased_ctls_high);
3499 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003500 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003501 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003502 PIN_BASED_EXT_INTR_MASK |
3503 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003504 PIN_BASED_VIRTUAL_NMIS |
3505 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003506 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003507 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003508 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003509
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003510 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003511 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003512 msrs->exit_ctls_low,
3513 msrs->exit_ctls_high);
3514 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003515 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003516
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003517 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003518#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003519 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003520#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003521 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003522 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003523 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003524 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003525 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3526
Jan Kiszka2996fca2014-06-16 13:59:43 +02003527 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003528 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003529
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003530 /* entry controls */
3531 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003532 msrs->entry_ctls_low,
3533 msrs->entry_ctls_high);
3534 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003535 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003536 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003537#ifdef CONFIG_X86_64
3538 VM_ENTRY_IA32E_MODE |
3539#endif
3540 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003542 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003543
Jan Kiszka2996fca2014-06-16 13:59:43 +02003544 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003546
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003547 /* cpu-based controls */
3548 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003549 msrs->procbased_ctls_low,
3550 msrs->procbased_ctls_high);
3551 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003552 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003553 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003554 CPU_BASED_VIRTUAL_INTR_PENDING |
3555 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003556 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3557 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3558 CPU_BASED_CR3_STORE_EXITING |
3559#ifdef CONFIG_X86_64
3560 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3561#endif
3562 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003563 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3564 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3565 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3566 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003567 /*
3568 * We can allow some features even when not supported by the
3569 * hardware. For example, L1 can specify an MSR bitmap - and we
3570 * can use it to avoid exits to L1 - even when L0 runs L2
3571 * without MSR bitmaps.
3572 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003573 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003574 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003575 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003576
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003577 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003579 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3580
Paolo Bonzini80154d72017-08-24 13:55:35 +02003581 /*
3582 * secondary cpu-based controls. Do not include those that
3583 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3584 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003585 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003586 msrs->secondary_ctls_low,
3587 msrs->secondary_ctls_high);
3588 msrs->secondary_ctls_low = 0;
3589 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003590 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003591 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003592 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003593 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003594 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003595
Liran Alon32c7acf2018-06-23 02:35:11 +03003596 /*
3597 * We can emulate "VMCS shadowing," even if the hardware
3598 * doesn't support it.
3599 */
3600 msrs->secondary_ctls_high |=
3601 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003602
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003603 if (enable_ept) {
3604 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003605 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003606 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003608 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003609 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003611 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->ept_caps &= vmx_capability.ept;
3613 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003614 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3615 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003616 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003617 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003618 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003620 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003621 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003622
Bandan Das27c42a12017-08-03 15:54:42 -04003623 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003625 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003626 /*
3627 * Advertise EPTP switching unconditionally
3628 * since we emulate it
3629 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003630 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003631 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003632 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003633 }
3634
Paolo Bonzinief697a72016-03-18 16:58:38 +01003635 /*
3636 * Old versions of KVM use the single-context version without
3637 * checking for support, so declare that it is supported even
3638 * though it is treated as global context. The alternative is
3639 * not failing the single-context invvpid, and it is worse.
3640 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003641 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003643 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003644 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003645 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003646 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003647
Radim Krčmář0790ec12015-03-17 14:02:32 +01003648 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003650 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3651
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003652 if (flexpriority_enabled)
3653 msrs->secondary_ctls_high |=
3654 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3655
Jan Kiszkac18911a2013-03-13 16:06:41 +01003656 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003657 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->misc_low,
3659 msrs->misc_high);
3660 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3661 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003662 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003663 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003664 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003665 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003666
3667 /*
3668 * This MSR reports some information about VMX support. We
3669 * should return information about the VMX we emulate for the
3670 * guest, and the VMCS structure we give it - not about the
3671 * VMX support of the underlying hardware.
3672 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003674 VMCS12_REVISION |
3675 VMX_BASIC_TRUE_CTLS |
3676 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3677 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3678
3679 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003680 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003681
3682 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003683 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003684 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3685 * We picked the standard core2 setting.
3686 */
3687#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3688#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003689 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3690 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003691
3692 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3694 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003695
3696 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003697 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003698}
3699
David Matlack38991522016-11-29 18:14:08 -08003700/*
3701 * if fixed0[i] == 1: val[i] must be 1
3702 * if fixed1[i] == 0: val[i] must be 0
3703 */
3704static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3705{
3706 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003707}
3708
3709static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3710{
David Matlack38991522016-11-29 18:14:08 -08003711 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003712}
3713
3714static inline u64 vmx_control_msr(u32 low, u32 high)
3715{
3716 return low | ((u64)high << 32);
3717}
3718
David Matlack62cc6b9d2016-11-29 18:14:07 -08003719static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3720{
3721 superset &= mask;
3722 subset &= mask;
3723
3724 return (superset | subset) == superset;
3725}
3726
3727static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3728{
3729 const u64 feature_and_reserved =
3730 /* feature (except bit 48; see below) */
3731 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3732 /* reserved */
3733 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003734 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003735
3736 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3737 return -EINVAL;
3738
3739 /*
3740 * KVM does not emulate a version of VMX that constrains physical
3741 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3742 */
3743 if (data & BIT_ULL(48))
3744 return -EINVAL;
3745
3746 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3747 vmx_basic_vmcs_revision_id(data))
3748 return -EINVAL;
3749
3750 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3751 return -EINVAL;
3752
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003753 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003754 return 0;
3755}
3756
3757static int
3758vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3759{
3760 u64 supported;
3761 u32 *lowp, *highp;
3762
3763 switch (msr_index) {
3764 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003765 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3766 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003767 break;
3768 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003769 lowp = &vmx->nested.msrs.procbased_ctls_low;
3770 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003771 break;
3772 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003773 lowp = &vmx->nested.msrs.exit_ctls_low;
3774 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003775 break;
3776 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003777 lowp = &vmx->nested.msrs.entry_ctls_low;
3778 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003779 break;
3780 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003781 lowp = &vmx->nested.msrs.secondary_ctls_low;
3782 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003783 break;
3784 default:
3785 BUG();
3786 }
3787
3788 supported = vmx_control_msr(*lowp, *highp);
3789
3790 /* Check must-be-1 bits are still 1. */
3791 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3792 return -EINVAL;
3793
3794 /* Check must-be-0 bits are still 0. */
3795 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3796 return -EINVAL;
3797
3798 *lowp = data;
3799 *highp = data >> 32;
3800 return 0;
3801}
3802
3803static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3804{
3805 const u64 feature_and_reserved_bits =
3806 /* feature */
3807 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3808 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3809 /* reserved */
3810 GENMASK_ULL(13, 9) | BIT_ULL(31);
3811 u64 vmx_misc;
3812
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003813 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3814 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003815
3816 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3817 return -EINVAL;
3818
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003819 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003820 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3821 vmx_misc_preemption_timer_rate(data) !=
3822 vmx_misc_preemption_timer_rate(vmx_misc))
3823 return -EINVAL;
3824
3825 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3826 return -EINVAL;
3827
3828 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3829 return -EINVAL;
3830
3831 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3832 return -EINVAL;
3833
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003834 vmx->nested.msrs.misc_low = data;
3835 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003836
3837 /*
3838 * If L1 has read-only VM-exit information fields, use the
3839 * less permissive vmx_vmwrite_bitmap to specify write
3840 * permissions for the shadow VMCS.
3841 */
3842 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3843 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3844
David Matlack62cc6b9d2016-11-29 18:14:07 -08003845 return 0;
3846}
3847
3848static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3849{
3850 u64 vmx_ept_vpid_cap;
3851
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003852 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3853 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003854
3855 /* Every bit is either reserved or a feature bit. */
3856 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3857 return -EINVAL;
3858
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003859 vmx->nested.msrs.ept_caps = data;
3860 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003861 return 0;
3862}
3863
3864static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3865{
3866 u64 *msr;
3867
3868 switch (msr_index) {
3869 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003870 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003871 break;
3872 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003873 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003874 break;
3875 default:
3876 BUG();
3877 }
3878
3879 /*
3880 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3881 * must be 1 in the restored value.
3882 */
3883 if (!is_bitwise_subset(data, *msr, -1ULL))
3884 return -EINVAL;
3885
3886 *msr = data;
3887 return 0;
3888}
3889
3890/*
3891 * Called when userspace is restoring VMX MSRs.
3892 *
3893 * Returns 0 on success, non-0 otherwise.
3894 */
3895static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3896{
3897 struct vcpu_vmx *vmx = to_vmx(vcpu);
3898
Jim Mattsona943ac52018-05-29 09:11:32 -07003899 /*
3900 * Don't allow changes to the VMX capability MSRs while the vCPU
3901 * is in VMX operation.
3902 */
3903 if (vmx->nested.vmxon)
3904 return -EBUSY;
3905
David Matlack62cc6b9d2016-11-29 18:14:07 -08003906 switch (msr_index) {
3907 case MSR_IA32_VMX_BASIC:
3908 return vmx_restore_vmx_basic(vmx, data);
3909 case MSR_IA32_VMX_PINBASED_CTLS:
3910 case MSR_IA32_VMX_PROCBASED_CTLS:
3911 case MSR_IA32_VMX_EXIT_CTLS:
3912 case MSR_IA32_VMX_ENTRY_CTLS:
3913 /*
3914 * The "non-true" VMX capability MSRs are generated from the
3915 * "true" MSRs, so we do not support restoring them directly.
3916 *
3917 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3918 * should restore the "true" MSRs with the must-be-1 bits
3919 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3920 * DEFAULT SETTINGS".
3921 */
3922 return -EINVAL;
3923 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3924 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3925 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3926 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3927 case MSR_IA32_VMX_PROCBASED_CTLS2:
3928 return vmx_restore_control_msr(vmx, msr_index, data);
3929 case MSR_IA32_VMX_MISC:
3930 return vmx_restore_vmx_misc(vmx, data);
3931 case MSR_IA32_VMX_CR0_FIXED0:
3932 case MSR_IA32_VMX_CR4_FIXED0:
3933 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3934 case MSR_IA32_VMX_CR0_FIXED1:
3935 case MSR_IA32_VMX_CR4_FIXED1:
3936 /*
3937 * These MSRs are generated based on the vCPU's CPUID, so we
3938 * do not support restoring them directly.
3939 */
3940 return -EINVAL;
3941 case MSR_IA32_VMX_EPT_VPID_CAP:
3942 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3943 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003944 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003945 return 0;
3946 default:
3947 /*
3948 * The rest of the VMX capability MSRs do not support restore.
3949 */
3950 return -EINVAL;
3951 }
3952}
3953
Jan Kiszkacae50132014-01-04 18:47:22 +01003954/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003955static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003956{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003957 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003958 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003959 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003960 break;
3961 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3962 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003963 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003964 msrs->pinbased_ctls_low,
3965 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003966 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3967 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003968 break;
3969 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3970 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003971 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003972 msrs->procbased_ctls_low,
3973 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003974 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3975 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003976 break;
3977 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3978 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003979 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003980 msrs->exit_ctls_low,
3981 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003982 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3983 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003984 break;
3985 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3986 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003987 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003988 msrs->entry_ctls_low,
3989 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003990 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3991 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003992 break;
3993 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003994 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003995 msrs->misc_low,
3996 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003998 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003999 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 break;
4001 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004002 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004003 break;
4004 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004005 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004006 break;
4007 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004008 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004009 break;
4010 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004011 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004012 break;
4013 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004014 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004015 msrs->secondary_ctls_low,
4016 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004017 break;
4018 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004019 *pdata = msrs->ept_caps |
4020 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004021 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004022 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004023 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004024 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004025 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004027 }
4028
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004029 return 0;
4030}
4031
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004032static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4033 uint64_t val)
4034{
4035 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4036
4037 return !(val & ~valid_bits);
4038}
4039
Tom Lendacky801e4592018-02-21 13:39:51 -06004040static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4041{
Paolo Bonzini13893092018-02-26 13:40:09 +01004042 switch (msr->index) {
4043 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4044 if (!nested)
4045 return 1;
4046 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4047 default:
4048 return 1;
4049 }
4050
4051 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004052}
4053
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004054/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055 * Reads an msr value (of 'msr_index') into 'pdata'.
4056 * Returns 0 on success, non-0 otherwise.
4057 * Assumes vcpu_load() was already called.
4058 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004059static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004061 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004062 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004065#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004067 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 break;
4069 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004070 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004072 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004073 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004074 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004075#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004077 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004078 case MSR_IA32_SPEC_CTRL:
4079 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004080 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4081 return 1;
4082
4083 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4084 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004085 case MSR_IA32_ARCH_CAPABILITIES:
4086 if (!msr_info->host_initiated &&
4087 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4088 return 1;
4089 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4090 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004092 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004093 break;
4094 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004095 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096 break;
4097 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004098 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004099 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004100 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004101 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004102 (!msr_info->host_initiated &&
4103 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004104 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004105 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004106 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004107 case MSR_IA32_MCG_EXT_CTL:
4108 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004109 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004110 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004111 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004112 msr_info->data = vcpu->arch.mcg_ext_ctl;
4113 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004114 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004115 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004116 break;
4117 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4118 if (!nested_vmx_allowed(vcpu))
4119 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004120 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4121 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004122 case MSR_IA32_XSS:
4123 if (!vmx_xsaves_supported())
4124 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004125 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004126 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004127 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004128 if (!msr_info->host_initiated &&
4129 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004130 return 1;
4131 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004133 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004134 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004135 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004136 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004138 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139 }
4140
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 return 0;
4142}
4143
Jan Kiszkacae50132014-01-04 18:47:22 +01004144static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4145
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146/*
4147 * Writes msr value into into the appropriate "register".
4148 * Returns 0 on success, non-0 otherwise.
4149 * Assumes vcpu_load() was already called.
4150 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004151static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004153 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004154 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004155 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004156 u32 msr_index = msr_info->index;
4157 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004158
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004160 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004161 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004162 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004163#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004165 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 vmcs_writel(GUEST_FS_BASE, data);
4167 break;
4168 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004169 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170 vmcs_writel(GUEST_GS_BASE, data);
4171 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004172 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004173 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004174 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175#endif
4176 case MSR_IA32_SYSENTER_CS:
4177 vmcs_write32(GUEST_SYSENTER_CS, data);
4178 break;
4179 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004180 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 break;
4182 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004183 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004185 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004186 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004187 (!msr_info->host_initiated &&
4188 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004189 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004190 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004191 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004193 vmcs_write64(GUEST_BNDCFGS, data);
4194 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004195 case MSR_IA32_SPEC_CTRL:
4196 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004197 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4198 return 1;
4199
4200 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004201 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004202 return 1;
4203
4204 vmx->spec_ctrl = data;
4205
4206 if (!data)
4207 break;
4208
4209 /*
4210 * For non-nested:
4211 * When it's written (to non-zero) for the first time, pass
4212 * it through.
4213 *
4214 * For nested:
4215 * The handling of the MSR bitmap for L2 guests is done in
4216 * nested_vmx_merge_msr_bitmap. We should not touch the
4217 * vmcs02.msr_bitmap here since it gets completely overwritten
4218 * in the merging. We update the vmcs01 here for L1 as well
4219 * since it will end up touching the MSR anyway now.
4220 */
4221 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4222 MSR_IA32_SPEC_CTRL,
4223 MSR_TYPE_RW);
4224 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004225 case MSR_IA32_PRED_CMD:
4226 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004227 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4228 return 1;
4229
4230 if (data & ~PRED_CMD_IBPB)
4231 return 1;
4232
4233 if (!data)
4234 break;
4235
4236 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4237
4238 /*
4239 * For non-nested:
4240 * When it's written (to non-zero) for the first time, pass
4241 * it through.
4242 *
4243 * For nested:
4244 * The handling of the MSR bitmap for L2 guests is done in
4245 * nested_vmx_merge_msr_bitmap. We should not touch the
4246 * vmcs02.msr_bitmap here since it gets completely overwritten
4247 * in the merging.
4248 */
4249 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4250 MSR_TYPE_W);
4251 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004252 case MSR_IA32_ARCH_CAPABILITIES:
4253 if (!msr_info->host_initiated)
4254 return 1;
4255 vmx->arch_capabilities = data;
4256 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004257 case MSR_IA32_CR_PAT:
4258 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004259 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4260 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004261 vmcs_write64(GUEST_IA32_PAT, data);
4262 vcpu->arch.pat = data;
4263 break;
4264 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004265 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004266 break;
Will Auldba904632012-11-29 12:42:50 -08004267 case MSR_IA32_TSC_ADJUST:
4268 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004269 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004270 case MSR_IA32_MCG_EXT_CTL:
4271 if ((!msr_info->host_initiated &&
4272 !(to_vmx(vcpu)->msr_ia32_feature_control &
4273 FEATURE_CONTROL_LMCE)) ||
4274 (data & ~MCG_EXT_CTL_LMCE_EN))
4275 return 1;
4276 vcpu->arch.mcg_ext_ctl = data;
4277 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004278 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004279 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004280 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004281 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4282 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004283 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004284 if (msr_info->host_initiated && data == 0)
4285 vmx_leave_nested(vcpu);
4286 break;
4287 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004288 if (!msr_info->host_initiated)
4289 return 1; /* they are read-only */
4290 if (!nested_vmx_allowed(vcpu))
4291 return 1;
4292 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004293 case MSR_IA32_XSS:
4294 if (!vmx_xsaves_supported())
4295 return 1;
4296 /*
4297 * The only supported bit as of Skylake is bit 8, but
4298 * it is not supported on KVM.
4299 */
4300 if (data != 0)
4301 return 1;
4302 vcpu->arch.ia32_xss = data;
4303 if (vcpu->arch.ia32_xss != host_xss)
4304 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004305 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004306 else
4307 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4308 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004309 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004310 if (!msr_info->host_initiated &&
4311 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004312 return 1;
4313 /* Check reserved bit, higher 32 bits should be zero */
4314 if ((data >> 32) != 0)
4315 return 1;
4316 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004317 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004318 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004319 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004320 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004321 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004322 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4323 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004324 ret = kvm_set_shared_msr(msr->index, msr->data,
4325 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004326 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004327 if (ret)
4328 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004329 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004330 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004332 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333 }
4334
Eddie Dong2cc51562007-05-21 07:28:09 +03004335 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336}
4337
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004338static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004339{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004340 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4341 switch (reg) {
4342 case VCPU_REGS_RSP:
4343 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4344 break;
4345 case VCPU_REGS_RIP:
4346 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4347 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004348 case VCPU_EXREG_PDPTR:
4349 if (enable_ept)
4350 ept_save_pdptrs(vcpu);
4351 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004352 default:
4353 break;
4354 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004355}
4356
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357static __init int cpu_has_kvm_support(void)
4358{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004359 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360}
4361
4362static __init int vmx_disabled_by_bios(void)
4363{
4364 u64 msr;
4365
4366 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004367 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004368 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004369 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4370 && tboot_enabled())
4371 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004372 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004373 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004374 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004375 && !tboot_enabled()) {
4376 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004377 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004378 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004379 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004380 /* launched w/o TXT and VMX disabled */
4381 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4382 && !tboot_enabled())
4383 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004384 }
4385
4386 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387}
4388
Dongxiao Xu7725b892010-05-11 18:29:38 +08004389static void kvm_cpu_vmxon(u64 addr)
4390{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004391 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004392 intel_pt_handle_vmx(1);
4393
Dongxiao Xu7725b892010-05-11 18:29:38 +08004394 asm volatile (ASM_VMX_VMXON_RAX
4395 : : "a"(&addr), "m"(addr)
4396 : "memory", "cc");
4397}
4398
Radim Krčmář13a34e02014-08-28 15:13:03 +02004399static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400{
4401 int cpu = raw_smp_processor_id();
4402 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004403 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004404
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004405 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004406 return -EBUSY;
4407
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004408 /*
4409 * This can happen if we hot-added a CPU but failed to allocate
4410 * VP assist page for it.
4411 */
4412 if (static_branch_unlikely(&enable_evmcs) &&
4413 !hv_get_vp_assist_page(cpu))
4414 return -EFAULT;
4415
Nadav Har'Eld462b812011-05-24 15:26:10 +03004416 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004417 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4418 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004419
4420 /*
4421 * Now we can enable the vmclear operation in kdump
4422 * since the loaded_vmcss_on_cpu list on this cpu
4423 * has been initialized.
4424 *
4425 * Though the cpu is not in VMX operation now, there
4426 * is no problem to enable the vmclear operation
4427 * for the loaded_vmcss_on_cpu list is empty!
4428 */
4429 crash_enable_local_vmclear(cpu);
4430
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004432
4433 test_bits = FEATURE_CONTROL_LOCKED;
4434 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4435 if (tboot_enabled())
4436 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4437
4438 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004440 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4441 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004442 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004443 if (enable_ept)
4444 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004445
4446 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004447}
4448
Nadav Har'Eld462b812011-05-24 15:26:10 +03004449static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004450{
4451 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004452 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004453
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4455 loaded_vmcss_on_cpu_link)
4456 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004457}
4458
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004459
4460/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4461 * tricks.
4462 */
4463static void kvm_cpu_vmxoff(void)
4464{
4465 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004466
4467 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004468 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004469}
4470
Radim Krčmář13a34e02014-08-28 15:13:03 +02004471static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004473 vmclear_local_loaded_vmcss();
4474 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475}
4476
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004477static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004478 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004479{
4480 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004481 u32 ctl = ctl_min | ctl_opt;
4482
4483 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4484
4485 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4486 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4487
4488 /* Ensure minimum (required) set of control bits are supported. */
4489 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004490 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004491
4492 *result = ctl;
4493 return 0;
4494}
4495
Avi Kivity110312c2010-12-21 12:54:20 +02004496static __init bool allow_1_setting(u32 msr, u32 ctl)
4497{
4498 u32 vmx_msr_low, vmx_msr_high;
4499
4500 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4501 return vmx_msr_high & ctl;
4502}
4503
Yang, Sheng002c7f72007-07-31 14:23:01 +03004504static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004505{
4506 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004507 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004508 u32 _pin_based_exec_control = 0;
4509 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004510 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004511 u32 _vmexit_control = 0;
4512 u32 _vmentry_control = 0;
4513
Paolo Bonzini13893092018-02-26 13:40:09 +01004514 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304515 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516#ifdef CONFIG_X86_64
4517 CPU_BASED_CR8_LOAD_EXITING |
4518 CPU_BASED_CR8_STORE_EXITING |
4519#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004520 CPU_BASED_CR3_LOAD_EXITING |
4521 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004522 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004523 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004524 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004525 CPU_BASED_MWAIT_EXITING |
4526 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004527 CPU_BASED_INVLPG_EXITING |
4528 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004529
Sheng Yangf78e0e22007-10-29 09:40:42 +08004530 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004531 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004532 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004533 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4534 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004535 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004536#ifdef CONFIG_X86_64
4537 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4538 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4539 ~CPU_BASED_CR8_STORE_EXITING;
4540#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004541 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004542 min2 = 0;
4543 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004544 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004545 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004546 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004547 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004548 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004549 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004550 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004551 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004552 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004553 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004554 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004555 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004556 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004557 SECONDARY_EXEC_RDSEED_EXITING |
4558 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004559 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004560 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004561 SECONDARY_EXEC_ENABLE_VMFUNC |
4562 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004563 if (adjust_vmx_controls(min2, opt2,
4564 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004565 &_cpu_based_2nd_exec_control) < 0)
4566 return -EIO;
4567 }
4568#ifndef CONFIG_X86_64
4569 if (!(_cpu_based_2nd_exec_control &
4570 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4571 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4572#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004573
4574 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4575 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004576 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004577 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4578 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004579
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004580 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4581 &vmx_capability.ept, &vmx_capability.vpid);
4582
Sheng Yangd56f5462008-04-25 10:13:16 +08004583 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004584 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4585 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004586 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4587 CPU_BASED_CR3_STORE_EXITING |
4588 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004589 } else if (vmx_capability.ept) {
4590 vmx_capability.ept = 0;
4591 pr_warn_once("EPT CAP should not exist if not support "
4592 "1-setting enable EPT VM-execution control\n");
4593 }
4594 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4595 vmx_capability.vpid) {
4596 vmx_capability.vpid = 0;
4597 pr_warn_once("VPID CAP should not exist if not support "
4598 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004599 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004600
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004601 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004602#ifdef CONFIG_X86_64
4603 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4604#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004605 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004606 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004607 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4608 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004609 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004610
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004611 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4612 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4613 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004614 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4615 &_pin_based_exec_control) < 0)
4616 return -EIO;
4617
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004618 if (cpu_has_broken_vmx_preemption_timer())
4619 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004620 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004621 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004622 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4623
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004624 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004625 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004626 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4627 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004628 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004630 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631
4632 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4633 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004634 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004635
4636#ifdef CONFIG_X86_64
4637 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4638 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004639 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004640#endif
4641
4642 /* Require Write-Back (WB) memory type for VMCS accesses. */
4643 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645
Yang, Sheng002c7f72007-07-31 14:23:01 +03004646 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004647 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004648 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004649
Liran Alon2307af12018-06-29 22:59:04 +03004650 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004651
Yang, Sheng002c7f72007-07-31 14:23:01 +03004652 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4653 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004654 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004655 vmcs_conf->vmexit_ctrl = _vmexit_control;
4656 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004657
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004658 if (static_branch_unlikely(&enable_evmcs))
4659 evmcs_sanitize_exec_ctrls(vmcs_conf);
4660
Avi Kivity110312c2010-12-21 12:54:20 +02004661 cpu_has_load_ia32_efer =
4662 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4663 VM_ENTRY_LOAD_IA32_EFER)
4664 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4665 VM_EXIT_LOAD_IA32_EFER);
4666
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004667 cpu_has_load_perf_global_ctrl =
4668 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4669 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4670 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4671 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4672
4673 /*
4674 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004675 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004676 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4677 *
4678 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4679 *
4680 * AAK155 (model 26)
4681 * AAP115 (model 30)
4682 * AAT100 (model 37)
4683 * BC86,AAY89,BD102 (model 44)
4684 * BA97 (model 46)
4685 *
4686 */
4687 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4688 switch (boot_cpu_data.x86_model) {
4689 case 26:
4690 case 30:
4691 case 37:
4692 case 44:
4693 case 46:
4694 cpu_has_load_perf_global_ctrl = false;
4695 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4696 "does not work properly. Using workaround\n");
4697 break;
4698 default:
4699 break;
4700 }
4701 }
4702
Borislav Petkov782511b2016-04-04 22:25:03 +02004703 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004704 rdmsrl(MSR_IA32_XSS, host_xss);
4705
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004706 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004707}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708
Liran Alon491a6032018-06-23 02:35:12 +03004709static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710{
4711 int node = cpu_to_node(cpu);
4712 struct page *pages;
4713 struct vmcs *vmcs;
4714
Vlastimil Babka96db8002015-09-08 15:03:50 -07004715 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716 if (!pages)
4717 return NULL;
4718 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004719 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004720
4721 /* KVM supports Enlightened VMCS v1 only */
4722 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004723 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004724 else
Liran Alon392b2f22018-06-23 02:35:01 +03004725 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004726
Liran Alon491a6032018-06-23 02:35:12 +03004727 if (shadow)
4728 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729 return vmcs;
4730}
4731
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732static void free_vmcs(struct vmcs *vmcs)
4733{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004734 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735}
4736
Nadav Har'Eld462b812011-05-24 15:26:10 +03004737/*
4738 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4739 */
4740static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4741{
4742 if (!loaded_vmcs->vmcs)
4743 return;
4744 loaded_vmcs_clear(loaded_vmcs);
4745 free_vmcs(loaded_vmcs->vmcs);
4746 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004747 if (loaded_vmcs->msr_bitmap)
4748 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004749 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004750}
4751
Liran Alon491a6032018-06-23 02:35:12 +03004752static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004753{
Liran Alon491a6032018-06-23 02:35:12 +03004754 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004755}
4756
4757static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4758{
Liran Alon491a6032018-06-23 02:35:12 +03004759 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004760 if (!loaded_vmcs->vmcs)
4761 return -ENOMEM;
4762
4763 loaded_vmcs->shadow_vmcs = NULL;
4764 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004765
4766 if (cpu_has_vmx_msr_bitmap()) {
4767 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4768 if (!loaded_vmcs->msr_bitmap)
4769 goto out_vmcs;
4770 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004771
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004772 if (IS_ENABLED(CONFIG_HYPERV) &&
4773 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004774 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4775 struct hv_enlightened_vmcs *evmcs =
4776 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4777
4778 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4779 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004780 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004781
4782 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4783
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004784 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004785
4786out_vmcs:
4787 free_loaded_vmcs(loaded_vmcs);
4788 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004789}
4790
Sam Ravnborg39959582007-06-01 00:47:13 -07004791static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792{
4793 int cpu;
4794
Zachary Amsden3230bb42009-09-29 11:38:37 -10004795 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004797 per_cpu(vmxarea, cpu) = NULL;
4798 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799}
4800
Jim Mattsond37f4262017-12-22 12:12:16 -08004801enum vmcs_field_width {
4802 VMCS_FIELD_WIDTH_U16 = 0,
4803 VMCS_FIELD_WIDTH_U64 = 1,
4804 VMCS_FIELD_WIDTH_U32 = 2,
4805 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004806};
4807
Jim Mattsond37f4262017-12-22 12:12:16 -08004808static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004809{
4810 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004811 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004812 return (field >> 13) & 0x3 ;
4813}
4814
4815static inline int vmcs_field_readonly(unsigned long field)
4816{
4817 return (((field >> 10) & 0x3) == 1);
4818}
4819
Bandan Dasfe2b2012014-04-21 15:20:14 -04004820static void init_vmcs_shadow_fields(void)
4821{
4822 int i, j;
4823
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004824 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4825 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004826 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004827 (i + 1 == max_shadow_read_only_fields ||
4828 shadow_read_only_fields[i + 1] != field + 1))
4829 pr_err("Missing field from shadow_read_only_field %x\n",
4830 field + 1);
4831
4832 clear_bit(field, vmx_vmread_bitmap);
4833#ifdef CONFIG_X86_64
4834 if (field & 1)
4835 continue;
4836#endif
4837 if (j < i)
4838 shadow_read_only_fields[j] = field;
4839 j++;
4840 }
4841 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004842
4843 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004844 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004845 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004846 (i + 1 == max_shadow_read_write_fields ||
4847 shadow_read_write_fields[i + 1] != field + 1))
4848 pr_err("Missing field from shadow_read_write_field %x\n",
4849 field + 1);
4850
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004851 /*
4852 * PML and the preemption timer can be emulated, but the
4853 * processor cannot vmwrite to fields that don't exist
4854 * on bare metal.
4855 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004856 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004857 case GUEST_PML_INDEX:
4858 if (!cpu_has_vmx_pml())
4859 continue;
4860 break;
4861 case VMX_PREEMPTION_TIMER_VALUE:
4862 if (!cpu_has_vmx_preemption_timer())
4863 continue;
4864 break;
4865 case GUEST_INTR_STATUS:
4866 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004867 continue;
4868 break;
4869 default:
4870 break;
4871 }
4872
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004873 clear_bit(field, vmx_vmwrite_bitmap);
4874 clear_bit(field, vmx_vmread_bitmap);
4875#ifdef CONFIG_X86_64
4876 if (field & 1)
4877 continue;
4878#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004879 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004880 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004881 j++;
4882 }
4883 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004884}
4885
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886static __init int alloc_kvm_area(void)
4887{
4888 int cpu;
4889
Zachary Amsden3230bb42009-09-29 11:38:37 -10004890 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 struct vmcs *vmcs;
4892
Liran Alon491a6032018-06-23 02:35:12 +03004893 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 if (!vmcs) {
4895 free_kvm_area();
4896 return -ENOMEM;
4897 }
4898
Liran Alon2307af12018-06-29 22:59:04 +03004899 /*
4900 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4901 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4902 * revision_id reported by MSR_IA32_VMX_BASIC.
4903 *
4904 * However, even though not explictly documented by
4905 * TLFS, VMXArea passed as VMXON argument should
4906 * still be marked with revision_id reported by
4907 * physical CPU.
4908 */
4909 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004910 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004911
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912 per_cpu(vmxarea, cpu) = vmcs;
4913 }
4914 return 0;
4915}
4916
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004917static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004918 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004920 if (!emulate_invalid_guest_state) {
4921 /*
4922 * CS and SS RPL should be equal during guest entry according
4923 * to VMX spec, but in reality it is not always so. Since vcpu
4924 * is in the middle of the transition from real mode to
4925 * protected mode it is safe to assume that RPL 0 is a good
4926 * default value.
4927 */
4928 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004929 save->selector &= ~SEGMENT_RPL_MASK;
4930 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004931 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004932 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004933 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934}
4935
4936static void enter_pmode(struct kvm_vcpu *vcpu)
4937{
4938 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004939 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940
Gleb Natapovd99e4152012-12-20 16:57:45 +02004941 /*
4942 * Update real mode segment cache. It may be not up-to-date if sement
4943 * register was written while vcpu was in a guest mode.
4944 */
4945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4951
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004952 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953
Avi Kivity2fb92db2011-04-27 19:42:18 +03004954 vmx_segment_cache_clear(vmx);
4955
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004956 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957
4958 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004959 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4960 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961 vmcs_writel(GUEST_RFLAGS, flags);
4962
Rusty Russell66aee912007-07-17 23:34:16 +10004963 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4964 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965
4966 update_exception_bitmap(vcpu);
4967
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004968 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4969 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4970 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4971 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4972 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4973 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974}
4975
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004976static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977{
Mathias Krause772e0312012-08-30 01:30:19 +02004978 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004979 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980
Gleb Natapovd99e4152012-12-20 16:57:45 +02004981 var.dpl = 0x3;
4982 if (seg == VCPU_SREG_CS)
4983 var.type = 0x3;
4984
4985 if (!emulate_invalid_guest_state) {
4986 var.selector = var.base >> 4;
4987 var.base = var.base & 0xffff0;
4988 var.limit = 0xffff;
4989 var.g = 0;
4990 var.db = 0;
4991 var.present = 1;
4992 var.s = 1;
4993 var.l = 0;
4994 var.unusable = 0;
4995 var.type = 0x3;
4996 var.avl = 0;
4997 if (save->base & 0xf)
4998 printk_once(KERN_WARNING "kvm: segment base is not "
4999 "paragraph aligned when entering "
5000 "protected mode (seg=%d)", seg);
5001 }
5002
5003 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005004 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005005 vmcs_write32(sf->limit, var.limit);
5006 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007}
5008
5009static void enter_rmode(struct kvm_vcpu *vcpu)
5010{
5011 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005012 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005013 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005022
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005023 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005024
Gleb Natapov776e58e2011-03-13 12:34:27 +02005025 /*
5026 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005027 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005028 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005029 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005030 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5031 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005032
Avi Kivity2fb92db2011-04-27 19:42:18 +03005033 vmx_segment_cache_clear(vmx);
5034
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005035 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5038
5039 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005040 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005042 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005043
5044 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005045 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 update_exception_bitmap(vcpu);
5047
Gleb Natapovd99e4152012-12-20 16:57:45 +02005048 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5049 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5050 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5051 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5052 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5053 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005054
Eddie Dong8668a3c2007-10-10 14:26:45 +08005055 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056}
5057
Amit Shah401d10d2009-02-20 22:53:37 +05305058static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5059{
5060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005061 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5062
5063 if (!msr)
5064 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305065
Avi Kivityf6801df2010-01-21 15:31:50 +02005066 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305067 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005068 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305069 msr->data = efer;
5070 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005071 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305072
5073 msr->data = efer & ~EFER_LME;
5074 }
5075 setup_msrs(vmx);
5076}
5077
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005078#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079
5080static void enter_lmode(struct kvm_vcpu *vcpu)
5081{
5082 u32 guest_tr_ar;
5083
Avi Kivity2fb92db2011-04-27 19:42:18 +03005084 vmx_segment_cache_clear(to_vmx(vcpu));
5085
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005087 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005088 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5089 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005090 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005091 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5092 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005093 }
Avi Kivityda38f432010-07-06 11:30:49 +03005094 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095}
5096
5097static void exit_lmode(struct kvm_vcpu *vcpu)
5098{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005099 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005100 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005101}
5102
5103#endif
5104
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005105static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5106 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005107{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005108 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005109 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5110 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005111 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005112 } else {
5113 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005114 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005115}
5116
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005117static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005118{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005119 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005120}
5121
Junaid Shahidfaff8752018-06-29 13:10:05 -07005122static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5123{
5124 int vpid = to_vmx(vcpu)->vpid;
5125
5126 if (!vpid_sync_vcpu_addr(vpid, addr))
5127 vpid_sync_context(vpid);
5128
5129 /*
5130 * If VPIDs are not supported or enabled, then the above is a no-op.
5131 * But we don't really need a TLB flush in that case anyway, because
5132 * each VM entry/exit includes an implicit flush when VPID is 0.
5133 */
5134}
5135
Avi Kivitye8467fd2009-12-29 18:43:06 +02005136static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5137{
5138 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5139
5140 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5141 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5142}
5143
Avi Kivityaff48ba2010-12-05 18:56:11 +02005144static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5145{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005146 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005147 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5148 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5149}
5150
Anthony Liguori25c4c272007-04-27 09:29:21 +03005151static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005152{
Avi Kivityfc78f512009-12-07 12:16:48 +02005153 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5154
5155 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5156 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005157}
5158
Sheng Yang14394422008-04-28 12:24:45 +08005159static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5160{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005161 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5162
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005163 if (!test_bit(VCPU_EXREG_PDPTR,
5164 (unsigned long *)&vcpu->arch.regs_dirty))
5165 return;
5166
Sheng Yang14394422008-04-28 12:24:45 +08005167 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005168 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5169 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5170 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5171 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005172 }
5173}
5174
Avi Kivity8f5d5492009-05-31 18:41:29 +03005175static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5176{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005177 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5178
Avi Kivity8f5d5492009-05-31 18:41:29 +03005179 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005180 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5181 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5182 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5183 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005184 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005185
5186 __set_bit(VCPU_EXREG_PDPTR,
5187 (unsigned long *)&vcpu->arch.regs_avail);
5188 __set_bit(VCPU_EXREG_PDPTR,
5189 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005190}
5191
David Matlack38991522016-11-29 18:14:08 -08005192static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5193{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005194 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5195 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005196 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5197
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005198 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005199 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5200 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5201 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5202
5203 return fixed_bits_valid(val, fixed0, fixed1);
5204}
5205
5206static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5207{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005208 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5209 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005210
5211 return fixed_bits_valid(val, fixed0, fixed1);
5212}
5213
5214static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5215{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005216 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5217 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005218
5219 return fixed_bits_valid(val, fixed0, fixed1);
5220}
5221
5222/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5223#define nested_guest_cr4_valid nested_cr4_valid
5224#define nested_host_cr4_valid nested_cr4_valid
5225
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005226static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005227
5228static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5229 unsigned long cr0,
5230 struct kvm_vcpu *vcpu)
5231{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005232 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5233 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005234 if (!(cr0 & X86_CR0_PG)) {
5235 /* From paging/starting to nonpaging */
5236 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005237 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005238 (CPU_BASED_CR3_LOAD_EXITING |
5239 CPU_BASED_CR3_STORE_EXITING));
5240 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005241 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005242 } else if (!is_paging(vcpu)) {
5243 /* From nonpaging to paging */
5244 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005245 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005246 ~(CPU_BASED_CR3_LOAD_EXITING |
5247 CPU_BASED_CR3_STORE_EXITING));
5248 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005249 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005250 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005251
5252 if (!(cr0 & X86_CR0_WP))
5253 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005254}
5255
Avi Kivity6aa8b732006-12-10 02:21:36 -08005256static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5257{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005258 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005259 unsigned long hw_cr0;
5260
Sean Christopherson3de63472018-07-13 08:42:30 -07005261 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005262 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005263 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005264 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005265 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005266
Gleb Natapov218e7632013-01-21 15:36:45 +02005267 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5268 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005269
Gleb Natapov218e7632013-01-21 15:36:45 +02005270 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5271 enter_rmode(vcpu);
5272 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005274#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005275 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005276 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005277 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005278 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279 exit_lmode(vcpu);
5280 }
5281#endif
5282
Sean Christophersonb4d18512018-03-05 12:04:40 -08005283 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005284 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5285
Avi Kivity6aa8b732006-12-10 02:21:36 -08005286 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005287 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005288 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005289
5290 /* depends on vcpu->arch.cr0 to be set to a new value */
5291 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292}
5293
Yu Zhang855feb62017-08-24 20:27:55 +08005294static int get_ept_level(struct kvm_vcpu *vcpu)
5295{
5296 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5297 return 5;
5298 return 4;
5299}
5300
Peter Feiner995f00a2017-06-30 17:26:32 -07005301static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005302{
Yu Zhang855feb62017-08-24 20:27:55 +08005303 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005304
Yu Zhang855feb62017-08-24 20:27:55 +08005305 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005306
Peter Feiner995f00a2017-06-30 17:26:32 -07005307 if (enable_ept_ad_bits &&
5308 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005309 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005310 eptp |= (root_hpa & PAGE_MASK);
5311
5312 return eptp;
5313}
5314
Avi Kivity6aa8b732006-12-10 02:21:36 -08005315static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5316{
Tianyu Lan877ad952018-07-19 08:40:23 +00005317 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005318 unsigned long guest_cr3;
5319 u64 eptp;
5320
5321 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005322 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005323 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005324 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005325
5326 if (kvm_x86_ops->tlb_remote_flush) {
5327 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5328 to_vmx(vcpu)->ept_pointer = eptp;
5329 to_kvm_vmx(kvm)->ept_pointers_match
5330 = EPT_POINTERS_CHECK;
5331 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5332 }
5333
Sean Christophersone90008d2018-03-05 12:04:37 -08005334 if (enable_unrestricted_guest || is_paging(vcpu) ||
5335 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005336 guest_cr3 = kvm_read_cr3(vcpu);
5337 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005338 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005339 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005340 }
5341
Sheng Yang14394422008-04-28 12:24:45 +08005342 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343}
5344
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005345static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005346{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005347 /*
5348 * Pass through host's Machine Check Enable value to hw_cr4, which
5349 * is in force while we are in guest mode. Do not let guests control
5350 * this bit, even if host CR4.MCE == 0.
5351 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005352 unsigned long hw_cr4;
5353
5354 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5355 if (enable_unrestricted_guest)
5356 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5357 else if (to_vmx(vcpu)->rmode.vm86_active)
5358 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5359 else
5360 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005361
Sean Christopherson64f7a112018-04-30 10:01:06 -07005362 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5363 if (cr4 & X86_CR4_UMIP) {
5364 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005365 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005366 hw_cr4 &= ~X86_CR4_UMIP;
5367 } else if (!is_guest_mode(vcpu) ||
5368 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5369 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5370 SECONDARY_EXEC_DESC);
5371 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005372
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005373 if (cr4 & X86_CR4_VMXE) {
5374 /*
5375 * To use VMXON (and later other VMX instructions), a guest
5376 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5377 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005378 * is here. We operate under the default treatment of SMM,
5379 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005380 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005381 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005382 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005383 }
David Matlack38991522016-11-29 18:14:08 -08005384
5385 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005386 return 1;
5387
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005388 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005389
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005390 if (!enable_unrestricted_guest) {
5391 if (enable_ept) {
5392 if (!is_paging(vcpu)) {
5393 hw_cr4 &= ~X86_CR4_PAE;
5394 hw_cr4 |= X86_CR4_PSE;
5395 } else if (!(cr4 & X86_CR4_PAE)) {
5396 hw_cr4 &= ~X86_CR4_PAE;
5397 }
5398 }
5399
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005400 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005401 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5402 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5403 * to be manually disabled when guest switches to non-paging
5404 * mode.
5405 *
5406 * If !enable_unrestricted_guest, the CPU is always running
5407 * with CR0.PG=1 and CR4 needs to be modified.
5408 * If enable_unrestricted_guest, the CPU automatically
5409 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005410 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005411 if (!is_paging(vcpu))
5412 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5413 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005414
Sheng Yang14394422008-04-28 12:24:45 +08005415 vmcs_writel(CR4_READ_SHADOW, cr4);
5416 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005417 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005418}
5419
Avi Kivity6aa8b732006-12-10 02:21:36 -08005420static void vmx_get_segment(struct kvm_vcpu *vcpu,
5421 struct kvm_segment *var, int seg)
5422{
Avi Kivitya9179492011-01-03 14:28:52 +02005423 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005424 u32 ar;
5425
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005426 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005427 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005428 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005429 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005430 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005431 var->base = vmx_read_guest_seg_base(vmx, seg);
5432 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5433 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005434 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005435 var->base = vmx_read_guest_seg_base(vmx, seg);
5436 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5437 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5438 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005439 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005440 var->type = ar & 15;
5441 var->s = (ar >> 4) & 1;
5442 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005443 /*
5444 * Some userspaces do not preserve unusable property. Since usable
5445 * segment has to be present according to VMX spec we can use present
5446 * property to amend userspace bug by making unusable segment always
5447 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5448 * segment as unusable.
5449 */
5450 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451 var->avl = (ar >> 12) & 1;
5452 var->l = (ar >> 13) & 1;
5453 var->db = (ar >> 14) & 1;
5454 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005455}
5456
Avi Kivitya9179492011-01-03 14:28:52 +02005457static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5458{
Avi Kivitya9179492011-01-03 14:28:52 +02005459 struct kvm_segment s;
5460
5461 if (to_vmx(vcpu)->rmode.vm86_active) {
5462 vmx_get_segment(vcpu, &s, seg);
5463 return s.base;
5464 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005465 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005466}
5467
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005468static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005469{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005470 struct vcpu_vmx *vmx = to_vmx(vcpu);
5471
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005472 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005473 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005474 else {
5475 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005476 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005477 }
Avi Kivity69c73022011-03-07 15:26:44 +02005478}
5479
Avi Kivity653e3102007-05-07 10:55:37 +03005480static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005482 u32 ar;
5483
Avi Kivityf0495f92012-06-07 17:06:10 +03005484 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005485 ar = 1 << 16;
5486 else {
5487 ar = var->type & 15;
5488 ar |= (var->s & 1) << 4;
5489 ar |= (var->dpl & 3) << 5;
5490 ar |= (var->present & 1) << 7;
5491 ar |= (var->avl & 1) << 12;
5492 ar |= (var->l & 1) << 13;
5493 ar |= (var->db & 1) << 14;
5494 ar |= (var->g & 1) << 15;
5495 }
Avi Kivity653e3102007-05-07 10:55:37 +03005496
5497 return ar;
5498}
5499
5500static void vmx_set_segment(struct kvm_vcpu *vcpu,
5501 struct kvm_segment *var, int seg)
5502{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005503 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005504 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005505
Avi Kivity2fb92db2011-04-27 19:42:18 +03005506 vmx_segment_cache_clear(vmx);
5507
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005508 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5509 vmx->rmode.segs[seg] = *var;
5510 if (seg == VCPU_SREG_TR)
5511 vmcs_write16(sf->selector, var->selector);
5512 else if (var->s)
5513 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005514 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005515 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005516
Avi Kivity653e3102007-05-07 10:55:37 +03005517 vmcs_writel(sf->base, var->base);
5518 vmcs_write32(sf->limit, var->limit);
5519 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005520
5521 /*
5522 * Fix the "Accessed" bit in AR field of segment registers for older
5523 * qemu binaries.
5524 * IA32 arch specifies that at the time of processor reset the
5525 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005526 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005527 * state vmexit when "unrestricted guest" mode is turned on.
5528 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5529 * tree. Newer qemu binaries with that qemu fix would not need this
5530 * kvm hack.
5531 */
5532 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005533 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005534
Gleb Natapovf924d662012-12-12 19:10:55 +02005535 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005536
5537out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005538 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005539}
5540
Avi Kivity6aa8b732006-12-10 02:21:36 -08005541static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5542{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005543 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005544
5545 *db = (ar >> 14) & 1;
5546 *l = (ar >> 13) & 1;
5547}
5548
Gleb Natapov89a27f42010-02-16 10:51:48 +02005549static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005551 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5552 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005553}
5554
Gleb Natapov89a27f42010-02-16 10:51:48 +02005555static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005557 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5558 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559}
5560
Gleb Natapov89a27f42010-02-16 10:51:48 +02005561static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005563 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5564 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005565}
5566
Gleb Natapov89a27f42010-02-16 10:51:48 +02005567static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005568{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005569 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5570 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005571}
5572
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005573static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5574{
5575 struct kvm_segment var;
5576 u32 ar;
5577
5578 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005579 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005580 if (seg == VCPU_SREG_CS)
5581 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005582 ar = vmx_segment_access_rights(&var);
5583
5584 if (var.base != (var.selector << 4))
5585 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005586 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005587 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005588 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005589 return false;
5590
5591 return true;
5592}
5593
5594static bool code_segment_valid(struct kvm_vcpu *vcpu)
5595{
5596 struct kvm_segment cs;
5597 unsigned int cs_rpl;
5598
5599 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005600 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005601
Avi Kivity1872a3f2009-01-04 23:26:52 +02005602 if (cs.unusable)
5603 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005604 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005605 return false;
5606 if (!cs.s)
5607 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005608 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005609 if (cs.dpl > cs_rpl)
5610 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005611 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005612 if (cs.dpl != cs_rpl)
5613 return false;
5614 }
5615 if (!cs.present)
5616 return false;
5617
5618 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5619 return true;
5620}
5621
5622static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5623{
5624 struct kvm_segment ss;
5625 unsigned int ss_rpl;
5626
5627 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005628 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005629
Avi Kivity1872a3f2009-01-04 23:26:52 +02005630 if (ss.unusable)
5631 return true;
5632 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005633 return false;
5634 if (!ss.s)
5635 return false;
5636 if (ss.dpl != ss_rpl) /* DPL != RPL */
5637 return false;
5638 if (!ss.present)
5639 return false;
5640
5641 return true;
5642}
5643
5644static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5645{
5646 struct kvm_segment var;
5647 unsigned int rpl;
5648
5649 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005650 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005651
Avi Kivity1872a3f2009-01-04 23:26:52 +02005652 if (var.unusable)
5653 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005654 if (!var.s)
5655 return false;
5656 if (!var.present)
5657 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005658 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005659 if (var.dpl < rpl) /* DPL < RPL */
5660 return false;
5661 }
5662
5663 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5664 * rights flags
5665 */
5666 return true;
5667}
5668
5669static bool tr_valid(struct kvm_vcpu *vcpu)
5670{
5671 struct kvm_segment tr;
5672
5673 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5674
Avi Kivity1872a3f2009-01-04 23:26:52 +02005675 if (tr.unusable)
5676 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005677 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005678 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005679 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005680 return false;
5681 if (!tr.present)
5682 return false;
5683
5684 return true;
5685}
5686
5687static bool ldtr_valid(struct kvm_vcpu *vcpu)
5688{
5689 struct kvm_segment ldtr;
5690
5691 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5692
Avi Kivity1872a3f2009-01-04 23:26:52 +02005693 if (ldtr.unusable)
5694 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005695 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005696 return false;
5697 if (ldtr.type != 2)
5698 return false;
5699 if (!ldtr.present)
5700 return false;
5701
5702 return true;
5703}
5704
5705static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5706{
5707 struct kvm_segment cs, ss;
5708
5709 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5710 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5711
Nadav Amitb32a9912015-03-29 16:33:04 +03005712 return ((cs.selector & SEGMENT_RPL_MASK) ==
5713 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005714}
5715
5716/*
5717 * Check if guest state is valid. Returns true if valid, false if
5718 * not.
5719 * We assume that registers are always usable
5720 */
5721static bool guest_state_valid(struct kvm_vcpu *vcpu)
5722{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005723 if (enable_unrestricted_guest)
5724 return true;
5725
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005726 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005727 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005728 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5729 return false;
5730 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5731 return false;
5732 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5733 return false;
5734 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5735 return false;
5736 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5737 return false;
5738 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5739 return false;
5740 } else {
5741 /* protected mode guest state checks */
5742 if (!cs_ss_rpl_check(vcpu))
5743 return false;
5744 if (!code_segment_valid(vcpu))
5745 return false;
5746 if (!stack_segment_valid(vcpu))
5747 return false;
5748 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5749 return false;
5750 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5751 return false;
5752 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5753 return false;
5754 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5755 return false;
5756 if (!tr_valid(vcpu))
5757 return false;
5758 if (!ldtr_valid(vcpu))
5759 return false;
5760 }
5761 /* TODO:
5762 * - Add checks on RIP
5763 * - Add checks on RFLAGS
5764 */
5765
5766 return true;
5767}
5768
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005769static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5770{
5771 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5772}
5773
Mike Dayd77c26f2007-10-08 09:02:08 -04005774static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005776 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005777 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005778 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005779
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005780 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005781 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005782 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5783 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005784 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005785 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005786 r = kvm_write_guest_page(kvm, fn++, &data,
5787 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005788 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005789 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005790 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5791 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005792 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005793 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5794 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005795 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005796 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005797 r = kvm_write_guest_page(kvm, fn, &data,
5798 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5799 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005800out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005801 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005802 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005803}
5804
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005805static int init_rmode_identity_map(struct kvm *kvm)
5806{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005807 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005808 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005809 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005810 u32 tmp;
5811
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005812 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005813 mutex_lock(&kvm->slots_lock);
5814
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005815 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005816 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005817
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005818 if (!kvm_vmx->ept_identity_map_addr)
5819 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5820 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005821
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005822 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005823 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005824 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005825 goto out2;
5826
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005827 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005828 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5829 if (r < 0)
5830 goto out;
5831 /* Set up identity-mapping pagetable for EPT in real mode */
5832 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5833 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5834 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5835 r = kvm_write_guest_page(kvm, identity_map_pfn,
5836 &tmp, i * sizeof(tmp), sizeof(tmp));
5837 if (r < 0)
5838 goto out;
5839 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005840 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005841
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005842out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005843 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005844
5845out2:
5846 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005847 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005848}
5849
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850static void seg_setup(int seg)
5851{
Mathias Krause772e0312012-08-30 01:30:19 +02005852 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005853 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005854
5855 vmcs_write16(sf->selector, 0);
5856 vmcs_writel(sf->base, 0);
5857 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005858 ar = 0x93;
5859 if (seg == VCPU_SREG_CS)
5860 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005861
5862 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005863}
5864
Sheng Yangf78e0e22007-10-29 09:40:42 +08005865static int alloc_apic_access_page(struct kvm *kvm)
5866{
Xiao Guangrong44841412012-09-07 14:14:20 +08005867 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005868 int r = 0;
5869
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005870 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005871 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005872 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005873 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5874 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005875 if (r)
5876 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005877
Tang Chen73a6d942014-09-11 13:38:00 +08005878 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005879 if (is_error_page(page)) {
5880 r = -EFAULT;
5881 goto out;
5882 }
5883
Tang Chenc24ae0d2014-09-24 15:57:58 +08005884 /*
5885 * Do not pin the page in memory, so that memory hot-unplug
5886 * is able to migrate it.
5887 */
5888 put_page(page);
5889 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005890out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005891 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005892 return r;
5893}
5894
Wanpeng Li991e7a02015-09-16 17:30:05 +08005895static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005896{
5897 int vpid;
5898
Avi Kivity919818a2009-03-23 18:01:29 +02005899 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005900 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005901 spin_lock(&vmx_vpid_lock);
5902 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005903 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005904 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005905 else
5906 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005907 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005908 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005909}
5910
Wanpeng Li991e7a02015-09-16 17:30:05 +08005911static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005912{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005913 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005914 return;
5915 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005916 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005917 spin_unlock(&vmx_vpid_lock);
5918}
5919
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005920static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5921 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005922{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005923 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005924
5925 if (!cpu_has_vmx_msr_bitmap())
5926 return;
5927
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005928 if (static_branch_unlikely(&enable_evmcs))
5929 evmcs_touch_msr_bitmap();
5930
Sheng Yang25c5f222008-03-28 13:18:56 +08005931 /*
5932 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5933 * have the write-low and read-high bitmap offsets the wrong way round.
5934 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5935 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005936 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005937 if (type & MSR_TYPE_R)
5938 /* read-low */
5939 __clear_bit(msr, msr_bitmap + 0x000 / f);
5940
5941 if (type & MSR_TYPE_W)
5942 /* write-low */
5943 __clear_bit(msr, msr_bitmap + 0x800 / f);
5944
Sheng Yang25c5f222008-03-28 13:18:56 +08005945 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5946 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005947 if (type & MSR_TYPE_R)
5948 /* read-high */
5949 __clear_bit(msr, msr_bitmap + 0x400 / f);
5950
5951 if (type & MSR_TYPE_W)
5952 /* write-high */
5953 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5954
5955 }
5956}
5957
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005958static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5959 u32 msr, int type)
5960{
5961 int f = sizeof(unsigned long);
5962
5963 if (!cpu_has_vmx_msr_bitmap())
5964 return;
5965
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005966 if (static_branch_unlikely(&enable_evmcs))
5967 evmcs_touch_msr_bitmap();
5968
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005969 /*
5970 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5971 * have the write-low and read-high bitmap offsets the wrong way round.
5972 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5973 */
5974 if (msr <= 0x1fff) {
5975 if (type & MSR_TYPE_R)
5976 /* read-low */
5977 __set_bit(msr, msr_bitmap + 0x000 / f);
5978
5979 if (type & MSR_TYPE_W)
5980 /* write-low */
5981 __set_bit(msr, msr_bitmap + 0x800 / f);
5982
5983 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5984 msr &= 0x1fff;
5985 if (type & MSR_TYPE_R)
5986 /* read-high */
5987 __set_bit(msr, msr_bitmap + 0x400 / f);
5988
5989 if (type & MSR_TYPE_W)
5990 /* write-high */
5991 __set_bit(msr, msr_bitmap + 0xc00 / f);
5992
5993 }
5994}
5995
5996static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5997 u32 msr, int type, bool value)
5998{
5999 if (value)
6000 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6001 else
6002 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6003}
6004
Wincy Vanf2b93282015-02-03 23:56:03 +08006005/*
6006 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6007 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6008 */
6009static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6010 unsigned long *msr_bitmap_nested,
6011 u32 msr, int type)
6012{
6013 int f = sizeof(unsigned long);
6014
Wincy Vanf2b93282015-02-03 23:56:03 +08006015 /*
6016 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6017 * have the write-low and read-high bitmap offsets the wrong way round.
6018 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6019 */
6020 if (msr <= 0x1fff) {
6021 if (type & MSR_TYPE_R &&
6022 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6023 /* read-low */
6024 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6025
6026 if (type & MSR_TYPE_W &&
6027 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6028 /* write-low */
6029 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6030
6031 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6032 msr &= 0x1fff;
6033 if (type & MSR_TYPE_R &&
6034 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6035 /* read-high */
6036 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6037
6038 if (type & MSR_TYPE_W &&
6039 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6040 /* write-high */
6041 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6042
6043 }
6044}
6045
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006046static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006047{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006048 u8 mode = 0;
6049
6050 if (cpu_has_secondary_exec_ctrls() &&
6051 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6052 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6053 mode |= MSR_BITMAP_MODE_X2APIC;
6054 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6055 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6056 }
6057
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006058 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006059}
6060
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006061#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6062
6063static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6064 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006065{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006066 int msr;
6067
6068 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6069 unsigned word = msr / BITS_PER_LONG;
6070 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6071 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006072 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006073
6074 if (mode & MSR_BITMAP_MODE_X2APIC) {
6075 /*
6076 * TPR reads and writes can be virtualized even if virtual interrupt
6077 * delivery is not in use.
6078 */
6079 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6080 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6081 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6082 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6083 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6084 }
6085 }
6086}
6087
6088static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6089{
6090 struct vcpu_vmx *vmx = to_vmx(vcpu);
6091 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6092 u8 mode = vmx_msr_bitmap_mode(vcpu);
6093 u8 changed = mode ^ vmx->msr_bitmap_mode;
6094
6095 if (!changed)
6096 return;
6097
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006098 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6099 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6100
6101 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006102}
6103
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006104static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006105{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006106 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006107}
6108
David Matlackc9f04402017-08-01 14:00:40 -07006109static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6110{
6111 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6112 gfn_t gfn;
6113
6114 /*
6115 * Don't need to mark the APIC access page dirty; it is never
6116 * written to by the CPU during APIC virtualization.
6117 */
6118
6119 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6120 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6121 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6122 }
6123
6124 if (nested_cpu_has_posted_intr(vmcs12)) {
6125 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6126 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6127 }
6128}
6129
6130
David Hildenbrand6342c502017-01-25 11:58:58 +01006131static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006132{
6133 struct vcpu_vmx *vmx = to_vmx(vcpu);
6134 int max_irr;
6135 void *vapic_page;
6136 u16 status;
6137
David Matlackc9f04402017-08-01 14:00:40 -07006138 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6139 return;
Wincy Van705699a2015-02-03 23:58:17 +08006140
David Matlackc9f04402017-08-01 14:00:40 -07006141 vmx->nested.pi_pending = false;
6142 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6143 return;
Wincy Van705699a2015-02-03 23:58:17 +08006144
David Matlackc9f04402017-08-01 14:00:40 -07006145 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6146 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006147 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006148 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6149 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006150 kunmap(vmx->nested.virtual_apic_page);
6151
6152 status = vmcs_read16(GUEST_INTR_STATUS);
6153 if ((u8)max_irr > ((u8)status & 0xff)) {
6154 status &= ~0xff;
6155 status |= (u8)max_irr;
6156 vmcs_write16(GUEST_INTR_STATUS, status);
6157 }
6158 }
David Matlackc9f04402017-08-01 14:00:40 -07006159
6160 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006161}
6162
Paolo Bonzini7e712682018-10-03 13:44:26 +02006163static u8 vmx_get_rvi(void)
6164{
6165 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6166}
6167
Liran Alone6c67d82018-09-04 10:56:52 +03006168static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6169{
6170 struct vcpu_vmx *vmx = to_vmx(vcpu);
6171 void *vapic_page;
6172 u32 vppr;
6173 int rvi;
6174
6175 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6176 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6177 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6178 return false;
6179
Paolo Bonzini7e712682018-10-03 13:44:26 +02006180 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006181
6182 vapic_page = kmap(vmx->nested.virtual_apic_page);
6183 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6184 kunmap(vmx->nested.virtual_apic_page);
6185
6186 return ((rvi & 0xf0) > (vppr & 0xf0));
6187}
6188
Wincy Van06a55242017-04-28 13:13:59 +08006189static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6190 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006191{
6192#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006193 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6194
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006195 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006196 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006197 * The vector of interrupt to be delivered to vcpu had
6198 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006199 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006200 * Following cases will be reached in this block, and
6201 * we always send a notification event in all cases as
6202 * explained below.
6203 *
6204 * Case 1: vcpu keeps in non-root mode. Sending a
6205 * notification event posts the interrupt to vcpu.
6206 *
6207 * Case 2: vcpu exits to root mode and is still
6208 * runnable. PIR will be synced to vIRR before the
6209 * next vcpu entry. Sending a notification event in
6210 * this case has no effect, as vcpu is not in root
6211 * mode.
6212 *
6213 * Case 3: vcpu exits to root mode and is blocked.
6214 * vcpu_block() has already synced PIR to vIRR and
6215 * never blocks vcpu if vIRR is not cleared. Therefore,
6216 * a blocked vcpu here does not wait for any requested
6217 * interrupts in PIR, and sending a notification event
6218 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006219 */
Feng Wu28b835d2015-09-18 22:29:54 +08006220
Wincy Van06a55242017-04-28 13:13:59 +08006221 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006222 return true;
6223 }
6224#endif
6225 return false;
6226}
6227
Wincy Van705699a2015-02-03 23:58:17 +08006228static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6229 int vector)
6230{
6231 struct vcpu_vmx *vmx = to_vmx(vcpu);
6232
6233 if (is_guest_mode(vcpu) &&
6234 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006235 /*
6236 * If a posted intr is not recognized by hardware,
6237 * we will accomplish it in the next vmentry.
6238 */
6239 vmx->nested.pi_pending = true;
6240 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006241 /* the PIR and ON have been set by L1. */
6242 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6243 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006244 return 0;
6245 }
6246 return -1;
6247}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006248/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006249 * Send interrupt to vcpu via posted interrupt way.
6250 * 1. If target vcpu is running(non-root mode), send posted interrupt
6251 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6252 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6253 * interrupt from PIR in next vmentry.
6254 */
6255static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6256{
6257 struct vcpu_vmx *vmx = to_vmx(vcpu);
6258 int r;
6259
Wincy Van705699a2015-02-03 23:58:17 +08006260 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6261 if (!r)
6262 return;
6263
Yang Zhanga20ed542013-04-11 19:25:15 +08006264 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6265 return;
6266
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006267 /* If a previous notification has sent the IPI, nothing to do. */
6268 if (pi_test_and_set_on(&vmx->pi_desc))
6269 return;
6270
Wincy Van06a55242017-04-28 13:13:59 +08006271 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006272 kvm_vcpu_kick(vcpu);
6273}
6274
Avi Kivity6aa8b732006-12-10 02:21:36 -08006275/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006276 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6277 * will not change in the lifetime of the guest.
6278 * Note that host-state that does change is set elsewhere. E.g., host-state
6279 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6280 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006281static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006282{
6283 u32 low32, high32;
6284 unsigned long tmpl;
6285 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006286 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006287
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006288 cr0 = read_cr0();
6289 WARN_ON(cr0 & X86_CR0_TS);
6290 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006291
6292 /*
6293 * Save the most likely value for this task's CR3 in the VMCS.
6294 * We can't use __get_current_cr3_fast() because we're not atomic.
6295 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006296 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006297 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006298 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006299
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006300 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006301 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006302 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006303 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006304
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006305 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006306#ifdef CONFIG_X86_64
6307 /*
6308 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006309 * vmx_prepare_switch_to_host(), in case userspace uses
6310 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006311 */
6312 vmcs_write16(HOST_DS_SELECTOR, 0);
6313 vmcs_write16(HOST_ES_SELECTOR, 0);
6314#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006315 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6316 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006317#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006318 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6319 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6320
Juergen Gross87930012017-09-04 12:25:27 +02006321 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006322 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006323 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006324
Avi Kivity83287ea422012-09-16 15:10:57 +03006325 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006326
6327 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6328 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6329 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6330 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6331
6332 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6333 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6334 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6335 }
6336}
6337
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006338static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6339{
6340 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6341 if (enable_ept)
6342 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006343 if (is_guest_mode(&vmx->vcpu))
6344 vmx->vcpu.arch.cr4_guest_owned_bits &=
6345 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006346 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6347}
6348
Yang Zhang01e439b2013-04-11 19:25:12 +08006349static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6350{
6351 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6352
Andrey Smetanind62caab2015-11-10 15:36:33 +03006353 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006354 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006355
6356 if (!enable_vnmi)
6357 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6358
Yunhong Jiang64672c92016-06-13 14:19:59 -07006359 /* Enable the preemption timer dynamically */
6360 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006361 return pin_based_exec_ctrl;
6362}
6363
Andrey Smetanind62caab2015-11-10 15:36:33 +03006364static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6365{
6366 struct vcpu_vmx *vmx = to_vmx(vcpu);
6367
6368 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006369 if (cpu_has_secondary_exec_ctrls()) {
6370 if (kvm_vcpu_apicv_active(vcpu))
6371 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6372 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6373 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6374 else
6375 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6376 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6377 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6378 }
6379
6380 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006381 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006382}
6383
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006384static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6385{
6386 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006387
6388 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6389 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6390
Paolo Bonzini35754c92015-07-29 12:05:37 +02006391 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006392 exec_control &= ~CPU_BASED_TPR_SHADOW;
6393#ifdef CONFIG_X86_64
6394 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6395 CPU_BASED_CR8_LOAD_EXITING;
6396#endif
6397 }
6398 if (!enable_ept)
6399 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6400 CPU_BASED_CR3_LOAD_EXITING |
6401 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006402 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6403 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6404 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006405 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6406 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006407 return exec_control;
6408}
6409
Jim Mattson45ec3682017-08-23 16:32:04 -07006410static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006411{
Jim Mattson45ec3682017-08-23 16:32:04 -07006412 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006413 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006414}
6415
Jim Mattson75f4fc82017-08-23 16:32:03 -07006416static bool vmx_rdseed_supported(void)
6417{
6418 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006419 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006420}
6421
Paolo Bonzini80154d72017-08-24 13:55:35 +02006422static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006424 struct kvm_vcpu *vcpu = &vmx->vcpu;
6425
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006426 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006427
Paolo Bonzini80154d72017-08-24 13:55:35 +02006428 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006429 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6430 if (vmx->vpid == 0)
6431 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6432 if (!enable_ept) {
6433 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6434 enable_unrestricted_guest = 0;
6435 }
6436 if (!enable_unrestricted_guest)
6437 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006438 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006439 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006440 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006441 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6442 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006443 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006444
6445 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6446 * in vmx_set_cr4. */
6447 exec_control &= ~SECONDARY_EXEC_DESC;
6448
Abel Gordonabc4fc52013-04-18 14:35:25 +03006449 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6450 (handle_vmptrld).
6451 We can NOT enable shadow_vmcs here because we don't have yet
6452 a current VMCS12
6453 */
6454 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006455
6456 if (!enable_pml)
6457 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006458
Paolo Bonzini3db13482017-08-24 14:48:03 +02006459 if (vmx_xsaves_supported()) {
6460 /* Exposing XSAVES only when XSAVE is exposed */
6461 bool xsaves_enabled =
6462 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6463 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6464
6465 if (!xsaves_enabled)
6466 exec_control &= ~SECONDARY_EXEC_XSAVES;
6467
6468 if (nested) {
6469 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006470 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006471 SECONDARY_EXEC_XSAVES;
6472 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006473 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006474 ~SECONDARY_EXEC_XSAVES;
6475 }
6476 }
6477
Paolo Bonzini80154d72017-08-24 13:55:35 +02006478 if (vmx_rdtscp_supported()) {
6479 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6480 if (!rdtscp_enabled)
6481 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6482
6483 if (nested) {
6484 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006485 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006486 SECONDARY_EXEC_RDTSCP;
6487 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006488 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006489 ~SECONDARY_EXEC_RDTSCP;
6490 }
6491 }
6492
6493 if (vmx_invpcid_supported()) {
6494 /* Exposing INVPCID only when PCID is exposed */
6495 bool invpcid_enabled =
6496 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6497 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6498
6499 if (!invpcid_enabled) {
6500 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6501 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6502 }
6503
6504 if (nested) {
6505 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006506 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006507 SECONDARY_EXEC_ENABLE_INVPCID;
6508 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006509 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006510 ~SECONDARY_EXEC_ENABLE_INVPCID;
6511 }
6512 }
6513
Jim Mattson45ec3682017-08-23 16:32:04 -07006514 if (vmx_rdrand_supported()) {
6515 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6516 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006517 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006518
6519 if (nested) {
6520 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006521 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006522 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006523 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006524 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006525 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006526 }
6527 }
6528
Jim Mattson75f4fc82017-08-23 16:32:03 -07006529 if (vmx_rdseed_supported()) {
6530 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6531 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006532 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006533
6534 if (nested) {
6535 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006536 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006537 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006538 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006539 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006540 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006541 }
6542 }
6543
Paolo Bonzini80154d72017-08-24 13:55:35 +02006544 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006545}
6546
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006547static void ept_set_mmio_spte_mask(void)
6548{
6549 /*
6550 * EPT Misconfigurations can be generated if the value of bits 2:0
6551 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006552 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006553 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6554 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006555}
6556
Wanpeng Lif53cd632014-12-02 19:14:58 +08006557#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006558/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006559 * Sets up the vmcs for emulated real mode.
6560 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006561static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006562{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006563 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006564
Abel Gordon4607c2d2013-04-18 14:35:55 +03006565 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006566 /*
6567 * At vCPU creation, "VMWRITE to any supported field
6568 * in the VMCS" is supported, so use the more
6569 * permissive vmx_vmread_bitmap to specify both read
6570 * and write permissions for the shadow VMCS.
6571 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006572 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006573 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006574 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006575 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006576 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006577
Avi Kivity6aa8b732006-12-10 02:21:36 -08006578 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6579
Avi Kivity6aa8b732006-12-10 02:21:36 -08006580 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006581 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006582 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006583
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006584 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006585
Dan Williamsdfa169b2016-06-02 11:17:24 -07006586 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006587 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006588 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006589 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006590 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006591
Andrey Smetanind62caab2015-11-10 15:36:33 +03006592 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006593 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6594 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6595 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6596 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6597
6598 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006599
Li RongQing0bcf2612015-12-03 13:29:34 +08006600 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006601 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006602 }
6603
Wanpeng Lib31c1142018-03-12 04:53:04 -07006604 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006605 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006606 vmx->ple_window = ple_window;
6607 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006608 }
6609
Xiao Guangrongc3707952011-07-12 03:28:04 +08006610 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6611 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006612 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6613
Avi Kivity9581d442010-10-19 16:46:55 +02006614 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6615 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006616 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6618 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006619
Bandan Das2a499e42017-08-03 15:54:41 -04006620 if (cpu_has_vmx_vmfunc())
6621 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6622
Eddie Dong2cc51562007-05-21 07:28:09 +03006623 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6624 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006625 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006626 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006627 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006628
Radim Krčmář74545702015-04-27 15:11:25 +02006629 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6630 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006631
Paolo Bonzini03916db2014-07-24 14:21:57 +02006632 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006633 u32 index = vmx_msr_index[i];
6634 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006635 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006636
6637 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6638 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006639 if (wrmsr_safe(index, data_low, data_high) < 0)
6640 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006641 vmx->guest_msrs[j].index = i;
6642 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006643 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006644 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006645 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006646
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006647 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006648
6649 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006650
6651 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006652 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006653
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006654 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6655 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6656
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006657 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006658
Wanpeng Lif53cd632014-12-02 19:14:58 +08006659 if (vmx_xsaves_supported())
6660 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6661
Peter Feiner4e595162016-07-07 14:49:58 -07006662 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006663 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6664 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6665 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006666
6667 if (cpu_has_vmx_encls_vmexit())
6668 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006669}
6670
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006671static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006672{
6673 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006674 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006675 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006676
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006677 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006678 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006679
Wanpeng Li518e7b92018-02-28 14:03:31 +08006680 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006681 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006682 kvm_set_cr8(vcpu, 0);
6683
6684 if (!init_event) {
6685 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6686 MSR_IA32_APICBASE_ENABLE;
6687 if (kvm_vcpu_is_reset_bsp(vcpu))
6688 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6689 apic_base_msr.host_initiated = true;
6690 kvm_set_apic_base(vcpu, &apic_base_msr);
6691 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006692
Avi Kivity2fb92db2011-04-27 19:42:18 +03006693 vmx_segment_cache_clear(vmx);
6694
Avi Kivity5706be02008-08-20 15:07:31 +03006695 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006696 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006697 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006698
6699 seg_setup(VCPU_SREG_DS);
6700 seg_setup(VCPU_SREG_ES);
6701 seg_setup(VCPU_SREG_FS);
6702 seg_setup(VCPU_SREG_GS);
6703 seg_setup(VCPU_SREG_SS);
6704
6705 vmcs_write16(GUEST_TR_SELECTOR, 0);
6706 vmcs_writel(GUEST_TR_BASE, 0);
6707 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6708 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6709
6710 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6711 vmcs_writel(GUEST_LDTR_BASE, 0);
6712 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6713 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6714
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006715 if (!init_event) {
6716 vmcs_write32(GUEST_SYSENTER_CS, 0);
6717 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6718 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6719 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6720 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006721
Wanpeng Lic37c2872017-11-20 14:52:21 -08006722 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006723 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006724
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006725 vmcs_writel(GUEST_GDTR_BASE, 0);
6726 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6727
6728 vmcs_writel(GUEST_IDTR_BASE, 0);
6729 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6730
Anthony Liguori443381a2010-12-06 10:53:38 -06006731 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006732 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006733 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006734 if (kvm_mpx_supported())
6735 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006736
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006737 setup_msrs(vmx);
6738
Avi Kivity6aa8b732006-12-10 02:21:36 -08006739 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6740
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006741 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006742 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006743 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006744 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006745 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006746 vmcs_write32(TPR_THRESHOLD, 0);
6747 }
6748
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006749 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006750
Sheng Yang2384d2b2008-01-17 15:14:33 +08006751 if (vmx->vpid != 0)
6752 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6753
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006754 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006755 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006756 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006757 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006758 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006759
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006760 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006761
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006762 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006763 if (init_event)
6764 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006765}
6766
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006767/*
6768 * In nested virtualization, check if L1 asked to exit on external interrupts.
6769 * For most existing hypervisors, this will always return true.
6770 */
6771static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6772{
6773 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6774 PIN_BASED_EXT_INTR_MASK;
6775}
6776
Bandan Das77b0f5d2014-04-19 18:17:45 -04006777/*
6778 * In nested virtualization, check if L1 has set
6779 * VM_EXIT_ACK_INTR_ON_EXIT
6780 */
6781static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6782{
6783 return get_vmcs12(vcpu)->vm_exit_controls &
6784 VM_EXIT_ACK_INTR_ON_EXIT;
6785}
6786
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006787static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6788{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006789 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006790}
6791
Jan Kiszkac9a79532014-03-07 20:03:15 +01006792static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006793{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006794 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6795 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006796}
6797
Jan Kiszkac9a79532014-03-07 20:03:15 +01006798static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006799{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006800 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006801 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006802 enable_irq_window(vcpu);
6803 return;
6804 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006805
Paolo Bonzini47c01522016-12-19 11:44:07 +01006806 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6807 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006808}
6809
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006810static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006811{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006812 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006813 uint32_t intr;
6814 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006815
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006816 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006817
Avi Kivityfa89a812008-09-01 15:57:51 +03006818 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006819 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006820 int inc_eip = 0;
6821 if (vcpu->arch.interrupt.soft)
6822 inc_eip = vcpu->arch.event_exit_inst_len;
6823 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006824 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006825 return;
6826 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006827 intr = irq | INTR_INFO_VALID_MASK;
6828 if (vcpu->arch.interrupt.soft) {
6829 intr |= INTR_TYPE_SOFT_INTR;
6830 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6831 vmx->vcpu.arch.event_exit_inst_len);
6832 } else
6833 intr |= INTR_TYPE_EXT_INTR;
6834 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006835
6836 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006837}
6838
Sheng Yangf08864b2008-05-15 18:23:25 +08006839static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6840{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006841 struct vcpu_vmx *vmx = to_vmx(vcpu);
6842
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006843 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006844 /*
6845 * Tracking the NMI-blocked state in software is built upon
6846 * finding the next open IRQ window. This, in turn, depends on
6847 * well-behaving guests: They have to keep IRQs disabled at
6848 * least as long as the NMI handler runs. Otherwise we may
6849 * cause NMI nesting, maybe breaking the guest. But as this is
6850 * highly unlikely, we can live with the residual risk.
6851 */
6852 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6853 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6854 }
6855
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006856 ++vcpu->stat.nmi_injections;
6857 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006858
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006859 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006860 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006861 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006862 return;
6863 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006864
Sheng Yangf08864b2008-05-15 18:23:25 +08006865 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6866 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006867
6868 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006869}
6870
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006871static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6872{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006873 struct vcpu_vmx *vmx = to_vmx(vcpu);
6874 bool masked;
6875
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006876 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006877 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006878 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006879 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006880 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6881 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6882 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006883}
6884
6885static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6886{
6887 struct vcpu_vmx *vmx = to_vmx(vcpu);
6888
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006889 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006890 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6891 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6892 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6893 }
6894 } else {
6895 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6896 if (masked)
6897 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6898 GUEST_INTR_STATE_NMI);
6899 else
6900 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6901 GUEST_INTR_STATE_NMI);
6902 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006903}
6904
Jan Kiszka2505dc92013-04-14 12:12:47 +02006905static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6906{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006907 if (to_vmx(vcpu)->nested.nested_run_pending)
6908 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006909
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006910 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006911 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6912 return 0;
6913
Jan Kiszka2505dc92013-04-14 12:12:47 +02006914 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6915 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6916 | GUEST_INTR_STATE_NMI));
6917}
6918
Gleb Natapov78646122009-03-23 12:12:11 +02006919static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6920{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006921 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6922 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006923 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6924 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006925}
6926
Izik Eiduscbc94022007-10-25 00:29:55 +02006927static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6928{
6929 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006930
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006931 if (enable_unrestricted_guest)
6932 return 0;
6933
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006934 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6935 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006936 if (ret)
6937 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006938 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006939 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006940}
6941
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006942static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6943{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006944 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006945 return 0;
6946}
6947
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006948static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006949{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006950 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006951 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006952 /*
6953 * Update instruction length as we may reinject the exception
6954 * from user space while in guest debugging mode.
6955 */
6956 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6957 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006958 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006959 return false;
6960 /* fall through */
6961 case DB_VECTOR:
6962 if (vcpu->guest_debug &
6963 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6964 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006965 /* fall through */
6966 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006967 case OF_VECTOR:
6968 case BR_VECTOR:
6969 case UD_VECTOR:
6970 case DF_VECTOR:
6971 case SS_VECTOR:
6972 case GP_VECTOR:
6973 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006974 return true;
6975 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006976 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006977 return false;
6978}
6979
6980static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6981 int vec, u32 err_code)
6982{
6983 /*
6984 * Instruction with address size override prefix opcode 0x67
6985 * Cause the #SS fault with 0 error code in VM86 mode.
6986 */
6987 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006988 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006989 if (vcpu->arch.halt_request) {
6990 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006991 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006992 }
6993 return 1;
6994 }
6995 return 0;
6996 }
6997
6998 /*
6999 * Forward all other exceptions that are valid in real mode.
7000 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7001 * the required debugging infrastructure rework.
7002 */
7003 kvm_queue_exception(vcpu, vec);
7004 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007005}
7006
Andi Kleena0861c02009-06-08 17:37:09 +08007007/*
7008 * Trigger machine check on the host. We assume all the MSRs are already set up
7009 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7010 * We pass a fake environment to the machine check handler because we want
7011 * the guest to be always treated like user space, no matter what context
7012 * it used internally.
7013 */
7014static void kvm_machine_check(void)
7015{
7016#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7017 struct pt_regs regs = {
7018 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7019 .flags = X86_EFLAGS_IF,
7020 };
7021
7022 do_machine_check(&regs, 0);
7023#endif
7024}
7025
Avi Kivity851ba692009-08-24 11:10:17 +03007026static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007027{
7028 /* already handled by vcpu_run */
7029 return 1;
7030}
7031
Avi Kivity851ba692009-08-24 11:10:17 +03007032static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007033{
Avi Kivity1155f762007-11-22 11:30:47 +02007034 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007035 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007036 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007037 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007038 u32 vect_info;
7039 enum emulation_result er;
7040
Avi Kivity1155f762007-11-22 11:30:47 +02007041 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007042 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007043
Andi Kleena0861c02009-06-08 17:37:09 +08007044 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007045 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007046
Jim Mattsonef85b672016-12-12 11:01:37 -08007047 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007048 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007049
Wanpeng Li082d06e2018-04-03 16:28:48 -07007050 if (is_invalid_opcode(intr_info))
7051 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007052
Avi Kivity6aa8b732006-12-10 02:21:36 -08007053 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007054 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007055 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007056
Liran Alon9e869482018-03-12 13:12:51 +02007057 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7058 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007059 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007060 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7061 if (er == EMULATE_USER_EXIT)
7062 return 0;
7063 else if (er != EMULATE_DONE)
7064 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7065 return 1;
7066 }
7067
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007068 /*
7069 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7070 * MMIO, it is better to report an internal error.
7071 * See the comments in vmx_handle_exit.
7072 */
7073 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7074 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7075 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7076 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007077 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007078 vcpu->run->internal.data[0] = vect_info;
7079 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007080 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007081 return 0;
7082 }
7083
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084 if (is_page_fault(intr_info)) {
7085 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007086 /* EPT won't cause page fault directly */
7087 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007088 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007089 }
7090
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007091 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007092
7093 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7094 return handle_rmode_exception(vcpu, ex_no, error_code);
7095
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007096 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007097 case AC_VECTOR:
7098 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7099 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007100 case DB_VECTOR:
7101 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7102 if (!(vcpu->guest_debug &
7103 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007104 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007105 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007106 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007107 skip_emulated_instruction(vcpu);
7108
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007109 kvm_queue_exception(vcpu, DB_VECTOR);
7110 return 1;
7111 }
7112 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7113 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7114 /* fall through */
7115 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007116 /*
7117 * Update instruction length as we may reinject #BP from
7118 * user space while in guest debugging mode. Reading it for
7119 * #DB as well causes no harm, it is not used in that case.
7120 */
7121 vmx->vcpu.arch.event_exit_inst_len =
7122 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007123 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007124 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007125 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7126 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007127 break;
7128 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007129 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7130 kvm_run->ex.exception = ex_no;
7131 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007133 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007134 return 0;
7135}
7136
Avi Kivity851ba692009-08-24 11:10:17 +03007137static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007138{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007139 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007140 return 1;
7141}
7142
Avi Kivity851ba692009-08-24 11:10:17 +03007143static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007144{
Avi Kivity851ba692009-08-24 11:10:17 +03007145 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007146 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007147 return 0;
7148}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007149
Avi Kivity851ba692009-08-24 11:10:17 +03007150static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007151{
He, Qingbfdaab02007-09-12 14:18:28 +08007152 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007153 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007154 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007155
He, Qingbfdaab02007-09-12 14:18:28 +08007156 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007157 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007158
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007159 ++vcpu->stat.io_exits;
7160
Sean Christopherson432baf62018-03-08 08:57:26 -08007161 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007162 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007163
7164 port = exit_qualification >> 16;
7165 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007166 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007167
Sean Christophersondca7f122018-03-08 08:57:27 -08007168 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007169}
7170
Ingo Molnar102d8322007-02-19 14:37:47 +02007171static void
7172vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7173{
7174 /*
7175 * Patch in the VMCALL instruction:
7176 */
7177 hypercall[0] = 0x0f;
7178 hypercall[1] = 0x01;
7179 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007180}
7181
Guo Chao0fa06072012-06-28 15:16:19 +08007182/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007183static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7184{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007185 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007186 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7187 unsigned long orig_val = val;
7188
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007189 /*
7190 * We get here when L2 changed cr0 in a way that did not change
7191 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007192 * but did change L0 shadowed bits. So we first calculate the
7193 * effective cr0 value that L1 would like to write into the
7194 * hardware. It consists of the L2-owned bits from the new
7195 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007196 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007197 val = (val & ~vmcs12->cr0_guest_host_mask) |
7198 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7199
David Matlack38991522016-11-29 18:14:08 -08007200 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007201 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007202
7203 if (kvm_set_cr0(vcpu, val))
7204 return 1;
7205 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007206 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007207 } else {
7208 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007209 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007210 return 1;
David Matlack38991522016-11-29 18:14:08 -08007211
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007212 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007213 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007214}
7215
7216static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7217{
7218 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007219 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7220 unsigned long orig_val = val;
7221
7222 /* analogously to handle_set_cr0 */
7223 val = (val & ~vmcs12->cr4_guest_host_mask) |
7224 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7225 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007226 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007227 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007228 return 0;
7229 } else
7230 return kvm_set_cr4(vcpu, val);
7231}
7232
Paolo Bonzini0367f202016-07-12 10:44:55 +02007233static int handle_desc(struct kvm_vcpu *vcpu)
7234{
7235 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007236 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007237}
7238
Avi Kivity851ba692009-08-24 11:10:17 +03007239static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007241 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007242 int cr;
7243 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007244 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007245 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246
He, Qingbfdaab02007-09-12 14:18:28 +08007247 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007248 cr = exit_qualification & 15;
7249 reg = (exit_qualification >> 8) & 15;
7250 switch ((exit_qualification >> 4) & 3) {
7251 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007252 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007253 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007254 switch (cr) {
7255 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007256 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007258 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007259 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007260 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007261 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007262 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007263 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007264 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007265 case 8: {
7266 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007267 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007268 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007269 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007270 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007271 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007272 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007273 return ret;
7274 /*
7275 * TODO: we might be squashing a
7276 * KVM_GUESTDBG_SINGLESTEP-triggered
7277 * KVM_EXIT_DEBUG here.
7278 */
Avi Kivity851ba692009-08-24 11:10:17 +03007279 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007280 return 0;
7281 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007282 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007283 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007284 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007285 WARN_ONCE(1, "Guest should always own CR0.TS");
7286 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007287 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007288 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 case 1: /*mov from cr*/
7290 switch (cr) {
7291 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007292 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007293 val = kvm_read_cr3(vcpu);
7294 kvm_register_write(vcpu, reg, val);
7295 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007296 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007297 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007298 val = kvm_get_cr8(vcpu);
7299 kvm_register_write(vcpu, reg, val);
7300 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007301 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007302 }
7303 break;
7304 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007305 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007306 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007307 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308
Kyle Huey6affcbe2016-11-29 12:40:40 -08007309 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007310 default:
7311 break;
7312 }
Avi Kivity851ba692009-08-24 11:10:17 +03007313 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007314 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007315 (int)(exit_qualification >> 4) & 3, cr);
7316 return 0;
7317}
7318
Avi Kivity851ba692009-08-24 11:10:17 +03007319static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007320{
He, Qingbfdaab02007-09-12 14:18:28 +08007321 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007322 int dr, dr7, reg;
7323
7324 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7325 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7326
7327 /* First, if DR does not exist, trigger UD */
7328 if (!kvm_require_dr(vcpu, dr))
7329 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007330
Jan Kiszkaf2483412010-01-20 18:20:20 +01007331 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007332 if (!kvm_require_cpl(vcpu, 0))
7333 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007334 dr7 = vmcs_readl(GUEST_DR7);
7335 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007336 /*
7337 * As the vm-exit takes precedence over the debug trap, we
7338 * need to emulate the latter, either for the host or the
7339 * guest debugging itself.
7340 */
7341 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007342 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007343 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007344 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007345 vcpu->run->debug.arch.exception = DB_VECTOR;
7346 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007347 return 0;
7348 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007349 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007350 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007351 kvm_queue_exception(vcpu, DB_VECTOR);
7352 return 1;
7353 }
7354 }
7355
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007356 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007357 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7358 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007359
7360 /*
7361 * No more DR vmexits; force a reload of the debug registers
7362 * and reenter on this instruction. The next vmexit will
7363 * retrieve the full state of the debug registers.
7364 */
7365 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7366 return 1;
7367 }
7368
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007369 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7370 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007371 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007372
7373 if (kvm_get_dr(vcpu, dr, &val))
7374 return 1;
7375 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007376 } else
Nadav Amit57773922014-06-18 17:19:23 +03007377 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007378 return 1;
7379
Kyle Huey6affcbe2016-11-29 12:40:40 -08007380 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007381}
7382
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007383static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7384{
7385 return vcpu->arch.dr6;
7386}
7387
7388static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7389{
7390}
7391
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007392static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7393{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007394 get_debugreg(vcpu->arch.db[0], 0);
7395 get_debugreg(vcpu->arch.db[1], 1);
7396 get_debugreg(vcpu->arch.db[2], 2);
7397 get_debugreg(vcpu->arch.db[3], 3);
7398 get_debugreg(vcpu->arch.dr6, 6);
7399 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7400
7401 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007402 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007403}
7404
Gleb Natapov020df072010-04-13 10:05:23 +03007405static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7406{
7407 vmcs_writel(GUEST_DR7, val);
7408}
7409
Avi Kivity851ba692009-08-24 11:10:17 +03007410static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007411{
Kyle Huey6a908b62016-11-29 12:40:37 -08007412 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007413}
7414
Avi Kivity851ba692009-08-24 11:10:17 +03007415static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007416{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007417 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007418 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007419
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007420 msr_info.index = ecx;
7421 msr_info.host_initiated = false;
7422 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007423 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007424 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007425 return 1;
7426 }
7427
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007428 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007429
Avi Kivity6aa8b732006-12-10 02:21:36 -08007430 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007431 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7432 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007433 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007434}
7435
Avi Kivity851ba692009-08-24 11:10:17 +03007436static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007437{
Will Auld8fe8ab42012-11-29 12:42:12 -08007438 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007439 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7440 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7441 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007442
Will Auld8fe8ab42012-11-29 12:42:12 -08007443 msr.data = data;
7444 msr.index = ecx;
7445 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007446 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007447 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007448 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007449 return 1;
7450 }
7451
Avi Kivity59200272010-01-25 19:47:02 +02007452 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007453 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007454}
7455
Avi Kivity851ba692009-08-24 11:10:17 +03007456static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007457{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007458 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007459 return 1;
7460}
7461
Avi Kivity851ba692009-08-24 11:10:17 +03007462static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007463{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007464 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7465 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007466
Avi Kivity3842d132010-07-27 12:30:24 +03007467 kvm_make_request(KVM_REQ_EVENT, vcpu);
7468
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007469 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007470 return 1;
7471}
7472
Avi Kivity851ba692009-08-24 11:10:17 +03007473static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007474{
Avi Kivityd3bef152007-06-05 15:53:05 +03007475 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007476}
7477
Avi Kivity851ba692009-08-24 11:10:17 +03007478static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007479{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007480 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007481}
7482
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007483static int handle_invd(struct kvm_vcpu *vcpu)
7484{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007485 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007486}
7487
Avi Kivity851ba692009-08-24 11:10:17 +03007488static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007489{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007490 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007491
7492 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007493 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007494}
7495
Avi Kivityfee84b02011-11-10 14:57:25 +02007496static int handle_rdpmc(struct kvm_vcpu *vcpu)
7497{
7498 int err;
7499
7500 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007501 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007502}
7503
Avi Kivity851ba692009-08-24 11:10:17 +03007504static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007505{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007506 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007507}
7508
Dexuan Cui2acf9232010-06-10 11:27:12 +08007509static int handle_xsetbv(struct kvm_vcpu *vcpu)
7510{
7511 u64 new_bv = kvm_read_edx_eax(vcpu);
7512 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7513
7514 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007515 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007516 return 1;
7517}
7518
Wanpeng Lif53cd632014-12-02 19:14:58 +08007519static int handle_xsaves(struct kvm_vcpu *vcpu)
7520{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007521 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007522 WARN(1, "this should never happen\n");
7523 return 1;
7524}
7525
7526static int handle_xrstors(struct kvm_vcpu *vcpu)
7527{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007528 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007529 WARN(1, "this should never happen\n");
7530 return 1;
7531}
7532
Avi Kivity851ba692009-08-24 11:10:17 +03007533static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007534{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007535 if (likely(fasteoi)) {
7536 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7537 int access_type, offset;
7538
7539 access_type = exit_qualification & APIC_ACCESS_TYPE;
7540 offset = exit_qualification & APIC_ACCESS_OFFSET;
7541 /*
7542 * Sane guest uses MOV to write EOI, with written value
7543 * not cared. So make a short-circuit here by avoiding
7544 * heavy instruction emulation.
7545 */
7546 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7547 (offset == APIC_EOI)) {
7548 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007549 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007550 }
7551 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007552 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007553}
7554
Yang Zhangc7c9c562013-01-25 10:18:51 +08007555static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7556{
7557 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7558 int vector = exit_qualification & 0xff;
7559
7560 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7561 kvm_apic_set_eoi_accelerated(vcpu, vector);
7562 return 1;
7563}
7564
Yang Zhang83d4c282013-01-25 10:18:49 +08007565static int handle_apic_write(struct kvm_vcpu *vcpu)
7566{
7567 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7568 u32 offset = exit_qualification & 0xfff;
7569
7570 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7571 kvm_apic_write_nodecode(vcpu, offset);
7572 return 1;
7573}
7574
Avi Kivity851ba692009-08-24 11:10:17 +03007575static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007576{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007577 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007578 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007579 bool has_error_code = false;
7580 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007581 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007582 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007583
7584 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007585 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007586 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007587
7588 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7589
7590 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007591 if (reason == TASK_SWITCH_GATE && idt_v) {
7592 switch (type) {
7593 case INTR_TYPE_NMI_INTR:
7594 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007595 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007596 break;
7597 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007598 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007599 kvm_clear_interrupt_queue(vcpu);
7600 break;
7601 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007602 if (vmx->idt_vectoring_info &
7603 VECTORING_INFO_DELIVER_CODE_MASK) {
7604 has_error_code = true;
7605 error_code =
7606 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7607 }
7608 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007609 case INTR_TYPE_SOFT_EXCEPTION:
7610 kvm_clear_exception_queue(vcpu);
7611 break;
7612 default:
7613 break;
7614 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007615 }
Izik Eidus37817f22008-03-24 23:14:53 +02007616 tss_selector = exit_qualification;
7617
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007618 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7619 type != INTR_TYPE_EXT_INTR &&
7620 type != INTR_TYPE_NMI_INTR))
7621 skip_emulated_instruction(vcpu);
7622
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007623 if (kvm_task_switch(vcpu, tss_selector,
7624 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7625 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007626 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7627 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7628 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007629 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007630 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007631
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007632 /*
7633 * TODO: What about debug traps on tss switch?
7634 * Are we supposed to inject them and update dr6?
7635 */
7636
7637 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007638}
7639
Avi Kivity851ba692009-08-24 11:10:17 +03007640static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007641{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007642 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007643 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007644 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007645
Sheng Yangf9c617f2009-03-25 10:08:52 +08007646 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007647
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007648 /*
7649 * EPT violation happened while executing iret from NMI,
7650 * "blocked by NMI" bit has to be set before next VM entry.
7651 * There are errata that may cause this bit to not be set:
7652 * AAK134, BY25.
7653 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007654 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007655 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007656 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007657 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7658
Sheng Yang14394422008-04-28 12:24:45 +08007659 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007660 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007661
Junaid Shahid27959a42016-12-06 16:46:10 -08007662 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007663 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007664 ? PFERR_USER_MASK : 0;
7665 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007666 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007667 ? PFERR_WRITE_MASK : 0;
7668 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007669 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007670 ? PFERR_FETCH_MASK : 0;
7671 /* ept page table entry is present? */
7672 error_code |= (exit_qualification &
7673 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7674 EPT_VIOLATION_EXECUTABLE))
7675 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007676
Paolo Bonzinieebed242016-11-28 14:39:58 +01007677 error_code |= (exit_qualification & 0x100) != 0 ?
7678 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007679
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007680 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007681 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007682}
7683
Avi Kivity851ba692009-08-24 11:10:17 +03007684static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007685{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007686 gpa_t gpa;
7687
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007688 /*
7689 * A nested guest cannot optimize MMIO vmexits, because we have an
7690 * nGPA here instead of the required GPA.
7691 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007692 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007693 if (!is_guest_mode(vcpu) &&
7694 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007695 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007696 /*
7697 * Doing kvm_skip_emulated_instruction() depends on undefined
7698 * behavior: Intel's manual doesn't mandate
7699 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7700 * occurs and while on real hardware it was observed to be set,
7701 * other hypervisors (namely Hyper-V) don't set it, we end up
7702 * advancing IP with some random value. Disable fast mmio when
7703 * running nested and keep it for real hardware in hope that
7704 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7705 */
7706 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7707 return kvm_skip_emulated_instruction(vcpu);
7708 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007709 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007710 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007711 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007712
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007713 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007714}
7715
Avi Kivity851ba692009-08-24 11:10:17 +03007716static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007717{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007718 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007719 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7720 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007721 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007722 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007723
7724 return 1;
7725}
7726
Mohammed Gamal80ced182009-09-01 12:48:18 +02007727static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007728{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007729 struct vcpu_vmx *vmx = to_vmx(vcpu);
7730 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007731 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007732 u32 cpu_exec_ctrl;
7733 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007734 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007735
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007736 /*
7737 * We should never reach the point where we are emulating L2
7738 * due to invalid guest state as that means we incorrectly
7739 * allowed a nested VMEntry with an invalid vmcs12.
7740 */
7741 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7742
Avi Kivity49e9d552010-09-19 14:34:08 +02007743 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7744 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007745
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007746 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007747 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007748 return handle_interrupt_window(&vmx->vcpu);
7749
Radim Krčmář72875d82017-04-26 22:32:19 +02007750 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007751 return 1;
7752
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007753 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007754
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007755 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007756 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007757 ret = 0;
7758 goto out;
7759 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007760
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007761 if (err != EMULATE_DONE)
7762 goto emulation_error;
7763
7764 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7765 vcpu->arch.exception.pending)
7766 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007767
Gleb Natapov8d76c492013-05-08 18:38:44 +03007768 if (vcpu->arch.halt_request) {
7769 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007770 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007771 goto out;
7772 }
7773
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007774 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007775 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007776 if (need_resched())
7777 schedule();
7778 }
7779
Mohammed Gamal80ced182009-09-01 12:48:18 +02007780out:
7781 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007782
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007783emulation_error:
7784 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7785 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7786 vcpu->run->internal.ndata = 0;
7787 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007788}
7789
7790static void grow_ple_window(struct kvm_vcpu *vcpu)
7791{
7792 struct vcpu_vmx *vmx = to_vmx(vcpu);
7793 int old = vmx->ple_window;
7794
Babu Mogerc8e88712018-03-16 16:37:24 -04007795 vmx->ple_window = __grow_ple_window(old, ple_window,
7796 ple_window_grow,
7797 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007798
7799 if (vmx->ple_window != old)
7800 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007801
7802 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007803}
7804
7805static void shrink_ple_window(struct kvm_vcpu *vcpu)
7806{
7807 struct vcpu_vmx *vmx = to_vmx(vcpu);
7808 int old = vmx->ple_window;
7809
Babu Mogerc8e88712018-03-16 16:37:24 -04007810 vmx->ple_window = __shrink_ple_window(old, ple_window,
7811 ple_window_shrink,
7812 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007813
7814 if (vmx->ple_window != old)
7815 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007816
7817 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007818}
7819
7820/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007821 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7822 */
7823static void wakeup_handler(void)
7824{
7825 struct kvm_vcpu *vcpu;
7826 int cpu = smp_processor_id();
7827
7828 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7829 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7830 blocked_vcpu_list) {
7831 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7832
7833 if (pi_test_on(pi_desc) == 1)
7834 kvm_vcpu_kick(vcpu);
7835 }
7836 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7837}
7838
Peng Haoe01bca22018-04-07 05:47:32 +08007839static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007840{
7841 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7842 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7843 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7844 0ull, VMX_EPT_EXECUTABLE_MASK,
7845 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007846 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007847
7848 ept_set_mmio_spte_mask();
7849 kvm_enable_tdp();
7850}
7851
Tiejun Chenf2c76482014-10-28 10:14:47 +08007852static __init int hardware_setup(void)
7853{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007854 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007855 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007856
7857 rdmsrl_safe(MSR_EFER, &host_efer);
7858
7859 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7860 kvm_define_shared_msr(i, vmx_msr_index[i]);
7861
Radim Krčmář23611332016-09-29 22:41:33 +02007862 for (i = 0; i < VMX_BITMAP_NR; i++) {
7863 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7864 if (!vmx_bitmap[i])
7865 goto out;
7866 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007867
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007868 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7869 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7870
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007871 if (setup_vmcs_config(&vmcs_config) < 0) {
7872 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007873 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007874 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007875
7876 if (boot_cpu_has(X86_FEATURE_NX))
7877 kvm_enable_efer_bits(EFER_NX);
7878
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007879 if (boot_cpu_has(X86_FEATURE_MPX)) {
7880 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7881 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7882 }
7883
Wanpeng Li08d839c2017-03-23 05:30:08 -07007884 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7885 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007886 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007887
Tiejun Chenf2c76482014-10-28 10:14:47 +08007888 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007889 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007890 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007891 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007892 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007893
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007894 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007895 enable_ept_ad_bits = 0;
7896
Wanpeng Li8ad81822017-10-09 15:51:53 -07007897 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007898 enable_unrestricted_guest = 0;
7899
Paolo Bonziniad15a292015-01-30 16:18:49 +01007900 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007901 flexpriority_enabled = 0;
7902
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007903 if (!cpu_has_virtual_nmis())
7904 enable_vnmi = 0;
7905
Paolo Bonziniad15a292015-01-30 16:18:49 +01007906 /*
7907 * set_apic_access_page_addr() is used to reload apic access
7908 * page upon invalidation. No need to do anything if not
7909 * using the APIC_ACCESS_ADDR VMCS field.
7910 */
7911 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007912 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007913
7914 if (!cpu_has_vmx_tpr_shadow())
7915 kvm_x86_ops->update_cr8_intercept = NULL;
7916
7917 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7918 kvm_disable_largepages();
7919
Tianyu Lan877ad952018-07-19 08:40:23 +00007920#if IS_ENABLED(CONFIG_HYPERV)
7921 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7922 && enable_ept)
7923 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7924#endif
7925
Wanpeng Li0f107682017-09-28 18:06:24 -07007926 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007927 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007928 ple_window = 0;
7929 ple_window_grow = 0;
7930 ple_window_max = 0;
7931 ple_window_shrink = 0;
7932 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007933
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007934 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007935 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007936 kvm_x86_ops->sync_pir_to_irr = NULL;
7937 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007938
Haozhong Zhang64903d62015-10-20 15:39:09 +08007939 if (cpu_has_vmx_tsc_scaling()) {
7940 kvm_has_tsc_control = true;
7941 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7942 kvm_tsc_scaling_ratio_frac_bits = 48;
7943 }
7944
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007945 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7946
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007947 if (enable_ept)
7948 vmx_enable_tdp();
7949 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007950 kvm_disable_tdp();
7951
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007952 if (!nested) {
7953 kvm_x86_ops->get_nested_state = NULL;
7954 kvm_x86_ops->set_nested_state = NULL;
7955 }
7956
Kai Huang843e4332015-01-28 10:54:28 +08007957 /*
7958 * Only enable PML when hardware supports PML feature, and both EPT
7959 * and EPT A/D bit features are enabled -- PML depends on them to work.
7960 */
7961 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7962 enable_pml = 0;
7963
7964 if (!enable_pml) {
7965 kvm_x86_ops->slot_enable_log_dirty = NULL;
7966 kvm_x86_ops->slot_disable_log_dirty = NULL;
7967 kvm_x86_ops->flush_log_dirty = NULL;
7968 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7969 }
7970
Sean Christophersond264ee02018-08-27 15:21:12 -07007971 if (!cpu_has_vmx_preemption_timer())
7972 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7973
Yunhong Jiang64672c92016-06-13 14:19:59 -07007974 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7975 u64 vmx_msr;
7976
7977 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7978 cpu_preemption_timer_multi =
7979 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7980 } else {
7981 kvm_x86_ops->set_hv_timer = NULL;
7982 kvm_x86_ops->cancel_hv_timer = NULL;
7983 }
7984
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007985 if (!cpu_has_vmx_shadow_vmcs())
7986 enable_shadow_vmcs = 0;
7987 if (enable_shadow_vmcs)
7988 init_vmcs_shadow_fields();
7989
Feng Wubf9f6ac2015-09-18 22:29:55 +08007990 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007991 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007992
Ashok Rajc45dcc72016-06-22 14:59:56 +08007993 kvm_mce_cap_supported |= MCG_LMCE_P;
7994
Tiejun Chenf2c76482014-10-28 10:14:47 +08007995 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007996
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007997out:
Radim Krčmář23611332016-09-29 22:41:33 +02007998 for (i = 0; i < VMX_BITMAP_NR; i++)
7999 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008000
8001 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008002}
8003
8004static __exit void hardware_unsetup(void)
8005{
Radim Krčmář23611332016-09-29 22:41:33 +02008006 int i;
8007
8008 for (i = 0; i < VMX_BITMAP_NR; i++)
8009 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008010
Tiejun Chenf2c76482014-10-28 10:14:47 +08008011 free_kvm_area();
8012}
8013
Avi Kivity6aa8b732006-12-10 02:21:36 -08008014/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008015 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8016 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8017 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008018static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008019{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008020 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008021 grow_ple_window(vcpu);
8022
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008023 /*
8024 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8025 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8026 * never set PAUSE_EXITING and just set PLE if supported,
8027 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8028 */
8029 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008030 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008031}
8032
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008033static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008034{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008035 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008036}
8037
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008038static int handle_mwait(struct kvm_vcpu *vcpu)
8039{
8040 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8041 return handle_nop(vcpu);
8042}
8043
Jim Mattson45ec3682017-08-23 16:32:04 -07008044static int handle_invalid_op(struct kvm_vcpu *vcpu)
8045{
8046 kvm_queue_exception(vcpu, UD_VECTOR);
8047 return 1;
8048}
8049
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008050static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8051{
8052 return 1;
8053}
8054
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008055static int handle_monitor(struct kvm_vcpu *vcpu)
8056{
8057 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8058 return handle_nop(vcpu);
8059}
8060
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008061/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008062 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8063 * set the success or error code of an emulated VMX instruction, as specified
8064 * by Vol 2B, VMX Instruction Reference, "Conventions".
8065 */
8066static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8067{
8068 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8069 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8070 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8071}
8072
8073static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8074{
8075 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8076 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8077 X86_EFLAGS_SF | X86_EFLAGS_OF))
8078 | X86_EFLAGS_CF);
8079}
8080
Abel Gordon145c28d2013-04-18 14:36:55 +03008081static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008082 u32 vm_instruction_error)
8083{
8084 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8085 /*
8086 * failValid writes the error number to the current VMCS, which
8087 * can't be done there isn't a current VMCS.
8088 */
8089 nested_vmx_failInvalid(vcpu);
8090 return;
8091 }
8092 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8093 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8094 X86_EFLAGS_SF | X86_EFLAGS_OF))
8095 | X86_EFLAGS_ZF);
8096 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8097 /*
8098 * We don't need to force a shadow sync because
8099 * VM_INSTRUCTION_ERROR is not shadowed
8100 */
8101}
Abel Gordon145c28d2013-04-18 14:36:55 +03008102
Wincy Vanff651cb2014-12-11 08:52:58 +03008103static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8104{
8105 /* TODO: not to reset guest simply here. */
8106 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008107 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008108}
8109
Jan Kiszkaf4124502014-03-07 20:03:13 +01008110static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8111{
8112 struct vcpu_vmx *vmx =
8113 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8114
8115 vmx->nested.preemption_timer_expired = true;
8116 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8117 kvm_vcpu_kick(&vmx->vcpu);
8118
8119 return HRTIMER_NORESTART;
8120}
8121
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008122/*
Bandan Das19677e32014-05-06 02:19:15 -04008123 * Decode the memory-address operand of a vmx instruction, as recorded on an
8124 * exit caused by such an instruction (run by a guest hypervisor).
8125 * On success, returns 0. When the operand is invalid, returns 1 and throws
8126 * #UD or #GP.
8127 */
8128static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8129 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008130 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008131{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008132 gva_t off;
8133 bool exn;
8134 struct kvm_segment s;
8135
Bandan Das19677e32014-05-06 02:19:15 -04008136 /*
8137 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8138 * Execution", on an exit, vmx_instruction_info holds most of the
8139 * addressing components of the operand. Only the displacement part
8140 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8141 * For how an actual address is calculated from all these components,
8142 * refer to Vol. 1, "Operand Addressing".
8143 */
8144 int scaling = vmx_instruction_info & 3;
8145 int addr_size = (vmx_instruction_info >> 7) & 7;
8146 bool is_reg = vmx_instruction_info & (1u << 10);
8147 int seg_reg = (vmx_instruction_info >> 15) & 7;
8148 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8149 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8150 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8151 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8152
8153 if (is_reg) {
8154 kvm_queue_exception(vcpu, UD_VECTOR);
8155 return 1;
8156 }
8157
8158 /* Addr = segment_base + offset */
8159 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008160 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008161 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008162 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008163 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008164 off += kvm_register_read(vcpu, index_reg)<<scaling;
8165 vmx_get_segment(vcpu, &s, seg_reg);
8166 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008167
8168 if (addr_size == 1) /* 32 bit */
8169 *ret &= 0xffffffff;
8170
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008171 /* Checks for #GP/#SS exceptions. */
8172 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008173 if (is_long_mode(vcpu)) {
8174 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8175 * non-canonical form. This is the only check on the memory
8176 * destination for long mode!
8177 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008178 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008179 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008180 /* Protected mode: apply checks for segment validity in the
8181 * following order:
8182 * - segment type check (#GP(0) may be thrown)
8183 * - usability check (#GP(0)/#SS(0))
8184 * - limit check (#GP(0)/#SS(0))
8185 */
8186 if (wr)
8187 /* #GP(0) if the destination operand is located in a
8188 * read-only data segment or any code segment.
8189 */
8190 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8191 else
8192 /* #GP(0) if the source operand is located in an
8193 * execute-only code segment
8194 */
8195 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008196 if (exn) {
8197 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8198 return 1;
8199 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008200 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8201 */
8202 exn = (s.unusable != 0);
8203 /* Protected mode: #GP(0)/#SS(0) if the memory
8204 * operand is outside the segment limit.
8205 */
8206 exn = exn || (off + sizeof(u64) > s.limit);
8207 }
8208 if (exn) {
8209 kvm_queue_exception_e(vcpu,
8210 seg_reg == VCPU_SREG_SS ?
8211 SS_VECTOR : GP_VECTOR,
8212 0);
8213 return 1;
8214 }
8215
Bandan Das19677e32014-05-06 02:19:15 -04008216 return 0;
8217}
8218
Radim Krčmářcbf71272017-05-19 15:48:51 +02008219static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008220{
8221 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008222 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008223
8224 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008225 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008226 return 1;
8227
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008228 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008229 kvm_inject_page_fault(vcpu, &e);
8230 return 1;
8231 }
8232
Bandan Das3573e222014-05-06 02:19:16 -04008233 return 0;
8234}
8235
Liran Alonabfc52c2018-06-23 02:35:13 +03008236/*
8237 * Allocate a shadow VMCS and associate it with the currently loaded
8238 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8239 * VMCS is also VMCLEARed, so that it is ready for use.
8240 */
8241static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8242{
8243 struct vcpu_vmx *vmx = to_vmx(vcpu);
8244 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8245
8246 /*
8247 * We should allocate a shadow vmcs for vmcs01 only when L1
8248 * executes VMXON and free it when L1 executes VMXOFF.
8249 * As it is invalid to execute VMXON twice, we shouldn't reach
8250 * here when vmcs01 already have an allocated shadow vmcs.
8251 */
8252 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8253
8254 if (!loaded_vmcs->shadow_vmcs) {
8255 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8256 if (loaded_vmcs->shadow_vmcs)
8257 vmcs_clear(loaded_vmcs->shadow_vmcs);
8258 }
8259 return loaded_vmcs->shadow_vmcs;
8260}
8261
Jim Mattsone29acc52016-11-30 12:03:43 -08008262static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8263{
8264 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008265 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008266
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008267 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8268 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008269 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008270
8271 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8272 if (!vmx->nested.cached_vmcs12)
8273 goto out_cached_vmcs12;
8274
Liran Alon61ada742018-06-23 02:35:08 +03008275 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8276 if (!vmx->nested.cached_shadow_vmcs12)
8277 goto out_cached_shadow_vmcs12;
8278
Liran Alonabfc52c2018-06-23 02:35:13 +03008279 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8280 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008281
Jim Mattsone29acc52016-11-30 12:03:43 -08008282 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8283 HRTIMER_MODE_REL_PINNED);
8284 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8285
Roman Kagan63aff652018-07-19 21:59:07 +03008286 vmx->nested.vpid02 = allocate_vpid();
8287
Sean Christopherson9d6105b22018-09-26 09:23:51 -07008288 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008289 vmx->nested.vmxon = true;
8290 return 0;
8291
8292out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008293 kfree(vmx->nested.cached_shadow_vmcs12);
8294
8295out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008296 kfree(vmx->nested.cached_vmcs12);
8297
8298out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008299 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008300
Jim Mattsonde3a0022017-11-27 17:22:25 -06008301out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008302 return -ENOMEM;
8303}
8304
Bandan Das3573e222014-05-06 02:19:16 -04008305/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008306 * Emulate the VMXON instruction.
8307 * Currently, we just remember that VMX is active, and do not save or even
8308 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8309 * do not currently need to store anything in that guest-allocated memory
8310 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8311 * argument is different from the VMXON pointer (which the spec says they do).
8312 */
8313static int handle_vmon(struct kvm_vcpu *vcpu)
8314{
Jim Mattsone29acc52016-11-30 12:03:43 -08008315 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008316 gpa_t vmptr;
8317 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008318 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008319 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8320 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008321
Jim Mattson70f3aac2017-04-26 08:53:46 -07008322 /*
8323 * The Intel VMX Instruction Reference lists a bunch of bits that are
8324 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8325 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8326 * Otherwise, we should fail with #UD. But most faulting conditions
8327 * have already been checked by hardware, prior to the VM-exit for
8328 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8329 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008330 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008331 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008332 kvm_queue_exception(vcpu, UD_VECTOR);
8333 return 1;
8334 }
8335
Felix Wilhelm727ba742018-06-11 09:43:44 +02008336 /* CPL=0 must be checked manually. */
8337 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008338 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008339 return 1;
8340 }
8341
Abel Gordon145c28d2013-04-18 14:36:55 +03008342 if (vmx->nested.vmxon) {
8343 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008344 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008345 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008346
Haozhong Zhang3b840802016-06-22 14:59:54 +08008347 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008348 != VMXON_NEEDED_FEATURES) {
8349 kvm_inject_gp(vcpu, 0);
8350 return 1;
8351 }
8352
Radim Krčmářcbf71272017-05-19 15:48:51 +02008353 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008354 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008355
8356 /*
8357 * SDM 3: 24.11.5
8358 * The first 4 bytes of VMXON region contain the supported
8359 * VMCS revision identifier
8360 *
8361 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8362 * which replaces physical address width with 32
8363 */
8364 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8365 nested_vmx_failInvalid(vcpu);
8366 return kvm_skip_emulated_instruction(vcpu);
8367 }
8368
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008369 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8370 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008371 nested_vmx_failInvalid(vcpu);
8372 return kvm_skip_emulated_instruction(vcpu);
8373 }
8374 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8375 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008376 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008377 nested_vmx_failInvalid(vcpu);
8378 return kvm_skip_emulated_instruction(vcpu);
8379 }
8380 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008381 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008382
8383 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008384 ret = enter_vmx_operation(vcpu);
8385 if (ret)
8386 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008387
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008388 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008389 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008390}
8391
8392/*
8393 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8394 * for running VMX instructions (except VMXON, whose prerequisites are
8395 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008396 * Note that many of these exceptions have priority over VM exits, so they
8397 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008398 */
8399static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8400{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008401 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008402 kvm_queue_exception(vcpu, UD_VECTOR);
8403 return 0;
8404 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008405
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008406 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008407 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008408 return 0;
8409 }
8410
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008411 return 1;
8412}
8413
David Matlack8ca44e82017-08-01 14:00:39 -07008414static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8415{
8416 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8417 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8418}
8419
Abel Gordone7953d72013-04-18 14:37:55 +03008420static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8421{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008422 if (vmx->nested.current_vmptr == -1ull)
8423 return;
8424
Abel Gordon012f83c2013-04-18 14:39:25 +03008425 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008426 /* copy to memory all shadowed fields in case
8427 they were modified */
8428 copy_shadow_to_vmcs12(vmx);
8429 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008430 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008431 }
Wincy Van705699a2015-02-03 23:58:17 +08008432 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008433
8434 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008435 kvm_vcpu_write_guest_page(&vmx->vcpu,
8436 vmx->nested.current_vmptr >> PAGE_SHIFT,
8437 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008438
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008439 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008440}
8441
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008442/*
8443 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8444 * just stops using VMX.
8445 */
8446static void free_nested(struct vcpu_vmx *vmx)
8447{
Wanpeng Lib7455822017-11-22 14:04:00 -08008448 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008449 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008450
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008451 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008452 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008453 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008454 vmx->nested.posted_intr_nv = -1;
8455 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008456 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008457 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008458 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8459 free_vmcs(vmx->vmcs01.shadow_vmcs);
8460 vmx->vmcs01.shadow_vmcs = NULL;
8461 }
David Matlack4f2777b2016-07-13 17:16:37 -07008462 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008463 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008464 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008465 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008466 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008467 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008468 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008469 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008470 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008471 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008472 }
Wincy Van705699a2015-02-03 23:58:17 +08008473 if (vmx->nested.pi_desc_page) {
8474 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008475 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008476 vmx->nested.pi_desc_page = NULL;
8477 vmx->nested.pi_desc = NULL;
8478 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008479
Jim Mattsonde3a0022017-11-27 17:22:25 -06008480 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008481}
8482
8483/* Emulate the VMXOFF instruction */
8484static int handle_vmoff(struct kvm_vcpu *vcpu)
8485{
8486 if (!nested_vmx_check_permission(vcpu))
8487 return 1;
8488 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008489 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008490 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008491}
8492
Nadav Har'El27d6c862011-05-25 23:06:59 +03008493/* Emulate the VMCLEAR instruction */
8494static int handle_vmclear(struct kvm_vcpu *vcpu)
8495{
8496 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008497 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008498 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008499
8500 if (!nested_vmx_check_permission(vcpu))
8501 return 1;
8502
Radim Krčmářcbf71272017-05-19 15:48:51 +02008503 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008504 return 1;
8505
Radim Krčmářcbf71272017-05-19 15:48:51 +02008506 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8507 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8508 return kvm_skip_emulated_instruction(vcpu);
8509 }
8510
8511 if (vmptr == vmx->nested.vmxon_ptr) {
8512 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8513 return kvm_skip_emulated_instruction(vcpu);
8514 }
8515
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008516 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008517 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008518
Jim Mattson587d7e722017-03-02 12:41:48 -08008519 kvm_vcpu_write_guest(vcpu,
8520 vmptr + offsetof(struct vmcs12, launch_state),
8521 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008522
Nadav Har'El27d6c862011-05-25 23:06:59 +03008523 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008524 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008525}
8526
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008527static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8528
8529/* Emulate the VMLAUNCH instruction */
8530static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8531{
8532 return nested_vmx_run(vcpu, true);
8533}
8534
8535/* Emulate the VMRESUME instruction */
8536static int handle_vmresume(struct kvm_vcpu *vcpu)
8537{
8538
8539 return nested_vmx_run(vcpu, false);
8540}
8541
Nadav Har'El49f705c2011-05-25 23:08:30 +03008542/*
8543 * Read a vmcs12 field. Since these can have varying lengths and we return
8544 * one type, we chose the biggest type (u64) and zero-extend the return value
8545 * to that size. Note that the caller, handle_vmread, might need to use only
8546 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8547 * 64-bit fields are to be returned).
8548 */
Liran Alone2536742018-06-23 02:35:02 +03008549static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008550 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551{
8552 short offset = vmcs_field_to_offset(field);
8553 char *p;
8554
8555 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008556 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008557
Liran Alone2536742018-06-23 02:35:02 +03008558 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559
Jim Mattsond37f4262017-12-22 12:12:16 -08008560 switch (vmcs_field_width(field)) {
8561 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008563 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008564 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008565 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008566 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008567 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008568 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008569 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008570 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008571 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008572 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008573 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 WARN_ON(1);
8575 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008576 }
8577}
8578
Abel Gordon20b97fe2013-04-18 14:36:25 +03008579
Liran Alone2536742018-06-23 02:35:02 +03008580static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008581 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008582 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008583 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008585 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586
Jim Mattsond37f4262017-12-22 12:12:16 -08008587 switch (vmcs_field_width(field)) {
8588 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008589 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008591 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008592 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008593 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008594 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008595 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008596 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008597 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008598 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008599 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008600 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008601 WARN_ON(1);
8602 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008603 }
8604
8605}
8606
Jim Mattsonf4160e42018-05-29 09:11:33 -07008607/*
8608 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8609 * they have been modified by the L1 guest. Note that the "read-only"
8610 * VM-exit information fields are actually writable if the vCPU is
8611 * configured to support "VMWRITE to any supported field in the VMCS."
8612 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008613static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8614{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008615 const u16 *fields[] = {
8616 shadow_read_write_fields,
8617 shadow_read_only_fields
8618 };
8619 const int max_fields[] = {
8620 max_shadow_read_write_fields,
8621 max_shadow_read_only_fields
8622 };
8623 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008624 unsigned long field;
8625 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008626 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008627
Jan Kiszka282da872014-10-08 18:05:39 +02008628 preempt_disable();
8629
Abel Gordon16f5b902013-04-18 14:38:25 +03008630 vmcs_load(shadow_vmcs);
8631
Jim Mattsonf4160e42018-05-29 09:11:33 -07008632 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8633 for (i = 0; i < max_fields[q]; i++) {
8634 field = fields[q][i];
8635 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008636 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008637 }
8638 /*
8639 * Skip the VM-exit information fields if they are read-only.
8640 */
8641 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8642 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008643 }
8644
8645 vmcs_clear(shadow_vmcs);
8646 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008647
8648 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008649}
8650
Abel Gordonc3114422013-04-18 14:38:55 +03008651static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8652{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008653 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008654 shadow_read_write_fields,
8655 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008656 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008657 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008658 max_shadow_read_write_fields,
8659 max_shadow_read_only_fields
8660 };
8661 int i, q;
8662 unsigned long field;
8663 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008664 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008665
8666 vmcs_load(shadow_vmcs);
8667
Mathias Krausec2bae892013-06-26 20:36:21 +02008668 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008669 for (i = 0; i < max_fields[q]; i++) {
8670 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008671 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008672 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008673 }
8674 }
8675
8676 vmcs_clear(shadow_vmcs);
8677 vmcs_load(vmx->loaded_vmcs->vmcs);
8678}
8679
Nadav Har'El49f705c2011-05-25 23:08:30 +03008680/*
8681 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8682 * used before) all generate the same failure when it is missing.
8683 */
8684static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8685{
8686 struct vcpu_vmx *vmx = to_vmx(vcpu);
8687 if (vmx->nested.current_vmptr == -1ull) {
8688 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008689 return 0;
8690 }
8691 return 1;
8692}
8693
8694static int handle_vmread(struct kvm_vcpu *vcpu)
8695{
8696 unsigned long field;
8697 u64 field_value;
8698 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8699 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8700 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008701 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008702
Kyle Hueyeb277562016-11-29 12:40:39 -08008703 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008704 return 1;
8705
Kyle Huey6affcbe2016-11-29 12:40:40 -08008706 if (!nested_vmx_check_vmcs12(vcpu))
8707 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008708
Liran Alon6d894f42018-06-23 02:35:09 +03008709 if (!is_guest_mode(vcpu))
8710 vmcs12 = get_vmcs12(vcpu);
8711 else {
8712 /*
8713 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8714 * to shadowed-field sets the ALU flags for VMfailInvalid.
8715 */
8716 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8717 nested_vmx_failInvalid(vcpu);
8718 return kvm_skip_emulated_instruction(vcpu);
8719 }
8720 vmcs12 = get_shadow_vmcs12(vcpu);
8721 }
8722
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008724 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008725 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008726 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008727 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008728 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008729 }
8730 /*
8731 * Now copy part of this value to register or memory, as requested.
8732 * Note that the number of bits actually copied is 32 or 64 depending
8733 * on the guest's mode (32 or 64 bit), not on the given field's length.
8734 */
8735 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008736 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008737 field_value);
8738 } else {
8739 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008740 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008741 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008742 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008743 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8744 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008745 }
8746
8747 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008748 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008749}
8750
8751
8752static int handle_vmwrite(struct kvm_vcpu *vcpu)
8753{
8754 unsigned long field;
8755 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008756 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008757 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8758 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008759
Nadav Har'El49f705c2011-05-25 23:08:30 +03008760 /* The value to write might be 32 or 64 bits, depending on L1's long
8761 * mode, and eventually we need to write that into a field of several
8762 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008763 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008764 * bits into the vmcs12 field.
8765 */
8766 u64 field_value = 0;
8767 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008768 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008769
Kyle Hueyeb277562016-11-29 12:40:39 -08008770 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008771 return 1;
8772
Kyle Huey6affcbe2016-11-29 12:40:40 -08008773 if (!nested_vmx_check_vmcs12(vcpu))
8774 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008775
Nadav Har'El49f705c2011-05-25 23:08:30 +03008776 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008777 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008778 (((vmx_instruction_info) >> 3) & 0xf));
8779 else {
8780 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008781 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008782 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008783 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8784 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008785 kvm_inject_page_fault(vcpu, &e);
8786 return 1;
8787 }
8788 }
8789
8790
Nadav Amit27e6fb52014-06-18 17:19:26 +03008791 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008792 /*
8793 * If the vCPU supports "VMWRITE to any supported field in the
8794 * VMCS," then the "read-only" fields are actually read/write.
8795 */
8796 if (vmcs_field_readonly(field) &&
8797 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008798 nested_vmx_failValid(vcpu,
8799 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008800 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008801 }
8802
Liran Alon6d894f42018-06-23 02:35:09 +03008803 if (!is_guest_mode(vcpu))
8804 vmcs12 = get_vmcs12(vcpu);
8805 else {
8806 /*
8807 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8808 * to shadowed-field sets the ALU flags for VMfailInvalid.
8809 */
8810 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8811 nested_vmx_failInvalid(vcpu);
8812 return kvm_skip_emulated_instruction(vcpu);
8813 }
8814 vmcs12 = get_shadow_vmcs12(vcpu);
8815
8816 }
8817
8818 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008819 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008820 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008821 }
8822
Liran Alon6d894f42018-06-23 02:35:09 +03008823 /*
8824 * Do not track vmcs12 dirty-state if in guest-mode
8825 * as we actually dirty shadow vmcs12 instead of vmcs12.
8826 */
8827 if (!is_guest_mode(vcpu)) {
8828 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008829#define SHADOW_FIELD_RW(x) case x:
8830#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008831 /*
8832 * The fields that can be updated by L1 without a vmexit are
8833 * always updated in the vmcs02, the others go down the slow
8834 * path of prepare_vmcs02.
8835 */
8836 break;
8837 default:
8838 vmx->nested.dirty_vmcs12 = true;
8839 break;
8840 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008841 }
8842
Nadav Har'El49f705c2011-05-25 23:08:30 +03008843 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008844 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008845}
8846
Jim Mattsona8bc2842016-11-30 12:03:44 -08008847static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8848{
8849 vmx->nested.current_vmptr = vmptr;
8850 if (enable_shadow_vmcs) {
8851 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8852 SECONDARY_EXEC_SHADOW_VMCS);
8853 vmcs_write64(VMCS_LINK_POINTER,
8854 __pa(vmx->vmcs01.shadow_vmcs));
8855 vmx->nested.sync_shadow_vmcs = true;
8856 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008857 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008858}
8859
Nadav Har'El63846662011-05-25 23:07:29 +03008860/* Emulate the VMPTRLD instruction */
8861static int handle_vmptrld(struct kvm_vcpu *vcpu)
8862{
8863 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008864 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008865
8866 if (!nested_vmx_check_permission(vcpu))
8867 return 1;
8868
Radim Krčmářcbf71272017-05-19 15:48:51 +02008869 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008870 return 1;
8871
Radim Krčmářcbf71272017-05-19 15:48:51 +02008872 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8873 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8874 return kvm_skip_emulated_instruction(vcpu);
8875 }
8876
8877 if (vmptr == vmx->nested.vmxon_ptr) {
8878 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8879 return kvm_skip_emulated_instruction(vcpu);
8880 }
8881
Nadav Har'El63846662011-05-25 23:07:29 +03008882 if (vmx->nested.current_vmptr != vmptr) {
8883 struct vmcs12 *new_vmcs12;
8884 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008885 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8886 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008887 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008888 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008889 }
8890 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008891 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008892 (new_vmcs12->hdr.shadow_vmcs &&
8893 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008894 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008895 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008896 nested_vmx_failValid(vcpu,
8897 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008898 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008899 }
Nadav Har'El63846662011-05-25 23:07:29 +03008900
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008901 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008902 /*
8903 * Load VMCS12 from guest memory since it is not already
8904 * cached.
8905 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008906 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8907 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008908 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008909
Jim Mattsona8bc2842016-11-30 12:03:44 -08008910 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008911 }
8912
8913 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008914 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008915}
8916
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917/* Emulate the VMPTRST instruction */
8918static int handle_vmptrst(struct kvm_vcpu *vcpu)
8919{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008920 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8921 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8922 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008923 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008924 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008925
8926 if (!nested_vmx_check_permission(vcpu))
8927 return 1;
8928
Sean Christopherson0a06d422018-07-19 10:31:00 -07008929 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008930 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008931 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008932 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8933 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008934 kvm_inject_page_fault(vcpu, &e);
8935 return 1;
8936 }
8937 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008938 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008939}
8940
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008941/* Emulate the INVEPT instruction */
8942static int handle_invept(struct kvm_vcpu *vcpu)
8943{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008945 u32 vmx_instruction_info, types;
8946 unsigned long type;
8947 gva_t gva;
8948 struct x86_exception e;
8949 struct {
8950 u64 eptp, gpa;
8951 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008952
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008953 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008954 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008955 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008956 kvm_queue_exception(vcpu, UD_VECTOR);
8957 return 1;
8958 }
8959
8960 if (!nested_vmx_check_permission(vcpu))
8961 return 1;
8962
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008963 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008964 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008965
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008966 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008967
Jim Mattson85c856b2016-10-26 08:38:38 -07008968 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008969 nested_vmx_failValid(vcpu,
8970 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008971 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008972 }
8973
8974 /* According to the Intel VMX instruction reference, the memory
8975 * operand is read even if it isn't needed (e.g., for type==global)
8976 */
8977 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008978 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008979 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008980 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008981 kvm_inject_page_fault(vcpu, &e);
8982 return 1;
8983 }
8984
8985 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008986 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008987 /*
8988 * TODO: track mappings and invalidate
8989 * single context requests appropriately
8990 */
8991 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008992 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008993 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008994 nested_vmx_succeed(vcpu);
8995 break;
8996 default:
8997 BUG_ON(1);
8998 break;
8999 }
9000
Kyle Huey6affcbe2016-11-29 12:40:40 -08009001 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009002}
9003
Liran Alon3d5bdae2018-10-08 23:42:18 +03009004static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9005{
9006 struct vcpu_vmx *vmx = to_vmx(vcpu);
9007
9008 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9009}
9010
Petr Matouseka642fc32014-09-23 20:22:30 +02009011static int handle_invvpid(struct kvm_vcpu *vcpu)
9012{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009013 struct vcpu_vmx *vmx = to_vmx(vcpu);
9014 u32 vmx_instruction_info;
9015 unsigned long type, types;
9016 gva_t gva;
9017 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009018 struct {
9019 u64 vpid;
9020 u64 gla;
9021 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009022 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009023
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009024 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009025 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009026 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009027 kvm_queue_exception(vcpu, UD_VECTOR);
9028 return 1;
9029 }
9030
9031 if (!nested_vmx_check_permission(vcpu))
9032 return 1;
9033
9034 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9035 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9036
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009037 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009038 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009039
Jim Mattson85c856b2016-10-26 08:38:38 -07009040 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009041 nested_vmx_failValid(vcpu,
9042 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009043 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009044 }
9045
9046 /* according to the intel vmx instruction reference, the memory
9047 * operand is read even if it isn't needed (e.g., for type==global)
9048 */
9049 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9050 vmx_instruction_info, false, &gva))
9051 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009052 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009053 kvm_inject_page_fault(vcpu, &e);
9054 return 1;
9055 }
Jim Mattson40352602017-06-28 09:37:37 -07009056 if (operand.vpid >> 16) {
9057 nested_vmx_failValid(vcpu,
9058 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9059 return kvm_skip_emulated_instruction(vcpu);
9060 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009061
Liran Alon3d5bdae2018-10-08 23:42:18 +03009062 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009063 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009064 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009065 if (!operand.vpid ||
9066 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009067 nested_vmx_failValid(vcpu,
9068 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9069 return kvm_skip_emulated_instruction(vcpu);
9070 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009071 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009072 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009073 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009074 } else
Liran Alon327c0722018-10-08 23:42:19 +03009075 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009076 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009077 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009078 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009079 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009080 nested_vmx_failValid(vcpu,
9081 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009082 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009083 }
Liran Alon327c0722018-10-08 23:42:19 +03009084 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009085 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009086 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009087 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009088 break;
9089 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009090 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009091 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009092 }
9093
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009094 nested_vmx_succeed(vcpu);
9095
Kyle Huey6affcbe2016-11-29 12:40:40 -08009096 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009097}
9098
Junaid Shahideb4b2482018-06-27 14:59:14 -07009099static int handle_invpcid(struct kvm_vcpu *vcpu)
9100{
9101 u32 vmx_instruction_info;
9102 unsigned long type;
9103 bool pcid_enabled;
9104 gva_t gva;
9105 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009106 unsigned i;
9107 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009108 struct {
9109 u64 pcid;
9110 u64 gla;
9111 } operand;
9112
9113 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9114 kvm_queue_exception(vcpu, UD_VECTOR);
9115 return 1;
9116 }
9117
9118 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9119 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9120
9121 if (type > 3) {
9122 kvm_inject_gp(vcpu, 0);
9123 return 1;
9124 }
9125
9126 /* According to the Intel instruction reference, the memory operand
9127 * is read even if it isn't needed (e.g., for type==all)
9128 */
9129 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9130 vmx_instruction_info, false, &gva))
9131 return 1;
9132
9133 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9134 kvm_inject_page_fault(vcpu, &e);
9135 return 1;
9136 }
9137
9138 if (operand.pcid >> 12 != 0) {
9139 kvm_inject_gp(vcpu, 0);
9140 return 1;
9141 }
9142
9143 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9144
9145 switch (type) {
9146 case INVPCID_TYPE_INDIV_ADDR:
9147 if ((!pcid_enabled && (operand.pcid != 0)) ||
9148 is_noncanonical_address(operand.gla, vcpu)) {
9149 kvm_inject_gp(vcpu, 0);
9150 return 1;
9151 }
9152 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9153 return kvm_skip_emulated_instruction(vcpu);
9154
9155 case INVPCID_TYPE_SINGLE_CTXT:
9156 if (!pcid_enabled && (operand.pcid != 0)) {
9157 kvm_inject_gp(vcpu, 0);
9158 return 1;
9159 }
9160
9161 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9162 kvm_mmu_sync_roots(vcpu);
9163 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9164 }
9165
Junaid Shahidb94742c2018-06-27 14:59:20 -07009166 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9167 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9168 == operand.pcid)
9169 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009170
Junaid Shahidb94742c2018-06-27 14:59:20 -07009171 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009172 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009173 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009174 * given PCID, then nothing needs to be done here because a
9175 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009176 */
9177
9178 return kvm_skip_emulated_instruction(vcpu);
9179
9180 case INVPCID_TYPE_ALL_NON_GLOBAL:
9181 /*
9182 * Currently, KVM doesn't mark global entries in the shadow
9183 * page tables, so a non-global flush just degenerates to a
9184 * global flush. If needed, we could optimize this later by
9185 * keeping track of global entries in shadow page tables.
9186 */
9187
9188 /* fall-through */
9189 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9190 kvm_mmu_unload(vcpu);
9191 return kvm_skip_emulated_instruction(vcpu);
9192
9193 default:
9194 BUG(); /* We have already checked above that type <= 3 */
9195 }
9196}
9197
Kai Huang843e4332015-01-28 10:54:28 +08009198static int handle_pml_full(struct kvm_vcpu *vcpu)
9199{
9200 unsigned long exit_qualification;
9201
9202 trace_kvm_pml_full(vcpu->vcpu_id);
9203
9204 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9205
9206 /*
9207 * PML buffer FULL happened while executing iret from NMI,
9208 * "blocked by NMI" bit has to be set before next VM entry.
9209 */
9210 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009211 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009212 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9213 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9214 GUEST_INTR_STATE_NMI);
9215
9216 /*
9217 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9218 * here.., and there's no userspace involvement needed for PML.
9219 */
9220 return 1;
9221}
9222
Yunhong Jiang64672c92016-06-13 14:19:59 -07009223static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9224{
Sean Christophersond264ee02018-08-27 15:21:12 -07009225 if (!to_vmx(vcpu)->req_immediate_exit)
9226 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009227 return 1;
9228}
9229
Bandan Das41ab9372017-08-03 15:54:43 -04009230static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9231{
9232 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009233 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9234
9235 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009236 switch (address & VMX_EPTP_MT_MASK) {
9237 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009238 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009239 return false;
9240 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009241 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009242 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009243 return false;
9244 break;
9245 default:
9246 return false;
9247 }
9248
David Hildenbrandbb97a012017-08-10 23:15:28 +02009249 /* only 4 levels page-walk length are valid */
9250 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009251 return false;
9252
9253 /* Reserved bits should not be set */
9254 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9255 return false;
9256
9257 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009258 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009259 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009260 return false;
9261 }
9262
9263 return true;
9264}
9265
9266static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9267 struct vmcs12 *vmcs12)
9268{
9269 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9270 u64 address;
9271 bool accessed_dirty;
9272 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9273
9274 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9275 !nested_cpu_has_ept(vmcs12))
9276 return 1;
9277
9278 if (index >= VMFUNC_EPTP_ENTRIES)
9279 return 1;
9280
9281
9282 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9283 &address, index * 8, 8))
9284 return 1;
9285
David Hildenbrandbb97a012017-08-10 23:15:28 +02009286 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009287
9288 /*
9289 * If the (L2) guest does a vmfunc to the currently
9290 * active ept pointer, we don't have to do anything else
9291 */
9292 if (vmcs12->ept_pointer != address) {
9293 if (!valid_ept_address(vcpu, address))
9294 return 1;
9295
9296 kvm_mmu_unload(vcpu);
9297 mmu->ept_ad = accessed_dirty;
9298 mmu->base_role.ad_disabled = !accessed_dirty;
9299 vmcs12->ept_pointer = address;
9300 /*
9301 * TODO: Check what's the correct approach in case
9302 * mmu reload fails. Currently, we just let the next
9303 * reload potentially fail
9304 */
9305 kvm_mmu_reload(vcpu);
9306 }
9307
9308 return 0;
9309}
9310
Bandan Das2a499e42017-08-03 15:54:41 -04009311static int handle_vmfunc(struct kvm_vcpu *vcpu)
9312{
Bandan Das27c42a12017-08-03 15:54:42 -04009313 struct vcpu_vmx *vmx = to_vmx(vcpu);
9314 struct vmcs12 *vmcs12;
9315 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9316
9317 /*
9318 * VMFUNC is only supported for nested guests, but we always enable the
9319 * secondary control for simplicity; for non-nested mode, fake that we
9320 * didn't by injecting #UD.
9321 */
9322 if (!is_guest_mode(vcpu)) {
9323 kvm_queue_exception(vcpu, UD_VECTOR);
9324 return 1;
9325 }
9326
9327 vmcs12 = get_vmcs12(vcpu);
9328 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9329 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009330
9331 switch (function) {
9332 case 0:
9333 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9334 goto fail;
9335 break;
9336 default:
9337 goto fail;
9338 }
9339 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009340
9341fail:
9342 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9343 vmcs_read32(VM_EXIT_INTR_INFO),
9344 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009345 return 1;
9346}
9347
Sean Christopherson0b665d32018-08-14 09:33:34 -07009348static int handle_encls(struct kvm_vcpu *vcpu)
9349{
9350 /*
9351 * SGX virtualization is not yet supported. There is no software
9352 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9353 * to prevent the guest from executing ENCLS.
9354 */
9355 kvm_queue_exception(vcpu, UD_VECTOR);
9356 return 1;
9357}
9358
Nadav Har'El0140cae2011-05-25 23:06:28 +03009359/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009360 * The exit handlers return 1 if the exit was handled fully and guest execution
9361 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9362 * to be done to userspace and return 0.
9363 */
Mathias Krause772e0312012-08-30 01:30:19 +02009364static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009365 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9366 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009367 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009368 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009369 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009370 [EXIT_REASON_CR_ACCESS] = handle_cr,
9371 [EXIT_REASON_DR_ACCESS] = handle_dr,
9372 [EXIT_REASON_CPUID] = handle_cpuid,
9373 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9374 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9375 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9376 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009377 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009378 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009379 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009380 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009381 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009382 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009383 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009384 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009385 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009386 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009387 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009388 [EXIT_REASON_VMOFF] = handle_vmoff,
9389 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009390 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9391 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009392 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009393 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009394 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009395 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009396 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009397 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009398 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9399 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009400 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9401 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009402 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009403 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009404 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009405 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009406 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009407 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009408 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009409 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009410 [EXIT_REASON_XSAVES] = handle_xsaves,
9411 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009412 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009413 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009414 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009415 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009416 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009417};
9418
9419static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009420 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009421
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009422static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9423 struct vmcs12 *vmcs12)
9424{
9425 unsigned long exit_qualification;
9426 gpa_t bitmap, last_bitmap;
9427 unsigned int port;
9428 int size;
9429 u8 b;
9430
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009431 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009432 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009433
9434 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9435
9436 port = exit_qualification >> 16;
9437 size = (exit_qualification & 7) + 1;
9438
9439 last_bitmap = (gpa_t)-1;
9440 b = -1;
9441
9442 while (size > 0) {
9443 if (port < 0x8000)
9444 bitmap = vmcs12->io_bitmap_a;
9445 else if (port < 0x10000)
9446 bitmap = vmcs12->io_bitmap_b;
9447 else
Joe Perches1d804d02015-03-30 16:46:09 -07009448 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009449 bitmap += (port & 0x7fff) / 8;
9450
9451 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009452 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009453 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009454 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009455 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009456
9457 port++;
9458 size--;
9459 last_bitmap = bitmap;
9460 }
9461
Joe Perches1d804d02015-03-30 16:46:09 -07009462 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009463}
9464
Nadav Har'El644d7112011-05-25 23:12:35 +03009465/*
9466 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9467 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9468 * disinterest in the current event (read or write a specific MSR) by using an
9469 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9470 */
9471static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9472 struct vmcs12 *vmcs12, u32 exit_reason)
9473{
9474 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9475 gpa_t bitmap;
9476
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009477 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009478 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009479
9480 /*
9481 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9482 * for the four combinations of read/write and low/high MSR numbers.
9483 * First we need to figure out which of the four to use:
9484 */
9485 bitmap = vmcs12->msr_bitmap;
9486 if (exit_reason == EXIT_REASON_MSR_WRITE)
9487 bitmap += 2048;
9488 if (msr_index >= 0xc0000000) {
9489 msr_index -= 0xc0000000;
9490 bitmap += 1024;
9491 }
9492
9493 /* Then read the msr_index'th bit from this bitmap: */
9494 if (msr_index < 1024*8) {
9495 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009496 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009497 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009498 return 1 & (b >> (msr_index & 7));
9499 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009500 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009501}
9502
9503/*
9504 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9505 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9506 * intercept (via guest_host_mask etc.) the current event.
9507 */
9508static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9509 struct vmcs12 *vmcs12)
9510{
9511 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9512 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009513 int reg;
9514 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009515
9516 switch ((exit_qualification >> 4) & 3) {
9517 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009518 reg = (exit_qualification >> 8) & 15;
9519 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009520 switch (cr) {
9521 case 0:
9522 if (vmcs12->cr0_guest_host_mask &
9523 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009524 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009525 break;
9526 case 3:
9527 if ((vmcs12->cr3_target_count >= 1 &&
9528 vmcs12->cr3_target_value0 == val) ||
9529 (vmcs12->cr3_target_count >= 2 &&
9530 vmcs12->cr3_target_value1 == val) ||
9531 (vmcs12->cr3_target_count >= 3 &&
9532 vmcs12->cr3_target_value2 == val) ||
9533 (vmcs12->cr3_target_count >= 4 &&
9534 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009535 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009536 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009537 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009538 break;
9539 case 4:
9540 if (vmcs12->cr4_guest_host_mask &
9541 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009542 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009543 break;
9544 case 8:
9545 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009546 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009547 break;
9548 }
9549 break;
9550 case 2: /* clts */
9551 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9552 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009553 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009554 break;
9555 case 1: /* mov from cr */
9556 switch (cr) {
9557 case 3:
9558 if (vmcs12->cpu_based_vm_exec_control &
9559 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009560 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009561 break;
9562 case 8:
9563 if (vmcs12->cpu_based_vm_exec_control &
9564 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009565 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009566 break;
9567 }
9568 break;
9569 case 3: /* lmsw */
9570 /*
9571 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9572 * cr0. Other attempted changes are ignored, with no exit.
9573 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009574 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009575 if (vmcs12->cr0_guest_host_mask & 0xe &
9576 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009577 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009578 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9579 !(vmcs12->cr0_read_shadow & 0x1) &&
9580 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009581 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009582 break;
9583 }
Joe Perches1d804d02015-03-30 16:46:09 -07009584 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009585}
9586
Liran Alona7cde482018-06-23 02:35:10 +03009587static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9588 struct vmcs12 *vmcs12, gpa_t bitmap)
9589{
9590 u32 vmx_instruction_info;
9591 unsigned long field;
9592 u8 b;
9593
9594 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9595 return true;
9596
9597 /* Decode instruction info and find the field to access */
9598 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9599 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9600
9601 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9602 if (field >> 15)
9603 return true;
9604
9605 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9606 return true;
9607
9608 return 1 & (b >> (field & 7));
9609}
9610
Nadav Har'El644d7112011-05-25 23:12:35 +03009611/*
9612 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9613 * should handle it ourselves in L0 (and then continue L2). Only call this
9614 * when in is_guest_mode (L2).
9615 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009616static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009617{
Nadav Har'El644d7112011-05-25 23:12:35 +03009618 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9619 struct vcpu_vmx *vmx = to_vmx(vcpu);
9620 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9621
Jim Mattson4f350c62017-09-14 16:31:44 -07009622 if (vmx->nested.nested_run_pending)
9623 return false;
9624
9625 if (unlikely(vmx->fail)) {
9626 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9627 vmcs_read32(VM_INSTRUCTION_ERROR));
9628 return true;
9629 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009630
David Matlackc9f04402017-08-01 14:00:40 -07009631 /*
9632 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009633 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9634 * Page). The CPU may write to these pages via their host
9635 * physical address while L2 is running, bypassing any
9636 * address-translation-based dirty tracking (e.g. EPT write
9637 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009638 *
9639 * Mark them dirty on every exit from L2 to prevent them from
9640 * getting out of sync with dirty tracking.
9641 */
9642 nested_mark_vmcs12_pages_dirty(vcpu);
9643
Jim Mattson4f350c62017-09-14 16:31:44 -07009644 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9645 vmcs_readl(EXIT_QUALIFICATION),
9646 vmx->idt_vectoring_info,
9647 intr_info,
9648 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9649 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009650
9651 switch (exit_reason) {
9652 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009653 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009654 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009655 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009656 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009657 else if (is_debug(intr_info) &&
9658 vcpu->guest_debug &
9659 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9660 return false;
9661 else if (is_breakpoint(intr_info) &&
9662 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9663 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 return vmcs12->exception_bitmap &
9665 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9666 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009667 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009668 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009669 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009670 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009671 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009672 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009673 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009674 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009675 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009676 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009677 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009678 case EXIT_REASON_HLT:
9679 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9680 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009681 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009682 case EXIT_REASON_INVLPG:
9683 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9684 case EXIT_REASON_RDPMC:
9685 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009686 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009687 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009688 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009689 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009690 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009691 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009692 case EXIT_REASON_VMREAD:
9693 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9694 vmcs12->vmread_bitmap);
9695 case EXIT_REASON_VMWRITE:
9696 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9697 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009698 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9699 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009700 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009701 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009702 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009703 /*
9704 * VMX instructions trap unconditionally. This allows L1 to
9705 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9706 */
Joe Perches1d804d02015-03-30 16:46:09 -07009707 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009708 case EXIT_REASON_CR_ACCESS:
9709 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9710 case EXIT_REASON_DR_ACCESS:
9711 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9712 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009713 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009714 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9715 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009716 case EXIT_REASON_MSR_READ:
9717 case EXIT_REASON_MSR_WRITE:
9718 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9719 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009720 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009721 case EXIT_REASON_MWAIT_INSTRUCTION:
9722 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009723 case EXIT_REASON_MONITOR_TRAP_FLAG:
9724 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009725 case EXIT_REASON_MONITOR_INSTRUCTION:
9726 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9727 case EXIT_REASON_PAUSE_INSTRUCTION:
9728 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9729 nested_cpu_has2(vmcs12,
9730 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9731 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009732 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009733 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009734 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009735 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009736 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009737 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009738 /*
9739 * The controls for "virtualize APIC accesses," "APIC-
9740 * register virtualization," and "virtual-interrupt
9741 * delivery" only come from vmcs12.
9742 */
Joe Perches1d804d02015-03-30 16:46:09 -07009743 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009744 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009745 /*
9746 * L0 always deals with the EPT violation. If nested EPT is
9747 * used, and the nested mmu code discovers that the address is
9748 * missing in the guest EPT table (EPT12), the EPT violation
9749 * will be injected with nested_ept_inject_page_fault()
9750 */
Joe Perches1d804d02015-03-30 16:46:09 -07009751 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009752 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009753 /*
9754 * L2 never uses directly L1's EPT, but rather L0's own EPT
9755 * table (shadow on EPT) or a merged EPT table that L0 built
9756 * (EPT on EPT). So any problems with the structure of the
9757 * table is L0's fault.
9758 */
Joe Perches1d804d02015-03-30 16:46:09 -07009759 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009760 case EXIT_REASON_INVPCID:
9761 return
9762 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9763 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009764 case EXIT_REASON_WBINVD:
9765 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9766 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009767 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009768 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9769 /*
9770 * This should never happen, since it is not possible to
9771 * set XSS to a non-zero value---neither in L1 nor in L2.
9772 * If if it were, XSS would have to be checked against
9773 * the XSS exit bitmap in vmcs12.
9774 */
9775 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009776 case EXIT_REASON_PREEMPTION_TIMER:
9777 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009778 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009779 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009780 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009781 case EXIT_REASON_VMFUNC:
9782 /* VM functions are emulated through L2->L0 vmexits. */
9783 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009784 case EXIT_REASON_ENCLS:
9785 /* SGX is never exposed to L1 */
9786 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009787 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009788 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009789 }
9790}
9791
Paolo Bonzini7313c692017-07-27 10:31:25 +02009792static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9793{
9794 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9795
9796 /*
9797 * At this point, the exit interruption info in exit_intr_info
9798 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9799 * we need to query the in-kernel LAPIC.
9800 */
9801 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9802 if ((exit_intr_info &
9803 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9804 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9805 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9806 vmcs12->vm_exit_intr_error_code =
9807 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9808 }
9809
9810 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9811 vmcs_readl(EXIT_QUALIFICATION));
9812 return 1;
9813}
9814
Avi Kivity586f9602010-11-18 13:09:54 +02009815static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9816{
9817 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9818 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9819}
9820
Kai Huanga3eaa862015-11-04 13:46:05 +08009821static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009822{
Kai Huanga3eaa862015-11-04 13:46:05 +08009823 if (vmx->pml_pg) {
9824 __free_page(vmx->pml_pg);
9825 vmx->pml_pg = NULL;
9826 }
Kai Huang843e4332015-01-28 10:54:28 +08009827}
9828
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009829static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009830{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009831 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009832 u64 *pml_buf;
9833 u16 pml_idx;
9834
9835 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9836
9837 /* Do nothing if PML buffer is empty */
9838 if (pml_idx == (PML_ENTITY_NUM - 1))
9839 return;
9840
9841 /* PML index always points to next available PML buffer entity */
9842 if (pml_idx >= PML_ENTITY_NUM)
9843 pml_idx = 0;
9844 else
9845 pml_idx++;
9846
9847 pml_buf = page_address(vmx->pml_pg);
9848 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9849 u64 gpa;
9850
9851 gpa = pml_buf[pml_idx];
9852 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009853 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009854 }
9855
9856 /* reset PML index */
9857 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9858}
9859
9860/*
9861 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9862 * Called before reporting dirty_bitmap to userspace.
9863 */
9864static void kvm_flush_pml_buffers(struct kvm *kvm)
9865{
9866 int i;
9867 struct kvm_vcpu *vcpu;
9868 /*
9869 * We only need to kick vcpu out of guest mode here, as PML buffer
9870 * is flushed at beginning of all VMEXITs, and it's obvious that only
9871 * vcpus running in guest are possible to have unflushed GPAs in PML
9872 * buffer.
9873 */
9874 kvm_for_each_vcpu(i, vcpu, kvm)
9875 kvm_vcpu_kick(vcpu);
9876}
9877
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009878static void vmx_dump_sel(char *name, uint32_t sel)
9879{
9880 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009881 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009882 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9883 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9884 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9885}
9886
9887static void vmx_dump_dtsel(char *name, uint32_t limit)
9888{
9889 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9890 name, vmcs_read32(limit),
9891 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9892}
9893
9894static void dump_vmcs(void)
9895{
9896 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9897 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9898 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9899 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9900 u32 secondary_exec_control = 0;
9901 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009902 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009903 int i, n;
9904
9905 if (cpu_has_secondary_exec_ctrls())
9906 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9907
9908 pr_err("*** Guest State ***\n");
9909 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9910 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9911 vmcs_readl(CR0_GUEST_HOST_MASK));
9912 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9913 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9914 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9915 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9916 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9917 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009918 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9919 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9920 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9921 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009922 }
9923 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9924 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9925 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9926 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9927 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9928 vmcs_readl(GUEST_SYSENTER_ESP),
9929 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9930 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9931 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9932 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9933 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9934 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9935 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9936 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9937 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9938 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9939 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9940 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9941 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009942 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9943 efer, vmcs_read64(GUEST_IA32_PAT));
9944 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9945 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009946 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009947 if (cpu_has_load_perf_global_ctrl &&
9948 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009949 pr_err("PerfGlobCtl = 0x%016llx\n",
9950 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009951 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009952 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009953 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9954 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9955 vmcs_read32(GUEST_ACTIVITY_STATE));
9956 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9957 pr_err("InterruptStatus = %04x\n",
9958 vmcs_read16(GUEST_INTR_STATUS));
9959
9960 pr_err("*** Host State ***\n");
9961 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9962 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9963 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9964 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9965 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9966 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9967 vmcs_read16(HOST_TR_SELECTOR));
9968 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9969 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9970 vmcs_readl(HOST_TR_BASE));
9971 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9972 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9973 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9974 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9975 vmcs_readl(HOST_CR4));
9976 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9977 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9978 vmcs_read32(HOST_IA32_SYSENTER_CS),
9979 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9980 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009981 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9982 vmcs_read64(HOST_IA32_EFER),
9983 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009984 if (cpu_has_load_perf_global_ctrl &&
9985 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009986 pr_err("PerfGlobCtl = 0x%016llx\n",
9987 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009988
9989 pr_err("*** Control State ***\n");
9990 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9991 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9992 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9993 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9994 vmcs_read32(EXCEPTION_BITMAP),
9995 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9996 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9997 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9998 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9999 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10000 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10001 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10002 vmcs_read32(VM_EXIT_INTR_INFO),
10003 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10004 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10005 pr_err(" reason=%08x qualification=%016lx\n",
10006 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10007 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10008 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10009 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010010 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010011 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010012 pr_err("TSC Multiplier = 0x%016llx\n",
10013 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010014 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10015 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10016 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10017 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10018 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010019 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010020 n = vmcs_read32(CR3_TARGET_COUNT);
10021 for (i = 0; i + 1 < n; i += 4)
10022 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10023 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10024 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10025 if (i < n)
10026 pr_err("CR3 target%u=%016lx\n",
10027 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10028 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10029 pr_err("PLE Gap=%08x Window=%08x\n",
10030 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10031 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10032 pr_err("Virtual processor ID = 0x%04x\n",
10033 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10034}
10035
Avi Kivity6aa8b732006-12-10 02:21:36 -080010036/*
10037 * The guest has exited. See if we can fix it or if we need userspace
10038 * assistance.
10039 */
Avi Kivity851ba692009-08-24 11:10:17 +030010040static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010041{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010042 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010043 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010044 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010045
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010046 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10047
Kai Huang843e4332015-01-28 10:54:28 +080010048 /*
10049 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10050 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10051 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10052 * mode as if vcpus is in root mode, the PML buffer must has been
10053 * flushed already.
10054 */
10055 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010056 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010057
Mohammed Gamal80ced182009-09-01 12:48:18 +020010058 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010059 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010060 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010061
Paolo Bonzini7313c692017-07-27 10:31:25 +020010062 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10063 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010064
Mohammed Gamal51207022010-05-31 22:40:54 +030010065 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010066 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010067 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10068 vcpu->run->fail_entry.hardware_entry_failure_reason
10069 = exit_reason;
10070 return 0;
10071 }
10072
Avi Kivity29bd8a72007-09-10 17:27:03 +030010073 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010074 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10075 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010076 = vmcs_read32(VM_INSTRUCTION_ERROR);
10077 return 0;
10078 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010079
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010080 /*
10081 * Note:
10082 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10083 * delivery event since it indicates guest is accessing MMIO.
10084 * The vm-exit can be triggered again after return to guest that
10085 * will cause infinite loop.
10086 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010087 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010088 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010089 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010090 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010091 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10092 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10093 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010094 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010095 vcpu->run->internal.data[0] = vectoring_info;
10096 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010097 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10098 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10099 vcpu->run->internal.ndata++;
10100 vcpu->run->internal.data[3] =
10101 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10102 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010103 return 0;
10104 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010105
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010106 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010107 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10108 if (vmx_interrupt_allowed(vcpu)) {
10109 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10110 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10111 vcpu->arch.nmi_pending) {
10112 /*
10113 * This CPU don't support us in finding the end of an
10114 * NMI-blocked window if the guest runs with IRQs
10115 * disabled. So we pull the trigger after 1 s of
10116 * futile waiting, but inform the user about this.
10117 */
10118 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10119 "state on VCPU %d after 1 s timeout\n",
10120 __func__, vcpu->vcpu_id);
10121 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10122 }
10123 }
10124
Avi Kivity6aa8b732006-12-10 02:21:36 -080010125 if (exit_reason < kvm_vmx_max_exit_handlers
10126 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010127 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010128 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010129 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10130 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010131 kvm_queue_exception(vcpu, UD_VECTOR);
10132 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010133 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010134}
10135
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010136/*
10137 * Software based L1D cache flush which is used when microcode providing
10138 * the cache control MSR is not loaded.
10139 *
10140 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10141 * flush it is required to read in 64 KiB because the replacement algorithm
10142 * is not exactly LRU. This could be sized at runtime via topology
10143 * information but as all relevant affected CPUs have 32KiB L1D cache size
10144 * there is no point in doing so.
10145 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010146static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010147{
10148 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010149
10150 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010151 * This code is only executed when the the flush mode is 'cond' or
10152 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010153 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010154 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010155 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010156
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010157 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010158 * Clear the per-vcpu flush bit, it gets set again
10159 * either from vcpu_run() or from one of the unsafe
10160 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010161 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010162 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010163 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010164
10165 /*
10166 * Clear the per-cpu flush bit, it gets set again from
10167 * the interrupt handlers.
10168 */
10169 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10170 kvm_clear_cpu_l1tf_flush_l1d();
10171
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010172 if (!flush_l1d)
10173 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010174 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010175
10176 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010177
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010178 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10179 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10180 return;
10181 }
10182
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010183 asm volatile(
10184 /* First ensure the pages are in the TLB */
10185 "xorl %%eax, %%eax\n"
10186 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010187 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010188 "addl $4096, %%eax\n\t"
10189 "cmpl %%eax, %[size]\n\t"
10190 "jne .Lpopulate_tlb\n\t"
10191 "xorl %%eax, %%eax\n\t"
10192 "cpuid\n\t"
10193 /* Now fill the cache */
10194 "xorl %%eax, %%eax\n"
10195 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010196 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010197 "addl $64, %%eax\n\t"
10198 "cmpl %%eax, %[size]\n\t"
10199 "jne .Lfill_cache\n\t"
10200 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010201 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010202 [size] "r" (size)
10203 : "eax", "ebx", "ecx", "edx");
10204}
10205
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010206static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010207{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010208 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10209
10210 if (is_guest_mode(vcpu) &&
10211 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10212 return;
10213
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010214 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010215 vmcs_write32(TPR_THRESHOLD, 0);
10216 return;
10217 }
10218
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010219 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010220}
10221
Jim Mattson8d860bb2018-05-09 16:56:05 -040010222static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010223{
10224 u32 sec_exec_control;
10225
Jim Mattson8d860bb2018-05-09 16:56:05 -040010226 if (!lapic_in_kernel(vcpu))
10227 return;
10228
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010229 if (!flexpriority_enabled &&
10230 !cpu_has_vmx_virtualize_x2apic_mode())
10231 return;
10232
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010233 /* Postpone execution until vmcs01 is the current VMCS. */
10234 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010235 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010236 return;
10237 }
10238
Yang Zhang8d146952013-01-25 10:18:50 +080010239 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010240 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10241 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010242
Jim Mattson8d860bb2018-05-09 16:56:05 -040010243 switch (kvm_get_apic_mode(vcpu)) {
10244 case LAPIC_MODE_INVALID:
10245 WARN_ONCE(true, "Invalid local APIC state");
10246 case LAPIC_MODE_DISABLED:
10247 break;
10248 case LAPIC_MODE_XAPIC:
10249 if (flexpriority_enabled) {
10250 sec_exec_control |=
10251 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10252 vmx_flush_tlb(vcpu, true);
10253 }
10254 break;
10255 case LAPIC_MODE_X2APIC:
10256 if (cpu_has_vmx_virtualize_x2apic_mode())
10257 sec_exec_control |=
10258 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10259 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010260 }
10261 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10262
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010263 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010264}
10265
Tang Chen38b99172014-09-24 15:57:54 +080010266static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10267{
Jim Mattsonab5df312018-05-09 17:02:03 -040010268 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010269 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010270 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010271 }
Tang Chen38b99172014-09-24 15:57:54 +080010272}
10273
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010274static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010275{
10276 u16 status;
10277 u8 old;
10278
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010279 if (max_isr == -1)
10280 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010281
10282 status = vmcs_read16(GUEST_INTR_STATUS);
10283 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010284 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010285 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010286 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010287 vmcs_write16(GUEST_INTR_STATUS, status);
10288 }
10289}
10290
10291static void vmx_set_rvi(int vector)
10292{
10293 u16 status;
10294 u8 old;
10295
Wei Wang4114c272014-11-05 10:53:43 +080010296 if (vector == -1)
10297 vector = 0;
10298
Yang Zhangc7c9c562013-01-25 10:18:51 +080010299 status = vmcs_read16(GUEST_INTR_STATUS);
10300 old = (u8)status & 0xff;
10301 if ((u8)vector != old) {
10302 status &= ~0xff;
10303 status |= (u8)vector;
10304 vmcs_write16(GUEST_INTR_STATUS, status);
10305 }
10306}
10307
10308static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10309{
Liran Alon851c1a182017-12-24 18:12:56 +020010310 /*
10311 * When running L2, updating RVI is only relevant when
10312 * vmcs12 virtual-interrupt-delivery enabled.
10313 * However, it can be enabled only when L1 also
10314 * intercepts external-interrupts and in that case
10315 * we should not update vmcs02 RVI but instead intercept
10316 * interrupt. Therefore, do nothing when running L2.
10317 */
10318 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010319 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010320}
10321
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010322static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010323{
10324 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010325 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010326 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010327
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010328 WARN_ON(!vcpu->arch.apicv_active);
10329 if (pi_test_on(&vmx->pi_desc)) {
10330 pi_clear_on(&vmx->pi_desc);
10331 /*
10332 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10333 * But on x86 this is just a compiler barrier anyway.
10334 */
10335 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010336 max_irr_updated =
10337 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10338
10339 /*
10340 * If we are running L2 and L1 has a new pending interrupt
10341 * which can be injected, we should re-evaluate
10342 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010343 * If L1 intercepts external-interrupts, we should
10344 * exit from L2 to L1. Otherwise, interrupt should be
10345 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010346 */
Liran Alon851c1a182017-12-24 18:12:56 +020010347 if (is_guest_mode(vcpu) && max_irr_updated) {
10348 if (nested_exit_on_intr(vcpu))
10349 kvm_vcpu_exiting_guest_mode(vcpu);
10350 else
10351 kvm_make_request(KVM_REQ_EVENT, vcpu);
10352 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010353 } else {
10354 max_irr = kvm_lapic_find_highest_irr(vcpu);
10355 }
10356 vmx_hwapic_irr_update(vcpu, max_irr);
10357 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010358}
10359
Paolo Bonzini7e712682018-10-03 13:44:26 +020010360static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10361{
10362 u8 rvi = vmx_get_rvi();
10363 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10364
10365 return ((rvi & 0xf0) > (vppr & 0xf0));
10366}
10367
Andrey Smetanin63086302015-11-10 15:36:32 +030010368static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010369{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010370 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010371 return;
10372
Yang Zhangc7c9c562013-01-25 10:18:51 +080010373 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10374 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10375 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10376 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10377}
10378
Paolo Bonzini967235d2016-12-19 14:03:45 +010010379static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10380{
10381 struct vcpu_vmx *vmx = to_vmx(vcpu);
10382
10383 pi_clear_on(&vmx->pi_desc);
10384 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10385}
10386
Avi Kivity51aa01d2010-07-20 14:31:20 +030010387static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010388{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010389 u32 exit_intr_info = 0;
10390 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010391
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010392 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10393 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010394 return;
10395
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010396 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10397 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10398 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010399
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010400 /* if exit due to PF check for async PF */
10401 if (is_page_fault(exit_intr_info))
10402 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10403
Andi Kleena0861c02009-06-08 17:37:09 +080010404 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010405 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10406 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010407 kvm_machine_check();
10408
Gleb Natapov20f65982009-05-11 13:35:55 +030010409 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010410 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010411 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010412 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010413 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010414 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010415}
Gleb Natapov20f65982009-05-11 13:35:55 +030010416
Yang Zhanga547c6d2013-04-11 19:25:10 +080010417static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10418{
10419 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10420
Yang Zhanga547c6d2013-04-11 19:25:10 +080010421 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10422 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10423 unsigned int vector;
10424 unsigned long entry;
10425 gate_desc *desc;
10426 struct vcpu_vmx *vmx = to_vmx(vcpu);
10427#ifdef CONFIG_X86_64
10428 unsigned long tmp;
10429#endif
10430
10431 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10432 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010433 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010434 asm volatile(
10435#ifdef CONFIG_X86_64
10436 "mov %%" _ASM_SP ", %[sp]\n\t"
10437 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10438 "push $%c[ss]\n\t"
10439 "push %[sp]\n\t"
10440#endif
10441 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010442 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010443 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010444 :
10445#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010446 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010447#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010448 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010449 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010450 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010451 [ss]"i"(__KERNEL_DS),
10452 [cs]"i"(__KERNEL_CS)
10453 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010454 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010455}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010456STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010457
Tom Lendackybc226f02018-05-10 22:06:39 +020010458static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010459{
Tom Lendackybc226f02018-05-10 22:06:39 +020010460 switch (index) {
10461 case MSR_IA32_SMBASE:
10462 /*
10463 * We cannot do SMM unless we can run the guest in big
10464 * real mode.
10465 */
10466 return enable_unrestricted_guest || emulate_invalid_guest_state;
10467 case MSR_AMD64_VIRT_SPEC_CTRL:
10468 /* This is AMD only. */
10469 return false;
10470 default:
10471 return true;
10472 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010473}
10474
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010475static bool vmx_mpx_supported(void)
10476{
10477 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10478 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10479}
10480
Wanpeng Li55412b22014-12-02 19:21:30 +080010481static bool vmx_xsaves_supported(void)
10482{
10483 return vmcs_config.cpu_based_2nd_exec_ctrl &
10484 SECONDARY_EXEC_XSAVES;
10485}
10486
Avi Kivity51aa01d2010-07-20 14:31:20 +030010487static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10488{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010489 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010490 bool unblock_nmi;
10491 u8 vector;
10492 bool idtv_info_valid;
10493
10494 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010495
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010496 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010497 if (vmx->loaded_vmcs->nmi_known_unmasked)
10498 return;
10499 /*
10500 * Can't use vmx->exit_intr_info since we're not sure what
10501 * the exit reason is.
10502 */
10503 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10504 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10505 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10506 /*
10507 * SDM 3: 27.7.1.2 (September 2008)
10508 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10509 * a guest IRET fault.
10510 * SDM 3: 23.2.2 (September 2008)
10511 * Bit 12 is undefined in any of the following cases:
10512 * If the VM exit sets the valid bit in the IDT-vectoring
10513 * information field.
10514 * If the VM exit is due to a double fault.
10515 */
10516 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10517 vector != DF_VECTOR && !idtv_info_valid)
10518 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10519 GUEST_INTR_STATE_NMI);
10520 else
10521 vmx->loaded_vmcs->nmi_known_unmasked =
10522 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10523 & GUEST_INTR_STATE_NMI);
10524 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10525 vmx->loaded_vmcs->vnmi_blocked_time +=
10526 ktime_to_ns(ktime_sub(ktime_get(),
10527 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010528}
10529
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010530static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010531 u32 idt_vectoring_info,
10532 int instr_len_field,
10533 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010534{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010535 u8 vector;
10536 int type;
10537 bool idtv_info_valid;
10538
10539 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010540
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010541 vcpu->arch.nmi_injected = false;
10542 kvm_clear_exception_queue(vcpu);
10543 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010544
10545 if (!idtv_info_valid)
10546 return;
10547
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010548 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010549
Avi Kivity668f6122008-07-02 09:28:55 +030010550 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10551 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010552
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010553 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010554 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010555 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010556 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010557 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010558 * Clear bit "block by NMI" before VM entry if a NMI
10559 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010560 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010561 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010562 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010563 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010564 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010565 /* fall through */
10566 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010567 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010568 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010569 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010570 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010571 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010572 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010573 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010574 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010575 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010576 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010577 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010578 break;
10579 default:
10580 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010581 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010582}
10583
Avi Kivity83422e12010-07-20 14:43:23 +030010584static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10585{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010586 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010587 VM_EXIT_INSTRUCTION_LEN,
10588 IDT_VECTORING_ERROR_CODE);
10589}
10590
Avi Kivityb463a6f2010-07-20 15:06:17 +030010591static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10592{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010593 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010594 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10595 VM_ENTRY_INSTRUCTION_LEN,
10596 VM_ENTRY_EXCEPTION_ERROR_CODE);
10597
10598 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10599}
10600
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010601static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10602{
10603 int i, nr_msrs;
10604 struct perf_guest_switch_msr *msrs;
10605
10606 msrs = perf_guest_get_msrs(&nr_msrs);
10607
10608 if (!msrs)
10609 return;
10610
10611 for (i = 0; i < nr_msrs; i++)
10612 if (msrs[i].host == msrs[i].guest)
10613 clear_atomic_switch_msr(vmx, msrs[i].msr);
10614 else
10615 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010616 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010617}
10618
Sean Christophersonf459a702018-08-27 15:21:11 -070010619static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10620{
10621 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10622 if (!vmx->loaded_vmcs->hv_timer_armed)
10623 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10624 PIN_BASED_VMX_PREEMPTION_TIMER);
10625 vmx->loaded_vmcs->hv_timer_armed = true;
10626}
10627
10628static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010629{
10630 struct vcpu_vmx *vmx = to_vmx(vcpu);
10631 u64 tscl;
10632 u32 delta_tsc;
10633
Sean Christophersond264ee02018-08-27 15:21:12 -070010634 if (vmx->req_immediate_exit) {
10635 vmx_arm_hv_timer(vmx, 0);
10636 return;
10637 }
10638
Sean Christophersonf459a702018-08-27 15:21:11 -070010639 if (vmx->hv_deadline_tsc != -1) {
10640 tscl = rdtsc();
10641 if (vmx->hv_deadline_tsc > tscl)
10642 /* set_hv_timer ensures the delta fits in 32-bits */
10643 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10644 cpu_preemption_timer_multi);
10645 else
10646 delta_tsc = 0;
10647
10648 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010649 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010650 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010651
Sean Christophersonf459a702018-08-27 15:21:11 -070010652 if (vmx->loaded_vmcs->hv_timer_armed)
10653 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10654 PIN_BASED_VMX_PREEMPTION_TIMER);
10655 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010656}
10657
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010658static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010659{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010660 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010661 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010662
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010663 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010664 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010665 vmx->loaded_vmcs->soft_vnmi_blocked))
10666 vmx->loaded_vmcs->entry_time = ktime_get();
10667
Avi Kivity104f2262010-11-18 13:12:52 +020010668 /* Don't enter VMX if guest state is invalid, let the exit handler
10669 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010670 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010671 return;
10672
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010673 if (vmx->ple_window_dirty) {
10674 vmx->ple_window_dirty = false;
10675 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10676 }
10677
Abel Gordon012f83c2013-04-18 14:39:25 +030010678 if (vmx->nested.sync_shadow_vmcs) {
10679 copy_vmcs12_to_shadow(vmx);
10680 vmx->nested.sync_shadow_vmcs = false;
10681 }
10682
Avi Kivity104f2262010-11-18 13:12:52 +020010683 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10684 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10685 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10686 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10687
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010688 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010689 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010690 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010691 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010692 }
10693
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010694 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010695 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010696 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010697 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010698 }
10699
Avi Kivity104f2262010-11-18 13:12:52 +020010700 /* When single-stepping over STI and MOV SS, we must clear the
10701 * corresponding interruptibility bits in the guest state. Otherwise
10702 * vmentry fails as it then expects bit 14 (BS) in pending debug
10703 * exceptions being set, but that's not correct for the guest debugging
10704 * case. */
10705 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10706 vmx_set_interrupt_shadow(vcpu, 0);
10707
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010708 if (static_cpu_has(X86_FEATURE_PKU) &&
10709 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10710 vcpu->arch.pkru != vmx->host_pkru)
10711 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010712
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010713 atomic_switch_perf_msrs(vmx);
10714
Sean Christophersonf459a702018-08-27 15:21:11 -070010715 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010716
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010717 /*
10718 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10719 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10720 * is no need to worry about the conditional branch over the wrmsr
10721 * being speculatively taken.
10722 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010723 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010724
Nadav Har'Eld462b812011-05-24 15:26:10 +030010725 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010726
10727 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10728 (unsigned long)&current_evmcs->host_rsp : 0;
10729
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010730 if (static_branch_unlikely(&vmx_l1d_should_flush))
10731 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010732
Avi Kivity104f2262010-11-18 13:12:52 +020010733 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010734 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010735 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10736 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10737 "push %%" _ASM_CX " \n\t"
10738 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010739 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010740 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010741 /* Avoid VMWRITE when Enlightened VMCS is in use */
10742 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10743 "jz 2f \n\t"
10744 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10745 "jmp 1f \n\t"
10746 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010747 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010748 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010749 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010750 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10751 "mov %%cr2, %%" _ASM_DX " \n\t"
10752 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010753 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010754 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010755 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010756 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010757 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010758 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010759 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10760 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10761 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10762 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10763 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10764 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010765#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010766 "mov %c[r8](%0), %%r8 \n\t"
10767 "mov %c[r9](%0), %%r9 \n\t"
10768 "mov %c[r10](%0), %%r10 \n\t"
10769 "mov %c[r11](%0), %%r11 \n\t"
10770 "mov %c[r12](%0), %%r12 \n\t"
10771 "mov %c[r13](%0), %%r13 \n\t"
10772 "mov %c[r14](%0), %%r14 \n\t"
10773 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010774#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010775 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010776
Avi Kivity6aa8b732006-12-10 02:21:36 -080010777 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010778 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010779 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010780 "jmp 2f \n\t"
10781 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10782 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010783 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010784 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010785 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010786 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010787 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10788 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10789 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10790 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10791 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10792 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10793 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010794#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010795 "mov %%r8, %c[r8](%0) \n\t"
10796 "mov %%r9, %c[r9](%0) \n\t"
10797 "mov %%r10, %c[r10](%0) \n\t"
10798 "mov %%r11, %c[r11](%0) \n\t"
10799 "mov %%r12, %c[r12](%0) \n\t"
10800 "mov %%r13, %c[r13](%0) \n\t"
10801 "mov %%r14, %c[r14](%0) \n\t"
10802 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010803 "xor %%r8d, %%r8d \n\t"
10804 "xor %%r9d, %%r9d \n\t"
10805 "xor %%r10d, %%r10d \n\t"
10806 "xor %%r11d, %%r11d \n\t"
10807 "xor %%r12d, %%r12d \n\t"
10808 "xor %%r13d, %%r13d \n\t"
10809 "xor %%r14d, %%r14d \n\t"
10810 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010811#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010812 "mov %%cr2, %%" _ASM_AX " \n\t"
10813 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010814
Jim Mattson0cb5b302018-01-03 14:31:38 -080010815 "xor %%eax, %%eax \n\t"
10816 "xor %%ebx, %%ebx \n\t"
10817 "xor %%esi, %%esi \n\t"
10818 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010819 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010820 ".pushsection .rodata \n\t"
10821 ".global vmx_return \n\t"
10822 "vmx_return: " _ASM_PTR " 2b \n\t"
10823 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010824 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010825 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010826 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010827 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010828 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10829 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10830 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10831 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10832 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10833 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10834 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010835#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010836 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10837 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10838 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10839 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10840 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10841 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10842 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10843 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010844#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010845 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10846 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010847 : "cc", "memory"
10848#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010849 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010850 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010851#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010852 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010853#endif
10854 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010855
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010856 /*
10857 * We do not use IBRS in the kernel. If this vCPU has used the
10858 * SPEC_CTRL MSR it may have left it on; save the value and
10859 * turn it off. This is much more efficient than blindly adding
10860 * it to the atomic save/restore list. Especially as the former
10861 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10862 *
10863 * For non-nested case:
10864 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10865 * save it.
10866 *
10867 * For nested case:
10868 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10869 * save it.
10870 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010871 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010872 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010873
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010874 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010875
David Woodhouse117cc7a2018-01-12 11:11:27 +000010876 /* Eliminate branch target predictions from guest mode */
10877 vmexit_fill_RSB();
10878
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010879 /* All fields are clean at this point */
10880 if (static_branch_unlikely(&enable_evmcs))
10881 current_evmcs->hv_clean_fields |=
10882 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10883
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010884 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010885 if (vmx->host_debugctlmsr)
10886 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010887
Avi Kivityaa67f602012-08-01 16:48:03 +030010888#ifndef CONFIG_X86_64
10889 /*
10890 * The sysexit path does not restore ds/es, so we must set them to
10891 * a reasonable value ourselves.
10892 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010893 * We can't defer this to vmx_prepare_switch_to_host() since that
10894 * function may be executed in interrupt context, which saves and
10895 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010896 */
10897 loadsegment(ds, __USER_DS);
10898 loadsegment(es, __USER_DS);
10899#endif
10900
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010901 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010902 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010903 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010904 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010905 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010906 vcpu->arch.regs_dirty = 0;
10907
Gleb Natapove0b890d2013-09-25 12:51:33 +030010908 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010909 * eager fpu is enabled if PKEY is supported and CR4 is switched
10910 * back on host, so it is safe to read guest PKRU from current
10911 * XSAVE.
10912 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010913 if (static_cpu_has(X86_FEATURE_PKU) &&
10914 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10915 vcpu->arch.pkru = __read_pkru();
10916 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010917 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010918 }
10919
Gleb Natapove0b890d2013-09-25 12:51:33 +030010920 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010921 vmx->idt_vectoring_info = 0;
10922
10923 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10924 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10925 return;
10926
10927 vmx->loaded_vmcs->launched = 1;
10928 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010929
Avi Kivity51aa01d2010-07-20 14:31:20 +030010930 vmx_complete_atomic_exit(vmx);
10931 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010932 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010933}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010934STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010935
Sean Christopherson434a1e92018-03-20 12:17:18 -070010936static struct kvm *vmx_vm_alloc(void)
10937{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010938 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010939 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010940}
10941
10942static void vmx_vm_free(struct kvm *kvm)
10943{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010944 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010945}
10946
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010947static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010948{
10949 struct vcpu_vmx *vmx = to_vmx(vcpu);
10950 int cpu;
10951
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010952 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010953 return;
10954
10955 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010956 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010957 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010958 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010959 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010960
10961 vm_entry_controls_reset_shadow(vmx);
10962 vm_exit_controls_reset_shadow(vmx);
10963 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010964}
10965
Jim Mattson2f1fe812016-07-08 15:36:06 -070010966/*
10967 * Ensure that the current vmcs of the logical processor is the
10968 * vmcs01 of the vcpu before calling free_nested().
10969 */
10970static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10971{
10972 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010973
Christoffer Dallec7660c2017-12-04 21:35:23 +010010974 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010975 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010976 free_nested(vmx);
10977 vcpu_put(vcpu);
10978}
10979
Avi Kivity6aa8b732006-12-10 02:21:36 -080010980static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10981{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010982 struct vcpu_vmx *vmx = to_vmx(vcpu);
10983
Kai Huang843e4332015-01-28 10:54:28 +080010984 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010985 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010986 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010987 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010988 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010989 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010990 kfree(vmx->guest_msrs);
10991 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010992 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010993}
10994
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010995static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010996{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010997 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010998 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010999 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011000 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011001
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011002 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011003 return ERR_PTR(-ENOMEM);
11004
Wanpeng Li991e7a02015-09-16 17:30:05 +080011005 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011006
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011007 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11008 if (err)
11009 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011010
Peter Feiner4e595162016-07-07 14:49:58 -070011011 err = -ENOMEM;
11012
11013 /*
11014 * If PML is turned on, failure on enabling PML just results in failure
11015 * of creating the vcpu, therefore we can simplify PML logic (by
11016 * avoiding dealing with cases, such as enabling PML partially on vcpus
11017 * for the guest, etc.
11018 */
11019 if (enable_pml) {
11020 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11021 if (!vmx->pml_pg)
11022 goto uninit_vcpu;
11023 }
11024
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011025 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011026 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11027 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011028
Peter Feiner4e595162016-07-07 14:49:58 -070011029 if (!vmx->guest_msrs)
11030 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011031
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011032 err = alloc_loaded_vmcs(&vmx->vmcs01);
11033 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011034 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011035
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011036 msr_bitmap = vmx->vmcs01.msr_bitmap;
11037 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11038 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11039 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11040 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11041 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11042 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11043 vmx->msr_bitmap_mode = 0;
11044
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011045 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011046 cpu = get_cpu();
11047 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011048 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011049 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011050 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011051 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011052 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011053 err = alloc_apic_access_page(kvm);
11054 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011055 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011056 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011057
Sean Christophersone90008d2018-03-05 12:04:37 -080011058 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011059 err = init_rmode_identity_map(kvm);
11060 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011061 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011062 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011063
Roman Kagan63aff652018-07-19 21:59:07 +030011064 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011065 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11066 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011067
Wincy Van705699a2015-02-03 23:58:17 +080011068 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011069 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011070
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011071 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11072
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011073 /*
11074 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11075 * or POSTED_INTR_WAKEUP_VECTOR.
11076 */
11077 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11078 vmx->pi_desc.sn = 1;
11079
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011080 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011081
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011082free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011083 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011084free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011085 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011086free_pml:
11087 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011088uninit_vcpu:
11089 kvm_vcpu_uninit(&vmx->vcpu);
11090free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011091 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011092 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011093 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011094}
11095
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011096#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"
11097#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 -040011098
Wanpeng Lib31c1142018-03-12 04:53:04 -070011099static int vmx_vm_init(struct kvm *kvm)
11100{
Tianyu Lan877ad952018-07-19 08:40:23 +000011101 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11102
Wanpeng Lib31c1142018-03-12 04:53:04 -070011103 if (!ple_gap)
11104 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011105
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011106 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11107 switch (l1tf_mitigation) {
11108 case L1TF_MITIGATION_OFF:
11109 case L1TF_MITIGATION_FLUSH_NOWARN:
11110 /* 'I explicitly don't care' is set */
11111 break;
11112 case L1TF_MITIGATION_FLUSH:
11113 case L1TF_MITIGATION_FLUSH_NOSMT:
11114 case L1TF_MITIGATION_FULL:
11115 /*
11116 * Warn upon starting the first VM in a potentially
11117 * insecure environment.
11118 */
11119 if (cpu_smt_control == CPU_SMT_ENABLED)
11120 pr_warn_once(L1TF_MSG_SMT);
11121 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11122 pr_warn_once(L1TF_MSG_L1D);
11123 break;
11124 case L1TF_MITIGATION_FULL_FORCE:
11125 /* Flush is enforced */
11126 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011127 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011128 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011129 return 0;
11130}
11131
Yang, Sheng002c7f72007-07-31 14:23:01 +030011132static void __init vmx_check_processor_compat(void *rtn)
11133{
11134 struct vmcs_config vmcs_conf;
11135
11136 *(int *)rtn = 0;
11137 if (setup_vmcs_config(&vmcs_conf) < 0)
11138 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011139 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011140 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11141 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11142 smp_processor_id());
11143 *(int *)rtn = -EIO;
11144 }
11145}
11146
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011147static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011148{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011149 u8 cache;
11150 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011151
Sheng Yang522c68c2009-04-27 20:35:43 +080011152 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011153 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011154 * 2. EPT with VT-d:
11155 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011156 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011157 * b. VT-d with snooping control feature: snooping control feature of
11158 * VT-d engine can guarantee the cache correctness. Just set it
11159 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011160 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011161 * consistent with host MTRR
11162 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011163 if (is_mmio) {
11164 cache = MTRR_TYPE_UNCACHABLE;
11165 goto exit;
11166 }
11167
11168 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011169 ipat = VMX_EPT_IPAT_BIT;
11170 cache = MTRR_TYPE_WRBACK;
11171 goto exit;
11172 }
11173
11174 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11175 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011176 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011177 cache = MTRR_TYPE_WRBACK;
11178 else
11179 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011180 goto exit;
11181 }
11182
Xiao Guangrongff536042015-06-15 16:55:22 +080011183 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011184
11185exit:
11186 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011187}
11188
Sheng Yang17cc3932010-01-05 19:02:27 +080011189static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011190{
Sheng Yang878403b2010-01-05 19:02:29 +080011191 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11192 return PT_DIRECTORY_LEVEL;
11193 else
11194 /* For shadow and EPT supported 1GB page */
11195 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011196}
11197
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011198static void vmcs_set_secondary_exec_control(u32 new_ctl)
11199{
11200 /*
11201 * These bits in the secondary execution controls field
11202 * are dynamic, the others are mostly based on the hypervisor
11203 * architecture and the guest's CPUID. Do not touch the
11204 * dynamic bits.
11205 */
11206 u32 mask =
11207 SECONDARY_EXEC_SHADOW_VMCS |
11208 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011209 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11210 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011211
11212 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11213
11214 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11215 (new_ctl & ~mask) | (cur_ctl & mask));
11216}
11217
David Matlack8322ebb2016-11-29 18:14:09 -080011218/*
11219 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11220 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11221 */
11222static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11223{
11224 struct vcpu_vmx *vmx = to_vmx(vcpu);
11225 struct kvm_cpuid_entry2 *entry;
11226
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011227 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11228 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011229
11230#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11231 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011232 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011233} while (0)
11234
11235 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11236 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11237 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11238 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11239 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11240 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11241 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11242 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11243 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11244 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11245 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11246 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11247 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11248 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11249 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11250
11251 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11252 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11253 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11254 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11255 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011256 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011257
11258#undef cr4_fixed1_update
11259}
11260
Liran Alon5f76f6f2018-09-14 03:25:52 +030011261static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11262{
11263 struct vcpu_vmx *vmx = to_vmx(vcpu);
11264
11265 if (kvm_mpx_supported()) {
11266 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11267
11268 if (mpx_enabled) {
11269 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11270 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11271 } else {
11272 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11273 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11274 }
11275 }
11276}
11277
Sheng Yang0e851882009-12-18 16:48:46 +080011278static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11279{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011280 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011281
Paolo Bonzini80154d72017-08-24 13:55:35 +020011282 if (cpu_has_secondary_exec_ctrls()) {
11283 vmx_compute_secondary_exec_control(vmx);
11284 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011285 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011286
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011287 if (nested_vmx_allowed(vcpu))
11288 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11289 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11290 else
11291 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11292 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011293
Liran Alon5f76f6f2018-09-14 03:25:52 +030011294 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011295 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011296 nested_vmx_entry_exit_ctls_update(vcpu);
11297 }
Sheng Yang0e851882009-12-18 16:48:46 +080011298}
11299
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011300static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11301{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011302 if (func == 1 && nested)
11303 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011304}
11305
Yang Zhang25d92082013-08-06 12:00:32 +030011306static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11307 struct x86_exception *fault)
11308{
Jan Kiszka533558b2014-01-04 18:47:20 +010011309 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011311 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011312 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011313
Bandan Dasc5f983f2017-05-05 15:25:14 -040011314 if (vmx->nested.pml_full) {
11315 exit_reason = EXIT_REASON_PML_FULL;
11316 vmx->nested.pml_full = false;
11317 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11318 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011319 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011320 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011321 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011322
11323 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011324 vmcs12->guest_physical_address = fault->address;
11325}
11326
Peter Feiner995f00a2017-06-30 17:26:32 -070011327static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11328{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011329 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011330}
11331
Nadav Har'El155a97a2013-08-05 11:07:16 +030011332/* Callbacks for nested_ept_init_mmu_context: */
11333
11334static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11335{
11336 /* return the page table to be shadowed - in our case, EPT12 */
11337 return get_vmcs12(vcpu)->ept_pointer;
11338}
11339
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011340static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011341{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011342 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011343
Paolo Bonziniad896af2013-10-02 16:56:14 +020011344 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011345 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011346 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011347 nested_ept_ad_enabled(vcpu),
11348 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011349 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11350 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11351 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11352
11353 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011354}
11355
11356static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11357{
11358 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11359}
11360
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011361static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11362 u16 error_code)
11363{
11364 bool inequality, bit;
11365
11366 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11367 inequality =
11368 (error_code & vmcs12->page_fault_error_code_mask) !=
11369 vmcs12->page_fault_error_code_match;
11370 return inequality ^ bit;
11371}
11372
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011373static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11374 struct x86_exception *fault)
11375{
11376 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11377
11378 WARN_ON(!is_guest_mode(vcpu));
11379
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011380 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11381 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011382 vmcs12->vm_exit_intr_error_code = fault->error_code;
11383 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11384 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11385 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11386 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011387 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011388 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011389 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011390}
11391
Paolo Bonzinic9923842017-12-13 14:16:30 +010011392static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11393 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011394
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011395static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011396{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011397 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011398 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011399 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011400 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011401
11402 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011403 /*
11404 * Translate L1 physical address to host physical
11405 * address for vmcs02. Keep the page pinned, so this
11406 * physical address remains valid. We keep a reference
11407 * to it so we can release it later.
11408 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011409 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011410 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011411 vmx->nested.apic_access_page = NULL;
11412 }
11413 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011414 /*
11415 * If translation failed, no matter: This feature asks
11416 * to exit when accessing the given address, and if it
11417 * can never be accessed, this feature won't do
11418 * anything anyway.
11419 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011420 if (!is_error_page(page)) {
11421 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011422 hpa = page_to_phys(vmx->nested.apic_access_page);
11423 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11424 } else {
11425 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11426 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11427 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011428 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011429
11430 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011431 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011432 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011433 vmx->nested.virtual_apic_page = NULL;
11434 }
11435 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011436
11437 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011438 * If translation failed, VM entry will fail because
11439 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11440 * Failing the vm entry is _not_ what the processor
11441 * does but it's basically the only possibility we
11442 * have. We could still enter the guest if CR8 load
11443 * exits are enabled, CR8 store exits are enabled, and
11444 * virtualize APIC access is disabled; in this case
11445 * the processor would never use the TPR shadow and we
11446 * could simply clear the bit from the execution
11447 * control. But such a configuration is useless, so
11448 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011449 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011450 if (!is_error_page(page)) {
11451 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011452 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11453 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11454 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011455 }
11456
Wincy Van705699a2015-02-03 23:58:17 +080011457 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011458 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11459 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011460 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011461 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011462 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011463 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11464 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011465 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011466 vmx->nested.pi_desc_page = page;
11467 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011468 vmx->nested.pi_desc =
11469 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11470 (unsigned long)(vmcs12->posted_intr_desc_addr &
11471 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011472 vmcs_write64(POSTED_INTR_DESC_ADDR,
11473 page_to_phys(vmx->nested.pi_desc_page) +
11474 (unsigned long)(vmcs12->posted_intr_desc_addr &
11475 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011476 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011477 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011478 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11479 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011480 else
11481 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11482 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011483}
11484
Jan Kiszkaf4124502014-03-07 20:03:13 +010011485static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11486{
11487 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11488 struct vcpu_vmx *vmx = to_vmx(vcpu);
11489
Sean Christopherson4c008122018-08-27 15:21:10 -070011490 /*
11491 * A timer value of zero is architecturally guaranteed to cause
11492 * a VMExit prior to executing any instructions in the guest.
11493 */
11494 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011495 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11496 return;
11497 }
11498
Sean Christopherson4c008122018-08-27 15:21:10 -070011499 if (vcpu->arch.virtual_tsc_khz == 0)
11500 return;
11501
Jan Kiszkaf4124502014-03-07 20:03:13 +010011502 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11503 preemption_timeout *= 1000000;
11504 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11505 hrtimer_start(&vmx->nested.preemption_timer,
11506 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11507}
11508
Jim Mattson56a20512017-07-06 16:33:06 -070011509static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11510 struct vmcs12 *vmcs12)
11511{
11512 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11513 return 0;
11514
11515 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11516 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11517 return -EINVAL;
11518
11519 return 0;
11520}
11521
Wincy Van3af18d92015-02-03 23:49:31 +080011522static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11523 struct vmcs12 *vmcs12)
11524{
Wincy Van3af18d92015-02-03 23:49:31 +080011525 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11526 return 0;
11527
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011528 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011529 return -EINVAL;
11530
11531 return 0;
11532}
11533
Jim Mattson712b12d2017-08-24 13:24:47 -070011534static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11535 struct vmcs12 *vmcs12)
11536{
11537 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11538 return 0;
11539
11540 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11541 return -EINVAL;
11542
11543 return 0;
11544}
11545
Wincy Van3af18d92015-02-03 23:49:31 +080011546/*
11547 * Merge L0's and L1's MSR bitmap, return false to indicate that
11548 * we do not use the hardware.
11549 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011550static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11551 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011552{
Wincy Van82f0dd42015-02-03 23:57:18 +080011553 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011554 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011555 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011556 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011557 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011558 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011559 *
11560 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11561 * ensures that we do not accidentally generate an L02 MSR bitmap
11562 * from the L12 MSR bitmap that is too permissive.
11563 * 2. That L1 or L2s have actually used the MSR. This avoids
11564 * unnecessarily merging of the bitmap if the MSR is unused. This
11565 * works properly because we only update the L01 MSR bitmap lazily.
11566 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11567 * updated to reflect this when L1 (or its L2s) actually write to
11568 * the MSR.
11569 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011570 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11571 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011572
Paolo Bonzinic9923842017-12-13 14:16:30 +010011573 /* Nothing to do if the MSR bitmap is not in use. */
11574 if (!cpu_has_vmx_msr_bitmap() ||
11575 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11576 return false;
11577
Ashok Raj15d45072018-02-01 22:59:43 +010011578 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011579 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011580 return false;
11581
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011582 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11583 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011584 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011585
Radim Krčmářd048c092016-08-08 20:16:22 +020011586 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011587 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11588 /*
11589 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11590 * just lets the processor take the value from the virtual-APIC page;
11591 * take those 256 bits directly from the L1 bitmap.
11592 */
11593 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11594 unsigned word = msr / BITS_PER_LONG;
11595 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11596 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011597 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011598 } else {
11599 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11600 unsigned word = msr / BITS_PER_LONG;
11601 msr_bitmap_l0[word] = ~0;
11602 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11603 }
11604 }
11605
11606 nested_vmx_disable_intercept_for_msr(
11607 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011608 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011609 MSR_TYPE_W);
11610
11611 if (nested_cpu_has_vid(vmcs12)) {
11612 nested_vmx_disable_intercept_for_msr(
11613 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011614 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011615 MSR_TYPE_W);
11616 nested_vmx_disable_intercept_for_msr(
11617 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011618 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011619 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011620 }
Ashok Raj15d45072018-02-01 22:59:43 +010011621
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011622 if (spec_ctrl)
11623 nested_vmx_disable_intercept_for_msr(
11624 msr_bitmap_l1, msr_bitmap_l0,
11625 MSR_IA32_SPEC_CTRL,
11626 MSR_TYPE_R | MSR_TYPE_W);
11627
Ashok Raj15d45072018-02-01 22:59:43 +010011628 if (pred_cmd)
11629 nested_vmx_disable_intercept_for_msr(
11630 msr_bitmap_l1, msr_bitmap_l0,
11631 MSR_IA32_PRED_CMD,
11632 MSR_TYPE_W);
11633
Wincy Vanf2b93282015-02-03 23:56:03 +080011634 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011635 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011636
11637 return true;
11638}
11639
Liran Alon61ada742018-06-23 02:35:08 +030011640static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11641 struct vmcs12 *vmcs12)
11642{
11643 struct vmcs12 *shadow;
11644 struct page *page;
11645
11646 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11647 vmcs12->vmcs_link_pointer == -1ull)
11648 return;
11649
11650 shadow = get_shadow_vmcs12(vcpu);
11651 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11652
11653 memcpy(shadow, kmap(page), VMCS12_SIZE);
11654
11655 kunmap(page);
11656 kvm_release_page_clean(page);
11657}
11658
11659static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11660 struct vmcs12 *vmcs12)
11661{
11662 struct vcpu_vmx *vmx = to_vmx(vcpu);
11663
11664 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11665 vmcs12->vmcs_link_pointer == -1ull)
11666 return;
11667
11668 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11669 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11670}
11671
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011672static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11673 struct vmcs12 *vmcs12)
11674{
11675 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11676 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11677 return -EINVAL;
11678 else
11679 return 0;
11680}
11681
Wincy Vanf2b93282015-02-03 23:56:03 +080011682static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11683 struct vmcs12 *vmcs12)
11684{
Wincy Van82f0dd42015-02-03 23:57:18 +080011685 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011686 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011687 !nested_cpu_has_vid(vmcs12) &&
11688 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011689 return 0;
11690
11691 /*
11692 * If virtualize x2apic mode is enabled,
11693 * virtualize apic access must be disabled.
11694 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011695 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11696 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011697 return -EINVAL;
11698
Wincy Van608406e2015-02-03 23:57:51 +080011699 /*
11700 * If virtual interrupt delivery is enabled,
11701 * we must exit on external interrupts.
11702 */
11703 if (nested_cpu_has_vid(vmcs12) &&
11704 !nested_exit_on_intr(vcpu))
11705 return -EINVAL;
11706
Wincy Van705699a2015-02-03 23:58:17 +080011707 /*
11708 * bits 15:8 should be zero in posted_intr_nv,
11709 * the descriptor address has been already checked
11710 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011711 *
11712 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011713 */
11714 if (nested_cpu_has_posted_intr(vmcs12) &&
11715 (!nested_cpu_has_vid(vmcs12) ||
11716 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011717 (vmcs12->posted_intr_nv & 0xff00) ||
11718 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11719 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011720 return -EINVAL;
11721
Wincy Vanf2b93282015-02-03 23:56:03 +080011722 /* tpr shadow is needed by all apicv features. */
11723 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11724 return -EINVAL;
11725
11726 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011727}
11728
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011729static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11730 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011731 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011732{
Liran Alone2536742018-06-23 02:35:02 +030011733 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011734 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011735 u64 count, addr;
11736
Liran Alone2536742018-06-23 02:35:02 +030011737 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11738 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011739 WARN_ON(1);
11740 return -EINVAL;
11741 }
11742 if (count == 0)
11743 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011744 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011745 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11746 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011747 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011748 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11749 addr_field, maxphyaddr, count, addr);
11750 return -EINVAL;
11751 }
11752 return 0;
11753}
11754
11755static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11756 struct vmcs12 *vmcs12)
11757{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011758 if (vmcs12->vm_exit_msr_load_count == 0 &&
11759 vmcs12->vm_exit_msr_store_count == 0 &&
11760 vmcs12->vm_entry_msr_load_count == 0)
11761 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011762 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011763 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011764 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011765 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011766 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011767 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011768 return -EINVAL;
11769 return 0;
11770}
11771
Bandan Dasc5f983f2017-05-05 15:25:14 -040011772static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11773 struct vmcs12 *vmcs12)
11774{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011775 if (!nested_cpu_has_pml(vmcs12))
11776 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011777
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011778 if (!nested_cpu_has_ept(vmcs12) ||
11779 !page_address_valid(vcpu, vmcs12->pml_address))
11780 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011781
11782 return 0;
11783}
11784
Liran Alona8a7c022018-06-23 02:35:06 +030011785static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11786 struct vmcs12 *vmcs12)
11787{
11788 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11789 return 0;
11790
11791 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11792 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11793 return -EINVAL;
11794
11795 return 0;
11796}
11797
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011798static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11799 struct vmx_msr_entry *e)
11800{
11801 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011802 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011803 return -EINVAL;
11804 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11805 e->index == MSR_IA32_UCODE_REV)
11806 return -EINVAL;
11807 if (e->reserved != 0)
11808 return -EINVAL;
11809 return 0;
11810}
11811
11812static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11813 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011814{
11815 if (e->index == MSR_FS_BASE ||
11816 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011817 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11818 nested_vmx_msr_check_common(vcpu, e))
11819 return -EINVAL;
11820 return 0;
11821}
11822
11823static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11824 struct vmx_msr_entry *e)
11825{
11826 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11827 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011828 return -EINVAL;
11829 return 0;
11830}
11831
11832/*
11833 * Load guest's/host's msr at nested entry/exit.
11834 * return 0 for success, entry index for failure.
11835 */
11836static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11837{
11838 u32 i;
11839 struct vmx_msr_entry e;
11840 struct msr_data msr;
11841
11842 msr.host_initiated = false;
11843 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011844 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11845 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011846 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011847 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11848 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011849 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011850 }
11851 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011852 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011853 "%s check failed (%u, 0x%x, 0x%x)\n",
11854 __func__, i, e.index, e.reserved);
11855 goto fail;
11856 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011857 msr.index = e.index;
11858 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011859 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011860 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011861 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11862 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011863 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011864 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011865 }
11866 return 0;
11867fail:
11868 return i + 1;
11869}
11870
11871static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11872{
11873 u32 i;
11874 struct vmx_msr_entry e;
11875
11876 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011877 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011878 if (kvm_vcpu_read_guest(vcpu,
11879 gpa + i * sizeof(e),
11880 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011881 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011882 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11883 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011884 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011885 }
11886 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011887 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011888 "%s check failed (%u, 0x%x, 0x%x)\n",
11889 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011890 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011891 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011892 msr_info.host_initiated = false;
11893 msr_info.index = e.index;
11894 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011895 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011896 "%s cannot read MSR (%u, 0x%x)\n",
11897 __func__, i, e.index);
11898 return -EINVAL;
11899 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011900 if (kvm_vcpu_write_guest(vcpu,
11901 gpa + i * sizeof(e) +
11902 offsetof(struct vmx_msr_entry, value),
11903 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011904 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011905 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011906 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011907 return -EINVAL;
11908 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011909 }
11910 return 0;
11911}
11912
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011913static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11914{
11915 unsigned long invalid_mask;
11916
11917 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11918 return (val & invalid_mask) == 0;
11919}
11920
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011921/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011922 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11923 * emulating VM entry into a guest with EPT enabled.
11924 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11925 * is assigned to entry_failure_code on failure.
11926 */
11927static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011928 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011929{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011930 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011931 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011932 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11933 return 1;
11934 }
11935
11936 /*
11937 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11938 * must not be dereferenced.
11939 */
11940 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11941 !nested_ept) {
11942 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11943 *entry_failure_code = ENTRY_FAIL_PDPTE;
11944 return 1;
11945 }
11946 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011947 }
11948
Junaid Shahid50c28f22018-06-27 14:59:11 -070011949 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011950 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011951
11952 vcpu->arch.cr3 = cr3;
11953 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11954
11955 kvm_init_mmu(vcpu, false);
11956
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011957 return 0;
11958}
11959
Liran Alonefebf0a2018-10-08 23:42:20 +030011960/*
11961 * Returns if KVM is able to config CPU to tag TLB entries
11962 * populated by L2 differently than TLB entries populated
11963 * by L1.
11964 *
11965 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11966 *
11967 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11968 * with different VPID (L1 entries are tagged with vmx->vpid
11969 * while L2 entries are tagged with vmx->nested.vpid02).
11970 */
11971static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11972{
11973 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11974
11975 return nested_cpu_has_ept(vmcs12) ||
11976 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11977}
11978
Sean Christopherson3df5c372018-09-26 09:23:44 -070011979static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11980{
11981 if (vmx->nested.nested_run_pending &&
11982 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11983 return vmcs12->guest_ia32_efer;
11984 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11985 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11986 else
11987 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11988}
11989
Sean Christopherson09abe322018-09-26 09:23:50 -070011990static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011991{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011992 /*
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011993 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070011994 * according to L0's settings (vmcs12 is irrelevant here). Host
11995 * fields that come from L0 and are not constant, e.g. HOST_CR3,
11996 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011997 */
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011998 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070011999 return;
Sean Christopherson9d6105b22018-09-26 09:23:51 -070012000 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012001
12002 /* All VMFUNCs are currently emulated through L0 vmexits. */
12003 if (cpu_has_vmx_vmfunc())
12004 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12005
Sean Christopherson09abe322018-09-26 09:23:50 -070012006 if (cpu_has_vmx_posted_intr())
12007 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12008
12009 if (cpu_has_vmx_msr_bitmap())
12010 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12011
12012 if (enable_pml)
12013 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012014
12015 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012016 * Set the MSR load/store lists to match L0's settings. Only the
12017 * addresses are constant (for vmcs02), the counts can change based
12018 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012019 */
12020 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012021 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012022 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012023
Sean Christopherson09abe322018-09-26 09:23:50 -070012024 vmx_set_constant_host_state(vmx);
12025}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012026
Sean Christopherson09abe322018-09-26 09:23:50 -070012027static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12028 struct vmcs12 *vmcs12)
12029{
12030 prepare_vmcs02_constant_state(vmx);
12031
12032 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012033
12034 if (enable_vpid) {
12035 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12036 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12037 else
12038 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12039 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012040}
12041
Sean Christopherson09abe322018-09-26 09:23:50 -070012042static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012043{
Bandan Das03efce62017-05-05 15:25:15 -040012044 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012045 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012046
Sean Christopherson09abe322018-09-26 09:23:50 -070012047 if (vmx->nested.dirty_vmcs12)
12048 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012049
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012050 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012051 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12052 * entry, but only if the current (host) sp changed from the value
12053 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12054 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12055 * here we just force the write to happen on entry.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012056 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012057 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012058
Sean Christopherson09abe322018-09-26 09:23:50 -070012059 /*
12060 * PIN CONTROLS
12061 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012062 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012063
Sean Christophersonf459a702018-08-27 15:21:11 -070012064 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012065 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012066 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12067 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012068
12069 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012070 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012071 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12072 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012073 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012074 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012075 }
Jan Kiszkaf4124502014-03-07 20:03:13 +010012076 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012077
Sean Christopherson09abe322018-09-26 09:23:50 -070012078 /*
12079 * EXEC CONTROLS
12080 */
12081 exec_control = vmx_exec_control(vmx); /* L0's desires */
12082 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12083 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12084 exec_control &= ~CPU_BASED_TPR_SHADOW;
12085 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012086
Sean Christopherson09abe322018-09-26 09:23:50 -070012087 /*
12088 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12089 * nested_get_vmcs12_pages can't fix it up, the illegal value
12090 * will result in a VM entry failure.
12091 */
12092 if (exec_control & CPU_BASED_TPR_SHADOW) {
12093 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12094 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12095 } else {
12096#ifdef CONFIG_X86_64
12097 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12098 CPU_BASED_CR8_STORE_EXITING;
12099#endif
12100 }
12101
12102 /*
12103 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12104 * for I/O port accesses.
12105 */
12106 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12107 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12108 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12109
12110 /*
12111 * SECONDARY EXEC CONTROLS
12112 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012113 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012114 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012115
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012116 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012117 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012118 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012119 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012120 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012121 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012122 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12123 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012124 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012125 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12126 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12127 ~SECONDARY_EXEC_ENABLE_PML;
12128 exec_control |= vmcs12_exec_ctrl;
12129 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012130
Liran Alon32c7acf2018-06-23 02:35:11 +030012131 /* VMCS shadowing for L2 is emulated for now */
12132 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12133
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012134 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012135 vmcs_write16(GUEST_INTR_STATUS,
12136 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012137
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012138 /*
12139 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12140 * nested_get_vmcs12_pages will either fix it up or
12141 * remove the VM execution control.
12142 */
12143 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12144 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12145
Sean Christopherson0b665d32018-08-14 09:33:34 -070012146 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12147 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12148
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012149 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12150 }
12151
Jim Mattson83bafef2016-10-04 10:48:38 -070012152 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012153 * ENTRY CONTROLS
12154 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012155 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012156 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12157 * on the related bits (if supported by the CPU) in the hope that
12158 * we can avoid VMWrites during vmx_set_efer().
Sean Christopherson3df5c372018-09-26 09:23:44 -070012159 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012160 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12161 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12162 if (cpu_has_load_ia32_efer) {
12163 if (guest_efer & EFER_LMA)
12164 exec_control |= VM_ENTRY_IA32E_MODE;
12165 if (guest_efer != host_efer)
12166 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12167 }
12168 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012169
Sean Christopherson09abe322018-09-26 09:23:50 -070012170 /*
12171 * EXIT CONTROLS
12172 *
12173 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12174 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12175 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
12176 */
12177 exec_control = vmcs_config.vmexit_ctrl;
12178 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12179 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12180 vm_exit_controls_init(vmx, exec_control);
12181
12182 /*
12183 * Conceptually we want to copy the PML address and index from
12184 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12185 * since we always flush the log on each vmexit and never change
12186 * the PML address (once set), this happens to be equivalent to
12187 * simply resetting the index in vmcs02.
12188 */
12189 if (enable_pml)
12190 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12191
12192 /*
12193 * Interrupt/Exception Fields
12194 */
12195 if (vmx->nested.nested_run_pending) {
12196 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12197 vmcs12->vm_entry_intr_info_field);
12198 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12199 vmcs12->vm_entry_exception_error_code);
12200 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12201 vmcs12->vm_entry_instruction_len);
12202 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12203 vmcs12->guest_interruptibility_info);
12204 vmx->loaded_vmcs->nmi_known_unmasked =
12205 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12206 } else {
12207 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12208 }
12209}
12210
12211static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12212{
12213 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12214 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12215 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12216 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12217 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12218 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12219 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12220 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12221 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12222 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12223 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12224 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12225 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12226 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12227 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12228 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12229 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12230 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12231 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12232 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12233 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12234 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12235 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12236 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12237 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12238 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12239 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12240 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12241 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12242 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12243 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12244
12245 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12246 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12247 vmcs12->guest_pending_dbg_exceptions);
12248 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12249 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12250
12251 if (nested_cpu_has_xsaves(vmcs12))
12252 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12253
12254 /*
12255 * Whether page-faults are trapped is determined by a combination of
12256 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12257 * If enable_ept, L0 doesn't care about page faults and we should
12258 * set all of these to L1's desires. However, if !enable_ept, L0 does
12259 * care about (at least some) page faults, and because it is not easy
12260 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12261 * to exit on each and every L2 page fault. This is done by setting
12262 * MASK=MATCH=0 and (see below) EB.PF=1.
12263 * Note that below we don't need special code to set EB.PF beyond the
12264 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12265 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12266 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12267 */
12268 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12269 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12270 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12271 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12272
12273 if (cpu_has_vmx_apicv()) {
12274 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12275 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12276 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12277 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12278 }
12279
12280 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12281 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12282
12283 set_cr4_guest_host_mask(vmx);
12284
12285 if (kvm_mpx_supported()) {
12286 if (vmx->nested.nested_run_pending &&
12287 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12288 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12289 else
12290 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12291 }
12292
12293 /*
12294 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12295 */
12296 if (enable_ept) {
12297 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12298 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12299 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12300 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12301 }
12302}
12303
12304/*
12305 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12306 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12307 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12308 * guest in a way that will both be appropriate to L1's requests, and our
12309 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12310 * function also has additional necessary side-effects, like setting various
12311 * vcpu->arch fields.
12312 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12313 * is assigned to entry_failure_code on failure.
12314 */
12315static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12316 u32 *entry_failure_code)
12317{
12318 struct vcpu_vmx *vmx = to_vmx(vcpu);
12319
12320 if (vmx->nested.dirty_vmcs12) {
12321 prepare_vmcs02_full(vmx, vmcs12);
12322 vmx->nested.dirty_vmcs12 = false;
12323 }
12324
12325 /*
12326 * First, the fields that are shadowed. This must be kept in sync
12327 * with vmx_shadow_fields.h.
12328 */
12329
12330 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12331 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12332 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12333 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12334 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12335
12336 if (vmx->nested.nested_run_pending &&
12337 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12338 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12339 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12340 } else {
12341 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12342 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12343 }
12344 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12345
12346 vmx->nested.preemption_timer_expired = false;
12347 if (nested_cpu_has_preemption_timer(vmcs12))
12348 vmx_start_preemption_timer(vcpu);
12349
12350 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12351 * bitwise-or of what L1 wants to trap for L2, and what we want to
12352 * trap. Note that CR0.TS also needs updating - we do this later.
12353 */
12354 update_exception_bitmap(vcpu);
12355 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12356 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12357
Jim Mattson6514dc32018-04-26 16:09:12 -070012358 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012359 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012360 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012361 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012362 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012363 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012364 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012365
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012366 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12367
Peter Feinerc95ba922016-08-17 09:36:47 -070012368 if (kvm_has_tsc_control)
12369 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012370
12371 if (enable_vpid) {
12372 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012373 * There is no direct mapping between vpid02 and vpid12, the
12374 * vpid02 is per-vCPU for L0 and reused while the value of
12375 * vpid12 is changed w/ one invvpid during nested vmentry.
12376 * The vpid12 is allocated by L1 for L2, so it will not
12377 * influence global bitmap(for vpid01 and vpid02 allocation)
12378 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012379 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012380 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012381 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12382 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012383 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012384 }
12385 } else {
Liran Alon14389212018-10-08 23:42:17 +030012386 /*
12387 * If L1 use EPT, then L0 needs to execute INVEPT on
12388 * EPTP02 instead of EPTP01. Therefore, delay TLB
12389 * flush until vmcs02->eptp is fully updated by
12390 * KVM_REQ_LOAD_CR3. Note that this assumes
12391 * KVM_REQ_TLB_FLUSH is evaluated after
12392 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12393 */
12394 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012395 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012396 }
12397
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012398 if (nested_cpu_has_ept(vmcs12))
12399 nested_ept_init_mmu_context(vcpu);
12400 else if (nested_cpu_has2(vmcs12,
12401 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012402 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012403
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012404 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012405 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12406 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012407 * The CR0_READ_SHADOW is what L2 should have expected to read given
12408 * the specifications by L1; It's not enough to take
12409 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12410 * have more bits than L1 expected.
12411 */
12412 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12413 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12414
12415 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12416 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12417
Sean Christopherson09abe322018-09-26 09:23:50 -070012418 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012419 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012420 vmx_set_efer(vcpu, vcpu->arch.efer);
12421
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012422 /*
12423 * Guest state is invalid and unrestricted guest is disabled,
12424 * which means L1 attempted VMEntry to L2 with invalid state.
12425 * Fail the VMEntry.
12426 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012427 if (vmx->emulation_required) {
12428 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012429 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012430 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012431
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012432 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012433 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012434 entry_failure_code))
12435 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012436
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012437 if (!enable_ept)
12438 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12439
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012440 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12441 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012442 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012443}
12444
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012445static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12446{
12447 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12448 nested_cpu_has_virtual_nmis(vmcs12))
12449 return -EINVAL;
12450
12451 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12452 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12453 return -EINVAL;
12454
12455 return 0;
12456}
12457
Jim Mattsonca0bde22016-11-30 12:03:46 -080012458static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12459{
12460 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012461 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012462
12463 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12464 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12465 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12466
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012467 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12468 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12469
Jim Mattson56a20512017-07-06 16:33:06 -070012470 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12471 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12472
Jim Mattsonca0bde22016-11-30 12:03:46 -080012473 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12474 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12475
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012476 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12477 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12478
Jim Mattson712b12d2017-08-24 13:24:47 -070012479 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12480 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12481
Jim Mattsonca0bde22016-11-30 12:03:46 -080012482 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484
12485 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12486 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12487
Bandan Dasc5f983f2017-05-05 15:25:14 -040012488 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12489 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12490
Liran Alona8a7c022018-06-23 02:35:06 +030012491 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12492 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12493
Jim Mattsonca0bde22016-11-30 12:03:46 -080012494 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012495 vmx->nested.msrs.procbased_ctls_low,
12496 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012497 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12498 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012499 vmx->nested.msrs.secondary_ctls_low,
12500 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012501 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012502 vmx->nested.msrs.pinbased_ctls_low,
12503 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012504 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012505 vmx->nested.msrs.exit_ctls_low,
12506 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012507 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012508 vmx->nested.msrs.entry_ctls_low,
12509 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012510 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12511
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012512 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012513 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12514
Bandan Das41ab9372017-08-03 15:54:43 -040012515 if (nested_cpu_has_vmfunc(vmcs12)) {
12516 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012517 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012518 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12519
12520 if (nested_cpu_has_eptp_switching(vmcs12)) {
12521 if (!nested_cpu_has_ept(vmcs12) ||
12522 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12523 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12524 }
12525 }
Bandan Das27c42a12017-08-03 15:54:42 -040012526
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012527 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12528 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12529
Jim Mattsonca0bde22016-11-30 12:03:46 -080012530 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12531 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12532 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12533 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12534
Marc Orr04473782018-06-20 17:21:29 -070012535 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012536 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12537 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12538 * the values of the LMA and LME bits in the field must each be that of
12539 * the host address-space size VM-exit control.
12540 */
12541 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12542 ia32e = (vmcs12->vm_exit_controls &
12543 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12544 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12545 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12546 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12547 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12548 }
12549
12550 /*
Marc Orr04473782018-06-20 17:21:29 -070012551 * From the Intel SDM, volume 3:
12552 * Fields relevant to VM-entry event injection must be set properly.
12553 * These fields are the VM-entry interruption-information field, the
12554 * VM-entry exception error code, and the VM-entry instruction length.
12555 */
12556 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12557 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12558 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12559 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12560 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12561 bool should_have_error_code;
12562 bool urg = nested_cpu_has2(vmcs12,
12563 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12564 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12565
12566 /* VM-entry interruption-info field: interruption type */
12567 if (intr_type == INTR_TYPE_RESERVED ||
12568 (intr_type == INTR_TYPE_OTHER_EVENT &&
12569 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12570 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12571
12572 /* VM-entry interruption-info field: vector */
12573 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12574 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12575 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12576 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12577
12578 /* VM-entry interruption-info field: deliver error code */
12579 should_have_error_code =
12580 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12581 x86_exception_has_error_code(vector);
12582 if (has_error_code != should_have_error_code)
12583 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12584
12585 /* VM-entry exception error code */
12586 if (has_error_code &&
12587 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12588 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12589
12590 /* VM-entry interruption-info field: reserved bits */
12591 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12592 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12593
12594 /* VM-entry instruction length */
12595 switch (intr_type) {
12596 case INTR_TYPE_SOFT_EXCEPTION:
12597 case INTR_TYPE_SOFT_INTR:
12598 case INTR_TYPE_PRIV_SW_EXCEPTION:
12599 if ((vmcs12->vm_entry_instruction_len > 15) ||
12600 (vmcs12->vm_entry_instruction_len == 0 &&
12601 !nested_cpu_has_zero_length_injection(vcpu)))
12602 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12603 }
12604 }
12605
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012606 if (nested_cpu_has_ept(vmcs12) &&
12607 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12608 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12609
Jim Mattsonca0bde22016-11-30 12:03:46 -080012610 return 0;
12611}
12612
Liran Alonf145d902018-06-23 02:35:07 +030012613static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12614 struct vmcs12 *vmcs12)
12615{
12616 int r;
12617 struct page *page;
12618 struct vmcs12 *shadow;
12619
12620 if (vmcs12->vmcs_link_pointer == -1ull)
12621 return 0;
12622
12623 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12624 return -EINVAL;
12625
12626 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12627 if (is_error_page(page))
12628 return -EINVAL;
12629
12630 r = 0;
12631 shadow = kmap(page);
12632 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12633 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12634 r = -EINVAL;
12635 kunmap(page);
12636 kvm_release_page_clean(page);
12637 return r;
12638}
12639
Jim Mattsonca0bde22016-11-30 12:03:46 -080012640static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12641 u32 *exit_qual)
12642{
12643 bool ia32e;
12644
12645 *exit_qual = ENTRY_FAIL_DEFAULT;
12646
12647 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12648 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12649 return 1;
12650
Liran Alonf145d902018-06-23 02:35:07 +030012651 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012652 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12653 return 1;
12654 }
12655
12656 /*
12657 * If the load IA32_EFER VM-entry control is 1, the following checks
12658 * are performed on the field for the IA32_EFER MSR:
12659 * - Bits reserved in the IA32_EFER MSR must be 0.
12660 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12661 * the IA-32e mode guest VM-exit control. It must also be identical
12662 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12663 * CR0.PG) is 1.
12664 */
12665 if (to_vmx(vcpu)->nested.nested_run_pending &&
12666 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12667 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12668 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12669 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12670 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12671 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12672 return 1;
12673 }
12674
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012675 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12676 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12677 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12678 return 1;
12679
Jim Mattsonca0bde22016-11-30 12:03:46 -080012680 return 0;
12681}
12682
Sean Christophersona633e412018-09-26 09:23:47 -070012683static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12684 struct vmcs12 *vmcs12);
12685
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012686/*
Sean Christophersona633e412018-09-26 09:23:47 -070012687 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012688 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012689 */
Sean Christophersona633e412018-09-26 09:23:47 -070012690static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
12691 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080012692{
12693 struct vcpu_vmx *vmx = to_vmx(vcpu);
12694 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020012695 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070012696 u32 exit_reason = EXIT_REASON_INVALID_STATE;
12697 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080012698
Paolo Bonzini7e712682018-10-03 13:44:26 +020012699 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12700 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12701 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12702 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012703
Jim Mattson858e25c2016-11-30 12:03:47 -080012704 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12705 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012706 if (kvm_mpx_supported() &&
12707 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12708 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012709
Jim Mattsonde3a0022017-11-27 17:22:25 -060012710 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012711
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012712 prepare_vmcs02_early(vmx, vmcs12);
12713
12714 if (from_vmentry) {
12715 nested_get_vmcs12_pages(vcpu);
12716
12717 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12718 goto vmentry_fail_vmexit;
12719 }
12720
12721 enter_guest_mode(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012722 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12723 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12724
Sean Christophersona633e412018-09-26 09:23:47 -070012725 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070012726 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080012727
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012728 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070012729 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
12730 exit_qual = nested_vmx_load_msr(vcpu,
12731 vmcs12->vm_entry_msr_load_addr,
12732 vmcs12->vm_entry_msr_load_count);
12733 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070012734 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012735 } else {
12736 /*
12737 * The MMU is not initialized to point at the right entities yet and
12738 * "get pages" would need to read data from the guest (i.e. we will
12739 * need to perform gpa to hpa translation). Request a call
12740 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12741 * have already been set at vmentry time and should not be reset.
12742 */
12743 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12744 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012745
Jim Mattson858e25c2016-11-30 12:03:47 -080012746 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012747 * If L1 had a pending IRQ/NMI until it executed
12748 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12749 * disallowed (e.g. interrupts disabled), L0 needs to
12750 * evaluate if this pending event should cause an exit from L2
12751 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12752 * intercept EXTERNAL_INTERRUPT).
12753 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012754 * Usually this would be handled by the processor noticing an
12755 * IRQ/NMI window request, or checking RVI during evaluation of
12756 * pending virtual interrupts. However, this setting was done
12757 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12758 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012759 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012760 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012761 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012762
12763 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012764 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12765 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12766 * returned as far as L1 is concerned. It will only return (and set
12767 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12768 */
12769 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012770
Sean Christophersona633e412018-09-26 09:23:47 -070012771 /*
12772 * A failed consistency check that leads to a VMExit during L1's
12773 * VMEnter to L2 is a variation of a normal VMexit, as explained in
12774 * 26.7 "VM-entry failures during or after loading guest state".
12775 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070012776vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012777 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12778 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12779 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012780
12781vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012782 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070012783
12784 if (!from_vmentry)
12785 return 1;
12786
Sean Christophersona633e412018-09-26 09:23:47 -070012787 load_vmcs12_host_state(vcpu, vmcs12);
12788 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12789 vmcs12->exit_qualification = exit_qual;
12790 nested_vmx_succeed(vcpu);
12791 if (enable_shadow_vmcs)
12792 vmx->nested.sync_shadow_vmcs = true;
12793 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012794}
12795
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012796/*
12797 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12798 * for running an L2 nested guest.
12799 */
12800static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12801{
12802 struct vmcs12 *vmcs12;
12803 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012804 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012805 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012806
Kyle Hueyeb277562016-11-29 12:40:39 -080012807 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012808 return 1;
12809
Kyle Hueyeb277562016-11-29 12:40:39 -080012810 if (!nested_vmx_check_vmcs12(vcpu))
12811 goto out;
12812
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012813 vmcs12 = get_vmcs12(vcpu);
12814
Liran Alona6192d42018-06-23 02:35:04 +030012815 /*
12816 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12817 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12818 * rather than RFLAGS.ZF, and no error number is stored to the
12819 * VM-instruction error field.
12820 */
12821 if (vmcs12->hdr.shadow_vmcs) {
12822 nested_vmx_failInvalid(vcpu);
12823 goto out;
12824 }
12825
Abel Gordon012f83c2013-04-18 14:39:25 +030012826 if (enable_shadow_vmcs)
12827 copy_shadow_to_vmcs12(vmx);
12828
Nadav Har'El7c177932011-05-25 23:12:04 +030012829 /*
12830 * The nested entry process starts with enforcing various prerequisites
12831 * on vmcs12 as required by the Intel SDM, and act appropriately when
12832 * they fail: As the SDM explains, some conditions should cause the
12833 * instruction to fail, while others will cause the instruction to seem
12834 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12835 * To speed up the normal (success) code path, we should avoid checking
12836 * for misconfigurations which will anyway be caught by the processor
12837 * when using the merged vmcs02.
12838 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012839 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12840 nested_vmx_failValid(vcpu,
12841 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12842 goto out;
12843 }
12844
Nadav Har'El7c177932011-05-25 23:12:04 +030012845 if (vmcs12->launch_state == launch) {
12846 nested_vmx_failValid(vcpu,
12847 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12848 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012849 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012850 }
12851
Jim Mattsonca0bde22016-11-30 12:03:46 -080012852 ret = check_vmentry_prereqs(vcpu, vmcs12);
12853 if (ret) {
12854 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012855 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012856 }
12857
Nadav Har'El7c177932011-05-25 23:12:04 +030012858 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012859 * After this point, the trap flag no longer triggers a singlestep trap
12860 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12861 * This is not 100% correct; for performance reasons, we delegate most
12862 * of the checks on host state to the processor. If those fail,
12863 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012864 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012865 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012866
Jan Kiszka384bb782013-04-20 10:52:36 +020012867 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012868 * We're finally done with prerequisite checking, and can start with
12869 * the nested entry.
12870 */
12871
Jim Mattson6514dc32018-04-26 16:09:12 -070012872 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070012873 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Jim Mattson6514dc32018-04-26 16:09:12 -070012874 if (ret) {
12875 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012876 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012877 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012878
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012879 /* Hide L1D cache contents from the nested guest. */
12880 vmx->vcpu.arch.l1tf_flush_l1d = true;
12881
Chao Gao135a06c2018-02-11 10:06:30 +080012882 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012883 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012884 * also be used as part of restoring nVMX state for
12885 * snapshot restore (migration).
12886 *
12887 * In this flow, it is assumed that vmcs12 cache was
12888 * trasferred as part of captured nVMX state and should
12889 * therefore not be read from guest memory (which may not
12890 * exist on destination host yet).
12891 */
12892 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12893
12894 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012895 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12896 * by event injection, halt vcpu.
12897 */
12898 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012899 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12900 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012901 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012902 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012903 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012904
12905out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012906 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012907}
12908
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012909/*
12910 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12911 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12912 * This function returns the new value we should put in vmcs12.guest_cr0.
12913 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12914 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12915 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12916 * didn't trap the bit, because if L1 did, so would L0).
12917 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12918 * been modified by L2, and L1 knows it. So just leave the old value of
12919 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12920 * isn't relevant, because if L0 traps this bit it can set it to anything.
12921 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12922 * changed these bits, and therefore they need to be updated, but L0
12923 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12924 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12925 */
12926static inline unsigned long
12927vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12928{
12929 return
12930 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12931 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12932 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12933 vcpu->arch.cr0_guest_owned_bits));
12934}
12935
12936static inline unsigned long
12937vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12938{
12939 return
12940 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12941 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12942 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12943 vcpu->arch.cr4_guest_owned_bits));
12944}
12945
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012946static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12947 struct vmcs12 *vmcs12)
12948{
12949 u32 idt_vectoring;
12950 unsigned int nr;
12951
Wanpeng Li664f8e22017-08-24 03:35:09 -070012952 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012953 nr = vcpu->arch.exception.nr;
12954 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12955
12956 if (kvm_exception_is_soft(nr)) {
12957 vmcs12->vm_exit_instruction_len =
12958 vcpu->arch.event_exit_inst_len;
12959 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12960 } else
12961 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12962
12963 if (vcpu->arch.exception.has_error_code) {
12964 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12965 vmcs12->idt_vectoring_error_code =
12966 vcpu->arch.exception.error_code;
12967 }
12968
12969 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012970 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012971 vmcs12->idt_vectoring_info_field =
12972 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012973 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012974 nr = vcpu->arch.interrupt.nr;
12975 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12976
12977 if (vcpu->arch.interrupt.soft) {
12978 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12979 vmcs12->vm_entry_instruction_len =
12980 vcpu->arch.event_exit_inst_len;
12981 } else
12982 idt_vectoring |= INTR_TYPE_EXT_INTR;
12983
12984 vmcs12->idt_vectoring_info_field = idt_vectoring;
12985 }
12986}
12987
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012988static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12989{
12990 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012991 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012992 bool block_nested_events =
12993 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012994
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012995 if (vcpu->arch.exception.pending &&
12996 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012997 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012998 return -EBUSY;
12999 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013000 return 0;
13001 }
13002
Jan Kiszkaf4124502014-03-07 20:03:13 +010013003 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13004 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013005 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010013006 return -EBUSY;
13007 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13008 return 0;
13009 }
13010
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013011 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013012 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013013 return -EBUSY;
13014 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13015 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13016 INTR_INFO_VALID_MASK, 0);
13017 /*
13018 * The NMI-triggered VM exit counts as injection:
13019 * clear this one and block further NMIs.
13020 */
13021 vcpu->arch.nmi_pending = 0;
13022 vmx_set_nmi_mask(vcpu, true);
13023 return 0;
13024 }
13025
13026 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13027 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013028 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013029 return -EBUSY;
13030 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013031 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013032 }
13033
David Hildenbrand6342c502017-01-25 11:58:58 +010013034 vmx_complete_nested_posted_interrupt(vcpu);
13035 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013036}
13037
Sean Christophersond264ee02018-08-27 15:21:12 -070013038static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13039{
13040 to_vmx(vcpu)->req_immediate_exit = true;
13041}
13042
Jan Kiszkaf4124502014-03-07 20:03:13 +010013043static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13044{
13045 ktime_t remaining =
13046 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13047 u64 value;
13048
13049 if (ktime_to_ns(remaining) <= 0)
13050 return 0;
13051
13052 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13053 do_div(value, 1000000);
13054 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13055}
13056
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013057/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013058 * Update the guest state fields of vmcs12 to reflect changes that
13059 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13060 * VM-entry controls is also updated, since this is really a guest
13061 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013062 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013063static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013064{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013065 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13066 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13067
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013068 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13069 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13070 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13071
13072 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13073 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13074 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13075 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13076 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13077 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13078 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13079 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13080 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13081 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13082 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13083 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13084 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13085 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13086 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13087 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13088 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13089 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13090 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13091 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13092 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13093 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13094 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13095 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13096 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13097 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13098 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13099 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13100 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13101 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13102 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13103 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13104 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13105 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13106 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13107 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13108
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013109 vmcs12->guest_interruptibility_info =
13110 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13111 vmcs12->guest_pending_dbg_exceptions =
13112 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013113 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13114 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13115 else
13116 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013117
Jan Kiszkaf4124502014-03-07 20:03:13 +010013118 if (nested_cpu_has_preemption_timer(vmcs12)) {
13119 if (vmcs12->vm_exit_controls &
13120 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13121 vmcs12->vmx_preemption_timer_value =
13122 vmx_get_preemption_timer_value(vcpu);
13123 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13124 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013125
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013126 /*
13127 * In some cases (usually, nested EPT), L2 is allowed to change its
13128 * own CR3 without exiting. If it has changed it, we must keep it.
13129 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13130 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13131 *
13132 * Additionally, restore L2's PDPTR to vmcs12.
13133 */
13134 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013135 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013136 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13137 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13138 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13139 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13140 }
13141
Jim Mattsond281e132017-06-01 12:44:46 -070013142 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013143
Wincy Van608406e2015-02-03 23:57:51 +080013144 if (nested_cpu_has_vid(vmcs12))
13145 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13146
Jan Kiszkac18911a2013-03-13 16:06:41 +010013147 vmcs12->vm_entry_controls =
13148 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013149 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013150
Jan Kiszka2996fca2014-06-16 13:59:43 +020013151 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13152 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13153 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13154 }
13155
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013156 /* TODO: These cannot have changed unless we have MSR bitmaps and
13157 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013158 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013159 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013160 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13161 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013162 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13163 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13164 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013165 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013166 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013167}
13168
13169/*
13170 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13171 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13172 * and this function updates it to reflect the changes to the guest state while
13173 * L2 was running (and perhaps made some exits which were handled directly by L0
13174 * without going back to L1), and to reflect the exit reason.
13175 * Note that we do not have to copy here all VMCS fields, just those that
13176 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13177 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13178 * which already writes to vmcs12 directly.
13179 */
13180static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13181 u32 exit_reason, u32 exit_intr_info,
13182 unsigned long exit_qualification)
13183{
13184 /* update guest state fields: */
13185 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013186
13187 /* update exit information fields: */
13188
Jan Kiszka533558b2014-01-04 18:47:20 +010013189 vmcs12->vm_exit_reason = exit_reason;
13190 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013191 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013192
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013193 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013194 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13195 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13196
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013197 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013198 vmcs12->launch_state = 1;
13199
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013200 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13201 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013202 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013203
13204 /*
13205 * Transfer the event that L0 or L1 may wanted to inject into
13206 * L2 to IDT_VECTORING_INFO_FIELD.
13207 */
13208 vmcs12_save_pending_event(vcpu, vmcs12);
13209 }
13210
13211 /*
13212 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13213 * preserved above and would only end up incorrectly in L1.
13214 */
13215 vcpu->arch.nmi_injected = false;
13216 kvm_clear_exception_queue(vcpu);
13217 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013218}
13219
13220/*
13221 * A part of what we need to when the nested L2 guest exits and we want to
13222 * run its L1 parent, is to reset L1's guest state to the host state specified
13223 * in vmcs12.
13224 * This function is to be called not only on normal nested exit, but also on
13225 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13226 * Failures During or After Loading Guest State").
13227 * This function should be called when the active VMCS is L1's (vmcs01).
13228 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013229static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13230 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013231{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013232 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013233 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013234
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013235 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13236 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013237 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013238 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13239 else
13240 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13241 vmx_set_efer(vcpu, vcpu->arch.efer);
13242
13243 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13244 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013245 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013246 /*
13247 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013248 * actually changed, because vmx_set_cr0 refers to efer set above.
13249 *
13250 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13251 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013252 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013253 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013254 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013255
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013256 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013257 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013258 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013259
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013260 nested_ept_uninit_mmu_context(vcpu);
13261
13262 /*
13263 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13264 * couldn't have changed.
13265 */
13266 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13267 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13268
13269 if (!enable_ept)
13270 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013271
Liran Alon6f1e03b2018-05-22 17:16:14 +030013272 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013273 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013274 * VMEntry/VMExit. Thus, no need to flush TLB.
13275 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013276 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13277 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013278 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013279 * Otherwise, we can preserve TLB entries as long as we are
13280 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013281 *
13282 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13283 * and therefore we request the TLB flush to happen only after VMCS EPTP
13284 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013285 */
13286 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013287 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013288 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013289 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013290
13291 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13292 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13293 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13294 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13295 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013296 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13297 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013298
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013299 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13300 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13301 vmcs_write64(GUEST_BNDCFGS, 0);
13302
Jan Kiszka44811c02013-08-04 17:17:27 +020013303 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013304 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013305 vcpu->arch.pat = vmcs12->host_ia32_pat;
13306 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013307 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13308 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13309 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013310
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013311 /* Set L1 segment info according to Intel SDM
13312 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13313 seg = (struct kvm_segment) {
13314 .base = 0,
13315 .limit = 0xFFFFFFFF,
13316 .selector = vmcs12->host_cs_selector,
13317 .type = 11,
13318 .present = 1,
13319 .s = 1,
13320 .g = 1
13321 };
13322 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13323 seg.l = 1;
13324 else
13325 seg.db = 1;
13326 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13327 seg = (struct kvm_segment) {
13328 .base = 0,
13329 .limit = 0xFFFFFFFF,
13330 .type = 3,
13331 .present = 1,
13332 .s = 1,
13333 .db = 1,
13334 .g = 1
13335 };
13336 seg.selector = vmcs12->host_ds_selector;
13337 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13338 seg.selector = vmcs12->host_es_selector;
13339 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13340 seg.selector = vmcs12->host_ss_selector;
13341 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13342 seg.selector = vmcs12->host_fs_selector;
13343 seg.base = vmcs12->host_fs_base;
13344 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13345 seg.selector = vmcs12->host_gs_selector;
13346 seg.base = vmcs12->host_gs_base;
13347 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13348 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013349 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013350 .limit = 0x67,
13351 .selector = vmcs12->host_tr_selector,
13352 .type = 11,
13353 .present = 1
13354 };
13355 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13356
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013357 kvm_set_dr(vcpu, 7, 0x400);
13358 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013359
Wincy Van3af18d92015-02-03 23:49:31 +080013360 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013361 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013362
Wincy Vanff651cb2014-12-11 08:52:58 +030013363 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13364 vmcs12->vm_exit_msr_load_count))
13365 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013366}
13367
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013368static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13369{
13370 struct shared_msr_entry *efer_msr;
13371 unsigned int i;
13372
13373 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13374 return vmcs_read64(GUEST_IA32_EFER);
13375
13376 if (cpu_has_load_ia32_efer)
13377 return host_efer;
13378
13379 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13380 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13381 return vmx->msr_autoload.guest.val[i].value;
13382 }
13383
13384 efer_msr = find_msr_entry(vmx, MSR_EFER);
13385 if (efer_msr)
13386 return efer_msr->data;
13387
13388 return host_efer;
13389}
13390
13391static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13392{
13393 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13394 struct vcpu_vmx *vmx = to_vmx(vcpu);
13395 struct vmx_msr_entry g, h;
13396 struct msr_data msr;
13397 gpa_t gpa;
13398 u32 i, j;
13399
13400 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13401
13402 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13403 /*
13404 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13405 * as vmcs01.GUEST_DR7 contains a userspace defined value
13406 * and vcpu->arch.dr7 is not squirreled away before the
13407 * nested VMENTER (not worth adding a variable in nested_vmx).
13408 */
13409 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13410 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13411 else
13412 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13413 }
13414
13415 /*
13416 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13417 * handle a variety of side effects to KVM's software model.
13418 */
13419 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13420
13421 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13422 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13423
13424 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13425 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13426
13427 nested_ept_uninit_mmu_context(vcpu);
13428 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13429 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13430
13431 /*
13432 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13433 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13434 * VMFail, like everything else we just need to ensure our
13435 * software model is up-to-date.
13436 */
13437 ept_save_pdptrs(vcpu);
13438
13439 kvm_mmu_reset_context(vcpu);
13440
13441 if (cpu_has_vmx_msr_bitmap())
13442 vmx_update_msr_bitmap(vcpu);
13443
13444 /*
13445 * This nasty bit of open coding is a compromise between blindly
13446 * loading L1's MSRs using the exit load lists (incorrect emulation
13447 * of VMFail), leaving the nested VM's MSRs in the software model
13448 * (incorrect behavior) and snapshotting the modified MSRs (too
13449 * expensive since the lists are unbound by hardware). For each
13450 * MSR that was (prematurely) loaded from the nested VMEntry load
13451 * list, reload it from the exit load list if it exists and differs
13452 * from the guest value. The intent is to stuff host state as
13453 * silently as possible, not to fully process the exit load list.
13454 */
13455 msr.host_initiated = false;
13456 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13457 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13458 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13459 pr_debug_ratelimited(
13460 "%s read MSR index failed (%u, 0x%08llx)\n",
13461 __func__, i, gpa);
13462 goto vmabort;
13463 }
13464
13465 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13466 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13467 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13468 pr_debug_ratelimited(
13469 "%s read MSR failed (%u, 0x%08llx)\n",
13470 __func__, j, gpa);
13471 goto vmabort;
13472 }
13473 if (h.index != g.index)
13474 continue;
13475 if (h.value == g.value)
13476 break;
13477
13478 if (nested_vmx_load_msr_check(vcpu, &h)) {
13479 pr_debug_ratelimited(
13480 "%s check failed (%u, 0x%x, 0x%x)\n",
13481 __func__, j, h.index, h.reserved);
13482 goto vmabort;
13483 }
13484
13485 msr.index = h.index;
13486 msr.data = h.value;
13487 if (kvm_set_msr(vcpu, &msr)) {
13488 pr_debug_ratelimited(
13489 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13490 __func__, j, h.index, h.value);
13491 goto vmabort;
13492 }
13493 }
13494 }
13495
13496 return;
13497
13498vmabort:
13499 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13500}
13501
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013502/*
13503 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13504 * and modify vmcs12 to make it see what it would expect to see there if
13505 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13506 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013507static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13508 u32 exit_intr_info,
13509 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013510{
13511 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013512 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13513
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013514 /* trying to cancel vmlaunch/vmresume is a bug */
13515 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13516
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013517 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013518 * The only expected VM-instruction error is "VM entry with
13519 * invalid control field(s)." Anything else indicates a
13520 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013521 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013522 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13523 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13524
13525 leave_guest_mode(vcpu);
13526
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013527 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13528 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13529
Jim Mattson4f350c62017-09-14 16:31:44 -070013530 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013531 if (exit_reason == -1)
13532 sync_vmcs12(vcpu, vmcs12);
13533 else
13534 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13535 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013536
Liran Alon61ada742018-06-23 02:35:08 +030013537 /*
13538 * Must happen outside of sync_vmcs12() as it will
13539 * also be used to capture vmcs12 cache as part of
13540 * capturing nVMX state for snapshot (migration).
13541 *
13542 * Otherwise, this flush will dirty guest memory at a
13543 * point it is already assumed by user-space to be
13544 * immutable.
13545 */
13546 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13547
Jim Mattson4f350c62017-09-14 16:31:44 -070013548 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13549 vmcs12->vm_exit_msr_store_count))
13550 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013551 }
13552
Jim Mattson4f350c62017-09-14 16:31:44 -070013553 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013554
Paolo Bonzini93140062016-07-06 13:23:51 +020013555 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013556 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13557 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013558 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013559
Peter Feinerc95ba922016-08-17 09:36:47 -070013560 if (kvm_has_tsc_control)
13561 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013562
Jim Mattson8d860bb2018-05-09 16:56:05 -040013563 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13564 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13565 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013566 } else if (!nested_cpu_has_ept(vmcs12) &&
13567 nested_cpu_has2(vmcs12,
13568 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013569 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013570 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013571
13572 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13573 vmx->host_rsp = 0;
13574
13575 /* Unpin physical memory we referred to in vmcs02 */
13576 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013577 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013578 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013579 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013580 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013581 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013582 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013583 }
Wincy Van705699a2015-02-03 23:58:17 +080013584 if (vmx->nested.pi_desc_page) {
13585 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013586 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013587 vmx->nested.pi_desc_page = NULL;
13588 vmx->nested.pi_desc = NULL;
13589 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013590
13591 /*
Tang Chen38b99172014-09-24 15:57:54 +080013592 * We are now running in L2, mmu_notifier will force to reload the
13593 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13594 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013595 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013596
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013597 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013598 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013599
13600 /* in case we halted in L2 */
13601 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013602
13603 if (likely(!vmx->fail)) {
13604 /*
13605 * TODO: SDM says that with acknowledge interrupt on
13606 * exit, bit 31 of the VM-exit interrupt information
13607 * (valid interrupt) is always set to 1 on
13608 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13609 * need kvm_cpu_has_interrupt(). See the commit
13610 * message for details.
13611 */
13612 if (nested_exit_intr_ack_set(vcpu) &&
13613 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13614 kvm_cpu_has_interrupt(vcpu)) {
13615 int irq = kvm_cpu_get_interrupt(vcpu);
13616 WARN_ON(irq < 0);
13617 vmcs12->vm_exit_intr_info = irq |
13618 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13619 }
13620
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013621 if (exit_reason != -1)
13622 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13623 vmcs12->exit_qualification,
13624 vmcs12->idt_vectoring_info_field,
13625 vmcs12->vm_exit_intr_info,
13626 vmcs12->vm_exit_intr_error_code,
13627 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013628
13629 load_vmcs12_host_state(vcpu, vmcs12);
13630
13631 return;
13632 }
13633
13634 /*
13635 * After an early L2 VM-entry failure, we're now back
13636 * in L1 which thinks it just finished a VMLAUNCH or
13637 * VMRESUME instruction, so we need to set the failure
13638 * flag and the VM-instruction error field of the VMCS
13639 * accordingly.
13640 */
13641 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013642
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013643 /*
13644 * Restore L1's host state to KVM's software model. We're here
13645 * because a consistency check was caught by hardware, which
13646 * means some amount of guest state has been propagated to KVM's
13647 * model and needs to be unwound to the host's state.
13648 */
13649 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013650
Jim Mattson4f350c62017-09-14 16:31:44 -070013651 /*
13652 * The emulated instruction was already skipped in
13653 * nested_vmx_run, but the updated RIP was never
13654 * written back to the vmcs01.
13655 */
13656 skip_emulated_instruction(vcpu);
13657 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013658}
13659
Nadav Har'El7c177932011-05-25 23:12:04 +030013660/*
Jan Kiszka42124922014-01-04 18:47:19 +010013661 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13662 */
13663static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13664{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013665 if (is_guest_mode(vcpu)) {
13666 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013667 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013668 }
Jan Kiszka42124922014-01-04 18:47:19 +010013669 free_nested(to_vmx(vcpu));
13670}
13671
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013672static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13673 struct x86_instruction_info *info,
13674 enum x86_intercept_stage stage)
13675{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013676 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13677 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13678
13679 /*
13680 * RDPID causes #UD if disabled through secondary execution controls.
13681 * Because it is marked as EmulateOnUD, we need to intercept it here.
13682 */
13683 if (info->intercept == x86_intercept_rdtscp &&
13684 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13685 ctxt->exception.vector = UD_VECTOR;
13686 ctxt->exception.error_code_valid = false;
13687 return X86EMUL_PROPAGATE_FAULT;
13688 }
13689
13690 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013691 return X86EMUL_CONTINUE;
13692}
13693
Yunhong Jiang64672c92016-06-13 14:19:59 -070013694#ifdef CONFIG_X86_64
13695/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13696static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13697 u64 divisor, u64 *result)
13698{
13699 u64 low = a << shift, high = a >> (64 - shift);
13700
13701 /* To avoid the overflow on divq */
13702 if (high >= divisor)
13703 return 1;
13704
13705 /* Low hold the result, high hold rem which is discarded */
13706 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13707 "rm" (divisor), "0" (low), "1" (high));
13708 *result = low;
13709
13710 return 0;
13711}
13712
13713static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13714{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013715 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013716 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013717
13718 if (kvm_mwait_in_guest(vcpu->kvm))
13719 return -EOPNOTSUPP;
13720
13721 vmx = to_vmx(vcpu);
13722 tscl = rdtsc();
13723 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13724 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013725 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13726
13727 if (delta_tsc > lapic_timer_advance_cycles)
13728 delta_tsc -= lapic_timer_advance_cycles;
13729 else
13730 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013731
13732 /* Convert to host delta tsc if tsc scaling is enabled */
13733 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13734 u64_shl_div_u64(delta_tsc,
13735 kvm_tsc_scaling_ratio_frac_bits,
13736 vcpu->arch.tsc_scaling_ratio,
13737 &delta_tsc))
13738 return -ERANGE;
13739
13740 /*
13741 * If the delta tsc can't fit in the 32 bit after the multi shift,
13742 * we can't use the preemption timer.
13743 * It's possible that it fits on later vmentries, but checking
13744 * on every vmentry is costly so we just use an hrtimer.
13745 */
13746 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13747 return -ERANGE;
13748
13749 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013750 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013751}
13752
13753static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13754{
Sean Christophersonf459a702018-08-27 15:21:11 -070013755 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013756}
13757#endif
13758
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013759static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013760{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013761 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013762 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013763}
13764
Kai Huang843e4332015-01-28 10:54:28 +080013765static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13766 struct kvm_memory_slot *slot)
13767{
13768 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13769 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13770}
13771
13772static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13773 struct kvm_memory_slot *slot)
13774{
13775 kvm_mmu_slot_set_dirty(kvm, slot);
13776}
13777
13778static void vmx_flush_log_dirty(struct kvm *kvm)
13779{
13780 kvm_flush_pml_buffers(kvm);
13781}
13782
Bandan Dasc5f983f2017-05-05 15:25:14 -040013783static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13784{
13785 struct vmcs12 *vmcs12;
13786 struct vcpu_vmx *vmx = to_vmx(vcpu);
13787 gpa_t gpa;
13788 struct page *page = NULL;
13789 u64 *pml_address;
13790
13791 if (is_guest_mode(vcpu)) {
13792 WARN_ON_ONCE(vmx->nested.pml_full);
13793
13794 /*
13795 * Check if PML is enabled for the nested guest.
13796 * Whether eptp bit 6 is set is already checked
13797 * as part of A/D emulation.
13798 */
13799 vmcs12 = get_vmcs12(vcpu);
13800 if (!nested_cpu_has_pml(vmcs12))
13801 return 0;
13802
Dan Carpenter47698862017-05-10 22:43:17 +030013803 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013804 vmx->nested.pml_full = true;
13805 return 1;
13806 }
13807
13808 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13809
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013810 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13811 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013812 return 0;
13813
13814 pml_address = kmap(page);
13815 pml_address[vmcs12->guest_pml_index--] = gpa;
13816 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013817 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013818 }
13819
13820 return 0;
13821}
13822
Kai Huang843e4332015-01-28 10:54:28 +080013823static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13824 struct kvm_memory_slot *memslot,
13825 gfn_t offset, unsigned long mask)
13826{
13827 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13828}
13829
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013830static void __pi_post_block(struct kvm_vcpu *vcpu)
13831{
13832 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13833 struct pi_desc old, new;
13834 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013835
13836 do {
13837 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013838 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13839 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013840
13841 dest = cpu_physical_id(vcpu->cpu);
13842
13843 if (x2apic_enabled())
13844 new.ndst = dest;
13845 else
13846 new.ndst = (dest << 8) & 0xFF00;
13847
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013848 /* set 'NV' to 'notification vector' */
13849 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013850 } while (cmpxchg64(&pi_desc->control, old.control,
13851 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013852
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013853 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13854 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013855 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013856 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013857 vcpu->pre_pcpu = -1;
13858 }
13859}
13860
Feng Wuefc64402015-09-18 22:29:51 +080013861/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013862 * This routine does the following things for vCPU which is going
13863 * to be blocked if VT-d PI is enabled.
13864 * - Store the vCPU to the wakeup list, so when interrupts happen
13865 * we can find the right vCPU to wake up.
13866 * - Change the Posted-interrupt descriptor as below:
13867 * 'NDST' <-- vcpu->pre_pcpu
13868 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13869 * - If 'ON' is set during this process, which means at least one
13870 * interrupt is posted for this vCPU, we cannot block it, in
13871 * this case, return 1, otherwise, return 0.
13872 *
13873 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013874static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013875{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013876 unsigned int dest;
13877 struct pi_desc old, new;
13878 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13879
13880 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013881 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13882 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013883 return 0;
13884
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013885 WARN_ON(irqs_disabled());
13886 local_irq_disable();
13887 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13888 vcpu->pre_pcpu = vcpu->cpu;
13889 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13890 list_add_tail(&vcpu->blocked_vcpu_list,
13891 &per_cpu(blocked_vcpu_on_cpu,
13892 vcpu->pre_pcpu));
13893 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13894 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013895
13896 do {
13897 old.control = new.control = pi_desc->control;
13898
Feng Wubf9f6ac2015-09-18 22:29:55 +080013899 WARN((pi_desc->sn == 1),
13900 "Warning: SN field of posted-interrupts "
13901 "is set before blocking\n");
13902
13903 /*
13904 * Since vCPU can be preempted during this process,
13905 * vcpu->cpu could be different with pre_pcpu, we
13906 * need to set pre_pcpu as the destination of wakeup
13907 * notification event, then we can find the right vCPU
13908 * to wakeup in wakeup handler if interrupts happen
13909 * when the vCPU is in blocked state.
13910 */
13911 dest = cpu_physical_id(vcpu->pre_pcpu);
13912
13913 if (x2apic_enabled())
13914 new.ndst = dest;
13915 else
13916 new.ndst = (dest << 8) & 0xFF00;
13917
13918 /* set 'NV' to 'wakeup vector' */
13919 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013920 } while (cmpxchg64(&pi_desc->control, old.control,
13921 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013922
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013923 /* We should not block the vCPU if an interrupt is posted for it. */
13924 if (pi_test_on(pi_desc) == 1)
13925 __pi_post_block(vcpu);
13926
13927 local_irq_enable();
13928 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013929}
13930
Yunhong Jiangbc225122016-06-13 14:19:58 -070013931static int vmx_pre_block(struct kvm_vcpu *vcpu)
13932{
13933 if (pi_pre_block(vcpu))
13934 return 1;
13935
Yunhong Jiang64672c92016-06-13 14:19:59 -070013936 if (kvm_lapic_hv_timer_in_use(vcpu))
13937 kvm_lapic_switch_to_sw_timer(vcpu);
13938
Yunhong Jiangbc225122016-06-13 14:19:58 -070013939 return 0;
13940}
13941
13942static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013943{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013944 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013945 return;
13946
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013947 WARN_ON(irqs_disabled());
13948 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013949 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013950 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013951}
13952
Yunhong Jiangbc225122016-06-13 14:19:58 -070013953static void vmx_post_block(struct kvm_vcpu *vcpu)
13954{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013955 if (kvm_x86_ops->set_hv_timer)
13956 kvm_lapic_switch_to_hv_timer(vcpu);
13957
Yunhong Jiangbc225122016-06-13 14:19:58 -070013958 pi_post_block(vcpu);
13959}
13960
Feng Wubf9f6ac2015-09-18 22:29:55 +080013961/*
Feng Wuefc64402015-09-18 22:29:51 +080013962 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13963 *
13964 * @kvm: kvm
13965 * @host_irq: host irq of the interrupt
13966 * @guest_irq: gsi of the interrupt
13967 * @set: set or unset PI
13968 * returns 0 on success, < 0 on failure
13969 */
13970static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13971 uint32_t guest_irq, bool set)
13972{
13973 struct kvm_kernel_irq_routing_entry *e;
13974 struct kvm_irq_routing_table *irq_rt;
13975 struct kvm_lapic_irq irq;
13976 struct kvm_vcpu *vcpu;
13977 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013978 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013979
13980 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013981 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13982 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013983 return 0;
13984
13985 idx = srcu_read_lock(&kvm->irq_srcu);
13986 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013987 if (guest_irq >= irq_rt->nr_rt_entries ||
13988 hlist_empty(&irq_rt->map[guest_irq])) {
13989 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13990 guest_irq, irq_rt->nr_rt_entries);
13991 goto out;
13992 }
Feng Wuefc64402015-09-18 22:29:51 +080013993
13994 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13995 if (e->type != KVM_IRQ_ROUTING_MSI)
13996 continue;
13997 /*
13998 * VT-d PI cannot support posting multicast/broadcast
13999 * interrupts to a vCPU, we still use interrupt remapping
14000 * for these kind of interrupts.
14001 *
14002 * For lowest-priority interrupts, we only support
14003 * those with single CPU as the destination, e.g. user
14004 * configures the interrupts via /proc/irq or uses
14005 * irqbalance to make the interrupts single-CPU.
14006 *
14007 * We will support full lowest-priority interrupt later.
14008 */
14009
Radim Krčmář371313132016-07-12 22:09:27 +020014010 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014011 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14012 /*
14013 * Make sure the IRTE is in remapped mode if
14014 * we don't handle it in posted mode.
14015 */
14016 ret = irq_set_vcpu_affinity(host_irq, NULL);
14017 if (ret < 0) {
14018 printk(KERN_INFO
14019 "failed to back to remapped mode, irq: %u\n",
14020 host_irq);
14021 goto out;
14022 }
14023
Feng Wuefc64402015-09-18 22:29:51 +080014024 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014025 }
Feng Wuefc64402015-09-18 22:29:51 +080014026
14027 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14028 vcpu_info.vector = irq.vector;
14029
hu huajun2698d822018-04-11 15:16:40 +080014030 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014031 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14032
14033 if (set)
14034 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014035 else
Feng Wuefc64402015-09-18 22:29:51 +080014036 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014037
14038 if (ret < 0) {
14039 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14040 __func__);
14041 goto out;
14042 }
14043 }
14044
14045 ret = 0;
14046out:
14047 srcu_read_unlock(&kvm->irq_srcu, idx);
14048 return ret;
14049}
14050
Ashok Rajc45dcc72016-06-22 14:59:56 +080014051static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14052{
14053 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14054 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14055 FEATURE_CONTROL_LMCE;
14056 else
14057 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14058 ~FEATURE_CONTROL_LMCE;
14059}
14060
Ladi Prosek72d7b372017-10-11 16:54:41 +020014061static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14062{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014063 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14064 if (to_vmx(vcpu)->nested.nested_run_pending)
14065 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014066 return 1;
14067}
14068
Ladi Prosek0234bf82017-10-11 16:54:40 +020014069static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14070{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014071 struct vcpu_vmx *vmx = to_vmx(vcpu);
14072
14073 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14074 if (vmx->nested.smm.guest_mode)
14075 nested_vmx_vmexit(vcpu, -1, 0, 0);
14076
14077 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14078 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014079 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014080 return 0;
14081}
14082
14083static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14084{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014085 struct vcpu_vmx *vmx = to_vmx(vcpu);
14086 int ret;
14087
14088 if (vmx->nested.smm.vmxon) {
14089 vmx->nested.vmxon = true;
14090 vmx->nested.smm.vmxon = false;
14091 }
14092
14093 if (vmx->nested.smm.guest_mode) {
14094 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014095 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014096 vcpu->arch.hflags |= HF_SMM_MASK;
14097 if (ret)
14098 return ret;
14099
14100 vmx->nested.smm.guest_mode = false;
14101 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014102 return 0;
14103}
14104
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014105static int enable_smi_window(struct kvm_vcpu *vcpu)
14106{
14107 return 0;
14108}
14109
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014110static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14111 struct kvm_nested_state __user *user_kvm_nested_state,
14112 u32 user_data_size)
14113{
14114 struct vcpu_vmx *vmx;
14115 struct vmcs12 *vmcs12;
14116 struct kvm_nested_state kvm_state = {
14117 .flags = 0,
14118 .format = 0,
14119 .size = sizeof(kvm_state),
14120 .vmx.vmxon_pa = -1ull,
14121 .vmx.vmcs_pa = -1ull,
14122 };
14123
14124 if (!vcpu)
14125 return kvm_state.size + 2 * VMCS12_SIZE;
14126
14127 vmx = to_vmx(vcpu);
14128 vmcs12 = get_vmcs12(vcpu);
14129 if (nested_vmx_allowed(vcpu) &&
14130 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14131 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14132 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14133
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014134 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014135 kvm_state.size += VMCS12_SIZE;
14136
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014137 if (is_guest_mode(vcpu) &&
14138 nested_cpu_has_shadow_vmcs(vmcs12) &&
14139 vmcs12->vmcs_link_pointer != -1ull)
14140 kvm_state.size += VMCS12_SIZE;
14141 }
14142
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014143 if (vmx->nested.smm.vmxon)
14144 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14145
14146 if (vmx->nested.smm.guest_mode)
14147 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14148
14149 if (is_guest_mode(vcpu)) {
14150 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14151
14152 if (vmx->nested.nested_run_pending)
14153 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14154 }
14155 }
14156
14157 if (user_data_size < kvm_state.size)
14158 goto out;
14159
14160 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14161 return -EFAULT;
14162
14163 if (vmx->nested.current_vmptr == -1ull)
14164 goto out;
14165
14166 /*
14167 * When running L2, the authoritative vmcs12 state is in the
14168 * vmcs02. When running L1, the authoritative vmcs12 state is
14169 * in the shadow vmcs linked to vmcs01, unless
14170 * sync_shadow_vmcs is set, in which case, the authoritative
14171 * vmcs12 state is in the vmcs12 already.
14172 */
14173 if (is_guest_mode(vcpu))
14174 sync_vmcs12(vcpu, vmcs12);
14175 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14176 copy_shadow_to_vmcs12(vmx);
14177
14178 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14179 return -EFAULT;
14180
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014181 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14182 vmcs12->vmcs_link_pointer != -1ull) {
14183 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14184 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14185 return -EFAULT;
14186 }
14187
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014188out:
14189 return kvm_state.size;
14190}
14191
14192static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14193 struct kvm_nested_state __user *user_kvm_nested_state,
14194 struct kvm_nested_state *kvm_state)
14195{
14196 struct vcpu_vmx *vmx = to_vmx(vcpu);
14197 struct vmcs12 *vmcs12;
14198 u32 exit_qual;
14199 int ret;
14200
14201 if (kvm_state->format != 0)
14202 return -EINVAL;
14203
14204 if (!nested_vmx_allowed(vcpu))
14205 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14206
14207 if (kvm_state->vmx.vmxon_pa == -1ull) {
14208 if (kvm_state->vmx.smm.flags)
14209 return -EINVAL;
14210
14211 if (kvm_state->vmx.vmcs_pa != -1ull)
14212 return -EINVAL;
14213
14214 vmx_leave_nested(vcpu);
14215 return 0;
14216 }
14217
14218 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14219 return -EINVAL;
14220
14221 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14222 return -EINVAL;
14223
14224 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14225 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14226 return -EINVAL;
14227
14228 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14229 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14230 return -EINVAL;
14231
14232 if (kvm_state->vmx.smm.flags &
14233 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14234 return -EINVAL;
14235
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014236 /*
14237 * SMM temporarily disables VMX, so we cannot be in guest mode,
14238 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14239 * must be zero.
14240 */
14241 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14242 return -EINVAL;
14243
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014244 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14245 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14246 return -EINVAL;
14247
14248 vmx_leave_nested(vcpu);
14249 if (kvm_state->vmx.vmxon_pa == -1ull)
14250 return 0;
14251
14252 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14253 ret = enter_vmx_operation(vcpu);
14254 if (ret)
14255 return ret;
14256
14257 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14258
14259 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14260 vmx->nested.smm.vmxon = true;
14261 vmx->nested.vmxon = false;
14262
14263 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14264 vmx->nested.smm.guest_mode = true;
14265 }
14266
14267 vmcs12 = get_vmcs12(vcpu);
14268 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14269 return -EFAULT;
14270
Liran Alon392b2f22018-06-23 02:35:01 +030014271 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014272 return -EINVAL;
14273
14274 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14275 return 0;
14276
14277 vmx->nested.nested_run_pending =
14278 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14279
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014280 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14281 vmcs12->vmcs_link_pointer != -1ull) {
14282 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14283 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14284 return -EINVAL;
14285
14286 if (copy_from_user(shadow_vmcs12,
14287 user_kvm_nested_state->data + VMCS12_SIZE,
14288 sizeof(*vmcs12)))
14289 return -EFAULT;
14290
14291 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14292 !shadow_vmcs12->hdr.shadow_vmcs)
14293 return -EINVAL;
14294 }
14295
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014296 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14297 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14298 return -EINVAL;
14299
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014300 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014301 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014302 if (ret)
14303 return -EINVAL;
14304
14305 return 0;
14306}
14307
Kees Cook404f6aa2016-08-08 16:29:06 -070014308static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014309 .cpu_has_kvm_support = cpu_has_kvm_support,
14310 .disabled_by_bios = vmx_disabled_by_bios,
14311 .hardware_setup = hardware_setup,
14312 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014313 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014314 .hardware_enable = hardware_enable,
14315 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014316 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014317 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014318
Wanpeng Lib31c1142018-03-12 04:53:04 -070014319 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014320 .vm_alloc = vmx_vm_alloc,
14321 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014322
Avi Kivity6aa8b732006-12-10 02:21:36 -080014323 .vcpu_create = vmx_create_vcpu,
14324 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014325 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014326
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014327 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014328 .vcpu_load = vmx_vcpu_load,
14329 .vcpu_put = vmx_vcpu_put,
14330
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014331 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014332 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014333 .get_msr = vmx_get_msr,
14334 .set_msr = vmx_set_msr,
14335 .get_segment_base = vmx_get_segment_base,
14336 .get_segment = vmx_get_segment,
14337 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014338 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014339 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014340 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014341 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014342 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014343 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014344 .set_cr3 = vmx_set_cr3,
14345 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014346 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014347 .get_idt = vmx_get_idt,
14348 .set_idt = vmx_set_idt,
14349 .get_gdt = vmx_get_gdt,
14350 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014351 .get_dr6 = vmx_get_dr6,
14352 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014353 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014354 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014355 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014356 .get_rflags = vmx_get_rflags,
14357 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014358
Avi Kivity6aa8b732006-12-10 02:21:36 -080014359 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014360 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014361
Avi Kivity6aa8b732006-12-10 02:21:36 -080014362 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014363 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014364 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014365 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14366 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014367 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014368 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014369 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014370 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014371 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014372 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014373 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014374 .get_nmi_mask = vmx_get_nmi_mask,
14375 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014376 .enable_nmi_window = enable_nmi_window,
14377 .enable_irq_window = enable_irq_window,
14378 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014379 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014380 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014381 .get_enable_apicv = vmx_get_enable_apicv,
14382 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014383 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014384 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014385 .hwapic_irr_update = vmx_hwapic_irr_update,
14386 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014387 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014388 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14389 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014390
Izik Eiduscbc94022007-10-25 00:29:55 +020014391 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014392 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014393 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014394 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014395
Avi Kivity586f9602010-11-18 13:09:54 +020014396 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014397
Sheng Yang17cc3932010-01-05 19:02:27 +080014398 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014399
14400 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014401
14402 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014403 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014404
14405 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014406
14407 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014408
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014409 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014410 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014411
14412 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014413
14414 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014415 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014416 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014417 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014418 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014419
14420 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014421 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014422
14423 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014424
14425 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14426 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14427 .flush_log_dirty = vmx_flush_log_dirty,
14428 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014429 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014430
Feng Wubf9f6ac2015-09-18 22:29:55 +080014431 .pre_block = vmx_pre_block,
14432 .post_block = vmx_post_block,
14433
Wei Huang25462f72015-06-19 15:45:05 +020014434 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014435
14436 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014437
14438#ifdef CONFIG_X86_64
14439 .set_hv_timer = vmx_set_hv_timer,
14440 .cancel_hv_timer = vmx_cancel_hv_timer,
14441#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014442
14443 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014444
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014445 .get_nested_state = vmx_get_nested_state,
14446 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014447 .get_vmcs12_pages = nested_get_vmcs12_pages,
14448
Ladi Prosek72d7b372017-10-11 16:54:41 +020014449 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014450 .pre_enter_smm = vmx_pre_enter_smm,
14451 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014452 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014453};
14454
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014455static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014456{
14457 if (vmx_l1d_flush_pages) {
14458 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14459 vmx_l1d_flush_pages = NULL;
14460 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014461 /* Restore state so sysfs ignores VMX */
14462 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014463}
14464
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014465static void vmx_exit(void)
14466{
14467#ifdef CONFIG_KEXEC_CORE
14468 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14469 synchronize_rcu();
14470#endif
14471
14472 kvm_exit();
14473
14474#if IS_ENABLED(CONFIG_HYPERV)
14475 if (static_branch_unlikely(&enable_evmcs)) {
14476 int cpu;
14477 struct hv_vp_assist_page *vp_ap;
14478 /*
14479 * Reset everything to support using non-enlightened VMCS
14480 * access later (e.g. when we reload the module with
14481 * enlightened_vmcs=0)
14482 */
14483 for_each_online_cpu(cpu) {
14484 vp_ap = hv_get_vp_assist_page(cpu);
14485
14486 if (!vp_ap)
14487 continue;
14488
14489 vp_ap->current_nested_vmcs = 0;
14490 vp_ap->enlighten_vmentry = 0;
14491 }
14492
14493 static_branch_disable(&enable_evmcs);
14494 }
14495#endif
14496 vmx_cleanup_l1d_flush();
14497}
14498module_exit(vmx_exit);
14499
Avi Kivity6aa8b732006-12-10 02:21:36 -080014500static int __init vmx_init(void)
14501{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014502 int r;
14503
14504#if IS_ENABLED(CONFIG_HYPERV)
14505 /*
14506 * Enlightened VMCS usage should be recommended and the host needs
14507 * to support eVMCS v1 or above. We can also disable eVMCS support
14508 * with module parameter.
14509 */
14510 if (enlightened_vmcs &&
14511 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14512 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14513 KVM_EVMCS_VERSION) {
14514 int cpu;
14515
14516 /* Check that we have assist pages on all online CPUs */
14517 for_each_online_cpu(cpu) {
14518 if (!hv_get_vp_assist_page(cpu)) {
14519 enlightened_vmcs = false;
14520 break;
14521 }
14522 }
14523
14524 if (enlightened_vmcs) {
14525 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14526 static_branch_enable(&enable_evmcs);
14527 }
14528 } else {
14529 enlightened_vmcs = false;
14530 }
14531#endif
14532
14533 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014534 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014535 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014536 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014537
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014538 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014539 * Must be called after kvm_init() so enable_ept is properly set
14540 * up. Hand the parameter mitigation value in which was stored in
14541 * the pre module init parser. If no parameter was given, it will
14542 * contain 'auto' which will be turned into the default 'cond'
14543 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014544 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014545 if (boot_cpu_has(X86_BUG_L1TF)) {
14546 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14547 if (r) {
14548 vmx_exit();
14549 return r;
14550 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014551 }
14552
Dave Young2965faa2015-09-09 15:38:55 -070014553#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014554 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14555 crash_vmclear_local_loaded_vmcss);
14556#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014557 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014558
He, Qingfdef3ad2007-04-30 09:45:24 +030014559 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014560}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014561module_init(vmx_init);