blob: 8efce8e0a468d307d68e3e9a07bbf7e3dfe3dd84 [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
Jim Mattson8d860bb2018-05-09 16:56:05 -0400836 bool change_vmcs01_virtual_apic_mode;
837
Nadav Har'El644d7112011-05-25 23:12:35 +0300838 /* L2 must run next, and mustn't decide to exit to L1. */
839 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600840
841 struct loaded_vmcs vmcs02;
842
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300843 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600844 * Guest pages referred to in the vmcs02 with host-physical
845 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300846 */
847 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800848 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800849 struct page *pi_desc_page;
850 struct pi_desc *pi_desc;
851 bool pi_pending;
852 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100853
854 struct hrtimer preemption_timer;
855 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200856
857 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
858 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300859 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800860
Wanpeng Li5c614b32015-10-13 09:18:36 -0700861 u16 vpid02;
862 u16 last_vpid;
863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100864 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200865
866 /* SMM related state */
867 struct {
868 /* in VMX operation on SMM entry? */
869 bool vmxon;
870 /* in guest mode on SMM entry? */
871 bool guest_mode;
872 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300873};
874
Yang Zhang01e439b2013-04-11 19:25:12 +0800875#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800876#define POSTED_INTR_SN 1
877
Yang Zhang01e439b2013-04-11 19:25:12 +0800878/* Posted-Interrupt Descriptor */
879struct pi_desc {
880 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800881 union {
882 struct {
883 /* bit 256 - Outstanding Notification */
884 u16 on : 1,
885 /* bit 257 - Suppress Notification */
886 sn : 1,
887 /* bit 271:258 - Reserved */
888 rsvd_1 : 14;
889 /* bit 279:272 - Notification Vector */
890 u8 nv;
891 /* bit 287:280 - Reserved */
892 u8 rsvd_2;
893 /* bit 319:288 - Notification Destination */
894 u32 ndst;
895 };
896 u64 control;
897 };
898 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800899} __aligned(64);
900
Yang Zhanga20ed542013-04-11 19:25:15 +0800901static bool pi_test_and_set_on(struct pi_desc *pi_desc)
902{
903 return test_and_set_bit(POSTED_INTR_ON,
904 (unsigned long *)&pi_desc->control);
905}
906
907static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
908{
909 return test_and_clear_bit(POSTED_INTR_ON,
910 (unsigned long *)&pi_desc->control);
911}
912
913static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
914{
915 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
916}
917
Feng Wuebbfc762015-09-18 22:29:46 +0800918static inline void pi_clear_sn(struct pi_desc *pi_desc)
919{
920 return clear_bit(POSTED_INTR_SN,
921 (unsigned long *)&pi_desc->control);
922}
923
924static inline void pi_set_sn(struct pi_desc *pi_desc)
925{
926 return set_bit(POSTED_INTR_SN,
927 (unsigned long *)&pi_desc->control);
928}
929
Paolo Bonziniad361092016-09-20 16:15:05 +0200930static inline void pi_clear_on(struct pi_desc *pi_desc)
931{
932 clear_bit(POSTED_INTR_ON,
933 (unsigned long *)&pi_desc->control);
934}
935
Feng Wuebbfc762015-09-18 22:29:46 +0800936static inline int pi_test_on(struct pi_desc *pi_desc)
937{
938 return test_bit(POSTED_INTR_ON,
939 (unsigned long *)&pi_desc->control);
940}
941
942static inline int pi_test_sn(struct pi_desc *pi_desc)
943{
944 return test_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400948struct vmx_msrs {
949 unsigned int nr;
950 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
951};
952
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400953struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000954 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300955 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300956 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100957 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300958 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200959 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200960 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300961 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400962 int nmsrs;
963 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800964 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400965#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300966 u64 msr_host_kernel_gs_base;
967 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100969
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100970 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100971 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100972
Gleb Natapov2961e8762013-11-25 15:37:13 +0200973 u32 vm_entry_controls_shadow;
974 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200975 u32 secondary_exec_control;
976
Nadav Har'Eld462b812011-05-24 15:26:10 +0300977 /*
978 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
979 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700980 * guest (L2), it points to a different VMCS. loaded_cpu_state points
981 * to the VMCS whose state is loaded into the CPU registers that only
982 * need to be switched when transitioning to/from the kernel; a NULL
983 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300984 */
985 struct loaded_vmcs vmcs01;
986 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700987 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300988 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300989 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400990 struct vmx_msrs guest;
991 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300992 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700993
Avi Kivity9c8cba32007-11-22 11:42:59 +0200994 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300995 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300996 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300997 struct kvm_segment segs[8];
998 } rmode;
999 struct {
1000 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001001 struct kvm_save_segment {
1002 u16 selector;
1003 unsigned long base;
1004 u32 limit;
1005 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001006 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001007 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001008 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001009 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001010
Andi Kleena0861c02009-06-08 17:37:09 +08001011 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001012
Yang Zhang01e439b2013-04-11 19:25:12 +08001013 /* Posted interrupt descriptor */
1014 struct pi_desc pi_desc;
1015
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001016 /* Support for a guest hypervisor (nested VMX) */
1017 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001018
1019 /* Dynamic PLE window. */
1020 int ple_window;
1021 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001022
Sean Christophersond264ee02018-08-27 15:21:12 -07001023 bool req_immediate_exit;
1024
Kai Huang843e4332015-01-28 10:54:28 +08001025 /* Support for PML */
1026#define PML_ENTITY_NUM 512
1027 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001028
Yunhong Jiang64672c92016-06-13 14:19:59 -07001029 /* apic deadline value in host tsc */
1030 u64 hv_deadline_tsc;
1031
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001032 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001033
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001034 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001035
Wanpeng Li74c55932017-11-29 01:31:20 -08001036 unsigned long host_debugctlmsr;
1037
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001038 /*
1039 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1040 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1041 * in msr_ia32_feature_control_valid_bits.
1042 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001043 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001044 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001045 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001046};
1047
Avi Kivity2fb92db2011-04-27 19:42:18 +03001048enum segment_cache_field {
1049 SEG_FIELD_SEL = 0,
1050 SEG_FIELD_BASE = 1,
1051 SEG_FIELD_LIMIT = 2,
1052 SEG_FIELD_AR = 3,
1053
1054 SEG_FIELD_NR = 4
1055};
1056
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001057static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1058{
1059 return container_of(kvm, struct kvm_vmx, kvm);
1060}
1061
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1063{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001064 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065}
1066
Feng Wuefc64402015-09-18 22:29:51 +08001067static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1068{
1069 return &(to_vmx(vcpu)->pi_desc);
1070}
1071
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001072#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001073#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001074#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1075#define FIELD64(number, name) \
1076 FIELD(number, name), \
1077 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078
Abel Gordon4607c2d2013-04-18 14:35:55 +03001079
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001080static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001081#define SHADOW_FIELD_RO(x) x,
1082#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001083};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001084static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001085 ARRAY_SIZE(shadow_read_only_fields);
1086
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001087static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001088#define SHADOW_FIELD_RW(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_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001092 ARRAY_SIZE(shadow_read_write_fields);
1093
Mathias Krause772e0312012-08-30 01:30:19 +02001094static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001095 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001096 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001097 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1098 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1099 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1100 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1101 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1102 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1103 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1104 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001105 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001106 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001107 FIELD(HOST_ES_SELECTOR, host_es_selector),
1108 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1109 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1110 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1111 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1112 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1113 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1114 FIELD64(IO_BITMAP_A, io_bitmap_a),
1115 FIELD64(IO_BITMAP_B, io_bitmap_b),
1116 FIELD64(MSR_BITMAP, msr_bitmap),
1117 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1118 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1119 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001120 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001121 FIELD64(TSC_OFFSET, tsc_offset),
1122 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1123 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001124 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001125 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001126 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001127 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1128 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1129 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1130 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001131 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001132 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1133 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001134 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001135 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1136 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1137 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1138 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1139 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1140 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1141 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1142 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1143 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1144 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001145 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1147 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1148 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1149 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1150 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1151 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1152 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1153 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1154 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1155 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1156 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1157 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1158 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1159 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1160 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1161 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1162 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1163 FIELD(TPR_THRESHOLD, tpr_threshold),
1164 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1165 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1166 FIELD(VM_EXIT_REASON, vm_exit_reason),
1167 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1168 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1169 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1170 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1171 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1172 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1173 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1174 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1175 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1176 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1177 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1178 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1179 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1180 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1181 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1182 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1183 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1184 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1185 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1186 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1187 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1188 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1189 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1190 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1191 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1192 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1193 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1194 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001195 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001196 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1197 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1198 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1199 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1200 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1201 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1202 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1203 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1204 FIELD(EXIT_QUALIFICATION, exit_qualification),
1205 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1206 FIELD(GUEST_CR0, guest_cr0),
1207 FIELD(GUEST_CR3, guest_cr3),
1208 FIELD(GUEST_CR4, guest_cr4),
1209 FIELD(GUEST_ES_BASE, guest_es_base),
1210 FIELD(GUEST_CS_BASE, guest_cs_base),
1211 FIELD(GUEST_SS_BASE, guest_ss_base),
1212 FIELD(GUEST_DS_BASE, guest_ds_base),
1213 FIELD(GUEST_FS_BASE, guest_fs_base),
1214 FIELD(GUEST_GS_BASE, guest_gs_base),
1215 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1216 FIELD(GUEST_TR_BASE, guest_tr_base),
1217 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1218 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1219 FIELD(GUEST_DR7, guest_dr7),
1220 FIELD(GUEST_RSP, guest_rsp),
1221 FIELD(GUEST_RIP, guest_rip),
1222 FIELD(GUEST_RFLAGS, guest_rflags),
1223 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1224 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1225 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1226 FIELD(HOST_CR0, host_cr0),
1227 FIELD(HOST_CR3, host_cr3),
1228 FIELD(HOST_CR4, host_cr4),
1229 FIELD(HOST_FS_BASE, host_fs_base),
1230 FIELD(HOST_GS_BASE, host_gs_base),
1231 FIELD(HOST_TR_BASE, host_tr_base),
1232 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1233 FIELD(HOST_IDTR_BASE, host_idtr_base),
1234 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1235 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1236 FIELD(HOST_RSP, host_rsp),
1237 FIELD(HOST_RIP, host_rip),
1238};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001239
1240static inline short vmcs_field_to_offset(unsigned long field)
1241{
Dan Williams085331d2018-01-31 17:47:03 -08001242 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1243 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001244 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001245
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001246 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001247 return -ENOENT;
1248
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001249 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001250 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001251 return -ENOENT;
1252
Linus Torvalds15303ba2018-02-10 13:16:35 -08001253 index = array_index_nospec(index, size);
1254 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001255 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001256 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001257 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001258}
1259
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001260static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1261{
David Matlack4f2777b2016-07-13 17:16:37 -07001262 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001263}
1264
Liran Alon61ada742018-06-23 02:35:08 +03001265static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1266{
1267 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1268}
1269
Peter Feiner995f00a2017-06-30 17:26:32 -07001270static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001271static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001272static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001273static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001274static void vmx_set_segment(struct kvm_vcpu *vcpu,
1275 struct kvm_segment *var, int seg);
1276static void vmx_get_segment(struct kvm_vcpu *vcpu,
1277 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001278static bool guest_state_valid(struct kvm_vcpu *vcpu);
1279static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001280static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001281static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1282static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1283static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1284 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001285static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001286static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1287 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001288
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1290static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001291/*
1292 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1293 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1294 */
1295static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Feng Wubf9f6ac2015-09-18 22:29:55 +08001297/*
1298 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1299 * can find which vCPU should be waken up.
1300 */
1301static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1302static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1303
Radim Krčmář23611332016-09-29 22:41:33 +02001304enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001305 VMX_VMREAD_BITMAP,
1306 VMX_VMWRITE_BITMAP,
1307 VMX_BITMAP_NR
1308};
1309
1310static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1311
Radim Krčmář23611332016-09-29 22:41:33 +02001312#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1313#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001314
Avi Kivity110312c2010-12-21 12:54:20 +02001315static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001316static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001317
Sheng Yang2384d2b2008-01-17 15:14:33 +08001318static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1319static DEFINE_SPINLOCK(vmx_vpid_lock);
1320
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 int size;
1323 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001324 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001326 u32 pin_based_exec_ctrl;
1327 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001328 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329 u32 vmexit_ctrl;
1330 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001331 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
Hannes Ederefff9e52008-11-28 17:02:06 +01001334static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001335 u32 ept;
1336 u32 vpid;
1337} vmx_capability;
1338
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339#define VMX_SEGMENT_FIELD(seg) \
1340 [VCPU_SREG_##seg] = { \
1341 .selector = GUEST_##seg##_SELECTOR, \
1342 .base = GUEST_##seg##_BASE, \
1343 .limit = GUEST_##seg##_LIMIT, \
1344 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1345 }
1346
Mathias Krause772e0312012-08-30 01:30:19 +02001347static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 unsigned selector;
1349 unsigned base;
1350 unsigned limit;
1351 unsigned ar_bytes;
1352} kvm_vmx_segment_fields[] = {
1353 VMX_SEGMENT_FIELD(CS),
1354 VMX_SEGMENT_FIELD(DS),
1355 VMX_SEGMENT_FIELD(ES),
1356 VMX_SEGMENT_FIELD(FS),
1357 VMX_SEGMENT_FIELD(GS),
1358 VMX_SEGMENT_FIELD(SS),
1359 VMX_SEGMENT_FIELD(TR),
1360 VMX_SEGMENT_FIELD(LDTR),
1361};
1362
Avi Kivity26bb0982009-09-07 11:14:12 +03001363static u64 host_efer;
1364
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001365static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1366
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001367/*
Brian Gerst8c065852010-07-17 09:03:26 -04001368 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001369 * away by decrementing the array size.
1370 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001372#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001373 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001375 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001378DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1379
1380#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1381
1382#define KVM_EVMCS_VERSION 1
1383
1384#if IS_ENABLED(CONFIG_HYPERV)
1385static bool __read_mostly enlightened_vmcs = true;
1386module_param(enlightened_vmcs, bool, 0444);
1387
1388static inline void evmcs_write64(unsigned long field, u64 value)
1389{
1390 u16 clean_field;
1391 int offset = get_evmcs_offset(field, &clean_field);
1392
1393 if (offset < 0)
1394 return;
1395
1396 *(u64 *)((char *)current_evmcs + offset) = value;
1397
1398 current_evmcs->hv_clean_fields &= ~clean_field;
1399}
1400
1401static inline void evmcs_write32(unsigned long field, u32 value)
1402{
1403 u16 clean_field;
1404 int offset = get_evmcs_offset(field, &clean_field);
1405
1406 if (offset < 0)
1407 return;
1408
1409 *(u32 *)((char *)current_evmcs + offset) = value;
1410 current_evmcs->hv_clean_fields &= ~clean_field;
1411}
1412
1413static inline void evmcs_write16(unsigned long field, u16 value)
1414{
1415 u16 clean_field;
1416 int offset = get_evmcs_offset(field, &clean_field);
1417
1418 if (offset < 0)
1419 return;
1420
1421 *(u16 *)((char *)current_evmcs + offset) = value;
1422 current_evmcs->hv_clean_fields &= ~clean_field;
1423}
1424
1425static inline u64 evmcs_read64(unsigned long field)
1426{
1427 int offset = get_evmcs_offset(field, NULL);
1428
1429 if (offset < 0)
1430 return 0;
1431
1432 return *(u64 *)((char *)current_evmcs + offset);
1433}
1434
1435static inline u32 evmcs_read32(unsigned long field)
1436{
1437 int offset = get_evmcs_offset(field, NULL);
1438
1439 if (offset < 0)
1440 return 0;
1441
1442 return *(u32 *)((char *)current_evmcs + offset);
1443}
1444
1445static inline u16 evmcs_read16(unsigned long field)
1446{
1447 int offset = get_evmcs_offset(field, NULL);
1448
1449 if (offset < 0)
1450 return 0;
1451
1452 return *(u16 *)((char *)current_evmcs + offset);
1453}
1454
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001455static inline void evmcs_touch_msr_bitmap(void)
1456{
1457 if (unlikely(!current_evmcs))
1458 return;
1459
1460 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1461 current_evmcs->hv_clean_fields &=
1462 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1463}
1464
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001465static void evmcs_load(u64 phys_addr)
1466{
1467 struct hv_vp_assist_page *vp_ap =
1468 hv_get_vp_assist_page(smp_processor_id());
1469
1470 vp_ap->current_nested_vmcs = phys_addr;
1471 vp_ap->enlighten_vmentry = 1;
1472}
1473
1474static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1475{
1476 /*
1477 * Enlightened VMCSv1 doesn't support these:
1478 *
1479 * POSTED_INTR_NV = 0x00000002,
1480 * GUEST_INTR_STATUS = 0x00000810,
1481 * APIC_ACCESS_ADDR = 0x00002014,
1482 * POSTED_INTR_DESC_ADDR = 0x00002016,
1483 * EOI_EXIT_BITMAP0 = 0x0000201c,
1484 * EOI_EXIT_BITMAP1 = 0x0000201e,
1485 * EOI_EXIT_BITMAP2 = 0x00002020,
1486 * EOI_EXIT_BITMAP3 = 0x00002022,
1487 */
1488 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1489 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1490 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1491 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1492 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1493 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1494 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1495
1496 /*
1497 * GUEST_PML_INDEX = 0x00000812,
1498 * PML_ADDRESS = 0x0000200e,
1499 */
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1501
1502 /* VM_FUNCTION_CONTROL = 0x00002018, */
1503 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1504
1505 /*
1506 * EPTP_LIST_ADDRESS = 0x00002024,
1507 * VMREAD_BITMAP = 0x00002026,
1508 * VMWRITE_BITMAP = 0x00002028,
1509 */
1510 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1511
1512 /*
1513 * TSC_MULTIPLIER = 0x00002032,
1514 */
1515 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1516
1517 /*
1518 * PLE_GAP = 0x00004020,
1519 * PLE_WINDOW = 0x00004022,
1520 */
1521 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1522
1523 /*
1524 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1525 */
1526 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1527
1528 /*
1529 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1530 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1531 */
1532 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1533 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1534
1535 /*
1536 * Currently unsupported in KVM:
1537 * GUEST_IA32_RTIT_CTL = 0x00002814,
1538 */
1539}
Tianyu Lan877ad952018-07-19 08:40:23 +00001540
1541/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1542static void check_ept_pointer_match(struct kvm *kvm)
1543{
1544 struct kvm_vcpu *vcpu;
1545 u64 tmp_eptp = INVALID_PAGE;
1546 int i;
1547
1548 kvm_for_each_vcpu(i, vcpu, kvm) {
1549 if (!VALID_PAGE(tmp_eptp)) {
1550 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1551 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1552 to_kvm_vmx(kvm)->ept_pointers_match
1553 = EPT_POINTERS_MISMATCH;
1554 return;
1555 }
1556 }
1557
1558 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1559}
1560
1561static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1562{
1563 int ret;
1564
1565 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1566
1567 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1568 check_ept_pointer_match(kvm);
1569
1570 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1571 ret = -ENOTSUPP;
1572 goto out;
1573 }
1574
1575 ret = hyperv_flush_guest_mapping(
1576 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1577
1578out:
1579 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1580 return ret;
1581}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001582#else /* !IS_ENABLED(CONFIG_HYPERV) */
1583static inline void evmcs_write64(unsigned long field, u64 value) {}
1584static inline void evmcs_write32(unsigned long field, u32 value) {}
1585static inline void evmcs_write16(unsigned long field, u16 value) {}
1586static inline u64 evmcs_read64(unsigned long field) { return 0; }
1587static inline u32 evmcs_read32(unsigned long field) { return 0; }
1588static inline u16 evmcs_read16(unsigned long field) { return 0; }
1589static inline void evmcs_load(u64 phys_addr) {}
1590static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001591static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001592#endif /* IS_ENABLED(CONFIG_HYPERV) */
1593
Jan Kiszka5bb16012016-02-09 20:14:21 +01001594static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
1596 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1597 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001598 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1599}
1600
Jan Kiszka6f054852016-02-09 20:15:18 +01001601static inline bool is_debug(u32 intr_info)
1602{
1603 return is_exception_n(intr_info, DB_VECTOR);
1604}
1605
1606static inline bool is_breakpoint(u32 intr_info)
1607{
1608 return is_exception_n(intr_info, BP_VECTOR);
1609}
1610
Jan Kiszka5bb16012016-02-09 20:14:21 +01001611static inline bool is_page_fault(u32 intr_info)
1612{
1613 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001617{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001618 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001619}
1620
Gui Jianfeng31299942010-03-15 17:29:09 +08001621static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001622{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001623 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001624}
1625
Liran Alon9e869482018-03-12 13:12:51 +02001626static inline bool is_gp_fault(u32 intr_info)
1627{
1628 return is_exception_n(intr_info, GP_VECTOR);
1629}
1630
Gui Jianfeng31299942010-03-15 17:29:09 +08001631static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632{
1633 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1634 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1635}
1636
Gui Jianfeng31299942010-03-15 17:29:09 +08001637static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001638{
1639 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1640 INTR_INFO_VALID_MASK)) ==
1641 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1642}
1643
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001644/* Undocumented: icebp/int1 */
1645static inline bool is_icebp(u32 intr_info)
1646{
1647 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1648 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1649}
1650
Gui Jianfeng31299942010-03-15 17:29:09 +08001651static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001652{
Sheng Yang04547152009-04-01 15:52:31 +08001653 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001654}
1655
Gui Jianfeng31299942010-03-15 17:29:09 +08001656static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001657{
Sheng Yang04547152009-04-01 15:52:31 +08001658 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001659}
1660
Paolo Bonzini35754c92015-07-29 12:05:37 +02001661static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001662{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001663 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001664}
1665
Gui Jianfeng31299942010-03-15 17:29:09 +08001666static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001667{
Sheng Yang04547152009-04-01 15:52:31 +08001668 return vmcs_config.cpu_based_exec_ctrl &
1669 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670}
1671
Avi Kivity774ead32007-12-26 13:57:04 +02001672static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001673{
Sheng Yang04547152009-04-01 15:52:31 +08001674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1676}
1677
Yang Zhang8d146952013-01-25 10:18:50 +08001678static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1682}
1683
Yang Zhang83d4c282013-01-25 10:18:49 +08001684static inline bool cpu_has_vmx_apic_register_virt(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1688}
1689
Yang Zhangc7c9c562013-01-25 10:18:51 +08001690static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1694}
1695
Sean Christopherson0b665d32018-08-14 09:33:34 -07001696static inline bool cpu_has_vmx_encls_vmexit(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_ENCLS_EXITING;
1700}
1701
Yunhong Jiang64672c92016-06-13 14:19:59 -07001702/*
1703 * Comment's format: document - errata name - stepping - processor name.
1704 * Refer from
1705 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1706 */
1707static u32 vmx_preemption_cpu_tfms[] = {
1708/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17090x000206E6,
1710/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1711/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1712/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17130x00020652,
1714/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17150x00020655,
1716/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1717/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1718/*
1719 * 320767.pdf - AAP86 - B1 -
1720 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1721 */
17220x000106E5,
1723/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17240x000106A0,
1725/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17260x000106A1,
1727/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17280x000106A4,
1729 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1730 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1731 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17320x000106A5,
1733};
1734
1735static inline bool cpu_has_broken_vmx_preemption_timer(void)
1736{
1737 u32 eax = cpuid_eax(0x00000001), i;
1738
1739 /* Clear the reserved bits */
1740 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001741 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001742 if (eax == vmx_preemption_cpu_tfms[i])
1743 return true;
1744
1745 return false;
1746}
1747
1748static inline bool cpu_has_vmx_preemption_timer(void)
1749{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001750 return vmcs_config.pin_based_exec_ctrl &
1751 PIN_BASED_VMX_PREEMPTION_TIMER;
1752}
1753
Yang Zhang01e439b2013-04-11 19:25:12 +08001754static inline bool cpu_has_vmx_posted_intr(void)
1755{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001756 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1757 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001758}
1759
1760static inline bool cpu_has_vmx_apicv(void)
1761{
1762 return cpu_has_vmx_apic_register_virt() &&
1763 cpu_has_vmx_virtual_intr_delivery() &&
1764 cpu_has_vmx_posted_intr();
1765}
1766
Sheng Yang04547152009-04-01 15:52:31 +08001767static inline bool cpu_has_vmx_flexpriority(void)
1768{
1769 return cpu_has_vmx_tpr_shadow() &&
1770 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001771}
1772
Marcelo Tosattie7997942009-06-11 12:07:40 -03001773static inline bool cpu_has_vmx_ept_execute_only(void)
1774{
Gui Jianfeng31299942010-03-15 17:29:09 +08001775 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001776}
1777
Marcelo Tosattie7997942009-06-11 12:07:40 -03001778static inline bool cpu_has_vmx_ept_2m_page(void)
1779{
Gui Jianfeng31299942010-03-15 17:29:09 +08001780 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001781}
1782
Sheng Yang878403b2010-01-05 19:02:29 +08001783static inline bool cpu_has_vmx_ept_1g_page(void)
1784{
Gui Jianfeng31299942010-03-15 17:29:09 +08001785 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001786}
1787
Sheng Yang4bc9b982010-06-02 14:05:24 +08001788static inline bool cpu_has_vmx_ept_4levels(void)
1789{
1790 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1791}
1792
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001793static inline bool cpu_has_vmx_ept_mt_wb(void)
1794{
1795 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1796}
1797
Yu Zhang855feb62017-08-24 20:27:55 +08001798static inline bool cpu_has_vmx_ept_5levels(void)
1799{
1800 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1801}
1802
Xudong Hao83c3a332012-05-28 19:33:35 +08001803static inline bool cpu_has_vmx_ept_ad_bits(void)
1804{
1805 return vmx_capability.ept & VMX_EPT_AD_BIT;
1806}
1807
Gui Jianfeng31299942010-03-15 17:29:09 +08001808static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001809{
Gui Jianfeng31299942010-03-15 17:29:09 +08001810 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001811}
1812
Gui Jianfeng31299942010-03-15 17:29:09 +08001813static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001814{
Gui Jianfeng31299942010-03-15 17:29:09 +08001815 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001816}
1817
Liran Aloncd9a4912018-05-22 17:16:15 +03001818static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1819{
1820 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1821}
1822
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001823static inline bool cpu_has_vmx_invvpid_single(void)
1824{
1825 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1826}
1827
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001828static inline bool cpu_has_vmx_invvpid_global(void)
1829{
1830 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1831}
1832
Wanpeng Li08d839c2017-03-23 05:30:08 -07001833static inline bool cpu_has_vmx_invvpid(void)
1834{
1835 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1836}
1837
Gui Jianfeng31299942010-03-15 17:29:09 +08001838static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001839{
Sheng Yang04547152009-04-01 15:52:31 +08001840 return vmcs_config.cpu_based_2nd_exec_ctrl &
1841 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001842}
1843
Gui Jianfeng31299942010-03-15 17:29:09 +08001844static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001845{
1846 return vmcs_config.cpu_based_2nd_exec_ctrl &
1847 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1848}
1849
Gui Jianfeng31299942010-03-15 17:29:09 +08001850static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001851{
1852 return vmcs_config.cpu_based_2nd_exec_ctrl &
1853 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1854}
1855
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001856static inline bool cpu_has_vmx_basic_inout(void)
1857{
1858 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1859}
1860
Paolo Bonzini35754c92015-07-29 12:05:37 +02001861static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001862{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001863 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001864}
1865
Gui Jianfeng31299942010-03-15 17:29:09 +08001866static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001867{
Sheng Yang04547152009-04-01 15:52:31 +08001868 return vmcs_config.cpu_based_2nd_exec_ctrl &
1869 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001870}
1871
Gui Jianfeng31299942010-03-15 17:29:09 +08001872static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001873{
1874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_RDTSCP;
1876}
1877
Mao, Junjiead756a12012-07-02 01:18:48 +00001878static inline bool cpu_has_vmx_invpcid(void)
1879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_ENABLE_INVPCID;
1882}
1883
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001884static inline bool cpu_has_virtual_nmis(void)
1885{
1886 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1887}
1888
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001889static inline bool cpu_has_vmx_wbinvd_exit(void)
1890{
1891 return vmcs_config.cpu_based_2nd_exec_ctrl &
1892 SECONDARY_EXEC_WBINVD_EXITING;
1893}
1894
Abel Gordonabc4fc52013-04-18 14:35:25 +03001895static inline bool cpu_has_vmx_shadow_vmcs(void)
1896{
1897 u64 vmx_msr;
1898 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1899 /* check if the cpu supports writing r/o exit information fields */
1900 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1901 return false;
1902
1903 return vmcs_config.cpu_based_2nd_exec_ctrl &
1904 SECONDARY_EXEC_SHADOW_VMCS;
1905}
1906
Kai Huang843e4332015-01-28 10:54:28 +08001907static inline bool cpu_has_vmx_pml(void)
1908{
1909 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1910}
1911
Haozhong Zhang64903d62015-10-20 15:39:09 +08001912static inline bool cpu_has_vmx_tsc_scaling(void)
1913{
1914 return vmcs_config.cpu_based_2nd_exec_ctrl &
1915 SECONDARY_EXEC_TSC_SCALING;
1916}
1917
Bandan Das2a499e42017-08-03 15:54:41 -04001918static inline bool cpu_has_vmx_vmfunc(void)
1919{
1920 return vmcs_config.cpu_based_2nd_exec_ctrl &
1921 SECONDARY_EXEC_ENABLE_VMFUNC;
1922}
1923
Sean Christopherson64f7a112018-04-30 10:01:06 -07001924static bool vmx_umip_emulated(void)
1925{
1926 return vmcs_config.cpu_based_2nd_exec_ctrl &
1927 SECONDARY_EXEC_DESC;
1928}
1929
Sheng Yang04547152009-04-01 15:52:31 +08001930static inline bool report_flexpriority(void)
1931{
1932 return flexpriority_enabled;
1933}
1934
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001935static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1936{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001937 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001938}
1939
Jim Mattsonf4160e42018-05-29 09:11:33 -07001940/*
1941 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1942 * to modify any valid field of the VMCS, or are the VM-exit
1943 * information fields read-only?
1944 */
1945static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1946{
1947 return to_vmx(vcpu)->nested.msrs.misc_low &
1948 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1949}
1950
Marc Orr04473782018-06-20 17:21:29 -07001951static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1952{
1953 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1954}
1955
1956static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1957{
1958 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1959 CPU_BASED_MONITOR_TRAP_FLAG;
1960}
1961
Liran Alonfa97d7d2018-07-18 14:07:59 +02001962static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1963{
1964 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1965 SECONDARY_EXEC_SHADOW_VMCS;
1966}
1967
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001968static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1969{
1970 return vmcs12->cpu_based_vm_exec_control & bit;
1971}
1972
1973static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1974{
1975 return (vmcs12->cpu_based_vm_exec_control &
1976 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1977 (vmcs12->secondary_vm_exec_control & bit);
1978}
1979
Jan Kiszkaf4124502014-03-07 20:03:13 +01001980static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1981{
1982 return vmcs12->pin_based_vm_exec_control &
1983 PIN_BASED_VMX_PREEMPTION_TIMER;
1984}
1985
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001986static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1987{
1988 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1989}
1990
1991static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1992{
1993 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1994}
1995
Nadav Har'El155a97a2013-08-05 11:07:16 +03001996static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1997{
1998 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1999}
2000
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002001static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2002{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002003 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002004}
2005
Bandan Dasc5f983f2017-05-05 15:25:14 -04002006static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2007{
2008 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2009}
2010
Wincy Vanf2b93282015-02-03 23:56:03 +08002011static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2012{
2013 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2014}
2015
Wanpeng Li5c614b32015-10-13 09:18:36 -07002016static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2017{
2018 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2019}
2020
Wincy Van82f0dd42015-02-03 23:57:18 +08002021static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2022{
2023 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2024}
2025
Wincy Van608406e2015-02-03 23:57:51 +08002026static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2027{
2028 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2029}
2030
Wincy Van705699a2015-02-03 23:58:17 +08002031static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2032{
2033 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2034}
2035
Bandan Das27c42a12017-08-03 15:54:42 -04002036static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2037{
2038 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2039}
2040
Bandan Das41ab9372017-08-03 15:54:43 -04002041static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2042{
2043 return nested_cpu_has_vmfunc(vmcs12) &&
2044 (vmcs12->vm_function_control &
2045 VMX_VMFUNC_EPTP_SWITCHING);
2046}
2047
Liran Alonf792d272018-06-23 02:35:05 +03002048static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2049{
2050 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2051}
2052
Jim Mattsonef85b672016-12-12 11:01:37 -08002053static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002054{
2055 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002056 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002057}
2058
Jan Kiszka533558b2014-01-04 18:47:20 +01002059static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2060 u32 exit_intr_info,
2061 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002062static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2063 struct vmcs12 *vmcs12,
2064 u32 reason, unsigned long qualification);
2065
Rusty Russell8b9cf982007-07-30 16:31:43 +10002066static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002067{
2068 int i;
2069
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002070 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002071 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002072 return i;
2073 return -1;
2074}
2075
Sheng Yang2384d2b2008-01-17 15:14:33 +08002076static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2077{
2078 struct {
2079 u64 vpid : 16;
2080 u64 rsvd : 48;
2081 u64 gva;
2082 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002083 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002084
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002085 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2086 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2087 : "memory");
2088 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002089}
2090
Sheng Yang14394422008-04-28 12:24:45 +08002091static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2092{
2093 struct {
2094 u64 eptp, gpa;
2095 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002096 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002097
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002098 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2099 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2100 : "memory");
2101 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002102}
2103
Avi Kivity26bb0982009-09-07 11:14:12 +03002104static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002105{
2106 int i;
2107
Rusty Russell8b9cf982007-07-30 16:31:43 +10002108 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002109 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002110 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002111 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void vmcs_clear(struct vmcs *vmcs)
2115{
2116 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002117 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002119 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2120 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2121 : "memory");
2122 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2124 vmcs, phys_addr);
2125}
2126
Nadav Har'Eld462b812011-05-24 15:26:10 +03002127static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2128{
2129 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002130 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2131 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002132 loaded_vmcs->cpu = -1;
2133 loaded_vmcs->launched = 0;
2134}
2135
Dongxiao Xu7725b892010-05-11 18:29:38 +08002136static void vmcs_load(struct vmcs *vmcs)
2137{
2138 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002139 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002140
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002141 if (static_branch_unlikely(&enable_evmcs))
2142 return evmcs_load(phys_addr);
2143
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002144 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2145 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2146 : "memory");
2147 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002148 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002149 vmcs, phys_addr);
2150}
2151
Dave Young2965faa2015-09-09 15:38:55 -07002152#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002153/*
2154 * This bitmap is used to indicate whether the vmclear
2155 * operation is enabled on all cpus. All disabled by
2156 * default.
2157 */
2158static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2159
2160static inline void crash_enable_local_vmclear(int cpu)
2161{
2162 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2163}
2164
2165static inline void crash_disable_local_vmclear(int cpu)
2166{
2167 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2168}
2169
2170static inline int crash_local_vmclear_enabled(int cpu)
2171{
2172 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2173}
2174
2175static void crash_vmclear_local_loaded_vmcss(void)
2176{
2177 int cpu = raw_smp_processor_id();
2178 struct loaded_vmcs *v;
2179
2180 if (!crash_local_vmclear_enabled(cpu))
2181 return;
2182
2183 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2184 loaded_vmcss_on_cpu_link)
2185 vmcs_clear(v->vmcs);
2186}
2187#else
2188static inline void crash_enable_local_vmclear(int cpu) { }
2189static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002190#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002191
Nadav Har'Eld462b812011-05-24 15:26:10 +03002192static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002194 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002195 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196
Nadav Har'Eld462b812011-05-24 15:26:10 +03002197 if (loaded_vmcs->cpu != cpu)
2198 return; /* vcpu migration can race with cpu offline */
2199 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002201 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002202 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002203
2204 /*
2205 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2206 * is before setting loaded_vmcs->vcpu to -1 which is done in
2207 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2208 * then adds the vmcs into percpu list before it is deleted.
2209 */
2210 smp_wmb();
2211
Nadav Har'Eld462b812011-05-24 15:26:10 +03002212 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002213 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214}
2215
Nadav Har'Eld462b812011-05-24 15:26:10 +03002216static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002217{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002218 int cpu = loaded_vmcs->cpu;
2219
2220 if (cpu != -1)
2221 smp_call_function_single(cpu,
2222 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002223}
2224
Junaid Shahidfaff8752018-06-29 13:10:05 -07002225static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2226{
2227 if (vpid == 0)
2228 return true;
2229
2230 if (cpu_has_vmx_invvpid_individual_addr()) {
2231 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2232 return true;
2233 }
2234
2235 return false;
2236}
2237
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002238static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002239{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002240 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002241 return;
2242
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002243 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002244 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002245}
2246
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002247static inline void vpid_sync_vcpu_global(void)
2248{
2249 if (cpu_has_vmx_invvpid_global())
2250 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2251}
2252
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002253static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002254{
2255 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002256 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002257 else
2258 vpid_sync_vcpu_global();
2259}
2260
Sheng Yang14394422008-04-28 12:24:45 +08002261static inline void ept_sync_global(void)
2262{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002263 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002264}
2265
2266static inline void ept_sync_context(u64 eptp)
2267{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002268 if (cpu_has_vmx_invept_context())
2269 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2270 else
2271 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002272}
2273
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002274static __always_inline void vmcs_check16(unsigned long field)
2275{
2276 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2277 "16-bit accessor invalid for 64-bit field");
2278 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2279 "16-bit accessor invalid for 64-bit high field");
2280 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2281 "16-bit accessor invalid for 32-bit high field");
2282 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2283 "16-bit accessor invalid for natural width field");
2284}
2285
2286static __always_inline void vmcs_check32(unsigned long field)
2287{
2288 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2289 "32-bit accessor invalid for 16-bit field");
2290 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2291 "32-bit accessor invalid for natural width field");
2292}
2293
2294static __always_inline void vmcs_check64(unsigned long field)
2295{
2296 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2297 "64-bit accessor invalid for 16-bit field");
2298 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2299 "64-bit accessor invalid for 64-bit high field");
2300 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2301 "64-bit accessor invalid for 32-bit field");
2302 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2303 "64-bit accessor invalid for natural width field");
2304}
2305
2306static __always_inline void vmcs_checkl(unsigned long field)
2307{
2308 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2309 "Natural width accessor invalid for 16-bit field");
2310 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2311 "Natural width accessor invalid for 64-bit field");
2312 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2313 "Natural width accessor invalid for 64-bit high field");
2314 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2315 "Natural width accessor invalid for 32-bit field");
2316}
2317
2318static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319{
Avi Kivity5e520e62011-05-15 10:13:12 -04002320 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
Avi Kivity5e520e62011-05-15 10:13:12 -04002322 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2323 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 return value;
2325}
2326
Avi Kivity96304212011-05-15 10:13:13 -04002327static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002330 if (static_branch_unlikely(&enable_evmcs))
2331 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002332 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333}
2334
Avi Kivity96304212011-05-15 10:13:13 -04002335static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002337 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002338 if (static_branch_unlikely(&enable_evmcs))
2339 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341}
2342
Avi Kivity96304212011-05-15 10:13:13 -04002343static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002345 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002346 if (static_branch_unlikely(&enable_evmcs))
2347 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002348#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002349 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002351 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352#endif
2353}
2354
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002355static __always_inline unsigned long vmcs_readl(unsigned long field)
2356{
2357 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002358 if (static_branch_unlikely(&enable_evmcs))
2359 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002360 return __vmcs_readl(field);
2361}
2362
Avi Kivitye52de1b2007-01-05 16:36:56 -08002363static noinline void vmwrite_error(unsigned long field, unsigned long value)
2364{
2365 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2366 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2367 dump_stack();
2368}
2369
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002370static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002372 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002374 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2375 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002376 if (unlikely(error))
2377 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002382 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002383 if (static_branch_unlikely(&enable_evmcs))
2384 return evmcs_write16(field, value);
2385
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387}
2388
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002391 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002392 if (static_branch_unlikely(&enable_evmcs))
2393 return evmcs_write32(field, value);
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396}
2397
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002398static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002400 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002401 if (static_branch_unlikely(&enable_evmcs))
2402 return evmcs_write64(field, value);
2403
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002405#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408#endif
2409}
2410
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002411static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002412{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002414 if (static_branch_unlikely(&enable_evmcs))
2415 return evmcs_write64(field, value);
2416
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002418}
2419
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002420static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002421{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002422 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2423 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002424 if (static_branch_unlikely(&enable_evmcs))
2425 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2426
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002427 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2428}
2429
2430static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2431{
2432 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2433 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002434 if (static_branch_unlikely(&enable_evmcs))
2435 return evmcs_write32(field, evmcs_read32(field) | mask);
2436
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002437 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002438}
2439
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002440static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2441{
2442 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2443}
2444
Gleb Natapov2961e8762013-11-25 15:37:13 +02002445static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2446{
2447 vmcs_write32(VM_ENTRY_CONTROLS, val);
2448 vmx->vm_entry_controls_shadow = val;
2449}
2450
2451static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2452{
2453 if (vmx->vm_entry_controls_shadow != val)
2454 vm_entry_controls_init(vmx, val);
2455}
2456
2457static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2458{
2459 return vmx->vm_entry_controls_shadow;
2460}
2461
2462
2463static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2464{
2465 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2466}
2467
2468static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2469{
2470 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2471}
2472
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002473static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2474{
2475 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2476}
2477
Gleb Natapov2961e8762013-11-25 15:37:13 +02002478static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2479{
2480 vmcs_write32(VM_EXIT_CONTROLS, val);
2481 vmx->vm_exit_controls_shadow = val;
2482}
2483
2484static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2485{
2486 if (vmx->vm_exit_controls_shadow != val)
2487 vm_exit_controls_init(vmx, val);
2488}
2489
2490static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2491{
2492 return vmx->vm_exit_controls_shadow;
2493}
2494
2495
2496static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2497{
2498 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2499}
2500
2501static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2502{
2503 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2504}
2505
Avi Kivity2fb92db2011-04-27 19:42:18 +03002506static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2507{
2508 vmx->segment_cache.bitmask = 0;
2509}
2510
2511static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2512 unsigned field)
2513{
2514 bool ret;
2515 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2516
2517 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2518 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2519 vmx->segment_cache.bitmask = 0;
2520 }
2521 ret = vmx->segment_cache.bitmask & mask;
2522 vmx->segment_cache.bitmask |= mask;
2523 return ret;
2524}
2525
2526static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2527{
2528 u16 *p = &vmx->segment_cache.seg[seg].selector;
2529
2530 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2531 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2532 return *p;
2533}
2534
2535static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2536{
2537 ulong *p = &vmx->segment_cache.seg[seg].base;
2538
2539 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2540 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2541 return *p;
2542}
2543
2544static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2545{
2546 u32 *p = &vmx->segment_cache.seg[seg].limit;
2547
2548 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2549 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2550 return *p;
2551}
2552
2553static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2554{
2555 u32 *p = &vmx->segment_cache.seg[seg].ar;
2556
2557 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2558 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2559 return *p;
2560}
2561
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002562static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2563{
2564 u32 eb;
2565
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002566 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002567 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002568 /*
2569 * Guest access to VMware backdoor ports could legitimately
2570 * trigger #GP because of TSS I/O permission bitmap.
2571 * We intercept those #GP and allow access to them anyway
2572 * as VMware does.
2573 */
2574 if (enable_vmware_backdoor)
2575 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002576 if ((vcpu->guest_debug &
2577 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2578 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2579 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002580 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002581 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002582 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002583 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002584
2585 /* When we are running a nested L2 guest and L1 specified for it a
2586 * certain exception bitmap, we must trap the same exceptions and pass
2587 * them to L1. When running L2, we will only handle the exceptions
2588 * specified above if L1 did not want them.
2589 */
2590 if (is_guest_mode(vcpu))
2591 eb |= get_vmcs12(vcpu)->exception_bitmap;
2592
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002593 vmcs_write32(EXCEPTION_BITMAP, eb);
2594}
2595
Ashok Raj15d45072018-02-01 22:59:43 +01002596/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002597 * Check if MSR is intercepted for currently loaded MSR bitmap.
2598 */
2599static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2600{
2601 unsigned long *msr_bitmap;
2602 int f = sizeof(unsigned long);
2603
2604 if (!cpu_has_vmx_msr_bitmap())
2605 return true;
2606
2607 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2608
2609 if (msr <= 0x1fff) {
2610 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2611 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2612 msr &= 0x1fff;
2613 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2614 }
2615
2616 return true;
2617}
2618
2619/*
Ashok Raj15d45072018-02-01 22:59:43 +01002620 * Check if MSR is intercepted for L01 MSR bitmap.
2621 */
2622static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2623{
2624 unsigned long *msr_bitmap;
2625 int f = sizeof(unsigned long);
2626
2627 if (!cpu_has_vmx_msr_bitmap())
2628 return true;
2629
2630 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2631
2632 if (msr <= 0x1fff) {
2633 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2634 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2635 msr &= 0x1fff;
2636 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2637 }
2638
2639 return true;
2640}
2641
Gleb Natapov2961e8762013-11-25 15:37:13 +02002642static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2643 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002644{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002645 vm_entry_controls_clearbit(vmx, entry);
2646 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002647}
2648
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002649static int find_msr(struct vmx_msrs *m, unsigned int msr)
2650{
2651 unsigned int i;
2652
2653 for (i = 0; i < m->nr; ++i) {
2654 if (m->val[i].index == msr)
2655 return i;
2656 }
2657 return -ENOENT;
2658}
2659
Avi Kivity61d2ef22010-04-28 16:40:38 +03002660static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2661{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002662 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002663 struct msr_autoload *m = &vmx->msr_autoload;
2664
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002665 switch (msr) {
2666 case MSR_EFER:
2667 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002668 clear_atomic_switch_msr_special(vmx,
2669 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002670 VM_EXIT_LOAD_IA32_EFER);
2671 return;
2672 }
2673 break;
2674 case MSR_CORE_PERF_GLOBAL_CTRL:
2675 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002676 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002677 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2678 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2679 return;
2680 }
2681 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002682 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002683 i = find_msr(&m->guest, msr);
2684 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002685 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002686 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002687 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002688 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002689
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002690skip_guest:
2691 i = find_msr(&m->host, msr);
2692 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002693 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002694
2695 --m->host.nr;
2696 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002697 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002698}
2699
Gleb Natapov2961e8762013-11-25 15:37:13 +02002700static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2701 unsigned long entry, unsigned long exit,
2702 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2703 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002704{
2705 vmcs_write64(guest_val_vmcs, guest_val);
2706 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002707 vm_entry_controls_setbit(vmx, entry);
2708 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002709}
2710
Avi Kivity61d2ef22010-04-28 16:40:38 +03002711static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002712 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002713{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002714 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002715 struct msr_autoload *m = &vmx->msr_autoload;
2716
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002717 switch (msr) {
2718 case MSR_EFER:
2719 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002720 add_atomic_switch_msr_special(vmx,
2721 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002722 VM_EXIT_LOAD_IA32_EFER,
2723 GUEST_IA32_EFER,
2724 HOST_IA32_EFER,
2725 guest_val, host_val);
2726 return;
2727 }
2728 break;
2729 case MSR_CORE_PERF_GLOBAL_CTRL:
2730 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002731 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002732 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2733 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2734 GUEST_IA32_PERF_GLOBAL_CTRL,
2735 HOST_IA32_PERF_GLOBAL_CTRL,
2736 guest_val, host_val);
2737 return;
2738 }
2739 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002740 case MSR_IA32_PEBS_ENABLE:
2741 /* PEBS needs a quiescent period after being disabled (to write
2742 * a record). Disabling PEBS through VMX MSR swapping doesn't
2743 * provide that period, so a CPU could write host's record into
2744 * guest's memory.
2745 */
2746 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002747 }
2748
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002749 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002750 if (!entry_only)
2751 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002752
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002753 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002754 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002755 "Can't add msr %x\n", msr);
2756 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002757 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002758 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002759 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002760 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002761 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002762 m->guest.val[i].index = msr;
2763 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002764
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002765 if (entry_only)
2766 return;
2767
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002768 if (j < 0) {
2769 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002770 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002771 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002772 m->host.val[j].index = msr;
2773 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002774}
2775
Avi Kivity92c0d902009-10-29 11:00:16 +02002776static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002777{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002778 u64 guest_efer = vmx->vcpu.arch.efer;
2779 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002780
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002781 if (!enable_ept) {
2782 /*
2783 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2784 * host CPUID is more efficient than testing guest CPUID
2785 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2786 */
2787 if (boot_cpu_has(X86_FEATURE_SMEP))
2788 guest_efer |= EFER_NX;
2789 else if (!(guest_efer & EFER_NX))
2790 ignore_bits |= EFER_NX;
2791 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002792
Avi Kivity51c6cf62007-08-29 03:48:05 +03002793 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002794 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002795 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002796 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002797#ifdef CONFIG_X86_64
2798 ignore_bits |= EFER_LMA | EFER_LME;
2799 /* SCE is meaningful only in long mode on Intel */
2800 if (guest_efer & EFER_LMA)
2801 ignore_bits &= ~(u64)EFER_SCE;
2802#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002803
2804 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002805
2806 /*
2807 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2808 * On CPUs that support "load IA32_EFER", always switch EFER
2809 * atomically, since it's faster than switching it manually.
2810 */
2811 if (cpu_has_load_ia32_efer ||
2812 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002813 if (!(guest_efer & EFER_LMA))
2814 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002815 if (guest_efer != host_efer)
2816 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002817 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002818 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002819 } else {
2820 guest_efer &= ~ignore_bits;
2821 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002822
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002823 vmx->guest_msrs[efer_offset].data = guest_efer;
2824 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2825
2826 return true;
2827 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002828}
2829
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002830#ifdef CONFIG_X86_32
2831/*
2832 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2833 * VMCS rather than the segment table. KVM uses this helper to figure
2834 * out the current bases to poke them into the VMCS before entry.
2835 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836static unsigned long segment_base(u16 selector)
2837{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 unsigned long v;
2840
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002841 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842 return 0;
2843
Thomas Garnier45fc8752017-03-14 10:05:08 -07002844 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002845
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002846 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847 u16 ldt_selector = kvm_read_ldt();
2848
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 return 0;
2851
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002852 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002853 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002854 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002855 return v;
2856}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002857#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002858
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002859static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002860{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002861 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002862 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002863#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002864 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002865#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002866 unsigned long fs_base, gs_base;
2867 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002868 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002869
Sean Christophersond264ee02018-08-27 15:21:12 -07002870 vmx->req_immediate_exit = false;
2871
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002873 return;
2874
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002875 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002876 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002877
Avi Kivity33ed6322007-05-02 16:54:03 +03002878 /*
2879 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2880 * allow segment selectors with cpl > 0 or ti == 1.
2881 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002882 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002883
2884#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002885 savesegment(ds, host_state->ds_sel);
2886 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002887
2888 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002889 if (likely(is_64bit_mm(current->mm))) {
2890 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002891 fs_sel = current->thread.fsindex;
2892 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002893 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002894 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002895 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002896 savesegment(fs, fs_sel);
2897 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002898 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002899 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002900 }
2901
Paolo Bonzini4679b612018-09-24 17:23:01 +02002902 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002903#else
Sean Christophersone368b872018-07-23 12:32:41 -07002904 savesegment(fs, fs_sel);
2905 savesegment(gs, gs_sel);
2906 fs_base = segment_base(fs_sel);
2907 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002908#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002909
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002910 if (unlikely(fs_sel != host_state->fs_sel)) {
2911 if (!(fs_sel & 7))
2912 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2913 else
2914 vmcs_write16(HOST_FS_SELECTOR, 0);
2915 host_state->fs_sel = fs_sel;
2916 }
2917 if (unlikely(gs_sel != host_state->gs_sel)) {
2918 if (!(gs_sel & 7))
2919 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2920 else
2921 vmcs_write16(HOST_GS_SELECTOR, 0);
2922 host_state->gs_sel = gs_sel;
2923 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002924 if (unlikely(fs_base != host_state->fs_base)) {
2925 vmcs_writel(HOST_FS_BASE, fs_base);
2926 host_state->fs_base = fs_base;
2927 }
2928 if (unlikely(gs_base != host_state->gs_base)) {
2929 vmcs_writel(HOST_GS_BASE, gs_base);
2930 host_state->gs_base = gs_base;
2931 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002932
Avi Kivity26bb0982009-09-07 11:14:12 +03002933 for (i = 0; i < vmx->save_nmsrs; ++i)
2934 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002935 vmx->guest_msrs[i].data,
2936 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002937}
2938
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002939static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002940{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002941 struct vmcs_host_state *host_state;
2942
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002943 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002944 return;
2945
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002946 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002947 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002948
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002949 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002950 vmx->loaded_cpu_state = NULL;
2951
Avi Kivityc8770e72010-11-11 12:37:26 +02002952#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002953 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002954#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2956 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002957#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002958 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002959#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002960 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002961#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002962 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002963 if (host_state->fs_sel & 7)
2964 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002965#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002966 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2967 loadsegment(ds, host_state->ds_sel);
2968 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002969 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002970#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002971 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002972#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002973 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002974#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002975 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002976}
2977
Sean Christopherson678e3152018-07-23 12:32:43 -07002978#ifdef CONFIG_X86_64
2979static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002980{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002981 preempt_disable();
2982 if (vmx->loaded_cpu_state)
2983 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2984 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002985 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002986}
2987
Sean Christopherson678e3152018-07-23 12:32:43 -07002988static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2989{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002990 preempt_disable();
2991 if (vmx->loaded_cpu_state)
2992 wrmsrl(MSR_KERNEL_GS_BASE, data);
2993 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002994 vmx->msr_guest_kernel_gs_base = data;
2995}
2996#endif
2997
Feng Wu28b835d2015-09-18 22:29:54 +08002998static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2999{
3000 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3001 struct pi_desc old, new;
3002 unsigned int dest;
3003
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003004 /*
3005 * In case of hot-plug or hot-unplug, we may have to undo
3006 * vmx_vcpu_pi_put even if there is no assigned device. And we
3007 * always keep PI.NDST up to date for simplicity: it makes the
3008 * code easier, and CPU migration is not a fast path.
3009 */
3010 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003011 return;
3012
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003013 /*
3014 * First handle the simple case where no cmpxchg is necessary; just
3015 * allow posting non-urgent interrupts.
3016 *
3017 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3018 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3019 * expects the VCPU to be on the blocked_vcpu_list that matches
3020 * PI.NDST.
3021 */
3022 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3023 vcpu->cpu == cpu) {
3024 pi_clear_sn(pi_desc);
3025 return;
3026 }
3027
3028 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003029 do {
3030 old.control = new.control = pi_desc->control;
3031
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003032 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003033
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003034 if (x2apic_enabled())
3035 new.ndst = dest;
3036 else
3037 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003038
Feng Wu28b835d2015-09-18 22:29:54 +08003039 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003040 } while (cmpxchg64(&pi_desc->control, old.control,
3041 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003042}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003043
Peter Feinerc95ba922016-08-17 09:36:47 -07003044static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3045{
3046 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3047 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3048}
3049
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050/*
3051 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3052 * vcpu mutex is already taken.
3053 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003054static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003056 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003057 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003059 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003060 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003061 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003062 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003063
3064 /*
3065 * Read loaded_vmcs->cpu should be before fetching
3066 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3067 * See the comments in __loaded_vmcs_clear().
3068 */
3069 smp_rmb();
3070
Nadav Har'Eld462b812011-05-24 15:26:10 +03003071 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3072 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003073 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003074 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003075 }
3076
3077 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3078 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3079 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003080 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003081 }
3082
3083 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003084 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003085 unsigned long sysenter_esp;
3086
3087 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003088
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 /*
3090 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003091 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003093 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003094 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003095 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003097 /*
3098 * VM exits change the host TR limit to 0x67 after a VM
3099 * exit. This is okay, since 0x67 covers everything except
3100 * the IO bitmap and have have code to handle the IO bitmap
3101 * being lost after a VM exit.
3102 */
3103 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3104
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3106 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003107
Nadav Har'Eld462b812011-05-24 15:26:10 +03003108 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 }
Feng Wu28b835d2015-09-18 22:29:54 +08003110
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003111 /* Setup TSC multiplier */
3112 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003113 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3114 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003115
Feng Wu28b835d2015-09-18 22:29:54 +08003116 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003117 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003118 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003119}
3120
3121static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3122{
3123 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3124
3125 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003126 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3127 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003128 return;
3129
3130 /* Set SN when the vCPU is preempted */
3131 if (vcpu->preempted)
3132 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133}
3134
3135static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3136{
Feng Wu28b835d2015-09-18 22:29:54 +08003137 vmx_vcpu_pi_put(vcpu);
3138
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003139 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140}
3141
Wanpeng Lif244dee2017-07-20 01:11:54 -07003142static bool emulation_required(struct kvm_vcpu *vcpu)
3143{
3144 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3145}
3146
Avi Kivityedcafe32009-12-30 18:07:40 +02003147static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3148
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003149/*
3150 * Return the cr0 value that a nested guest would read. This is a combination
3151 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3152 * its hypervisor (cr0_read_shadow).
3153 */
3154static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3155{
3156 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3157 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3158}
3159static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3160{
3161 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3162 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3163}
3164
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3166{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003167 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003168
Avi Kivity6de12732011-03-07 12:51:22 +02003169 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3170 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3171 rflags = vmcs_readl(GUEST_RFLAGS);
3172 if (to_vmx(vcpu)->rmode.vm86_active) {
3173 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3174 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3175 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3176 }
3177 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003178 }
Avi Kivity6de12732011-03-07 12:51:22 +02003179 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180}
3181
3182static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3183{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003184 unsigned long old_rflags = vmx_get_rflags(vcpu);
3185
Avi Kivity6de12732011-03-07 12:51:22 +02003186 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3187 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003188 if (to_vmx(vcpu)->rmode.vm86_active) {
3189 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003190 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003191 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003193
3194 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3195 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196}
3197
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003198static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003199{
3200 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3201 int ret = 0;
3202
3203 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003204 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003205 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003206 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003207
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003208 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003209}
3210
3211static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3212{
3213 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3214 u32 interruptibility = interruptibility_old;
3215
3216 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3217
Jan Kiszka48005f62010-02-19 19:38:07 +01003218 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003219 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003220 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003221 interruptibility |= GUEST_INTR_STATE_STI;
3222
3223 if ((interruptibility != interruptibility_old))
3224 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3225}
3226
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3228{
3229 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003231 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003233 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234
Glauber Costa2809f5d2009-05-12 16:21:05 -04003235 /* skipping an emulated instruction also counts */
3236 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237}
3238
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003239static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3240 unsigned long exit_qual)
3241{
3242 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3243 unsigned int nr = vcpu->arch.exception.nr;
3244 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3245
3246 if (vcpu->arch.exception.has_error_code) {
3247 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3248 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3249 }
3250
3251 if (kvm_exception_is_soft(nr))
3252 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3253 else
3254 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3255
3256 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3257 vmx_get_nmi_mask(vcpu))
3258 intr_info |= INTR_INFO_UNBLOCK_NMI;
3259
3260 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3261}
3262
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003263/*
3264 * KVM wants to inject page-faults which it got to the guest. This function
3265 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003266 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003267static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003268{
3269 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003270 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003271
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003272 if (nr == PF_VECTOR) {
3273 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003274 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003275 return 1;
3276 }
3277 /*
3278 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3279 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3280 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3281 * can be written only when inject_pending_event runs. This should be
3282 * conditional on a new capability---if the capability is disabled,
3283 * kvm_multiple_exception would write the ancillary information to
3284 * CR2 or DR6, for backwards ABI-compatibility.
3285 */
3286 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3287 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003288 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003289 return 1;
3290 }
3291 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003292 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003293 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003294 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003295 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3296 *exit_qual ^= DR6_RTM;
3297 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003298 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003299 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003300 return 1;
3301 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003302 }
3303
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003304 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003305}
3306
Wanpeng Licaa057a2018-03-12 04:53:03 -07003307static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3308{
3309 /*
3310 * Ensure that we clear the HLT state in the VMCS. We don't need to
3311 * explicitly skip the instruction because if the HLT state is set,
3312 * then the instruction is already executing and RIP has already been
3313 * advanced.
3314 */
3315 if (kvm_hlt_in_guest(vcpu->kvm) &&
3316 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3317 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3318}
3319
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003320static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003321{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003322 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003323 unsigned nr = vcpu->arch.exception.nr;
3324 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003325 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003326 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003327
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003328 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003329 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003330 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3331 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003332
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003333 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003334 int inc_eip = 0;
3335 if (kvm_exception_is_soft(nr))
3336 inc_eip = vcpu->arch.event_exit_inst_len;
3337 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003338 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003339 return;
3340 }
3341
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003342 WARN_ON_ONCE(vmx->emulation_required);
3343
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003344 if (kvm_exception_is_soft(nr)) {
3345 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3346 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003347 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3348 } else
3349 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3350
3351 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003352
3353 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003354}
3355
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003356static bool vmx_rdtscp_supported(void)
3357{
3358 return cpu_has_vmx_rdtscp();
3359}
3360
Mao, Junjiead756a12012-07-02 01:18:48 +00003361static bool vmx_invpcid_supported(void)
3362{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003363 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003364}
3365
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366/*
Eddie Donga75beee2007-05-17 18:55:15 +03003367 * Swap MSR entry in host/guest MSR entry array.
3368 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003369static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003370{
Avi Kivity26bb0982009-09-07 11:14:12 +03003371 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003372
3373 tmp = vmx->guest_msrs[to];
3374 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3375 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003376}
3377
3378/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003379 * Set up the vmcs to automatically save and restore system
3380 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3381 * mode, as fiddling with msrs is very expensive.
3382 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003383static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003384{
Avi Kivity26bb0982009-09-07 11:14:12 +03003385 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003386
Eddie Donga75beee2007-05-17 18:55:15 +03003387 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003388#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003389 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003390 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003391 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003392 move_msr_up(vmx, index, save_nmsrs++);
3393 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003394 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003395 move_msr_up(vmx, index, save_nmsrs++);
3396 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003397 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003398 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003399 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003400 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003401 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003402 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003403 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003404 * if efer.sce is enabled.
3405 */
Brian Gerst8c065852010-07-17 09:03:26 -04003406 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003407 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003408 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003409 }
Eddie Donga75beee2007-05-17 18:55:15 +03003410#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003411 index = __find_msr_index(vmx, MSR_EFER);
3412 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003413 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003414
Avi Kivity26bb0982009-09-07 11:14:12 +03003415 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003416
Yang Zhang8d146952013-01-25 10:18:50 +08003417 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003418 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003419}
3420
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003421static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003423 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003425 if (is_guest_mode(vcpu) &&
3426 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3427 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3428
3429 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430}
3431
3432/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003433 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003435static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003437 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003438 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003439 * We're here if L1 chose not to trap WRMSR to TSC. According
3440 * to the spec, this should set L1's TSC; The offset that L1
3441 * set for L2 remains unchanged, and still needs to be added
3442 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003443 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003444 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003445 /* recalculate vmcs02.TSC_OFFSET: */
3446 vmcs12 = get_vmcs12(vcpu);
3447 vmcs_write64(TSC_OFFSET, offset +
3448 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3449 vmcs12->tsc_offset : 0));
3450 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003451 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3452 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003453 vmcs_write64(TSC_OFFSET, offset);
3454 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455}
3456
Nadav Har'El801d3422011-05-25 23:02:23 +03003457/*
3458 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3459 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3460 * all guests if the "nested" module option is off, and can also be disabled
3461 * for a single guest by disabling its VMX cpuid bit.
3462 */
3463static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3464{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003465 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003466}
3467
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003469 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3470 * returned for the various VMX controls MSRs when nested VMX is enabled.
3471 * The same values should also be used to verify that vmcs12 control fields are
3472 * valid during nested entry from L1 to L2.
3473 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3474 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3475 * bit in the high half is on if the corresponding bit in the control field
3476 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003477 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003478static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003479{
Paolo Bonzini13893092018-02-26 13:40:09 +01003480 if (!nested) {
3481 memset(msrs, 0, sizeof(*msrs));
3482 return;
3483 }
3484
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003485 /*
3486 * Note that as a general rule, the high half of the MSRs (bits in
3487 * the control fields which may be 1) should be initialized by the
3488 * intersection of the underlying hardware's MSR (i.e., features which
3489 * can be supported) and the list of features we want to expose -
3490 * because they are known to be properly supported in our code.
3491 * Also, usually, the low half of the MSRs (bits which must be 1) can
3492 * be set to 0, meaning that L1 may turn off any of these bits. The
3493 * reason is that if one of these bits is necessary, it will appear
3494 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3495 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003496 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003497 * These rules have exceptions below.
3498 */
3499
3500 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003501 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502 msrs->pinbased_ctls_low,
3503 msrs->pinbased_ctls_high);
3504 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003505 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003506 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003507 PIN_BASED_EXT_INTR_MASK |
3508 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003509 PIN_BASED_VIRTUAL_NMIS |
3510 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003511 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003512 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003513 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003514
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003515 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003516 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003517 msrs->exit_ctls_low,
3518 msrs->exit_ctls_high);
3519 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003520 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003521
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003522 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003523#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003524 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003525#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003526 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003527 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003528 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003529 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003530 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3531
Jan Kiszka2996fca2014-06-16 13:59:43 +02003532 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003534
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003535 /* entry controls */
3536 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->entry_ctls_low,
3538 msrs->entry_ctls_high);
3539 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003540 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003542#ifdef CONFIG_X86_64
3543 VM_ENTRY_IA32E_MODE |
3544#endif
3545 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003547 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003548
Jan Kiszka2996fca2014-06-16 13:59:43 +02003549 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003550 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003551
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003552 /* cpu-based controls */
3553 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003554 msrs->procbased_ctls_low,
3555 msrs->procbased_ctls_high);
3556 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003557 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003558 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003559 CPU_BASED_VIRTUAL_INTR_PENDING |
3560 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003561 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3562 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3563 CPU_BASED_CR3_STORE_EXITING |
3564#ifdef CONFIG_X86_64
3565 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3566#endif
3567 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003568 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3569 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3570 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3571 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003572 /*
3573 * We can allow some features even when not supported by the
3574 * hardware. For example, L1 can specify an MSR bitmap - and we
3575 * can use it to avoid exits to L1 - even when L0 runs L2
3576 * without MSR bitmaps.
3577 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003579 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003580 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003581
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003582 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003584 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3585
Paolo Bonzini80154d72017-08-24 13:55:35 +02003586 /*
3587 * secondary cpu-based controls. Do not include those that
3588 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3589 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003590 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003591 msrs->secondary_ctls_low,
3592 msrs->secondary_ctls_high);
3593 msrs->secondary_ctls_low = 0;
3594 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003595 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003596 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003598 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003599 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003600
Liran Alon32c7acf2018-06-23 02:35:11 +03003601 /*
3602 * We can emulate "VMCS shadowing," even if the hardware
3603 * doesn't support it.
3604 */
3605 msrs->secondary_ctls_high |=
3606 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003607
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003608 if (enable_ept) {
3609 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003611 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003613 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003614 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003616 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003617 msrs->ept_caps &= vmx_capability.ept;
3618 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003619 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3620 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003621 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003622 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003623 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003625 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003626 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003627
Bandan Das27c42a12017-08-03 15:54:42 -04003628 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003629 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003630 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003631 /*
3632 * Advertise EPTP switching unconditionally
3633 * since we emulate it
3634 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003635 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003636 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003637 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003638 }
3639
Paolo Bonzinief697a72016-03-18 16:58:38 +01003640 /*
3641 * Old versions of KVM use the single-context version without
3642 * checking for support, so declare that it is supported even
3643 * though it is treated as global context. The alternative is
3644 * not failing the single-context invvpid, and it is worse.
3645 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003646 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003648 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003650 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003651 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003652
Radim Krčmář0790ec12015-03-17 14:02:32 +01003653 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003655 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3656
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003657 if (flexpriority_enabled)
3658 msrs->secondary_ctls_high |=
3659 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3660
Jan Kiszkac18911a2013-03-13 16:06:41 +01003661 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003662 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 msrs->misc_low,
3664 msrs->misc_high);
3665 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3666 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003667 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003668 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003669 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003671
3672 /*
3673 * This MSR reports some information about VMX support. We
3674 * should return information about the VMX we emulate for the
3675 * guest, and the VMCS structure we give it - not about the
3676 * VMX support of the underlying hardware.
3677 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003679 VMCS12_REVISION |
3680 VMX_BASIC_TRUE_CTLS |
3681 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3682 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3683
3684 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003686
3687 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003688 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003689 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3690 * We picked the standard core2 setting.
3691 */
3692#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3693#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3695 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003696
3697 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003698 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3699 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003700
3701 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703}
3704
David Matlack38991522016-11-29 18:14:08 -08003705/*
3706 * if fixed0[i] == 1: val[i] must be 1
3707 * if fixed1[i] == 0: val[i] must be 0
3708 */
3709static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3710{
3711 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003712}
3713
3714static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3715{
David Matlack38991522016-11-29 18:14:08 -08003716 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717}
3718
3719static inline u64 vmx_control_msr(u32 low, u32 high)
3720{
3721 return low | ((u64)high << 32);
3722}
3723
David Matlack62cc6b9d2016-11-29 18:14:07 -08003724static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3725{
3726 superset &= mask;
3727 subset &= mask;
3728
3729 return (superset | subset) == superset;
3730}
3731
3732static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3733{
3734 const u64 feature_and_reserved =
3735 /* feature (except bit 48; see below) */
3736 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3737 /* reserved */
3738 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003740
3741 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3742 return -EINVAL;
3743
3744 /*
3745 * KVM does not emulate a version of VMX that constrains physical
3746 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3747 */
3748 if (data & BIT_ULL(48))
3749 return -EINVAL;
3750
3751 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3752 vmx_basic_vmcs_revision_id(data))
3753 return -EINVAL;
3754
3755 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3756 return -EINVAL;
3757
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003759 return 0;
3760}
3761
3762static int
3763vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3764{
3765 u64 supported;
3766 u32 *lowp, *highp;
3767
3768 switch (msr_index) {
3769 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3771 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772 break;
3773 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 lowp = &vmx->nested.msrs.procbased_ctls_low;
3775 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003776 break;
3777 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 lowp = &vmx->nested.msrs.exit_ctls_low;
3779 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003780 break;
3781 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003782 lowp = &vmx->nested.msrs.entry_ctls_low;
3783 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003784 break;
3785 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 lowp = &vmx->nested.msrs.secondary_ctls_low;
3787 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003788 break;
3789 default:
3790 BUG();
3791 }
3792
3793 supported = vmx_control_msr(*lowp, *highp);
3794
3795 /* Check must-be-1 bits are still 1. */
3796 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3797 return -EINVAL;
3798
3799 /* Check must-be-0 bits are still 0. */
3800 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3801 return -EINVAL;
3802
3803 *lowp = data;
3804 *highp = data >> 32;
3805 return 0;
3806}
3807
3808static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3809{
3810 const u64 feature_and_reserved_bits =
3811 /* feature */
3812 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3813 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3814 /* reserved */
3815 GENMASK_ULL(13, 9) | BIT_ULL(31);
3816 u64 vmx_misc;
3817
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003818 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3819 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003820
3821 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3822 return -EINVAL;
3823
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003824 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003825 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3826 vmx_misc_preemption_timer_rate(data) !=
3827 vmx_misc_preemption_timer_rate(vmx_misc))
3828 return -EINVAL;
3829
3830 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3831 return -EINVAL;
3832
3833 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3834 return -EINVAL;
3835
3836 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3837 return -EINVAL;
3838
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003839 vmx->nested.msrs.misc_low = data;
3840 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003841
3842 /*
3843 * If L1 has read-only VM-exit information fields, use the
3844 * less permissive vmx_vmwrite_bitmap to specify write
3845 * permissions for the shadow VMCS.
3846 */
3847 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3848 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3849
David Matlack62cc6b9d2016-11-29 18:14:07 -08003850 return 0;
3851}
3852
3853static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3854{
3855 u64 vmx_ept_vpid_cap;
3856
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003857 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3858 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003859
3860 /* Every bit is either reserved or a feature bit. */
3861 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3862 return -EINVAL;
3863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003864 vmx->nested.msrs.ept_caps = data;
3865 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003866 return 0;
3867}
3868
3869static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3870{
3871 u64 *msr;
3872
3873 switch (msr_index) {
3874 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003876 break;
3877 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003878 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003879 break;
3880 default:
3881 BUG();
3882 }
3883
3884 /*
3885 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3886 * must be 1 in the restored value.
3887 */
3888 if (!is_bitwise_subset(data, *msr, -1ULL))
3889 return -EINVAL;
3890
3891 *msr = data;
3892 return 0;
3893}
3894
3895/*
3896 * Called when userspace is restoring VMX MSRs.
3897 *
3898 * Returns 0 on success, non-0 otherwise.
3899 */
3900static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3901{
3902 struct vcpu_vmx *vmx = to_vmx(vcpu);
3903
Jim Mattsona943ac52018-05-29 09:11:32 -07003904 /*
3905 * Don't allow changes to the VMX capability MSRs while the vCPU
3906 * is in VMX operation.
3907 */
3908 if (vmx->nested.vmxon)
3909 return -EBUSY;
3910
David Matlack62cc6b9d2016-11-29 18:14:07 -08003911 switch (msr_index) {
3912 case MSR_IA32_VMX_BASIC:
3913 return vmx_restore_vmx_basic(vmx, data);
3914 case MSR_IA32_VMX_PINBASED_CTLS:
3915 case MSR_IA32_VMX_PROCBASED_CTLS:
3916 case MSR_IA32_VMX_EXIT_CTLS:
3917 case MSR_IA32_VMX_ENTRY_CTLS:
3918 /*
3919 * The "non-true" VMX capability MSRs are generated from the
3920 * "true" MSRs, so we do not support restoring them directly.
3921 *
3922 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3923 * should restore the "true" MSRs with the must-be-1 bits
3924 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3925 * DEFAULT SETTINGS".
3926 */
3927 return -EINVAL;
3928 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3929 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3930 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3931 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3932 case MSR_IA32_VMX_PROCBASED_CTLS2:
3933 return vmx_restore_control_msr(vmx, msr_index, data);
3934 case MSR_IA32_VMX_MISC:
3935 return vmx_restore_vmx_misc(vmx, data);
3936 case MSR_IA32_VMX_CR0_FIXED0:
3937 case MSR_IA32_VMX_CR4_FIXED0:
3938 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3939 case MSR_IA32_VMX_CR0_FIXED1:
3940 case MSR_IA32_VMX_CR4_FIXED1:
3941 /*
3942 * These MSRs are generated based on the vCPU's CPUID, so we
3943 * do not support restoring them directly.
3944 */
3945 return -EINVAL;
3946 case MSR_IA32_VMX_EPT_VPID_CAP:
3947 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3948 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003949 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003950 return 0;
3951 default:
3952 /*
3953 * The rest of the VMX capability MSRs do not support restore.
3954 */
3955 return -EINVAL;
3956 }
3957}
3958
Jan Kiszkacae50132014-01-04 18:47:22 +01003959/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003960static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003961{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003962 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003963 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003964 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003965 break;
3966 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3967 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003968 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003969 msrs->pinbased_ctls_low,
3970 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003971 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3972 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003973 break;
3974 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3975 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003976 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003977 msrs->procbased_ctls_low,
3978 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003979 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3980 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003981 break;
3982 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3983 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003984 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003985 msrs->exit_ctls_low,
3986 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003987 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3988 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003989 break;
3990 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3991 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003992 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003993 msrs->entry_ctls_low,
3994 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003995 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3996 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003999 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004000 msrs->misc_low,
4001 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004003 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004010 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004013 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
4015 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004016 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004017 break;
4018 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004019 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004020 msrs->secondary_ctls_low,
4021 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 break;
4023 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004024 *pdata = msrs->ept_caps |
4025 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004027 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004028 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004029 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004030 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004031 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004032 }
4033
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004034 return 0;
4035}
4036
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004037static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4038 uint64_t val)
4039{
4040 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4041
4042 return !(val & ~valid_bits);
4043}
4044
Tom Lendacky801e4592018-02-21 13:39:51 -06004045static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4046{
Paolo Bonzini13893092018-02-26 13:40:09 +01004047 switch (msr->index) {
4048 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4049 if (!nested)
4050 return 1;
4051 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4052 default:
4053 return 1;
4054 }
4055
4056 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004057}
4058
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004059/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060 * Reads an msr value (of 'msr_index') into 'pdata'.
4061 * Returns 0 on success, non-0 otherwise.
4062 * Assumes vcpu_load() was already called.
4063 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004066 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004067 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004070#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004072 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
4074 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004075 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004077 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004078 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004079 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004080#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004082 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004083 case MSR_IA32_SPEC_CTRL:
4084 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004085 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4086 return 1;
4087
4088 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4089 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004090 case MSR_IA32_ARCH_CAPABILITIES:
4091 if (!msr_info->host_initiated &&
4092 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4093 return 1;
4094 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4095 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
4099 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 break;
4102 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004103 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004105 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004106 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004107 (!msr_info->host_initiated &&
4108 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004109 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004110 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004111 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004112 case MSR_IA32_MCG_EXT_CTL:
4113 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004114 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004115 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004116 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004117 msr_info->data = vcpu->arch.mcg_ext_ctl;
4118 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004119 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004120 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004121 break;
4122 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4123 if (!nested_vmx_allowed(vcpu))
4124 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004125 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4126 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004127 case MSR_IA32_XSS:
4128 if (!vmx_xsaves_supported())
4129 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004130 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004131 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004132 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004133 if (!msr_info->host_initiated &&
4134 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004135 return 1;
4136 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004138 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004139 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004143 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 }
4145
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 return 0;
4147}
4148
Jan Kiszkacae50132014-01-04 18:47:22 +01004149static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151/*
4152 * Writes msr value into into the appropriate "register".
4153 * Returns 0 on success, non-0 otherwise.
4154 * Assumes vcpu_load() was already called.
4155 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004156static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004158 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004159 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004160 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004161 u32 msr_index = msr_info->index;
4162 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004163
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004165 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004166 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004167 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004168#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004170 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 vmcs_writel(GUEST_FS_BASE, data);
4172 break;
4173 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004174 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 vmcs_writel(GUEST_GS_BASE, data);
4176 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004177 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004178 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180#endif
4181 case MSR_IA32_SYSENTER_CS:
4182 vmcs_write32(GUEST_SYSENTER_CS, data);
4183 break;
4184 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004185 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 break;
4187 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004188 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004190 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004191 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004192 (!msr_info->host_initiated &&
4193 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004194 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004195 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004196 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004198 vmcs_write64(GUEST_BNDCFGS, data);
4199 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004200 case MSR_IA32_SPEC_CTRL:
4201 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004202 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4203 return 1;
4204
4205 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004206 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004207 return 1;
4208
4209 vmx->spec_ctrl = data;
4210
4211 if (!data)
4212 break;
4213
4214 /*
4215 * For non-nested:
4216 * When it's written (to non-zero) for the first time, pass
4217 * it through.
4218 *
4219 * For nested:
4220 * The handling of the MSR bitmap for L2 guests is done in
4221 * nested_vmx_merge_msr_bitmap. We should not touch the
4222 * vmcs02.msr_bitmap here since it gets completely overwritten
4223 * in the merging. We update the vmcs01 here for L1 as well
4224 * since it will end up touching the MSR anyway now.
4225 */
4226 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4227 MSR_IA32_SPEC_CTRL,
4228 MSR_TYPE_RW);
4229 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004230 case MSR_IA32_PRED_CMD:
4231 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004232 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4233 return 1;
4234
4235 if (data & ~PRED_CMD_IBPB)
4236 return 1;
4237
4238 if (!data)
4239 break;
4240
4241 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4242
4243 /*
4244 * For non-nested:
4245 * When it's written (to non-zero) for the first time, pass
4246 * it through.
4247 *
4248 * For nested:
4249 * The handling of the MSR bitmap for L2 guests is done in
4250 * nested_vmx_merge_msr_bitmap. We should not touch the
4251 * vmcs02.msr_bitmap here since it gets completely overwritten
4252 * in the merging.
4253 */
4254 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4255 MSR_TYPE_W);
4256 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004257 case MSR_IA32_ARCH_CAPABILITIES:
4258 if (!msr_info->host_initiated)
4259 return 1;
4260 vmx->arch_capabilities = data;
4261 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004262 case MSR_IA32_CR_PAT:
4263 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004264 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4265 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004266 vmcs_write64(GUEST_IA32_PAT, data);
4267 vcpu->arch.pat = data;
4268 break;
4269 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004270 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004271 break;
Will Auldba904632012-11-29 12:42:50 -08004272 case MSR_IA32_TSC_ADJUST:
4273 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004274 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004275 case MSR_IA32_MCG_EXT_CTL:
4276 if ((!msr_info->host_initiated &&
4277 !(to_vmx(vcpu)->msr_ia32_feature_control &
4278 FEATURE_CONTROL_LMCE)) ||
4279 (data & ~MCG_EXT_CTL_LMCE_EN))
4280 return 1;
4281 vcpu->arch.mcg_ext_ctl = data;
4282 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004283 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004284 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004285 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004286 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4287 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004288 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004289 if (msr_info->host_initiated && data == 0)
4290 vmx_leave_nested(vcpu);
4291 break;
4292 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004293 if (!msr_info->host_initiated)
4294 return 1; /* they are read-only */
4295 if (!nested_vmx_allowed(vcpu))
4296 return 1;
4297 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004298 case MSR_IA32_XSS:
4299 if (!vmx_xsaves_supported())
4300 return 1;
4301 /*
4302 * The only supported bit as of Skylake is bit 8, but
4303 * it is not supported on KVM.
4304 */
4305 if (data != 0)
4306 return 1;
4307 vcpu->arch.ia32_xss = data;
4308 if (vcpu->arch.ia32_xss != host_xss)
4309 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004310 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004311 else
4312 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4313 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004314 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004315 if (!msr_info->host_initiated &&
4316 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004317 return 1;
4318 /* Check reserved bit, higher 32 bits should be zero */
4319 if ((data >> 32) != 0)
4320 return 1;
4321 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004323 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004324 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004325 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004326 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004327 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4328 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004329 ret = kvm_set_shared_msr(msr->index, msr->data,
4330 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004331 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004332 if (ret)
4333 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004334 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004335 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004337 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 }
4339
Eddie Dong2cc51562007-05-21 07:28:09 +03004340 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341}
4342
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004343static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004345 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4346 switch (reg) {
4347 case VCPU_REGS_RSP:
4348 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4349 break;
4350 case VCPU_REGS_RIP:
4351 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4352 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004353 case VCPU_EXREG_PDPTR:
4354 if (enable_ept)
4355 ept_save_pdptrs(vcpu);
4356 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004357 default:
4358 break;
4359 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360}
4361
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362static __init int cpu_has_kvm_support(void)
4363{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004364 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
4367static __init int vmx_disabled_by_bios(void)
4368{
4369 u64 msr;
4370
4371 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004372 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004374 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4375 && tboot_enabled())
4376 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004377 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004378 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004379 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004380 && !tboot_enabled()) {
4381 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004382 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004383 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004384 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004385 /* launched w/o TXT and VMX disabled */
4386 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4387 && !tboot_enabled())
4388 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004389 }
4390
4391 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392}
4393
Dongxiao Xu7725b892010-05-11 18:29:38 +08004394static void kvm_cpu_vmxon(u64 addr)
4395{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004396 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004397 intel_pt_handle_vmx(1);
4398
Dongxiao Xu7725b892010-05-11 18:29:38 +08004399 asm volatile (ASM_VMX_VMXON_RAX
4400 : : "a"(&addr), "m"(addr)
4401 : "memory", "cc");
4402}
4403
Radim Krčmář13a34e02014-08-28 15:13:03 +02004404static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405{
4406 int cpu = raw_smp_processor_id();
4407 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004408 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004410 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004411 return -EBUSY;
4412
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004413 /*
4414 * This can happen if we hot-added a CPU but failed to allocate
4415 * VP assist page for it.
4416 */
4417 if (static_branch_unlikely(&enable_evmcs) &&
4418 !hv_get_vp_assist_page(cpu))
4419 return -EFAULT;
4420
Nadav Har'Eld462b812011-05-24 15:26:10 +03004421 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004422 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4423 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004424
4425 /*
4426 * Now we can enable the vmclear operation in kdump
4427 * since the loaded_vmcss_on_cpu list on this cpu
4428 * has been initialized.
4429 *
4430 * Though the cpu is not in VMX operation now, there
4431 * is no problem to enable the vmclear operation
4432 * for the loaded_vmcss_on_cpu list is empty!
4433 */
4434 crash_enable_local_vmclear(cpu);
4435
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004437
4438 test_bits = FEATURE_CONTROL_LOCKED;
4439 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4440 if (tboot_enabled())
4441 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4442
4443 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004445 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4446 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004447 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004448 if (enable_ept)
4449 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004450
4451 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452}
4453
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004455{
4456 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004457 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004458
Nadav Har'Eld462b812011-05-24 15:26:10 +03004459 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4460 loaded_vmcss_on_cpu_link)
4461 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004462}
4463
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004464
4465/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4466 * tricks.
4467 */
4468static void kvm_cpu_vmxoff(void)
4469{
4470 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004471
4472 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004473 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004474}
4475
Radim Krčmář13a34e02014-08-28 15:13:03 +02004476static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004478 vmclear_local_loaded_vmcss();
4479 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480}
4481
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004482static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004483 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484{
4485 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004486 u32 ctl = ctl_min | ctl_opt;
4487
4488 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4489
4490 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4491 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4492
4493 /* Ensure minimum (required) set of control bits are supported. */
4494 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004495 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004496
4497 *result = ctl;
4498 return 0;
4499}
4500
Avi Kivity110312c2010-12-21 12:54:20 +02004501static __init bool allow_1_setting(u32 msr, u32 ctl)
4502{
4503 u32 vmx_msr_low, vmx_msr_high;
4504
4505 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4506 return vmx_msr_high & ctl;
4507}
4508
Yang, Sheng002c7f72007-07-31 14:23:01 +03004509static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510{
4511 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004512 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004513 u32 _pin_based_exec_control = 0;
4514 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004515 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516 u32 _vmexit_control = 0;
4517 u32 _vmentry_control = 0;
4518
Paolo Bonzini13893092018-02-26 13:40:09 +01004519 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304520 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004521#ifdef CONFIG_X86_64
4522 CPU_BASED_CR8_LOAD_EXITING |
4523 CPU_BASED_CR8_STORE_EXITING |
4524#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004525 CPU_BASED_CR3_LOAD_EXITING |
4526 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004527 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004528 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004529 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004530 CPU_BASED_MWAIT_EXITING |
4531 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004532 CPU_BASED_INVLPG_EXITING |
4533 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004534
Sheng Yangf78e0e22007-10-29 09:40:42 +08004535 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004536 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004537 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004538 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4539 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004540 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004541#ifdef CONFIG_X86_64
4542 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4543 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4544 ~CPU_BASED_CR8_STORE_EXITING;
4545#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004546 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004547 min2 = 0;
4548 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004549 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004550 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004551 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004552 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004553 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004554 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004555 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004556 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004557 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004558 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004559 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004560 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004561 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004562 SECONDARY_EXEC_RDSEED_EXITING |
4563 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004564 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004565 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004566 SECONDARY_EXEC_ENABLE_VMFUNC |
4567 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004568 if (adjust_vmx_controls(min2, opt2,
4569 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004570 &_cpu_based_2nd_exec_control) < 0)
4571 return -EIO;
4572 }
4573#ifndef CONFIG_X86_64
4574 if (!(_cpu_based_2nd_exec_control &
4575 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4576 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4577#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004578
4579 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4580 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004581 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004582 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4583 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004584
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004585 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4586 &vmx_capability.ept, &vmx_capability.vpid);
4587
Sheng Yangd56f5462008-04-25 10:13:16 +08004588 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004589 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4590 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004591 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4592 CPU_BASED_CR3_STORE_EXITING |
4593 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004594 } else if (vmx_capability.ept) {
4595 vmx_capability.ept = 0;
4596 pr_warn_once("EPT CAP should not exist if not support "
4597 "1-setting enable EPT VM-execution control\n");
4598 }
4599 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4600 vmx_capability.vpid) {
4601 vmx_capability.vpid = 0;
4602 pr_warn_once("VPID CAP should not exist if not support "
4603 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004604 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004605
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004606 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004607#ifdef CONFIG_X86_64
4608 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4609#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004610 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004611 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004612 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4613 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004614 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004615
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004616 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4617 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4618 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004619 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4620 &_pin_based_exec_control) < 0)
4621 return -EIO;
4622
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004623 if (cpu_has_broken_vmx_preemption_timer())
4624 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004625 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004626 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004627 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4628
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004629 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004630 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4632 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004633 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004635 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004636
4637 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4638 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004639 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004640
4641#ifdef CONFIG_X86_64
4642 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4643 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645#endif
4646
4647 /* Require Write-Back (WB) memory type for VMCS accesses. */
4648 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650
Yang, Sheng002c7f72007-07-31 14:23:01 +03004651 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004652 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004653 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004654
Liran Alon2307af12018-06-29 22:59:04 +03004655 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004656
Yang, Sheng002c7f72007-07-31 14:23:01 +03004657 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4658 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004659 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004660 vmcs_conf->vmexit_ctrl = _vmexit_control;
4661 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004662
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004663 if (static_branch_unlikely(&enable_evmcs))
4664 evmcs_sanitize_exec_ctrls(vmcs_conf);
4665
Avi Kivity110312c2010-12-21 12:54:20 +02004666 cpu_has_load_ia32_efer =
4667 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4668 VM_ENTRY_LOAD_IA32_EFER)
4669 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4670 VM_EXIT_LOAD_IA32_EFER);
4671
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004672 cpu_has_load_perf_global_ctrl =
4673 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4674 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4675 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4676 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4677
4678 /*
4679 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004680 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004681 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4682 *
4683 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4684 *
4685 * AAK155 (model 26)
4686 * AAP115 (model 30)
4687 * AAT100 (model 37)
4688 * BC86,AAY89,BD102 (model 44)
4689 * BA97 (model 46)
4690 *
4691 */
4692 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4693 switch (boot_cpu_data.x86_model) {
4694 case 26:
4695 case 30:
4696 case 37:
4697 case 44:
4698 case 46:
4699 cpu_has_load_perf_global_ctrl = false;
4700 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4701 "does not work properly. Using workaround\n");
4702 break;
4703 default:
4704 break;
4705 }
4706 }
4707
Borislav Petkov782511b2016-04-04 22:25:03 +02004708 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004709 rdmsrl(MSR_IA32_XSS, host_xss);
4710
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004711 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004712}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713
Liran Alon491a6032018-06-23 02:35:12 +03004714static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715{
4716 int node = cpu_to_node(cpu);
4717 struct page *pages;
4718 struct vmcs *vmcs;
4719
Vlastimil Babka96db8002015-09-08 15:03:50 -07004720 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721 if (!pages)
4722 return NULL;
4723 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004724 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004725
4726 /* KVM supports Enlightened VMCS v1 only */
4727 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004728 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004729 else
Liran Alon392b2f22018-06-23 02:35:01 +03004730 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004731
Liran Alon491a6032018-06-23 02:35:12 +03004732 if (shadow)
4733 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734 return vmcs;
4735}
4736
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737static void free_vmcs(struct vmcs *vmcs)
4738{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004739 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740}
4741
Nadav Har'Eld462b812011-05-24 15:26:10 +03004742/*
4743 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4744 */
4745static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4746{
4747 if (!loaded_vmcs->vmcs)
4748 return;
4749 loaded_vmcs_clear(loaded_vmcs);
4750 free_vmcs(loaded_vmcs->vmcs);
4751 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004752 if (loaded_vmcs->msr_bitmap)
4753 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004754 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004755}
4756
Liran Alon491a6032018-06-23 02:35:12 +03004757static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004758{
Liran Alon491a6032018-06-23 02:35:12 +03004759 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004760}
4761
4762static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4763{
Liran Alon491a6032018-06-23 02:35:12 +03004764 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004765 if (!loaded_vmcs->vmcs)
4766 return -ENOMEM;
4767
4768 loaded_vmcs->shadow_vmcs = NULL;
4769 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004770
4771 if (cpu_has_vmx_msr_bitmap()) {
4772 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4773 if (!loaded_vmcs->msr_bitmap)
4774 goto out_vmcs;
4775 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004776
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004777 if (IS_ENABLED(CONFIG_HYPERV) &&
4778 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004779 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4780 struct hv_enlightened_vmcs *evmcs =
4781 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4782
4783 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4784 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004785 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004786
4787 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4788
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004789 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004790
4791out_vmcs:
4792 free_loaded_vmcs(loaded_vmcs);
4793 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004794}
4795
Sam Ravnborg39959582007-06-01 00:47:13 -07004796static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797{
4798 int cpu;
4799
Zachary Amsden3230bb42009-09-29 11:38:37 -10004800 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004802 per_cpu(vmxarea, cpu) = NULL;
4803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804}
4805
Jim Mattsond37f4262017-12-22 12:12:16 -08004806enum vmcs_field_width {
4807 VMCS_FIELD_WIDTH_U16 = 0,
4808 VMCS_FIELD_WIDTH_U64 = 1,
4809 VMCS_FIELD_WIDTH_U32 = 2,
4810 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004811};
4812
Jim Mattsond37f4262017-12-22 12:12:16 -08004813static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004814{
4815 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004816 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004817 return (field >> 13) & 0x3 ;
4818}
4819
4820static inline int vmcs_field_readonly(unsigned long field)
4821{
4822 return (((field >> 10) & 0x3) == 1);
4823}
4824
Bandan Dasfe2b2012014-04-21 15:20:14 -04004825static void init_vmcs_shadow_fields(void)
4826{
4827 int i, j;
4828
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004829 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4830 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004831 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004832 (i + 1 == max_shadow_read_only_fields ||
4833 shadow_read_only_fields[i + 1] != field + 1))
4834 pr_err("Missing field from shadow_read_only_field %x\n",
4835 field + 1);
4836
4837 clear_bit(field, vmx_vmread_bitmap);
4838#ifdef CONFIG_X86_64
4839 if (field & 1)
4840 continue;
4841#endif
4842 if (j < i)
4843 shadow_read_only_fields[j] = field;
4844 j++;
4845 }
4846 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004847
4848 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004849 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004850 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004851 (i + 1 == max_shadow_read_write_fields ||
4852 shadow_read_write_fields[i + 1] != field + 1))
4853 pr_err("Missing field from shadow_read_write_field %x\n",
4854 field + 1);
4855
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004856 /*
4857 * PML and the preemption timer can be emulated, but the
4858 * processor cannot vmwrite to fields that don't exist
4859 * on bare metal.
4860 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004861 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004862 case GUEST_PML_INDEX:
4863 if (!cpu_has_vmx_pml())
4864 continue;
4865 break;
4866 case VMX_PREEMPTION_TIMER_VALUE:
4867 if (!cpu_has_vmx_preemption_timer())
4868 continue;
4869 break;
4870 case GUEST_INTR_STATUS:
4871 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004872 continue;
4873 break;
4874 default:
4875 break;
4876 }
4877
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004878 clear_bit(field, vmx_vmwrite_bitmap);
4879 clear_bit(field, vmx_vmread_bitmap);
4880#ifdef CONFIG_X86_64
4881 if (field & 1)
4882 continue;
4883#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004884 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004885 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004886 j++;
4887 }
4888 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004889}
4890
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891static __init int alloc_kvm_area(void)
4892{
4893 int cpu;
4894
Zachary Amsden3230bb42009-09-29 11:38:37 -10004895 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 struct vmcs *vmcs;
4897
Liran Alon491a6032018-06-23 02:35:12 +03004898 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 if (!vmcs) {
4900 free_kvm_area();
4901 return -ENOMEM;
4902 }
4903
Liran Alon2307af12018-06-29 22:59:04 +03004904 /*
4905 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4906 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4907 * revision_id reported by MSR_IA32_VMX_BASIC.
4908 *
4909 * However, even though not explictly documented by
4910 * TLFS, VMXArea passed as VMXON argument should
4911 * still be marked with revision_id reported by
4912 * physical CPU.
4913 */
4914 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004915 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004916
Avi Kivity6aa8b732006-12-10 02:21:36 -08004917 per_cpu(vmxarea, cpu) = vmcs;
4918 }
4919 return 0;
4920}
4921
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004922static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004923 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004925 if (!emulate_invalid_guest_state) {
4926 /*
4927 * CS and SS RPL should be equal during guest entry according
4928 * to VMX spec, but in reality it is not always so. Since vcpu
4929 * is in the middle of the transition from real mode to
4930 * protected mode it is safe to assume that RPL 0 is a good
4931 * default value.
4932 */
4933 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004934 save->selector &= ~SEGMENT_RPL_MASK;
4935 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004936 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004938 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939}
4940
4941static void enter_pmode(struct kvm_vcpu *vcpu)
4942{
4943 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004945
Gleb Natapovd99e4152012-12-20 16:57:45 +02004946 /*
4947 * Update real mode segment cache. It may be not up-to-date if sement
4948 * register was written while vcpu was in a guest mode.
4949 */
4950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4952 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4953 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4954 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4955 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4956
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004957 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958
Avi Kivity2fb92db2011-04-27 19:42:18 +03004959 vmx_segment_cache_clear(vmx);
4960
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004961 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962
4963 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004964 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4965 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966 vmcs_writel(GUEST_RFLAGS, flags);
4967
Rusty Russell66aee912007-07-17 23:34:16 +10004968 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4969 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004970
4971 update_exception_bitmap(vcpu);
4972
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004973 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4974 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4975 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4976 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4977 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4978 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979}
4980
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004981static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982{
Mathias Krause772e0312012-08-30 01:30:19 +02004983 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004984 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004985
Gleb Natapovd99e4152012-12-20 16:57:45 +02004986 var.dpl = 0x3;
4987 if (seg == VCPU_SREG_CS)
4988 var.type = 0x3;
4989
4990 if (!emulate_invalid_guest_state) {
4991 var.selector = var.base >> 4;
4992 var.base = var.base & 0xffff0;
4993 var.limit = 0xffff;
4994 var.g = 0;
4995 var.db = 0;
4996 var.present = 1;
4997 var.s = 1;
4998 var.l = 0;
4999 var.unusable = 0;
5000 var.type = 0x3;
5001 var.avl = 0;
5002 if (save->base & 0xf)
5003 printk_once(KERN_WARNING "kvm: segment base is not "
5004 "paragraph aligned when entering "
5005 "protected mode (seg=%d)", seg);
5006 }
5007
5008 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005009 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005010 vmcs_write32(sf->limit, var.limit);
5011 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012}
5013
5014static void enter_rmode(struct kvm_vcpu *vcpu)
5015{
5016 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005017 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005018 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5022 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5023 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5024 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005025 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5026 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005027
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005028 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029
Gleb Natapov776e58e2011-03-13 12:34:27 +02005030 /*
5031 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005032 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005033 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005034 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005035 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5036 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005037
Avi Kivity2fb92db2011-04-27 19:42:18 +03005038 vmx_segment_cache_clear(vmx);
5039
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005040 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5043
5044 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005045 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005047 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048
5049 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005050 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 update_exception_bitmap(vcpu);
5052
Gleb Natapovd99e4152012-12-20 16:57:45 +02005053 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5054 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5055 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5056 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5057 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5058 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005059
Eddie Dong8668a3c2007-10-10 14:26:45 +08005060 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061}
5062
Amit Shah401d10d2009-02-20 22:53:37 +05305063static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5064{
5065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005066 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5067
5068 if (!msr)
5069 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305070
Avi Kivityf6801df2010-01-21 15:31:50 +02005071 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305072 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005073 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305074 msr->data = efer;
5075 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005076 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305077
5078 msr->data = efer & ~EFER_LME;
5079 }
5080 setup_msrs(vmx);
5081}
5082
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005083#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084
5085static void enter_lmode(struct kvm_vcpu *vcpu)
5086{
5087 u32 guest_tr_ar;
5088
Avi Kivity2fb92db2011-04-27 19:42:18 +03005089 vmx_segment_cache_clear(to_vmx(vcpu));
5090
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005092 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005093 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5094 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005096 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5097 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098 }
Avi Kivityda38f432010-07-06 11:30:49 +03005099 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100}
5101
5102static void exit_lmode(struct kvm_vcpu *vcpu)
5103{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005105 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106}
5107
5108#endif
5109
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005110static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5111 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005112{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005113 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005114 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5115 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005116 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005117 } else {
5118 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005119 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005120}
5121
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005122static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005123{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005124 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005125}
5126
Junaid Shahidfaff8752018-06-29 13:10:05 -07005127static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5128{
5129 int vpid = to_vmx(vcpu)->vpid;
5130
5131 if (!vpid_sync_vcpu_addr(vpid, addr))
5132 vpid_sync_context(vpid);
5133
5134 /*
5135 * If VPIDs are not supported or enabled, then the above is a no-op.
5136 * But we don't really need a TLB flush in that case anyway, because
5137 * each VM entry/exit includes an implicit flush when VPID is 0.
5138 */
5139}
5140
Avi Kivitye8467fd2009-12-29 18:43:06 +02005141static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5142{
5143 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5144
5145 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5146 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5147}
5148
Avi Kivityaff48ba2010-12-05 18:56:11 +02005149static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5150{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005151 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005152 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5153 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5154}
5155
Anthony Liguori25c4c272007-04-27 09:29:21 +03005156static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005157{
Avi Kivityfc78f512009-12-07 12:16:48 +02005158 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5159
5160 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5161 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005162}
5163
Sheng Yang14394422008-04-28 12:24:45 +08005164static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5165{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005166 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5167
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005168 if (!test_bit(VCPU_EXREG_PDPTR,
5169 (unsigned long *)&vcpu->arch.regs_dirty))
5170 return;
5171
Sheng Yang14394422008-04-28 12:24:45 +08005172 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005173 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5174 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5175 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5176 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005177 }
5178}
5179
Avi Kivity8f5d5492009-05-31 18:41:29 +03005180static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5181{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005182 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5183
Avi Kivity8f5d5492009-05-31 18:41:29 +03005184 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005185 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5186 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5187 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5188 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005189 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005190
5191 __set_bit(VCPU_EXREG_PDPTR,
5192 (unsigned long *)&vcpu->arch.regs_avail);
5193 __set_bit(VCPU_EXREG_PDPTR,
5194 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005195}
5196
David Matlack38991522016-11-29 18:14:08 -08005197static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5198{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005199 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5200 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005201 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5202
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005203 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005204 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5205 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5206 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5207
5208 return fixed_bits_valid(val, fixed0, fixed1);
5209}
5210
5211static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5212{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005213 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5214 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005215
5216 return fixed_bits_valid(val, fixed0, fixed1);
5217}
5218
5219static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5220{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005221 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5222 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005223
5224 return fixed_bits_valid(val, fixed0, fixed1);
5225}
5226
5227/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5228#define nested_guest_cr4_valid nested_cr4_valid
5229#define nested_host_cr4_valid nested_cr4_valid
5230
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005231static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005232
5233static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5234 unsigned long cr0,
5235 struct kvm_vcpu *vcpu)
5236{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005237 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5238 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005239 if (!(cr0 & X86_CR0_PG)) {
5240 /* From paging/starting to nonpaging */
5241 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005242 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005243 (CPU_BASED_CR3_LOAD_EXITING |
5244 CPU_BASED_CR3_STORE_EXITING));
5245 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005246 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005247 } else if (!is_paging(vcpu)) {
5248 /* From nonpaging to paging */
5249 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005250 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005251 ~(CPU_BASED_CR3_LOAD_EXITING |
5252 CPU_BASED_CR3_STORE_EXITING));
5253 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005254 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005255 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005256
5257 if (!(cr0 & X86_CR0_WP))
5258 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005259}
5260
Avi Kivity6aa8b732006-12-10 02:21:36 -08005261static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5262{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005263 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005264 unsigned long hw_cr0;
5265
Sean Christopherson3de63472018-07-13 08:42:30 -07005266 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005267 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005268 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005269 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005270 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005271
Gleb Natapov218e7632013-01-21 15:36:45 +02005272 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5273 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274
Gleb Natapov218e7632013-01-21 15:36:45 +02005275 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5276 enter_rmode(vcpu);
5277 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005279#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005280 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005281 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005282 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005283 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005284 exit_lmode(vcpu);
5285 }
5286#endif
5287
Sean Christophersonb4d18512018-03-05 12:04:40 -08005288 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005289 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5290
Avi Kivity6aa8b732006-12-10 02:21:36 -08005291 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005292 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005293 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005294
5295 /* depends on vcpu->arch.cr0 to be set to a new value */
5296 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005297}
5298
Yu Zhang855feb62017-08-24 20:27:55 +08005299static int get_ept_level(struct kvm_vcpu *vcpu)
5300{
5301 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5302 return 5;
5303 return 4;
5304}
5305
Peter Feiner995f00a2017-06-30 17:26:32 -07005306static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005307{
Yu Zhang855feb62017-08-24 20:27:55 +08005308 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005309
Yu Zhang855feb62017-08-24 20:27:55 +08005310 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005311
Peter Feiner995f00a2017-06-30 17:26:32 -07005312 if (enable_ept_ad_bits &&
5313 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005314 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005315 eptp |= (root_hpa & PAGE_MASK);
5316
5317 return eptp;
5318}
5319
Avi Kivity6aa8b732006-12-10 02:21:36 -08005320static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5321{
Tianyu Lan877ad952018-07-19 08:40:23 +00005322 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005323 unsigned long guest_cr3;
5324 u64 eptp;
5325
5326 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005327 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005328 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005329 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005330
5331 if (kvm_x86_ops->tlb_remote_flush) {
5332 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5333 to_vmx(vcpu)->ept_pointer = eptp;
5334 to_kvm_vmx(kvm)->ept_pointers_match
5335 = EPT_POINTERS_CHECK;
5336 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5337 }
5338
Sean Christophersone90008d2018-03-05 12:04:37 -08005339 if (enable_unrestricted_guest || is_paging(vcpu) ||
5340 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005341 guest_cr3 = kvm_read_cr3(vcpu);
5342 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005343 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005344 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005345 }
5346
Sheng Yang14394422008-04-28 12:24:45 +08005347 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348}
5349
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005350static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005351{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005352 /*
5353 * Pass through host's Machine Check Enable value to hw_cr4, which
5354 * is in force while we are in guest mode. Do not let guests control
5355 * this bit, even if host CR4.MCE == 0.
5356 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005357 unsigned long hw_cr4;
5358
5359 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5360 if (enable_unrestricted_guest)
5361 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5362 else if (to_vmx(vcpu)->rmode.vm86_active)
5363 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5364 else
5365 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005366
Sean Christopherson64f7a112018-04-30 10:01:06 -07005367 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5368 if (cr4 & X86_CR4_UMIP) {
5369 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005370 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005371 hw_cr4 &= ~X86_CR4_UMIP;
5372 } else if (!is_guest_mode(vcpu) ||
5373 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5374 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5375 SECONDARY_EXEC_DESC);
5376 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005377
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005378 if (cr4 & X86_CR4_VMXE) {
5379 /*
5380 * To use VMXON (and later other VMX instructions), a guest
5381 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5382 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005383 * is here. We operate under the default treatment of SMM,
5384 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005385 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005386 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005387 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005388 }
David Matlack38991522016-11-29 18:14:08 -08005389
5390 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005391 return 1;
5392
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005393 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005394
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005395 if (!enable_unrestricted_guest) {
5396 if (enable_ept) {
5397 if (!is_paging(vcpu)) {
5398 hw_cr4 &= ~X86_CR4_PAE;
5399 hw_cr4 |= X86_CR4_PSE;
5400 } else if (!(cr4 & X86_CR4_PAE)) {
5401 hw_cr4 &= ~X86_CR4_PAE;
5402 }
5403 }
5404
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005405 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005406 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5407 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5408 * to be manually disabled when guest switches to non-paging
5409 * mode.
5410 *
5411 * If !enable_unrestricted_guest, the CPU is always running
5412 * with CR0.PG=1 and CR4 needs to be modified.
5413 * If enable_unrestricted_guest, the CPU automatically
5414 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005415 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005416 if (!is_paging(vcpu))
5417 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5418 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005419
Sheng Yang14394422008-04-28 12:24:45 +08005420 vmcs_writel(CR4_READ_SHADOW, cr4);
5421 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005422 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005423}
5424
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425static void vmx_get_segment(struct kvm_vcpu *vcpu,
5426 struct kvm_segment *var, int seg)
5427{
Avi Kivitya9179492011-01-03 14:28:52 +02005428 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005429 u32 ar;
5430
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005431 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005432 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005433 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005434 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005435 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005436 var->base = vmx_read_guest_seg_base(vmx, seg);
5437 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5438 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005439 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005440 var->base = vmx_read_guest_seg_base(vmx, seg);
5441 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5442 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5443 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005444 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005445 var->type = ar & 15;
5446 var->s = (ar >> 4) & 1;
5447 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005448 /*
5449 * Some userspaces do not preserve unusable property. Since usable
5450 * segment has to be present according to VMX spec we can use present
5451 * property to amend userspace bug by making unusable segment always
5452 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5453 * segment as unusable.
5454 */
5455 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005456 var->avl = (ar >> 12) & 1;
5457 var->l = (ar >> 13) & 1;
5458 var->db = (ar >> 14) & 1;
5459 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005460}
5461
Avi Kivitya9179492011-01-03 14:28:52 +02005462static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5463{
Avi Kivitya9179492011-01-03 14:28:52 +02005464 struct kvm_segment s;
5465
5466 if (to_vmx(vcpu)->rmode.vm86_active) {
5467 vmx_get_segment(vcpu, &s, seg);
5468 return s.base;
5469 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005470 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005471}
5472
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005473static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005474{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005475 struct vcpu_vmx *vmx = to_vmx(vcpu);
5476
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005477 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005478 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005479 else {
5480 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005481 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005482 }
Avi Kivity69c73022011-03-07 15:26:44 +02005483}
5484
Avi Kivity653e3102007-05-07 10:55:37 +03005485static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005486{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487 u32 ar;
5488
Avi Kivityf0495f92012-06-07 17:06:10 +03005489 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005490 ar = 1 << 16;
5491 else {
5492 ar = var->type & 15;
5493 ar |= (var->s & 1) << 4;
5494 ar |= (var->dpl & 3) << 5;
5495 ar |= (var->present & 1) << 7;
5496 ar |= (var->avl & 1) << 12;
5497 ar |= (var->l & 1) << 13;
5498 ar |= (var->db & 1) << 14;
5499 ar |= (var->g & 1) << 15;
5500 }
Avi Kivity653e3102007-05-07 10:55:37 +03005501
5502 return ar;
5503}
5504
5505static void vmx_set_segment(struct kvm_vcpu *vcpu,
5506 struct kvm_segment *var, int seg)
5507{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005508 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005509 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005510
Avi Kivity2fb92db2011-04-27 19:42:18 +03005511 vmx_segment_cache_clear(vmx);
5512
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005513 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5514 vmx->rmode.segs[seg] = *var;
5515 if (seg == VCPU_SREG_TR)
5516 vmcs_write16(sf->selector, var->selector);
5517 else if (var->s)
5518 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005519 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005520 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005521
Avi Kivity653e3102007-05-07 10:55:37 +03005522 vmcs_writel(sf->base, var->base);
5523 vmcs_write32(sf->limit, var->limit);
5524 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005525
5526 /*
5527 * Fix the "Accessed" bit in AR field of segment registers for older
5528 * qemu binaries.
5529 * IA32 arch specifies that at the time of processor reset the
5530 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005531 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005532 * state vmexit when "unrestricted guest" mode is turned on.
5533 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5534 * tree. Newer qemu binaries with that qemu fix would not need this
5535 * kvm hack.
5536 */
5537 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005538 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005539
Gleb Natapovf924d662012-12-12 19:10:55 +02005540 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005541
5542out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005543 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005544}
5545
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5547{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005548 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549
5550 *db = (ar >> 14) & 1;
5551 *l = (ar >> 13) & 1;
5552}
5553
Gleb Natapov89a27f42010-02-16 10:51:48 +02005554static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005556 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5557 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558}
5559
Gleb Natapov89a27f42010-02-16 10:51:48 +02005560static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005562 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5563 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564}
5565
Gleb Natapov89a27f42010-02-16 10:51:48 +02005566static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005568 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5569 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570}
5571
Gleb Natapov89a27f42010-02-16 10:51:48 +02005572static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005574 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5575 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005576}
5577
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005578static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5579{
5580 struct kvm_segment var;
5581 u32 ar;
5582
5583 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005584 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005585 if (seg == VCPU_SREG_CS)
5586 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005587 ar = vmx_segment_access_rights(&var);
5588
5589 if (var.base != (var.selector << 4))
5590 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005591 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005592 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005593 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005594 return false;
5595
5596 return true;
5597}
5598
5599static bool code_segment_valid(struct kvm_vcpu *vcpu)
5600{
5601 struct kvm_segment cs;
5602 unsigned int cs_rpl;
5603
5604 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005605 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005606
Avi Kivity1872a3f2009-01-04 23:26:52 +02005607 if (cs.unusable)
5608 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005609 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005610 return false;
5611 if (!cs.s)
5612 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005613 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005614 if (cs.dpl > cs_rpl)
5615 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005616 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005617 if (cs.dpl != cs_rpl)
5618 return false;
5619 }
5620 if (!cs.present)
5621 return false;
5622
5623 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5624 return true;
5625}
5626
5627static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5628{
5629 struct kvm_segment ss;
5630 unsigned int ss_rpl;
5631
5632 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005633 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005634
Avi Kivity1872a3f2009-01-04 23:26:52 +02005635 if (ss.unusable)
5636 return true;
5637 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005638 return false;
5639 if (!ss.s)
5640 return false;
5641 if (ss.dpl != ss_rpl) /* DPL != RPL */
5642 return false;
5643 if (!ss.present)
5644 return false;
5645
5646 return true;
5647}
5648
5649static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5650{
5651 struct kvm_segment var;
5652 unsigned int rpl;
5653
5654 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005655 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005656
Avi Kivity1872a3f2009-01-04 23:26:52 +02005657 if (var.unusable)
5658 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005659 if (!var.s)
5660 return false;
5661 if (!var.present)
5662 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005663 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005664 if (var.dpl < rpl) /* DPL < RPL */
5665 return false;
5666 }
5667
5668 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5669 * rights flags
5670 */
5671 return true;
5672}
5673
5674static bool tr_valid(struct kvm_vcpu *vcpu)
5675{
5676 struct kvm_segment tr;
5677
5678 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5679
Avi Kivity1872a3f2009-01-04 23:26:52 +02005680 if (tr.unusable)
5681 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005682 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005683 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005684 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005685 return false;
5686 if (!tr.present)
5687 return false;
5688
5689 return true;
5690}
5691
5692static bool ldtr_valid(struct kvm_vcpu *vcpu)
5693{
5694 struct kvm_segment ldtr;
5695
5696 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5697
Avi Kivity1872a3f2009-01-04 23:26:52 +02005698 if (ldtr.unusable)
5699 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005700 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005701 return false;
5702 if (ldtr.type != 2)
5703 return false;
5704 if (!ldtr.present)
5705 return false;
5706
5707 return true;
5708}
5709
5710static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5711{
5712 struct kvm_segment cs, ss;
5713
5714 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5715 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5716
Nadav Amitb32a9912015-03-29 16:33:04 +03005717 return ((cs.selector & SEGMENT_RPL_MASK) ==
5718 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005719}
5720
5721/*
5722 * Check if guest state is valid. Returns true if valid, false if
5723 * not.
5724 * We assume that registers are always usable
5725 */
5726static bool guest_state_valid(struct kvm_vcpu *vcpu)
5727{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005728 if (enable_unrestricted_guest)
5729 return true;
5730
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005731 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005732 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005733 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5734 return false;
5735 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5736 return false;
5737 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5738 return false;
5739 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5740 return false;
5741 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5742 return false;
5743 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5744 return false;
5745 } else {
5746 /* protected mode guest state checks */
5747 if (!cs_ss_rpl_check(vcpu))
5748 return false;
5749 if (!code_segment_valid(vcpu))
5750 return false;
5751 if (!stack_segment_valid(vcpu))
5752 return false;
5753 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5754 return false;
5755 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5756 return false;
5757 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5758 return false;
5759 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5760 return false;
5761 if (!tr_valid(vcpu))
5762 return false;
5763 if (!ldtr_valid(vcpu))
5764 return false;
5765 }
5766 /* TODO:
5767 * - Add checks on RIP
5768 * - Add checks on RFLAGS
5769 */
5770
5771 return true;
5772}
5773
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005774static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5775{
5776 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5777}
5778
Mike Dayd77c26f2007-10-08 09:02:08 -04005779static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005780{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005781 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005782 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005783 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005784
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005785 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005786 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005787 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5788 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005789 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005790 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005791 r = kvm_write_guest_page(kvm, fn++, &data,
5792 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005793 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005794 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005795 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5796 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005797 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005798 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5799 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005800 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005801 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005802 r = kvm_write_guest_page(kvm, fn, &data,
5803 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5804 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005805out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005806 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005807 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005808}
5809
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005810static int init_rmode_identity_map(struct kvm *kvm)
5811{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005812 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005813 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005814 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005815 u32 tmp;
5816
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005817 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005818 mutex_lock(&kvm->slots_lock);
5819
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005820 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005821 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005822
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005823 if (!kvm_vmx->ept_identity_map_addr)
5824 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5825 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005826
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005827 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005828 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005829 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005830 goto out2;
5831
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005832 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005833 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5834 if (r < 0)
5835 goto out;
5836 /* Set up identity-mapping pagetable for EPT in real mode */
5837 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5838 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5839 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5840 r = kvm_write_guest_page(kvm, identity_map_pfn,
5841 &tmp, i * sizeof(tmp), sizeof(tmp));
5842 if (r < 0)
5843 goto out;
5844 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005845 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005846
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005847out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005848 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005849
5850out2:
5851 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005852 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005853}
5854
Avi Kivity6aa8b732006-12-10 02:21:36 -08005855static void seg_setup(int seg)
5856{
Mathias Krause772e0312012-08-30 01:30:19 +02005857 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005858 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005859
5860 vmcs_write16(sf->selector, 0);
5861 vmcs_writel(sf->base, 0);
5862 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005863 ar = 0x93;
5864 if (seg == VCPU_SREG_CS)
5865 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005866
5867 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005868}
5869
Sheng Yangf78e0e22007-10-29 09:40:42 +08005870static int alloc_apic_access_page(struct kvm *kvm)
5871{
Xiao Guangrong44841412012-09-07 14:14:20 +08005872 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005873 int r = 0;
5874
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005875 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005876 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005877 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005878 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5879 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005880 if (r)
5881 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005882
Tang Chen73a6d942014-09-11 13:38:00 +08005883 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005884 if (is_error_page(page)) {
5885 r = -EFAULT;
5886 goto out;
5887 }
5888
Tang Chenc24ae0d2014-09-24 15:57:58 +08005889 /*
5890 * Do not pin the page in memory, so that memory hot-unplug
5891 * is able to migrate it.
5892 */
5893 put_page(page);
5894 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005895out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005896 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005897 return r;
5898}
5899
Wanpeng Li991e7a02015-09-16 17:30:05 +08005900static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005901{
5902 int vpid;
5903
Avi Kivity919818a2009-03-23 18:01:29 +02005904 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005905 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005906 spin_lock(&vmx_vpid_lock);
5907 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005908 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005909 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005910 else
5911 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005912 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005913 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005914}
5915
Wanpeng Li991e7a02015-09-16 17:30:05 +08005916static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005917{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005918 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005919 return;
5920 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005921 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005922 spin_unlock(&vmx_vpid_lock);
5923}
5924
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005925static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5926 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005927{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005928 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005929
5930 if (!cpu_has_vmx_msr_bitmap())
5931 return;
5932
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005933 if (static_branch_unlikely(&enable_evmcs))
5934 evmcs_touch_msr_bitmap();
5935
Sheng Yang25c5f222008-03-28 13:18:56 +08005936 /*
5937 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5938 * have the write-low and read-high bitmap offsets the wrong way round.
5939 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5940 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005941 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005942 if (type & MSR_TYPE_R)
5943 /* read-low */
5944 __clear_bit(msr, msr_bitmap + 0x000 / f);
5945
5946 if (type & MSR_TYPE_W)
5947 /* write-low */
5948 __clear_bit(msr, msr_bitmap + 0x800 / f);
5949
Sheng Yang25c5f222008-03-28 13:18:56 +08005950 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5951 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005952 if (type & MSR_TYPE_R)
5953 /* read-high */
5954 __clear_bit(msr, msr_bitmap + 0x400 / f);
5955
5956 if (type & MSR_TYPE_W)
5957 /* write-high */
5958 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5959
5960 }
5961}
5962
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005963static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5964 u32 msr, int type)
5965{
5966 int f = sizeof(unsigned long);
5967
5968 if (!cpu_has_vmx_msr_bitmap())
5969 return;
5970
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005971 if (static_branch_unlikely(&enable_evmcs))
5972 evmcs_touch_msr_bitmap();
5973
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005974 /*
5975 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5976 * have the write-low and read-high bitmap offsets the wrong way round.
5977 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5978 */
5979 if (msr <= 0x1fff) {
5980 if (type & MSR_TYPE_R)
5981 /* read-low */
5982 __set_bit(msr, msr_bitmap + 0x000 / f);
5983
5984 if (type & MSR_TYPE_W)
5985 /* write-low */
5986 __set_bit(msr, msr_bitmap + 0x800 / f);
5987
5988 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5989 msr &= 0x1fff;
5990 if (type & MSR_TYPE_R)
5991 /* read-high */
5992 __set_bit(msr, msr_bitmap + 0x400 / f);
5993
5994 if (type & MSR_TYPE_W)
5995 /* write-high */
5996 __set_bit(msr, msr_bitmap + 0xc00 / f);
5997
5998 }
5999}
6000
6001static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6002 u32 msr, int type, bool value)
6003{
6004 if (value)
6005 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6006 else
6007 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6008}
6009
Wincy Vanf2b93282015-02-03 23:56:03 +08006010/*
6011 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6012 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6013 */
6014static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6015 unsigned long *msr_bitmap_nested,
6016 u32 msr, int type)
6017{
6018 int f = sizeof(unsigned long);
6019
Wincy Vanf2b93282015-02-03 23:56:03 +08006020 /*
6021 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6022 * have the write-low and read-high bitmap offsets the wrong way round.
6023 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6024 */
6025 if (msr <= 0x1fff) {
6026 if (type & MSR_TYPE_R &&
6027 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6028 /* read-low */
6029 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6030
6031 if (type & MSR_TYPE_W &&
6032 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6033 /* write-low */
6034 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6035
6036 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6037 msr &= 0x1fff;
6038 if (type & MSR_TYPE_R &&
6039 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6040 /* read-high */
6041 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6042
6043 if (type & MSR_TYPE_W &&
6044 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6045 /* write-high */
6046 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6047
6048 }
6049}
6050
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006051static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006052{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006053 u8 mode = 0;
6054
6055 if (cpu_has_secondary_exec_ctrls() &&
6056 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6057 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6058 mode |= MSR_BITMAP_MODE_X2APIC;
6059 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6060 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6061 }
6062
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006063 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006064}
6065
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006066#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6067
6068static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6069 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006070{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006071 int msr;
6072
6073 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6074 unsigned word = msr / BITS_PER_LONG;
6075 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6076 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006077 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006078
6079 if (mode & MSR_BITMAP_MODE_X2APIC) {
6080 /*
6081 * TPR reads and writes can be virtualized even if virtual interrupt
6082 * delivery is not in use.
6083 */
6084 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6085 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6086 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6087 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6088 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6089 }
6090 }
6091}
6092
6093static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6094{
6095 struct vcpu_vmx *vmx = to_vmx(vcpu);
6096 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6097 u8 mode = vmx_msr_bitmap_mode(vcpu);
6098 u8 changed = mode ^ vmx->msr_bitmap_mode;
6099
6100 if (!changed)
6101 return;
6102
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006103 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6104 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6105
6106 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006107}
6108
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006109static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006110{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006111 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006112}
6113
David Matlackc9f04402017-08-01 14:00:40 -07006114static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6115{
6116 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6117 gfn_t gfn;
6118
6119 /*
6120 * Don't need to mark the APIC access page dirty; it is never
6121 * written to by the CPU during APIC virtualization.
6122 */
6123
6124 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6125 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6126 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6127 }
6128
6129 if (nested_cpu_has_posted_intr(vmcs12)) {
6130 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6131 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6132 }
6133}
6134
6135
David Hildenbrand6342c502017-01-25 11:58:58 +01006136static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006137{
6138 struct vcpu_vmx *vmx = to_vmx(vcpu);
6139 int max_irr;
6140 void *vapic_page;
6141 u16 status;
6142
David Matlackc9f04402017-08-01 14:00:40 -07006143 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6144 return;
Wincy Van705699a2015-02-03 23:58:17 +08006145
David Matlackc9f04402017-08-01 14:00:40 -07006146 vmx->nested.pi_pending = false;
6147 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6148 return;
Wincy Van705699a2015-02-03 23:58:17 +08006149
David Matlackc9f04402017-08-01 14:00:40 -07006150 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6151 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006152 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006153 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6154 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006155 kunmap(vmx->nested.virtual_apic_page);
6156
6157 status = vmcs_read16(GUEST_INTR_STATUS);
6158 if ((u8)max_irr > ((u8)status & 0xff)) {
6159 status &= ~0xff;
6160 status |= (u8)max_irr;
6161 vmcs_write16(GUEST_INTR_STATUS, status);
6162 }
6163 }
David Matlackc9f04402017-08-01 14:00:40 -07006164
6165 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006166}
6167
Paolo Bonzini7e712682018-10-03 13:44:26 +02006168static u8 vmx_get_rvi(void)
6169{
6170 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6171}
6172
Liran Alone6c67d82018-09-04 10:56:52 +03006173static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6174{
6175 struct vcpu_vmx *vmx = to_vmx(vcpu);
6176 void *vapic_page;
6177 u32 vppr;
6178 int rvi;
6179
6180 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6181 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6182 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6183 return false;
6184
Paolo Bonzini7e712682018-10-03 13:44:26 +02006185 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006186
6187 vapic_page = kmap(vmx->nested.virtual_apic_page);
6188 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6189 kunmap(vmx->nested.virtual_apic_page);
6190
6191 return ((rvi & 0xf0) > (vppr & 0xf0));
6192}
6193
Wincy Van06a55242017-04-28 13:13:59 +08006194static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6195 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006196{
6197#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006198 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6199
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006200 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006201 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006202 * The vector of interrupt to be delivered to vcpu had
6203 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006204 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006205 * Following cases will be reached in this block, and
6206 * we always send a notification event in all cases as
6207 * explained below.
6208 *
6209 * Case 1: vcpu keeps in non-root mode. Sending a
6210 * notification event posts the interrupt to vcpu.
6211 *
6212 * Case 2: vcpu exits to root mode and is still
6213 * runnable. PIR will be synced to vIRR before the
6214 * next vcpu entry. Sending a notification event in
6215 * this case has no effect, as vcpu is not in root
6216 * mode.
6217 *
6218 * Case 3: vcpu exits to root mode and is blocked.
6219 * vcpu_block() has already synced PIR to vIRR and
6220 * never blocks vcpu if vIRR is not cleared. Therefore,
6221 * a blocked vcpu here does not wait for any requested
6222 * interrupts in PIR, and sending a notification event
6223 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006224 */
Feng Wu28b835d2015-09-18 22:29:54 +08006225
Wincy Van06a55242017-04-28 13:13:59 +08006226 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006227 return true;
6228 }
6229#endif
6230 return false;
6231}
6232
Wincy Van705699a2015-02-03 23:58:17 +08006233static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6234 int vector)
6235{
6236 struct vcpu_vmx *vmx = to_vmx(vcpu);
6237
6238 if (is_guest_mode(vcpu) &&
6239 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006240 /*
6241 * If a posted intr is not recognized by hardware,
6242 * we will accomplish it in the next vmentry.
6243 */
6244 vmx->nested.pi_pending = true;
6245 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006246 /* the PIR and ON have been set by L1. */
6247 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6248 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006249 return 0;
6250 }
6251 return -1;
6252}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006253/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006254 * Send interrupt to vcpu via posted interrupt way.
6255 * 1. If target vcpu is running(non-root mode), send posted interrupt
6256 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6257 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6258 * interrupt from PIR in next vmentry.
6259 */
6260static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6261{
6262 struct vcpu_vmx *vmx = to_vmx(vcpu);
6263 int r;
6264
Wincy Van705699a2015-02-03 23:58:17 +08006265 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6266 if (!r)
6267 return;
6268
Yang Zhanga20ed542013-04-11 19:25:15 +08006269 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6270 return;
6271
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006272 /* If a previous notification has sent the IPI, nothing to do. */
6273 if (pi_test_and_set_on(&vmx->pi_desc))
6274 return;
6275
Wincy Van06a55242017-04-28 13:13:59 +08006276 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006277 kvm_vcpu_kick(vcpu);
6278}
6279
Avi Kivity6aa8b732006-12-10 02:21:36 -08006280/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006281 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6282 * will not change in the lifetime of the guest.
6283 * Note that host-state that does change is set elsewhere. E.g., host-state
6284 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6285 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006286static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006287{
6288 u32 low32, high32;
6289 unsigned long tmpl;
6290 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006291 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006292
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006293 cr0 = read_cr0();
6294 WARN_ON(cr0 & X86_CR0_TS);
6295 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006296
6297 /*
6298 * Save the most likely value for this task's CR3 in the VMCS.
6299 * We can't use __get_current_cr3_fast() because we're not atomic.
6300 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006301 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006302 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006303 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006304
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006305 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006306 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006307 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006308 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006309
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006310 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006311#ifdef CONFIG_X86_64
6312 /*
6313 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006314 * vmx_prepare_switch_to_host(), in case userspace uses
6315 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006316 */
6317 vmcs_write16(HOST_DS_SELECTOR, 0);
6318 vmcs_write16(HOST_ES_SELECTOR, 0);
6319#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006320 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6321 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006322#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006323 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6324 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6325
Juergen Gross87930012017-09-04 12:25:27 +02006326 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006327 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006328 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006329
Avi Kivity83287ea422012-09-16 15:10:57 +03006330 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006331
6332 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6333 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6334 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6335 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6336
6337 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6338 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6339 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6340 }
6341}
6342
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006343static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6344{
6345 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6346 if (enable_ept)
6347 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006348 if (is_guest_mode(&vmx->vcpu))
6349 vmx->vcpu.arch.cr4_guest_owned_bits &=
6350 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006351 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6352}
6353
Yang Zhang01e439b2013-04-11 19:25:12 +08006354static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6355{
6356 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6357
Andrey Smetanind62caab2015-11-10 15:36:33 +03006358 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006359 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006360
6361 if (!enable_vnmi)
6362 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6363
Yunhong Jiang64672c92016-06-13 14:19:59 -07006364 /* Enable the preemption timer dynamically */
6365 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006366 return pin_based_exec_ctrl;
6367}
6368
Andrey Smetanind62caab2015-11-10 15:36:33 +03006369static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6370{
6371 struct vcpu_vmx *vmx = to_vmx(vcpu);
6372
6373 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006374 if (cpu_has_secondary_exec_ctrls()) {
6375 if (kvm_vcpu_apicv_active(vcpu))
6376 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6377 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6378 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6379 else
6380 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6381 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6382 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6383 }
6384
6385 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006386 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006387}
6388
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006389static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6390{
6391 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006392
6393 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6394 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6395
Paolo Bonzini35754c92015-07-29 12:05:37 +02006396 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006397 exec_control &= ~CPU_BASED_TPR_SHADOW;
6398#ifdef CONFIG_X86_64
6399 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6400 CPU_BASED_CR8_LOAD_EXITING;
6401#endif
6402 }
6403 if (!enable_ept)
6404 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6405 CPU_BASED_CR3_LOAD_EXITING |
6406 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006407 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6408 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6409 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006410 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6411 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006412 return exec_control;
6413}
6414
Jim Mattson45ec3682017-08-23 16:32:04 -07006415static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006416{
Jim Mattson45ec3682017-08-23 16:32:04 -07006417 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006418 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006419}
6420
Jim Mattson75f4fc82017-08-23 16:32:03 -07006421static bool vmx_rdseed_supported(void)
6422{
6423 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006424 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006425}
6426
Paolo Bonzini80154d72017-08-24 13:55:35 +02006427static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006428{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006429 struct kvm_vcpu *vcpu = &vmx->vcpu;
6430
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006431 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006432
Paolo Bonzini80154d72017-08-24 13:55:35 +02006433 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006434 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6435 if (vmx->vpid == 0)
6436 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6437 if (!enable_ept) {
6438 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6439 enable_unrestricted_guest = 0;
6440 }
6441 if (!enable_unrestricted_guest)
6442 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006443 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006444 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006445 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006446 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6447 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006448 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006449
6450 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6451 * in vmx_set_cr4. */
6452 exec_control &= ~SECONDARY_EXEC_DESC;
6453
Abel Gordonabc4fc52013-04-18 14:35:25 +03006454 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6455 (handle_vmptrld).
6456 We can NOT enable shadow_vmcs here because we don't have yet
6457 a current VMCS12
6458 */
6459 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006460
6461 if (!enable_pml)
6462 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006463
Paolo Bonzini3db13482017-08-24 14:48:03 +02006464 if (vmx_xsaves_supported()) {
6465 /* Exposing XSAVES only when XSAVE is exposed */
6466 bool xsaves_enabled =
6467 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6468 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6469
6470 if (!xsaves_enabled)
6471 exec_control &= ~SECONDARY_EXEC_XSAVES;
6472
6473 if (nested) {
6474 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006475 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006476 SECONDARY_EXEC_XSAVES;
6477 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006478 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006479 ~SECONDARY_EXEC_XSAVES;
6480 }
6481 }
6482
Paolo Bonzini80154d72017-08-24 13:55:35 +02006483 if (vmx_rdtscp_supported()) {
6484 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6485 if (!rdtscp_enabled)
6486 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6487
6488 if (nested) {
6489 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006490 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006491 SECONDARY_EXEC_RDTSCP;
6492 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006493 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006494 ~SECONDARY_EXEC_RDTSCP;
6495 }
6496 }
6497
6498 if (vmx_invpcid_supported()) {
6499 /* Exposing INVPCID only when PCID is exposed */
6500 bool invpcid_enabled =
6501 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6502 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6503
6504 if (!invpcid_enabled) {
6505 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6506 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6507 }
6508
6509 if (nested) {
6510 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006511 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006512 SECONDARY_EXEC_ENABLE_INVPCID;
6513 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006514 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006515 ~SECONDARY_EXEC_ENABLE_INVPCID;
6516 }
6517 }
6518
Jim Mattson45ec3682017-08-23 16:32:04 -07006519 if (vmx_rdrand_supported()) {
6520 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6521 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006522 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006523
6524 if (nested) {
6525 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006526 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006527 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006528 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006529 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006530 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006531 }
6532 }
6533
Jim Mattson75f4fc82017-08-23 16:32:03 -07006534 if (vmx_rdseed_supported()) {
6535 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6536 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006537 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006538
6539 if (nested) {
6540 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006541 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006542 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006543 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006544 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006545 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006546 }
6547 }
6548
Paolo Bonzini80154d72017-08-24 13:55:35 +02006549 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006550}
6551
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006552static void ept_set_mmio_spte_mask(void)
6553{
6554 /*
6555 * EPT Misconfigurations can be generated if the value of bits 2:0
6556 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006557 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006558 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6559 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006560}
6561
Wanpeng Lif53cd632014-12-02 19:14:58 +08006562#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006563/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006564 * Sets up the vmcs for emulated real mode.
6565 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006566static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006567{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006568 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006569
Abel Gordon4607c2d2013-04-18 14:35:55 +03006570 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006571 /*
6572 * At vCPU creation, "VMWRITE to any supported field
6573 * in the VMCS" is supported, so use the more
6574 * permissive vmx_vmread_bitmap to specify both read
6575 * and write permissions for the shadow VMCS.
6576 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006577 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006578 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006579 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006580 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006581 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006582
Avi Kivity6aa8b732006-12-10 02:21:36 -08006583 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6584
Avi Kivity6aa8b732006-12-10 02:21:36 -08006585 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006586 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006587 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006588
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006589 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006590
Dan Williamsdfa169b2016-06-02 11:17:24 -07006591 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006592 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006593 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006594 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006595 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006596
Andrey Smetanind62caab2015-11-10 15:36:33 +03006597 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006598 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6599 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6600 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6601 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6602
6603 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006604
Li RongQing0bcf2612015-12-03 13:29:34 +08006605 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006606 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006607 }
6608
Wanpeng Lib31c1142018-03-12 04:53:04 -07006609 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006610 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006611 vmx->ple_window = ple_window;
6612 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006613 }
6614
Xiao Guangrongc3707952011-07-12 03:28:04 +08006615 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6616 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6618
Avi Kivity9581d442010-10-19 16:46:55 +02006619 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6620 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006621 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006622 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6623 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006624
Bandan Das2a499e42017-08-03 15:54:41 -04006625 if (cpu_has_vmx_vmfunc())
6626 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6627
Eddie Dong2cc51562007-05-21 07:28:09 +03006628 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6629 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006630 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006631 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006632 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006633
Radim Krčmář74545702015-04-27 15:11:25 +02006634 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6635 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006636
Paolo Bonzini03916db2014-07-24 14:21:57 +02006637 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006638 u32 index = vmx_msr_index[i];
6639 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006640 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006641
6642 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6643 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006644 if (wrmsr_safe(index, data_low, data_high) < 0)
6645 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006646 vmx->guest_msrs[j].index = i;
6647 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006648 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006649 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006650 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006651
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006652 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006653
6654 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006655
6656 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006657 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006658
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006659 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6660 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6661
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006662 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006663
Wanpeng Lif53cd632014-12-02 19:14:58 +08006664 if (vmx_xsaves_supported())
6665 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6666
Peter Feiner4e595162016-07-07 14:49:58 -07006667 if (enable_pml) {
6668 ASSERT(vmx->pml_pg);
6669 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6670 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6671 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006672
6673 if (cpu_has_vmx_encls_vmexit())
6674 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006675}
6676
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006677static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006678{
6679 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006680 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006681 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006682
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006683 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006684 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006685
Wanpeng Li518e7b92018-02-28 14:03:31 +08006686 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006687 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006688 kvm_set_cr8(vcpu, 0);
6689
6690 if (!init_event) {
6691 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6692 MSR_IA32_APICBASE_ENABLE;
6693 if (kvm_vcpu_is_reset_bsp(vcpu))
6694 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6695 apic_base_msr.host_initiated = true;
6696 kvm_set_apic_base(vcpu, &apic_base_msr);
6697 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006698
Avi Kivity2fb92db2011-04-27 19:42:18 +03006699 vmx_segment_cache_clear(vmx);
6700
Avi Kivity5706be02008-08-20 15:07:31 +03006701 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006702 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006703 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006704
6705 seg_setup(VCPU_SREG_DS);
6706 seg_setup(VCPU_SREG_ES);
6707 seg_setup(VCPU_SREG_FS);
6708 seg_setup(VCPU_SREG_GS);
6709 seg_setup(VCPU_SREG_SS);
6710
6711 vmcs_write16(GUEST_TR_SELECTOR, 0);
6712 vmcs_writel(GUEST_TR_BASE, 0);
6713 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6714 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6715
6716 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6717 vmcs_writel(GUEST_LDTR_BASE, 0);
6718 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6719 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6720
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006721 if (!init_event) {
6722 vmcs_write32(GUEST_SYSENTER_CS, 0);
6723 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6724 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6725 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6726 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006727
Wanpeng Lic37c2872017-11-20 14:52:21 -08006728 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006729 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006730
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006731 vmcs_writel(GUEST_GDTR_BASE, 0);
6732 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6733
6734 vmcs_writel(GUEST_IDTR_BASE, 0);
6735 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6736
Anthony Liguori443381a2010-12-06 10:53:38 -06006737 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006738 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006739 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006740 if (kvm_mpx_supported())
6741 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006742
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006743 setup_msrs(vmx);
6744
Avi Kivity6aa8b732006-12-10 02:21:36 -08006745 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6746
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006747 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006748 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006749 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006750 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006751 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006752 vmcs_write32(TPR_THRESHOLD, 0);
6753 }
6754
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006755 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006756
Sheng Yang2384d2b2008-01-17 15:14:33 +08006757 if (vmx->vpid != 0)
6758 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6759
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006760 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006761 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006762 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006763 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006764 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006765
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006766 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006767
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006768 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006769 if (init_event)
6770 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006771}
6772
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006773/*
6774 * In nested virtualization, check if L1 asked to exit on external interrupts.
6775 * For most existing hypervisors, this will always return true.
6776 */
6777static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6778{
6779 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6780 PIN_BASED_EXT_INTR_MASK;
6781}
6782
Bandan Das77b0f5d2014-04-19 18:17:45 -04006783/*
6784 * In nested virtualization, check if L1 has set
6785 * VM_EXIT_ACK_INTR_ON_EXIT
6786 */
6787static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6788{
6789 return get_vmcs12(vcpu)->vm_exit_controls &
6790 VM_EXIT_ACK_INTR_ON_EXIT;
6791}
6792
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006793static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6794{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006795 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006796}
6797
Jan Kiszkac9a79532014-03-07 20:03:15 +01006798static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006799{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006800 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6801 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006802}
6803
Jan Kiszkac9a79532014-03-07 20:03:15 +01006804static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006805{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006806 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006807 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006808 enable_irq_window(vcpu);
6809 return;
6810 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006811
Paolo Bonzini47c01522016-12-19 11:44:07 +01006812 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6813 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006814}
6815
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006816static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006817{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006819 uint32_t intr;
6820 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006821
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006822 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006823
Avi Kivityfa89a812008-09-01 15:57:51 +03006824 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006825 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006826 int inc_eip = 0;
6827 if (vcpu->arch.interrupt.soft)
6828 inc_eip = vcpu->arch.event_exit_inst_len;
6829 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006830 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006831 return;
6832 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006833 intr = irq | INTR_INFO_VALID_MASK;
6834 if (vcpu->arch.interrupt.soft) {
6835 intr |= INTR_TYPE_SOFT_INTR;
6836 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6837 vmx->vcpu.arch.event_exit_inst_len);
6838 } else
6839 intr |= INTR_TYPE_EXT_INTR;
6840 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006841
6842 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006843}
6844
Sheng Yangf08864b2008-05-15 18:23:25 +08006845static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6846{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006847 struct vcpu_vmx *vmx = to_vmx(vcpu);
6848
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006849 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006850 /*
6851 * Tracking the NMI-blocked state in software is built upon
6852 * finding the next open IRQ window. This, in turn, depends on
6853 * well-behaving guests: They have to keep IRQs disabled at
6854 * least as long as the NMI handler runs. Otherwise we may
6855 * cause NMI nesting, maybe breaking the guest. But as this is
6856 * highly unlikely, we can live with the residual risk.
6857 */
6858 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6859 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6860 }
6861
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006862 ++vcpu->stat.nmi_injections;
6863 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006864
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006865 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006866 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006867 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006868 return;
6869 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006870
Sheng Yangf08864b2008-05-15 18:23:25 +08006871 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6872 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006873
6874 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006875}
6876
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006877static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6878{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006879 struct vcpu_vmx *vmx = to_vmx(vcpu);
6880 bool masked;
6881
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006882 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006883 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006884 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006885 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006886 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6887 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6888 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006889}
6890
6891static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6892{
6893 struct vcpu_vmx *vmx = to_vmx(vcpu);
6894
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006895 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006896 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6897 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6898 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6899 }
6900 } else {
6901 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6902 if (masked)
6903 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6904 GUEST_INTR_STATE_NMI);
6905 else
6906 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6907 GUEST_INTR_STATE_NMI);
6908 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006909}
6910
Jan Kiszka2505dc92013-04-14 12:12:47 +02006911static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6912{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006913 if (to_vmx(vcpu)->nested.nested_run_pending)
6914 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006915
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006916 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006917 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6918 return 0;
6919
Jan Kiszka2505dc92013-04-14 12:12:47 +02006920 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6921 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6922 | GUEST_INTR_STATE_NMI));
6923}
6924
Gleb Natapov78646122009-03-23 12:12:11 +02006925static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6926{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006927 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6928 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006929 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6930 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006931}
6932
Izik Eiduscbc94022007-10-25 00:29:55 +02006933static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6934{
6935 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006936
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006937 if (enable_unrestricted_guest)
6938 return 0;
6939
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006940 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6941 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006942 if (ret)
6943 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006944 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006945 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006946}
6947
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006948static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6949{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006950 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006951 return 0;
6952}
6953
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006954static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006955{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006956 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006957 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006958 /*
6959 * Update instruction length as we may reinject the exception
6960 * from user space while in guest debugging mode.
6961 */
6962 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6963 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006964 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006965 return false;
6966 /* fall through */
6967 case DB_VECTOR:
6968 if (vcpu->guest_debug &
6969 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6970 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006971 /* fall through */
6972 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006973 case OF_VECTOR:
6974 case BR_VECTOR:
6975 case UD_VECTOR:
6976 case DF_VECTOR:
6977 case SS_VECTOR:
6978 case GP_VECTOR:
6979 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006980 return true;
6981 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006982 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006983 return false;
6984}
6985
6986static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6987 int vec, u32 err_code)
6988{
6989 /*
6990 * Instruction with address size override prefix opcode 0x67
6991 * Cause the #SS fault with 0 error code in VM86 mode.
6992 */
6993 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006994 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006995 if (vcpu->arch.halt_request) {
6996 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006997 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006998 }
6999 return 1;
7000 }
7001 return 0;
7002 }
7003
7004 /*
7005 * Forward all other exceptions that are valid in real mode.
7006 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7007 * the required debugging infrastructure rework.
7008 */
7009 kvm_queue_exception(vcpu, vec);
7010 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007011}
7012
Andi Kleena0861c02009-06-08 17:37:09 +08007013/*
7014 * Trigger machine check on the host. We assume all the MSRs are already set up
7015 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7016 * We pass a fake environment to the machine check handler because we want
7017 * the guest to be always treated like user space, no matter what context
7018 * it used internally.
7019 */
7020static void kvm_machine_check(void)
7021{
7022#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7023 struct pt_regs regs = {
7024 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7025 .flags = X86_EFLAGS_IF,
7026 };
7027
7028 do_machine_check(&regs, 0);
7029#endif
7030}
7031
Avi Kivity851ba692009-08-24 11:10:17 +03007032static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007033{
7034 /* already handled by vcpu_run */
7035 return 1;
7036}
7037
Avi Kivity851ba692009-08-24 11:10:17 +03007038static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007039{
Avi Kivity1155f762007-11-22 11:30:47 +02007040 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007041 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007042 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007043 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007044 u32 vect_info;
7045 enum emulation_result er;
7046
Avi Kivity1155f762007-11-22 11:30:47 +02007047 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007048 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007049
Andi Kleena0861c02009-06-08 17:37:09 +08007050 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007051 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007052
Jim Mattsonef85b672016-12-12 11:01:37 -08007053 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007054 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007055
Wanpeng Li082d06e2018-04-03 16:28:48 -07007056 if (is_invalid_opcode(intr_info))
7057 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007058
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007060 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007061 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007062
Liran Alon9e869482018-03-12 13:12:51 +02007063 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7064 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007065 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007066 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7067 if (er == EMULATE_USER_EXIT)
7068 return 0;
7069 else if (er != EMULATE_DONE)
7070 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7071 return 1;
7072 }
7073
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007074 /*
7075 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7076 * MMIO, it is better to report an internal error.
7077 * See the comments in vmx_handle_exit.
7078 */
7079 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7080 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7081 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7082 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007083 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007084 vcpu->run->internal.data[0] = vect_info;
7085 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007086 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007087 return 0;
7088 }
7089
Avi Kivity6aa8b732006-12-10 02:21:36 -08007090 if (is_page_fault(intr_info)) {
7091 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007092 /* EPT won't cause page fault directly */
7093 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007094 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007095 }
7096
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007097 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007098
7099 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7100 return handle_rmode_exception(vcpu, ex_no, error_code);
7101
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007102 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007103 case AC_VECTOR:
7104 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7105 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007106 case DB_VECTOR:
7107 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7108 if (!(vcpu->guest_debug &
7109 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007110 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007111 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007112 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007113 skip_emulated_instruction(vcpu);
7114
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007115 kvm_queue_exception(vcpu, DB_VECTOR);
7116 return 1;
7117 }
7118 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7119 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7120 /* fall through */
7121 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007122 /*
7123 * Update instruction length as we may reinject #BP from
7124 * user space while in guest debugging mode. Reading it for
7125 * #DB as well causes no harm, it is not used in that case.
7126 */
7127 vmx->vcpu.arch.event_exit_inst_len =
7128 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007129 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007130 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007131 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7132 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007133 break;
7134 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007135 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7136 kvm_run->ex.exception = ex_no;
7137 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007138 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007139 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007140 return 0;
7141}
7142
Avi Kivity851ba692009-08-24 11:10:17 +03007143static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007144{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007145 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007146 return 1;
7147}
7148
Avi Kivity851ba692009-08-24 11:10:17 +03007149static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007150{
Avi Kivity851ba692009-08-24 11:10:17 +03007151 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007152 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007153 return 0;
7154}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007155
Avi Kivity851ba692009-08-24 11:10:17 +03007156static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007157{
He, Qingbfdaab02007-09-12 14:18:28 +08007158 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007159 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007160 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007161
He, Qingbfdaab02007-09-12 14:18:28 +08007162 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007163 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007164
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007165 ++vcpu->stat.io_exits;
7166
Sean Christopherson432baf62018-03-08 08:57:26 -08007167 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007168 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007169
7170 port = exit_qualification >> 16;
7171 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007172 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007173
Sean Christophersondca7f122018-03-08 08:57:27 -08007174 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007175}
7176
Ingo Molnar102d8322007-02-19 14:37:47 +02007177static void
7178vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7179{
7180 /*
7181 * Patch in the VMCALL instruction:
7182 */
7183 hypercall[0] = 0x0f;
7184 hypercall[1] = 0x01;
7185 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007186}
7187
Guo Chao0fa06072012-06-28 15:16:19 +08007188/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007189static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7190{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007191 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007192 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7193 unsigned long orig_val = val;
7194
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007195 /*
7196 * We get here when L2 changed cr0 in a way that did not change
7197 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007198 * but did change L0 shadowed bits. So we first calculate the
7199 * effective cr0 value that L1 would like to write into the
7200 * hardware. It consists of the L2-owned bits from the new
7201 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007202 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007203 val = (val & ~vmcs12->cr0_guest_host_mask) |
7204 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7205
David Matlack38991522016-11-29 18:14:08 -08007206 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007207 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007208
7209 if (kvm_set_cr0(vcpu, val))
7210 return 1;
7211 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007212 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007213 } else {
7214 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007215 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007216 return 1;
David Matlack38991522016-11-29 18:14:08 -08007217
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007218 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007219 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007220}
7221
7222static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7223{
7224 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7226 unsigned long orig_val = val;
7227
7228 /* analogously to handle_set_cr0 */
7229 val = (val & ~vmcs12->cr4_guest_host_mask) |
7230 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7231 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007232 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007233 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007234 return 0;
7235 } else
7236 return kvm_set_cr4(vcpu, val);
7237}
7238
Paolo Bonzini0367f202016-07-12 10:44:55 +02007239static int handle_desc(struct kvm_vcpu *vcpu)
7240{
7241 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007242 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007243}
7244
Avi Kivity851ba692009-08-24 11:10:17 +03007245static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007247 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007248 int cr;
7249 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007250 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007251 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007252
He, Qingbfdaab02007-09-12 14:18:28 +08007253 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007254 cr = exit_qualification & 15;
7255 reg = (exit_qualification >> 8) & 15;
7256 switch ((exit_qualification >> 4) & 3) {
7257 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007258 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007259 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007260 switch (cr) {
7261 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007262 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007263 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007264 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007265 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007266 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007267 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007268 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007269 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007271 case 8: {
7272 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007273 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007274 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007275 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007276 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007277 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007278 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007279 return ret;
7280 /*
7281 * TODO: we might be squashing a
7282 * KVM_GUESTDBG_SINGLESTEP-triggered
7283 * KVM_EXIT_DEBUG here.
7284 */
Avi Kivity851ba692009-08-24 11:10:17 +03007285 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007286 return 0;
7287 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007288 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007290 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007291 WARN_ONCE(1, "Guest should always own CR0.TS");
7292 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007293 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007294 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007295 case 1: /*mov from cr*/
7296 switch (cr) {
7297 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007298 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007299 val = kvm_read_cr3(vcpu);
7300 kvm_register_write(vcpu, reg, val);
7301 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007302 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007303 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007304 val = kvm_get_cr8(vcpu);
7305 kvm_register_write(vcpu, reg, val);
7306 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007307 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 }
7309 break;
7310 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007311 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007312 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007313 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007314
Kyle Huey6affcbe2016-11-29 12:40:40 -08007315 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007316 default:
7317 break;
7318 }
Avi Kivity851ba692009-08-24 11:10:17 +03007319 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007320 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007321 (int)(exit_qualification >> 4) & 3, cr);
7322 return 0;
7323}
7324
Avi Kivity851ba692009-08-24 11:10:17 +03007325static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007326{
He, Qingbfdaab02007-09-12 14:18:28 +08007327 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007328 int dr, dr7, reg;
7329
7330 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7331 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7332
7333 /* First, if DR does not exist, trigger UD */
7334 if (!kvm_require_dr(vcpu, dr))
7335 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007336
Jan Kiszkaf2483412010-01-20 18:20:20 +01007337 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007338 if (!kvm_require_cpl(vcpu, 0))
7339 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007340 dr7 = vmcs_readl(GUEST_DR7);
7341 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007342 /*
7343 * As the vm-exit takes precedence over the debug trap, we
7344 * need to emulate the latter, either for the host or the
7345 * guest debugging itself.
7346 */
7347 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007348 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007349 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007350 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007351 vcpu->run->debug.arch.exception = DB_VECTOR;
7352 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007353 return 0;
7354 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007355 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007356 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007357 kvm_queue_exception(vcpu, DB_VECTOR);
7358 return 1;
7359 }
7360 }
7361
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007362 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007363 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7364 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007365
7366 /*
7367 * No more DR vmexits; force a reload of the debug registers
7368 * and reenter on this instruction. The next vmexit will
7369 * retrieve the full state of the debug registers.
7370 */
7371 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7372 return 1;
7373 }
7374
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007375 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7376 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007377 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007378
7379 if (kvm_get_dr(vcpu, dr, &val))
7380 return 1;
7381 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007382 } else
Nadav Amit57773922014-06-18 17:19:23 +03007383 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007384 return 1;
7385
Kyle Huey6affcbe2016-11-29 12:40:40 -08007386 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007387}
7388
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007389static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7390{
7391 return vcpu->arch.dr6;
7392}
7393
7394static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7395{
7396}
7397
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007398static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7399{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007400 get_debugreg(vcpu->arch.db[0], 0);
7401 get_debugreg(vcpu->arch.db[1], 1);
7402 get_debugreg(vcpu->arch.db[2], 2);
7403 get_debugreg(vcpu->arch.db[3], 3);
7404 get_debugreg(vcpu->arch.dr6, 6);
7405 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7406
7407 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007408 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007409}
7410
Gleb Natapov020df072010-04-13 10:05:23 +03007411static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7412{
7413 vmcs_writel(GUEST_DR7, val);
7414}
7415
Avi Kivity851ba692009-08-24 11:10:17 +03007416static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007417{
Kyle Huey6a908b62016-11-29 12:40:37 -08007418 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007419}
7420
Avi Kivity851ba692009-08-24 11:10:17 +03007421static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007422{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007423 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007424 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007425
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007426 msr_info.index = ecx;
7427 msr_info.host_initiated = false;
7428 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007429 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007430 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007431 return 1;
7432 }
7433
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007434 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007435
Avi Kivity6aa8b732006-12-10 02:21:36 -08007436 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007437 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7438 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007439 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007440}
7441
Avi Kivity851ba692009-08-24 11:10:17 +03007442static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007443{
Will Auld8fe8ab42012-11-29 12:42:12 -08007444 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007445 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7446 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7447 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007448
Will Auld8fe8ab42012-11-29 12:42:12 -08007449 msr.data = data;
7450 msr.index = ecx;
7451 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007452 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007453 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007454 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007455 return 1;
7456 }
7457
Avi Kivity59200272010-01-25 19:47:02 +02007458 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007459 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007460}
7461
Avi Kivity851ba692009-08-24 11:10:17 +03007462static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007463{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007464 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007465 return 1;
7466}
7467
Avi Kivity851ba692009-08-24 11:10:17 +03007468static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007470 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7471 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007472
Avi Kivity3842d132010-07-27 12:30:24 +03007473 kvm_make_request(KVM_REQ_EVENT, vcpu);
7474
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007475 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007476 return 1;
7477}
7478
Avi Kivity851ba692009-08-24 11:10:17 +03007479static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007480{
Avi Kivityd3bef152007-06-05 15:53:05 +03007481 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007482}
7483
Avi Kivity851ba692009-08-24 11:10:17 +03007484static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007485{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007486 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007487}
7488
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007489static int handle_invd(struct kvm_vcpu *vcpu)
7490{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007491 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007492}
7493
Avi Kivity851ba692009-08-24 11:10:17 +03007494static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007495{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007496 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007497
7498 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007499 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007500}
7501
Avi Kivityfee84b02011-11-10 14:57:25 +02007502static int handle_rdpmc(struct kvm_vcpu *vcpu)
7503{
7504 int err;
7505
7506 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007507 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007508}
7509
Avi Kivity851ba692009-08-24 11:10:17 +03007510static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007511{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007512 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007513}
7514
Dexuan Cui2acf9232010-06-10 11:27:12 +08007515static int handle_xsetbv(struct kvm_vcpu *vcpu)
7516{
7517 u64 new_bv = kvm_read_edx_eax(vcpu);
7518 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7519
7520 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007521 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007522 return 1;
7523}
7524
Wanpeng Lif53cd632014-12-02 19:14:58 +08007525static int handle_xsaves(struct kvm_vcpu *vcpu)
7526{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007527 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007528 WARN(1, "this should never happen\n");
7529 return 1;
7530}
7531
7532static int handle_xrstors(struct kvm_vcpu *vcpu)
7533{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007534 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007535 WARN(1, "this should never happen\n");
7536 return 1;
7537}
7538
Avi Kivity851ba692009-08-24 11:10:17 +03007539static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007540{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007541 if (likely(fasteoi)) {
7542 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7543 int access_type, offset;
7544
7545 access_type = exit_qualification & APIC_ACCESS_TYPE;
7546 offset = exit_qualification & APIC_ACCESS_OFFSET;
7547 /*
7548 * Sane guest uses MOV to write EOI, with written value
7549 * not cared. So make a short-circuit here by avoiding
7550 * heavy instruction emulation.
7551 */
7552 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7553 (offset == APIC_EOI)) {
7554 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007555 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007556 }
7557 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007558 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007559}
7560
Yang Zhangc7c9c562013-01-25 10:18:51 +08007561static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7562{
7563 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7564 int vector = exit_qualification & 0xff;
7565
7566 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7567 kvm_apic_set_eoi_accelerated(vcpu, vector);
7568 return 1;
7569}
7570
Yang Zhang83d4c282013-01-25 10:18:49 +08007571static int handle_apic_write(struct kvm_vcpu *vcpu)
7572{
7573 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7574 u32 offset = exit_qualification & 0xfff;
7575
7576 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7577 kvm_apic_write_nodecode(vcpu, offset);
7578 return 1;
7579}
7580
Avi Kivity851ba692009-08-24 11:10:17 +03007581static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007582{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007584 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007585 bool has_error_code = false;
7586 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007587 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007588 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007589
7590 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007591 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007592 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007593
7594 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7595
7596 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007597 if (reason == TASK_SWITCH_GATE && idt_v) {
7598 switch (type) {
7599 case INTR_TYPE_NMI_INTR:
7600 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007601 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007602 break;
7603 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007604 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007605 kvm_clear_interrupt_queue(vcpu);
7606 break;
7607 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007608 if (vmx->idt_vectoring_info &
7609 VECTORING_INFO_DELIVER_CODE_MASK) {
7610 has_error_code = true;
7611 error_code =
7612 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7613 }
7614 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007615 case INTR_TYPE_SOFT_EXCEPTION:
7616 kvm_clear_exception_queue(vcpu);
7617 break;
7618 default:
7619 break;
7620 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007621 }
Izik Eidus37817f22008-03-24 23:14:53 +02007622 tss_selector = exit_qualification;
7623
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007624 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7625 type != INTR_TYPE_EXT_INTR &&
7626 type != INTR_TYPE_NMI_INTR))
7627 skip_emulated_instruction(vcpu);
7628
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007629 if (kvm_task_switch(vcpu, tss_selector,
7630 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7631 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007632 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7633 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7634 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007635 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007636 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007637
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007638 /*
7639 * TODO: What about debug traps on tss switch?
7640 * Are we supposed to inject them and update dr6?
7641 */
7642
7643 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007644}
7645
Avi Kivity851ba692009-08-24 11:10:17 +03007646static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007647{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007648 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007649 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007650 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007651
Sheng Yangf9c617f2009-03-25 10:08:52 +08007652 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007653
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007654 /*
7655 * EPT violation happened while executing iret from NMI,
7656 * "blocked by NMI" bit has to be set before next VM entry.
7657 * There are errata that may cause this bit to not be set:
7658 * AAK134, BY25.
7659 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007660 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007661 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007662 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007663 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7664
Sheng Yang14394422008-04-28 12:24:45 +08007665 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007666 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007667
Junaid Shahid27959a42016-12-06 16:46:10 -08007668 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007669 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007670 ? PFERR_USER_MASK : 0;
7671 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007672 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007673 ? PFERR_WRITE_MASK : 0;
7674 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007675 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007676 ? PFERR_FETCH_MASK : 0;
7677 /* ept page table entry is present? */
7678 error_code |= (exit_qualification &
7679 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7680 EPT_VIOLATION_EXECUTABLE))
7681 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007682
Paolo Bonzinieebed242016-11-28 14:39:58 +01007683 error_code |= (exit_qualification & 0x100) != 0 ?
7684 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007685
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007686 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007687 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007688}
7689
Avi Kivity851ba692009-08-24 11:10:17 +03007690static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007691{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007692 gpa_t gpa;
7693
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007694 /*
7695 * A nested guest cannot optimize MMIO vmexits, because we have an
7696 * nGPA here instead of the required GPA.
7697 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007698 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007699 if (!is_guest_mode(vcpu) &&
7700 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007701 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007702 /*
7703 * Doing kvm_skip_emulated_instruction() depends on undefined
7704 * behavior: Intel's manual doesn't mandate
7705 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7706 * occurs and while on real hardware it was observed to be set,
7707 * other hypervisors (namely Hyper-V) don't set it, we end up
7708 * advancing IP with some random value. Disable fast mmio when
7709 * running nested and keep it for real hardware in hope that
7710 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7711 */
7712 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7713 return kvm_skip_emulated_instruction(vcpu);
7714 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007715 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007716 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007717 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007718
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007719 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007720}
7721
Avi Kivity851ba692009-08-24 11:10:17 +03007722static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007723{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007724 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007725 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7726 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007727 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007728 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007729
7730 return 1;
7731}
7732
Mohammed Gamal80ced182009-09-01 12:48:18 +02007733static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007734{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007735 struct vcpu_vmx *vmx = to_vmx(vcpu);
7736 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007737 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007738 u32 cpu_exec_ctrl;
7739 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007740 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007741
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007742 /*
7743 * We should never reach the point where we are emulating L2
7744 * due to invalid guest state as that means we incorrectly
7745 * allowed a nested VMEntry with an invalid vmcs12.
7746 */
7747 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7748
Avi Kivity49e9d552010-09-19 14:34:08 +02007749 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7750 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007751
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007752 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007753 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007754 return handle_interrupt_window(&vmx->vcpu);
7755
Radim Krčmář72875d82017-04-26 22:32:19 +02007756 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007757 return 1;
7758
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007759 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007760
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007761 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007762 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007763 ret = 0;
7764 goto out;
7765 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007766
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007767 if (err != EMULATE_DONE)
7768 goto emulation_error;
7769
7770 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7771 vcpu->arch.exception.pending)
7772 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007773
Gleb Natapov8d76c492013-05-08 18:38:44 +03007774 if (vcpu->arch.halt_request) {
7775 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007776 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007777 goto out;
7778 }
7779
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007780 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007781 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007782 if (need_resched())
7783 schedule();
7784 }
7785
Mohammed Gamal80ced182009-09-01 12:48:18 +02007786out:
7787 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007788
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007789emulation_error:
7790 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7791 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7792 vcpu->run->internal.ndata = 0;
7793 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007794}
7795
7796static void grow_ple_window(struct kvm_vcpu *vcpu)
7797{
7798 struct vcpu_vmx *vmx = to_vmx(vcpu);
7799 int old = vmx->ple_window;
7800
Babu Mogerc8e88712018-03-16 16:37:24 -04007801 vmx->ple_window = __grow_ple_window(old, ple_window,
7802 ple_window_grow,
7803 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007804
7805 if (vmx->ple_window != old)
7806 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007807
7808 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007809}
7810
7811static void shrink_ple_window(struct kvm_vcpu *vcpu)
7812{
7813 struct vcpu_vmx *vmx = to_vmx(vcpu);
7814 int old = vmx->ple_window;
7815
Babu Mogerc8e88712018-03-16 16:37:24 -04007816 vmx->ple_window = __shrink_ple_window(old, ple_window,
7817 ple_window_shrink,
7818 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007819
7820 if (vmx->ple_window != old)
7821 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007822
7823 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007824}
7825
7826/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007827 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7828 */
7829static void wakeup_handler(void)
7830{
7831 struct kvm_vcpu *vcpu;
7832 int cpu = smp_processor_id();
7833
7834 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7835 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7836 blocked_vcpu_list) {
7837 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7838
7839 if (pi_test_on(pi_desc) == 1)
7840 kvm_vcpu_kick(vcpu);
7841 }
7842 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7843}
7844
Peng Haoe01bca22018-04-07 05:47:32 +08007845static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007846{
7847 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7848 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7849 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7850 0ull, VMX_EPT_EXECUTABLE_MASK,
7851 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007852 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007853
7854 ept_set_mmio_spte_mask();
7855 kvm_enable_tdp();
7856}
7857
Tiejun Chenf2c76482014-10-28 10:14:47 +08007858static __init int hardware_setup(void)
7859{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007860 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007861 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007862
7863 rdmsrl_safe(MSR_EFER, &host_efer);
7864
7865 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7866 kvm_define_shared_msr(i, vmx_msr_index[i]);
7867
Radim Krčmář23611332016-09-29 22:41:33 +02007868 for (i = 0; i < VMX_BITMAP_NR; i++) {
7869 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7870 if (!vmx_bitmap[i])
7871 goto out;
7872 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007873
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007874 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7875 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7876
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007877 if (setup_vmcs_config(&vmcs_config) < 0) {
7878 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007879 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007880 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007881
7882 if (boot_cpu_has(X86_FEATURE_NX))
7883 kvm_enable_efer_bits(EFER_NX);
7884
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007885 if (boot_cpu_has(X86_FEATURE_MPX)) {
7886 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7887 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7888 }
7889
Wanpeng Li08d839c2017-03-23 05:30:08 -07007890 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7891 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007892 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007893
Tiejun Chenf2c76482014-10-28 10:14:47 +08007894 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007895 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007896 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007897 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007898 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007899
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007900 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007901 enable_ept_ad_bits = 0;
7902
Wanpeng Li8ad81822017-10-09 15:51:53 -07007903 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007904 enable_unrestricted_guest = 0;
7905
Paolo Bonziniad15a292015-01-30 16:18:49 +01007906 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007907 flexpriority_enabled = 0;
7908
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007909 if (!cpu_has_virtual_nmis())
7910 enable_vnmi = 0;
7911
Paolo Bonziniad15a292015-01-30 16:18:49 +01007912 /*
7913 * set_apic_access_page_addr() is used to reload apic access
7914 * page upon invalidation. No need to do anything if not
7915 * using the APIC_ACCESS_ADDR VMCS field.
7916 */
7917 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007918 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007919
7920 if (!cpu_has_vmx_tpr_shadow())
7921 kvm_x86_ops->update_cr8_intercept = NULL;
7922
7923 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7924 kvm_disable_largepages();
7925
Tianyu Lan877ad952018-07-19 08:40:23 +00007926#if IS_ENABLED(CONFIG_HYPERV)
7927 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7928 && enable_ept)
7929 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7930#endif
7931
Wanpeng Li0f107682017-09-28 18:06:24 -07007932 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007933 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007934 ple_window = 0;
7935 ple_window_grow = 0;
7936 ple_window_max = 0;
7937 ple_window_shrink = 0;
7938 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007939
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007940 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007941 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007942 kvm_x86_ops->sync_pir_to_irr = NULL;
7943 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007944
Haozhong Zhang64903d62015-10-20 15:39:09 +08007945 if (cpu_has_vmx_tsc_scaling()) {
7946 kvm_has_tsc_control = true;
7947 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7948 kvm_tsc_scaling_ratio_frac_bits = 48;
7949 }
7950
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007951 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7952
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007953 if (enable_ept)
7954 vmx_enable_tdp();
7955 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007956 kvm_disable_tdp();
7957
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007958 if (!nested) {
7959 kvm_x86_ops->get_nested_state = NULL;
7960 kvm_x86_ops->set_nested_state = NULL;
7961 }
7962
Kai Huang843e4332015-01-28 10:54:28 +08007963 /*
7964 * Only enable PML when hardware supports PML feature, and both EPT
7965 * and EPT A/D bit features are enabled -- PML depends on them to work.
7966 */
7967 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7968 enable_pml = 0;
7969
7970 if (!enable_pml) {
7971 kvm_x86_ops->slot_enable_log_dirty = NULL;
7972 kvm_x86_ops->slot_disable_log_dirty = NULL;
7973 kvm_x86_ops->flush_log_dirty = NULL;
7974 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7975 }
7976
Sean Christophersond264ee02018-08-27 15:21:12 -07007977 if (!cpu_has_vmx_preemption_timer())
7978 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7979
Yunhong Jiang64672c92016-06-13 14:19:59 -07007980 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7981 u64 vmx_msr;
7982
7983 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7984 cpu_preemption_timer_multi =
7985 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7986 } else {
7987 kvm_x86_ops->set_hv_timer = NULL;
7988 kvm_x86_ops->cancel_hv_timer = NULL;
7989 }
7990
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007991 if (!cpu_has_vmx_shadow_vmcs())
7992 enable_shadow_vmcs = 0;
7993 if (enable_shadow_vmcs)
7994 init_vmcs_shadow_fields();
7995
Feng Wubf9f6ac2015-09-18 22:29:55 +08007996 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007997 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007998
Ashok Rajc45dcc72016-06-22 14:59:56 +08007999 kvm_mce_cap_supported |= MCG_LMCE_P;
8000
Tiejun Chenf2c76482014-10-28 10:14:47 +08008001 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008002
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008003out:
Radim Krčmář23611332016-09-29 22:41:33 +02008004 for (i = 0; i < VMX_BITMAP_NR; i++)
8005 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008006
8007 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008008}
8009
8010static __exit void hardware_unsetup(void)
8011{
Radim Krčmář23611332016-09-29 22:41:33 +02008012 int i;
8013
8014 for (i = 0; i < VMX_BITMAP_NR; i++)
8015 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008016
Tiejun Chenf2c76482014-10-28 10:14:47 +08008017 free_kvm_area();
8018}
8019
Avi Kivity6aa8b732006-12-10 02:21:36 -08008020/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008021 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8022 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8023 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008024static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008025{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008026 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008027 grow_ple_window(vcpu);
8028
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008029 /*
8030 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8031 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8032 * never set PAUSE_EXITING and just set PLE if supported,
8033 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8034 */
8035 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008036 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008037}
8038
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008039static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008040{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008041 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008042}
8043
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008044static int handle_mwait(struct kvm_vcpu *vcpu)
8045{
8046 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8047 return handle_nop(vcpu);
8048}
8049
Jim Mattson45ec3682017-08-23 16:32:04 -07008050static int handle_invalid_op(struct kvm_vcpu *vcpu)
8051{
8052 kvm_queue_exception(vcpu, UD_VECTOR);
8053 return 1;
8054}
8055
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008056static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8057{
8058 return 1;
8059}
8060
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008061static int handle_monitor(struct kvm_vcpu *vcpu)
8062{
8063 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8064 return handle_nop(vcpu);
8065}
8066
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008067/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008068 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8069 * set the success or error code of an emulated VMX instruction, as specified
8070 * by Vol 2B, VMX Instruction Reference, "Conventions".
8071 */
8072static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8073{
8074 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8075 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8076 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8077}
8078
8079static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8080{
8081 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8082 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8083 X86_EFLAGS_SF | X86_EFLAGS_OF))
8084 | X86_EFLAGS_CF);
8085}
8086
Abel Gordon145c28d2013-04-18 14:36:55 +03008087static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008088 u32 vm_instruction_error)
8089{
8090 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8091 /*
8092 * failValid writes the error number to the current VMCS, which
8093 * can't be done there isn't a current VMCS.
8094 */
8095 nested_vmx_failInvalid(vcpu);
8096 return;
8097 }
8098 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8099 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8100 X86_EFLAGS_SF | X86_EFLAGS_OF))
8101 | X86_EFLAGS_ZF);
8102 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8103 /*
8104 * We don't need to force a shadow sync because
8105 * VM_INSTRUCTION_ERROR is not shadowed
8106 */
8107}
Abel Gordon145c28d2013-04-18 14:36:55 +03008108
Wincy Vanff651cb2014-12-11 08:52:58 +03008109static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8110{
8111 /* TODO: not to reset guest simply here. */
8112 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008113 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008114}
8115
Jan Kiszkaf4124502014-03-07 20:03:13 +01008116static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8117{
8118 struct vcpu_vmx *vmx =
8119 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8120
8121 vmx->nested.preemption_timer_expired = true;
8122 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8123 kvm_vcpu_kick(&vmx->vcpu);
8124
8125 return HRTIMER_NORESTART;
8126}
8127
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008128/*
Bandan Das19677e32014-05-06 02:19:15 -04008129 * Decode the memory-address operand of a vmx instruction, as recorded on an
8130 * exit caused by such an instruction (run by a guest hypervisor).
8131 * On success, returns 0. When the operand is invalid, returns 1 and throws
8132 * #UD or #GP.
8133 */
8134static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8135 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008136 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008137{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008138 gva_t off;
8139 bool exn;
8140 struct kvm_segment s;
8141
Bandan Das19677e32014-05-06 02:19:15 -04008142 /*
8143 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8144 * Execution", on an exit, vmx_instruction_info holds most of the
8145 * addressing components of the operand. Only the displacement part
8146 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8147 * For how an actual address is calculated from all these components,
8148 * refer to Vol. 1, "Operand Addressing".
8149 */
8150 int scaling = vmx_instruction_info & 3;
8151 int addr_size = (vmx_instruction_info >> 7) & 7;
8152 bool is_reg = vmx_instruction_info & (1u << 10);
8153 int seg_reg = (vmx_instruction_info >> 15) & 7;
8154 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8155 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8156 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8157 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8158
8159 if (is_reg) {
8160 kvm_queue_exception(vcpu, UD_VECTOR);
8161 return 1;
8162 }
8163
8164 /* Addr = segment_base + offset */
8165 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008166 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008167 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008168 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008169 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008170 off += kvm_register_read(vcpu, index_reg)<<scaling;
8171 vmx_get_segment(vcpu, &s, seg_reg);
8172 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008173
8174 if (addr_size == 1) /* 32 bit */
8175 *ret &= 0xffffffff;
8176
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008177 /* Checks for #GP/#SS exceptions. */
8178 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008179 if (is_long_mode(vcpu)) {
8180 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8181 * non-canonical form. This is the only check on the memory
8182 * destination for long mode!
8183 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008184 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008185 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008186 /* Protected mode: apply checks for segment validity in the
8187 * following order:
8188 * - segment type check (#GP(0) may be thrown)
8189 * - usability check (#GP(0)/#SS(0))
8190 * - limit check (#GP(0)/#SS(0))
8191 */
8192 if (wr)
8193 /* #GP(0) if the destination operand is located in a
8194 * read-only data segment or any code segment.
8195 */
8196 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8197 else
8198 /* #GP(0) if the source operand is located in an
8199 * execute-only code segment
8200 */
8201 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008202 if (exn) {
8203 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8204 return 1;
8205 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008206 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8207 */
8208 exn = (s.unusable != 0);
8209 /* Protected mode: #GP(0)/#SS(0) if the memory
8210 * operand is outside the segment limit.
8211 */
8212 exn = exn || (off + sizeof(u64) > s.limit);
8213 }
8214 if (exn) {
8215 kvm_queue_exception_e(vcpu,
8216 seg_reg == VCPU_SREG_SS ?
8217 SS_VECTOR : GP_VECTOR,
8218 0);
8219 return 1;
8220 }
8221
Bandan Das19677e32014-05-06 02:19:15 -04008222 return 0;
8223}
8224
Radim Krčmářcbf71272017-05-19 15:48:51 +02008225static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008226{
8227 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008228 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008229
8230 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008231 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008232 return 1;
8233
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008234 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008235 kvm_inject_page_fault(vcpu, &e);
8236 return 1;
8237 }
8238
Bandan Das3573e222014-05-06 02:19:16 -04008239 return 0;
8240}
8241
Liran Alonabfc52c2018-06-23 02:35:13 +03008242/*
8243 * Allocate a shadow VMCS and associate it with the currently loaded
8244 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8245 * VMCS is also VMCLEARed, so that it is ready for use.
8246 */
8247static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8248{
8249 struct vcpu_vmx *vmx = to_vmx(vcpu);
8250 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8251
8252 /*
8253 * We should allocate a shadow vmcs for vmcs01 only when L1
8254 * executes VMXON and free it when L1 executes VMXOFF.
8255 * As it is invalid to execute VMXON twice, we shouldn't reach
8256 * here when vmcs01 already have an allocated shadow vmcs.
8257 */
8258 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8259
8260 if (!loaded_vmcs->shadow_vmcs) {
8261 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8262 if (loaded_vmcs->shadow_vmcs)
8263 vmcs_clear(loaded_vmcs->shadow_vmcs);
8264 }
8265 return loaded_vmcs->shadow_vmcs;
8266}
8267
Jim Mattsone29acc52016-11-30 12:03:43 -08008268static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8269{
8270 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008271 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008272
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008273 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8274 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008275 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008276
8277 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8278 if (!vmx->nested.cached_vmcs12)
8279 goto out_cached_vmcs12;
8280
Liran Alon61ada742018-06-23 02:35:08 +03008281 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8282 if (!vmx->nested.cached_shadow_vmcs12)
8283 goto out_cached_shadow_vmcs12;
8284
Liran Alonabfc52c2018-06-23 02:35:13 +03008285 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8286 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008287
Jim Mattsone29acc52016-11-30 12:03:43 -08008288 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8289 HRTIMER_MODE_REL_PINNED);
8290 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8291
Roman Kagan63aff652018-07-19 21:59:07 +03008292 vmx->nested.vpid02 = allocate_vpid();
8293
Jim Mattsone29acc52016-11-30 12:03:43 -08008294 vmx->nested.vmxon = true;
8295 return 0;
8296
8297out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008298 kfree(vmx->nested.cached_shadow_vmcs12);
8299
8300out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008301 kfree(vmx->nested.cached_vmcs12);
8302
8303out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008304 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008305
Jim Mattsonde3a0022017-11-27 17:22:25 -06008306out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008307 return -ENOMEM;
8308}
8309
Bandan Das3573e222014-05-06 02:19:16 -04008310/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008311 * Emulate the VMXON instruction.
8312 * Currently, we just remember that VMX is active, and do not save or even
8313 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8314 * do not currently need to store anything in that guest-allocated memory
8315 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8316 * argument is different from the VMXON pointer (which the spec says they do).
8317 */
8318static int handle_vmon(struct kvm_vcpu *vcpu)
8319{
Jim Mattsone29acc52016-11-30 12:03:43 -08008320 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008321 gpa_t vmptr;
8322 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008323 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008324 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8325 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008326
Jim Mattson70f3aac2017-04-26 08:53:46 -07008327 /*
8328 * The Intel VMX Instruction Reference lists a bunch of bits that are
8329 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8330 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8331 * Otherwise, we should fail with #UD. But most faulting conditions
8332 * have already been checked by hardware, prior to the VM-exit for
8333 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8334 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008335 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008336 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008337 kvm_queue_exception(vcpu, UD_VECTOR);
8338 return 1;
8339 }
8340
Felix Wilhelm727ba742018-06-11 09:43:44 +02008341 /* CPL=0 must be checked manually. */
8342 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008343 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008344 return 1;
8345 }
8346
Abel Gordon145c28d2013-04-18 14:36:55 +03008347 if (vmx->nested.vmxon) {
8348 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008349 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008350 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008351
Haozhong Zhang3b840802016-06-22 14:59:54 +08008352 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008353 != VMXON_NEEDED_FEATURES) {
8354 kvm_inject_gp(vcpu, 0);
8355 return 1;
8356 }
8357
Radim Krčmářcbf71272017-05-19 15:48:51 +02008358 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008359 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008360
8361 /*
8362 * SDM 3: 24.11.5
8363 * The first 4 bytes of VMXON region contain the supported
8364 * VMCS revision identifier
8365 *
8366 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8367 * which replaces physical address width with 32
8368 */
8369 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8370 nested_vmx_failInvalid(vcpu);
8371 return kvm_skip_emulated_instruction(vcpu);
8372 }
8373
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008374 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8375 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008376 nested_vmx_failInvalid(vcpu);
8377 return kvm_skip_emulated_instruction(vcpu);
8378 }
8379 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
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 nested_vmx_failInvalid(vcpu);
8383 return kvm_skip_emulated_instruction(vcpu);
8384 }
8385 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008386 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008387
8388 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008389 ret = enter_vmx_operation(vcpu);
8390 if (ret)
8391 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008392
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008393 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008394 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008395}
8396
8397/*
8398 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8399 * for running VMX instructions (except VMXON, whose prerequisites are
8400 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008401 * Note that many of these exceptions have priority over VM exits, so they
8402 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008403 */
8404static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8405{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008406 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008407 kvm_queue_exception(vcpu, UD_VECTOR);
8408 return 0;
8409 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008410
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008411 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008412 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008413 return 0;
8414 }
8415
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008416 return 1;
8417}
8418
David Matlack8ca44e82017-08-01 14:00:39 -07008419static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8420{
8421 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8422 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8423}
8424
Abel Gordone7953d72013-04-18 14:37:55 +03008425static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8426{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008427 if (vmx->nested.current_vmptr == -1ull)
8428 return;
8429
Abel Gordon012f83c2013-04-18 14:39:25 +03008430 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008431 /* copy to memory all shadowed fields in case
8432 they were modified */
8433 copy_shadow_to_vmcs12(vmx);
8434 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008435 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008436 }
Wincy Van705699a2015-02-03 23:58:17 +08008437 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008438
8439 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008440 kvm_vcpu_write_guest_page(&vmx->vcpu,
8441 vmx->nested.current_vmptr >> PAGE_SHIFT,
8442 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008443
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008444 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008445}
8446
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008447/*
8448 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8449 * just stops using VMX.
8450 */
8451static void free_nested(struct vcpu_vmx *vmx)
8452{
Wanpeng Lib7455822017-11-22 14:04:00 -08008453 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008454 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008455
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008456 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008457 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008458 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008459 vmx->nested.posted_intr_nv = -1;
8460 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008461 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008462 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008463 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8464 free_vmcs(vmx->vmcs01.shadow_vmcs);
8465 vmx->vmcs01.shadow_vmcs = NULL;
8466 }
David Matlack4f2777b2016-07-13 17:16:37 -07008467 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008468 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008469 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008470 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008471 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008472 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008473 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008474 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008475 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008476 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008477 }
Wincy Van705699a2015-02-03 23:58:17 +08008478 if (vmx->nested.pi_desc_page) {
8479 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008480 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008481 vmx->nested.pi_desc_page = NULL;
8482 vmx->nested.pi_desc = NULL;
8483 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008484
Jim Mattsonde3a0022017-11-27 17:22:25 -06008485 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008486}
8487
8488/* Emulate the VMXOFF instruction */
8489static int handle_vmoff(struct kvm_vcpu *vcpu)
8490{
8491 if (!nested_vmx_check_permission(vcpu))
8492 return 1;
8493 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008494 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008495 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008496}
8497
Nadav Har'El27d6c862011-05-25 23:06:59 +03008498/* Emulate the VMCLEAR instruction */
8499static int handle_vmclear(struct kvm_vcpu *vcpu)
8500{
8501 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008502 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008503 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008504
8505 if (!nested_vmx_check_permission(vcpu))
8506 return 1;
8507
Radim Krčmářcbf71272017-05-19 15:48:51 +02008508 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008509 return 1;
8510
Radim Krčmářcbf71272017-05-19 15:48:51 +02008511 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8512 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8513 return kvm_skip_emulated_instruction(vcpu);
8514 }
8515
8516 if (vmptr == vmx->nested.vmxon_ptr) {
8517 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8518 return kvm_skip_emulated_instruction(vcpu);
8519 }
8520
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008521 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008522 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008523
Jim Mattson587d7e722017-03-02 12:41:48 -08008524 kvm_vcpu_write_guest(vcpu,
8525 vmptr + offsetof(struct vmcs12, launch_state),
8526 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008527
Nadav Har'El27d6c862011-05-25 23:06:59 +03008528 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008529 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008530}
8531
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008532static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8533
8534/* Emulate the VMLAUNCH instruction */
8535static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8536{
8537 return nested_vmx_run(vcpu, true);
8538}
8539
8540/* Emulate the VMRESUME instruction */
8541static int handle_vmresume(struct kvm_vcpu *vcpu)
8542{
8543
8544 return nested_vmx_run(vcpu, false);
8545}
8546
Nadav Har'El49f705c2011-05-25 23:08:30 +03008547/*
8548 * Read a vmcs12 field. Since these can have varying lengths and we return
8549 * one type, we chose the biggest type (u64) and zero-extend the return value
8550 * to that size. Note that the caller, handle_vmread, might need to use only
8551 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8552 * 64-bit fields are to be returned).
8553 */
Liran Alone2536742018-06-23 02:35:02 +03008554static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008555 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556{
8557 short offset = vmcs_field_to_offset(field);
8558 char *p;
8559
8560 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008561 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562
Liran Alone2536742018-06-23 02:35:02 +03008563 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564
Jim Mattsond37f4262017-12-22 12:12:16 -08008565 switch (vmcs_field_width(field)) {
8566 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008569 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008571 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008572 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008573 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008575 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008576 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008577 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008578 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008579 WARN_ON(1);
8580 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008581 }
8582}
8583
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584
Liran Alone2536742018-06-23 02:35:02 +03008585static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008586 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008587 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008588 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008589 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591
Jim Mattsond37f4262017-12-22 12:12:16 -08008592 switch (vmcs_field_width(field)) {
8593 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008596 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008597 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008598 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008599 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008600 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008601 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008602 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008603 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008604 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008605 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008606 WARN_ON(1);
8607 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008608 }
8609
8610}
8611
Jim Mattsonf4160e42018-05-29 09:11:33 -07008612/*
8613 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8614 * they have been modified by the L1 guest. Note that the "read-only"
8615 * VM-exit information fields are actually writable if the vCPU is
8616 * configured to support "VMWRITE to any supported field in the VMCS."
8617 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008618static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8619{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008620 const u16 *fields[] = {
8621 shadow_read_write_fields,
8622 shadow_read_only_fields
8623 };
8624 const int max_fields[] = {
8625 max_shadow_read_write_fields,
8626 max_shadow_read_only_fields
8627 };
8628 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008629 unsigned long field;
8630 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008631 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008632
Jan Kiszka282da872014-10-08 18:05:39 +02008633 preempt_disable();
8634
Abel Gordon16f5b902013-04-18 14:38:25 +03008635 vmcs_load(shadow_vmcs);
8636
Jim Mattsonf4160e42018-05-29 09:11:33 -07008637 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8638 for (i = 0; i < max_fields[q]; i++) {
8639 field = fields[q][i];
8640 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008641 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008642 }
8643 /*
8644 * Skip the VM-exit information fields if they are read-only.
8645 */
8646 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8647 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008648 }
8649
8650 vmcs_clear(shadow_vmcs);
8651 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008652
8653 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008654}
8655
Abel Gordonc3114422013-04-18 14:38:55 +03008656static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8657{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008658 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008659 shadow_read_write_fields,
8660 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008661 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008662 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008663 max_shadow_read_write_fields,
8664 max_shadow_read_only_fields
8665 };
8666 int i, q;
8667 unsigned long field;
8668 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008669 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008670
8671 vmcs_load(shadow_vmcs);
8672
Mathias Krausec2bae892013-06-26 20:36:21 +02008673 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008674 for (i = 0; i < max_fields[q]; i++) {
8675 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008676 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008677 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008678 }
8679 }
8680
8681 vmcs_clear(shadow_vmcs);
8682 vmcs_load(vmx->loaded_vmcs->vmcs);
8683}
8684
Nadav Har'El49f705c2011-05-25 23:08:30 +03008685/*
8686 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8687 * used before) all generate the same failure when it is missing.
8688 */
8689static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8690{
8691 struct vcpu_vmx *vmx = to_vmx(vcpu);
8692 if (vmx->nested.current_vmptr == -1ull) {
8693 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008694 return 0;
8695 }
8696 return 1;
8697}
8698
8699static int handle_vmread(struct kvm_vcpu *vcpu)
8700{
8701 unsigned long field;
8702 u64 field_value;
8703 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8704 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8705 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008706 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008707
Kyle Hueyeb277562016-11-29 12:40:39 -08008708 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008709 return 1;
8710
Kyle Huey6affcbe2016-11-29 12:40:40 -08008711 if (!nested_vmx_check_vmcs12(vcpu))
8712 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008713
Liran Alon6d894f42018-06-23 02:35:09 +03008714 if (!is_guest_mode(vcpu))
8715 vmcs12 = get_vmcs12(vcpu);
8716 else {
8717 /*
8718 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8719 * to shadowed-field sets the ALU flags for VMfailInvalid.
8720 */
8721 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8722 nested_vmx_failInvalid(vcpu);
8723 return kvm_skip_emulated_instruction(vcpu);
8724 }
8725 vmcs12 = get_shadow_vmcs12(vcpu);
8726 }
8727
Nadav Har'El49f705c2011-05-25 23:08:30 +03008728 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008729 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008730 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008731 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008732 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008733 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008734 }
8735 /*
8736 * Now copy part of this value to register or memory, as requested.
8737 * Note that the number of bits actually copied is 32 or 64 depending
8738 * on the guest's mode (32 or 64 bit), not on the given field's length.
8739 */
8740 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008741 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008742 field_value);
8743 } else {
8744 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008745 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008746 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008747 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008748 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8749 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008750 }
8751
8752 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008753 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008754}
8755
8756
8757static int handle_vmwrite(struct kvm_vcpu *vcpu)
8758{
8759 unsigned long field;
8760 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008761 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008762 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8763 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008764
Nadav Har'El49f705c2011-05-25 23:08:30 +03008765 /* The value to write might be 32 or 64 bits, depending on L1's long
8766 * mode, and eventually we need to write that into a field of several
8767 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008768 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008769 * bits into the vmcs12 field.
8770 */
8771 u64 field_value = 0;
8772 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008773 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008774
Kyle Hueyeb277562016-11-29 12:40:39 -08008775 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008776 return 1;
8777
Kyle Huey6affcbe2016-11-29 12:40:40 -08008778 if (!nested_vmx_check_vmcs12(vcpu))
8779 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008780
Nadav Har'El49f705c2011-05-25 23:08:30 +03008781 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008782 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008783 (((vmx_instruction_info) >> 3) & 0xf));
8784 else {
8785 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008786 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008787 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008788 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8789 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008790 kvm_inject_page_fault(vcpu, &e);
8791 return 1;
8792 }
8793 }
8794
8795
Nadav Amit27e6fb52014-06-18 17:19:26 +03008796 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008797 /*
8798 * If the vCPU supports "VMWRITE to any supported field in the
8799 * VMCS," then the "read-only" fields are actually read/write.
8800 */
8801 if (vmcs_field_readonly(field) &&
8802 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008803 nested_vmx_failValid(vcpu,
8804 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008805 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008806 }
8807
Liran Alon6d894f42018-06-23 02:35:09 +03008808 if (!is_guest_mode(vcpu))
8809 vmcs12 = get_vmcs12(vcpu);
8810 else {
8811 /*
8812 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8813 * to shadowed-field sets the ALU flags for VMfailInvalid.
8814 */
8815 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8816 nested_vmx_failInvalid(vcpu);
8817 return kvm_skip_emulated_instruction(vcpu);
8818 }
8819 vmcs12 = get_shadow_vmcs12(vcpu);
8820
8821 }
8822
8823 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008824 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008825 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008826 }
8827
Liran Alon6d894f42018-06-23 02:35:09 +03008828 /*
8829 * Do not track vmcs12 dirty-state if in guest-mode
8830 * as we actually dirty shadow vmcs12 instead of vmcs12.
8831 */
8832 if (!is_guest_mode(vcpu)) {
8833 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008834#define SHADOW_FIELD_RW(x) case x:
8835#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008836 /*
8837 * The fields that can be updated by L1 without a vmexit are
8838 * always updated in the vmcs02, the others go down the slow
8839 * path of prepare_vmcs02.
8840 */
8841 break;
8842 default:
8843 vmx->nested.dirty_vmcs12 = true;
8844 break;
8845 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008846 }
8847
Nadav Har'El49f705c2011-05-25 23:08:30 +03008848 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008849 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008850}
8851
Jim Mattsona8bc2842016-11-30 12:03:44 -08008852static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8853{
8854 vmx->nested.current_vmptr = vmptr;
8855 if (enable_shadow_vmcs) {
8856 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8857 SECONDARY_EXEC_SHADOW_VMCS);
8858 vmcs_write64(VMCS_LINK_POINTER,
8859 __pa(vmx->vmcs01.shadow_vmcs));
8860 vmx->nested.sync_shadow_vmcs = true;
8861 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008862 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008863}
8864
Nadav Har'El63846662011-05-25 23:07:29 +03008865/* Emulate the VMPTRLD instruction */
8866static int handle_vmptrld(struct kvm_vcpu *vcpu)
8867{
8868 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008869 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008870
8871 if (!nested_vmx_check_permission(vcpu))
8872 return 1;
8873
Radim Krčmářcbf71272017-05-19 15:48:51 +02008874 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008875 return 1;
8876
Radim Krčmářcbf71272017-05-19 15:48:51 +02008877 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8878 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8879 return kvm_skip_emulated_instruction(vcpu);
8880 }
8881
8882 if (vmptr == vmx->nested.vmxon_ptr) {
8883 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8884 return kvm_skip_emulated_instruction(vcpu);
8885 }
8886
Nadav Har'El63846662011-05-25 23:07:29 +03008887 if (vmx->nested.current_vmptr != vmptr) {
8888 struct vmcs12 *new_vmcs12;
8889 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008890 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8891 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008892 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008893 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008894 }
8895 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008896 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008897 (new_vmcs12->hdr.shadow_vmcs &&
8898 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008899 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008900 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008901 nested_vmx_failValid(vcpu,
8902 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008903 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008904 }
Nadav Har'El63846662011-05-25 23:07:29 +03008905
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008906 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008907 /*
8908 * Load VMCS12 from guest memory since it is not already
8909 * cached.
8910 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008911 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8912 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008913 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008914
Jim Mattsona8bc2842016-11-30 12:03:44 -08008915 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008916 }
8917
8918 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008919 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008920}
8921
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008922/* Emulate the VMPTRST instruction */
8923static int handle_vmptrst(struct kvm_vcpu *vcpu)
8924{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008925 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8926 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8927 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008928 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008929 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008930
8931 if (!nested_vmx_check_permission(vcpu))
8932 return 1;
8933
Sean Christopherson0a06d422018-07-19 10:31:00 -07008934 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008935 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008936 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008937 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8938 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008939 kvm_inject_page_fault(vcpu, &e);
8940 return 1;
8941 }
8942 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008943 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008944}
8945
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008946/* Emulate the INVEPT instruction */
8947static int handle_invept(struct kvm_vcpu *vcpu)
8948{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008949 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008950 u32 vmx_instruction_info, types;
8951 unsigned long type;
8952 gva_t gva;
8953 struct x86_exception e;
8954 struct {
8955 u64 eptp, gpa;
8956 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008958 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008959 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008960 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961 kvm_queue_exception(vcpu, UD_VECTOR);
8962 return 1;
8963 }
8964
8965 if (!nested_vmx_check_permission(vcpu))
8966 return 1;
8967
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008968 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008969 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008970
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008971 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008972
Jim Mattson85c856b2016-10-26 08:38:38 -07008973 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008974 nested_vmx_failValid(vcpu,
8975 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008976 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008977 }
8978
8979 /* According to the Intel VMX instruction reference, the memory
8980 * operand is read even if it isn't needed (e.g., for type==global)
8981 */
8982 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008983 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008984 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008985 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008986 kvm_inject_page_fault(vcpu, &e);
8987 return 1;
8988 }
8989
8990 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008991 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008992 /*
8993 * TODO: track mappings and invalidate
8994 * single context requests appropriately
8995 */
8996 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008997 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008998 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008999 nested_vmx_succeed(vcpu);
9000 break;
9001 default:
9002 BUG_ON(1);
9003 break;
9004 }
9005
Kyle Huey6affcbe2016-11-29 12:40:40 -08009006 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009007}
9008
Liran Alon3d5bdae2018-10-08 23:42:18 +03009009static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9010{
9011 struct vcpu_vmx *vmx = to_vmx(vcpu);
9012
9013 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9014}
9015
Petr Matouseka642fc32014-09-23 20:22:30 +02009016static int handle_invvpid(struct kvm_vcpu *vcpu)
9017{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009018 struct vcpu_vmx *vmx = to_vmx(vcpu);
9019 u32 vmx_instruction_info;
9020 unsigned long type, types;
9021 gva_t gva;
9022 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009023 struct {
9024 u64 vpid;
9025 u64 gla;
9026 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009027 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009028
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009029 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009030 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009031 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009032 kvm_queue_exception(vcpu, UD_VECTOR);
9033 return 1;
9034 }
9035
9036 if (!nested_vmx_check_permission(vcpu))
9037 return 1;
9038
9039 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9040 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9041
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009042 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009043 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009044
Jim Mattson85c856b2016-10-26 08:38:38 -07009045 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009046 nested_vmx_failValid(vcpu,
9047 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009048 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009049 }
9050
9051 /* according to the intel vmx instruction reference, the memory
9052 * operand is read even if it isn't needed (e.g., for type==global)
9053 */
9054 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9055 vmx_instruction_info, false, &gva))
9056 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009057 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009058 kvm_inject_page_fault(vcpu, &e);
9059 return 1;
9060 }
Jim Mattson40352602017-06-28 09:37:37 -07009061 if (operand.vpid >> 16) {
9062 nested_vmx_failValid(vcpu,
9063 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9064 return kvm_skip_emulated_instruction(vcpu);
9065 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009066
Liran Alon3d5bdae2018-10-08 23:42:18 +03009067 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009068 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009069 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009070 if (!operand.vpid ||
9071 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009072 nested_vmx_failValid(vcpu,
9073 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9074 return kvm_skip_emulated_instruction(vcpu);
9075 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009076 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009077 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009078 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009079 } else
Liran Alon327c0722018-10-08 23:42:19 +03009080 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009081 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009082 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009083 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009084 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009085 nested_vmx_failValid(vcpu,
9086 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009087 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009088 }
Liran Alon327c0722018-10-08 23:42:19 +03009089 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009090 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009091 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009092 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009093 break;
9094 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009095 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009096 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009097 }
9098
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009099 nested_vmx_succeed(vcpu);
9100
Kyle Huey6affcbe2016-11-29 12:40:40 -08009101 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009102}
9103
Junaid Shahideb4b2482018-06-27 14:59:14 -07009104static int handle_invpcid(struct kvm_vcpu *vcpu)
9105{
9106 u32 vmx_instruction_info;
9107 unsigned long type;
9108 bool pcid_enabled;
9109 gva_t gva;
9110 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009111 unsigned i;
9112 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009113 struct {
9114 u64 pcid;
9115 u64 gla;
9116 } operand;
9117
9118 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9119 kvm_queue_exception(vcpu, UD_VECTOR);
9120 return 1;
9121 }
9122
9123 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9124 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9125
9126 if (type > 3) {
9127 kvm_inject_gp(vcpu, 0);
9128 return 1;
9129 }
9130
9131 /* According to the Intel instruction reference, the memory operand
9132 * is read even if it isn't needed (e.g., for type==all)
9133 */
9134 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9135 vmx_instruction_info, false, &gva))
9136 return 1;
9137
9138 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9139 kvm_inject_page_fault(vcpu, &e);
9140 return 1;
9141 }
9142
9143 if (operand.pcid >> 12 != 0) {
9144 kvm_inject_gp(vcpu, 0);
9145 return 1;
9146 }
9147
9148 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9149
9150 switch (type) {
9151 case INVPCID_TYPE_INDIV_ADDR:
9152 if ((!pcid_enabled && (operand.pcid != 0)) ||
9153 is_noncanonical_address(operand.gla, vcpu)) {
9154 kvm_inject_gp(vcpu, 0);
9155 return 1;
9156 }
9157 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9158 return kvm_skip_emulated_instruction(vcpu);
9159
9160 case INVPCID_TYPE_SINGLE_CTXT:
9161 if (!pcid_enabled && (operand.pcid != 0)) {
9162 kvm_inject_gp(vcpu, 0);
9163 return 1;
9164 }
9165
9166 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9167 kvm_mmu_sync_roots(vcpu);
9168 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9169 }
9170
Junaid Shahidb94742c2018-06-27 14:59:20 -07009171 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9172 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9173 == operand.pcid)
9174 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009175
Junaid Shahidb94742c2018-06-27 14:59:20 -07009176 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009177 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009178 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009179 * given PCID, then nothing needs to be done here because a
9180 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009181 */
9182
9183 return kvm_skip_emulated_instruction(vcpu);
9184
9185 case INVPCID_TYPE_ALL_NON_GLOBAL:
9186 /*
9187 * Currently, KVM doesn't mark global entries in the shadow
9188 * page tables, so a non-global flush just degenerates to a
9189 * global flush. If needed, we could optimize this later by
9190 * keeping track of global entries in shadow page tables.
9191 */
9192
9193 /* fall-through */
9194 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9195 kvm_mmu_unload(vcpu);
9196 return kvm_skip_emulated_instruction(vcpu);
9197
9198 default:
9199 BUG(); /* We have already checked above that type <= 3 */
9200 }
9201}
9202
Kai Huang843e4332015-01-28 10:54:28 +08009203static int handle_pml_full(struct kvm_vcpu *vcpu)
9204{
9205 unsigned long exit_qualification;
9206
9207 trace_kvm_pml_full(vcpu->vcpu_id);
9208
9209 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9210
9211 /*
9212 * PML buffer FULL happened while executing iret from NMI,
9213 * "blocked by NMI" bit has to be set before next VM entry.
9214 */
9215 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009216 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009217 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9218 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9219 GUEST_INTR_STATE_NMI);
9220
9221 /*
9222 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9223 * here.., and there's no userspace involvement needed for PML.
9224 */
9225 return 1;
9226}
9227
Yunhong Jiang64672c92016-06-13 14:19:59 -07009228static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9229{
Sean Christophersond264ee02018-08-27 15:21:12 -07009230 if (!to_vmx(vcpu)->req_immediate_exit)
9231 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009232 return 1;
9233}
9234
Bandan Das41ab9372017-08-03 15:54:43 -04009235static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9236{
9237 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009238 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9239
9240 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009241 switch (address & VMX_EPTP_MT_MASK) {
9242 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009243 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009244 return false;
9245 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009246 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009247 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009248 return false;
9249 break;
9250 default:
9251 return false;
9252 }
9253
David Hildenbrandbb97a012017-08-10 23:15:28 +02009254 /* only 4 levels page-walk length are valid */
9255 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009256 return false;
9257
9258 /* Reserved bits should not be set */
9259 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9260 return false;
9261
9262 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009263 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009264 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009265 return false;
9266 }
9267
9268 return true;
9269}
9270
9271static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9272 struct vmcs12 *vmcs12)
9273{
9274 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9275 u64 address;
9276 bool accessed_dirty;
9277 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9278
9279 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9280 !nested_cpu_has_ept(vmcs12))
9281 return 1;
9282
9283 if (index >= VMFUNC_EPTP_ENTRIES)
9284 return 1;
9285
9286
9287 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9288 &address, index * 8, 8))
9289 return 1;
9290
David Hildenbrandbb97a012017-08-10 23:15:28 +02009291 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009292
9293 /*
9294 * If the (L2) guest does a vmfunc to the currently
9295 * active ept pointer, we don't have to do anything else
9296 */
9297 if (vmcs12->ept_pointer != address) {
9298 if (!valid_ept_address(vcpu, address))
9299 return 1;
9300
9301 kvm_mmu_unload(vcpu);
9302 mmu->ept_ad = accessed_dirty;
9303 mmu->base_role.ad_disabled = !accessed_dirty;
9304 vmcs12->ept_pointer = address;
9305 /*
9306 * TODO: Check what's the correct approach in case
9307 * mmu reload fails. Currently, we just let the next
9308 * reload potentially fail
9309 */
9310 kvm_mmu_reload(vcpu);
9311 }
9312
9313 return 0;
9314}
9315
Bandan Das2a499e42017-08-03 15:54:41 -04009316static int handle_vmfunc(struct kvm_vcpu *vcpu)
9317{
Bandan Das27c42a12017-08-03 15:54:42 -04009318 struct vcpu_vmx *vmx = to_vmx(vcpu);
9319 struct vmcs12 *vmcs12;
9320 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9321
9322 /*
9323 * VMFUNC is only supported for nested guests, but we always enable the
9324 * secondary control for simplicity; for non-nested mode, fake that we
9325 * didn't by injecting #UD.
9326 */
9327 if (!is_guest_mode(vcpu)) {
9328 kvm_queue_exception(vcpu, UD_VECTOR);
9329 return 1;
9330 }
9331
9332 vmcs12 = get_vmcs12(vcpu);
9333 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9334 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009335
9336 switch (function) {
9337 case 0:
9338 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9339 goto fail;
9340 break;
9341 default:
9342 goto fail;
9343 }
9344 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009345
9346fail:
9347 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9348 vmcs_read32(VM_EXIT_INTR_INFO),
9349 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009350 return 1;
9351}
9352
Sean Christopherson0b665d32018-08-14 09:33:34 -07009353static int handle_encls(struct kvm_vcpu *vcpu)
9354{
9355 /*
9356 * SGX virtualization is not yet supported. There is no software
9357 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9358 * to prevent the guest from executing ENCLS.
9359 */
9360 kvm_queue_exception(vcpu, UD_VECTOR);
9361 return 1;
9362}
9363
Nadav Har'El0140cae2011-05-25 23:06:28 +03009364/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009365 * The exit handlers return 1 if the exit was handled fully and guest execution
9366 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9367 * to be done to userspace and return 0.
9368 */
Mathias Krause772e0312012-08-30 01:30:19 +02009369static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009370 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9371 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009372 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009373 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009374 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009375 [EXIT_REASON_CR_ACCESS] = handle_cr,
9376 [EXIT_REASON_DR_ACCESS] = handle_dr,
9377 [EXIT_REASON_CPUID] = handle_cpuid,
9378 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9379 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9380 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9381 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009382 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009383 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009384 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009385 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009386 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009387 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009388 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009389 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009390 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009391 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009392 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009393 [EXIT_REASON_VMOFF] = handle_vmoff,
9394 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009395 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9396 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009397 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009398 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009399 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009400 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009401 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009402 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009403 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9404 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009405 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9406 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009407 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009408 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009409 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009410 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009411 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009412 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009413 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009414 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009415 [EXIT_REASON_XSAVES] = handle_xsaves,
9416 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009417 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009418 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009419 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009420 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009421 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009422};
9423
9424static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009425 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009426
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009427static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9428 struct vmcs12 *vmcs12)
9429{
9430 unsigned long exit_qualification;
9431 gpa_t bitmap, last_bitmap;
9432 unsigned int port;
9433 int size;
9434 u8 b;
9435
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009436 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009437 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009438
9439 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9440
9441 port = exit_qualification >> 16;
9442 size = (exit_qualification & 7) + 1;
9443
9444 last_bitmap = (gpa_t)-1;
9445 b = -1;
9446
9447 while (size > 0) {
9448 if (port < 0x8000)
9449 bitmap = vmcs12->io_bitmap_a;
9450 else if (port < 0x10000)
9451 bitmap = vmcs12->io_bitmap_b;
9452 else
Joe Perches1d804d02015-03-30 16:46:09 -07009453 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009454 bitmap += (port & 0x7fff) / 8;
9455
9456 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009457 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009458 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009459 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009460 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009461
9462 port++;
9463 size--;
9464 last_bitmap = bitmap;
9465 }
9466
Joe Perches1d804d02015-03-30 16:46:09 -07009467 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009468}
9469
Nadav Har'El644d7112011-05-25 23:12:35 +03009470/*
9471 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9472 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9473 * disinterest in the current event (read or write a specific MSR) by using an
9474 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9475 */
9476static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9477 struct vmcs12 *vmcs12, u32 exit_reason)
9478{
9479 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9480 gpa_t bitmap;
9481
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009482 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009483 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009484
9485 /*
9486 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9487 * for the four combinations of read/write and low/high MSR numbers.
9488 * First we need to figure out which of the four to use:
9489 */
9490 bitmap = vmcs12->msr_bitmap;
9491 if (exit_reason == EXIT_REASON_MSR_WRITE)
9492 bitmap += 2048;
9493 if (msr_index >= 0xc0000000) {
9494 msr_index -= 0xc0000000;
9495 bitmap += 1024;
9496 }
9497
9498 /* Then read the msr_index'th bit from this bitmap: */
9499 if (msr_index < 1024*8) {
9500 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009501 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009502 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009503 return 1 & (b >> (msr_index & 7));
9504 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009505 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009506}
9507
9508/*
9509 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9510 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9511 * intercept (via guest_host_mask etc.) the current event.
9512 */
9513static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9514 struct vmcs12 *vmcs12)
9515{
9516 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9517 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009518 int reg;
9519 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009520
9521 switch ((exit_qualification >> 4) & 3) {
9522 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009523 reg = (exit_qualification >> 8) & 15;
9524 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009525 switch (cr) {
9526 case 0:
9527 if (vmcs12->cr0_guest_host_mask &
9528 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009529 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009530 break;
9531 case 3:
9532 if ((vmcs12->cr3_target_count >= 1 &&
9533 vmcs12->cr3_target_value0 == val) ||
9534 (vmcs12->cr3_target_count >= 2 &&
9535 vmcs12->cr3_target_value1 == val) ||
9536 (vmcs12->cr3_target_count >= 3 &&
9537 vmcs12->cr3_target_value2 == val) ||
9538 (vmcs12->cr3_target_count >= 4 &&
9539 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009541 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009542 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009543 break;
9544 case 4:
9545 if (vmcs12->cr4_guest_host_mask &
9546 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009547 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009548 break;
9549 case 8:
9550 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009551 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009552 break;
9553 }
9554 break;
9555 case 2: /* clts */
9556 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9557 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009558 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009559 break;
9560 case 1: /* mov from cr */
9561 switch (cr) {
9562 case 3:
9563 if (vmcs12->cpu_based_vm_exec_control &
9564 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009565 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009566 break;
9567 case 8:
9568 if (vmcs12->cpu_based_vm_exec_control &
9569 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009570 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009571 break;
9572 }
9573 break;
9574 case 3: /* lmsw */
9575 /*
9576 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9577 * cr0. Other attempted changes are ignored, with no exit.
9578 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009579 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009580 if (vmcs12->cr0_guest_host_mask & 0xe &
9581 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009582 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009583 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9584 !(vmcs12->cr0_read_shadow & 0x1) &&
9585 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009586 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009587 break;
9588 }
Joe Perches1d804d02015-03-30 16:46:09 -07009589 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009590}
9591
Liran Alona7cde482018-06-23 02:35:10 +03009592static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9593 struct vmcs12 *vmcs12, gpa_t bitmap)
9594{
9595 u32 vmx_instruction_info;
9596 unsigned long field;
9597 u8 b;
9598
9599 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9600 return true;
9601
9602 /* Decode instruction info and find the field to access */
9603 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9604 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9605
9606 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9607 if (field >> 15)
9608 return true;
9609
9610 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9611 return true;
9612
9613 return 1 & (b >> (field & 7));
9614}
9615
Nadav Har'El644d7112011-05-25 23:12:35 +03009616/*
9617 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9618 * should handle it ourselves in L0 (and then continue L2). Only call this
9619 * when in is_guest_mode (L2).
9620 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009621static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009622{
Nadav Har'El644d7112011-05-25 23:12:35 +03009623 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9624 struct vcpu_vmx *vmx = to_vmx(vcpu);
9625 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9626
Jim Mattson4f350c62017-09-14 16:31:44 -07009627 if (vmx->nested.nested_run_pending)
9628 return false;
9629
9630 if (unlikely(vmx->fail)) {
9631 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9632 vmcs_read32(VM_INSTRUCTION_ERROR));
9633 return true;
9634 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009635
David Matlackc9f04402017-08-01 14:00:40 -07009636 /*
9637 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009638 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9639 * Page). The CPU may write to these pages via their host
9640 * physical address while L2 is running, bypassing any
9641 * address-translation-based dirty tracking (e.g. EPT write
9642 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009643 *
9644 * Mark them dirty on every exit from L2 to prevent them from
9645 * getting out of sync with dirty tracking.
9646 */
9647 nested_mark_vmcs12_pages_dirty(vcpu);
9648
Jim Mattson4f350c62017-09-14 16:31:44 -07009649 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9650 vmcs_readl(EXIT_QUALIFICATION),
9651 vmx->idt_vectoring_info,
9652 intr_info,
9653 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9654 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009655
9656 switch (exit_reason) {
9657 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009658 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009659 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009661 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009662 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009663 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009664 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009665 else if (is_debug(intr_info) &&
9666 vcpu->guest_debug &
9667 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9668 return false;
9669 else if (is_breakpoint(intr_info) &&
9670 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9671 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009672 return vmcs12->exception_bitmap &
9673 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9674 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009675 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009676 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009677 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009678 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009679 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009680 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009681 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009682 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009683 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009684 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009685 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009686 case EXIT_REASON_HLT:
9687 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9688 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009689 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009690 case EXIT_REASON_INVLPG:
9691 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9692 case EXIT_REASON_RDPMC:
9693 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009694 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009695 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009696 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009697 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009698 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009699 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009700 case EXIT_REASON_VMREAD:
9701 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9702 vmcs12->vmread_bitmap);
9703 case EXIT_REASON_VMWRITE:
9704 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9705 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009706 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9707 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009708 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009709 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009710 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009711 /*
9712 * VMX instructions trap unconditionally. This allows L1 to
9713 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9714 */
Joe Perches1d804d02015-03-30 16:46:09 -07009715 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009716 case EXIT_REASON_CR_ACCESS:
9717 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9718 case EXIT_REASON_DR_ACCESS:
9719 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9720 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009721 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009722 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9723 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009724 case EXIT_REASON_MSR_READ:
9725 case EXIT_REASON_MSR_WRITE:
9726 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9727 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009728 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009729 case EXIT_REASON_MWAIT_INSTRUCTION:
9730 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009731 case EXIT_REASON_MONITOR_TRAP_FLAG:
9732 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009733 case EXIT_REASON_MONITOR_INSTRUCTION:
9734 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9735 case EXIT_REASON_PAUSE_INSTRUCTION:
9736 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9737 nested_cpu_has2(vmcs12,
9738 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9739 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009740 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009741 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009742 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009743 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009744 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009745 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009746 /*
9747 * The controls for "virtualize APIC accesses," "APIC-
9748 * register virtualization," and "virtual-interrupt
9749 * delivery" only come from vmcs12.
9750 */
Joe Perches1d804d02015-03-30 16:46:09 -07009751 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009752 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009753 /*
9754 * L0 always deals with the EPT violation. If nested EPT is
9755 * used, and the nested mmu code discovers that the address is
9756 * missing in the guest EPT table (EPT12), the EPT violation
9757 * will be injected with nested_ept_inject_page_fault()
9758 */
Joe Perches1d804d02015-03-30 16:46:09 -07009759 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009760 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009761 /*
9762 * L2 never uses directly L1's EPT, but rather L0's own EPT
9763 * table (shadow on EPT) or a merged EPT table that L0 built
9764 * (EPT on EPT). So any problems with the structure of the
9765 * table is L0's fault.
9766 */
Joe Perches1d804d02015-03-30 16:46:09 -07009767 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009768 case EXIT_REASON_INVPCID:
9769 return
9770 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9771 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009772 case EXIT_REASON_WBINVD:
9773 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9774 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009775 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009776 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9777 /*
9778 * This should never happen, since it is not possible to
9779 * set XSS to a non-zero value---neither in L1 nor in L2.
9780 * If if it were, XSS would have to be checked against
9781 * the XSS exit bitmap in vmcs12.
9782 */
9783 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009784 case EXIT_REASON_PREEMPTION_TIMER:
9785 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009786 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009787 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009788 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009789 case EXIT_REASON_VMFUNC:
9790 /* VM functions are emulated through L2->L0 vmexits. */
9791 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009792 case EXIT_REASON_ENCLS:
9793 /* SGX is never exposed to L1 */
9794 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009795 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009796 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009797 }
9798}
9799
Paolo Bonzini7313c692017-07-27 10:31:25 +02009800static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9801{
9802 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9803
9804 /*
9805 * At this point, the exit interruption info in exit_intr_info
9806 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9807 * we need to query the in-kernel LAPIC.
9808 */
9809 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9810 if ((exit_intr_info &
9811 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9812 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9813 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9814 vmcs12->vm_exit_intr_error_code =
9815 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9816 }
9817
9818 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9819 vmcs_readl(EXIT_QUALIFICATION));
9820 return 1;
9821}
9822
Avi Kivity586f9602010-11-18 13:09:54 +02009823static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9824{
9825 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9826 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9827}
9828
Kai Huanga3eaa862015-11-04 13:46:05 +08009829static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009830{
Kai Huanga3eaa862015-11-04 13:46:05 +08009831 if (vmx->pml_pg) {
9832 __free_page(vmx->pml_pg);
9833 vmx->pml_pg = NULL;
9834 }
Kai Huang843e4332015-01-28 10:54:28 +08009835}
9836
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009837static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009838{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009839 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009840 u64 *pml_buf;
9841 u16 pml_idx;
9842
9843 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9844
9845 /* Do nothing if PML buffer is empty */
9846 if (pml_idx == (PML_ENTITY_NUM - 1))
9847 return;
9848
9849 /* PML index always points to next available PML buffer entity */
9850 if (pml_idx >= PML_ENTITY_NUM)
9851 pml_idx = 0;
9852 else
9853 pml_idx++;
9854
9855 pml_buf = page_address(vmx->pml_pg);
9856 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9857 u64 gpa;
9858
9859 gpa = pml_buf[pml_idx];
9860 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009861 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009862 }
9863
9864 /* reset PML index */
9865 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9866}
9867
9868/*
9869 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9870 * Called before reporting dirty_bitmap to userspace.
9871 */
9872static void kvm_flush_pml_buffers(struct kvm *kvm)
9873{
9874 int i;
9875 struct kvm_vcpu *vcpu;
9876 /*
9877 * We only need to kick vcpu out of guest mode here, as PML buffer
9878 * is flushed at beginning of all VMEXITs, and it's obvious that only
9879 * vcpus running in guest are possible to have unflushed GPAs in PML
9880 * buffer.
9881 */
9882 kvm_for_each_vcpu(i, vcpu, kvm)
9883 kvm_vcpu_kick(vcpu);
9884}
9885
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009886static void vmx_dump_sel(char *name, uint32_t sel)
9887{
9888 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009889 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009890 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9891 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9892 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9893}
9894
9895static void vmx_dump_dtsel(char *name, uint32_t limit)
9896{
9897 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9898 name, vmcs_read32(limit),
9899 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9900}
9901
9902static void dump_vmcs(void)
9903{
9904 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9905 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9906 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9907 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9908 u32 secondary_exec_control = 0;
9909 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009910 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009911 int i, n;
9912
9913 if (cpu_has_secondary_exec_ctrls())
9914 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9915
9916 pr_err("*** Guest State ***\n");
9917 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9918 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9919 vmcs_readl(CR0_GUEST_HOST_MASK));
9920 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9921 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9922 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9923 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9924 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9925 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009926 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9927 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9928 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9929 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009930 }
9931 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9932 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9933 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9934 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9935 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9936 vmcs_readl(GUEST_SYSENTER_ESP),
9937 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9938 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9939 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9940 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9941 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9942 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9943 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9944 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9945 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9946 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9947 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9948 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9949 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009950 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9951 efer, vmcs_read64(GUEST_IA32_PAT));
9952 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9953 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009954 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009955 if (cpu_has_load_perf_global_ctrl &&
9956 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009957 pr_err("PerfGlobCtl = 0x%016llx\n",
9958 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009959 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009960 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009961 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9962 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9963 vmcs_read32(GUEST_ACTIVITY_STATE));
9964 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9965 pr_err("InterruptStatus = %04x\n",
9966 vmcs_read16(GUEST_INTR_STATUS));
9967
9968 pr_err("*** Host State ***\n");
9969 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9970 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9971 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9972 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9973 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9974 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9975 vmcs_read16(HOST_TR_SELECTOR));
9976 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9977 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9978 vmcs_readl(HOST_TR_BASE));
9979 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9980 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9981 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9982 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9983 vmcs_readl(HOST_CR4));
9984 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9985 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9986 vmcs_read32(HOST_IA32_SYSENTER_CS),
9987 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9988 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009989 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9990 vmcs_read64(HOST_IA32_EFER),
9991 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009992 if (cpu_has_load_perf_global_ctrl &&
9993 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009994 pr_err("PerfGlobCtl = 0x%016llx\n",
9995 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009996
9997 pr_err("*** Control State ***\n");
9998 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9999 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10000 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10001 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10002 vmcs_read32(EXCEPTION_BITMAP),
10003 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10004 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10005 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10006 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10007 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10008 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10009 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10010 vmcs_read32(VM_EXIT_INTR_INFO),
10011 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10012 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10013 pr_err(" reason=%08x qualification=%016lx\n",
10014 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10015 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10016 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10017 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010018 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010019 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010020 pr_err("TSC Multiplier = 0x%016llx\n",
10021 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010022 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10023 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10024 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10025 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10026 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010027 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010028 n = vmcs_read32(CR3_TARGET_COUNT);
10029 for (i = 0; i + 1 < n; i += 4)
10030 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10031 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10032 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10033 if (i < n)
10034 pr_err("CR3 target%u=%016lx\n",
10035 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10036 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10037 pr_err("PLE Gap=%08x Window=%08x\n",
10038 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10039 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10040 pr_err("Virtual processor ID = 0x%04x\n",
10041 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10042}
10043
Avi Kivity6aa8b732006-12-10 02:21:36 -080010044/*
10045 * The guest has exited. See if we can fix it or if we need userspace
10046 * assistance.
10047 */
Avi Kivity851ba692009-08-24 11:10:17 +030010048static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010049{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010050 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010051 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010052 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010053
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010054 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10055
Kai Huang843e4332015-01-28 10:54:28 +080010056 /*
10057 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10058 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10059 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10060 * mode as if vcpus is in root mode, the PML buffer must has been
10061 * flushed already.
10062 */
10063 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010064 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010065
Mohammed Gamal80ced182009-09-01 12:48:18 +020010066 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010067 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010068 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010069
Paolo Bonzini7313c692017-07-27 10:31:25 +020010070 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10071 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010072
Mohammed Gamal51207022010-05-31 22:40:54 +030010073 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010074 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010075 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10076 vcpu->run->fail_entry.hardware_entry_failure_reason
10077 = exit_reason;
10078 return 0;
10079 }
10080
Avi Kivity29bd8a72007-09-10 17:27:03 +030010081 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010082 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10083 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010084 = vmcs_read32(VM_INSTRUCTION_ERROR);
10085 return 0;
10086 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010087
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010088 /*
10089 * Note:
10090 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10091 * delivery event since it indicates guest is accessing MMIO.
10092 * The vm-exit can be triggered again after return to guest that
10093 * will cause infinite loop.
10094 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010095 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010096 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010097 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010098 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010099 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10100 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10101 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010102 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010103 vcpu->run->internal.data[0] = vectoring_info;
10104 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010105 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10106 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10107 vcpu->run->internal.ndata++;
10108 vcpu->run->internal.data[3] =
10109 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10110 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010111 return 0;
10112 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010113
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010114 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010115 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10116 if (vmx_interrupt_allowed(vcpu)) {
10117 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10118 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10119 vcpu->arch.nmi_pending) {
10120 /*
10121 * This CPU don't support us in finding the end of an
10122 * NMI-blocked window if the guest runs with IRQs
10123 * disabled. So we pull the trigger after 1 s of
10124 * futile waiting, but inform the user about this.
10125 */
10126 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10127 "state on VCPU %d after 1 s timeout\n",
10128 __func__, vcpu->vcpu_id);
10129 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10130 }
10131 }
10132
Avi Kivity6aa8b732006-12-10 02:21:36 -080010133 if (exit_reason < kvm_vmx_max_exit_handlers
10134 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010135 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010136 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010137 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10138 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010139 kvm_queue_exception(vcpu, UD_VECTOR);
10140 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010141 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010142}
10143
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010144/*
10145 * Software based L1D cache flush which is used when microcode providing
10146 * the cache control MSR is not loaded.
10147 *
10148 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10149 * flush it is required to read in 64 KiB because the replacement algorithm
10150 * is not exactly LRU. This could be sized at runtime via topology
10151 * information but as all relevant affected CPUs have 32KiB L1D cache size
10152 * there is no point in doing so.
10153 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010154static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010155{
10156 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010157
10158 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010159 * This code is only executed when the the flush mode is 'cond' or
10160 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010161 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010162 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010163 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010164
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010165 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010166 * Clear the per-vcpu flush bit, it gets set again
10167 * either from vcpu_run() or from one of the unsafe
10168 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010169 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010170 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010171 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010172
10173 /*
10174 * Clear the per-cpu flush bit, it gets set again from
10175 * the interrupt handlers.
10176 */
10177 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10178 kvm_clear_cpu_l1tf_flush_l1d();
10179
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010180 if (!flush_l1d)
10181 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010182 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010183
10184 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010185
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010186 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10187 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10188 return;
10189 }
10190
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010191 asm volatile(
10192 /* First ensure the pages are in the TLB */
10193 "xorl %%eax, %%eax\n"
10194 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010195 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010196 "addl $4096, %%eax\n\t"
10197 "cmpl %%eax, %[size]\n\t"
10198 "jne .Lpopulate_tlb\n\t"
10199 "xorl %%eax, %%eax\n\t"
10200 "cpuid\n\t"
10201 /* Now fill the cache */
10202 "xorl %%eax, %%eax\n"
10203 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010204 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010205 "addl $64, %%eax\n\t"
10206 "cmpl %%eax, %[size]\n\t"
10207 "jne .Lfill_cache\n\t"
10208 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010209 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010210 [size] "r" (size)
10211 : "eax", "ebx", "ecx", "edx");
10212}
10213
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010214static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010215{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010216 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10217
10218 if (is_guest_mode(vcpu) &&
10219 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10220 return;
10221
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010222 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010223 vmcs_write32(TPR_THRESHOLD, 0);
10224 return;
10225 }
10226
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010227 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010228}
10229
Jim Mattson8d860bb2018-05-09 16:56:05 -040010230static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010231{
10232 u32 sec_exec_control;
10233
Jim Mattson8d860bb2018-05-09 16:56:05 -040010234 if (!lapic_in_kernel(vcpu))
10235 return;
10236
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010237 if (!flexpriority_enabled &&
10238 !cpu_has_vmx_virtualize_x2apic_mode())
10239 return;
10240
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010241 /* Postpone execution until vmcs01 is the current VMCS. */
10242 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010243 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010244 return;
10245 }
10246
Yang Zhang8d146952013-01-25 10:18:50 +080010247 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010248 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10249 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010250
Jim Mattson8d860bb2018-05-09 16:56:05 -040010251 switch (kvm_get_apic_mode(vcpu)) {
10252 case LAPIC_MODE_INVALID:
10253 WARN_ONCE(true, "Invalid local APIC state");
10254 case LAPIC_MODE_DISABLED:
10255 break;
10256 case LAPIC_MODE_XAPIC:
10257 if (flexpriority_enabled) {
10258 sec_exec_control |=
10259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10260 vmx_flush_tlb(vcpu, true);
10261 }
10262 break;
10263 case LAPIC_MODE_X2APIC:
10264 if (cpu_has_vmx_virtualize_x2apic_mode())
10265 sec_exec_control |=
10266 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10267 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010268 }
10269 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10270
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010271 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010272}
10273
Tang Chen38b99172014-09-24 15:57:54 +080010274static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10275{
Jim Mattsonab5df312018-05-09 17:02:03 -040010276 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010277 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010278 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010279 }
Tang Chen38b99172014-09-24 15:57:54 +080010280}
10281
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010282static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010283{
10284 u16 status;
10285 u8 old;
10286
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010287 if (max_isr == -1)
10288 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010289
10290 status = vmcs_read16(GUEST_INTR_STATUS);
10291 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010292 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010293 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010294 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010295 vmcs_write16(GUEST_INTR_STATUS, status);
10296 }
10297}
10298
10299static void vmx_set_rvi(int vector)
10300{
10301 u16 status;
10302 u8 old;
10303
Wei Wang4114c272014-11-05 10:53:43 +080010304 if (vector == -1)
10305 vector = 0;
10306
Yang Zhangc7c9c562013-01-25 10:18:51 +080010307 status = vmcs_read16(GUEST_INTR_STATUS);
10308 old = (u8)status & 0xff;
10309 if ((u8)vector != old) {
10310 status &= ~0xff;
10311 status |= (u8)vector;
10312 vmcs_write16(GUEST_INTR_STATUS, status);
10313 }
10314}
10315
10316static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10317{
Liran Alon851c1a182017-12-24 18:12:56 +020010318 /*
10319 * When running L2, updating RVI is only relevant when
10320 * vmcs12 virtual-interrupt-delivery enabled.
10321 * However, it can be enabled only when L1 also
10322 * intercepts external-interrupts and in that case
10323 * we should not update vmcs02 RVI but instead intercept
10324 * interrupt. Therefore, do nothing when running L2.
10325 */
10326 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010327 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010328}
10329
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010330static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010331{
10332 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010333 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010334 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010335
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010336 WARN_ON(!vcpu->arch.apicv_active);
10337 if (pi_test_on(&vmx->pi_desc)) {
10338 pi_clear_on(&vmx->pi_desc);
10339 /*
10340 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10341 * But on x86 this is just a compiler barrier anyway.
10342 */
10343 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010344 max_irr_updated =
10345 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10346
10347 /*
10348 * If we are running L2 and L1 has a new pending interrupt
10349 * which can be injected, we should re-evaluate
10350 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010351 * If L1 intercepts external-interrupts, we should
10352 * exit from L2 to L1. Otherwise, interrupt should be
10353 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010354 */
Liran Alon851c1a182017-12-24 18:12:56 +020010355 if (is_guest_mode(vcpu) && max_irr_updated) {
10356 if (nested_exit_on_intr(vcpu))
10357 kvm_vcpu_exiting_guest_mode(vcpu);
10358 else
10359 kvm_make_request(KVM_REQ_EVENT, vcpu);
10360 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010361 } else {
10362 max_irr = kvm_lapic_find_highest_irr(vcpu);
10363 }
10364 vmx_hwapic_irr_update(vcpu, max_irr);
10365 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010366}
10367
Paolo Bonzini7e712682018-10-03 13:44:26 +020010368static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10369{
10370 u8 rvi = vmx_get_rvi();
10371 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10372
10373 return ((rvi & 0xf0) > (vppr & 0xf0));
10374}
10375
Andrey Smetanin63086302015-11-10 15:36:32 +030010376static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010377{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010378 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010379 return;
10380
Yang Zhangc7c9c562013-01-25 10:18:51 +080010381 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10382 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10383 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10384 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10385}
10386
Paolo Bonzini967235d2016-12-19 14:03:45 +010010387static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10388{
10389 struct vcpu_vmx *vmx = to_vmx(vcpu);
10390
10391 pi_clear_on(&vmx->pi_desc);
10392 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10393}
10394
Avi Kivity51aa01d2010-07-20 14:31:20 +030010395static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010396{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010397 u32 exit_intr_info = 0;
10398 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010399
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010400 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10401 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010402 return;
10403
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010404 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10405 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10406 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010407
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010408 /* if exit due to PF check for async PF */
10409 if (is_page_fault(exit_intr_info))
10410 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10411
Andi Kleena0861c02009-06-08 17:37:09 +080010412 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010413 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10414 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010415 kvm_machine_check();
10416
Gleb Natapov20f65982009-05-11 13:35:55 +030010417 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010418 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010419 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010420 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010421 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010422 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010423}
Gleb Natapov20f65982009-05-11 13:35:55 +030010424
Yang Zhanga547c6d2013-04-11 19:25:10 +080010425static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10426{
10427 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10428
Yang Zhanga547c6d2013-04-11 19:25:10 +080010429 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10430 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10431 unsigned int vector;
10432 unsigned long entry;
10433 gate_desc *desc;
10434 struct vcpu_vmx *vmx = to_vmx(vcpu);
10435#ifdef CONFIG_X86_64
10436 unsigned long tmp;
10437#endif
10438
10439 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10440 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010441 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010442 asm volatile(
10443#ifdef CONFIG_X86_64
10444 "mov %%" _ASM_SP ", %[sp]\n\t"
10445 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10446 "push $%c[ss]\n\t"
10447 "push %[sp]\n\t"
10448#endif
10449 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010450 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010451 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010452 :
10453#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010454 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010455#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010456 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010457 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010458 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010459 [ss]"i"(__KERNEL_DS),
10460 [cs]"i"(__KERNEL_CS)
10461 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010462 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010463}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010464STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010465
Tom Lendackybc226f02018-05-10 22:06:39 +020010466static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010467{
Tom Lendackybc226f02018-05-10 22:06:39 +020010468 switch (index) {
10469 case MSR_IA32_SMBASE:
10470 /*
10471 * We cannot do SMM unless we can run the guest in big
10472 * real mode.
10473 */
10474 return enable_unrestricted_guest || emulate_invalid_guest_state;
10475 case MSR_AMD64_VIRT_SPEC_CTRL:
10476 /* This is AMD only. */
10477 return false;
10478 default:
10479 return true;
10480 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010481}
10482
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010483static bool vmx_mpx_supported(void)
10484{
10485 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10486 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10487}
10488
Wanpeng Li55412b22014-12-02 19:21:30 +080010489static bool vmx_xsaves_supported(void)
10490{
10491 return vmcs_config.cpu_based_2nd_exec_ctrl &
10492 SECONDARY_EXEC_XSAVES;
10493}
10494
Avi Kivity51aa01d2010-07-20 14:31:20 +030010495static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10496{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010497 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010498 bool unblock_nmi;
10499 u8 vector;
10500 bool idtv_info_valid;
10501
10502 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010503
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010504 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010505 if (vmx->loaded_vmcs->nmi_known_unmasked)
10506 return;
10507 /*
10508 * Can't use vmx->exit_intr_info since we're not sure what
10509 * the exit reason is.
10510 */
10511 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10512 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10513 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10514 /*
10515 * SDM 3: 27.7.1.2 (September 2008)
10516 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10517 * a guest IRET fault.
10518 * SDM 3: 23.2.2 (September 2008)
10519 * Bit 12 is undefined in any of the following cases:
10520 * If the VM exit sets the valid bit in the IDT-vectoring
10521 * information field.
10522 * If the VM exit is due to a double fault.
10523 */
10524 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10525 vector != DF_VECTOR && !idtv_info_valid)
10526 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10527 GUEST_INTR_STATE_NMI);
10528 else
10529 vmx->loaded_vmcs->nmi_known_unmasked =
10530 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10531 & GUEST_INTR_STATE_NMI);
10532 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10533 vmx->loaded_vmcs->vnmi_blocked_time +=
10534 ktime_to_ns(ktime_sub(ktime_get(),
10535 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010536}
10537
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010538static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010539 u32 idt_vectoring_info,
10540 int instr_len_field,
10541 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010542{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010543 u8 vector;
10544 int type;
10545 bool idtv_info_valid;
10546
10547 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010548
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010549 vcpu->arch.nmi_injected = false;
10550 kvm_clear_exception_queue(vcpu);
10551 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010552
10553 if (!idtv_info_valid)
10554 return;
10555
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010556 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010557
Avi Kivity668f6122008-07-02 09:28:55 +030010558 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10559 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010560
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010561 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010562 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010563 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010564 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010565 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010566 * Clear bit "block by NMI" before VM entry if a NMI
10567 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010568 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010569 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010570 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010571 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010572 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010573 /* fall through */
10574 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010575 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010576 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010577 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010578 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010579 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010580 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010581 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010582 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010583 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010584 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010585 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010586 break;
10587 default:
10588 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010589 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010590}
10591
Avi Kivity83422e12010-07-20 14:43:23 +030010592static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10593{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010594 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010595 VM_EXIT_INSTRUCTION_LEN,
10596 IDT_VECTORING_ERROR_CODE);
10597}
10598
Avi Kivityb463a6f2010-07-20 15:06:17 +030010599static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10600{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010601 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010602 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10603 VM_ENTRY_INSTRUCTION_LEN,
10604 VM_ENTRY_EXCEPTION_ERROR_CODE);
10605
10606 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10607}
10608
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010609static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10610{
10611 int i, nr_msrs;
10612 struct perf_guest_switch_msr *msrs;
10613
10614 msrs = perf_guest_get_msrs(&nr_msrs);
10615
10616 if (!msrs)
10617 return;
10618
10619 for (i = 0; i < nr_msrs; i++)
10620 if (msrs[i].host == msrs[i].guest)
10621 clear_atomic_switch_msr(vmx, msrs[i].msr);
10622 else
10623 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010624 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010625}
10626
Sean Christophersonf459a702018-08-27 15:21:11 -070010627static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10628{
10629 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10630 if (!vmx->loaded_vmcs->hv_timer_armed)
10631 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10632 PIN_BASED_VMX_PREEMPTION_TIMER);
10633 vmx->loaded_vmcs->hv_timer_armed = true;
10634}
10635
10636static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010637{
10638 struct vcpu_vmx *vmx = to_vmx(vcpu);
10639 u64 tscl;
10640 u32 delta_tsc;
10641
Sean Christophersond264ee02018-08-27 15:21:12 -070010642 if (vmx->req_immediate_exit) {
10643 vmx_arm_hv_timer(vmx, 0);
10644 return;
10645 }
10646
Sean Christophersonf459a702018-08-27 15:21:11 -070010647 if (vmx->hv_deadline_tsc != -1) {
10648 tscl = rdtsc();
10649 if (vmx->hv_deadline_tsc > tscl)
10650 /* set_hv_timer ensures the delta fits in 32-bits */
10651 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10652 cpu_preemption_timer_multi);
10653 else
10654 delta_tsc = 0;
10655
10656 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010657 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010658 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010659
Sean Christophersonf459a702018-08-27 15:21:11 -070010660 if (vmx->loaded_vmcs->hv_timer_armed)
10661 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10662 PIN_BASED_VMX_PREEMPTION_TIMER);
10663 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010664}
10665
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010666static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010667{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010668 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010669 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010670
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010671 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010672 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010673 vmx->loaded_vmcs->soft_vnmi_blocked))
10674 vmx->loaded_vmcs->entry_time = ktime_get();
10675
Avi Kivity104f2262010-11-18 13:12:52 +020010676 /* Don't enter VMX if guest state is invalid, let the exit handler
10677 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010678 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010679 return;
10680
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010681 if (vmx->ple_window_dirty) {
10682 vmx->ple_window_dirty = false;
10683 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10684 }
10685
Abel Gordon012f83c2013-04-18 14:39:25 +030010686 if (vmx->nested.sync_shadow_vmcs) {
10687 copy_vmcs12_to_shadow(vmx);
10688 vmx->nested.sync_shadow_vmcs = false;
10689 }
10690
Avi Kivity104f2262010-11-18 13:12:52 +020010691 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10692 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10693 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10694 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10695
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010696 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010697 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010698 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010699 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010700 }
10701
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010702 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010703 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010704 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010705 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010706 }
10707
Avi Kivity104f2262010-11-18 13:12:52 +020010708 /* When single-stepping over STI and MOV SS, we must clear the
10709 * corresponding interruptibility bits in the guest state. Otherwise
10710 * vmentry fails as it then expects bit 14 (BS) in pending debug
10711 * exceptions being set, but that's not correct for the guest debugging
10712 * case. */
10713 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10714 vmx_set_interrupt_shadow(vcpu, 0);
10715
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010716 if (static_cpu_has(X86_FEATURE_PKU) &&
10717 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10718 vcpu->arch.pkru != vmx->host_pkru)
10719 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010720
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010721 atomic_switch_perf_msrs(vmx);
10722
Sean Christophersonf459a702018-08-27 15:21:11 -070010723 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010724
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010725 /*
10726 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10727 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10728 * is no need to worry about the conditional branch over the wrmsr
10729 * being speculatively taken.
10730 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010731 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010732
Nadav Har'Eld462b812011-05-24 15:26:10 +030010733 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010734
10735 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10736 (unsigned long)&current_evmcs->host_rsp : 0;
10737
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010738 if (static_branch_unlikely(&vmx_l1d_should_flush))
10739 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010740
Avi Kivity104f2262010-11-18 13:12:52 +020010741 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010742 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010743 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10744 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10745 "push %%" _ASM_CX " \n\t"
10746 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010747 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010748 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010749 /* Avoid VMWRITE when Enlightened VMCS is in use */
10750 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10751 "jz 2f \n\t"
10752 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10753 "jmp 1f \n\t"
10754 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010755 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010756 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010757 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010758 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10759 "mov %%cr2, %%" _ASM_DX " \n\t"
10760 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010761 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010762 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010763 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010764 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010765 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010766 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010767 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10768 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10769 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10770 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10771 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10772 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010773#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010774 "mov %c[r8](%0), %%r8 \n\t"
10775 "mov %c[r9](%0), %%r9 \n\t"
10776 "mov %c[r10](%0), %%r10 \n\t"
10777 "mov %c[r11](%0), %%r11 \n\t"
10778 "mov %c[r12](%0), %%r12 \n\t"
10779 "mov %c[r13](%0), %%r13 \n\t"
10780 "mov %c[r14](%0), %%r14 \n\t"
10781 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010782#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010783 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010784
Avi Kivity6aa8b732006-12-10 02:21:36 -080010785 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010786 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010787 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010788 "jmp 2f \n\t"
10789 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10790 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010791 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010792 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010793 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010794 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010795 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10796 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10797 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10798 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10799 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10800 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10801 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010802#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010803 "mov %%r8, %c[r8](%0) \n\t"
10804 "mov %%r9, %c[r9](%0) \n\t"
10805 "mov %%r10, %c[r10](%0) \n\t"
10806 "mov %%r11, %c[r11](%0) \n\t"
10807 "mov %%r12, %c[r12](%0) \n\t"
10808 "mov %%r13, %c[r13](%0) \n\t"
10809 "mov %%r14, %c[r14](%0) \n\t"
10810 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010811 "xor %%r8d, %%r8d \n\t"
10812 "xor %%r9d, %%r9d \n\t"
10813 "xor %%r10d, %%r10d \n\t"
10814 "xor %%r11d, %%r11d \n\t"
10815 "xor %%r12d, %%r12d \n\t"
10816 "xor %%r13d, %%r13d \n\t"
10817 "xor %%r14d, %%r14d \n\t"
10818 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010819#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010820 "mov %%cr2, %%" _ASM_AX " \n\t"
10821 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010822
Jim Mattson0cb5b302018-01-03 14:31:38 -080010823 "xor %%eax, %%eax \n\t"
10824 "xor %%ebx, %%ebx \n\t"
10825 "xor %%esi, %%esi \n\t"
10826 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010827 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010828 ".pushsection .rodata \n\t"
10829 ".global vmx_return \n\t"
10830 "vmx_return: " _ASM_PTR " 2b \n\t"
10831 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010832 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010833 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010834 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010835 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010836 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10837 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10838 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10839 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10840 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10841 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10842 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010843#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010844 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10845 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10846 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10847 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10848 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10849 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10850 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10851 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010852#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010853 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10854 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010855 : "cc", "memory"
10856#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010857 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010858 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010859#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010860 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010861#endif
10862 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010863
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010864 /*
10865 * We do not use IBRS in the kernel. If this vCPU has used the
10866 * SPEC_CTRL MSR it may have left it on; save the value and
10867 * turn it off. This is much more efficient than blindly adding
10868 * it to the atomic save/restore list. Especially as the former
10869 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10870 *
10871 * For non-nested case:
10872 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10873 * save it.
10874 *
10875 * For nested case:
10876 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10877 * save it.
10878 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010879 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010880 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010881
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010882 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010883
David Woodhouse117cc7a2018-01-12 11:11:27 +000010884 /* Eliminate branch target predictions from guest mode */
10885 vmexit_fill_RSB();
10886
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010887 /* All fields are clean at this point */
10888 if (static_branch_unlikely(&enable_evmcs))
10889 current_evmcs->hv_clean_fields |=
10890 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10891
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010892 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010893 if (vmx->host_debugctlmsr)
10894 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010895
Avi Kivityaa67f602012-08-01 16:48:03 +030010896#ifndef CONFIG_X86_64
10897 /*
10898 * The sysexit path does not restore ds/es, so we must set them to
10899 * a reasonable value ourselves.
10900 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010901 * We can't defer this to vmx_prepare_switch_to_host() since that
10902 * function may be executed in interrupt context, which saves and
10903 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010904 */
10905 loadsegment(ds, __USER_DS);
10906 loadsegment(es, __USER_DS);
10907#endif
10908
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010909 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010910 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010911 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010912 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010913 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010914 vcpu->arch.regs_dirty = 0;
10915
Gleb Natapove0b890d2013-09-25 12:51:33 +030010916 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010917 * eager fpu is enabled if PKEY is supported and CR4 is switched
10918 * back on host, so it is safe to read guest PKRU from current
10919 * XSAVE.
10920 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010921 if (static_cpu_has(X86_FEATURE_PKU) &&
10922 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10923 vcpu->arch.pkru = __read_pkru();
10924 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010925 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010926 }
10927
Gleb Natapove0b890d2013-09-25 12:51:33 +030010928 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010929 vmx->idt_vectoring_info = 0;
10930
10931 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10932 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10933 return;
10934
10935 vmx->loaded_vmcs->launched = 1;
10936 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010937
Avi Kivity51aa01d2010-07-20 14:31:20 +030010938 vmx_complete_atomic_exit(vmx);
10939 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010940 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010941}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010942STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010943
Sean Christopherson434a1e92018-03-20 12:17:18 -070010944static struct kvm *vmx_vm_alloc(void)
10945{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010946 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010947 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010948}
10949
10950static void vmx_vm_free(struct kvm *kvm)
10951{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010952 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010953}
10954
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010955static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010956{
10957 struct vcpu_vmx *vmx = to_vmx(vcpu);
10958 int cpu;
10959
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010960 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010961 return;
10962
10963 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010964 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010965 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010966 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010967 put_cpu();
10968}
10969
Jim Mattson2f1fe812016-07-08 15:36:06 -070010970/*
10971 * Ensure that the current vmcs of the logical processor is the
10972 * vmcs01 of the vcpu before calling free_nested().
10973 */
10974static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10975{
10976 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010977
Christoffer Dallec7660c2017-12-04 21:35:23 +010010978 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010979 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010980 free_nested(vmx);
10981 vcpu_put(vcpu);
10982}
10983
Avi Kivity6aa8b732006-12-10 02:21:36 -080010984static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10985{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010986 struct vcpu_vmx *vmx = to_vmx(vcpu);
10987
Kai Huang843e4332015-01-28 10:54:28 +080010988 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010989 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010990 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010991 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010992 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010993 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010994 kfree(vmx->guest_msrs);
10995 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010996 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010997}
10998
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010999static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011000{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011001 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100011002 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011003 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011004 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011005
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011006 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011007 return ERR_PTR(-ENOMEM);
11008
Wanpeng Li991e7a02015-09-16 17:30:05 +080011009 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011010
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011011 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11012 if (err)
11013 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011014
Peter Feiner4e595162016-07-07 14:49:58 -070011015 err = -ENOMEM;
11016
11017 /*
11018 * If PML is turned on, failure on enabling PML just results in failure
11019 * of creating the vcpu, therefore we can simplify PML logic (by
11020 * avoiding dealing with cases, such as enabling PML partially on vcpus
11021 * for the guest, etc.
11022 */
11023 if (enable_pml) {
11024 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11025 if (!vmx->pml_pg)
11026 goto uninit_vcpu;
11027 }
11028
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011029 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011030 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11031 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011032
Peter Feiner4e595162016-07-07 14:49:58 -070011033 if (!vmx->guest_msrs)
11034 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011035
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011036 err = alloc_loaded_vmcs(&vmx->vmcs01);
11037 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011038 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011039
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011040 msr_bitmap = vmx->vmcs01.msr_bitmap;
11041 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11042 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11043 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11044 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11045 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11046 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11047 vmx->msr_bitmap_mode = 0;
11048
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011049 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011050 cpu = get_cpu();
11051 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011052 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011053 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011054 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011055 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011056 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011057 err = alloc_apic_access_page(kvm);
11058 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011059 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011060 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011061
Sean Christophersone90008d2018-03-05 12:04:37 -080011062 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011063 err = init_rmode_identity_map(kvm);
11064 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011065 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011066 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011067
Roman Kagan63aff652018-07-19 21:59:07 +030011068 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011069 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11070 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011071
Wincy Van705699a2015-02-03 23:58:17 +080011072 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011073 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011074
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011075 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11076
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011077 /*
11078 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11079 * or POSTED_INTR_WAKEUP_VECTOR.
11080 */
11081 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11082 vmx->pi_desc.sn = 1;
11083
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011084 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011085
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011086free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011087 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011088free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011089 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011090free_pml:
11091 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011092uninit_vcpu:
11093 kvm_vcpu_uninit(&vmx->vcpu);
11094free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011095 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011096 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011097 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011098}
11099
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011100#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"
11101#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 -040011102
Wanpeng Lib31c1142018-03-12 04:53:04 -070011103static int vmx_vm_init(struct kvm *kvm)
11104{
Tianyu Lan877ad952018-07-19 08:40:23 +000011105 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11106
Wanpeng Lib31c1142018-03-12 04:53:04 -070011107 if (!ple_gap)
11108 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011109
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011110 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11111 switch (l1tf_mitigation) {
11112 case L1TF_MITIGATION_OFF:
11113 case L1TF_MITIGATION_FLUSH_NOWARN:
11114 /* 'I explicitly don't care' is set */
11115 break;
11116 case L1TF_MITIGATION_FLUSH:
11117 case L1TF_MITIGATION_FLUSH_NOSMT:
11118 case L1TF_MITIGATION_FULL:
11119 /*
11120 * Warn upon starting the first VM in a potentially
11121 * insecure environment.
11122 */
11123 if (cpu_smt_control == CPU_SMT_ENABLED)
11124 pr_warn_once(L1TF_MSG_SMT);
11125 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11126 pr_warn_once(L1TF_MSG_L1D);
11127 break;
11128 case L1TF_MITIGATION_FULL_FORCE:
11129 /* Flush is enforced */
11130 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011131 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011132 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011133 return 0;
11134}
11135
Yang, Sheng002c7f72007-07-31 14:23:01 +030011136static void __init vmx_check_processor_compat(void *rtn)
11137{
11138 struct vmcs_config vmcs_conf;
11139
11140 *(int *)rtn = 0;
11141 if (setup_vmcs_config(&vmcs_conf) < 0)
11142 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011143 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011144 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11145 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11146 smp_processor_id());
11147 *(int *)rtn = -EIO;
11148 }
11149}
11150
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011151static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011152{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011153 u8 cache;
11154 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011155
Sheng Yang522c68c2009-04-27 20:35:43 +080011156 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011157 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011158 * 2. EPT with VT-d:
11159 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011160 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011161 * b. VT-d with snooping control feature: snooping control feature of
11162 * VT-d engine can guarantee the cache correctness. Just set it
11163 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011164 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011165 * consistent with host MTRR
11166 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011167 if (is_mmio) {
11168 cache = MTRR_TYPE_UNCACHABLE;
11169 goto exit;
11170 }
11171
11172 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011173 ipat = VMX_EPT_IPAT_BIT;
11174 cache = MTRR_TYPE_WRBACK;
11175 goto exit;
11176 }
11177
11178 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11179 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011180 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011181 cache = MTRR_TYPE_WRBACK;
11182 else
11183 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011184 goto exit;
11185 }
11186
Xiao Guangrongff536042015-06-15 16:55:22 +080011187 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011188
11189exit:
11190 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011191}
11192
Sheng Yang17cc3932010-01-05 19:02:27 +080011193static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011194{
Sheng Yang878403b2010-01-05 19:02:29 +080011195 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11196 return PT_DIRECTORY_LEVEL;
11197 else
11198 /* For shadow and EPT supported 1GB page */
11199 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011200}
11201
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011202static void vmcs_set_secondary_exec_control(u32 new_ctl)
11203{
11204 /*
11205 * These bits in the secondary execution controls field
11206 * are dynamic, the others are mostly based on the hypervisor
11207 * architecture and the guest's CPUID. Do not touch the
11208 * dynamic bits.
11209 */
11210 u32 mask =
11211 SECONDARY_EXEC_SHADOW_VMCS |
11212 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011213 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11214 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011215
11216 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11217
11218 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11219 (new_ctl & ~mask) | (cur_ctl & mask));
11220}
11221
David Matlack8322ebb2016-11-29 18:14:09 -080011222/*
11223 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11224 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11225 */
11226static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11227{
11228 struct vcpu_vmx *vmx = to_vmx(vcpu);
11229 struct kvm_cpuid_entry2 *entry;
11230
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011231 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11232 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011233
11234#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11235 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011236 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011237} while (0)
11238
11239 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11240 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11241 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11242 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11243 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11244 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11245 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11246 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11247 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11248 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11249 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11250 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11251 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11252 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11253 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11254
11255 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11256 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11257 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11258 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11259 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011260 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011261
11262#undef cr4_fixed1_update
11263}
11264
Liran Alon5f76f6f2018-09-14 03:25:52 +030011265static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11266{
11267 struct vcpu_vmx *vmx = to_vmx(vcpu);
11268
11269 if (kvm_mpx_supported()) {
11270 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11271
11272 if (mpx_enabled) {
11273 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11274 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11275 } else {
11276 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11277 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11278 }
11279 }
11280}
11281
Sheng Yang0e851882009-12-18 16:48:46 +080011282static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11283{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011284 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011285
Paolo Bonzini80154d72017-08-24 13:55:35 +020011286 if (cpu_has_secondary_exec_ctrls()) {
11287 vmx_compute_secondary_exec_control(vmx);
11288 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011289 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011290
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011291 if (nested_vmx_allowed(vcpu))
11292 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11293 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11294 else
11295 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11296 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011297
Liran Alon5f76f6f2018-09-14 03:25:52 +030011298 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011299 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011300 nested_vmx_entry_exit_ctls_update(vcpu);
11301 }
Sheng Yang0e851882009-12-18 16:48:46 +080011302}
11303
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011304static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11305{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011306 if (func == 1 && nested)
11307 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011308}
11309
Yang Zhang25d92082013-08-06 12:00:32 +030011310static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11311 struct x86_exception *fault)
11312{
Jan Kiszka533558b2014-01-04 18:47:20 +010011313 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011314 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011315 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011316 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011317
Bandan Dasc5f983f2017-05-05 15:25:14 -040011318 if (vmx->nested.pml_full) {
11319 exit_reason = EXIT_REASON_PML_FULL;
11320 vmx->nested.pml_full = false;
11321 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11322 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011323 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011324 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011325 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011326
11327 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011328 vmcs12->guest_physical_address = fault->address;
11329}
11330
Peter Feiner995f00a2017-06-30 17:26:32 -070011331static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11332{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011333 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011334}
11335
Nadav Har'El155a97a2013-08-05 11:07:16 +030011336/* Callbacks for nested_ept_init_mmu_context: */
11337
11338static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11339{
11340 /* return the page table to be shadowed - in our case, EPT12 */
11341 return get_vmcs12(vcpu)->ept_pointer;
11342}
11343
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011344static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011345{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011346 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011347 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011348 return 1;
11349
Paolo Bonziniad896af2013-10-02 16:56:14 +020011350 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011351 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011352 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011353 nested_ept_ad_enabled(vcpu),
11354 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011355 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11356 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11357 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11358
11359 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011360 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011361}
11362
11363static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11364{
11365 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11366}
11367
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011368static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11369 u16 error_code)
11370{
11371 bool inequality, bit;
11372
11373 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11374 inequality =
11375 (error_code & vmcs12->page_fault_error_code_mask) !=
11376 vmcs12->page_fault_error_code_match;
11377 return inequality ^ bit;
11378}
11379
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011380static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11381 struct x86_exception *fault)
11382{
11383 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11384
11385 WARN_ON(!is_guest_mode(vcpu));
11386
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011387 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11388 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011389 vmcs12->vm_exit_intr_error_code = fault->error_code;
11390 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11391 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11392 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11393 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011394 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011395 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011396 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011397}
11398
Paolo Bonzinic9923842017-12-13 14:16:30 +010011399static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11400 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011401
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011402static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011403{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011404 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011405 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011406 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011407 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011408
11409 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011410 /*
11411 * Translate L1 physical address to host physical
11412 * address for vmcs02. Keep the page pinned, so this
11413 * physical address remains valid. We keep a reference
11414 * to it so we can release it later.
11415 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011416 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011417 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011418 vmx->nested.apic_access_page = NULL;
11419 }
11420 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011421 /*
11422 * If translation failed, no matter: This feature asks
11423 * to exit when accessing the given address, and if it
11424 * can never be accessed, this feature won't do
11425 * anything anyway.
11426 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011427 if (!is_error_page(page)) {
11428 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011429 hpa = page_to_phys(vmx->nested.apic_access_page);
11430 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11431 } else {
11432 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11433 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11434 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011435 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011436
11437 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011438 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011439 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011440 vmx->nested.virtual_apic_page = NULL;
11441 }
11442 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011443
11444 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011445 * If translation failed, VM entry will fail because
11446 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11447 * Failing the vm entry is _not_ what the processor
11448 * does but it's basically the only possibility we
11449 * have. We could still enter the guest if CR8 load
11450 * exits are enabled, CR8 store exits are enabled, and
11451 * virtualize APIC access is disabled; in this case
11452 * the processor would never use the TPR shadow and we
11453 * could simply clear the bit from the execution
11454 * control. But such a configuration is useless, so
11455 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011456 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011457 if (!is_error_page(page)) {
11458 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011459 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11460 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11461 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011462 }
11463
Wincy Van705699a2015-02-03 23:58:17 +080011464 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011465 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11466 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011467 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011468 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011469 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011470 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11471 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011472 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011473 vmx->nested.pi_desc_page = page;
11474 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011475 vmx->nested.pi_desc =
11476 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11477 (unsigned long)(vmcs12->posted_intr_desc_addr &
11478 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011479 vmcs_write64(POSTED_INTR_DESC_ADDR,
11480 page_to_phys(vmx->nested.pi_desc_page) +
11481 (unsigned long)(vmcs12->posted_intr_desc_addr &
11482 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011483 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011484 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011485 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11486 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011487 else
11488 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11489 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011490}
11491
Jan Kiszkaf4124502014-03-07 20:03:13 +010011492static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11493{
11494 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11495 struct vcpu_vmx *vmx = to_vmx(vcpu);
11496
Sean Christopherson4c008122018-08-27 15:21:10 -070011497 /*
11498 * A timer value of zero is architecturally guaranteed to cause
11499 * a VMExit prior to executing any instructions in the guest.
11500 */
11501 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011502 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11503 return;
11504 }
11505
Sean Christopherson4c008122018-08-27 15:21:10 -070011506 if (vcpu->arch.virtual_tsc_khz == 0)
11507 return;
11508
Jan Kiszkaf4124502014-03-07 20:03:13 +010011509 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11510 preemption_timeout *= 1000000;
11511 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11512 hrtimer_start(&vmx->nested.preemption_timer,
11513 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11514}
11515
Jim Mattson56a20512017-07-06 16:33:06 -070011516static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11517 struct vmcs12 *vmcs12)
11518{
11519 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11520 return 0;
11521
11522 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11523 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11524 return -EINVAL;
11525
11526 return 0;
11527}
11528
Wincy Van3af18d92015-02-03 23:49:31 +080011529static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11530 struct vmcs12 *vmcs12)
11531{
Wincy Van3af18d92015-02-03 23:49:31 +080011532 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11533 return 0;
11534
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011535 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011536 return -EINVAL;
11537
11538 return 0;
11539}
11540
Jim Mattson712b12d2017-08-24 13:24:47 -070011541static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11542 struct vmcs12 *vmcs12)
11543{
11544 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11545 return 0;
11546
11547 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11548 return -EINVAL;
11549
11550 return 0;
11551}
11552
Wincy Van3af18d92015-02-03 23:49:31 +080011553/*
11554 * Merge L0's and L1's MSR bitmap, return false to indicate that
11555 * we do not use the hardware.
11556 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011557static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11558 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011559{
Wincy Van82f0dd42015-02-03 23:57:18 +080011560 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011561 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011562 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011563 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011564 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011565 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011566 *
11567 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11568 * ensures that we do not accidentally generate an L02 MSR bitmap
11569 * from the L12 MSR bitmap that is too permissive.
11570 * 2. That L1 or L2s have actually used the MSR. This avoids
11571 * unnecessarily merging of the bitmap if the MSR is unused. This
11572 * works properly because we only update the L01 MSR bitmap lazily.
11573 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11574 * updated to reflect this when L1 (or its L2s) actually write to
11575 * the MSR.
11576 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011577 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11578 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011579
Paolo Bonzinic9923842017-12-13 14:16:30 +010011580 /* Nothing to do if the MSR bitmap is not in use. */
11581 if (!cpu_has_vmx_msr_bitmap() ||
11582 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11583 return false;
11584
Ashok Raj15d45072018-02-01 22:59:43 +010011585 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011586 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011587 return false;
11588
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011589 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11590 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011591 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011592
Radim Krčmářd048c092016-08-08 20:16:22 +020011593 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011594 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11595 /*
11596 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11597 * just lets the processor take the value from the virtual-APIC page;
11598 * take those 256 bits directly from the L1 bitmap.
11599 */
11600 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11601 unsigned word = msr / BITS_PER_LONG;
11602 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11603 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011604 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011605 } else {
11606 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11607 unsigned word = msr / BITS_PER_LONG;
11608 msr_bitmap_l0[word] = ~0;
11609 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11610 }
11611 }
11612
11613 nested_vmx_disable_intercept_for_msr(
11614 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011615 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011616 MSR_TYPE_W);
11617
11618 if (nested_cpu_has_vid(vmcs12)) {
11619 nested_vmx_disable_intercept_for_msr(
11620 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011621 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011622 MSR_TYPE_W);
11623 nested_vmx_disable_intercept_for_msr(
11624 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011625 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011626 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011627 }
Ashok Raj15d45072018-02-01 22:59:43 +010011628
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011629 if (spec_ctrl)
11630 nested_vmx_disable_intercept_for_msr(
11631 msr_bitmap_l1, msr_bitmap_l0,
11632 MSR_IA32_SPEC_CTRL,
11633 MSR_TYPE_R | MSR_TYPE_W);
11634
Ashok Raj15d45072018-02-01 22:59:43 +010011635 if (pred_cmd)
11636 nested_vmx_disable_intercept_for_msr(
11637 msr_bitmap_l1, msr_bitmap_l0,
11638 MSR_IA32_PRED_CMD,
11639 MSR_TYPE_W);
11640
Wincy Vanf2b93282015-02-03 23:56:03 +080011641 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011642 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011643
11644 return true;
11645}
11646
Liran Alon61ada742018-06-23 02:35:08 +030011647static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11648 struct vmcs12 *vmcs12)
11649{
11650 struct vmcs12 *shadow;
11651 struct page *page;
11652
11653 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11654 vmcs12->vmcs_link_pointer == -1ull)
11655 return;
11656
11657 shadow = get_shadow_vmcs12(vcpu);
11658 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11659
11660 memcpy(shadow, kmap(page), VMCS12_SIZE);
11661
11662 kunmap(page);
11663 kvm_release_page_clean(page);
11664}
11665
11666static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11667 struct vmcs12 *vmcs12)
11668{
11669 struct vcpu_vmx *vmx = to_vmx(vcpu);
11670
11671 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11672 vmcs12->vmcs_link_pointer == -1ull)
11673 return;
11674
11675 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11676 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11677}
11678
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011679static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11680 struct vmcs12 *vmcs12)
11681{
11682 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11683 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11684 return -EINVAL;
11685 else
11686 return 0;
11687}
11688
Wincy Vanf2b93282015-02-03 23:56:03 +080011689static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11690 struct vmcs12 *vmcs12)
11691{
Wincy Van82f0dd42015-02-03 23:57:18 +080011692 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011693 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011694 !nested_cpu_has_vid(vmcs12) &&
11695 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011696 return 0;
11697
11698 /*
11699 * If virtualize x2apic mode is enabled,
11700 * virtualize apic access must be disabled.
11701 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011702 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11703 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011704 return -EINVAL;
11705
Wincy Van608406e2015-02-03 23:57:51 +080011706 /*
11707 * If virtual interrupt delivery is enabled,
11708 * we must exit on external interrupts.
11709 */
11710 if (nested_cpu_has_vid(vmcs12) &&
11711 !nested_exit_on_intr(vcpu))
11712 return -EINVAL;
11713
Wincy Van705699a2015-02-03 23:58:17 +080011714 /*
11715 * bits 15:8 should be zero in posted_intr_nv,
11716 * the descriptor address has been already checked
11717 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011718 *
11719 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011720 */
11721 if (nested_cpu_has_posted_intr(vmcs12) &&
11722 (!nested_cpu_has_vid(vmcs12) ||
11723 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011724 (vmcs12->posted_intr_nv & 0xff00) ||
11725 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11726 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011727 return -EINVAL;
11728
Wincy Vanf2b93282015-02-03 23:56:03 +080011729 /* tpr shadow is needed by all apicv features. */
11730 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11731 return -EINVAL;
11732
11733 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011734}
11735
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011736static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11737 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011738 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011739{
Liran Alone2536742018-06-23 02:35:02 +030011740 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011741 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011742 u64 count, addr;
11743
Liran Alone2536742018-06-23 02:35:02 +030011744 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11745 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011746 WARN_ON(1);
11747 return -EINVAL;
11748 }
11749 if (count == 0)
11750 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011751 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011752 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11753 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011754 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011755 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11756 addr_field, maxphyaddr, count, addr);
11757 return -EINVAL;
11758 }
11759 return 0;
11760}
11761
11762static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11763 struct vmcs12 *vmcs12)
11764{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011765 if (vmcs12->vm_exit_msr_load_count == 0 &&
11766 vmcs12->vm_exit_msr_store_count == 0 &&
11767 vmcs12->vm_entry_msr_load_count == 0)
11768 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011769 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011770 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011771 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011772 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011773 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011774 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011775 return -EINVAL;
11776 return 0;
11777}
11778
Bandan Dasc5f983f2017-05-05 15:25:14 -040011779static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11780 struct vmcs12 *vmcs12)
11781{
11782 u64 address = vmcs12->pml_address;
11783 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11784
11785 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11786 if (!nested_cpu_has_ept(vmcs12) ||
11787 !IS_ALIGNED(address, 4096) ||
11788 address >> maxphyaddr)
11789 return -EINVAL;
11790 }
11791
11792 return 0;
11793}
11794
Liran Alona8a7c022018-06-23 02:35:06 +030011795static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11796 struct vmcs12 *vmcs12)
11797{
11798 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11799 return 0;
11800
11801 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11802 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11803 return -EINVAL;
11804
11805 return 0;
11806}
11807
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011808static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11809 struct vmx_msr_entry *e)
11810{
11811 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011812 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011813 return -EINVAL;
11814 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11815 e->index == MSR_IA32_UCODE_REV)
11816 return -EINVAL;
11817 if (e->reserved != 0)
11818 return -EINVAL;
11819 return 0;
11820}
11821
11822static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11823 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011824{
11825 if (e->index == MSR_FS_BASE ||
11826 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011827 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11828 nested_vmx_msr_check_common(vcpu, e))
11829 return -EINVAL;
11830 return 0;
11831}
11832
11833static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11834 struct vmx_msr_entry *e)
11835{
11836 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11837 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011838 return -EINVAL;
11839 return 0;
11840}
11841
11842/*
11843 * Load guest's/host's msr at nested entry/exit.
11844 * return 0 for success, entry index for failure.
11845 */
11846static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11847{
11848 u32 i;
11849 struct vmx_msr_entry e;
11850 struct msr_data msr;
11851
11852 msr.host_initiated = false;
11853 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011854 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11855 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011856 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011857 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11858 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011859 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011860 }
11861 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011862 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011863 "%s check failed (%u, 0x%x, 0x%x)\n",
11864 __func__, i, e.index, e.reserved);
11865 goto fail;
11866 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011867 msr.index = e.index;
11868 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011869 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011870 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011871 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11872 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011873 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011874 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011875 }
11876 return 0;
11877fail:
11878 return i + 1;
11879}
11880
11881static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11882{
11883 u32 i;
11884 struct vmx_msr_entry e;
11885
11886 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011887 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011888 if (kvm_vcpu_read_guest(vcpu,
11889 gpa + i * sizeof(e),
11890 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011891 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011892 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11893 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011894 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011895 }
11896 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011897 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011898 "%s check failed (%u, 0x%x, 0x%x)\n",
11899 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011900 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011901 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011902 msr_info.host_initiated = false;
11903 msr_info.index = e.index;
11904 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011905 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011906 "%s cannot read MSR (%u, 0x%x)\n",
11907 __func__, i, e.index);
11908 return -EINVAL;
11909 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011910 if (kvm_vcpu_write_guest(vcpu,
11911 gpa + i * sizeof(e) +
11912 offsetof(struct vmx_msr_entry, value),
11913 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011914 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011915 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011916 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011917 return -EINVAL;
11918 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011919 }
11920 return 0;
11921}
11922
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011923static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11924{
11925 unsigned long invalid_mask;
11926
11927 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11928 return (val & invalid_mask) == 0;
11929}
11930
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011931/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011932 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11933 * emulating VM entry into a guest with EPT enabled.
11934 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11935 * is assigned to entry_failure_code on failure.
11936 */
11937static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011938 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011939{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011940 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011941 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011942 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11943 return 1;
11944 }
11945
11946 /*
11947 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11948 * must not be dereferenced.
11949 */
11950 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11951 !nested_ept) {
11952 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11953 *entry_failure_code = ENTRY_FAIL_PDPTE;
11954 return 1;
11955 }
11956 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011957 }
11958
Junaid Shahid50c28f22018-06-27 14:59:11 -070011959 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011960 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011961
11962 vcpu->arch.cr3 = cr3;
11963 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11964
11965 kvm_init_mmu(vcpu, false);
11966
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011967 return 0;
11968}
11969
Liran Alonefebf0a2018-10-08 23:42:20 +030011970/*
11971 * Returns if KVM is able to config CPU to tag TLB entries
11972 * populated by L2 differently than TLB entries populated
11973 * by L1.
11974 *
11975 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11976 *
11977 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11978 * with different VPID (L1 entries are tagged with vmx->vpid
11979 * while L2 entries are tagged with vmx->nested.vpid02).
11980 */
11981static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11982{
11983 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11984
11985 return nested_cpu_has_ept(vmcs12) ||
11986 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11987}
11988
Jim Mattson6514dc32018-04-26 16:09:12 -070011989static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011990{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011991 struct vcpu_vmx *vmx = to_vmx(vcpu);
11992
11993 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11994 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11995 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11996 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11997 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11998 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11999 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12000 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12001 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12002 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12003 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12004 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12005 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12006 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12007 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12008 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12009 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12010 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12011 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12012 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12013 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12014 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12015 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12016 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12017 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12018 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12019 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12020 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12021 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12022 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12023 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012024
12025 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12026 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12027 vmcs12->guest_pending_dbg_exceptions);
12028 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12029 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12030
12031 if (nested_cpu_has_xsaves(vmcs12))
12032 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12033 vmcs_write64(VMCS_LINK_POINTER, -1ull);
12034
12035 if (cpu_has_vmx_posted_intr())
12036 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12037
12038 /*
12039 * Whether page-faults are trapped is determined by a combination of
12040 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12041 * If enable_ept, L0 doesn't care about page faults and we should
12042 * set all of these to L1's desires. However, if !enable_ept, L0 does
12043 * care about (at least some) page faults, and because it is not easy
12044 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12045 * to exit on each and every L2 page fault. This is done by setting
12046 * MASK=MATCH=0 and (see below) EB.PF=1.
12047 * Note that below we don't need special code to set EB.PF beyond the
12048 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12049 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12050 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12051 */
12052 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12053 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12054 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12055 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12056
12057 /* All VMFUNCs are currently emulated through L0 vmexits. */
12058 if (cpu_has_vmx_vmfunc())
12059 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12060
12061 if (cpu_has_vmx_apicv()) {
12062 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12063 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12064 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12065 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12066 }
12067
12068 /*
12069 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12070 * Some constant fields are set here by vmx_set_constant_host_state().
12071 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070012072 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12073 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012074 */
12075 vmx_set_constant_host_state(vmx);
12076
12077 /*
12078 * Set the MSR load/store lists to match L0's settings.
12079 */
12080 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012081 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12082 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12083 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12084 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012085
12086 set_cr4_guest_host_mask(vmx);
12087
Liran Alon62cf9bd812018-09-14 03:25:54 +030012088 if (kvm_mpx_supported()) {
12089 if (vmx->nested.nested_run_pending &&
12090 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12091 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12092 else
12093 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12094 }
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012095
12096 if (enable_vpid) {
12097 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12098 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12099 else
12100 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12101 }
12102
12103 /*
12104 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12105 */
12106 if (enable_ept) {
12107 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12108 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12109 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12110 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12111 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012112
12113 if (cpu_has_vmx_msr_bitmap())
12114 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012115}
12116
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012117/*
12118 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12119 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012120 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012121 * guest in a way that will both be appropriate to L1's requests, and our
12122 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12123 * function also has additional necessary side-effects, like setting various
12124 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012125 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12126 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012127 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012128static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012129 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012130{
12131 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012132 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012133
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012134 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012135 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012136 vmx->nested.dirty_vmcs12 = false;
12137 }
12138
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012139 /*
12140 * First, the fields that are shadowed. This must be kept in sync
12141 * with vmx_shadow_fields.h.
12142 */
12143
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012144 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012145 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012146 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012147 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12148 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012149
Jim Mattson6514dc32018-04-26 16:09:12 -070012150 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012151 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012152 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12153 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12154 } else {
12155 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12156 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12157 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012158 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012159 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12160 vmcs12->vm_entry_intr_info_field);
12161 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12162 vmcs12->vm_entry_exception_error_code);
12163 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12164 vmcs12->vm_entry_instruction_len);
12165 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12166 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012167 vmx->loaded_vmcs->nmi_known_unmasked =
12168 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012169 } else {
12170 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12171 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012172 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012173
Jan Kiszkaf4124502014-03-07 20:03:13 +010012174 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012175
Sean Christophersonf459a702018-08-27 15:21:11 -070012176 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012177 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012178 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12179 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012180
12181 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012182 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012183 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12184 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012185 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012186 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012187 }
Wincy Van705699a2015-02-03 23:58:17 +080012188
Jan Kiszkaf4124502014-03-07 20:03:13 +010012189 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012190
Jan Kiszkaf4124502014-03-07 20:03:13 +010012191 vmx->nested.preemption_timer_expired = false;
12192 if (nested_cpu_has_preemption_timer(vmcs12))
12193 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012194
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012195 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012196 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012197
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012198 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012199 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012200 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012201 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012202 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012203 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012204 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12205 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012206 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012207 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12208 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12209 ~SECONDARY_EXEC_ENABLE_PML;
12210 exec_control |= vmcs12_exec_ctrl;
12211 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012212
Liran Alon32c7acf2018-06-23 02:35:11 +030012213 /* VMCS shadowing for L2 is emulated for now */
12214 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12215
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012216 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012217 vmcs_write16(GUEST_INTR_STATUS,
12218 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012219
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012220 /*
12221 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12222 * nested_get_vmcs12_pages will either fix it up or
12223 * remove the VM execution control.
12224 */
12225 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12226 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12227
Sean Christopherson0b665d32018-08-14 09:33:34 -070012228 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12229 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12230
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012231 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12232 }
12233
Jim Mattson83bafef2016-10-04 10:48:38 -070012234 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012235 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12236 * entry, but only if the current (host) sp changed from the value
12237 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12238 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12239 * here we just force the write to happen on entry.
12240 */
12241 vmx->host_rsp = 0;
12242
12243 exec_control = vmx_exec_control(vmx); /* L0's desires */
12244 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12245 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12246 exec_control &= ~CPU_BASED_TPR_SHADOW;
12247 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012248
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012249 /*
12250 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12251 * nested_get_vmcs12_pages can't fix it up, the illegal value
12252 * will result in a VM entry failure.
12253 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012254 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012255 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012256 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012257 } else {
12258#ifdef CONFIG_X86_64
12259 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12260 CPU_BASED_CR8_STORE_EXITING;
12261#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012262 }
12263
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012264 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012265 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12266 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012267 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012268 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12269 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12270
12271 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12272
12273 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12274 * bitwise-or of what L1 wants to trap for L2, and what we want to
12275 * trap. Note that CR0.TS also needs updating - we do this later.
12276 */
12277 update_exception_bitmap(vcpu);
12278 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12279 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12280
Nadav Har'El8049d652013-08-05 11:07:06 +030012281 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12282 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12283 * bits are further modified by vmx_set_efer() below.
12284 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012285 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012286
12287 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12288 * emulated by vmx_set_efer(), below.
12289 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012290 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012291 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12292 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012293 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12294
Jim Mattson6514dc32018-04-26 16:09:12 -070012295 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012296 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012297 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012298 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012299 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012300 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012301 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012302
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012303 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12304
Peter Feinerc95ba922016-08-17 09:36:47 -070012305 if (kvm_has_tsc_control)
12306 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012307
12308 if (enable_vpid) {
12309 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012310 * There is no direct mapping between vpid02 and vpid12, the
12311 * vpid02 is per-vCPU for L0 and reused while the value of
12312 * vpid12 is changed w/ one invvpid during nested vmentry.
12313 * The vpid12 is allocated by L1 for L2, so it will not
12314 * influence global bitmap(for vpid01 and vpid02 allocation)
12315 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012316 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012317 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012318 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12319 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012320 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012321 }
12322 } else {
Liran Alon14389212018-10-08 23:42:17 +030012323 /*
12324 * If L1 use EPT, then L0 needs to execute INVEPT on
12325 * EPTP02 instead of EPTP01. Therefore, delay TLB
12326 * flush until vmcs02->eptp is fully updated by
12327 * KVM_REQ_LOAD_CR3. Note that this assumes
12328 * KVM_REQ_TLB_FLUSH is evaluated after
12329 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12330 */
12331 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012332 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012333 }
12334
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012335 if (enable_pml) {
12336 /*
12337 * Conceptually we want to copy the PML address and index from
12338 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12339 * since we always flush the log on each vmexit, this happens
12340 * to be equivalent to simply resetting the fields in vmcs02.
12341 */
12342 ASSERT(vmx->pml_pg);
12343 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12344 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12345 }
12346
Nadav Har'El155a97a2013-08-05 11:07:16 +030012347 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012348 if (nested_ept_init_mmu_context(vcpu)) {
12349 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12350 return 1;
12351 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012352 } else if (nested_cpu_has2(vmcs12,
12353 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012354 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012355 }
12356
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012357 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012358 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12359 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012360 * The CR0_READ_SHADOW is what L2 should have expected to read given
12361 * the specifications by L1; It's not enough to take
12362 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12363 * have more bits than L1 expected.
12364 */
12365 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12366 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12367
12368 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12369 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12370
Jim Mattson6514dc32018-04-26 16:09:12 -070012371 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012372 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012373 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12374 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12375 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12376 else
12377 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12378 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12379 vmx_set_efer(vcpu, vcpu->arch.efer);
12380
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012381 /*
12382 * Guest state is invalid and unrestricted guest is disabled,
12383 * which means L1 attempted VMEntry to L2 with invalid state.
12384 * Fail the VMEntry.
12385 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012386 if (vmx->emulation_required) {
12387 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012388 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012389 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012390
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012391 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012392 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012393 entry_failure_code))
12394 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012395
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012396 if (!enable_ept)
12397 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12398
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012399 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12400 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012401 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012402}
12403
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012404static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12405{
12406 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12407 nested_cpu_has_virtual_nmis(vmcs12))
12408 return -EINVAL;
12409
12410 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12411 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12412 return -EINVAL;
12413
12414 return 0;
12415}
12416
Jim Mattsonca0bde22016-11-30 12:03:46 -080012417static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12418{
12419 struct vcpu_vmx *vmx = to_vmx(vcpu);
12420
12421 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12422 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12423 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12424
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012425 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12426 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12427
Jim Mattson56a20512017-07-06 16:33:06 -070012428 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12429 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12430
Jim Mattsonca0bde22016-11-30 12:03:46 -080012431 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12432 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12433
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012434 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12435 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12436
Jim Mattson712b12d2017-08-24 13:24:47 -070012437 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12438 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12439
Jim Mattsonca0bde22016-11-30 12:03:46 -080012440 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12441 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12442
12443 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12444 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12445
Bandan Dasc5f983f2017-05-05 15:25:14 -040012446 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12447 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12448
Liran Alona8a7c022018-06-23 02:35:06 +030012449 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12450 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12451
Jim Mattsonca0bde22016-11-30 12:03:46 -080012452 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012453 vmx->nested.msrs.procbased_ctls_low,
12454 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012455 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12456 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012457 vmx->nested.msrs.secondary_ctls_low,
12458 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012459 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012460 vmx->nested.msrs.pinbased_ctls_low,
12461 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012462 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012463 vmx->nested.msrs.exit_ctls_low,
12464 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012465 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012466 vmx->nested.msrs.entry_ctls_low,
12467 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012468 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12469
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012470 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012471 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12472
Bandan Das41ab9372017-08-03 15:54:43 -040012473 if (nested_cpu_has_vmfunc(vmcs12)) {
12474 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012475 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012476 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12477
12478 if (nested_cpu_has_eptp_switching(vmcs12)) {
12479 if (!nested_cpu_has_ept(vmcs12) ||
12480 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12481 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12482 }
12483 }
Bandan Das27c42a12017-08-03 15:54:42 -040012484
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012485 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12486 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12487
Jim Mattsonca0bde22016-11-30 12:03:46 -080012488 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12489 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12490 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12491 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12492
Marc Orr04473782018-06-20 17:21:29 -070012493 /*
12494 * From the Intel SDM, volume 3:
12495 * Fields relevant to VM-entry event injection must be set properly.
12496 * These fields are the VM-entry interruption-information field, the
12497 * VM-entry exception error code, and the VM-entry instruction length.
12498 */
12499 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12500 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12501 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12502 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12503 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12504 bool should_have_error_code;
12505 bool urg = nested_cpu_has2(vmcs12,
12506 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12507 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12508
12509 /* VM-entry interruption-info field: interruption type */
12510 if (intr_type == INTR_TYPE_RESERVED ||
12511 (intr_type == INTR_TYPE_OTHER_EVENT &&
12512 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12513 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12514
12515 /* VM-entry interruption-info field: vector */
12516 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12517 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12518 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12519 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12520
12521 /* VM-entry interruption-info field: deliver error code */
12522 should_have_error_code =
12523 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12524 x86_exception_has_error_code(vector);
12525 if (has_error_code != should_have_error_code)
12526 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12527
12528 /* VM-entry exception error code */
12529 if (has_error_code &&
12530 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12531 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12532
12533 /* VM-entry interruption-info field: reserved bits */
12534 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12535 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12536
12537 /* VM-entry instruction length */
12538 switch (intr_type) {
12539 case INTR_TYPE_SOFT_EXCEPTION:
12540 case INTR_TYPE_SOFT_INTR:
12541 case INTR_TYPE_PRIV_SW_EXCEPTION:
12542 if ((vmcs12->vm_entry_instruction_len > 15) ||
12543 (vmcs12->vm_entry_instruction_len == 0 &&
12544 !nested_cpu_has_zero_length_injection(vcpu)))
12545 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12546 }
12547 }
12548
Jim Mattsonca0bde22016-11-30 12:03:46 -080012549 return 0;
12550}
12551
Liran Alonf145d902018-06-23 02:35:07 +030012552static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12553 struct vmcs12 *vmcs12)
12554{
12555 int r;
12556 struct page *page;
12557 struct vmcs12 *shadow;
12558
12559 if (vmcs12->vmcs_link_pointer == -1ull)
12560 return 0;
12561
12562 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12563 return -EINVAL;
12564
12565 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12566 if (is_error_page(page))
12567 return -EINVAL;
12568
12569 r = 0;
12570 shadow = kmap(page);
12571 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12572 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12573 r = -EINVAL;
12574 kunmap(page);
12575 kvm_release_page_clean(page);
12576 return r;
12577}
12578
Jim Mattsonca0bde22016-11-30 12:03:46 -080012579static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12580 u32 *exit_qual)
12581{
12582 bool ia32e;
12583
12584 *exit_qual = ENTRY_FAIL_DEFAULT;
12585
12586 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12587 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12588 return 1;
12589
Liran Alonf145d902018-06-23 02:35:07 +030012590 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012591 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12592 return 1;
12593 }
12594
12595 /*
12596 * If the load IA32_EFER VM-entry control is 1, the following checks
12597 * are performed on the field for the IA32_EFER MSR:
12598 * - Bits reserved in the IA32_EFER MSR must be 0.
12599 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12600 * the IA-32e mode guest VM-exit control. It must also be identical
12601 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12602 * CR0.PG) is 1.
12603 */
12604 if (to_vmx(vcpu)->nested.nested_run_pending &&
12605 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12606 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12607 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12608 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12609 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12610 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12611 return 1;
12612 }
12613
12614 /*
12615 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12616 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12617 * the values of the LMA and LME bits in the field must each be that of
12618 * the host address-space size VM-exit control.
12619 */
12620 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12621 ia32e = (vmcs12->vm_exit_controls &
12622 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12623 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12624 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12625 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12626 return 1;
12627 }
12628
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012629 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12630 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12631 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12632 return 1;
12633
Jim Mattsonca0bde22016-11-30 12:03:46 -080012634 return 0;
12635}
12636
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012637/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012638 * If exit_qual is NULL, this is being called from state restore (either RSM
12639 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012640 */
12641static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012642{
12643 struct vcpu_vmx *vmx = to_vmx(vcpu);
12644 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012645 bool from_vmentry = !!exit_qual;
12646 u32 dummy_exit_qual;
Paolo Bonzini7e712682018-10-03 13:44:26 +020012647 bool evaluate_pending_interrupts;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012648 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012649
Paolo Bonzini7e712682018-10-03 13:44:26 +020012650 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12651 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12652 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12653 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012654
Jim Mattson858e25c2016-11-30 12:03:47 -080012655 enter_guest_mode(vcpu);
12656
12657 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12658 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012659 if (kvm_mpx_supported() &&
12660 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12661 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012662
Jim Mattsonde3a0022017-11-27 17:22:25 -060012663 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012664 vmx_segment_cache_clear(vmx);
12665
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012666 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12667 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12668
12669 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012670 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012671 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012672
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012673 if (from_vmentry) {
12674 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012675
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012676 r = EXIT_REASON_MSR_LOAD_FAIL;
12677 *exit_qual = nested_vmx_load_msr(vcpu,
12678 vmcs12->vm_entry_msr_load_addr,
12679 vmcs12->vm_entry_msr_load_count);
12680 if (*exit_qual)
12681 goto fail;
12682 } else {
12683 /*
12684 * The MMU is not initialized to point at the right entities yet and
12685 * "get pages" would need to read data from the guest (i.e. we will
12686 * need to perform gpa to hpa translation). Request a call
12687 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12688 * have already been set at vmentry time and should not be reset.
12689 */
12690 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12691 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012692
Jim Mattson858e25c2016-11-30 12:03:47 -080012693 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012694 * If L1 had a pending IRQ/NMI until it executed
12695 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12696 * disallowed (e.g. interrupts disabled), L0 needs to
12697 * evaluate if this pending event should cause an exit from L2
12698 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12699 * intercept EXTERNAL_INTERRUPT).
12700 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012701 * Usually this would be handled by the processor noticing an
12702 * IRQ/NMI window request, or checking RVI during evaluation of
12703 * pending virtual interrupts. However, this setting was done
12704 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12705 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012706 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012707 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012708 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012709
12710 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012711 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12712 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12713 * returned as far as L1 is concerned. It will only return (and set
12714 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12715 */
12716 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012717
12718fail:
12719 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12720 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12721 leave_guest_mode(vcpu);
12722 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012723 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012724}
12725
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012726/*
12727 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12728 * for running an L2 nested guest.
12729 */
12730static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12731{
12732 struct vmcs12 *vmcs12;
12733 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012734 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012735 u32 exit_qual;
12736 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012737
Kyle Hueyeb277562016-11-29 12:40:39 -080012738 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012739 return 1;
12740
Kyle Hueyeb277562016-11-29 12:40:39 -080012741 if (!nested_vmx_check_vmcs12(vcpu))
12742 goto out;
12743
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012744 vmcs12 = get_vmcs12(vcpu);
12745
Liran Alona6192d42018-06-23 02:35:04 +030012746 /*
12747 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12748 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12749 * rather than RFLAGS.ZF, and no error number is stored to the
12750 * VM-instruction error field.
12751 */
12752 if (vmcs12->hdr.shadow_vmcs) {
12753 nested_vmx_failInvalid(vcpu);
12754 goto out;
12755 }
12756
Abel Gordon012f83c2013-04-18 14:39:25 +030012757 if (enable_shadow_vmcs)
12758 copy_shadow_to_vmcs12(vmx);
12759
Nadav Har'El7c177932011-05-25 23:12:04 +030012760 /*
12761 * The nested entry process starts with enforcing various prerequisites
12762 * on vmcs12 as required by the Intel SDM, and act appropriately when
12763 * they fail: As the SDM explains, some conditions should cause the
12764 * instruction to fail, while others will cause the instruction to seem
12765 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12766 * To speed up the normal (success) code path, we should avoid checking
12767 * for misconfigurations which will anyway be caught by the processor
12768 * when using the merged vmcs02.
12769 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012770 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12771 nested_vmx_failValid(vcpu,
12772 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12773 goto out;
12774 }
12775
Nadav Har'El7c177932011-05-25 23:12:04 +030012776 if (vmcs12->launch_state == launch) {
12777 nested_vmx_failValid(vcpu,
12778 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12779 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012780 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012781 }
12782
Jim Mattsonca0bde22016-11-30 12:03:46 -080012783 ret = check_vmentry_prereqs(vcpu, vmcs12);
12784 if (ret) {
12785 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012786 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012787 }
12788
Nadav Har'El7c177932011-05-25 23:12:04 +030012789 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012790 * After this point, the trap flag no longer triggers a singlestep trap
12791 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12792 * This is not 100% correct; for performance reasons, we delegate most
12793 * of the checks on host state to the processor. If those fail,
12794 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012795 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012796 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012797
Jim Mattsonca0bde22016-11-30 12:03:46 -080012798 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12799 if (ret) {
12800 nested_vmx_entry_failure(vcpu, vmcs12,
12801 EXIT_REASON_INVALID_STATE, exit_qual);
12802 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012803 }
12804
12805 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012806 * We're finally done with prerequisite checking, and can start with
12807 * the nested entry.
12808 */
12809
Jim Mattson6514dc32018-04-26 16:09:12 -070012810 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012811 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012812 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012813 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012814 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012815 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012816 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012817
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012818 /* Hide L1D cache contents from the nested guest. */
12819 vmx->vcpu.arch.l1tf_flush_l1d = true;
12820
Chao Gao135a06c2018-02-11 10:06:30 +080012821 /*
Liran Alon61ada742018-06-23 02:35:08 +030012822 * Must happen outside of enter_vmx_non_root_mode() as it will
12823 * also be used as part of restoring nVMX state for
12824 * snapshot restore (migration).
12825 *
12826 * In this flow, it is assumed that vmcs12 cache was
12827 * trasferred as part of captured nVMX state and should
12828 * therefore not be read from guest memory (which may not
12829 * exist on destination host yet).
12830 */
12831 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12832
12833 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012834 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12835 * by event injection, halt vcpu.
12836 */
12837 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012838 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12839 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012840 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012841 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012842 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012843
12844out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012845 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012846}
12847
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012848/*
12849 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12850 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12851 * This function returns the new value we should put in vmcs12.guest_cr0.
12852 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12853 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12854 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12855 * didn't trap the bit, because if L1 did, so would L0).
12856 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12857 * been modified by L2, and L1 knows it. So just leave the old value of
12858 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12859 * isn't relevant, because if L0 traps this bit it can set it to anything.
12860 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12861 * changed these bits, and therefore they need to be updated, but L0
12862 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12863 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12864 */
12865static inline unsigned long
12866vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12867{
12868 return
12869 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12870 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12871 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12872 vcpu->arch.cr0_guest_owned_bits));
12873}
12874
12875static inline unsigned long
12876vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12877{
12878 return
12879 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12880 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12881 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12882 vcpu->arch.cr4_guest_owned_bits));
12883}
12884
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012885static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12886 struct vmcs12 *vmcs12)
12887{
12888 u32 idt_vectoring;
12889 unsigned int nr;
12890
Wanpeng Li664f8e22017-08-24 03:35:09 -070012891 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012892 nr = vcpu->arch.exception.nr;
12893 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12894
12895 if (kvm_exception_is_soft(nr)) {
12896 vmcs12->vm_exit_instruction_len =
12897 vcpu->arch.event_exit_inst_len;
12898 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12899 } else
12900 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12901
12902 if (vcpu->arch.exception.has_error_code) {
12903 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12904 vmcs12->idt_vectoring_error_code =
12905 vcpu->arch.exception.error_code;
12906 }
12907
12908 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012909 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012910 vmcs12->idt_vectoring_info_field =
12911 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012912 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012913 nr = vcpu->arch.interrupt.nr;
12914 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12915
12916 if (vcpu->arch.interrupt.soft) {
12917 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12918 vmcs12->vm_entry_instruction_len =
12919 vcpu->arch.event_exit_inst_len;
12920 } else
12921 idt_vectoring |= INTR_TYPE_EXT_INTR;
12922
12923 vmcs12->idt_vectoring_info_field = idt_vectoring;
12924 }
12925}
12926
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012927static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12928{
12929 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012930 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012931 bool block_nested_events =
12932 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012933
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012934 if (vcpu->arch.exception.pending &&
12935 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012936 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012937 return -EBUSY;
12938 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012939 return 0;
12940 }
12941
Jan Kiszkaf4124502014-03-07 20:03:13 +010012942 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12943 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012944 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012945 return -EBUSY;
12946 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12947 return 0;
12948 }
12949
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012950 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012951 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012952 return -EBUSY;
12953 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12954 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12955 INTR_INFO_VALID_MASK, 0);
12956 /*
12957 * The NMI-triggered VM exit counts as injection:
12958 * clear this one and block further NMIs.
12959 */
12960 vcpu->arch.nmi_pending = 0;
12961 vmx_set_nmi_mask(vcpu, true);
12962 return 0;
12963 }
12964
12965 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12966 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012967 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012968 return -EBUSY;
12969 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012970 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012971 }
12972
David Hildenbrand6342c502017-01-25 11:58:58 +010012973 vmx_complete_nested_posted_interrupt(vcpu);
12974 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012975}
12976
Sean Christophersond264ee02018-08-27 15:21:12 -070012977static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
12978{
12979 to_vmx(vcpu)->req_immediate_exit = true;
12980}
12981
Jan Kiszkaf4124502014-03-07 20:03:13 +010012982static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12983{
12984 ktime_t remaining =
12985 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12986 u64 value;
12987
12988 if (ktime_to_ns(remaining) <= 0)
12989 return 0;
12990
12991 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12992 do_div(value, 1000000);
12993 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12994}
12995
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012996/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012997 * Update the guest state fields of vmcs12 to reflect changes that
12998 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12999 * VM-entry controls is also updated, since this is really a guest
13000 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013001 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013002static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013003{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013004 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13005 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13006
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013007 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13008 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13009 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13010
13011 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13012 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13013 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13014 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13015 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13016 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13017 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13018 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13019 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13020 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13021 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13022 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13023 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13024 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13025 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13026 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13027 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13028 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13029 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13030 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13031 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13032 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13033 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13034 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13035 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13036 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13037 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13038 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13039 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13040 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13041 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13042 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13043 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13044 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13045 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13046 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13047
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013048 vmcs12->guest_interruptibility_info =
13049 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13050 vmcs12->guest_pending_dbg_exceptions =
13051 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013052 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13053 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13054 else
13055 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013056
Jan Kiszkaf4124502014-03-07 20:03:13 +010013057 if (nested_cpu_has_preemption_timer(vmcs12)) {
13058 if (vmcs12->vm_exit_controls &
13059 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13060 vmcs12->vmx_preemption_timer_value =
13061 vmx_get_preemption_timer_value(vcpu);
13062 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13063 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013064
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013065 /*
13066 * In some cases (usually, nested EPT), L2 is allowed to change its
13067 * own CR3 without exiting. If it has changed it, we must keep it.
13068 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13069 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13070 *
13071 * Additionally, restore L2's PDPTR to vmcs12.
13072 */
13073 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013074 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013075 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13076 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13077 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13078 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13079 }
13080
Jim Mattsond281e132017-06-01 12:44:46 -070013081 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013082
Wincy Van608406e2015-02-03 23:57:51 +080013083 if (nested_cpu_has_vid(vmcs12))
13084 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13085
Jan Kiszkac18911a2013-03-13 16:06:41 +010013086 vmcs12->vm_entry_controls =
13087 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013088 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013089
Jan Kiszka2996fca2014-06-16 13:59:43 +020013090 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13091 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13092 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13093 }
13094
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013095 /* TODO: These cannot have changed unless we have MSR bitmaps and
13096 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013097 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013098 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013099 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13100 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013101 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13102 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13103 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013104 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013105 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013106}
13107
13108/*
13109 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13110 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13111 * and this function updates it to reflect the changes to the guest state while
13112 * L2 was running (and perhaps made some exits which were handled directly by L0
13113 * without going back to L1), and to reflect the exit reason.
13114 * Note that we do not have to copy here all VMCS fields, just those that
13115 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13116 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13117 * which already writes to vmcs12 directly.
13118 */
13119static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13120 u32 exit_reason, u32 exit_intr_info,
13121 unsigned long exit_qualification)
13122{
13123 /* update guest state fields: */
13124 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013125
13126 /* update exit information fields: */
13127
Jan Kiszka533558b2014-01-04 18:47:20 +010013128 vmcs12->vm_exit_reason = exit_reason;
13129 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013130 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013131
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013132 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013133 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13134 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13135
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013136 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013137 vmcs12->launch_state = 1;
13138
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013139 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13140 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013141 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013142
13143 /*
13144 * Transfer the event that L0 or L1 may wanted to inject into
13145 * L2 to IDT_VECTORING_INFO_FIELD.
13146 */
13147 vmcs12_save_pending_event(vcpu, vmcs12);
13148 }
13149
13150 /*
13151 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13152 * preserved above and would only end up incorrectly in L1.
13153 */
13154 vcpu->arch.nmi_injected = false;
13155 kvm_clear_exception_queue(vcpu);
13156 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013157}
13158
13159/*
13160 * A part of what we need to when the nested L2 guest exits and we want to
13161 * run its L1 parent, is to reset L1's guest state to the host state specified
13162 * in vmcs12.
13163 * This function is to be called not only on normal nested exit, but also on
13164 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13165 * Failures During or After Loading Guest State").
13166 * This function should be called when the active VMCS is L1's (vmcs01).
13167 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013168static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13169 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013170{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013171 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013172 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013173
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013174 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13175 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013176 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013177 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13178 else
13179 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13180 vmx_set_efer(vcpu, vcpu->arch.efer);
13181
13182 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13183 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013184 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013185 /*
13186 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013187 * actually changed, because vmx_set_cr0 refers to efer set above.
13188 *
13189 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13190 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013191 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013192 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013193 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013194
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013195 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013196 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013197 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013198
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013199 nested_ept_uninit_mmu_context(vcpu);
13200
13201 /*
13202 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13203 * couldn't have changed.
13204 */
13205 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13206 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13207
13208 if (!enable_ept)
13209 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013210
Liran Alon6f1e03b2018-05-22 17:16:14 +030013211 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013212 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013213 * VMEntry/VMExit. Thus, no need to flush TLB.
13214 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013215 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13216 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013217 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013218 * Otherwise, we can preserve TLB entries as long as we are
13219 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013220 *
13221 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13222 * and therefore we request the TLB flush to happen only after VMCS EPTP
13223 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013224 */
13225 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013226 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013227 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013228 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013229
13230 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13231 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13232 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13233 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13234 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013235 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13236 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013237
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013238 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13239 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13240 vmcs_write64(GUEST_BNDCFGS, 0);
13241
Jan Kiszka44811c02013-08-04 17:17:27 +020013242 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013243 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013244 vcpu->arch.pat = vmcs12->host_ia32_pat;
13245 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013246 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13247 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13248 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013249
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013250 /* Set L1 segment info according to Intel SDM
13251 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13252 seg = (struct kvm_segment) {
13253 .base = 0,
13254 .limit = 0xFFFFFFFF,
13255 .selector = vmcs12->host_cs_selector,
13256 .type = 11,
13257 .present = 1,
13258 .s = 1,
13259 .g = 1
13260 };
13261 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13262 seg.l = 1;
13263 else
13264 seg.db = 1;
13265 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13266 seg = (struct kvm_segment) {
13267 .base = 0,
13268 .limit = 0xFFFFFFFF,
13269 .type = 3,
13270 .present = 1,
13271 .s = 1,
13272 .db = 1,
13273 .g = 1
13274 };
13275 seg.selector = vmcs12->host_ds_selector;
13276 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13277 seg.selector = vmcs12->host_es_selector;
13278 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13279 seg.selector = vmcs12->host_ss_selector;
13280 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13281 seg.selector = vmcs12->host_fs_selector;
13282 seg.base = vmcs12->host_fs_base;
13283 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13284 seg.selector = vmcs12->host_gs_selector;
13285 seg.base = vmcs12->host_gs_base;
13286 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13287 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013288 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013289 .limit = 0x67,
13290 .selector = vmcs12->host_tr_selector,
13291 .type = 11,
13292 .present = 1
13293 };
13294 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13295
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013296 kvm_set_dr(vcpu, 7, 0x400);
13297 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013298
Wincy Van3af18d92015-02-03 23:49:31 +080013299 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013300 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013301
Wincy Vanff651cb2014-12-11 08:52:58 +030013302 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13303 vmcs12->vm_exit_msr_load_count))
13304 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013305}
13306
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013307static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13308{
13309 struct shared_msr_entry *efer_msr;
13310 unsigned int i;
13311
13312 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13313 return vmcs_read64(GUEST_IA32_EFER);
13314
13315 if (cpu_has_load_ia32_efer)
13316 return host_efer;
13317
13318 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13319 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13320 return vmx->msr_autoload.guest.val[i].value;
13321 }
13322
13323 efer_msr = find_msr_entry(vmx, MSR_EFER);
13324 if (efer_msr)
13325 return efer_msr->data;
13326
13327 return host_efer;
13328}
13329
13330static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13331{
13332 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13333 struct vcpu_vmx *vmx = to_vmx(vcpu);
13334 struct vmx_msr_entry g, h;
13335 struct msr_data msr;
13336 gpa_t gpa;
13337 u32 i, j;
13338
13339 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13340
13341 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13342 /*
13343 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13344 * as vmcs01.GUEST_DR7 contains a userspace defined value
13345 * and vcpu->arch.dr7 is not squirreled away before the
13346 * nested VMENTER (not worth adding a variable in nested_vmx).
13347 */
13348 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13349 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13350 else
13351 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13352 }
13353
13354 /*
13355 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13356 * handle a variety of side effects to KVM's software model.
13357 */
13358 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13359
13360 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13361 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13362
13363 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13364 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13365
13366 nested_ept_uninit_mmu_context(vcpu);
13367 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13368 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13369
13370 /*
13371 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13372 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13373 * VMFail, like everything else we just need to ensure our
13374 * software model is up-to-date.
13375 */
13376 ept_save_pdptrs(vcpu);
13377
13378 kvm_mmu_reset_context(vcpu);
13379
13380 if (cpu_has_vmx_msr_bitmap())
13381 vmx_update_msr_bitmap(vcpu);
13382
13383 /*
13384 * This nasty bit of open coding is a compromise between blindly
13385 * loading L1's MSRs using the exit load lists (incorrect emulation
13386 * of VMFail), leaving the nested VM's MSRs in the software model
13387 * (incorrect behavior) and snapshotting the modified MSRs (too
13388 * expensive since the lists are unbound by hardware). For each
13389 * MSR that was (prematurely) loaded from the nested VMEntry load
13390 * list, reload it from the exit load list if it exists and differs
13391 * from the guest value. The intent is to stuff host state as
13392 * silently as possible, not to fully process the exit load list.
13393 */
13394 msr.host_initiated = false;
13395 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13396 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13397 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13398 pr_debug_ratelimited(
13399 "%s read MSR index failed (%u, 0x%08llx)\n",
13400 __func__, i, gpa);
13401 goto vmabort;
13402 }
13403
13404 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13405 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13406 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13407 pr_debug_ratelimited(
13408 "%s read MSR failed (%u, 0x%08llx)\n",
13409 __func__, j, gpa);
13410 goto vmabort;
13411 }
13412 if (h.index != g.index)
13413 continue;
13414 if (h.value == g.value)
13415 break;
13416
13417 if (nested_vmx_load_msr_check(vcpu, &h)) {
13418 pr_debug_ratelimited(
13419 "%s check failed (%u, 0x%x, 0x%x)\n",
13420 __func__, j, h.index, h.reserved);
13421 goto vmabort;
13422 }
13423
13424 msr.index = h.index;
13425 msr.data = h.value;
13426 if (kvm_set_msr(vcpu, &msr)) {
13427 pr_debug_ratelimited(
13428 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13429 __func__, j, h.index, h.value);
13430 goto vmabort;
13431 }
13432 }
13433 }
13434
13435 return;
13436
13437vmabort:
13438 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13439}
13440
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013441/*
13442 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13443 * and modify vmcs12 to make it see what it would expect to see there if
13444 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13445 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013446static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13447 u32 exit_intr_info,
13448 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013449{
13450 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13452
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013453 /* trying to cancel vmlaunch/vmresume is a bug */
13454 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13455
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013456 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013457 * The only expected VM-instruction error is "VM entry with
13458 * invalid control field(s)." Anything else indicates a
13459 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013460 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013461 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13462 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13463
13464 leave_guest_mode(vcpu);
13465
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013466 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13467 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13468
Jim Mattson4f350c62017-09-14 16:31:44 -070013469 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013470 if (exit_reason == -1)
13471 sync_vmcs12(vcpu, vmcs12);
13472 else
13473 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13474 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013475
Liran Alon61ada742018-06-23 02:35:08 +030013476 /*
13477 * Must happen outside of sync_vmcs12() as it will
13478 * also be used to capture vmcs12 cache as part of
13479 * capturing nVMX state for snapshot (migration).
13480 *
13481 * Otherwise, this flush will dirty guest memory at a
13482 * point it is already assumed by user-space to be
13483 * immutable.
13484 */
13485 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13486
Jim Mattson4f350c62017-09-14 16:31:44 -070013487 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13488 vmcs12->vm_exit_msr_store_count))
13489 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013490 }
13491
Jim Mattson4f350c62017-09-14 16:31:44 -070013492 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013493 vm_entry_controls_reset_shadow(vmx);
13494 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013495 vmx_segment_cache_clear(vmx);
13496
Paolo Bonzini93140062016-07-06 13:23:51 +020013497 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013498 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13499 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013500 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013501
Peter Feinerc95ba922016-08-17 09:36:47 -070013502 if (kvm_has_tsc_control)
13503 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013504
Jim Mattson8d860bb2018-05-09 16:56:05 -040013505 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13506 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13507 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013508 } else if (!nested_cpu_has_ept(vmcs12) &&
13509 nested_cpu_has2(vmcs12,
13510 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013511 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013512 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013513
13514 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13515 vmx->host_rsp = 0;
13516
13517 /* Unpin physical memory we referred to in vmcs02 */
13518 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013519 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013520 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013521 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013522 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013523 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013524 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013525 }
Wincy Van705699a2015-02-03 23:58:17 +080013526 if (vmx->nested.pi_desc_page) {
13527 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013528 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013529 vmx->nested.pi_desc_page = NULL;
13530 vmx->nested.pi_desc = NULL;
13531 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013532
13533 /*
Tang Chen38b99172014-09-24 15:57:54 +080013534 * We are now running in L2, mmu_notifier will force to reload the
13535 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13536 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013537 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013538
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013539 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013540 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013541
13542 /* in case we halted in L2 */
13543 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013544
13545 if (likely(!vmx->fail)) {
13546 /*
13547 * TODO: SDM says that with acknowledge interrupt on
13548 * exit, bit 31 of the VM-exit interrupt information
13549 * (valid interrupt) is always set to 1 on
13550 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13551 * need kvm_cpu_has_interrupt(). See the commit
13552 * message for details.
13553 */
13554 if (nested_exit_intr_ack_set(vcpu) &&
13555 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13556 kvm_cpu_has_interrupt(vcpu)) {
13557 int irq = kvm_cpu_get_interrupt(vcpu);
13558 WARN_ON(irq < 0);
13559 vmcs12->vm_exit_intr_info = irq |
13560 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13561 }
13562
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013563 if (exit_reason != -1)
13564 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13565 vmcs12->exit_qualification,
13566 vmcs12->idt_vectoring_info_field,
13567 vmcs12->vm_exit_intr_info,
13568 vmcs12->vm_exit_intr_error_code,
13569 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013570
13571 load_vmcs12_host_state(vcpu, vmcs12);
13572
13573 return;
13574 }
13575
13576 /*
13577 * After an early L2 VM-entry failure, we're now back
13578 * in L1 which thinks it just finished a VMLAUNCH or
13579 * VMRESUME instruction, so we need to set the failure
13580 * flag and the VM-instruction error field of the VMCS
13581 * accordingly.
13582 */
13583 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013584
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013585 /*
13586 * Restore L1's host state to KVM's software model. We're here
13587 * because a consistency check was caught by hardware, which
13588 * means some amount of guest state has been propagated to KVM's
13589 * model and needs to be unwound to the host's state.
13590 */
13591 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013592
Jim Mattson4f350c62017-09-14 16:31:44 -070013593 /*
13594 * The emulated instruction was already skipped in
13595 * nested_vmx_run, but the updated RIP was never
13596 * written back to the vmcs01.
13597 */
13598 skip_emulated_instruction(vcpu);
13599 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013600}
13601
Nadav Har'El7c177932011-05-25 23:12:04 +030013602/*
Jan Kiszka42124922014-01-04 18:47:19 +010013603 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13604 */
13605static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13606{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013607 if (is_guest_mode(vcpu)) {
13608 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013609 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013610 }
Jan Kiszka42124922014-01-04 18:47:19 +010013611 free_nested(to_vmx(vcpu));
13612}
13613
13614/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013615 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13616 * 23.7 "VM-entry failures during or after loading guest state" (this also
13617 * lists the acceptable exit-reason and exit-qualification parameters).
13618 * It should only be called before L2 actually succeeded to run, and when
13619 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13620 */
13621static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13622 struct vmcs12 *vmcs12,
13623 u32 reason, unsigned long qualification)
13624{
13625 load_vmcs12_host_state(vcpu, vmcs12);
13626 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13627 vmcs12->exit_qualification = qualification;
13628 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013629 if (enable_shadow_vmcs)
13630 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013631}
13632
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013633static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13634 struct x86_instruction_info *info,
13635 enum x86_intercept_stage stage)
13636{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013637 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13638 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13639
13640 /*
13641 * RDPID causes #UD if disabled through secondary execution controls.
13642 * Because it is marked as EmulateOnUD, we need to intercept it here.
13643 */
13644 if (info->intercept == x86_intercept_rdtscp &&
13645 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13646 ctxt->exception.vector = UD_VECTOR;
13647 ctxt->exception.error_code_valid = false;
13648 return X86EMUL_PROPAGATE_FAULT;
13649 }
13650
13651 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013652 return X86EMUL_CONTINUE;
13653}
13654
Yunhong Jiang64672c92016-06-13 14:19:59 -070013655#ifdef CONFIG_X86_64
13656/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13657static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13658 u64 divisor, u64 *result)
13659{
13660 u64 low = a << shift, high = a >> (64 - shift);
13661
13662 /* To avoid the overflow on divq */
13663 if (high >= divisor)
13664 return 1;
13665
13666 /* Low hold the result, high hold rem which is discarded */
13667 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13668 "rm" (divisor), "0" (low), "1" (high));
13669 *result = low;
13670
13671 return 0;
13672}
13673
13674static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13675{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013676 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013677 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013678
13679 if (kvm_mwait_in_guest(vcpu->kvm))
13680 return -EOPNOTSUPP;
13681
13682 vmx = to_vmx(vcpu);
13683 tscl = rdtsc();
13684 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13685 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013686 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13687
13688 if (delta_tsc > lapic_timer_advance_cycles)
13689 delta_tsc -= lapic_timer_advance_cycles;
13690 else
13691 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013692
13693 /* Convert to host delta tsc if tsc scaling is enabled */
13694 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13695 u64_shl_div_u64(delta_tsc,
13696 kvm_tsc_scaling_ratio_frac_bits,
13697 vcpu->arch.tsc_scaling_ratio,
13698 &delta_tsc))
13699 return -ERANGE;
13700
13701 /*
13702 * If the delta tsc can't fit in the 32 bit after the multi shift,
13703 * we can't use the preemption timer.
13704 * It's possible that it fits on later vmentries, but checking
13705 * on every vmentry is costly so we just use an hrtimer.
13706 */
13707 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13708 return -ERANGE;
13709
13710 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013711 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013712}
13713
13714static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13715{
Sean Christophersonf459a702018-08-27 15:21:11 -070013716 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013717}
13718#endif
13719
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013720static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013721{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013722 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013723 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013724}
13725
Kai Huang843e4332015-01-28 10:54:28 +080013726static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13727 struct kvm_memory_slot *slot)
13728{
13729 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13730 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13731}
13732
13733static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13734 struct kvm_memory_slot *slot)
13735{
13736 kvm_mmu_slot_set_dirty(kvm, slot);
13737}
13738
13739static void vmx_flush_log_dirty(struct kvm *kvm)
13740{
13741 kvm_flush_pml_buffers(kvm);
13742}
13743
Bandan Dasc5f983f2017-05-05 15:25:14 -040013744static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13745{
13746 struct vmcs12 *vmcs12;
13747 struct vcpu_vmx *vmx = to_vmx(vcpu);
13748 gpa_t gpa;
13749 struct page *page = NULL;
13750 u64 *pml_address;
13751
13752 if (is_guest_mode(vcpu)) {
13753 WARN_ON_ONCE(vmx->nested.pml_full);
13754
13755 /*
13756 * Check if PML is enabled for the nested guest.
13757 * Whether eptp bit 6 is set is already checked
13758 * as part of A/D emulation.
13759 */
13760 vmcs12 = get_vmcs12(vcpu);
13761 if (!nested_cpu_has_pml(vmcs12))
13762 return 0;
13763
Dan Carpenter47698862017-05-10 22:43:17 +030013764 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013765 vmx->nested.pml_full = true;
13766 return 1;
13767 }
13768
13769 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13770
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013771 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13772 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013773 return 0;
13774
13775 pml_address = kmap(page);
13776 pml_address[vmcs12->guest_pml_index--] = gpa;
13777 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013778 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013779 }
13780
13781 return 0;
13782}
13783
Kai Huang843e4332015-01-28 10:54:28 +080013784static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13785 struct kvm_memory_slot *memslot,
13786 gfn_t offset, unsigned long mask)
13787{
13788 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13789}
13790
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013791static void __pi_post_block(struct kvm_vcpu *vcpu)
13792{
13793 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13794 struct pi_desc old, new;
13795 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013796
13797 do {
13798 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013799 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13800 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013801
13802 dest = cpu_physical_id(vcpu->cpu);
13803
13804 if (x2apic_enabled())
13805 new.ndst = dest;
13806 else
13807 new.ndst = (dest << 8) & 0xFF00;
13808
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013809 /* set 'NV' to 'notification vector' */
13810 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013811 } while (cmpxchg64(&pi_desc->control, old.control,
13812 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013813
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013814 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13815 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013816 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013817 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013818 vcpu->pre_pcpu = -1;
13819 }
13820}
13821
Feng Wuefc64402015-09-18 22:29:51 +080013822/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013823 * This routine does the following things for vCPU which is going
13824 * to be blocked if VT-d PI is enabled.
13825 * - Store the vCPU to the wakeup list, so when interrupts happen
13826 * we can find the right vCPU to wake up.
13827 * - Change the Posted-interrupt descriptor as below:
13828 * 'NDST' <-- vcpu->pre_pcpu
13829 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13830 * - If 'ON' is set during this process, which means at least one
13831 * interrupt is posted for this vCPU, we cannot block it, in
13832 * this case, return 1, otherwise, return 0.
13833 *
13834 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013835static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013836{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013837 unsigned int dest;
13838 struct pi_desc old, new;
13839 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13840
13841 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013842 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13843 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013844 return 0;
13845
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013846 WARN_ON(irqs_disabled());
13847 local_irq_disable();
13848 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13849 vcpu->pre_pcpu = vcpu->cpu;
13850 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13851 list_add_tail(&vcpu->blocked_vcpu_list,
13852 &per_cpu(blocked_vcpu_on_cpu,
13853 vcpu->pre_pcpu));
13854 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13855 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013856
13857 do {
13858 old.control = new.control = pi_desc->control;
13859
Feng Wubf9f6ac2015-09-18 22:29:55 +080013860 WARN((pi_desc->sn == 1),
13861 "Warning: SN field of posted-interrupts "
13862 "is set before blocking\n");
13863
13864 /*
13865 * Since vCPU can be preempted during this process,
13866 * vcpu->cpu could be different with pre_pcpu, we
13867 * need to set pre_pcpu as the destination of wakeup
13868 * notification event, then we can find the right vCPU
13869 * to wakeup in wakeup handler if interrupts happen
13870 * when the vCPU is in blocked state.
13871 */
13872 dest = cpu_physical_id(vcpu->pre_pcpu);
13873
13874 if (x2apic_enabled())
13875 new.ndst = dest;
13876 else
13877 new.ndst = (dest << 8) & 0xFF00;
13878
13879 /* set 'NV' to 'wakeup vector' */
13880 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013881 } while (cmpxchg64(&pi_desc->control, old.control,
13882 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013883
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013884 /* We should not block the vCPU if an interrupt is posted for it. */
13885 if (pi_test_on(pi_desc) == 1)
13886 __pi_post_block(vcpu);
13887
13888 local_irq_enable();
13889 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013890}
13891
Yunhong Jiangbc225122016-06-13 14:19:58 -070013892static int vmx_pre_block(struct kvm_vcpu *vcpu)
13893{
13894 if (pi_pre_block(vcpu))
13895 return 1;
13896
Yunhong Jiang64672c92016-06-13 14:19:59 -070013897 if (kvm_lapic_hv_timer_in_use(vcpu))
13898 kvm_lapic_switch_to_sw_timer(vcpu);
13899
Yunhong Jiangbc225122016-06-13 14:19:58 -070013900 return 0;
13901}
13902
13903static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013904{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013905 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013906 return;
13907
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013908 WARN_ON(irqs_disabled());
13909 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013910 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013911 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013912}
13913
Yunhong Jiangbc225122016-06-13 14:19:58 -070013914static void vmx_post_block(struct kvm_vcpu *vcpu)
13915{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013916 if (kvm_x86_ops->set_hv_timer)
13917 kvm_lapic_switch_to_hv_timer(vcpu);
13918
Yunhong Jiangbc225122016-06-13 14:19:58 -070013919 pi_post_block(vcpu);
13920}
13921
Feng Wubf9f6ac2015-09-18 22:29:55 +080013922/*
Feng Wuefc64402015-09-18 22:29:51 +080013923 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13924 *
13925 * @kvm: kvm
13926 * @host_irq: host irq of the interrupt
13927 * @guest_irq: gsi of the interrupt
13928 * @set: set or unset PI
13929 * returns 0 on success, < 0 on failure
13930 */
13931static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13932 uint32_t guest_irq, bool set)
13933{
13934 struct kvm_kernel_irq_routing_entry *e;
13935 struct kvm_irq_routing_table *irq_rt;
13936 struct kvm_lapic_irq irq;
13937 struct kvm_vcpu *vcpu;
13938 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013939 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013940
13941 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013942 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13943 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013944 return 0;
13945
13946 idx = srcu_read_lock(&kvm->irq_srcu);
13947 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013948 if (guest_irq >= irq_rt->nr_rt_entries ||
13949 hlist_empty(&irq_rt->map[guest_irq])) {
13950 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13951 guest_irq, irq_rt->nr_rt_entries);
13952 goto out;
13953 }
Feng Wuefc64402015-09-18 22:29:51 +080013954
13955 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13956 if (e->type != KVM_IRQ_ROUTING_MSI)
13957 continue;
13958 /*
13959 * VT-d PI cannot support posting multicast/broadcast
13960 * interrupts to a vCPU, we still use interrupt remapping
13961 * for these kind of interrupts.
13962 *
13963 * For lowest-priority interrupts, we only support
13964 * those with single CPU as the destination, e.g. user
13965 * configures the interrupts via /proc/irq or uses
13966 * irqbalance to make the interrupts single-CPU.
13967 *
13968 * We will support full lowest-priority interrupt later.
13969 */
13970
Radim Krčmář371313132016-07-12 22:09:27 +020013971 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013972 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13973 /*
13974 * Make sure the IRTE is in remapped mode if
13975 * we don't handle it in posted mode.
13976 */
13977 ret = irq_set_vcpu_affinity(host_irq, NULL);
13978 if (ret < 0) {
13979 printk(KERN_INFO
13980 "failed to back to remapped mode, irq: %u\n",
13981 host_irq);
13982 goto out;
13983 }
13984
Feng Wuefc64402015-09-18 22:29:51 +080013985 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013986 }
Feng Wuefc64402015-09-18 22:29:51 +080013987
13988 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13989 vcpu_info.vector = irq.vector;
13990
hu huajun2698d822018-04-11 15:16:40 +080013991 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013992 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13993
13994 if (set)
13995 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013996 else
Feng Wuefc64402015-09-18 22:29:51 +080013997 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013998
13999 if (ret < 0) {
14000 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14001 __func__);
14002 goto out;
14003 }
14004 }
14005
14006 ret = 0;
14007out:
14008 srcu_read_unlock(&kvm->irq_srcu, idx);
14009 return ret;
14010}
14011
Ashok Rajc45dcc72016-06-22 14:59:56 +080014012static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14013{
14014 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14015 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14016 FEATURE_CONTROL_LMCE;
14017 else
14018 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14019 ~FEATURE_CONTROL_LMCE;
14020}
14021
Ladi Prosek72d7b372017-10-11 16:54:41 +020014022static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14023{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014024 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14025 if (to_vmx(vcpu)->nested.nested_run_pending)
14026 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014027 return 1;
14028}
14029
Ladi Prosek0234bf82017-10-11 16:54:40 +020014030static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14031{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014032 struct vcpu_vmx *vmx = to_vmx(vcpu);
14033
14034 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14035 if (vmx->nested.smm.guest_mode)
14036 nested_vmx_vmexit(vcpu, -1, 0, 0);
14037
14038 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14039 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014040 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014041 return 0;
14042}
14043
14044static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14045{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014046 struct vcpu_vmx *vmx = to_vmx(vcpu);
14047 int ret;
14048
14049 if (vmx->nested.smm.vmxon) {
14050 vmx->nested.vmxon = true;
14051 vmx->nested.smm.vmxon = false;
14052 }
14053
14054 if (vmx->nested.smm.guest_mode) {
14055 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014056 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014057 vcpu->arch.hflags |= HF_SMM_MASK;
14058 if (ret)
14059 return ret;
14060
14061 vmx->nested.smm.guest_mode = false;
14062 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014063 return 0;
14064}
14065
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014066static int enable_smi_window(struct kvm_vcpu *vcpu)
14067{
14068 return 0;
14069}
14070
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014071static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14072 struct kvm_nested_state __user *user_kvm_nested_state,
14073 u32 user_data_size)
14074{
14075 struct vcpu_vmx *vmx;
14076 struct vmcs12 *vmcs12;
14077 struct kvm_nested_state kvm_state = {
14078 .flags = 0,
14079 .format = 0,
14080 .size = sizeof(kvm_state),
14081 .vmx.vmxon_pa = -1ull,
14082 .vmx.vmcs_pa = -1ull,
14083 };
14084
14085 if (!vcpu)
14086 return kvm_state.size + 2 * VMCS12_SIZE;
14087
14088 vmx = to_vmx(vcpu);
14089 vmcs12 = get_vmcs12(vcpu);
14090 if (nested_vmx_allowed(vcpu) &&
14091 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14092 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14093 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14094
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014095 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014096 kvm_state.size += VMCS12_SIZE;
14097
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014098 if (is_guest_mode(vcpu) &&
14099 nested_cpu_has_shadow_vmcs(vmcs12) &&
14100 vmcs12->vmcs_link_pointer != -1ull)
14101 kvm_state.size += VMCS12_SIZE;
14102 }
14103
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014104 if (vmx->nested.smm.vmxon)
14105 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14106
14107 if (vmx->nested.smm.guest_mode)
14108 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14109
14110 if (is_guest_mode(vcpu)) {
14111 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14112
14113 if (vmx->nested.nested_run_pending)
14114 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14115 }
14116 }
14117
14118 if (user_data_size < kvm_state.size)
14119 goto out;
14120
14121 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14122 return -EFAULT;
14123
14124 if (vmx->nested.current_vmptr == -1ull)
14125 goto out;
14126
14127 /*
14128 * When running L2, the authoritative vmcs12 state is in the
14129 * vmcs02. When running L1, the authoritative vmcs12 state is
14130 * in the shadow vmcs linked to vmcs01, unless
14131 * sync_shadow_vmcs is set, in which case, the authoritative
14132 * vmcs12 state is in the vmcs12 already.
14133 */
14134 if (is_guest_mode(vcpu))
14135 sync_vmcs12(vcpu, vmcs12);
14136 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14137 copy_shadow_to_vmcs12(vmx);
14138
14139 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14140 return -EFAULT;
14141
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014142 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14143 vmcs12->vmcs_link_pointer != -1ull) {
14144 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14145 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14146 return -EFAULT;
14147 }
14148
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014149out:
14150 return kvm_state.size;
14151}
14152
14153static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14154 struct kvm_nested_state __user *user_kvm_nested_state,
14155 struct kvm_nested_state *kvm_state)
14156{
14157 struct vcpu_vmx *vmx = to_vmx(vcpu);
14158 struct vmcs12 *vmcs12;
14159 u32 exit_qual;
14160 int ret;
14161
14162 if (kvm_state->format != 0)
14163 return -EINVAL;
14164
14165 if (!nested_vmx_allowed(vcpu))
14166 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14167
14168 if (kvm_state->vmx.vmxon_pa == -1ull) {
14169 if (kvm_state->vmx.smm.flags)
14170 return -EINVAL;
14171
14172 if (kvm_state->vmx.vmcs_pa != -1ull)
14173 return -EINVAL;
14174
14175 vmx_leave_nested(vcpu);
14176 return 0;
14177 }
14178
14179 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14180 return -EINVAL;
14181
14182 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14183 return -EINVAL;
14184
14185 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14186 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14187 return -EINVAL;
14188
14189 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14190 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14191 return -EINVAL;
14192
14193 if (kvm_state->vmx.smm.flags &
14194 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14195 return -EINVAL;
14196
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014197 /*
14198 * SMM temporarily disables VMX, so we cannot be in guest mode,
14199 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14200 * must be zero.
14201 */
14202 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14203 return -EINVAL;
14204
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014205 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14206 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14207 return -EINVAL;
14208
14209 vmx_leave_nested(vcpu);
14210 if (kvm_state->vmx.vmxon_pa == -1ull)
14211 return 0;
14212
14213 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14214 ret = enter_vmx_operation(vcpu);
14215 if (ret)
14216 return ret;
14217
14218 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14219
14220 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14221 vmx->nested.smm.vmxon = true;
14222 vmx->nested.vmxon = false;
14223
14224 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14225 vmx->nested.smm.guest_mode = true;
14226 }
14227
14228 vmcs12 = get_vmcs12(vcpu);
14229 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14230 return -EFAULT;
14231
Liran Alon392b2f22018-06-23 02:35:01 +030014232 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014233 return -EINVAL;
14234
14235 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14236 return 0;
14237
14238 vmx->nested.nested_run_pending =
14239 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14240
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014241 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14242 vmcs12->vmcs_link_pointer != -1ull) {
14243 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14244 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14245 return -EINVAL;
14246
14247 if (copy_from_user(shadow_vmcs12,
14248 user_kvm_nested_state->data + VMCS12_SIZE,
14249 sizeof(*vmcs12)))
14250 return -EFAULT;
14251
14252 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14253 !shadow_vmcs12->hdr.shadow_vmcs)
14254 return -EINVAL;
14255 }
14256
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014257 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14258 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14259 return -EINVAL;
14260
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014261 vmx->nested.dirty_vmcs12 = true;
14262 ret = enter_vmx_non_root_mode(vcpu, NULL);
14263 if (ret)
14264 return -EINVAL;
14265
14266 return 0;
14267}
14268
Kees Cook404f6aa2016-08-08 16:29:06 -070014269static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014270 .cpu_has_kvm_support = cpu_has_kvm_support,
14271 .disabled_by_bios = vmx_disabled_by_bios,
14272 .hardware_setup = hardware_setup,
14273 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014274 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014275 .hardware_enable = hardware_enable,
14276 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014277 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014278 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014279
Wanpeng Lib31c1142018-03-12 04:53:04 -070014280 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014281 .vm_alloc = vmx_vm_alloc,
14282 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014283
Avi Kivity6aa8b732006-12-10 02:21:36 -080014284 .vcpu_create = vmx_create_vcpu,
14285 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014286 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014287
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014288 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014289 .vcpu_load = vmx_vcpu_load,
14290 .vcpu_put = vmx_vcpu_put,
14291
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014292 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014293 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014294 .get_msr = vmx_get_msr,
14295 .set_msr = vmx_set_msr,
14296 .get_segment_base = vmx_get_segment_base,
14297 .get_segment = vmx_get_segment,
14298 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014299 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014300 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014301 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014302 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014303 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014304 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014305 .set_cr3 = vmx_set_cr3,
14306 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014307 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014308 .get_idt = vmx_get_idt,
14309 .set_idt = vmx_set_idt,
14310 .get_gdt = vmx_get_gdt,
14311 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014312 .get_dr6 = vmx_get_dr6,
14313 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014314 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014315 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014316 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014317 .get_rflags = vmx_get_rflags,
14318 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014319
Avi Kivity6aa8b732006-12-10 02:21:36 -080014320 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014321 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014322
Avi Kivity6aa8b732006-12-10 02:21:36 -080014323 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014324 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014325 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014326 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14327 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014328 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014329 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014330 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014331 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014332 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014333 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014334 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014335 .get_nmi_mask = vmx_get_nmi_mask,
14336 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014337 .enable_nmi_window = enable_nmi_window,
14338 .enable_irq_window = enable_irq_window,
14339 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014340 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014341 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014342 .get_enable_apicv = vmx_get_enable_apicv,
14343 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014344 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014345 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014346 .hwapic_irr_update = vmx_hwapic_irr_update,
14347 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014348 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014349 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14350 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014351
Izik Eiduscbc94022007-10-25 00:29:55 +020014352 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014353 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014354 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014355 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014356
Avi Kivity586f9602010-11-18 13:09:54 +020014357 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014358
Sheng Yang17cc3932010-01-05 19:02:27 +080014359 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014360
14361 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014362
14363 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014364 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014365
14366 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014367
14368 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014369
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014370 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014371 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014372
14373 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014374
14375 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014376 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014377 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014378 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014379 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014380
14381 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014382 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014383
14384 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014385
14386 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14387 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14388 .flush_log_dirty = vmx_flush_log_dirty,
14389 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014390 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014391
Feng Wubf9f6ac2015-09-18 22:29:55 +080014392 .pre_block = vmx_pre_block,
14393 .post_block = vmx_post_block,
14394
Wei Huang25462f72015-06-19 15:45:05 +020014395 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014396
14397 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014398
14399#ifdef CONFIG_X86_64
14400 .set_hv_timer = vmx_set_hv_timer,
14401 .cancel_hv_timer = vmx_cancel_hv_timer,
14402#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014403
14404 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014405
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014406 .get_nested_state = vmx_get_nested_state,
14407 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014408 .get_vmcs12_pages = nested_get_vmcs12_pages,
14409
Ladi Prosek72d7b372017-10-11 16:54:41 +020014410 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014411 .pre_enter_smm = vmx_pre_enter_smm,
14412 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014413 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014414};
14415
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014416static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014417{
14418 if (vmx_l1d_flush_pages) {
14419 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14420 vmx_l1d_flush_pages = NULL;
14421 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014422 /* Restore state so sysfs ignores VMX */
14423 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014424}
14425
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014426static void vmx_exit(void)
14427{
14428#ifdef CONFIG_KEXEC_CORE
14429 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14430 synchronize_rcu();
14431#endif
14432
14433 kvm_exit();
14434
14435#if IS_ENABLED(CONFIG_HYPERV)
14436 if (static_branch_unlikely(&enable_evmcs)) {
14437 int cpu;
14438 struct hv_vp_assist_page *vp_ap;
14439 /*
14440 * Reset everything to support using non-enlightened VMCS
14441 * access later (e.g. when we reload the module with
14442 * enlightened_vmcs=0)
14443 */
14444 for_each_online_cpu(cpu) {
14445 vp_ap = hv_get_vp_assist_page(cpu);
14446
14447 if (!vp_ap)
14448 continue;
14449
14450 vp_ap->current_nested_vmcs = 0;
14451 vp_ap->enlighten_vmentry = 0;
14452 }
14453
14454 static_branch_disable(&enable_evmcs);
14455 }
14456#endif
14457 vmx_cleanup_l1d_flush();
14458}
14459module_exit(vmx_exit);
14460
Avi Kivity6aa8b732006-12-10 02:21:36 -080014461static int __init vmx_init(void)
14462{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014463 int r;
14464
14465#if IS_ENABLED(CONFIG_HYPERV)
14466 /*
14467 * Enlightened VMCS usage should be recommended and the host needs
14468 * to support eVMCS v1 or above. We can also disable eVMCS support
14469 * with module parameter.
14470 */
14471 if (enlightened_vmcs &&
14472 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14473 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14474 KVM_EVMCS_VERSION) {
14475 int cpu;
14476
14477 /* Check that we have assist pages on all online CPUs */
14478 for_each_online_cpu(cpu) {
14479 if (!hv_get_vp_assist_page(cpu)) {
14480 enlightened_vmcs = false;
14481 break;
14482 }
14483 }
14484
14485 if (enlightened_vmcs) {
14486 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14487 static_branch_enable(&enable_evmcs);
14488 }
14489 } else {
14490 enlightened_vmcs = false;
14491 }
14492#endif
14493
14494 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014495 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014496 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014497 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014498
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014499 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014500 * Must be called after kvm_init() so enable_ept is properly set
14501 * up. Hand the parameter mitigation value in which was stored in
14502 * the pre module init parser. If no parameter was given, it will
14503 * contain 'auto' which will be turned into the default 'cond'
14504 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014505 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014506 if (boot_cpu_has(X86_BUG_L1TF)) {
14507 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14508 if (r) {
14509 vmx_exit();
14510 return r;
14511 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014512 }
14513
Dave Young2965faa2015-09-09 15:38:55 -070014514#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014515 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14516 crash_vmclear_local_loaded_vmcss);
14517#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014518 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014519
He, Qingfdef3ad2007-04-30 09:45:24 +030014520 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014521}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014522module_init(vmx_init);