blob: a1dbc17c7a0318083951741bf0a5ca92f3711b6e [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 Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040074static bool __read_mostly nosmt;
75module_param(nosmt, bool, S_IRUGO);
76
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010080static bool __read_mostly enable_vnmi = 1;
81module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020087module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080088
Rusty Russell476bc002012-01-13 09:32:18 +103089static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070090module_param_named(unrestricted_guest,
91 enable_unrestricted_guest, bool, S_IRUGO);
92
Xudong Hao83c3a332012-05-28 19:33:35 +080093static bool __read_mostly enable_ept_ad_bits = 1;
94module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95
Avi Kivitya27685c2012-06-12 20:30:18 +030096static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020097module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300100module_param(fasteoi, bool, S_IRUGO);
101
Yang Zhang5a717852013-04-11 19:25:16 +0800102static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800103module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800104
Abel Gordonabc4fc52013-04-18 14:35:25 +0300105static bool __read_mostly enable_shadow_vmcs = 1;
106module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300107/*
108 * If nested=1, nested virtualization is supported, i.e., guests may use
109 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
110 * use VMX instructions.
111 */
Rusty Russell476bc002012-01-13 09:32:18 +1030112static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300113module_param(nested, bool, S_IRUGO);
114
Wanpeng Li20300092014-12-02 19:14:59 +0800115static u64 __read_mostly host_xss;
116
Kai Huang843e4332015-01-28 10:54:28 +0800117static bool __read_mostly enable_pml = 1;
118module_param_named(pml, enable_pml, bool, S_IRUGO);
119
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100120#define MSR_TYPE_R 1
121#define MSR_TYPE_W 2
122#define MSR_TYPE_RW 3
123
124#define MSR_BITMAP_MODE_X2APIC 1
125#define MSR_BITMAP_MODE_X2APIC_APICV 2
126#define MSR_BITMAP_MODE_LM 4
127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Gleb Natapov50378782013-02-04 16:00:28 +0200137#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf41245002014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400182module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
184/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400185static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187
188/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200191
Avi Kivity83287ea422012-09-16 15:10:57 +0300192extern const ulong vmx_return;
193
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
195
196/* These MUST be in sync with vmentry_l1d_param order. */
197enum vmx_l1d_flush_state {
198 VMENTER_L1D_FLUSH_NEVER,
199 VMENTER_L1D_FLUSH_COND,
200 VMENTER_L1D_FLUSH_ALWAYS,
201};
202
203static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush = VMENTER_L1D_FLUSH_COND;
204
205static const struct {
206 const char *option;
207 enum vmx_l1d_flush_state cmd;
208} vmentry_l1d_param[] = {
209 {"never", VMENTER_L1D_FLUSH_NEVER},
210 {"cond", VMENTER_L1D_FLUSH_COND},
211 {"always", VMENTER_L1D_FLUSH_ALWAYS},
212};
213
214static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
215{
216 unsigned int i;
217
218 if (!s)
219 return -EINVAL;
220
221 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
222 if (!strcmp(s, vmentry_l1d_param[i].option)) {
223 vmentry_l1d_flush = vmentry_l1d_param[i].cmd;
224 return 0;
225 }
226 }
227
228 return -EINVAL;
229}
230
231static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
232{
233 return sprintf(s, "%s\n", vmentry_l1d_param[vmentry_l1d_flush].option);
234}
235
236static const struct kernel_param_ops vmentry_l1d_flush_ops = {
237 .set = vmentry_l1d_flush_set,
238 .get = vmentry_l1d_flush_get,
239};
240module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, &vmentry_l1d_flush, S_IRUGO);
241
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700242struct kvm_vmx {
243 struct kvm kvm;
244
245 unsigned int tss_addr;
246 bool ept_identity_pagetable_done;
247 gpa_t ept_identity_map_addr;
248};
249
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200250#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300251
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400252struct vmcs {
253 u32 revision_id;
254 u32 abort;
255 char data[0];
256};
257
Nadav Har'Eld462b812011-05-24 15:26:10 +0300258/*
259 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
260 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
261 * loaded on this CPU (so we can clear them if the CPU goes down).
262 */
263struct loaded_vmcs {
264 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700265 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300266 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200267 bool launched;
268 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200269 unsigned long vmcs_host_cr3; /* May not match real cr3 */
270 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100271 /* Support for vnmi-less CPUs */
272 int soft_vnmi_blocked;
273 ktime_t entry_time;
274 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100275 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300276 struct list_head loaded_vmcss_on_cpu_link;
277};
278
Avi Kivity26bb0982009-09-07 11:14:12 +0300279struct shared_msr_entry {
280 unsigned index;
281 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200282 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300283};
284
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300285/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300286 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
287 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
288 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
289 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
290 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
291 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600292 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300293 * underlying hardware which will be used to run L2.
294 * This structure is packed to ensure that its layout is identical across
295 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700296 *
297 * IMPORTANT: Changing the layout of existing fields in this structure
298 * will break save/restore compatibility with older kvm releases. When
299 * adding new fields, either use space in the reserved padding* arrays
300 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300301 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300302typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300303struct __packed vmcs12 {
304 /* According to the Intel spec, a VMCS region must start with the
305 * following two fields. Then follow implementation-specific data.
306 */
307 u32 revision_id;
308 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300309
Nadav Har'El27d6c862011-05-25 23:06:59 +0300310 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
311 u32 padding[7]; /* room for future expansion */
312
Nadav Har'El22bd0352011-05-25 23:05:57 +0300313 u64 io_bitmap_a;
314 u64 io_bitmap_b;
315 u64 msr_bitmap;
316 u64 vm_exit_msr_store_addr;
317 u64 vm_exit_msr_load_addr;
318 u64 vm_entry_msr_load_addr;
319 u64 tsc_offset;
320 u64 virtual_apic_page_addr;
321 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800322 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300323 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800324 u64 eoi_exit_bitmap0;
325 u64 eoi_exit_bitmap1;
326 u64 eoi_exit_bitmap2;
327 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800328 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300329 u64 guest_physical_address;
330 u64 vmcs_link_pointer;
331 u64 guest_ia32_debugctl;
332 u64 guest_ia32_pat;
333 u64 guest_ia32_efer;
334 u64 guest_ia32_perf_global_ctrl;
335 u64 guest_pdptr0;
336 u64 guest_pdptr1;
337 u64 guest_pdptr2;
338 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100339 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300340 u64 host_ia32_pat;
341 u64 host_ia32_efer;
342 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700343 u64 vmread_bitmap;
344 u64 vmwrite_bitmap;
345 u64 vm_function_control;
346 u64 eptp_list_address;
347 u64 pml_address;
348 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300349 /*
350 * To allow migration of L1 (complete with its L2 guests) between
351 * machines of different natural widths (32 or 64 bit), we cannot have
352 * unsigned long fields with no explict size. We use u64 (aliased
353 * natural_width) instead. Luckily, x86 is little-endian.
354 */
355 natural_width cr0_guest_host_mask;
356 natural_width cr4_guest_host_mask;
357 natural_width cr0_read_shadow;
358 natural_width cr4_read_shadow;
359 natural_width cr3_target_value0;
360 natural_width cr3_target_value1;
361 natural_width cr3_target_value2;
362 natural_width cr3_target_value3;
363 natural_width exit_qualification;
364 natural_width guest_linear_address;
365 natural_width guest_cr0;
366 natural_width guest_cr3;
367 natural_width guest_cr4;
368 natural_width guest_es_base;
369 natural_width guest_cs_base;
370 natural_width guest_ss_base;
371 natural_width guest_ds_base;
372 natural_width guest_fs_base;
373 natural_width guest_gs_base;
374 natural_width guest_ldtr_base;
375 natural_width guest_tr_base;
376 natural_width guest_gdtr_base;
377 natural_width guest_idtr_base;
378 natural_width guest_dr7;
379 natural_width guest_rsp;
380 natural_width guest_rip;
381 natural_width guest_rflags;
382 natural_width guest_pending_dbg_exceptions;
383 natural_width guest_sysenter_esp;
384 natural_width guest_sysenter_eip;
385 natural_width host_cr0;
386 natural_width host_cr3;
387 natural_width host_cr4;
388 natural_width host_fs_base;
389 natural_width host_gs_base;
390 natural_width host_tr_base;
391 natural_width host_gdtr_base;
392 natural_width host_idtr_base;
393 natural_width host_ia32_sysenter_esp;
394 natural_width host_ia32_sysenter_eip;
395 natural_width host_rsp;
396 natural_width host_rip;
397 natural_width paddingl[8]; /* room for future expansion */
398 u32 pin_based_vm_exec_control;
399 u32 cpu_based_vm_exec_control;
400 u32 exception_bitmap;
401 u32 page_fault_error_code_mask;
402 u32 page_fault_error_code_match;
403 u32 cr3_target_count;
404 u32 vm_exit_controls;
405 u32 vm_exit_msr_store_count;
406 u32 vm_exit_msr_load_count;
407 u32 vm_entry_controls;
408 u32 vm_entry_msr_load_count;
409 u32 vm_entry_intr_info_field;
410 u32 vm_entry_exception_error_code;
411 u32 vm_entry_instruction_len;
412 u32 tpr_threshold;
413 u32 secondary_vm_exec_control;
414 u32 vm_instruction_error;
415 u32 vm_exit_reason;
416 u32 vm_exit_intr_info;
417 u32 vm_exit_intr_error_code;
418 u32 idt_vectoring_info_field;
419 u32 idt_vectoring_error_code;
420 u32 vm_exit_instruction_len;
421 u32 vmx_instruction_info;
422 u32 guest_es_limit;
423 u32 guest_cs_limit;
424 u32 guest_ss_limit;
425 u32 guest_ds_limit;
426 u32 guest_fs_limit;
427 u32 guest_gs_limit;
428 u32 guest_ldtr_limit;
429 u32 guest_tr_limit;
430 u32 guest_gdtr_limit;
431 u32 guest_idtr_limit;
432 u32 guest_es_ar_bytes;
433 u32 guest_cs_ar_bytes;
434 u32 guest_ss_ar_bytes;
435 u32 guest_ds_ar_bytes;
436 u32 guest_fs_ar_bytes;
437 u32 guest_gs_ar_bytes;
438 u32 guest_ldtr_ar_bytes;
439 u32 guest_tr_ar_bytes;
440 u32 guest_interruptibility_info;
441 u32 guest_activity_state;
442 u32 guest_sysenter_cs;
443 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100444 u32 vmx_preemption_timer_value;
445 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300446 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800447 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300448 u16 guest_es_selector;
449 u16 guest_cs_selector;
450 u16 guest_ss_selector;
451 u16 guest_ds_selector;
452 u16 guest_fs_selector;
453 u16 guest_gs_selector;
454 u16 guest_ldtr_selector;
455 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800456 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300457 u16 host_es_selector;
458 u16 host_cs_selector;
459 u16 host_ss_selector;
460 u16 host_ds_selector;
461 u16 host_fs_selector;
462 u16 host_gs_selector;
463 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700464 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300465};
466
467/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700468 * For save/restore compatibility, the vmcs12 field offsets must not change.
469 */
470#define CHECK_OFFSET(field, loc) \
471 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
472 "Offset of " #field " in struct vmcs12 has changed.")
473
474static inline void vmx_check_vmcs12_offsets(void) {
475 CHECK_OFFSET(revision_id, 0);
476 CHECK_OFFSET(abort, 4);
477 CHECK_OFFSET(launch_state, 8);
478 CHECK_OFFSET(io_bitmap_a, 40);
479 CHECK_OFFSET(io_bitmap_b, 48);
480 CHECK_OFFSET(msr_bitmap, 56);
481 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
482 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
483 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
484 CHECK_OFFSET(tsc_offset, 88);
485 CHECK_OFFSET(virtual_apic_page_addr, 96);
486 CHECK_OFFSET(apic_access_addr, 104);
487 CHECK_OFFSET(posted_intr_desc_addr, 112);
488 CHECK_OFFSET(ept_pointer, 120);
489 CHECK_OFFSET(eoi_exit_bitmap0, 128);
490 CHECK_OFFSET(eoi_exit_bitmap1, 136);
491 CHECK_OFFSET(eoi_exit_bitmap2, 144);
492 CHECK_OFFSET(eoi_exit_bitmap3, 152);
493 CHECK_OFFSET(xss_exit_bitmap, 160);
494 CHECK_OFFSET(guest_physical_address, 168);
495 CHECK_OFFSET(vmcs_link_pointer, 176);
496 CHECK_OFFSET(guest_ia32_debugctl, 184);
497 CHECK_OFFSET(guest_ia32_pat, 192);
498 CHECK_OFFSET(guest_ia32_efer, 200);
499 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
500 CHECK_OFFSET(guest_pdptr0, 216);
501 CHECK_OFFSET(guest_pdptr1, 224);
502 CHECK_OFFSET(guest_pdptr2, 232);
503 CHECK_OFFSET(guest_pdptr3, 240);
504 CHECK_OFFSET(guest_bndcfgs, 248);
505 CHECK_OFFSET(host_ia32_pat, 256);
506 CHECK_OFFSET(host_ia32_efer, 264);
507 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
508 CHECK_OFFSET(vmread_bitmap, 280);
509 CHECK_OFFSET(vmwrite_bitmap, 288);
510 CHECK_OFFSET(vm_function_control, 296);
511 CHECK_OFFSET(eptp_list_address, 304);
512 CHECK_OFFSET(pml_address, 312);
513 CHECK_OFFSET(cr0_guest_host_mask, 344);
514 CHECK_OFFSET(cr4_guest_host_mask, 352);
515 CHECK_OFFSET(cr0_read_shadow, 360);
516 CHECK_OFFSET(cr4_read_shadow, 368);
517 CHECK_OFFSET(cr3_target_value0, 376);
518 CHECK_OFFSET(cr3_target_value1, 384);
519 CHECK_OFFSET(cr3_target_value2, 392);
520 CHECK_OFFSET(cr3_target_value3, 400);
521 CHECK_OFFSET(exit_qualification, 408);
522 CHECK_OFFSET(guest_linear_address, 416);
523 CHECK_OFFSET(guest_cr0, 424);
524 CHECK_OFFSET(guest_cr3, 432);
525 CHECK_OFFSET(guest_cr4, 440);
526 CHECK_OFFSET(guest_es_base, 448);
527 CHECK_OFFSET(guest_cs_base, 456);
528 CHECK_OFFSET(guest_ss_base, 464);
529 CHECK_OFFSET(guest_ds_base, 472);
530 CHECK_OFFSET(guest_fs_base, 480);
531 CHECK_OFFSET(guest_gs_base, 488);
532 CHECK_OFFSET(guest_ldtr_base, 496);
533 CHECK_OFFSET(guest_tr_base, 504);
534 CHECK_OFFSET(guest_gdtr_base, 512);
535 CHECK_OFFSET(guest_idtr_base, 520);
536 CHECK_OFFSET(guest_dr7, 528);
537 CHECK_OFFSET(guest_rsp, 536);
538 CHECK_OFFSET(guest_rip, 544);
539 CHECK_OFFSET(guest_rflags, 552);
540 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
541 CHECK_OFFSET(guest_sysenter_esp, 568);
542 CHECK_OFFSET(guest_sysenter_eip, 576);
543 CHECK_OFFSET(host_cr0, 584);
544 CHECK_OFFSET(host_cr3, 592);
545 CHECK_OFFSET(host_cr4, 600);
546 CHECK_OFFSET(host_fs_base, 608);
547 CHECK_OFFSET(host_gs_base, 616);
548 CHECK_OFFSET(host_tr_base, 624);
549 CHECK_OFFSET(host_gdtr_base, 632);
550 CHECK_OFFSET(host_idtr_base, 640);
551 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
552 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
553 CHECK_OFFSET(host_rsp, 664);
554 CHECK_OFFSET(host_rip, 672);
555 CHECK_OFFSET(pin_based_vm_exec_control, 744);
556 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
557 CHECK_OFFSET(exception_bitmap, 752);
558 CHECK_OFFSET(page_fault_error_code_mask, 756);
559 CHECK_OFFSET(page_fault_error_code_match, 760);
560 CHECK_OFFSET(cr3_target_count, 764);
561 CHECK_OFFSET(vm_exit_controls, 768);
562 CHECK_OFFSET(vm_exit_msr_store_count, 772);
563 CHECK_OFFSET(vm_exit_msr_load_count, 776);
564 CHECK_OFFSET(vm_entry_controls, 780);
565 CHECK_OFFSET(vm_entry_msr_load_count, 784);
566 CHECK_OFFSET(vm_entry_intr_info_field, 788);
567 CHECK_OFFSET(vm_entry_exception_error_code, 792);
568 CHECK_OFFSET(vm_entry_instruction_len, 796);
569 CHECK_OFFSET(tpr_threshold, 800);
570 CHECK_OFFSET(secondary_vm_exec_control, 804);
571 CHECK_OFFSET(vm_instruction_error, 808);
572 CHECK_OFFSET(vm_exit_reason, 812);
573 CHECK_OFFSET(vm_exit_intr_info, 816);
574 CHECK_OFFSET(vm_exit_intr_error_code, 820);
575 CHECK_OFFSET(idt_vectoring_info_field, 824);
576 CHECK_OFFSET(idt_vectoring_error_code, 828);
577 CHECK_OFFSET(vm_exit_instruction_len, 832);
578 CHECK_OFFSET(vmx_instruction_info, 836);
579 CHECK_OFFSET(guest_es_limit, 840);
580 CHECK_OFFSET(guest_cs_limit, 844);
581 CHECK_OFFSET(guest_ss_limit, 848);
582 CHECK_OFFSET(guest_ds_limit, 852);
583 CHECK_OFFSET(guest_fs_limit, 856);
584 CHECK_OFFSET(guest_gs_limit, 860);
585 CHECK_OFFSET(guest_ldtr_limit, 864);
586 CHECK_OFFSET(guest_tr_limit, 868);
587 CHECK_OFFSET(guest_gdtr_limit, 872);
588 CHECK_OFFSET(guest_idtr_limit, 876);
589 CHECK_OFFSET(guest_es_ar_bytes, 880);
590 CHECK_OFFSET(guest_cs_ar_bytes, 884);
591 CHECK_OFFSET(guest_ss_ar_bytes, 888);
592 CHECK_OFFSET(guest_ds_ar_bytes, 892);
593 CHECK_OFFSET(guest_fs_ar_bytes, 896);
594 CHECK_OFFSET(guest_gs_ar_bytes, 900);
595 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
596 CHECK_OFFSET(guest_tr_ar_bytes, 908);
597 CHECK_OFFSET(guest_interruptibility_info, 912);
598 CHECK_OFFSET(guest_activity_state, 916);
599 CHECK_OFFSET(guest_sysenter_cs, 920);
600 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
601 CHECK_OFFSET(vmx_preemption_timer_value, 928);
602 CHECK_OFFSET(virtual_processor_id, 960);
603 CHECK_OFFSET(posted_intr_nv, 962);
604 CHECK_OFFSET(guest_es_selector, 964);
605 CHECK_OFFSET(guest_cs_selector, 966);
606 CHECK_OFFSET(guest_ss_selector, 968);
607 CHECK_OFFSET(guest_ds_selector, 970);
608 CHECK_OFFSET(guest_fs_selector, 972);
609 CHECK_OFFSET(guest_gs_selector, 974);
610 CHECK_OFFSET(guest_ldtr_selector, 976);
611 CHECK_OFFSET(guest_tr_selector, 978);
612 CHECK_OFFSET(guest_intr_status, 980);
613 CHECK_OFFSET(host_es_selector, 982);
614 CHECK_OFFSET(host_cs_selector, 984);
615 CHECK_OFFSET(host_ss_selector, 986);
616 CHECK_OFFSET(host_ds_selector, 988);
617 CHECK_OFFSET(host_fs_selector, 990);
618 CHECK_OFFSET(host_gs_selector, 992);
619 CHECK_OFFSET(host_tr_selector, 994);
620 CHECK_OFFSET(guest_pml_index, 996);
621}
622
623/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300624 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
625 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
626 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700627 *
628 * IMPORTANT: Changing this value will break save/restore compatibility with
629 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300630 */
631#define VMCS12_REVISION 0x11e57ed0
632
633/*
634 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
635 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
636 * current implementation, 4K are reserved to avoid future complications.
637 */
638#define VMCS12_SIZE 0x1000
639
640/*
Jim Mattson5b157062017-12-22 12:11:12 -0800641 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
642 * supported VMCS12 field encoding.
643 */
644#define VMCS12_MAX_FIELD_INDEX 0x17
645
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100646struct nested_vmx_msrs {
647 /*
648 * We only store the "true" versions of the VMX capability MSRs. We
649 * generate the "non-true" versions by setting the must-be-1 bits
650 * according to the SDM.
651 */
652 u32 procbased_ctls_low;
653 u32 procbased_ctls_high;
654 u32 secondary_ctls_low;
655 u32 secondary_ctls_high;
656 u32 pinbased_ctls_low;
657 u32 pinbased_ctls_high;
658 u32 exit_ctls_low;
659 u32 exit_ctls_high;
660 u32 entry_ctls_low;
661 u32 entry_ctls_high;
662 u32 misc_low;
663 u32 misc_high;
664 u32 ept_caps;
665 u32 vpid_caps;
666 u64 basic;
667 u64 cr0_fixed0;
668 u64 cr0_fixed1;
669 u64 cr4_fixed0;
670 u64 cr4_fixed1;
671 u64 vmcs_enum;
672 u64 vmfunc_controls;
673};
674
Jim Mattson5b157062017-12-22 12:11:12 -0800675/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300676 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
677 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
678 */
679struct nested_vmx {
680 /* Has the level1 guest done vmxon? */
681 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400682 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400683 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300684
685 /* The guest-physical address of the current VMCS L1 keeps for L2 */
686 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700687 /*
688 * Cache of the guest's VMCS, existing outside of guest memory.
689 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700690 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700691 */
692 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300693 /*
694 * Indicates if the shadow vmcs must be updated with the
695 * data hold by vmcs12
696 */
697 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100698 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300699
Jim Mattson8d860bb2018-05-09 16:56:05 -0400700 bool change_vmcs01_virtual_apic_mode;
701
Nadav Har'El644d7112011-05-25 23:12:35 +0300702 /* L2 must run next, and mustn't decide to exit to L1. */
703 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600704
705 struct loaded_vmcs vmcs02;
706
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300707 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600708 * Guest pages referred to in the vmcs02 with host-physical
709 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300710 */
711 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800712 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800713 struct page *pi_desc_page;
714 struct pi_desc *pi_desc;
715 bool pi_pending;
716 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100717
718 struct hrtimer preemption_timer;
719 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200720
721 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
722 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800723
Wanpeng Li5c614b32015-10-13 09:18:36 -0700724 u16 vpid02;
725 u16 last_vpid;
726
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100727 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200728
729 /* SMM related state */
730 struct {
731 /* in VMX operation on SMM entry? */
732 bool vmxon;
733 /* in guest mode on SMM entry? */
734 bool guest_mode;
735 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300736};
737
Yang Zhang01e439b2013-04-11 19:25:12 +0800738#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800739#define POSTED_INTR_SN 1
740
Yang Zhang01e439b2013-04-11 19:25:12 +0800741/* Posted-Interrupt Descriptor */
742struct pi_desc {
743 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800744 union {
745 struct {
746 /* bit 256 - Outstanding Notification */
747 u16 on : 1,
748 /* bit 257 - Suppress Notification */
749 sn : 1,
750 /* bit 271:258 - Reserved */
751 rsvd_1 : 14;
752 /* bit 279:272 - Notification Vector */
753 u8 nv;
754 /* bit 287:280 - Reserved */
755 u8 rsvd_2;
756 /* bit 319:288 - Notification Destination */
757 u32 ndst;
758 };
759 u64 control;
760 };
761 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800762} __aligned(64);
763
Yang Zhanga20ed542013-04-11 19:25:15 +0800764static bool pi_test_and_set_on(struct pi_desc *pi_desc)
765{
766 return test_and_set_bit(POSTED_INTR_ON,
767 (unsigned long *)&pi_desc->control);
768}
769
770static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
771{
772 return test_and_clear_bit(POSTED_INTR_ON,
773 (unsigned long *)&pi_desc->control);
774}
775
776static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
777{
778 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
779}
780
Feng Wuebbfc762015-09-18 22:29:46 +0800781static inline void pi_clear_sn(struct pi_desc *pi_desc)
782{
783 return clear_bit(POSTED_INTR_SN,
784 (unsigned long *)&pi_desc->control);
785}
786
787static inline void pi_set_sn(struct pi_desc *pi_desc)
788{
789 return set_bit(POSTED_INTR_SN,
790 (unsigned long *)&pi_desc->control);
791}
792
Paolo Bonziniad361092016-09-20 16:15:05 +0200793static inline void pi_clear_on(struct pi_desc *pi_desc)
794{
795 clear_bit(POSTED_INTR_ON,
796 (unsigned long *)&pi_desc->control);
797}
798
Feng Wuebbfc762015-09-18 22:29:46 +0800799static inline int pi_test_on(struct pi_desc *pi_desc)
800{
801 return test_bit(POSTED_INTR_ON,
802 (unsigned long *)&pi_desc->control);
803}
804
805static inline int pi_test_sn(struct pi_desc *pi_desc)
806{
807 return test_bit(POSTED_INTR_SN,
808 (unsigned long *)&pi_desc->control);
809}
810
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400811struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000812 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300813 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300814 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100815 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300816 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200817 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200818 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300819 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400820 int nmsrs;
821 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800822 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400823#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300824 u64 msr_host_kernel_gs_base;
825 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400826#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100827
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100828 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100829 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100830
Gleb Natapov2961e8762013-11-25 15:37:13 +0200831 u32 vm_entry_controls_shadow;
832 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200833 u32 secondary_exec_control;
834
Nadav Har'Eld462b812011-05-24 15:26:10 +0300835 /*
836 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
837 * non-nested (L1) guest, it always points to vmcs01. For a nested
838 * guest (L2), it points to a different VMCS.
839 */
840 struct loaded_vmcs vmcs01;
841 struct loaded_vmcs *loaded_vmcs;
842 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300843 struct msr_autoload {
844 unsigned nr;
845 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
846 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
847 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400848 struct {
849 int loaded;
850 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300851#ifdef CONFIG_X86_64
852 u16 ds_sel, es_sel;
853#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200854 int gs_ldt_reload_needed;
855 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000856 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400857 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200858 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300859 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300860 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300861 struct kvm_segment segs[8];
862 } rmode;
863 struct {
864 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300865 struct kvm_save_segment {
866 u16 selector;
867 unsigned long base;
868 u32 limit;
869 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300870 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300871 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800872 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300873 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200874
Andi Kleena0861c02009-06-08 17:37:09 +0800875 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800876
Yang Zhang01e439b2013-04-11 19:25:12 +0800877 /* Posted interrupt descriptor */
878 struct pi_desc pi_desc;
879
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300880 /* Support for a guest hypervisor (nested VMX) */
881 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200882
883 /* Dynamic PLE window. */
884 int ple_window;
885 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800886
887 /* Support for PML */
888#define PML_ENTITY_NUM 512
889 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800890
Yunhong Jiang64672c92016-06-13 14:19:59 -0700891 /* apic deadline value in host tsc */
892 u64 hv_deadline_tsc;
893
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800894 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800895
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800896 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800897
Wanpeng Li74c55932017-11-29 01:31:20 -0800898 unsigned long host_debugctlmsr;
899
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800900 /*
901 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
902 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
903 * in msr_ia32_feature_control_valid_bits.
904 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800905 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800906 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400907};
908
Avi Kivity2fb92db2011-04-27 19:42:18 +0300909enum segment_cache_field {
910 SEG_FIELD_SEL = 0,
911 SEG_FIELD_BASE = 1,
912 SEG_FIELD_LIMIT = 2,
913 SEG_FIELD_AR = 3,
914
915 SEG_FIELD_NR = 4
916};
917
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700918static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
919{
920 return container_of(kvm, struct kvm_vmx, kvm);
921}
922
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400923static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
924{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000925 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400926}
927
Feng Wuefc64402015-09-18 22:29:51 +0800928static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
929{
930 return &(to_vmx(vcpu)->pi_desc);
931}
932
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800933#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300934#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800935#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
936#define FIELD64(number, name) \
937 FIELD(number, name), \
938 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300939
Abel Gordon4607c2d2013-04-18 14:35:55 +0300940
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100941static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100942#define SHADOW_FIELD_RO(x) x,
943#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300944};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400945static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300946 ARRAY_SIZE(shadow_read_only_fields);
947
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100948static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100949#define SHADOW_FIELD_RW(x) x,
950#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300951};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400952static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300953 ARRAY_SIZE(shadow_read_write_fields);
954
Mathias Krause772e0312012-08-30 01:30:19 +0200955static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300956 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800957 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300958 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
959 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
960 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
961 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
962 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
963 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
964 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
965 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800966 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400967 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300968 FIELD(HOST_ES_SELECTOR, host_es_selector),
969 FIELD(HOST_CS_SELECTOR, host_cs_selector),
970 FIELD(HOST_SS_SELECTOR, host_ss_selector),
971 FIELD(HOST_DS_SELECTOR, host_ds_selector),
972 FIELD(HOST_FS_SELECTOR, host_fs_selector),
973 FIELD(HOST_GS_SELECTOR, host_gs_selector),
974 FIELD(HOST_TR_SELECTOR, host_tr_selector),
975 FIELD64(IO_BITMAP_A, io_bitmap_a),
976 FIELD64(IO_BITMAP_B, io_bitmap_b),
977 FIELD64(MSR_BITMAP, msr_bitmap),
978 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
979 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
980 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700981 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300982 FIELD64(TSC_OFFSET, tsc_offset),
983 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
984 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800985 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400986 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300987 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800988 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
989 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
990 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
991 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400992 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700993 FIELD64(VMREAD_BITMAP, vmread_bitmap),
994 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800995 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300996 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
997 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
998 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
999 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1000 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1001 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1002 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1003 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1004 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1005 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001006 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001007 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1008 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1009 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1010 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1011 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1012 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1013 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1014 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1015 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1016 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1017 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1018 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1019 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1020 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1021 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1022 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1023 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1024 FIELD(TPR_THRESHOLD, tpr_threshold),
1025 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1026 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1027 FIELD(VM_EXIT_REASON, vm_exit_reason),
1028 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1029 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1030 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1031 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1032 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1033 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1034 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1035 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1036 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1037 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1038 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1039 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1040 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1041 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1042 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1043 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1044 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1045 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1046 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1047 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1048 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1049 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1050 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1051 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1052 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1053 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1054 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1055 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001056 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001057 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1058 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1059 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1060 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1061 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1062 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1063 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1064 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1065 FIELD(EXIT_QUALIFICATION, exit_qualification),
1066 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1067 FIELD(GUEST_CR0, guest_cr0),
1068 FIELD(GUEST_CR3, guest_cr3),
1069 FIELD(GUEST_CR4, guest_cr4),
1070 FIELD(GUEST_ES_BASE, guest_es_base),
1071 FIELD(GUEST_CS_BASE, guest_cs_base),
1072 FIELD(GUEST_SS_BASE, guest_ss_base),
1073 FIELD(GUEST_DS_BASE, guest_ds_base),
1074 FIELD(GUEST_FS_BASE, guest_fs_base),
1075 FIELD(GUEST_GS_BASE, guest_gs_base),
1076 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1077 FIELD(GUEST_TR_BASE, guest_tr_base),
1078 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1079 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1080 FIELD(GUEST_DR7, guest_dr7),
1081 FIELD(GUEST_RSP, guest_rsp),
1082 FIELD(GUEST_RIP, guest_rip),
1083 FIELD(GUEST_RFLAGS, guest_rflags),
1084 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1085 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1086 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1087 FIELD(HOST_CR0, host_cr0),
1088 FIELD(HOST_CR3, host_cr3),
1089 FIELD(HOST_CR4, host_cr4),
1090 FIELD(HOST_FS_BASE, host_fs_base),
1091 FIELD(HOST_GS_BASE, host_gs_base),
1092 FIELD(HOST_TR_BASE, host_tr_base),
1093 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1094 FIELD(HOST_IDTR_BASE, host_idtr_base),
1095 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1096 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1097 FIELD(HOST_RSP, host_rsp),
1098 FIELD(HOST_RIP, host_rip),
1099};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001100
1101static inline short vmcs_field_to_offset(unsigned long field)
1102{
Dan Williams085331d2018-01-31 17:47:03 -08001103 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1104 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001105 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001106
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001107 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001108 return -ENOENT;
1109
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001110 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001111 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001112 return -ENOENT;
1113
Linus Torvalds15303ba2018-02-10 13:16:35 -08001114 index = array_index_nospec(index, size);
1115 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001116 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001117 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001118 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001119}
1120
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001121static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1122{
David Matlack4f2777b2016-07-13 17:16:37 -07001123 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001124}
1125
Peter Feiner995f00a2017-06-30 17:26:32 -07001126static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001127static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001128static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001129static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001130static void vmx_set_segment(struct kvm_vcpu *vcpu,
1131 struct kvm_segment *var, int seg);
1132static void vmx_get_segment(struct kvm_vcpu *vcpu,
1133 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001134static bool guest_state_valid(struct kvm_vcpu *vcpu);
1135static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001136static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001137static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1138static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1139static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1140 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001141static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001142static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1143 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001144
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1146static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001147/*
1148 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1149 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1150 */
1151static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152
Feng Wubf9f6ac2015-09-18 22:29:55 +08001153/*
1154 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1155 * can find which vCPU should be waken up.
1156 */
1157static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1158static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1159
Radim Krčmář23611332016-09-29 22:41:33 +02001160enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001161 VMX_VMREAD_BITMAP,
1162 VMX_VMWRITE_BITMAP,
1163 VMX_BITMAP_NR
1164};
1165
1166static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1167
Radim Krčmář23611332016-09-29 22:41:33 +02001168#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1169#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001170
Avi Kivity110312c2010-12-21 12:54:20 +02001171static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001172static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001173
Sheng Yang2384d2b2008-01-17 15:14:33 +08001174static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1175static DEFINE_SPINLOCK(vmx_vpid_lock);
1176
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001177static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 int size;
1179 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001180 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001182 u32 pin_based_exec_ctrl;
1183 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001184 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001185 u32 vmexit_ctrl;
1186 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001187 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001188} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
Hannes Ederefff9e52008-11-28 17:02:06 +01001190static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001191 u32 ept;
1192 u32 vpid;
1193} vmx_capability;
1194
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195#define VMX_SEGMENT_FIELD(seg) \
1196 [VCPU_SREG_##seg] = { \
1197 .selector = GUEST_##seg##_SELECTOR, \
1198 .base = GUEST_##seg##_BASE, \
1199 .limit = GUEST_##seg##_LIMIT, \
1200 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1201 }
1202
Mathias Krause772e0312012-08-30 01:30:19 +02001203static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204 unsigned selector;
1205 unsigned base;
1206 unsigned limit;
1207 unsigned ar_bytes;
1208} kvm_vmx_segment_fields[] = {
1209 VMX_SEGMENT_FIELD(CS),
1210 VMX_SEGMENT_FIELD(DS),
1211 VMX_SEGMENT_FIELD(ES),
1212 VMX_SEGMENT_FIELD(FS),
1213 VMX_SEGMENT_FIELD(GS),
1214 VMX_SEGMENT_FIELD(SS),
1215 VMX_SEGMENT_FIELD(TR),
1216 VMX_SEGMENT_FIELD(LDTR),
1217};
1218
Avi Kivity26bb0982009-09-07 11:14:12 +03001219static u64 host_efer;
1220
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001221static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1222
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001223/*
Brian Gerst8c065852010-07-17 09:03:26 -04001224 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001225 * away by decrementing the array size.
1226 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001228#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001229 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001231 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001234DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1235
1236#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1237
1238#define KVM_EVMCS_VERSION 1
1239
1240#if IS_ENABLED(CONFIG_HYPERV)
1241static bool __read_mostly enlightened_vmcs = true;
1242module_param(enlightened_vmcs, bool, 0444);
1243
1244static inline void evmcs_write64(unsigned long field, u64 value)
1245{
1246 u16 clean_field;
1247 int offset = get_evmcs_offset(field, &clean_field);
1248
1249 if (offset < 0)
1250 return;
1251
1252 *(u64 *)((char *)current_evmcs + offset) = value;
1253
1254 current_evmcs->hv_clean_fields &= ~clean_field;
1255}
1256
1257static inline void evmcs_write32(unsigned long field, u32 value)
1258{
1259 u16 clean_field;
1260 int offset = get_evmcs_offset(field, &clean_field);
1261
1262 if (offset < 0)
1263 return;
1264
1265 *(u32 *)((char *)current_evmcs + offset) = value;
1266 current_evmcs->hv_clean_fields &= ~clean_field;
1267}
1268
1269static inline void evmcs_write16(unsigned long field, u16 value)
1270{
1271 u16 clean_field;
1272 int offset = get_evmcs_offset(field, &clean_field);
1273
1274 if (offset < 0)
1275 return;
1276
1277 *(u16 *)((char *)current_evmcs + offset) = value;
1278 current_evmcs->hv_clean_fields &= ~clean_field;
1279}
1280
1281static inline u64 evmcs_read64(unsigned long field)
1282{
1283 int offset = get_evmcs_offset(field, NULL);
1284
1285 if (offset < 0)
1286 return 0;
1287
1288 return *(u64 *)((char *)current_evmcs + offset);
1289}
1290
1291static inline u32 evmcs_read32(unsigned long field)
1292{
1293 int offset = get_evmcs_offset(field, NULL);
1294
1295 if (offset < 0)
1296 return 0;
1297
1298 return *(u32 *)((char *)current_evmcs + offset);
1299}
1300
1301static inline u16 evmcs_read16(unsigned long field)
1302{
1303 int offset = get_evmcs_offset(field, NULL);
1304
1305 if (offset < 0)
1306 return 0;
1307
1308 return *(u16 *)((char *)current_evmcs + offset);
1309}
1310
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001311static inline void evmcs_touch_msr_bitmap(void)
1312{
1313 if (unlikely(!current_evmcs))
1314 return;
1315
1316 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1317 current_evmcs->hv_clean_fields &=
1318 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1319}
1320
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001321static void evmcs_load(u64 phys_addr)
1322{
1323 struct hv_vp_assist_page *vp_ap =
1324 hv_get_vp_assist_page(smp_processor_id());
1325
1326 vp_ap->current_nested_vmcs = phys_addr;
1327 vp_ap->enlighten_vmentry = 1;
1328}
1329
1330static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1331{
1332 /*
1333 * Enlightened VMCSv1 doesn't support these:
1334 *
1335 * POSTED_INTR_NV = 0x00000002,
1336 * GUEST_INTR_STATUS = 0x00000810,
1337 * APIC_ACCESS_ADDR = 0x00002014,
1338 * POSTED_INTR_DESC_ADDR = 0x00002016,
1339 * EOI_EXIT_BITMAP0 = 0x0000201c,
1340 * EOI_EXIT_BITMAP1 = 0x0000201e,
1341 * EOI_EXIT_BITMAP2 = 0x00002020,
1342 * EOI_EXIT_BITMAP3 = 0x00002022,
1343 */
1344 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1345 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1346 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1347 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1348 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1349 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1350 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1351
1352 /*
1353 * GUEST_PML_INDEX = 0x00000812,
1354 * PML_ADDRESS = 0x0000200e,
1355 */
1356 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1357
1358 /* VM_FUNCTION_CONTROL = 0x00002018, */
1359 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1360
1361 /*
1362 * EPTP_LIST_ADDRESS = 0x00002024,
1363 * VMREAD_BITMAP = 0x00002026,
1364 * VMWRITE_BITMAP = 0x00002028,
1365 */
1366 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1367
1368 /*
1369 * TSC_MULTIPLIER = 0x00002032,
1370 */
1371 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1372
1373 /*
1374 * PLE_GAP = 0x00004020,
1375 * PLE_WINDOW = 0x00004022,
1376 */
1377 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1378
1379 /*
1380 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1381 */
1382 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1383
1384 /*
1385 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1386 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1387 */
1388 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1389 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1390
1391 /*
1392 * Currently unsupported in KVM:
1393 * GUEST_IA32_RTIT_CTL = 0x00002814,
1394 */
1395}
1396#else /* !IS_ENABLED(CONFIG_HYPERV) */
1397static inline void evmcs_write64(unsigned long field, u64 value) {}
1398static inline void evmcs_write32(unsigned long field, u32 value) {}
1399static inline void evmcs_write16(unsigned long field, u16 value) {}
1400static inline u64 evmcs_read64(unsigned long field) { return 0; }
1401static inline u32 evmcs_read32(unsigned long field) { return 0; }
1402static inline u16 evmcs_read16(unsigned long field) { return 0; }
1403static inline void evmcs_load(u64 phys_addr) {}
1404static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001405static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001406#endif /* IS_ENABLED(CONFIG_HYPERV) */
1407
Jan Kiszka5bb16012016-02-09 20:14:21 +01001408static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409{
1410 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1411 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001412 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1413}
1414
Jan Kiszka6f054852016-02-09 20:15:18 +01001415static inline bool is_debug(u32 intr_info)
1416{
1417 return is_exception_n(intr_info, DB_VECTOR);
1418}
1419
1420static inline bool is_breakpoint(u32 intr_info)
1421{
1422 return is_exception_n(intr_info, BP_VECTOR);
1423}
1424
Jan Kiszka5bb16012016-02-09 20:14:21 +01001425static inline bool is_page_fault(u32 intr_info)
1426{
1427 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428}
1429
Gui Jianfeng31299942010-03-15 17:29:09 +08001430static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001431{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001432 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001433}
1434
Gui Jianfeng31299942010-03-15 17:29:09 +08001435static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001436{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001437 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001438}
1439
Liran Alon9e869482018-03-12 13:12:51 +02001440static inline bool is_gp_fault(u32 intr_info)
1441{
1442 return is_exception_n(intr_info, GP_VECTOR);
1443}
1444
Gui Jianfeng31299942010-03-15 17:29:09 +08001445static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446{
1447 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1448 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1449}
1450
Gui Jianfeng31299942010-03-15 17:29:09 +08001451static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001452{
1453 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1454 INTR_INFO_VALID_MASK)) ==
1455 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1456}
1457
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001458/* Undocumented: icebp/int1 */
1459static inline bool is_icebp(u32 intr_info)
1460{
1461 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1462 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1463}
1464
Gui Jianfeng31299942010-03-15 17:29:09 +08001465static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001466{
Sheng Yang04547152009-04-01 15:52:31 +08001467 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001468}
1469
Gui Jianfeng31299942010-03-15 17:29:09 +08001470static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001471{
Sheng Yang04547152009-04-01 15:52:31 +08001472 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001473}
1474
Paolo Bonzini35754c92015-07-29 12:05:37 +02001475static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001476{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001477 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001478}
1479
Gui Jianfeng31299942010-03-15 17:29:09 +08001480static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001481{
Sheng Yang04547152009-04-01 15:52:31 +08001482 return vmcs_config.cpu_based_exec_ctrl &
1483 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001484}
1485
Avi Kivity774ead32007-12-26 13:57:04 +02001486static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001487{
Sheng Yang04547152009-04-01 15:52:31 +08001488 return vmcs_config.cpu_based_2nd_exec_ctrl &
1489 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1490}
1491
Yang Zhang8d146952013-01-25 10:18:50 +08001492static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1493{
1494 return vmcs_config.cpu_based_2nd_exec_ctrl &
1495 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1496}
1497
Yang Zhang83d4c282013-01-25 10:18:49 +08001498static inline bool cpu_has_vmx_apic_register_virt(void)
1499{
1500 return vmcs_config.cpu_based_2nd_exec_ctrl &
1501 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1502}
1503
Yang Zhangc7c9c562013-01-25 10:18:51 +08001504static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1505{
1506 return vmcs_config.cpu_based_2nd_exec_ctrl &
1507 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1508}
1509
Yunhong Jiang64672c92016-06-13 14:19:59 -07001510/*
1511 * Comment's format: document - errata name - stepping - processor name.
1512 * Refer from
1513 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1514 */
1515static u32 vmx_preemption_cpu_tfms[] = {
1516/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15170x000206E6,
1518/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1519/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1520/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15210x00020652,
1522/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15230x00020655,
1524/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1525/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1526/*
1527 * 320767.pdf - AAP86 - B1 -
1528 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1529 */
15300x000106E5,
1531/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15320x000106A0,
1533/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15340x000106A1,
1535/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15360x000106A4,
1537 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1538 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1539 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15400x000106A5,
1541};
1542
1543static inline bool cpu_has_broken_vmx_preemption_timer(void)
1544{
1545 u32 eax = cpuid_eax(0x00000001), i;
1546
1547 /* Clear the reserved bits */
1548 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001549 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001550 if (eax == vmx_preemption_cpu_tfms[i])
1551 return true;
1552
1553 return false;
1554}
1555
1556static inline bool cpu_has_vmx_preemption_timer(void)
1557{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001558 return vmcs_config.pin_based_exec_ctrl &
1559 PIN_BASED_VMX_PREEMPTION_TIMER;
1560}
1561
Yang Zhang01e439b2013-04-11 19:25:12 +08001562static inline bool cpu_has_vmx_posted_intr(void)
1563{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001564 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1565 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001566}
1567
1568static inline bool cpu_has_vmx_apicv(void)
1569{
1570 return cpu_has_vmx_apic_register_virt() &&
1571 cpu_has_vmx_virtual_intr_delivery() &&
1572 cpu_has_vmx_posted_intr();
1573}
1574
Sheng Yang04547152009-04-01 15:52:31 +08001575static inline bool cpu_has_vmx_flexpriority(void)
1576{
1577 return cpu_has_vmx_tpr_shadow() &&
1578 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001579}
1580
Marcelo Tosattie7997942009-06-11 12:07:40 -03001581static inline bool cpu_has_vmx_ept_execute_only(void)
1582{
Gui Jianfeng31299942010-03-15 17:29:09 +08001583 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001584}
1585
Marcelo Tosattie7997942009-06-11 12:07:40 -03001586static inline bool cpu_has_vmx_ept_2m_page(void)
1587{
Gui Jianfeng31299942010-03-15 17:29:09 +08001588 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001589}
1590
Sheng Yang878403b2010-01-05 19:02:29 +08001591static inline bool cpu_has_vmx_ept_1g_page(void)
1592{
Gui Jianfeng31299942010-03-15 17:29:09 +08001593 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001594}
1595
Sheng Yang4bc9b982010-06-02 14:05:24 +08001596static inline bool cpu_has_vmx_ept_4levels(void)
1597{
1598 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1599}
1600
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001601static inline bool cpu_has_vmx_ept_mt_wb(void)
1602{
1603 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1604}
1605
Yu Zhang855feb62017-08-24 20:27:55 +08001606static inline bool cpu_has_vmx_ept_5levels(void)
1607{
1608 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1609}
1610
Xudong Hao83c3a332012-05-28 19:33:35 +08001611static inline bool cpu_has_vmx_ept_ad_bits(void)
1612{
1613 return vmx_capability.ept & VMX_EPT_AD_BIT;
1614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001617{
Gui Jianfeng31299942010-03-15 17:29:09 +08001618 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001619}
1620
Gui Jianfeng31299942010-03-15 17:29:09 +08001621static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001622{
Gui Jianfeng31299942010-03-15 17:29:09 +08001623 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001624}
1625
Liran Aloncd9a4912018-05-22 17:16:15 +03001626static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1627{
1628 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1629}
1630
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001631static inline bool cpu_has_vmx_invvpid_single(void)
1632{
1633 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1634}
1635
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001636static inline bool cpu_has_vmx_invvpid_global(void)
1637{
1638 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1639}
1640
Wanpeng Li08d839c2017-03-23 05:30:08 -07001641static inline bool cpu_has_vmx_invvpid(void)
1642{
1643 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1644}
1645
Gui Jianfeng31299942010-03-15 17:29:09 +08001646static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001647{
Sheng Yang04547152009-04-01 15:52:31 +08001648 return vmcs_config.cpu_based_2nd_exec_ctrl &
1649 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001650}
1651
Gui Jianfeng31299942010-03-15 17:29:09 +08001652static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001653{
1654 return vmcs_config.cpu_based_2nd_exec_ctrl &
1655 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1656}
1657
Gui Jianfeng31299942010-03-15 17:29:09 +08001658static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001659{
1660 return vmcs_config.cpu_based_2nd_exec_ctrl &
1661 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1662}
1663
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001664static inline bool cpu_has_vmx_basic_inout(void)
1665{
1666 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1667}
1668
Paolo Bonzini35754c92015-07-29 12:05:37 +02001669static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001671 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001672}
1673
Gui Jianfeng31299942010-03-15 17:29:09 +08001674static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001675{
Sheng Yang04547152009-04-01 15:52:31 +08001676 return vmcs_config.cpu_based_2nd_exec_ctrl &
1677 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001678}
1679
Gui Jianfeng31299942010-03-15 17:29:09 +08001680static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001681{
1682 return vmcs_config.cpu_based_2nd_exec_ctrl &
1683 SECONDARY_EXEC_RDTSCP;
1684}
1685
Mao, Junjiead756a12012-07-02 01:18:48 +00001686static inline bool cpu_has_vmx_invpcid(void)
1687{
1688 return vmcs_config.cpu_based_2nd_exec_ctrl &
1689 SECONDARY_EXEC_ENABLE_INVPCID;
1690}
1691
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001692static inline bool cpu_has_virtual_nmis(void)
1693{
1694 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1695}
1696
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001697static inline bool cpu_has_vmx_wbinvd_exit(void)
1698{
1699 return vmcs_config.cpu_based_2nd_exec_ctrl &
1700 SECONDARY_EXEC_WBINVD_EXITING;
1701}
1702
Abel Gordonabc4fc52013-04-18 14:35:25 +03001703static inline bool cpu_has_vmx_shadow_vmcs(void)
1704{
1705 u64 vmx_msr;
1706 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1707 /* check if the cpu supports writing r/o exit information fields */
1708 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1709 return false;
1710
1711 return vmcs_config.cpu_based_2nd_exec_ctrl &
1712 SECONDARY_EXEC_SHADOW_VMCS;
1713}
1714
Kai Huang843e4332015-01-28 10:54:28 +08001715static inline bool cpu_has_vmx_pml(void)
1716{
1717 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1718}
1719
Haozhong Zhang64903d62015-10-20 15:39:09 +08001720static inline bool cpu_has_vmx_tsc_scaling(void)
1721{
1722 return vmcs_config.cpu_based_2nd_exec_ctrl &
1723 SECONDARY_EXEC_TSC_SCALING;
1724}
1725
Bandan Das2a499e42017-08-03 15:54:41 -04001726static inline bool cpu_has_vmx_vmfunc(void)
1727{
1728 return vmcs_config.cpu_based_2nd_exec_ctrl &
1729 SECONDARY_EXEC_ENABLE_VMFUNC;
1730}
1731
Sean Christopherson64f7a112018-04-30 10:01:06 -07001732static bool vmx_umip_emulated(void)
1733{
1734 return vmcs_config.cpu_based_2nd_exec_ctrl &
1735 SECONDARY_EXEC_DESC;
1736}
1737
Sheng Yang04547152009-04-01 15:52:31 +08001738static inline bool report_flexpriority(void)
1739{
1740 return flexpriority_enabled;
1741}
1742
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001743static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1744{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001745 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001746}
1747
Jim Mattsonf4160e42018-05-29 09:11:33 -07001748/*
1749 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1750 * to modify any valid field of the VMCS, or are the VM-exit
1751 * information fields read-only?
1752 */
1753static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1754{
1755 return to_vmx(vcpu)->nested.msrs.misc_low &
1756 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1757}
1758
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001759static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1760{
1761 return vmcs12->cpu_based_vm_exec_control & bit;
1762}
1763
1764static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1765{
1766 return (vmcs12->cpu_based_vm_exec_control &
1767 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1768 (vmcs12->secondary_vm_exec_control & bit);
1769}
1770
Jan Kiszkaf41245002014-03-07 20:03:13 +01001771static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1772{
1773 return vmcs12->pin_based_vm_exec_control &
1774 PIN_BASED_VMX_PREEMPTION_TIMER;
1775}
1776
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001777static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1778{
1779 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1780}
1781
1782static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1783{
1784 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1785}
1786
Nadav Har'El155a97a2013-08-05 11:07:16 +03001787static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1788{
1789 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1790}
1791
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001792static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1793{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001794 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001795}
1796
Bandan Dasc5f983f2017-05-05 15:25:14 -04001797static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1798{
1799 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1800}
1801
Wincy Vanf2b93282015-02-03 23:56:03 +08001802static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1803{
1804 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1805}
1806
Wanpeng Li5c614b32015-10-13 09:18:36 -07001807static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1808{
1809 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1810}
1811
Wincy Van82f0dd42015-02-03 23:57:18 +08001812static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1813{
1814 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1815}
1816
Wincy Van608406e2015-02-03 23:57:51 +08001817static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1818{
1819 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1820}
1821
Wincy Van705699a2015-02-03 23:58:17 +08001822static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1823{
1824 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1825}
1826
Bandan Das27c42a12017-08-03 15:54:42 -04001827static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1828{
1829 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1830}
1831
Bandan Das41ab9372017-08-03 15:54:43 -04001832static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1833{
1834 return nested_cpu_has_vmfunc(vmcs12) &&
1835 (vmcs12->vm_function_control &
1836 VMX_VMFUNC_EPTP_SWITCHING);
1837}
1838
Jim Mattsonef85b672016-12-12 11:01:37 -08001839static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001840{
1841 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001842 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001843}
1844
Jan Kiszka533558b2014-01-04 18:47:20 +01001845static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1846 u32 exit_intr_info,
1847 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001848static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1849 struct vmcs12 *vmcs12,
1850 u32 reason, unsigned long qualification);
1851
Rusty Russell8b9cf982007-07-30 16:31:43 +10001852static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001853{
1854 int i;
1855
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001856 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001857 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001858 return i;
1859 return -1;
1860}
1861
Sheng Yang2384d2b2008-01-17 15:14:33 +08001862static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1863{
1864 struct {
1865 u64 vpid : 16;
1866 u64 rsvd : 48;
1867 u64 gva;
1868 } operand = { vpid, 0, gva };
1869
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001870 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001871 /* CF==1 or ZF==1 --> rc = -1 */
1872 "; ja 1f ; ud2 ; 1:"
1873 : : "a"(&operand), "c"(ext) : "cc", "memory");
1874}
1875
Sheng Yang14394422008-04-28 12:24:45 +08001876static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1877{
1878 struct {
1879 u64 eptp, gpa;
1880 } operand = {eptp, gpa};
1881
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001882 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001883 /* CF==1 or ZF==1 --> rc = -1 */
1884 "; ja 1f ; ud2 ; 1:\n"
1885 : : "a" (&operand), "c" (ext) : "cc", "memory");
1886}
1887
Avi Kivity26bb0982009-09-07 11:14:12 +03001888static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001889{
1890 int i;
1891
Rusty Russell8b9cf982007-07-30 16:31:43 +10001892 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001893 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001894 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001895 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001896}
1897
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898static void vmcs_clear(struct vmcs *vmcs)
1899{
1900 u64 phys_addr = __pa(vmcs);
1901 u8 error;
1902
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001903 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001904 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 : "cc", "memory");
1906 if (error)
1907 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1908 vmcs, phys_addr);
1909}
1910
Nadav Har'Eld462b812011-05-24 15:26:10 +03001911static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1912{
1913 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001914 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1915 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001916 loaded_vmcs->cpu = -1;
1917 loaded_vmcs->launched = 0;
1918}
1919
Dongxiao Xu7725b892010-05-11 18:29:38 +08001920static void vmcs_load(struct vmcs *vmcs)
1921{
1922 u64 phys_addr = __pa(vmcs);
1923 u8 error;
1924
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001925 if (static_branch_unlikely(&enable_evmcs))
1926 return evmcs_load(phys_addr);
1927
Dongxiao Xu7725b892010-05-11 18:29:38 +08001928 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001929 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001930 : "cc", "memory");
1931 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001932 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001933 vmcs, phys_addr);
1934}
1935
Dave Young2965faa2015-09-09 15:38:55 -07001936#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001937/*
1938 * This bitmap is used to indicate whether the vmclear
1939 * operation is enabled on all cpus. All disabled by
1940 * default.
1941 */
1942static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1943
1944static inline void crash_enable_local_vmclear(int cpu)
1945{
1946 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1947}
1948
1949static inline void crash_disable_local_vmclear(int cpu)
1950{
1951 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1952}
1953
1954static inline int crash_local_vmclear_enabled(int cpu)
1955{
1956 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1957}
1958
1959static void crash_vmclear_local_loaded_vmcss(void)
1960{
1961 int cpu = raw_smp_processor_id();
1962 struct loaded_vmcs *v;
1963
1964 if (!crash_local_vmclear_enabled(cpu))
1965 return;
1966
1967 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1968 loaded_vmcss_on_cpu_link)
1969 vmcs_clear(v->vmcs);
1970}
1971#else
1972static inline void crash_enable_local_vmclear(int cpu) { }
1973static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001974#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001975
Nadav Har'Eld462b812011-05-24 15:26:10 +03001976static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001978 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001979 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980
Nadav Har'Eld462b812011-05-24 15:26:10 +03001981 if (loaded_vmcs->cpu != cpu)
1982 return; /* vcpu migration can race with cpu offline */
1983 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001985 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001986 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001987
1988 /*
1989 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1990 * is before setting loaded_vmcs->vcpu to -1 which is done in
1991 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1992 * then adds the vmcs into percpu list before it is deleted.
1993 */
1994 smp_wmb();
1995
Nadav Har'Eld462b812011-05-24 15:26:10 +03001996 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001997 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998}
1999
Nadav Har'Eld462b812011-05-24 15:26:10 +03002000static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002001{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002002 int cpu = loaded_vmcs->cpu;
2003
2004 if (cpu != -1)
2005 smp_call_function_single(cpu,
2006 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002007}
2008
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002009static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002010{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002011 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002012 return;
2013
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002014 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002015 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002016}
2017
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002018static inline void vpid_sync_vcpu_global(void)
2019{
2020 if (cpu_has_vmx_invvpid_global())
2021 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2022}
2023
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002024static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002025{
2026 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002027 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002028 else
2029 vpid_sync_vcpu_global();
2030}
2031
Sheng Yang14394422008-04-28 12:24:45 +08002032static inline void ept_sync_global(void)
2033{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002034 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002035}
2036
2037static inline void ept_sync_context(u64 eptp)
2038{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002039 if (cpu_has_vmx_invept_context())
2040 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2041 else
2042 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002043}
2044
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002045static __always_inline void vmcs_check16(unsigned long field)
2046{
2047 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2048 "16-bit accessor invalid for 64-bit field");
2049 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2050 "16-bit accessor invalid for 64-bit high field");
2051 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2052 "16-bit accessor invalid for 32-bit high field");
2053 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2054 "16-bit accessor invalid for natural width field");
2055}
2056
2057static __always_inline void vmcs_check32(unsigned long field)
2058{
2059 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2060 "32-bit accessor invalid for 16-bit field");
2061 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2062 "32-bit accessor invalid for natural width field");
2063}
2064
2065static __always_inline void vmcs_check64(unsigned long field)
2066{
2067 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2068 "64-bit accessor invalid for 16-bit field");
2069 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2070 "64-bit accessor invalid for 64-bit high field");
2071 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2072 "64-bit accessor invalid for 32-bit field");
2073 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2074 "64-bit accessor invalid for natural width field");
2075}
2076
2077static __always_inline void vmcs_checkl(unsigned long field)
2078{
2079 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2080 "Natural width accessor invalid for 16-bit field");
2081 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2082 "Natural width accessor invalid for 64-bit field");
2083 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2084 "Natural width accessor invalid for 64-bit high field");
2085 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2086 "Natural width accessor invalid for 32-bit field");
2087}
2088
2089static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090{
Avi Kivity5e520e62011-05-15 10:13:12 -04002091 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092
Avi Kivity5e520e62011-05-15 10:13:12 -04002093 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2094 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095 return value;
2096}
2097
Avi Kivity96304212011-05-15 10:13:13 -04002098static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002100 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002101 if (static_branch_unlikely(&enable_evmcs))
2102 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002103 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104}
2105
Avi Kivity96304212011-05-15 10:13:13 -04002106static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002108 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002109 if (static_branch_unlikely(&enable_evmcs))
2110 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002111 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112}
2113
Avi Kivity96304212011-05-15 10:13:13 -04002114static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002116 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002117 if (static_branch_unlikely(&enable_evmcs))
2118 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002119#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002120 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002122 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123#endif
2124}
2125
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002126static __always_inline unsigned long vmcs_readl(unsigned long field)
2127{
2128 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002129 if (static_branch_unlikely(&enable_evmcs))
2130 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002131 return __vmcs_readl(field);
2132}
2133
Avi Kivitye52de1b2007-01-05 16:36:56 -08002134static noinline void vmwrite_error(unsigned long field, unsigned long value)
2135{
2136 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2137 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2138 dump_stack();
2139}
2140
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002141static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142{
2143 u8 error;
2144
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002145 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002146 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002147 if (unlikely(error))
2148 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149}
2150
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002151static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002153 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002154 if (static_branch_unlikely(&enable_evmcs))
2155 return evmcs_write16(field, value);
2156
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158}
2159
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002160static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002162 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002163 if (static_branch_unlikely(&enable_evmcs))
2164 return evmcs_write32(field, value);
2165
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002166 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167}
2168
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002169static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002171 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002172 if (static_branch_unlikely(&enable_evmcs))
2173 return evmcs_write64(field, value);
2174
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002175 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002176#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002178 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179#endif
2180}
2181
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002183{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002184 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002185 if (static_branch_unlikely(&enable_evmcs))
2186 return evmcs_write64(field, value);
2187
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002188 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002189}
2190
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002191static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002192{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002193 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2194 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002195 if (static_branch_unlikely(&enable_evmcs))
2196 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2197
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002198 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2199}
2200
2201static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2202{
2203 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2204 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002205 if (static_branch_unlikely(&enable_evmcs))
2206 return evmcs_write32(field, evmcs_read32(field) | mask);
2207
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002208 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002209}
2210
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002211static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2212{
2213 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2214}
2215
Gleb Natapov2961e8762013-11-25 15:37:13 +02002216static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2217{
2218 vmcs_write32(VM_ENTRY_CONTROLS, val);
2219 vmx->vm_entry_controls_shadow = val;
2220}
2221
2222static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2223{
2224 if (vmx->vm_entry_controls_shadow != val)
2225 vm_entry_controls_init(vmx, val);
2226}
2227
2228static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2229{
2230 return vmx->vm_entry_controls_shadow;
2231}
2232
2233
2234static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2235{
2236 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2237}
2238
2239static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2240{
2241 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2242}
2243
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002244static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2245{
2246 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2247}
2248
Gleb Natapov2961e8762013-11-25 15:37:13 +02002249static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2250{
2251 vmcs_write32(VM_EXIT_CONTROLS, val);
2252 vmx->vm_exit_controls_shadow = val;
2253}
2254
2255static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2256{
2257 if (vmx->vm_exit_controls_shadow != val)
2258 vm_exit_controls_init(vmx, val);
2259}
2260
2261static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2262{
2263 return vmx->vm_exit_controls_shadow;
2264}
2265
2266
2267static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2268{
2269 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2270}
2271
2272static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2273{
2274 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2275}
2276
Avi Kivity2fb92db2011-04-27 19:42:18 +03002277static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2278{
2279 vmx->segment_cache.bitmask = 0;
2280}
2281
2282static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2283 unsigned field)
2284{
2285 bool ret;
2286 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2287
2288 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2289 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2290 vmx->segment_cache.bitmask = 0;
2291 }
2292 ret = vmx->segment_cache.bitmask & mask;
2293 vmx->segment_cache.bitmask |= mask;
2294 return ret;
2295}
2296
2297static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2298{
2299 u16 *p = &vmx->segment_cache.seg[seg].selector;
2300
2301 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2302 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2303 return *p;
2304}
2305
2306static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2307{
2308 ulong *p = &vmx->segment_cache.seg[seg].base;
2309
2310 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2311 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2312 return *p;
2313}
2314
2315static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2316{
2317 u32 *p = &vmx->segment_cache.seg[seg].limit;
2318
2319 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2320 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2321 return *p;
2322}
2323
2324static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2325{
2326 u32 *p = &vmx->segment_cache.seg[seg].ar;
2327
2328 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2329 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2330 return *p;
2331}
2332
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002333static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2334{
2335 u32 eb;
2336
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002337 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002338 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002339 /*
2340 * Guest access to VMware backdoor ports could legitimately
2341 * trigger #GP because of TSS I/O permission bitmap.
2342 * We intercept those #GP and allow access to them anyway
2343 * as VMware does.
2344 */
2345 if (enable_vmware_backdoor)
2346 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002347 if ((vcpu->guest_debug &
2348 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2349 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2350 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002351 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002352 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002353 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002354 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002355
2356 /* When we are running a nested L2 guest and L1 specified for it a
2357 * certain exception bitmap, we must trap the same exceptions and pass
2358 * them to L1. When running L2, we will only handle the exceptions
2359 * specified above if L1 did not want them.
2360 */
2361 if (is_guest_mode(vcpu))
2362 eb |= get_vmcs12(vcpu)->exception_bitmap;
2363
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002364 vmcs_write32(EXCEPTION_BITMAP, eb);
2365}
2366
Ashok Raj15d45072018-02-01 22:59:43 +01002367/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002368 * Check if MSR is intercepted for currently loaded MSR bitmap.
2369 */
2370static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2371{
2372 unsigned long *msr_bitmap;
2373 int f = sizeof(unsigned long);
2374
2375 if (!cpu_has_vmx_msr_bitmap())
2376 return true;
2377
2378 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2379
2380 if (msr <= 0x1fff) {
2381 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2382 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2383 msr &= 0x1fff;
2384 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2385 }
2386
2387 return true;
2388}
2389
2390/*
Ashok Raj15d45072018-02-01 22:59:43 +01002391 * Check if MSR is intercepted for L01 MSR bitmap.
2392 */
2393static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2394{
2395 unsigned long *msr_bitmap;
2396 int f = sizeof(unsigned long);
2397
2398 if (!cpu_has_vmx_msr_bitmap())
2399 return true;
2400
2401 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2402
2403 if (msr <= 0x1fff) {
2404 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2405 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2406 msr &= 0x1fff;
2407 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2408 }
2409
2410 return true;
2411}
2412
Gleb Natapov2961e8762013-11-25 15:37:13 +02002413static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2414 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002415{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002416 vm_entry_controls_clearbit(vmx, entry);
2417 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002418}
2419
Avi Kivity61d2ef22010-04-28 16:40:38 +03002420static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2421{
2422 unsigned i;
2423 struct msr_autoload *m = &vmx->msr_autoload;
2424
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002425 switch (msr) {
2426 case MSR_EFER:
2427 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002428 clear_atomic_switch_msr_special(vmx,
2429 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002430 VM_EXIT_LOAD_IA32_EFER);
2431 return;
2432 }
2433 break;
2434 case MSR_CORE_PERF_GLOBAL_CTRL:
2435 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002436 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002437 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2438 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2439 return;
2440 }
2441 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002442 }
2443
Avi Kivity61d2ef22010-04-28 16:40:38 +03002444 for (i = 0; i < m->nr; ++i)
2445 if (m->guest[i].index == msr)
2446 break;
2447
2448 if (i == m->nr)
2449 return;
2450 --m->nr;
2451 m->guest[i] = m->guest[m->nr];
2452 m->host[i] = m->host[m->nr];
2453 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2454 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2455}
2456
Gleb Natapov2961e8762013-11-25 15:37:13 +02002457static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2458 unsigned long entry, unsigned long exit,
2459 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2460 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002461{
2462 vmcs_write64(guest_val_vmcs, guest_val);
2463 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002464 vm_entry_controls_setbit(vmx, entry);
2465 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002466}
2467
Avi Kivity61d2ef22010-04-28 16:40:38 +03002468static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2469 u64 guest_val, u64 host_val)
2470{
2471 unsigned i;
2472 struct msr_autoload *m = &vmx->msr_autoload;
2473
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002474 switch (msr) {
2475 case MSR_EFER:
2476 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002477 add_atomic_switch_msr_special(vmx,
2478 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002479 VM_EXIT_LOAD_IA32_EFER,
2480 GUEST_IA32_EFER,
2481 HOST_IA32_EFER,
2482 guest_val, host_val);
2483 return;
2484 }
2485 break;
2486 case MSR_CORE_PERF_GLOBAL_CTRL:
2487 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002488 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002489 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2490 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2491 GUEST_IA32_PERF_GLOBAL_CTRL,
2492 HOST_IA32_PERF_GLOBAL_CTRL,
2493 guest_val, host_val);
2494 return;
2495 }
2496 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002497 case MSR_IA32_PEBS_ENABLE:
2498 /* PEBS needs a quiescent period after being disabled (to write
2499 * a record). Disabling PEBS through VMX MSR swapping doesn't
2500 * provide that period, so a CPU could write host's record into
2501 * guest's memory.
2502 */
2503 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002504 }
2505
Avi Kivity61d2ef22010-04-28 16:40:38 +03002506 for (i = 0; i < m->nr; ++i)
2507 if (m->guest[i].index == msr)
2508 break;
2509
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002510 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002511 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002512 "Can't add msr %x\n", msr);
2513 return;
2514 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002515 ++m->nr;
2516 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2517 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2518 }
2519
2520 m->guest[i].index = msr;
2521 m->guest[i].value = guest_val;
2522 m->host[i].index = msr;
2523 m->host[i].value = host_val;
2524}
2525
Avi Kivity92c0d902009-10-29 11:00:16 +02002526static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002527{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002528 u64 guest_efer = vmx->vcpu.arch.efer;
2529 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002530
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002531 if (!enable_ept) {
2532 /*
2533 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2534 * host CPUID is more efficient than testing guest CPUID
2535 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2536 */
2537 if (boot_cpu_has(X86_FEATURE_SMEP))
2538 guest_efer |= EFER_NX;
2539 else if (!(guest_efer & EFER_NX))
2540 ignore_bits |= EFER_NX;
2541 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002542
Avi Kivity51c6cf62007-08-29 03:48:05 +03002543 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002544 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002545 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002546 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002547#ifdef CONFIG_X86_64
2548 ignore_bits |= EFER_LMA | EFER_LME;
2549 /* SCE is meaningful only in long mode on Intel */
2550 if (guest_efer & EFER_LMA)
2551 ignore_bits &= ~(u64)EFER_SCE;
2552#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002553
2554 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002555
2556 /*
2557 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2558 * On CPUs that support "load IA32_EFER", always switch EFER
2559 * atomically, since it's faster than switching it manually.
2560 */
2561 if (cpu_has_load_ia32_efer ||
2562 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002563 if (!(guest_efer & EFER_LMA))
2564 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002565 if (guest_efer != host_efer)
2566 add_atomic_switch_msr(vmx, MSR_EFER,
2567 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002568 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002569 } else {
2570 guest_efer &= ~ignore_bits;
2571 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002572
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002573 vmx->guest_msrs[efer_offset].data = guest_efer;
2574 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2575
2576 return true;
2577 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002578}
2579
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002580#ifdef CONFIG_X86_32
2581/*
2582 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2583 * VMCS rather than the segment table. KVM uses this helper to figure
2584 * out the current bases to poke them into the VMCS before entry.
2585 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002586static unsigned long segment_base(u16 selector)
2587{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002588 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002589 unsigned long v;
2590
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002591 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002592 return 0;
2593
Thomas Garnier45fc8752017-03-14 10:05:08 -07002594 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002595
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002596 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002597 u16 ldt_selector = kvm_read_ldt();
2598
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002599 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002600 return 0;
2601
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002602 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002603 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002604 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002605 return v;
2606}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002607#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002608
Avi Kivity04d2cc72007-09-10 18:10:54 +03002609static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002610{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002611 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002612#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002613 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002614#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002615 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002616
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002617 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002618 return;
2619
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002620 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002621 /*
2622 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2623 * allow segment selectors with cpl > 0 or ti == 1.
2624 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002625 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002626 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002627
2628#ifdef CONFIG_X86_64
2629 save_fsgs_for_kvm();
2630 vmx->host_state.fs_sel = current->thread.fsindex;
2631 vmx->host_state.gs_sel = current->thread.gsindex;
2632#else
Avi Kivity9581d442010-10-19 16:46:55 +02002633 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002634 savesegment(gs, vmx->host_state.gs_sel);
2635#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002636 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002637 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002638 vmx->host_state.fs_reload_needed = 0;
2639 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002640 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002641 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002642 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002643 if (!(vmx->host_state.gs_sel & 7))
2644 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002645 else {
2646 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002647 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002648 }
2649
2650#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002651 savesegment(ds, vmx->host_state.ds_sel);
2652 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002653
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002654 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002655 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002656
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002657 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002658 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002659 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002660#else
2661 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2662 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2663#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002664 if (boot_cpu_has(X86_FEATURE_MPX))
2665 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002666 for (i = 0; i < vmx->save_nmsrs; ++i)
2667 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002668 vmx->guest_msrs[i].data,
2669 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002670}
2671
Avi Kivitya9b21b62008-06-24 11:48:49 +03002672static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002673{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002674 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002675 return;
2676
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002677 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002678 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002679#ifdef CONFIG_X86_64
2680 if (is_long_mode(&vmx->vcpu))
2681 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2682#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002683 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002684 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002685#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002686 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002687#else
2688 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002689#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002690 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002691 if (vmx->host_state.fs_reload_needed)
2692 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002693#ifdef CONFIG_X86_64
2694 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2695 loadsegment(ds, vmx->host_state.ds_sel);
2696 loadsegment(es, vmx->host_state.es_sel);
2697 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002698#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002699 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002700#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002701 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002702#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002703 if (vmx->host_state.msr_host_bndcfgs)
2704 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002705 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002706}
2707
Avi Kivitya9b21b62008-06-24 11:48:49 +03002708static void vmx_load_host_state(struct vcpu_vmx *vmx)
2709{
2710 preempt_disable();
2711 __vmx_load_host_state(vmx);
2712 preempt_enable();
2713}
2714
Feng Wu28b835d2015-09-18 22:29:54 +08002715static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2716{
2717 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2718 struct pi_desc old, new;
2719 unsigned int dest;
2720
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002721 /*
2722 * In case of hot-plug or hot-unplug, we may have to undo
2723 * vmx_vcpu_pi_put even if there is no assigned device. And we
2724 * always keep PI.NDST up to date for simplicity: it makes the
2725 * code easier, and CPU migration is not a fast path.
2726 */
2727 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002728 return;
2729
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002730 /*
2731 * First handle the simple case where no cmpxchg is necessary; just
2732 * allow posting non-urgent interrupts.
2733 *
2734 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2735 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2736 * expects the VCPU to be on the blocked_vcpu_list that matches
2737 * PI.NDST.
2738 */
2739 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2740 vcpu->cpu == cpu) {
2741 pi_clear_sn(pi_desc);
2742 return;
2743 }
2744
2745 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002746 do {
2747 old.control = new.control = pi_desc->control;
2748
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002749 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002750
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002751 if (x2apic_enabled())
2752 new.ndst = dest;
2753 else
2754 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002755
Feng Wu28b835d2015-09-18 22:29:54 +08002756 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002757 } while (cmpxchg64(&pi_desc->control, old.control,
2758 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002759}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002760
Peter Feinerc95ba922016-08-17 09:36:47 -07002761static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2762{
2763 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2764 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2765}
2766
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767/*
2768 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2769 * vcpu mutex is already taken.
2770 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002771static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002773 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002774 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002776 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002777 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002778 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002779 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002780
2781 /*
2782 * Read loaded_vmcs->cpu should be before fetching
2783 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2784 * See the comments in __loaded_vmcs_clear().
2785 */
2786 smp_rmb();
2787
Nadav Har'Eld462b812011-05-24 15:26:10 +03002788 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2789 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002790 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002791 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002792 }
2793
2794 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2795 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2796 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002797 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002798 }
2799
2800 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002801 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002802 unsigned long sysenter_esp;
2803
2804 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002805
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 /*
2807 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002808 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002810 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002811 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002812 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002814 /*
2815 * VM exits change the host TR limit to 0x67 after a VM
2816 * exit. This is okay, since 0x67 covers everything except
2817 * the IO bitmap and have have code to handle the IO bitmap
2818 * being lost after a VM exit.
2819 */
2820 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2821
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2823 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002824
Nadav Har'Eld462b812011-05-24 15:26:10 +03002825 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 }
Feng Wu28b835d2015-09-18 22:29:54 +08002827
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002828 /* Setup TSC multiplier */
2829 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002830 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2831 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002832
Feng Wu28b835d2015-09-18 22:29:54 +08002833 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002834 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002835 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002836}
2837
2838static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2839{
2840 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2841
2842 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002843 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2844 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002845 return;
2846
2847 /* Set SN when the vCPU is preempted */
2848 if (vcpu->preempted)
2849 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850}
2851
2852static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2853{
Feng Wu28b835d2015-09-18 22:29:54 +08002854 vmx_vcpu_pi_put(vcpu);
2855
Avi Kivitya9b21b62008-06-24 11:48:49 +03002856 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857}
2858
Wanpeng Lif244dee2017-07-20 01:11:54 -07002859static bool emulation_required(struct kvm_vcpu *vcpu)
2860{
2861 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2862}
2863
Avi Kivityedcafe32009-12-30 18:07:40 +02002864static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2865
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002866/*
2867 * Return the cr0 value that a nested guest would read. This is a combination
2868 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2869 * its hypervisor (cr0_read_shadow).
2870 */
2871static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2872{
2873 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2874 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2875}
2876static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2877{
2878 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2879 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2880}
2881
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2883{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002884 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002885
Avi Kivity6de12732011-03-07 12:51:22 +02002886 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2887 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2888 rflags = vmcs_readl(GUEST_RFLAGS);
2889 if (to_vmx(vcpu)->rmode.vm86_active) {
2890 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2891 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2892 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2893 }
2894 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002895 }
Avi Kivity6de12732011-03-07 12:51:22 +02002896 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897}
2898
2899static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2900{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002901 unsigned long old_rflags = vmx_get_rflags(vcpu);
2902
Avi Kivity6de12732011-03-07 12:51:22 +02002903 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2904 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002905 if (to_vmx(vcpu)->rmode.vm86_active) {
2906 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002907 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002908 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002910
2911 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2912 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913}
2914
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002915static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002916{
2917 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2918 int ret = 0;
2919
2920 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002921 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002922 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002923 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002924
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002925 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002926}
2927
2928static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2929{
2930 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2931 u32 interruptibility = interruptibility_old;
2932
2933 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2934
Jan Kiszka48005f62010-02-19 19:38:07 +01002935 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002936 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002937 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002938 interruptibility |= GUEST_INTR_STATE_STI;
2939
2940 if ((interruptibility != interruptibility_old))
2941 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2942}
2943
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2945{
2946 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002948 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002950 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951
Glauber Costa2809f5d2009-05-12 16:21:05 -04002952 /* skipping an emulated instruction also counts */
2953 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954}
2955
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002956static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2957 unsigned long exit_qual)
2958{
2959 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2960 unsigned int nr = vcpu->arch.exception.nr;
2961 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2962
2963 if (vcpu->arch.exception.has_error_code) {
2964 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2965 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2966 }
2967
2968 if (kvm_exception_is_soft(nr))
2969 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2970 else
2971 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2972
2973 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2974 vmx_get_nmi_mask(vcpu))
2975 intr_info |= INTR_INFO_UNBLOCK_NMI;
2976
2977 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2978}
2979
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002980/*
2981 * KVM wants to inject page-faults which it got to the guest. This function
2982 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002983 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002984static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002985{
2986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002987 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002988
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002989 if (nr == PF_VECTOR) {
2990 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002991 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002992 return 1;
2993 }
2994 /*
2995 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2996 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2997 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2998 * can be written only when inject_pending_event runs. This should be
2999 * conditional on a new capability---if the capability is disabled,
3000 * kvm_multiple_exception would write the ancillary information to
3001 * CR2 or DR6, for backwards ABI-compatibility.
3002 */
3003 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3004 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003005 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003006 return 1;
3007 }
3008 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003009 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003010 if (nr == DB_VECTOR)
3011 *exit_qual = vcpu->arch.dr6;
3012 else
3013 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003014 return 1;
3015 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003016 }
3017
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003018 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003019}
3020
Wanpeng Licaa057a2018-03-12 04:53:03 -07003021static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3022{
3023 /*
3024 * Ensure that we clear the HLT state in the VMCS. We don't need to
3025 * explicitly skip the instruction because if the HLT state is set,
3026 * then the instruction is already executing and RIP has already been
3027 * advanced.
3028 */
3029 if (kvm_hlt_in_guest(vcpu->kvm) &&
3030 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3031 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3032}
3033
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003034static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003035{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003036 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003037 unsigned nr = vcpu->arch.exception.nr;
3038 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003039 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003040 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003041
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003042 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003043 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003044 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3045 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003046
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003047 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003048 int inc_eip = 0;
3049 if (kvm_exception_is_soft(nr))
3050 inc_eip = vcpu->arch.event_exit_inst_len;
3051 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003052 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003053 return;
3054 }
3055
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003056 WARN_ON_ONCE(vmx->emulation_required);
3057
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003058 if (kvm_exception_is_soft(nr)) {
3059 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3060 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003061 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3062 } else
3063 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3064
3065 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003066
3067 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003068}
3069
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003070static bool vmx_rdtscp_supported(void)
3071{
3072 return cpu_has_vmx_rdtscp();
3073}
3074
Mao, Junjiead756a12012-07-02 01:18:48 +00003075static bool vmx_invpcid_supported(void)
3076{
3077 return cpu_has_vmx_invpcid() && enable_ept;
3078}
3079
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080/*
Eddie Donga75beee2007-05-17 18:55:15 +03003081 * Swap MSR entry in host/guest MSR entry array.
3082 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003083static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003084{
Avi Kivity26bb0982009-09-07 11:14:12 +03003085 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003086
3087 tmp = vmx->guest_msrs[to];
3088 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3089 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003090}
3091
3092/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003093 * Set up the vmcs to automatically save and restore system
3094 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3095 * mode, as fiddling with msrs is very expensive.
3096 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003097static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003098{
Avi Kivity26bb0982009-09-07 11:14:12 +03003099 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003100
Eddie Donga75beee2007-05-17 18:55:15 +03003101 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003102#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003103 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003104 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003105 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003106 move_msr_up(vmx, index, save_nmsrs++);
3107 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003108 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003109 move_msr_up(vmx, index, save_nmsrs++);
3110 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003111 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003112 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003113 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003114 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003115 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003116 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003117 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003118 * if efer.sce is enabled.
3119 */
Brian Gerst8c065852010-07-17 09:03:26 -04003120 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003121 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003122 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003123 }
Eddie Donga75beee2007-05-17 18:55:15 +03003124#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003125 index = __find_msr_index(vmx, MSR_EFER);
3126 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003127 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003128
Avi Kivity26bb0982009-09-07 11:14:12 +03003129 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003130
Yang Zhang8d146952013-01-25 10:18:50 +08003131 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003132 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003133}
3134
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003135static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003137 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003139 if (is_guest_mode(vcpu) &&
3140 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3141 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3142
3143 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144}
3145
3146/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003147 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003149static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003151 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003152 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003153 * We're here if L1 chose not to trap WRMSR to TSC. According
3154 * to the spec, this should set L1's TSC; The offset that L1
3155 * set for L2 remains unchanged, and still needs to be added
3156 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003157 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003158 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003159 /* recalculate vmcs02.TSC_OFFSET: */
3160 vmcs12 = get_vmcs12(vcpu);
3161 vmcs_write64(TSC_OFFSET, offset +
3162 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3163 vmcs12->tsc_offset : 0));
3164 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003165 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3166 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003167 vmcs_write64(TSC_OFFSET, offset);
3168 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169}
3170
Nadav Har'El801d3422011-05-25 23:02:23 +03003171/*
3172 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3173 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3174 * all guests if the "nested" module option is off, and can also be disabled
3175 * for a single guest by disabling its VMX cpuid bit.
3176 */
3177static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3178{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003179 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003180}
3181
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003183 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3184 * returned for the various VMX controls MSRs when nested VMX is enabled.
3185 * The same values should also be used to verify that vmcs12 control fields are
3186 * valid during nested entry from L1 to L2.
3187 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3188 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3189 * bit in the high half is on if the corresponding bit in the control field
3190 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003191 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003192static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003193{
Paolo Bonzini13893092018-02-26 13:40:09 +01003194 if (!nested) {
3195 memset(msrs, 0, sizeof(*msrs));
3196 return;
3197 }
3198
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003199 /*
3200 * Note that as a general rule, the high half of the MSRs (bits in
3201 * the control fields which may be 1) should be initialized by the
3202 * intersection of the underlying hardware's MSR (i.e., features which
3203 * can be supported) and the list of features we want to expose -
3204 * because they are known to be properly supported in our code.
3205 * Also, usually, the low half of the MSRs (bits which must be 1) can
3206 * be set to 0, meaning that L1 may turn off any of these bits. The
3207 * reason is that if one of these bits is necessary, it will appear
3208 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3209 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003210 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003211 * These rules have exceptions below.
3212 */
3213
3214 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003215 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->pinbased_ctls_low,
3217 msrs->pinbased_ctls_high);
3218 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003219 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003220 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003221 PIN_BASED_EXT_INTR_MASK |
3222 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003223 PIN_BASED_VIRTUAL_NMIS |
3224 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003225 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003226 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003227 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003228
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003229 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003230 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003231 msrs->exit_ctls_low,
3232 msrs->exit_ctls_high);
3233 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003234 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003235
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003236 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003237#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003238 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003240 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003242 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003243 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003244 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3245
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003246 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003247 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003248
Jan Kiszka2996fca2014-06-16 13:59:43 +02003249 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003250 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003251
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003252 /* entry controls */
3253 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003254 msrs->entry_ctls_low,
3255 msrs->entry_ctls_high);
3256 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003257 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003258 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003259#ifdef CONFIG_X86_64
3260 VM_ENTRY_IA32E_MODE |
3261#endif
3262 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003263 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003264 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003265 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003266 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003267
Jan Kiszka2996fca2014-06-16 13:59:43 +02003268 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003269 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003270
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003271 /* cpu-based controls */
3272 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003273 msrs->procbased_ctls_low,
3274 msrs->procbased_ctls_high);
3275 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003276 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003277 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003278 CPU_BASED_VIRTUAL_INTR_PENDING |
3279 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003280 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3281 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3282 CPU_BASED_CR3_STORE_EXITING |
3283#ifdef CONFIG_X86_64
3284 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3285#endif
3286 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003287 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3288 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3289 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3290 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003291 /*
3292 * We can allow some features even when not supported by the
3293 * hardware. For example, L1 can specify an MSR bitmap - and we
3294 * can use it to avoid exits to L1 - even when L0 runs L2
3295 * without MSR bitmaps.
3296 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003298 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003299 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003300
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003301 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003302 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003303 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3304
Paolo Bonzini80154d72017-08-24 13:55:35 +02003305 /*
3306 * secondary cpu-based controls. Do not include those that
3307 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3308 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003309 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 msrs->secondary_ctls_low,
3311 msrs->secondary_ctls_high);
3312 msrs->secondary_ctls_low = 0;
3313 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003314 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003315 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003316 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003317 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003318 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003319 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003320
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003321 if (enable_ept) {
3322 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003323 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003324 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003326 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003327 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003329 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003330 msrs->ept_caps &= vmx_capability.ept;
3331 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003332 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3333 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003334 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003336 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003337 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003338 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003339 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003340
Bandan Das27c42a12017-08-03 15:54:42 -04003341 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003342 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003343 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003344 /*
3345 * Advertise EPTP switching unconditionally
3346 * since we emulate it
3347 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003348 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003349 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003350 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003351 }
3352
Paolo Bonzinief697a72016-03-18 16:58:38 +01003353 /*
3354 * Old versions of KVM use the single-context version without
3355 * checking for support, so declare that it is supported even
3356 * though it is treated as global context. The alternative is
3357 * not failing the single-context invvpid, and it is worse.
3358 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003359 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003360 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003361 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003362 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003363 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003364 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003365
Radim Krčmář0790ec12015-03-17 14:02:32 +01003366 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003367 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003368 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3369
Jan Kiszkac18911a2013-03-13 16:06:41 +01003370 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003371 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003372 msrs->misc_low,
3373 msrs->misc_high);
3374 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3375 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003376 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003377 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003378 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003379 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003380
3381 /*
3382 * This MSR reports some information about VMX support. We
3383 * should return information about the VMX we emulate for the
3384 * guest, and the VMCS structure we give it - not about the
3385 * VMX support of the underlying hardware.
3386 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003387 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003388 VMCS12_REVISION |
3389 VMX_BASIC_TRUE_CTLS |
3390 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3391 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3392
3393 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003394 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003395
3396 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003397 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003398 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3399 * We picked the standard core2 setting.
3400 */
3401#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3402#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003403 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3404 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003405
3406 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3408 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003409
3410 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003411 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003412}
3413
David Matlack38991522016-11-29 18:14:08 -08003414/*
3415 * if fixed0[i] == 1: val[i] must be 1
3416 * if fixed1[i] == 0: val[i] must be 0
3417 */
3418static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3419{
3420 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003421}
3422
3423static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3424{
David Matlack38991522016-11-29 18:14:08 -08003425 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003426}
3427
3428static inline u64 vmx_control_msr(u32 low, u32 high)
3429{
3430 return low | ((u64)high << 32);
3431}
3432
David Matlack62cc6b9d2016-11-29 18:14:07 -08003433static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3434{
3435 superset &= mask;
3436 subset &= mask;
3437
3438 return (superset | subset) == superset;
3439}
3440
3441static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3442{
3443 const u64 feature_and_reserved =
3444 /* feature (except bit 48; see below) */
3445 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3446 /* reserved */
3447 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003448 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003449
3450 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3451 return -EINVAL;
3452
3453 /*
3454 * KVM does not emulate a version of VMX that constrains physical
3455 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3456 */
3457 if (data & BIT_ULL(48))
3458 return -EINVAL;
3459
3460 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3461 vmx_basic_vmcs_revision_id(data))
3462 return -EINVAL;
3463
3464 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3465 return -EINVAL;
3466
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003468 return 0;
3469}
3470
3471static int
3472vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3473{
3474 u64 supported;
3475 u32 *lowp, *highp;
3476
3477 switch (msr_index) {
3478 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003479 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3480 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003481 break;
3482 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003483 lowp = &vmx->nested.msrs.procbased_ctls_low;
3484 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003485 break;
3486 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003487 lowp = &vmx->nested.msrs.exit_ctls_low;
3488 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003489 break;
3490 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003491 lowp = &vmx->nested.msrs.entry_ctls_low;
3492 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003493 break;
3494 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003495 lowp = &vmx->nested.msrs.secondary_ctls_low;
3496 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003497 break;
3498 default:
3499 BUG();
3500 }
3501
3502 supported = vmx_control_msr(*lowp, *highp);
3503
3504 /* Check must-be-1 bits are still 1. */
3505 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3506 return -EINVAL;
3507
3508 /* Check must-be-0 bits are still 0. */
3509 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3510 return -EINVAL;
3511
3512 *lowp = data;
3513 *highp = data >> 32;
3514 return 0;
3515}
3516
3517static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3518{
3519 const u64 feature_and_reserved_bits =
3520 /* feature */
3521 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3522 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3523 /* reserved */
3524 GENMASK_ULL(13, 9) | BIT_ULL(31);
3525 u64 vmx_misc;
3526
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003527 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3528 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003529
3530 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3531 return -EINVAL;
3532
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003534 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3535 vmx_misc_preemption_timer_rate(data) !=
3536 vmx_misc_preemption_timer_rate(vmx_misc))
3537 return -EINVAL;
3538
3539 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3540 return -EINVAL;
3541
3542 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3543 return -EINVAL;
3544
3545 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3546 return -EINVAL;
3547
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 vmx->nested.msrs.misc_low = data;
3549 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003550
3551 /*
3552 * If L1 has read-only VM-exit information fields, use the
3553 * less permissive vmx_vmwrite_bitmap to specify write
3554 * permissions for the shadow VMCS.
3555 */
3556 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3557 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3558
David Matlack62cc6b9d2016-11-29 18:14:07 -08003559 return 0;
3560}
3561
3562static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3563{
3564 u64 vmx_ept_vpid_cap;
3565
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003566 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3567 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003568
3569 /* Every bit is either reserved or a feature bit. */
3570 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3571 return -EINVAL;
3572
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003573 vmx->nested.msrs.ept_caps = data;
3574 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003575 return 0;
3576}
3577
3578static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3579{
3580 u64 *msr;
3581
3582 switch (msr_index) {
3583 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003584 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003585 break;
3586 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003587 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003588 break;
3589 default:
3590 BUG();
3591 }
3592
3593 /*
3594 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3595 * must be 1 in the restored value.
3596 */
3597 if (!is_bitwise_subset(data, *msr, -1ULL))
3598 return -EINVAL;
3599
3600 *msr = data;
3601 return 0;
3602}
3603
3604/*
3605 * Called when userspace is restoring VMX MSRs.
3606 *
3607 * Returns 0 on success, non-0 otherwise.
3608 */
3609static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3610{
3611 struct vcpu_vmx *vmx = to_vmx(vcpu);
3612
Jim Mattsona943ac52018-05-29 09:11:32 -07003613 /*
3614 * Don't allow changes to the VMX capability MSRs while the vCPU
3615 * is in VMX operation.
3616 */
3617 if (vmx->nested.vmxon)
3618 return -EBUSY;
3619
David Matlack62cc6b9d2016-11-29 18:14:07 -08003620 switch (msr_index) {
3621 case MSR_IA32_VMX_BASIC:
3622 return vmx_restore_vmx_basic(vmx, data);
3623 case MSR_IA32_VMX_PINBASED_CTLS:
3624 case MSR_IA32_VMX_PROCBASED_CTLS:
3625 case MSR_IA32_VMX_EXIT_CTLS:
3626 case MSR_IA32_VMX_ENTRY_CTLS:
3627 /*
3628 * The "non-true" VMX capability MSRs are generated from the
3629 * "true" MSRs, so we do not support restoring them directly.
3630 *
3631 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3632 * should restore the "true" MSRs with the must-be-1 bits
3633 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3634 * DEFAULT SETTINGS".
3635 */
3636 return -EINVAL;
3637 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3638 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3639 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3640 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3641 case MSR_IA32_VMX_PROCBASED_CTLS2:
3642 return vmx_restore_control_msr(vmx, msr_index, data);
3643 case MSR_IA32_VMX_MISC:
3644 return vmx_restore_vmx_misc(vmx, data);
3645 case MSR_IA32_VMX_CR0_FIXED0:
3646 case MSR_IA32_VMX_CR4_FIXED0:
3647 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3648 case MSR_IA32_VMX_CR0_FIXED1:
3649 case MSR_IA32_VMX_CR4_FIXED1:
3650 /*
3651 * These MSRs are generated based on the vCPU's CPUID, so we
3652 * do not support restoring them directly.
3653 */
3654 return -EINVAL;
3655 case MSR_IA32_VMX_EPT_VPID_CAP:
3656 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3657 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003659 return 0;
3660 default:
3661 /*
3662 * The rest of the VMX capability MSRs do not support restore.
3663 */
3664 return -EINVAL;
3665 }
3666}
3667
Jan Kiszkacae50132014-01-04 18:47:22 +01003668/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003669static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003670{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003671 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003672 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003674 break;
3675 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3676 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003677 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->pinbased_ctls_low,
3679 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003680 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3681 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003682 break;
3683 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3684 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003685 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 msrs->procbased_ctls_low,
3687 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003688 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3689 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003690 break;
3691 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3692 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003693 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 msrs->exit_ctls_low,
3695 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003696 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3697 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003698 break;
3699 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3700 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003701 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 msrs->entry_ctls_low,
3703 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003704 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3705 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003706 break;
3707 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003708 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003709 msrs->misc_low,
3710 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003711 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003712 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003713 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003714 break;
3715 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003716 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717 break;
3718 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003719 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003720 break;
3721 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003722 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003723 break;
3724 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003725 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003726 break;
3727 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003728 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003729 msrs->secondary_ctls_low,
3730 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003731 break;
3732 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003733 *pdata = msrs->ept_caps |
3734 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003735 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003736 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003737 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003738 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003739 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003740 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003741 }
3742
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003743 return 0;
3744}
3745
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003746static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3747 uint64_t val)
3748{
3749 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3750
3751 return !(val & ~valid_bits);
3752}
3753
Tom Lendacky801e4592018-02-21 13:39:51 -06003754static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3755{
Paolo Bonzini13893092018-02-26 13:40:09 +01003756 switch (msr->index) {
3757 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3758 if (!nested)
3759 return 1;
3760 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3761 default:
3762 return 1;
3763 }
3764
3765 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003766}
3767
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003768/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769 * Reads an msr value (of 'msr_index') into 'pdata'.
3770 * Returns 0 on success, non-0 otherwise.
3771 * Assumes vcpu_load() was already called.
3772 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003773static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003775 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003776 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003778 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003779#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003781 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 break;
3783 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003784 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003785 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003786 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003787 vmx_load_host_state(vmx);
3788 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003789 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003790#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003792 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003793 case MSR_IA32_SPEC_CTRL:
3794 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003795 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3796 return 1;
3797
3798 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3799 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003800 case MSR_IA32_ARCH_CAPABILITIES:
3801 if (!msr_info->host_initiated &&
3802 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3803 return 1;
3804 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3805 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003807 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808 break;
3809 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003810 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811 break;
3812 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003813 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003815 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003816 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003817 (!msr_info->host_initiated &&
3818 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003819 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003820 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003821 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003822 case MSR_IA32_MCG_EXT_CTL:
3823 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003824 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003825 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003826 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003827 msr_info->data = vcpu->arch.mcg_ext_ctl;
3828 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003829 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003830 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003831 break;
3832 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3833 if (!nested_vmx_allowed(vcpu))
3834 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003835 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3836 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003837 case MSR_IA32_XSS:
3838 if (!vmx_xsaves_supported())
3839 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003840 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003841 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003842 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003843 if (!msr_info->host_initiated &&
3844 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003845 return 1;
3846 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003848 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003849 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003850 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003851 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003853 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 }
3855
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856 return 0;
3857}
3858
Jan Kiszkacae50132014-01-04 18:47:22 +01003859static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3860
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861/*
3862 * Writes msr value into into the appropriate "register".
3863 * Returns 0 on success, non-0 otherwise.
3864 * Assumes vcpu_load() was already called.
3865 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003866static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003868 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003869 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003870 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003871 u32 msr_index = msr_info->index;
3872 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003873
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003875 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003876 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003877 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003878#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003880 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 vmcs_writel(GUEST_FS_BASE, data);
3882 break;
3883 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003884 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885 vmcs_writel(GUEST_GS_BASE, data);
3886 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003887 case MSR_KERNEL_GS_BASE:
3888 vmx_load_host_state(vmx);
3889 vmx->msr_guest_kernel_gs_base = data;
3890 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891#endif
3892 case MSR_IA32_SYSENTER_CS:
3893 vmcs_write32(GUEST_SYSENTER_CS, data);
3894 break;
3895 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003896 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897 break;
3898 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003899 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003901 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003902 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003903 (!msr_info->host_initiated &&
3904 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003905 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003906 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003907 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003909 vmcs_write64(GUEST_BNDCFGS, data);
3910 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003911 case MSR_IA32_SPEC_CTRL:
3912 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003913 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3914 return 1;
3915
3916 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003917 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003918 return 1;
3919
3920 vmx->spec_ctrl = data;
3921
3922 if (!data)
3923 break;
3924
3925 /*
3926 * For non-nested:
3927 * When it's written (to non-zero) for the first time, pass
3928 * it through.
3929 *
3930 * For nested:
3931 * The handling of the MSR bitmap for L2 guests is done in
3932 * nested_vmx_merge_msr_bitmap. We should not touch the
3933 * vmcs02.msr_bitmap here since it gets completely overwritten
3934 * in the merging. We update the vmcs01 here for L1 as well
3935 * since it will end up touching the MSR anyway now.
3936 */
3937 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3938 MSR_IA32_SPEC_CTRL,
3939 MSR_TYPE_RW);
3940 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003941 case MSR_IA32_PRED_CMD:
3942 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003943 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3944 return 1;
3945
3946 if (data & ~PRED_CMD_IBPB)
3947 return 1;
3948
3949 if (!data)
3950 break;
3951
3952 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3953
3954 /*
3955 * For non-nested:
3956 * When it's written (to non-zero) for the first time, pass
3957 * it through.
3958 *
3959 * For nested:
3960 * The handling of the MSR bitmap for L2 guests is done in
3961 * nested_vmx_merge_msr_bitmap. We should not touch the
3962 * vmcs02.msr_bitmap here since it gets completely overwritten
3963 * in the merging.
3964 */
3965 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3966 MSR_TYPE_W);
3967 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003968 case MSR_IA32_ARCH_CAPABILITIES:
3969 if (!msr_info->host_initiated)
3970 return 1;
3971 vmx->arch_capabilities = data;
3972 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003973 case MSR_IA32_CR_PAT:
3974 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003975 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3976 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003977 vmcs_write64(GUEST_IA32_PAT, data);
3978 vcpu->arch.pat = data;
3979 break;
3980 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003981 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003982 break;
Will Auldba904632012-11-29 12:42:50 -08003983 case MSR_IA32_TSC_ADJUST:
3984 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003985 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003986 case MSR_IA32_MCG_EXT_CTL:
3987 if ((!msr_info->host_initiated &&
3988 !(to_vmx(vcpu)->msr_ia32_feature_control &
3989 FEATURE_CONTROL_LMCE)) ||
3990 (data & ~MCG_EXT_CTL_LMCE_EN))
3991 return 1;
3992 vcpu->arch.mcg_ext_ctl = data;
3993 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003994 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003995 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003996 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003997 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3998 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003999 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004000 if (msr_info->host_initiated && data == 0)
4001 vmx_leave_nested(vcpu);
4002 break;
4003 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004004 if (!msr_info->host_initiated)
4005 return 1; /* they are read-only */
4006 if (!nested_vmx_allowed(vcpu))
4007 return 1;
4008 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004009 case MSR_IA32_XSS:
4010 if (!vmx_xsaves_supported())
4011 return 1;
4012 /*
4013 * The only supported bit as of Skylake is bit 8, but
4014 * it is not supported on KVM.
4015 */
4016 if (data != 0)
4017 return 1;
4018 vcpu->arch.ia32_xss = data;
4019 if (vcpu->arch.ia32_xss != host_xss)
4020 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4021 vcpu->arch.ia32_xss, host_xss);
4022 else
4023 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4024 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004025 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004026 if (!msr_info->host_initiated &&
4027 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004028 return 1;
4029 /* Check reserved bit, higher 32 bits should be zero */
4030 if ((data >> 32) != 0)
4031 return 1;
4032 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004033 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004034 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004035 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004036 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004037 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004038 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4039 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004040 ret = kvm_set_shared_msr(msr->index, msr->data,
4041 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004042 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004043 if (ret)
4044 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004045 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004046 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004048 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 }
4050
Eddie Dong2cc51562007-05-21 07:28:09 +03004051 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052}
4053
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004054static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004056 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4057 switch (reg) {
4058 case VCPU_REGS_RSP:
4059 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4060 break;
4061 case VCPU_REGS_RIP:
4062 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4063 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004064 case VCPU_EXREG_PDPTR:
4065 if (enable_ept)
4066 ept_save_pdptrs(vcpu);
4067 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004068 default:
4069 break;
4070 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071}
4072
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073static __init int cpu_has_kvm_support(void)
4074{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004075 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076}
4077
4078static __init int vmx_disabled_by_bios(void)
4079{
4080 u64 msr;
4081
4082 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004083 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004084 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004085 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4086 && tboot_enabled())
4087 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004088 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004089 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004090 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004091 && !tboot_enabled()) {
4092 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004093 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004094 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004095 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004096 /* launched w/o TXT and VMX disabled */
4097 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4098 && !tboot_enabled())
4099 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004100 }
4101
4102 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103}
4104
Dongxiao Xu7725b892010-05-11 18:29:38 +08004105static void kvm_cpu_vmxon(u64 addr)
4106{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004107 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004108 intel_pt_handle_vmx(1);
4109
Dongxiao Xu7725b892010-05-11 18:29:38 +08004110 asm volatile (ASM_VMX_VMXON_RAX
4111 : : "a"(&addr), "m"(addr)
4112 : "memory", "cc");
4113}
4114
Radim Krčmář13a34e02014-08-28 15:13:03 +02004115static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116{
4117 int cpu = raw_smp_processor_id();
4118 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004119 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004121 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004122 return -EBUSY;
4123
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004124 /*
4125 * This can happen if we hot-added a CPU but failed to allocate
4126 * VP assist page for it.
4127 */
4128 if (static_branch_unlikely(&enable_evmcs) &&
4129 !hv_get_vp_assist_page(cpu))
4130 return -EFAULT;
4131
Nadav Har'Eld462b812011-05-24 15:26:10 +03004132 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004133 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4134 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004135
4136 /*
4137 * Now we can enable the vmclear operation in kdump
4138 * since the loaded_vmcss_on_cpu list on this cpu
4139 * has been initialized.
4140 *
4141 * Though the cpu is not in VMX operation now, there
4142 * is no problem to enable the vmclear operation
4143 * for the loaded_vmcss_on_cpu list is empty!
4144 */
4145 crash_enable_local_vmclear(cpu);
4146
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004148
4149 test_bits = FEATURE_CONTROL_LOCKED;
4150 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4151 if (tboot_enabled())
4152 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4153
4154 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004156 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4157 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004158 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004159 if (enable_ept)
4160 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004161
4162 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163}
4164
Nadav Har'Eld462b812011-05-24 15:26:10 +03004165static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004166{
4167 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004168 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004169
Nadav Har'Eld462b812011-05-24 15:26:10 +03004170 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4171 loaded_vmcss_on_cpu_link)
4172 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004173}
4174
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004175
4176/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4177 * tricks.
4178 */
4179static void kvm_cpu_vmxoff(void)
4180{
4181 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004182
4183 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004184 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004185}
4186
Radim Krčmář13a34e02014-08-28 15:13:03 +02004187static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004189 vmclear_local_loaded_vmcss();
4190 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004191}
4192
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004193static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004194 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195{
4196 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004197 u32 ctl = ctl_min | ctl_opt;
4198
4199 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4200
4201 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4202 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4203
4204 /* Ensure minimum (required) set of control bits are supported. */
4205 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004206 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004207
4208 *result = ctl;
4209 return 0;
4210}
4211
Avi Kivity110312c2010-12-21 12:54:20 +02004212static __init bool allow_1_setting(u32 msr, u32 ctl)
4213{
4214 u32 vmx_msr_low, vmx_msr_high;
4215
4216 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4217 return vmx_msr_high & ctl;
4218}
4219
Yang, Sheng002c7f72007-07-31 14:23:01 +03004220static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004221{
4222 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004223 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004224 u32 _pin_based_exec_control = 0;
4225 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004226 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004227 u32 _vmexit_control = 0;
4228 u32 _vmentry_control = 0;
4229
Paolo Bonzini13893092018-02-26 13:40:09 +01004230 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304231 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004232#ifdef CONFIG_X86_64
4233 CPU_BASED_CR8_LOAD_EXITING |
4234 CPU_BASED_CR8_STORE_EXITING |
4235#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004236 CPU_BASED_CR3_LOAD_EXITING |
4237 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004238 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004239 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004240 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004241 CPU_BASED_MWAIT_EXITING |
4242 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004243 CPU_BASED_INVLPG_EXITING |
4244 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004245
Sheng Yangf78e0e22007-10-29 09:40:42 +08004246 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004247 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004248 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004249 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4250 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004251 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004252#ifdef CONFIG_X86_64
4253 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4254 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4255 ~CPU_BASED_CR8_STORE_EXITING;
4256#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004257 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004258 min2 = 0;
4259 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004260 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004261 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004262 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004263 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004264 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004265 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004266 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004267 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004268 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004269 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004270 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004271 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004272 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004273 SECONDARY_EXEC_RDSEED_EXITING |
4274 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004275 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004276 SECONDARY_EXEC_TSC_SCALING |
4277 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004278 if (adjust_vmx_controls(min2, opt2,
4279 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004280 &_cpu_based_2nd_exec_control) < 0)
4281 return -EIO;
4282 }
4283#ifndef CONFIG_X86_64
4284 if (!(_cpu_based_2nd_exec_control &
4285 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4286 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4287#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004288
4289 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4290 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004291 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004292 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4293 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004294
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004295 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4296 &vmx_capability.ept, &vmx_capability.vpid);
4297
Sheng Yangd56f5462008-04-25 10:13:16 +08004298 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004299 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4300 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004301 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4302 CPU_BASED_CR3_STORE_EXITING |
4303 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004304 } else if (vmx_capability.ept) {
4305 vmx_capability.ept = 0;
4306 pr_warn_once("EPT CAP should not exist if not support "
4307 "1-setting enable EPT VM-execution control\n");
4308 }
4309 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4310 vmx_capability.vpid) {
4311 vmx_capability.vpid = 0;
4312 pr_warn_once("VPID CAP should not exist if not support "
4313 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004314 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004315
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004316 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004317#ifdef CONFIG_X86_64
4318 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4319#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004320 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004321 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004322 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4323 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004324 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004325
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004326 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4327 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4328 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004329 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4330 &_pin_based_exec_control) < 0)
4331 return -EIO;
4332
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004333 if (cpu_has_broken_vmx_preemption_timer())
4334 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004335 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004336 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004337 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4338
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004339 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004340 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004341 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4342 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004343 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004345 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004346
4347 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4348 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004349 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004350
4351#ifdef CONFIG_X86_64
4352 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4353 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004354 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004355#endif
4356
4357 /* Require Write-Back (WB) memory type for VMCS accesses. */
4358 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004359 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004360
Yang, Sheng002c7f72007-07-31 14:23:01 +03004361 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004362 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004363 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004364
4365 /* KVM supports Enlightened VMCS v1 only */
4366 if (static_branch_unlikely(&enable_evmcs))
4367 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4368 else
4369 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004370
Yang, Sheng002c7f72007-07-31 14:23:01 +03004371 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4372 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004373 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004374 vmcs_conf->vmexit_ctrl = _vmexit_control;
4375 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004376
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004377 if (static_branch_unlikely(&enable_evmcs))
4378 evmcs_sanitize_exec_ctrls(vmcs_conf);
4379
Avi Kivity110312c2010-12-21 12:54:20 +02004380 cpu_has_load_ia32_efer =
4381 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4382 VM_ENTRY_LOAD_IA32_EFER)
4383 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4384 VM_EXIT_LOAD_IA32_EFER);
4385
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004386 cpu_has_load_perf_global_ctrl =
4387 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4388 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4389 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4390 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4391
4392 /*
4393 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004394 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004395 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4396 *
4397 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4398 *
4399 * AAK155 (model 26)
4400 * AAP115 (model 30)
4401 * AAT100 (model 37)
4402 * BC86,AAY89,BD102 (model 44)
4403 * BA97 (model 46)
4404 *
4405 */
4406 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4407 switch (boot_cpu_data.x86_model) {
4408 case 26:
4409 case 30:
4410 case 37:
4411 case 44:
4412 case 46:
4413 cpu_has_load_perf_global_ctrl = false;
4414 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4415 "does not work properly. Using workaround\n");
4416 break;
4417 default:
4418 break;
4419 }
4420 }
4421
Borislav Petkov782511b2016-04-04 22:25:03 +02004422 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004423 rdmsrl(MSR_IA32_XSS, host_xss);
4424
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004425 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004426}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427
4428static struct vmcs *alloc_vmcs_cpu(int cpu)
4429{
4430 int node = cpu_to_node(cpu);
4431 struct page *pages;
4432 struct vmcs *vmcs;
4433
Vlastimil Babka96db8002015-09-08 15:03:50 -07004434 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435 if (!pages)
4436 return NULL;
4437 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004438 memset(vmcs, 0, vmcs_config.size);
4439 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440 return vmcs;
4441}
4442
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443static void free_vmcs(struct vmcs *vmcs)
4444{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004445 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446}
4447
Nadav Har'Eld462b812011-05-24 15:26:10 +03004448/*
4449 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4450 */
4451static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4452{
4453 if (!loaded_vmcs->vmcs)
4454 return;
4455 loaded_vmcs_clear(loaded_vmcs);
4456 free_vmcs(loaded_vmcs->vmcs);
4457 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004458 if (loaded_vmcs->msr_bitmap)
4459 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004460 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004461}
4462
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004463static struct vmcs *alloc_vmcs(void)
4464{
4465 return alloc_vmcs_cpu(raw_smp_processor_id());
4466}
4467
4468static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4469{
4470 loaded_vmcs->vmcs = alloc_vmcs();
4471 if (!loaded_vmcs->vmcs)
4472 return -ENOMEM;
4473
4474 loaded_vmcs->shadow_vmcs = NULL;
4475 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004476
4477 if (cpu_has_vmx_msr_bitmap()) {
4478 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4479 if (!loaded_vmcs->msr_bitmap)
4480 goto out_vmcs;
4481 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004482
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004483 if (IS_ENABLED(CONFIG_HYPERV) &&
4484 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004485 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4486 struct hv_enlightened_vmcs *evmcs =
4487 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4488
4489 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4490 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004491 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004492 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004493
4494out_vmcs:
4495 free_loaded_vmcs(loaded_vmcs);
4496 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004497}
4498
Sam Ravnborg39959582007-06-01 00:47:13 -07004499static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500{
4501 int cpu;
4502
Zachary Amsden3230bb42009-09-29 11:38:37 -10004503 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004505 per_cpu(vmxarea, cpu) = NULL;
4506 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004507}
4508
Jim Mattsond37f4262017-12-22 12:12:16 -08004509enum vmcs_field_width {
4510 VMCS_FIELD_WIDTH_U16 = 0,
4511 VMCS_FIELD_WIDTH_U64 = 1,
4512 VMCS_FIELD_WIDTH_U32 = 2,
4513 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004514};
4515
Jim Mattsond37f4262017-12-22 12:12:16 -08004516static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004517{
4518 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004519 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004520 return (field >> 13) & 0x3 ;
4521}
4522
4523static inline int vmcs_field_readonly(unsigned long field)
4524{
4525 return (((field >> 10) & 0x3) == 1);
4526}
4527
Bandan Dasfe2b2012014-04-21 15:20:14 -04004528static void init_vmcs_shadow_fields(void)
4529{
4530 int i, j;
4531
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004532 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4533 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004534 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004535 (i + 1 == max_shadow_read_only_fields ||
4536 shadow_read_only_fields[i + 1] != field + 1))
4537 pr_err("Missing field from shadow_read_only_field %x\n",
4538 field + 1);
4539
4540 clear_bit(field, vmx_vmread_bitmap);
4541#ifdef CONFIG_X86_64
4542 if (field & 1)
4543 continue;
4544#endif
4545 if (j < i)
4546 shadow_read_only_fields[j] = field;
4547 j++;
4548 }
4549 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004550
4551 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004552 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004553 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004554 (i + 1 == max_shadow_read_write_fields ||
4555 shadow_read_write_fields[i + 1] != field + 1))
4556 pr_err("Missing field from shadow_read_write_field %x\n",
4557 field + 1);
4558
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004559 /*
4560 * PML and the preemption timer can be emulated, but the
4561 * processor cannot vmwrite to fields that don't exist
4562 * on bare metal.
4563 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004564 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004565 case GUEST_PML_INDEX:
4566 if (!cpu_has_vmx_pml())
4567 continue;
4568 break;
4569 case VMX_PREEMPTION_TIMER_VALUE:
4570 if (!cpu_has_vmx_preemption_timer())
4571 continue;
4572 break;
4573 case GUEST_INTR_STATUS:
4574 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004575 continue;
4576 break;
4577 default:
4578 break;
4579 }
4580
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004581 clear_bit(field, vmx_vmwrite_bitmap);
4582 clear_bit(field, vmx_vmread_bitmap);
4583#ifdef CONFIG_X86_64
4584 if (field & 1)
4585 continue;
4586#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004587 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004588 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004589 j++;
4590 }
4591 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004592}
4593
Avi Kivity6aa8b732006-12-10 02:21:36 -08004594static __init int alloc_kvm_area(void)
4595{
4596 int cpu;
4597
Zachary Amsden3230bb42009-09-29 11:38:37 -10004598 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004599 struct vmcs *vmcs;
4600
4601 vmcs = alloc_vmcs_cpu(cpu);
4602 if (!vmcs) {
4603 free_kvm_area();
4604 return -ENOMEM;
4605 }
4606
4607 per_cpu(vmxarea, cpu) = vmcs;
4608 }
4609 return 0;
4610}
4611
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004612static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004613 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004615 if (!emulate_invalid_guest_state) {
4616 /*
4617 * CS and SS RPL should be equal during guest entry according
4618 * to VMX spec, but in reality it is not always so. Since vcpu
4619 * is in the middle of the transition from real mode to
4620 * protected mode it is safe to assume that RPL 0 is a good
4621 * default value.
4622 */
4623 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004624 save->selector &= ~SEGMENT_RPL_MASK;
4625 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004626 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004627 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004628 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629}
4630
4631static void enter_pmode(struct kvm_vcpu *vcpu)
4632{
4633 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004634 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635
Gleb Natapovd99e4152012-12-20 16:57:45 +02004636 /*
4637 * Update real mode segment cache. It may be not up-to-date if sement
4638 * register was written while vcpu was in a guest mode.
4639 */
4640 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4641 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4642 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4643 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4644 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4645 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4646
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004647 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004648
Avi Kivity2fb92db2011-04-27 19:42:18 +03004649 vmx_segment_cache_clear(vmx);
4650
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004651 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004652
4653 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004654 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4655 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656 vmcs_writel(GUEST_RFLAGS, flags);
4657
Rusty Russell66aee912007-07-17 23:34:16 +10004658 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4659 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660
4661 update_exception_bitmap(vcpu);
4662
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004663 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4664 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4665 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4666 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4667 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4668 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004669}
4670
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004671static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004672{
Mathias Krause772e0312012-08-30 01:30:19 +02004673 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004674 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675
Gleb Natapovd99e4152012-12-20 16:57:45 +02004676 var.dpl = 0x3;
4677 if (seg == VCPU_SREG_CS)
4678 var.type = 0x3;
4679
4680 if (!emulate_invalid_guest_state) {
4681 var.selector = var.base >> 4;
4682 var.base = var.base & 0xffff0;
4683 var.limit = 0xffff;
4684 var.g = 0;
4685 var.db = 0;
4686 var.present = 1;
4687 var.s = 1;
4688 var.l = 0;
4689 var.unusable = 0;
4690 var.type = 0x3;
4691 var.avl = 0;
4692 if (save->base & 0xf)
4693 printk_once(KERN_WARNING "kvm: segment base is not "
4694 "paragraph aligned when entering "
4695 "protected mode (seg=%d)", seg);
4696 }
4697
4698 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004699 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004700 vmcs_write32(sf->limit, var.limit);
4701 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702}
4703
4704static void enter_rmode(struct kvm_vcpu *vcpu)
4705{
4706 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004707 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004708 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004710 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4711 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4712 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4713 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4714 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004715 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4716 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004717
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004718 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719
Gleb Natapov776e58e2011-03-13 12:34:27 +02004720 /*
4721 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004722 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004723 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004724 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004725 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4726 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004727
Avi Kivity2fb92db2011-04-27 19:42:18 +03004728 vmx_segment_cache_clear(vmx);
4729
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004730 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4733
4734 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004735 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004736
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004737 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738
4739 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004740 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741 update_exception_bitmap(vcpu);
4742
Gleb Natapovd99e4152012-12-20 16:57:45 +02004743 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4744 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4745 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4746 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4747 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4748 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004749
Eddie Dong8668a3c2007-10-10 14:26:45 +08004750 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751}
4752
Amit Shah401d10d2009-02-20 22:53:37 +05304753static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4754{
4755 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004756 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4757
4758 if (!msr)
4759 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304760
Avi Kivity44ea2b12009-09-06 15:55:37 +03004761 /*
4762 * Force kernel_gs_base reloading before EFER changes, as control
4763 * of this msr depends on is_long_mode().
4764 */
4765 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004766 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304767 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004768 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304769 msr->data = efer;
4770 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004771 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304772
4773 msr->data = efer & ~EFER_LME;
4774 }
4775 setup_msrs(vmx);
4776}
4777
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004778#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004779
4780static void enter_lmode(struct kvm_vcpu *vcpu)
4781{
4782 u32 guest_tr_ar;
4783
Avi Kivity2fb92db2011-04-27 19:42:18 +03004784 vmx_segment_cache_clear(to_vmx(vcpu));
4785
Avi Kivity6aa8b732006-12-10 02:21:36 -08004786 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004787 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004788 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4789 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004791 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4792 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793 }
Avi Kivityda38f432010-07-06 11:30:49 +03004794 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795}
4796
4797static void exit_lmode(struct kvm_vcpu *vcpu)
4798{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004799 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004800 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801}
4802
4803#endif
4804
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004805static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4806 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004807{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004808 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004809 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4810 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004811 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004812 } else {
4813 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004814 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004815}
4816
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004817static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004818{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004819 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004820}
4821
Avi Kivitye8467fd2009-12-29 18:43:06 +02004822static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4823{
4824 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4825
4826 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4827 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4828}
4829
Avi Kivityaff48ba2010-12-05 18:56:11 +02004830static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4831{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004832 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004833 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4834 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4835}
4836
Anthony Liguori25c4c272007-04-27 09:29:21 +03004837static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004838{
Avi Kivityfc78f512009-12-07 12:16:48 +02004839 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4840
4841 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4842 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004843}
4844
Sheng Yang14394422008-04-28 12:24:45 +08004845static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4846{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004847 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4848
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004849 if (!test_bit(VCPU_EXREG_PDPTR,
4850 (unsigned long *)&vcpu->arch.regs_dirty))
4851 return;
4852
Sheng Yang14394422008-04-28 12:24:45 +08004853 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004854 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4855 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4856 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4857 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004858 }
4859}
4860
Avi Kivity8f5d5492009-05-31 18:41:29 +03004861static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4862{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004863 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4864
Avi Kivity8f5d5492009-05-31 18:41:29 +03004865 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004866 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4867 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4868 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4869 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004870 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004871
4872 __set_bit(VCPU_EXREG_PDPTR,
4873 (unsigned long *)&vcpu->arch.regs_avail);
4874 __set_bit(VCPU_EXREG_PDPTR,
4875 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004876}
4877
David Matlack38991522016-11-29 18:14:08 -08004878static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4879{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004880 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4881 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004882 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4883
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004884 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004885 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4886 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4887 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4888
4889 return fixed_bits_valid(val, fixed0, fixed1);
4890}
4891
4892static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4893{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004894 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4895 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004896
4897 return fixed_bits_valid(val, fixed0, fixed1);
4898}
4899
4900static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4901{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004902 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4903 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004904
4905 return fixed_bits_valid(val, fixed0, fixed1);
4906}
4907
4908/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4909#define nested_guest_cr4_valid nested_cr4_valid
4910#define nested_host_cr4_valid nested_cr4_valid
4911
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004912static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004913
4914static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4915 unsigned long cr0,
4916 struct kvm_vcpu *vcpu)
4917{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004918 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4919 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004920 if (!(cr0 & X86_CR0_PG)) {
4921 /* From paging/starting to nonpaging */
4922 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004923 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004924 (CPU_BASED_CR3_LOAD_EXITING |
4925 CPU_BASED_CR3_STORE_EXITING));
4926 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004927 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004928 } else if (!is_paging(vcpu)) {
4929 /* From nonpaging to paging */
4930 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004931 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004932 ~(CPU_BASED_CR3_LOAD_EXITING |
4933 CPU_BASED_CR3_STORE_EXITING));
4934 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004935 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004936 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004937
4938 if (!(cr0 & X86_CR0_WP))
4939 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004940}
4941
Avi Kivity6aa8b732006-12-10 02:21:36 -08004942static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4943{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004945 unsigned long hw_cr0;
4946
Gleb Natapov50378782013-02-04 16:00:28 +02004947 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004948 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004949 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004950 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004951 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004952
Gleb Natapov218e7632013-01-21 15:36:45 +02004953 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4954 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955
Gleb Natapov218e7632013-01-21 15:36:45 +02004956 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4957 enter_rmode(vcpu);
4958 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004960#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004961 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004962 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004964 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965 exit_lmode(vcpu);
4966 }
4967#endif
4968
Sean Christophersonb4d18512018-03-05 12:04:40 -08004969 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004970 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4971
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004973 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004974 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004975
4976 /* depends on vcpu->arch.cr0 to be set to a new value */
4977 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978}
4979
Yu Zhang855feb62017-08-24 20:27:55 +08004980static int get_ept_level(struct kvm_vcpu *vcpu)
4981{
4982 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4983 return 5;
4984 return 4;
4985}
4986
Peter Feiner995f00a2017-06-30 17:26:32 -07004987static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004988{
Yu Zhang855feb62017-08-24 20:27:55 +08004989 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004990
Yu Zhang855feb62017-08-24 20:27:55 +08004991 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004992
Peter Feiner995f00a2017-06-30 17:26:32 -07004993 if (enable_ept_ad_bits &&
4994 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004995 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004996 eptp |= (root_hpa & PAGE_MASK);
4997
4998 return eptp;
4999}
5000
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5002{
Sheng Yang14394422008-04-28 12:24:45 +08005003 unsigned long guest_cr3;
5004 u64 eptp;
5005
5006 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005007 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005008 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005009 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005010 if (enable_unrestricted_guest || is_paging(vcpu) ||
5011 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005012 guest_cr3 = kvm_read_cr3(vcpu);
5013 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005014 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005015 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005016 }
5017
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005018 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005019 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020}
5021
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005022static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005024 /*
5025 * Pass through host's Machine Check Enable value to hw_cr4, which
5026 * is in force while we are in guest mode. Do not let guests control
5027 * this bit, even if host CR4.MCE == 0.
5028 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005029 unsigned long hw_cr4;
5030
5031 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5032 if (enable_unrestricted_guest)
5033 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5034 else if (to_vmx(vcpu)->rmode.vm86_active)
5035 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5036 else
5037 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005038
Sean Christopherson64f7a112018-04-30 10:01:06 -07005039 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5040 if (cr4 & X86_CR4_UMIP) {
5041 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005042 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005043 hw_cr4 &= ~X86_CR4_UMIP;
5044 } else if (!is_guest_mode(vcpu) ||
5045 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5046 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5047 SECONDARY_EXEC_DESC);
5048 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005049
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005050 if (cr4 & X86_CR4_VMXE) {
5051 /*
5052 * To use VMXON (and later other VMX instructions), a guest
5053 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5054 * So basically the check on whether to allow nested VMX
5055 * is here.
5056 */
5057 if (!nested_vmx_allowed(vcpu))
5058 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005059 }
David Matlack38991522016-11-29 18:14:08 -08005060
5061 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005062 return 1;
5063
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005064 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005065
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005066 if (!enable_unrestricted_guest) {
5067 if (enable_ept) {
5068 if (!is_paging(vcpu)) {
5069 hw_cr4 &= ~X86_CR4_PAE;
5070 hw_cr4 |= X86_CR4_PSE;
5071 } else if (!(cr4 & X86_CR4_PAE)) {
5072 hw_cr4 &= ~X86_CR4_PAE;
5073 }
5074 }
5075
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005076 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005077 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5078 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5079 * to be manually disabled when guest switches to non-paging
5080 * mode.
5081 *
5082 * If !enable_unrestricted_guest, the CPU is always running
5083 * with CR0.PG=1 and CR4 needs to be modified.
5084 * If enable_unrestricted_guest, the CPU automatically
5085 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005086 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005087 if (!is_paging(vcpu))
5088 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5089 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005090
Sheng Yang14394422008-04-28 12:24:45 +08005091 vmcs_writel(CR4_READ_SHADOW, cr4);
5092 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005093 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094}
5095
Avi Kivity6aa8b732006-12-10 02:21:36 -08005096static void vmx_get_segment(struct kvm_vcpu *vcpu,
5097 struct kvm_segment *var, int seg)
5098{
Avi Kivitya9179492011-01-03 14:28:52 +02005099 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100 u32 ar;
5101
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005102 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005103 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005104 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005105 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005106 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005107 var->base = vmx_read_guest_seg_base(vmx, seg);
5108 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5109 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005110 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005111 var->base = vmx_read_guest_seg_base(vmx, seg);
5112 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5113 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5114 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005115 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005116 var->type = ar & 15;
5117 var->s = (ar >> 4) & 1;
5118 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005119 /*
5120 * Some userspaces do not preserve unusable property. Since usable
5121 * segment has to be present according to VMX spec we can use present
5122 * property to amend userspace bug by making unusable segment always
5123 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5124 * segment as unusable.
5125 */
5126 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005127 var->avl = (ar >> 12) & 1;
5128 var->l = (ar >> 13) & 1;
5129 var->db = (ar >> 14) & 1;
5130 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005131}
5132
Avi Kivitya9179492011-01-03 14:28:52 +02005133static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5134{
Avi Kivitya9179492011-01-03 14:28:52 +02005135 struct kvm_segment s;
5136
5137 if (to_vmx(vcpu)->rmode.vm86_active) {
5138 vmx_get_segment(vcpu, &s, seg);
5139 return s.base;
5140 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005141 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005142}
5143
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005144static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005145{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005146 struct vcpu_vmx *vmx = to_vmx(vcpu);
5147
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005148 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005149 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005150 else {
5151 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005152 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005153 }
Avi Kivity69c73022011-03-07 15:26:44 +02005154}
5155
Avi Kivity653e3102007-05-07 10:55:37 +03005156static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005158 u32 ar;
5159
Avi Kivityf0495f92012-06-07 17:06:10 +03005160 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 ar = 1 << 16;
5162 else {
5163 ar = var->type & 15;
5164 ar |= (var->s & 1) << 4;
5165 ar |= (var->dpl & 3) << 5;
5166 ar |= (var->present & 1) << 7;
5167 ar |= (var->avl & 1) << 12;
5168 ar |= (var->l & 1) << 13;
5169 ar |= (var->db & 1) << 14;
5170 ar |= (var->g & 1) << 15;
5171 }
Avi Kivity653e3102007-05-07 10:55:37 +03005172
5173 return ar;
5174}
5175
5176static void vmx_set_segment(struct kvm_vcpu *vcpu,
5177 struct kvm_segment *var, int seg)
5178{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005179 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005180 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005181
Avi Kivity2fb92db2011-04-27 19:42:18 +03005182 vmx_segment_cache_clear(vmx);
5183
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005184 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5185 vmx->rmode.segs[seg] = *var;
5186 if (seg == VCPU_SREG_TR)
5187 vmcs_write16(sf->selector, var->selector);
5188 else if (var->s)
5189 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005190 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005191 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005192
Avi Kivity653e3102007-05-07 10:55:37 +03005193 vmcs_writel(sf->base, var->base);
5194 vmcs_write32(sf->limit, var->limit);
5195 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005196
5197 /*
5198 * Fix the "Accessed" bit in AR field of segment registers for older
5199 * qemu binaries.
5200 * IA32 arch specifies that at the time of processor reset the
5201 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005202 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005203 * state vmexit when "unrestricted guest" mode is turned on.
5204 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5205 * tree. Newer qemu binaries with that qemu fix would not need this
5206 * kvm hack.
5207 */
5208 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005209 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005210
Gleb Natapovf924d662012-12-12 19:10:55 +02005211 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005212
5213out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005214 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215}
5216
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5218{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005219 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005220
5221 *db = (ar >> 14) & 1;
5222 *l = (ar >> 13) & 1;
5223}
5224
Gleb Natapov89a27f42010-02-16 10:51:48 +02005225static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005226{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005227 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5228 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229}
5230
Gleb Natapov89a27f42010-02-16 10:51:48 +02005231static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005232{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005233 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5234 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235}
5236
Gleb Natapov89a27f42010-02-16 10:51:48 +02005237static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005239 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5240 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005241}
5242
Gleb Natapov89a27f42010-02-16 10:51:48 +02005243static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005244{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005245 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5246 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005247}
5248
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005249static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5250{
5251 struct kvm_segment var;
5252 u32 ar;
5253
5254 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005255 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005256 if (seg == VCPU_SREG_CS)
5257 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005258 ar = vmx_segment_access_rights(&var);
5259
5260 if (var.base != (var.selector << 4))
5261 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005262 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005263 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005264 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005265 return false;
5266
5267 return true;
5268}
5269
5270static bool code_segment_valid(struct kvm_vcpu *vcpu)
5271{
5272 struct kvm_segment cs;
5273 unsigned int cs_rpl;
5274
5275 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005276 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005277
Avi Kivity1872a3f2009-01-04 23:26:52 +02005278 if (cs.unusable)
5279 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005280 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005281 return false;
5282 if (!cs.s)
5283 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005284 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005285 if (cs.dpl > cs_rpl)
5286 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005287 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005288 if (cs.dpl != cs_rpl)
5289 return false;
5290 }
5291 if (!cs.present)
5292 return false;
5293
5294 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5295 return true;
5296}
5297
5298static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5299{
5300 struct kvm_segment ss;
5301 unsigned int ss_rpl;
5302
5303 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005304 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005305
Avi Kivity1872a3f2009-01-04 23:26:52 +02005306 if (ss.unusable)
5307 return true;
5308 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005309 return false;
5310 if (!ss.s)
5311 return false;
5312 if (ss.dpl != ss_rpl) /* DPL != RPL */
5313 return false;
5314 if (!ss.present)
5315 return false;
5316
5317 return true;
5318}
5319
5320static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5321{
5322 struct kvm_segment var;
5323 unsigned int rpl;
5324
5325 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005326 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005327
Avi Kivity1872a3f2009-01-04 23:26:52 +02005328 if (var.unusable)
5329 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005330 if (!var.s)
5331 return false;
5332 if (!var.present)
5333 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005334 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005335 if (var.dpl < rpl) /* DPL < RPL */
5336 return false;
5337 }
5338
5339 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5340 * rights flags
5341 */
5342 return true;
5343}
5344
5345static bool tr_valid(struct kvm_vcpu *vcpu)
5346{
5347 struct kvm_segment tr;
5348
5349 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5350
Avi Kivity1872a3f2009-01-04 23:26:52 +02005351 if (tr.unusable)
5352 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005353 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005354 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005355 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005356 return false;
5357 if (!tr.present)
5358 return false;
5359
5360 return true;
5361}
5362
5363static bool ldtr_valid(struct kvm_vcpu *vcpu)
5364{
5365 struct kvm_segment ldtr;
5366
5367 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5368
Avi Kivity1872a3f2009-01-04 23:26:52 +02005369 if (ldtr.unusable)
5370 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005371 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005372 return false;
5373 if (ldtr.type != 2)
5374 return false;
5375 if (!ldtr.present)
5376 return false;
5377
5378 return true;
5379}
5380
5381static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5382{
5383 struct kvm_segment cs, ss;
5384
5385 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5386 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5387
Nadav Amitb32a9912015-03-29 16:33:04 +03005388 return ((cs.selector & SEGMENT_RPL_MASK) ==
5389 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005390}
5391
5392/*
5393 * Check if guest state is valid. Returns true if valid, false if
5394 * not.
5395 * We assume that registers are always usable
5396 */
5397static bool guest_state_valid(struct kvm_vcpu *vcpu)
5398{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005399 if (enable_unrestricted_guest)
5400 return true;
5401
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005402 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005403 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005404 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5405 return false;
5406 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5407 return false;
5408 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5409 return false;
5410 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5411 return false;
5412 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5413 return false;
5414 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5415 return false;
5416 } else {
5417 /* protected mode guest state checks */
5418 if (!cs_ss_rpl_check(vcpu))
5419 return false;
5420 if (!code_segment_valid(vcpu))
5421 return false;
5422 if (!stack_segment_valid(vcpu))
5423 return false;
5424 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5425 return false;
5426 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5427 return false;
5428 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5429 return false;
5430 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5431 return false;
5432 if (!tr_valid(vcpu))
5433 return false;
5434 if (!ldtr_valid(vcpu))
5435 return false;
5436 }
5437 /* TODO:
5438 * - Add checks on RIP
5439 * - Add checks on RFLAGS
5440 */
5441
5442 return true;
5443}
5444
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005445static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5446{
5447 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5448}
5449
Mike Dayd77c26f2007-10-08 09:02:08 -04005450static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005452 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005453 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005454 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005455
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005456 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005457 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005458 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5459 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005460 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005461 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005462 r = kvm_write_guest_page(kvm, fn++, &data,
5463 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005464 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005465 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005466 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5467 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005468 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005469 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5470 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005471 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005472 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005473 r = kvm_write_guest_page(kvm, fn, &data,
5474 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5475 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005476out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005477 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005478 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005479}
5480
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005481static int init_rmode_identity_map(struct kvm *kvm)
5482{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005483 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005484 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005485 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005486 u32 tmp;
5487
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005488 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005489 mutex_lock(&kvm->slots_lock);
5490
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005491 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005492 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005493
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005494 if (!kvm_vmx->ept_identity_map_addr)
5495 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5496 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005497
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005498 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005499 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005500 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005501 goto out2;
5502
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005503 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005504 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5505 if (r < 0)
5506 goto out;
5507 /* Set up identity-mapping pagetable for EPT in real mode */
5508 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5509 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5510 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5511 r = kvm_write_guest_page(kvm, identity_map_pfn,
5512 &tmp, i * sizeof(tmp), sizeof(tmp));
5513 if (r < 0)
5514 goto out;
5515 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005516 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005517
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005518out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005519 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005520
5521out2:
5522 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005523 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005524}
5525
Avi Kivity6aa8b732006-12-10 02:21:36 -08005526static void seg_setup(int seg)
5527{
Mathias Krause772e0312012-08-30 01:30:19 +02005528 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005529 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005530
5531 vmcs_write16(sf->selector, 0);
5532 vmcs_writel(sf->base, 0);
5533 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005534 ar = 0x93;
5535 if (seg == VCPU_SREG_CS)
5536 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005537
5538 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005539}
5540
Sheng Yangf78e0e22007-10-29 09:40:42 +08005541static int alloc_apic_access_page(struct kvm *kvm)
5542{
Xiao Guangrong44841412012-09-07 14:14:20 +08005543 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005544 int r = 0;
5545
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005546 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005547 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005548 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005549 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5550 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005551 if (r)
5552 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005553
Tang Chen73a6d942014-09-11 13:38:00 +08005554 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005555 if (is_error_page(page)) {
5556 r = -EFAULT;
5557 goto out;
5558 }
5559
Tang Chenc24ae0d2014-09-24 15:57:58 +08005560 /*
5561 * Do not pin the page in memory, so that memory hot-unplug
5562 * is able to migrate it.
5563 */
5564 put_page(page);
5565 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005566out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005567 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005568 return r;
5569}
5570
Wanpeng Li991e7a02015-09-16 17:30:05 +08005571static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005572{
5573 int vpid;
5574
Avi Kivity919818a2009-03-23 18:01:29 +02005575 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005576 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005577 spin_lock(&vmx_vpid_lock);
5578 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005579 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005580 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005581 else
5582 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005583 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005584 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005585}
5586
Wanpeng Li991e7a02015-09-16 17:30:05 +08005587static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005588{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005589 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005590 return;
5591 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005592 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005593 spin_unlock(&vmx_vpid_lock);
5594}
5595
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005596static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5597 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005598{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005599 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005600
5601 if (!cpu_has_vmx_msr_bitmap())
5602 return;
5603
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005604 if (static_branch_unlikely(&enable_evmcs))
5605 evmcs_touch_msr_bitmap();
5606
Sheng Yang25c5f222008-03-28 13:18:56 +08005607 /*
5608 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5609 * have the write-low and read-high bitmap offsets the wrong way round.
5610 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5611 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005612 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005613 if (type & MSR_TYPE_R)
5614 /* read-low */
5615 __clear_bit(msr, msr_bitmap + 0x000 / f);
5616
5617 if (type & MSR_TYPE_W)
5618 /* write-low */
5619 __clear_bit(msr, msr_bitmap + 0x800 / f);
5620
Sheng Yang25c5f222008-03-28 13:18:56 +08005621 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5622 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005623 if (type & MSR_TYPE_R)
5624 /* read-high */
5625 __clear_bit(msr, msr_bitmap + 0x400 / f);
5626
5627 if (type & MSR_TYPE_W)
5628 /* write-high */
5629 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5630
5631 }
5632}
5633
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005634static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5635 u32 msr, int type)
5636{
5637 int f = sizeof(unsigned long);
5638
5639 if (!cpu_has_vmx_msr_bitmap())
5640 return;
5641
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005642 if (static_branch_unlikely(&enable_evmcs))
5643 evmcs_touch_msr_bitmap();
5644
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005645 /*
5646 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5647 * have the write-low and read-high bitmap offsets the wrong way round.
5648 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5649 */
5650 if (msr <= 0x1fff) {
5651 if (type & MSR_TYPE_R)
5652 /* read-low */
5653 __set_bit(msr, msr_bitmap + 0x000 / f);
5654
5655 if (type & MSR_TYPE_W)
5656 /* write-low */
5657 __set_bit(msr, msr_bitmap + 0x800 / f);
5658
5659 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5660 msr &= 0x1fff;
5661 if (type & MSR_TYPE_R)
5662 /* read-high */
5663 __set_bit(msr, msr_bitmap + 0x400 / f);
5664
5665 if (type & MSR_TYPE_W)
5666 /* write-high */
5667 __set_bit(msr, msr_bitmap + 0xc00 / f);
5668
5669 }
5670}
5671
5672static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5673 u32 msr, int type, bool value)
5674{
5675 if (value)
5676 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5677 else
5678 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5679}
5680
Wincy Vanf2b93282015-02-03 23:56:03 +08005681/*
5682 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5683 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5684 */
5685static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5686 unsigned long *msr_bitmap_nested,
5687 u32 msr, int type)
5688{
5689 int f = sizeof(unsigned long);
5690
Wincy Vanf2b93282015-02-03 23:56:03 +08005691 /*
5692 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5693 * have the write-low and read-high bitmap offsets the wrong way round.
5694 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5695 */
5696 if (msr <= 0x1fff) {
5697 if (type & MSR_TYPE_R &&
5698 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5699 /* read-low */
5700 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5701
5702 if (type & MSR_TYPE_W &&
5703 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5704 /* write-low */
5705 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5706
5707 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5708 msr &= 0x1fff;
5709 if (type & MSR_TYPE_R &&
5710 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5711 /* read-high */
5712 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5713
5714 if (type & MSR_TYPE_W &&
5715 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5716 /* write-high */
5717 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5718
5719 }
5720}
5721
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005722static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005723{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005724 u8 mode = 0;
5725
5726 if (cpu_has_secondary_exec_ctrls() &&
5727 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5728 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5729 mode |= MSR_BITMAP_MODE_X2APIC;
5730 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5731 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5732 }
5733
5734 if (is_long_mode(vcpu))
5735 mode |= MSR_BITMAP_MODE_LM;
5736
5737 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005738}
5739
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005740#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5741
5742static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5743 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005744{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005745 int msr;
5746
5747 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5748 unsigned word = msr / BITS_PER_LONG;
5749 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5750 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005751 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005752
5753 if (mode & MSR_BITMAP_MODE_X2APIC) {
5754 /*
5755 * TPR reads and writes can be virtualized even if virtual interrupt
5756 * delivery is not in use.
5757 */
5758 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5759 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5760 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5761 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5762 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5763 }
5764 }
5765}
5766
5767static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5768{
5769 struct vcpu_vmx *vmx = to_vmx(vcpu);
5770 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5771 u8 mode = vmx_msr_bitmap_mode(vcpu);
5772 u8 changed = mode ^ vmx->msr_bitmap_mode;
5773
5774 if (!changed)
5775 return;
5776
5777 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5778 !(mode & MSR_BITMAP_MODE_LM));
5779
5780 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5781 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5782
5783 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005784}
5785
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005786static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005787{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005788 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005789}
5790
David Matlackc9f04402017-08-01 14:00:40 -07005791static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5792{
5793 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5794 gfn_t gfn;
5795
5796 /*
5797 * Don't need to mark the APIC access page dirty; it is never
5798 * written to by the CPU during APIC virtualization.
5799 */
5800
5801 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5802 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5803 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5804 }
5805
5806 if (nested_cpu_has_posted_intr(vmcs12)) {
5807 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5808 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5809 }
5810}
5811
5812
David Hildenbrand6342c502017-01-25 11:58:58 +01005813static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005814{
5815 struct vcpu_vmx *vmx = to_vmx(vcpu);
5816 int max_irr;
5817 void *vapic_page;
5818 u16 status;
5819
David Matlackc9f04402017-08-01 14:00:40 -07005820 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5821 return;
Wincy Van705699a2015-02-03 23:58:17 +08005822
David Matlackc9f04402017-08-01 14:00:40 -07005823 vmx->nested.pi_pending = false;
5824 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5825 return;
Wincy Van705699a2015-02-03 23:58:17 +08005826
David Matlackc9f04402017-08-01 14:00:40 -07005827 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5828 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005829 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005830 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5831 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005832 kunmap(vmx->nested.virtual_apic_page);
5833
5834 status = vmcs_read16(GUEST_INTR_STATUS);
5835 if ((u8)max_irr > ((u8)status & 0xff)) {
5836 status &= ~0xff;
5837 status |= (u8)max_irr;
5838 vmcs_write16(GUEST_INTR_STATUS, status);
5839 }
5840 }
David Matlackc9f04402017-08-01 14:00:40 -07005841
5842 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005843}
5844
Wincy Van06a55242017-04-28 13:13:59 +08005845static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5846 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005847{
5848#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005849 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5850
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005851 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005852 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005853 * The vector of interrupt to be delivered to vcpu had
5854 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005855 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005856 * Following cases will be reached in this block, and
5857 * we always send a notification event in all cases as
5858 * explained below.
5859 *
5860 * Case 1: vcpu keeps in non-root mode. Sending a
5861 * notification event posts the interrupt to vcpu.
5862 *
5863 * Case 2: vcpu exits to root mode and is still
5864 * runnable. PIR will be synced to vIRR before the
5865 * next vcpu entry. Sending a notification event in
5866 * this case has no effect, as vcpu is not in root
5867 * mode.
5868 *
5869 * Case 3: vcpu exits to root mode and is blocked.
5870 * vcpu_block() has already synced PIR to vIRR and
5871 * never blocks vcpu if vIRR is not cleared. Therefore,
5872 * a blocked vcpu here does not wait for any requested
5873 * interrupts in PIR, and sending a notification event
5874 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005875 */
Feng Wu28b835d2015-09-18 22:29:54 +08005876
Wincy Van06a55242017-04-28 13:13:59 +08005877 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005878 return true;
5879 }
5880#endif
5881 return false;
5882}
5883
Wincy Van705699a2015-02-03 23:58:17 +08005884static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5885 int vector)
5886{
5887 struct vcpu_vmx *vmx = to_vmx(vcpu);
5888
5889 if (is_guest_mode(vcpu) &&
5890 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005891 /*
5892 * If a posted intr is not recognized by hardware,
5893 * we will accomplish it in the next vmentry.
5894 */
5895 vmx->nested.pi_pending = true;
5896 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005897 /* the PIR and ON have been set by L1. */
5898 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5899 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005900 return 0;
5901 }
5902 return -1;
5903}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005904/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005905 * Send interrupt to vcpu via posted interrupt way.
5906 * 1. If target vcpu is running(non-root mode), send posted interrupt
5907 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5908 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5909 * interrupt from PIR in next vmentry.
5910 */
5911static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5912{
5913 struct vcpu_vmx *vmx = to_vmx(vcpu);
5914 int r;
5915
Wincy Van705699a2015-02-03 23:58:17 +08005916 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5917 if (!r)
5918 return;
5919
Yang Zhanga20ed542013-04-11 19:25:15 +08005920 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5921 return;
5922
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005923 /* If a previous notification has sent the IPI, nothing to do. */
5924 if (pi_test_and_set_on(&vmx->pi_desc))
5925 return;
5926
Wincy Van06a55242017-04-28 13:13:59 +08005927 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005928 kvm_vcpu_kick(vcpu);
5929}
5930
Avi Kivity6aa8b732006-12-10 02:21:36 -08005931/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005932 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5933 * will not change in the lifetime of the guest.
5934 * Note that host-state that does change is set elsewhere. E.g., host-state
5935 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5936 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005937static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005938{
5939 u32 low32, high32;
5940 unsigned long tmpl;
5941 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005942 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005943
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005944 cr0 = read_cr0();
5945 WARN_ON(cr0 & X86_CR0_TS);
5946 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005947
5948 /*
5949 * Save the most likely value for this task's CR3 in the VMCS.
5950 * We can't use __get_current_cr3_fast() because we're not atomic.
5951 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005952 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005953 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005954 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005955
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005956 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005957 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005958 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005959 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005960
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005961 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005962#ifdef CONFIG_X86_64
5963 /*
5964 * Load null selectors, so we can avoid reloading them in
5965 * __vmx_load_host_state(), in case userspace uses the null selectors
5966 * too (the expected case).
5967 */
5968 vmcs_write16(HOST_DS_SELECTOR, 0);
5969 vmcs_write16(HOST_ES_SELECTOR, 0);
5970#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005971 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5972 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005973#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005974 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5975 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5976
Juergen Gross87930012017-09-04 12:25:27 +02005977 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005978 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005979 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005980
Avi Kivity83287ea422012-09-16 15:10:57 +03005981 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005982
5983 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5984 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5985 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5986 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5987
5988 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5989 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5990 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5991 }
5992}
5993
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005994static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5995{
5996 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5997 if (enable_ept)
5998 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005999 if (is_guest_mode(&vmx->vcpu))
6000 vmx->vcpu.arch.cr4_guest_owned_bits &=
6001 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006002 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6003}
6004
Yang Zhang01e439b2013-04-11 19:25:12 +08006005static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6006{
6007 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6008
Andrey Smetanind62caab2015-11-10 15:36:33 +03006009 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006010 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006011
6012 if (!enable_vnmi)
6013 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6014
Yunhong Jiang64672c92016-06-13 14:19:59 -07006015 /* Enable the preemption timer dynamically */
6016 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006017 return pin_based_exec_ctrl;
6018}
6019
Andrey Smetanind62caab2015-11-10 15:36:33 +03006020static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6021{
6022 struct vcpu_vmx *vmx = to_vmx(vcpu);
6023
6024 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006025 if (cpu_has_secondary_exec_ctrls()) {
6026 if (kvm_vcpu_apicv_active(vcpu))
6027 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6028 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6029 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6030 else
6031 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6032 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6033 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6034 }
6035
6036 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006037 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006038}
6039
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006040static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6041{
6042 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006043
6044 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6045 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6046
Paolo Bonzini35754c92015-07-29 12:05:37 +02006047 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006048 exec_control &= ~CPU_BASED_TPR_SHADOW;
6049#ifdef CONFIG_X86_64
6050 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6051 CPU_BASED_CR8_LOAD_EXITING;
6052#endif
6053 }
6054 if (!enable_ept)
6055 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6056 CPU_BASED_CR3_LOAD_EXITING |
6057 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006058 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6059 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6060 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006061 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6062 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006063 return exec_control;
6064}
6065
Jim Mattson45ec3682017-08-23 16:32:04 -07006066static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006067{
Jim Mattson45ec3682017-08-23 16:32:04 -07006068 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006069 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006070}
6071
Jim Mattson75f4fc82017-08-23 16:32:03 -07006072static bool vmx_rdseed_supported(void)
6073{
6074 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006075 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006076}
6077
Paolo Bonzini80154d72017-08-24 13:55:35 +02006078static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006079{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006080 struct kvm_vcpu *vcpu = &vmx->vcpu;
6081
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006082 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006083
Paolo Bonzini80154d72017-08-24 13:55:35 +02006084 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006085 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6086 if (vmx->vpid == 0)
6087 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6088 if (!enable_ept) {
6089 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6090 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006091 /* Enable INVPCID for non-ept guests may cause performance regression. */
6092 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006093 }
6094 if (!enable_unrestricted_guest)
6095 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006096 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006097 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006098 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006099 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6100 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006101 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006102
6103 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6104 * in vmx_set_cr4. */
6105 exec_control &= ~SECONDARY_EXEC_DESC;
6106
Abel Gordonabc4fc52013-04-18 14:35:25 +03006107 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6108 (handle_vmptrld).
6109 We can NOT enable shadow_vmcs here because we don't have yet
6110 a current VMCS12
6111 */
6112 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006113
6114 if (!enable_pml)
6115 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006116
Paolo Bonzini3db13482017-08-24 14:48:03 +02006117 if (vmx_xsaves_supported()) {
6118 /* Exposing XSAVES only when XSAVE is exposed */
6119 bool xsaves_enabled =
6120 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6121 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6122
6123 if (!xsaves_enabled)
6124 exec_control &= ~SECONDARY_EXEC_XSAVES;
6125
6126 if (nested) {
6127 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006128 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006129 SECONDARY_EXEC_XSAVES;
6130 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006131 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006132 ~SECONDARY_EXEC_XSAVES;
6133 }
6134 }
6135
Paolo Bonzini80154d72017-08-24 13:55:35 +02006136 if (vmx_rdtscp_supported()) {
6137 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6138 if (!rdtscp_enabled)
6139 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6140
6141 if (nested) {
6142 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006143 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006144 SECONDARY_EXEC_RDTSCP;
6145 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006146 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006147 ~SECONDARY_EXEC_RDTSCP;
6148 }
6149 }
6150
6151 if (vmx_invpcid_supported()) {
6152 /* Exposing INVPCID only when PCID is exposed */
6153 bool invpcid_enabled =
6154 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6155 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6156
6157 if (!invpcid_enabled) {
6158 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6159 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6160 }
6161
6162 if (nested) {
6163 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006164 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006165 SECONDARY_EXEC_ENABLE_INVPCID;
6166 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006167 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006168 ~SECONDARY_EXEC_ENABLE_INVPCID;
6169 }
6170 }
6171
Jim Mattson45ec3682017-08-23 16:32:04 -07006172 if (vmx_rdrand_supported()) {
6173 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6174 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006175 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006176
6177 if (nested) {
6178 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006179 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006180 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006181 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006182 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006183 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006184 }
6185 }
6186
Jim Mattson75f4fc82017-08-23 16:32:03 -07006187 if (vmx_rdseed_supported()) {
6188 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6189 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006190 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006191
6192 if (nested) {
6193 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006194 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006195 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006196 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006197 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006198 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006199 }
6200 }
6201
Paolo Bonzini80154d72017-08-24 13:55:35 +02006202 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006203}
6204
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006205static void ept_set_mmio_spte_mask(void)
6206{
6207 /*
6208 * EPT Misconfigurations can be generated if the value of bits 2:0
6209 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006210 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006211 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6212 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006213}
6214
Wanpeng Lif53cd632014-12-02 19:14:58 +08006215#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006216/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006217 * Sets up the vmcs for emulated real mode.
6218 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006219static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006220{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006221#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006222 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006223#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006224 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006225
Abel Gordon4607c2d2013-04-18 14:35:55 +03006226 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006227 /*
6228 * At vCPU creation, "VMWRITE to any supported field
6229 * in the VMCS" is supported, so use the more
6230 * permissive vmx_vmread_bitmap to specify both read
6231 * and write permissions for the shadow VMCS.
6232 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006233 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006234 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006235 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006236 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006237 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006238
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6240
Avi Kivity6aa8b732006-12-10 02:21:36 -08006241 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006242 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006243 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006244
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006245 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006246
Dan Williamsdfa169b2016-06-02 11:17:24 -07006247 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006248 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006249 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006250 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006251 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006252
Andrey Smetanind62caab2015-11-10 15:36:33 +03006253 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006254 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6255 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6256 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6257 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6258
6259 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006260
Li RongQing0bcf2612015-12-03 13:29:34 +08006261 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006262 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006263 }
6264
Wanpeng Lib31c1142018-03-12 04:53:04 -07006265 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006266 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006267 vmx->ple_window = ple_window;
6268 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006269 }
6270
Xiao Guangrongc3707952011-07-12 03:28:04 +08006271 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6272 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006273 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6274
Avi Kivity9581d442010-10-19 16:46:55 +02006275 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6276 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006277 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006278#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006279 rdmsrl(MSR_FS_BASE, a);
6280 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6281 rdmsrl(MSR_GS_BASE, a);
6282 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6283#else
6284 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6285 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6286#endif
6287
Bandan Das2a499e42017-08-03 15:54:41 -04006288 if (cpu_has_vmx_vmfunc())
6289 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6290
Eddie Dong2cc51562007-05-21 07:28:09 +03006291 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6292 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006293 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006294 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006295 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006296
Radim Krčmář74545702015-04-27 15:11:25 +02006297 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6298 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006299
Paolo Bonzini03916db2014-07-24 14:21:57 +02006300 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006301 u32 index = vmx_msr_index[i];
6302 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006303 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006304
6305 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6306 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006307 if (wrmsr_safe(index, data_low, data_high) < 0)
6308 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006309 vmx->guest_msrs[j].index = i;
6310 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006311 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006312 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006313 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006315 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6316 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006317
6318 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319
6320 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006321 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006322
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006323 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6324 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6325
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006326 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006327
Wanpeng Lif53cd632014-12-02 19:14:58 +08006328 if (vmx_xsaves_supported())
6329 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6330
Peter Feiner4e595162016-07-07 14:49:58 -07006331 if (enable_pml) {
6332 ASSERT(vmx->pml_pg);
6333 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6334 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6335 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006336}
6337
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006338static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006339{
6340 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006341 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006342 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006343
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006344 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006345 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006346
Wanpeng Li518e7b92018-02-28 14:03:31 +08006347 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006348 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006349 kvm_set_cr8(vcpu, 0);
6350
6351 if (!init_event) {
6352 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6353 MSR_IA32_APICBASE_ENABLE;
6354 if (kvm_vcpu_is_reset_bsp(vcpu))
6355 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6356 apic_base_msr.host_initiated = true;
6357 kvm_set_apic_base(vcpu, &apic_base_msr);
6358 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006359
Avi Kivity2fb92db2011-04-27 19:42:18 +03006360 vmx_segment_cache_clear(vmx);
6361
Avi Kivity5706be02008-08-20 15:07:31 +03006362 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006363 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006364 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006365
6366 seg_setup(VCPU_SREG_DS);
6367 seg_setup(VCPU_SREG_ES);
6368 seg_setup(VCPU_SREG_FS);
6369 seg_setup(VCPU_SREG_GS);
6370 seg_setup(VCPU_SREG_SS);
6371
6372 vmcs_write16(GUEST_TR_SELECTOR, 0);
6373 vmcs_writel(GUEST_TR_BASE, 0);
6374 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6375 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6376
6377 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6378 vmcs_writel(GUEST_LDTR_BASE, 0);
6379 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6380 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6381
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006382 if (!init_event) {
6383 vmcs_write32(GUEST_SYSENTER_CS, 0);
6384 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6385 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6386 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6387 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006388
Wanpeng Lic37c2872017-11-20 14:52:21 -08006389 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006390 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006391
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006392 vmcs_writel(GUEST_GDTR_BASE, 0);
6393 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6394
6395 vmcs_writel(GUEST_IDTR_BASE, 0);
6396 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6397
Anthony Liguori443381a2010-12-06 10:53:38 -06006398 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006399 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006400 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006401 if (kvm_mpx_supported())
6402 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006403
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006404 setup_msrs(vmx);
6405
Avi Kivity6aa8b732006-12-10 02:21:36 -08006406 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6407
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006408 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006409 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006410 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006411 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006412 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006413 vmcs_write32(TPR_THRESHOLD, 0);
6414 }
6415
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006416 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417
Sheng Yang2384d2b2008-01-17 15:14:33 +08006418 if (vmx->vpid != 0)
6419 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6420
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006421 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006422 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006423 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006424 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006425 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006426
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006427 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006428
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006429 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006430 if (init_event)
6431 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006432}
6433
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006434/*
6435 * In nested virtualization, check if L1 asked to exit on external interrupts.
6436 * For most existing hypervisors, this will always return true.
6437 */
6438static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6439{
6440 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6441 PIN_BASED_EXT_INTR_MASK;
6442}
6443
Bandan Das77b0f5d2014-04-19 18:17:45 -04006444/*
6445 * In nested virtualization, check if L1 has set
6446 * VM_EXIT_ACK_INTR_ON_EXIT
6447 */
6448static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6449{
6450 return get_vmcs12(vcpu)->vm_exit_controls &
6451 VM_EXIT_ACK_INTR_ON_EXIT;
6452}
6453
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006454static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6455{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006456 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006457}
6458
Jan Kiszkac9a79532014-03-07 20:03:15 +01006459static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006460{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006461 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6462 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006463}
6464
Jan Kiszkac9a79532014-03-07 20:03:15 +01006465static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006466{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006467 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006468 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006469 enable_irq_window(vcpu);
6470 return;
6471 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006472
Paolo Bonzini47c01522016-12-19 11:44:07 +01006473 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6474 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006475}
6476
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006477static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006478{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006479 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006480 uint32_t intr;
6481 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006482
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006483 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006484
Avi Kivityfa89a812008-09-01 15:57:51 +03006485 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006486 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006487 int inc_eip = 0;
6488 if (vcpu->arch.interrupt.soft)
6489 inc_eip = vcpu->arch.event_exit_inst_len;
6490 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006491 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006492 return;
6493 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006494 intr = irq | INTR_INFO_VALID_MASK;
6495 if (vcpu->arch.interrupt.soft) {
6496 intr |= INTR_TYPE_SOFT_INTR;
6497 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6498 vmx->vcpu.arch.event_exit_inst_len);
6499 } else
6500 intr |= INTR_TYPE_EXT_INTR;
6501 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006502
6503 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006504}
6505
Sheng Yangf08864b2008-05-15 18:23:25 +08006506static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6507{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006508 struct vcpu_vmx *vmx = to_vmx(vcpu);
6509
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006510 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006511 /*
6512 * Tracking the NMI-blocked state in software is built upon
6513 * finding the next open IRQ window. This, in turn, depends on
6514 * well-behaving guests: They have to keep IRQs disabled at
6515 * least as long as the NMI handler runs. Otherwise we may
6516 * cause NMI nesting, maybe breaking the guest. But as this is
6517 * highly unlikely, we can live with the residual risk.
6518 */
6519 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6520 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6521 }
6522
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006523 ++vcpu->stat.nmi_injections;
6524 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006525
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006526 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006527 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006528 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006529 return;
6530 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006531
Sheng Yangf08864b2008-05-15 18:23:25 +08006532 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6533 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006534
6535 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006536}
6537
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006538static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6539{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006540 struct vcpu_vmx *vmx = to_vmx(vcpu);
6541 bool masked;
6542
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006543 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006544 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006545 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006546 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006547 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6548 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6549 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006550}
6551
6552static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6553{
6554 struct vcpu_vmx *vmx = to_vmx(vcpu);
6555
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006556 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006557 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6558 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6559 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6560 }
6561 } else {
6562 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6563 if (masked)
6564 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6565 GUEST_INTR_STATE_NMI);
6566 else
6567 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6568 GUEST_INTR_STATE_NMI);
6569 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006570}
6571
Jan Kiszka2505dc92013-04-14 12:12:47 +02006572static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6573{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006574 if (to_vmx(vcpu)->nested.nested_run_pending)
6575 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006576
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006577 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006578 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6579 return 0;
6580
Jan Kiszka2505dc92013-04-14 12:12:47 +02006581 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6582 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6583 | GUEST_INTR_STATE_NMI));
6584}
6585
Gleb Natapov78646122009-03-23 12:12:11 +02006586static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6587{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006588 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6589 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006590 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6591 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006592}
6593
Izik Eiduscbc94022007-10-25 00:29:55 +02006594static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6595{
6596 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006597
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006598 if (enable_unrestricted_guest)
6599 return 0;
6600
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006601 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6602 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006603 if (ret)
6604 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006605 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006606 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006607}
6608
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006609static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6610{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006611 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006612 return 0;
6613}
6614
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006615static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006616{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006617 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006618 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006619 /*
6620 * Update instruction length as we may reinject the exception
6621 * from user space while in guest debugging mode.
6622 */
6623 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6624 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006625 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006626 return false;
6627 /* fall through */
6628 case DB_VECTOR:
6629 if (vcpu->guest_debug &
6630 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6631 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006632 /* fall through */
6633 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006634 case OF_VECTOR:
6635 case BR_VECTOR:
6636 case UD_VECTOR:
6637 case DF_VECTOR:
6638 case SS_VECTOR:
6639 case GP_VECTOR:
6640 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006641 return true;
6642 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006643 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006644 return false;
6645}
6646
6647static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6648 int vec, u32 err_code)
6649{
6650 /*
6651 * Instruction with address size override prefix opcode 0x67
6652 * Cause the #SS fault with 0 error code in VM86 mode.
6653 */
6654 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6655 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6656 if (vcpu->arch.halt_request) {
6657 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006658 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006659 }
6660 return 1;
6661 }
6662 return 0;
6663 }
6664
6665 /*
6666 * Forward all other exceptions that are valid in real mode.
6667 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6668 * the required debugging infrastructure rework.
6669 */
6670 kvm_queue_exception(vcpu, vec);
6671 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006672}
6673
Andi Kleena0861c02009-06-08 17:37:09 +08006674/*
6675 * Trigger machine check on the host. We assume all the MSRs are already set up
6676 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6677 * We pass a fake environment to the machine check handler because we want
6678 * the guest to be always treated like user space, no matter what context
6679 * it used internally.
6680 */
6681static void kvm_machine_check(void)
6682{
6683#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6684 struct pt_regs regs = {
6685 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6686 .flags = X86_EFLAGS_IF,
6687 };
6688
6689 do_machine_check(&regs, 0);
6690#endif
6691}
6692
Avi Kivity851ba692009-08-24 11:10:17 +03006693static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006694{
6695 /* already handled by vcpu_run */
6696 return 1;
6697}
6698
Avi Kivity851ba692009-08-24 11:10:17 +03006699static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006700{
Avi Kivity1155f762007-11-22 11:30:47 +02006701 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006702 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006703 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006704 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006705 u32 vect_info;
6706 enum emulation_result er;
6707
Avi Kivity1155f762007-11-22 11:30:47 +02006708 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006709 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006710
Andi Kleena0861c02009-06-08 17:37:09 +08006711 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006712 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006713
Jim Mattsonef85b672016-12-12 11:01:37 -08006714 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006715 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006716
Wanpeng Li082d06e2018-04-03 16:28:48 -07006717 if (is_invalid_opcode(intr_info))
6718 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006719
Avi Kivity6aa8b732006-12-10 02:21:36 -08006720 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006721 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006722 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006723
Liran Alon9e869482018-03-12 13:12:51 +02006724 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6725 WARN_ON_ONCE(!enable_vmware_backdoor);
6726 er = emulate_instruction(vcpu,
6727 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6728 if (er == EMULATE_USER_EXIT)
6729 return 0;
6730 else if (er != EMULATE_DONE)
6731 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6732 return 1;
6733 }
6734
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006735 /*
6736 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6737 * MMIO, it is better to report an internal error.
6738 * See the comments in vmx_handle_exit.
6739 */
6740 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6741 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6742 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6743 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006744 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006745 vcpu->run->internal.data[0] = vect_info;
6746 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006747 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006748 return 0;
6749 }
6750
Avi Kivity6aa8b732006-12-10 02:21:36 -08006751 if (is_page_fault(intr_info)) {
6752 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006753 /* EPT won't cause page fault directly */
6754 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006755 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006756 }
6757
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006758 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006759
6760 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6761 return handle_rmode_exception(vcpu, ex_no, error_code);
6762
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006763 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006764 case AC_VECTOR:
6765 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6766 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006767 case DB_VECTOR:
6768 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6769 if (!(vcpu->guest_debug &
6770 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006771 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006772 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006773 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006774 skip_emulated_instruction(vcpu);
6775
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006776 kvm_queue_exception(vcpu, DB_VECTOR);
6777 return 1;
6778 }
6779 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6780 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6781 /* fall through */
6782 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006783 /*
6784 * Update instruction length as we may reinject #BP from
6785 * user space while in guest debugging mode. Reading it for
6786 * #DB as well causes no harm, it is not used in that case.
6787 */
6788 vmx->vcpu.arch.event_exit_inst_len =
6789 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006790 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006791 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006792 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6793 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006794 break;
6795 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006796 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6797 kvm_run->ex.exception = ex_no;
6798 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006799 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006801 return 0;
6802}
6803
Avi Kivity851ba692009-08-24 11:10:17 +03006804static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006805{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006806 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006807 return 1;
6808}
6809
Avi Kivity851ba692009-08-24 11:10:17 +03006810static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006811{
Avi Kivity851ba692009-08-24 11:10:17 +03006812 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006813 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006814 return 0;
6815}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006816
Avi Kivity851ba692009-08-24 11:10:17 +03006817static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006818{
He, Qingbfdaab02007-09-12 14:18:28 +08006819 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006820 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006821 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822
He, Qingbfdaab02007-09-12 14:18:28 +08006823 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006824 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006825
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006826 ++vcpu->stat.io_exits;
6827
Sean Christopherson432baf62018-03-08 08:57:26 -08006828 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006829 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006830
6831 port = exit_qualification >> 16;
6832 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006833 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006834
Sean Christophersondca7f122018-03-08 08:57:27 -08006835 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006836}
6837
Ingo Molnar102d8322007-02-19 14:37:47 +02006838static void
6839vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6840{
6841 /*
6842 * Patch in the VMCALL instruction:
6843 */
6844 hypercall[0] = 0x0f;
6845 hypercall[1] = 0x01;
6846 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006847}
6848
Guo Chao0fa06072012-06-28 15:16:19 +08006849/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006850static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6851{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006852 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006853 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6854 unsigned long orig_val = val;
6855
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006856 /*
6857 * We get here when L2 changed cr0 in a way that did not change
6858 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006859 * but did change L0 shadowed bits. So we first calculate the
6860 * effective cr0 value that L1 would like to write into the
6861 * hardware. It consists of the L2-owned bits from the new
6862 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006863 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006864 val = (val & ~vmcs12->cr0_guest_host_mask) |
6865 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6866
David Matlack38991522016-11-29 18:14:08 -08006867 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006868 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006869
6870 if (kvm_set_cr0(vcpu, val))
6871 return 1;
6872 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006873 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006874 } else {
6875 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006876 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006877 return 1;
David Matlack38991522016-11-29 18:14:08 -08006878
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006879 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006880 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006881}
6882
6883static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6884{
6885 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006886 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6887 unsigned long orig_val = val;
6888
6889 /* analogously to handle_set_cr0 */
6890 val = (val & ~vmcs12->cr4_guest_host_mask) |
6891 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6892 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006893 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006894 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006895 return 0;
6896 } else
6897 return kvm_set_cr4(vcpu, val);
6898}
6899
Paolo Bonzini0367f202016-07-12 10:44:55 +02006900static int handle_desc(struct kvm_vcpu *vcpu)
6901{
6902 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6903 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6904}
6905
Avi Kivity851ba692009-08-24 11:10:17 +03006906static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006907{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006908 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006909 int cr;
6910 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006911 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006912 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913
He, Qingbfdaab02007-09-12 14:18:28 +08006914 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006915 cr = exit_qualification & 15;
6916 reg = (exit_qualification >> 8) & 15;
6917 switch ((exit_qualification >> 4) & 3) {
6918 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006919 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006920 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006921 switch (cr) {
6922 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006923 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006924 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006925 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006926 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006927 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006928 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006930 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006931 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006932 case 8: {
6933 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006934 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006935 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006936 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006937 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006938 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006939 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006940 return ret;
6941 /*
6942 * TODO: we might be squashing a
6943 * KVM_GUESTDBG_SINGLESTEP-triggered
6944 * KVM_EXIT_DEBUG here.
6945 */
Avi Kivity851ba692009-08-24 11:10:17 +03006946 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006947 return 0;
6948 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006949 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006950 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006951 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006952 WARN_ONCE(1, "Guest should always own CR0.TS");
6953 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006954 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006955 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006956 case 1: /*mov from cr*/
6957 switch (cr) {
6958 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006959 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006960 val = kvm_read_cr3(vcpu);
6961 kvm_register_write(vcpu, reg, val);
6962 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006963 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006964 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006965 val = kvm_get_cr8(vcpu);
6966 kvm_register_write(vcpu, reg, val);
6967 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006968 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006969 }
6970 break;
6971 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006972 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006973 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006974 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006975
Kyle Huey6affcbe2016-11-29 12:40:40 -08006976 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006977 default:
6978 break;
6979 }
Avi Kivity851ba692009-08-24 11:10:17 +03006980 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006981 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006982 (int)(exit_qualification >> 4) & 3, cr);
6983 return 0;
6984}
6985
Avi Kivity851ba692009-08-24 11:10:17 +03006986static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006987{
He, Qingbfdaab02007-09-12 14:18:28 +08006988 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006989 int dr, dr7, reg;
6990
6991 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6992 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6993
6994 /* First, if DR does not exist, trigger UD */
6995 if (!kvm_require_dr(vcpu, dr))
6996 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006997
Jan Kiszkaf2483412010-01-20 18:20:20 +01006998 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006999 if (!kvm_require_cpl(vcpu, 0))
7000 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007001 dr7 = vmcs_readl(GUEST_DR7);
7002 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007003 /*
7004 * As the vm-exit takes precedence over the debug trap, we
7005 * need to emulate the latter, either for the host or the
7006 * guest debugging itself.
7007 */
7008 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007009 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007010 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007011 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007012 vcpu->run->debug.arch.exception = DB_VECTOR;
7013 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007014 return 0;
7015 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007016 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007017 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007018 kvm_queue_exception(vcpu, DB_VECTOR);
7019 return 1;
7020 }
7021 }
7022
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007023 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007024 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7025 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007026
7027 /*
7028 * No more DR vmexits; force a reload of the debug registers
7029 * and reenter on this instruction. The next vmexit will
7030 * retrieve the full state of the debug registers.
7031 */
7032 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7033 return 1;
7034 }
7035
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007036 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7037 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007038 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007039
7040 if (kvm_get_dr(vcpu, dr, &val))
7041 return 1;
7042 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007043 } else
Nadav Amit57773922014-06-18 17:19:23 +03007044 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007045 return 1;
7046
Kyle Huey6affcbe2016-11-29 12:40:40 -08007047 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048}
7049
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007050static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7051{
7052 return vcpu->arch.dr6;
7053}
7054
7055static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7056{
7057}
7058
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007059static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7060{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007061 get_debugreg(vcpu->arch.db[0], 0);
7062 get_debugreg(vcpu->arch.db[1], 1);
7063 get_debugreg(vcpu->arch.db[2], 2);
7064 get_debugreg(vcpu->arch.db[3], 3);
7065 get_debugreg(vcpu->arch.dr6, 6);
7066 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7067
7068 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007069 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007070}
7071
Gleb Natapov020df072010-04-13 10:05:23 +03007072static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7073{
7074 vmcs_writel(GUEST_DR7, val);
7075}
7076
Avi Kivity851ba692009-08-24 11:10:17 +03007077static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078{
Kyle Huey6a908b62016-11-29 12:40:37 -08007079 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080}
7081
Avi Kivity851ba692009-08-24 11:10:17 +03007082static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007083{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007084 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007085 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007087 msr_info.index = ecx;
7088 msr_info.host_initiated = false;
7089 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007090 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007091 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092 return 1;
7093 }
7094
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007095 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007096
Avi Kivity6aa8b732006-12-10 02:21:36 -08007097 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007098 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7099 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007100 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007101}
7102
Avi Kivity851ba692009-08-24 11:10:17 +03007103static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007104{
Will Auld8fe8ab42012-11-29 12:42:12 -08007105 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007106 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7107 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7108 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007109
Will Auld8fe8ab42012-11-29 12:42:12 -08007110 msr.data = data;
7111 msr.index = ecx;
7112 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007113 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007114 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007115 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116 return 1;
7117 }
7118
Avi Kivity59200272010-01-25 19:47:02 +02007119 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007120 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007121}
7122
Avi Kivity851ba692009-08-24 11:10:17 +03007123static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007124{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007125 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007126 return 1;
7127}
7128
Avi Kivity851ba692009-08-24 11:10:17 +03007129static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007131 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7132 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007133
Avi Kivity3842d132010-07-27 12:30:24 +03007134 kvm_make_request(KVM_REQ_EVENT, vcpu);
7135
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007136 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007137 return 1;
7138}
7139
Avi Kivity851ba692009-08-24 11:10:17 +03007140static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007141{
Avi Kivityd3bef152007-06-05 15:53:05 +03007142 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007143}
7144
Avi Kivity851ba692009-08-24 11:10:17 +03007145static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007146{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007147 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007148}
7149
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007150static int handle_invd(struct kvm_vcpu *vcpu)
7151{
Andre Przywara51d8b662010-12-21 11:12:02 +01007152 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007153}
7154
Avi Kivity851ba692009-08-24 11:10:17 +03007155static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007156{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007157 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007158
7159 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007160 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007161}
7162
Avi Kivityfee84b02011-11-10 14:57:25 +02007163static int handle_rdpmc(struct kvm_vcpu *vcpu)
7164{
7165 int err;
7166
7167 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007168 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007169}
7170
Avi Kivity851ba692009-08-24 11:10:17 +03007171static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007172{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007173 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007174}
7175
Dexuan Cui2acf9232010-06-10 11:27:12 +08007176static int handle_xsetbv(struct kvm_vcpu *vcpu)
7177{
7178 u64 new_bv = kvm_read_edx_eax(vcpu);
7179 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7180
7181 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007182 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007183 return 1;
7184}
7185
Wanpeng Lif53cd632014-12-02 19:14:58 +08007186static int handle_xsaves(struct kvm_vcpu *vcpu)
7187{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007188 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007189 WARN(1, "this should never happen\n");
7190 return 1;
7191}
7192
7193static int handle_xrstors(struct kvm_vcpu *vcpu)
7194{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007195 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007196 WARN(1, "this should never happen\n");
7197 return 1;
7198}
7199
Avi Kivity851ba692009-08-24 11:10:17 +03007200static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007201{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007202 if (likely(fasteoi)) {
7203 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7204 int access_type, offset;
7205
7206 access_type = exit_qualification & APIC_ACCESS_TYPE;
7207 offset = exit_qualification & APIC_ACCESS_OFFSET;
7208 /*
7209 * Sane guest uses MOV to write EOI, with written value
7210 * not cared. So make a short-circuit here by avoiding
7211 * heavy instruction emulation.
7212 */
7213 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7214 (offset == APIC_EOI)) {
7215 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007216 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007217 }
7218 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007219 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007220}
7221
Yang Zhangc7c9c562013-01-25 10:18:51 +08007222static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7223{
7224 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7225 int vector = exit_qualification & 0xff;
7226
7227 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7228 kvm_apic_set_eoi_accelerated(vcpu, vector);
7229 return 1;
7230}
7231
Yang Zhang83d4c282013-01-25 10:18:49 +08007232static int handle_apic_write(struct kvm_vcpu *vcpu)
7233{
7234 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7235 u32 offset = exit_qualification & 0xfff;
7236
7237 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7238 kvm_apic_write_nodecode(vcpu, offset);
7239 return 1;
7240}
7241
Avi Kivity851ba692009-08-24 11:10:17 +03007242static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007243{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007244 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007245 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007246 bool has_error_code = false;
7247 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007248 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007249 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007250
7251 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007252 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007253 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007254
7255 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7256
7257 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007258 if (reason == TASK_SWITCH_GATE && idt_v) {
7259 switch (type) {
7260 case INTR_TYPE_NMI_INTR:
7261 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007262 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007263 break;
7264 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007265 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007266 kvm_clear_interrupt_queue(vcpu);
7267 break;
7268 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007269 if (vmx->idt_vectoring_info &
7270 VECTORING_INFO_DELIVER_CODE_MASK) {
7271 has_error_code = true;
7272 error_code =
7273 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7274 }
7275 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007276 case INTR_TYPE_SOFT_EXCEPTION:
7277 kvm_clear_exception_queue(vcpu);
7278 break;
7279 default:
7280 break;
7281 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007282 }
Izik Eidus37817f22008-03-24 23:14:53 +02007283 tss_selector = exit_qualification;
7284
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007285 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7286 type != INTR_TYPE_EXT_INTR &&
7287 type != INTR_TYPE_NMI_INTR))
7288 skip_emulated_instruction(vcpu);
7289
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007290 if (kvm_task_switch(vcpu, tss_selector,
7291 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7292 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007293 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7294 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7295 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007296 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007297 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007298
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007299 /*
7300 * TODO: What about debug traps on tss switch?
7301 * Are we supposed to inject them and update dr6?
7302 */
7303
7304 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007305}
7306
Avi Kivity851ba692009-08-24 11:10:17 +03007307static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007308{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007309 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007310 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007311 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007312
Sheng Yangf9c617f2009-03-25 10:08:52 +08007313 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007314
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007315 /*
7316 * EPT violation happened while executing iret from NMI,
7317 * "blocked by NMI" bit has to be set before next VM entry.
7318 * There are errata that may cause this bit to not be set:
7319 * AAK134, BY25.
7320 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007321 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007322 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007323 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007324 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7325
Sheng Yang14394422008-04-28 12:24:45 +08007326 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007327 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007328
Junaid Shahid27959a42016-12-06 16:46:10 -08007329 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007330 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007331 ? PFERR_USER_MASK : 0;
7332 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007333 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007334 ? PFERR_WRITE_MASK : 0;
7335 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007336 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007337 ? PFERR_FETCH_MASK : 0;
7338 /* ept page table entry is present? */
7339 error_code |= (exit_qualification &
7340 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7341 EPT_VIOLATION_EXECUTABLE))
7342 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007343
Paolo Bonzinieebed242016-11-28 14:39:58 +01007344 error_code |= (exit_qualification & 0x100) != 0 ?
7345 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007346
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007347 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007348 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007349}
7350
Avi Kivity851ba692009-08-24 11:10:17 +03007351static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007352{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007353 gpa_t gpa;
7354
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007355 /*
7356 * A nested guest cannot optimize MMIO vmexits, because we have an
7357 * nGPA here instead of the required GPA.
7358 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007359 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007360 if (!is_guest_mode(vcpu) &&
7361 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007362 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007363 /*
7364 * Doing kvm_skip_emulated_instruction() depends on undefined
7365 * behavior: Intel's manual doesn't mandate
7366 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7367 * occurs and while on real hardware it was observed to be set,
7368 * other hypervisors (namely Hyper-V) don't set it, we end up
7369 * advancing IP with some random value. Disable fast mmio when
7370 * running nested and keep it for real hardware in hope that
7371 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7372 */
7373 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7374 return kvm_skip_emulated_instruction(vcpu);
7375 else
7376 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7377 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007378 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007379
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007380 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007381}
7382
Avi Kivity851ba692009-08-24 11:10:17 +03007383static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007384{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007385 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007386 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7387 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007388 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007389 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007390
7391 return 1;
7392}
7393
Mohammed Gamal80ced182009-09-01 12:48:18 +02007394static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007395{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007396 struct vcpu_vmx *vmx = to_vmx(vcpu);
7397 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007398 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007399 u32 cpu_exec_ctrl;
7400 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007401 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007402
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007403 /*
7404 * We should never reach the point where we are emulating L2
7405 * due to invalid guest state as that means we incorrectly
7406 * allowed a nested VMEntry with an invalid vmcs12.
7407 */
7408 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7409
Avi Kivity49e9d552010-09-19 14:34:08 +02007410 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7411 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007412
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007413 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007414 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007415 return handle_interrupt_window(&vmx->vcpu);
7416
Radim Krčmář72875d82017-04-26 22:32:19 +02007417 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007418 return 1;
7419
Liran Alon9b8ae632017-11-05 16:56:34 +02007420 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007421
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007422 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007423 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007424 ret = 0;
7425 goto out;
7426 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007427
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007428 if (err != EMULATE_DONE)
7429 goto emulation_error;
7430
7431 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7432 vcpu->arch.exception.pending)
7433 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007434
Gleb Natapov8d76c492013-05-08 18:38:44 +03007435 if (vcpu->arch.halt_request) {
7436 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007437 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007438 goto out;
7439 }
7440
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007441 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007442 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007443 if (need_resched())
7444 schedule();
7445 }
7446
Mohammed Gamal80ced182009-09-01 12:48:18 +02007447out:
7448 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007449
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007450emulation_error:
7451 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7452 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7453 vcpu->run->internal.ndata = 0;
7454 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007455}
7456
7457static void grow_ple_window(struct kvm_vcpu *vcpu)
7458{
7459 struct vcpu_vmx *vmx = to_vmx(vcpu);
7460 int old = vmx->ple_window;
7461
Babu Mogerc8e88712018-03-16 16:37:24 -04007462 vmx->ple_window = __grow_ple_window(old, ple_window,
7463 ple_window_grow,
7464 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007465
7466 if (vmx->ple_window != old)
7467 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007468
7469 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007470}
7471
7472static void shrink_ple_window(struct kvm_vcpu *vcpu)
7473{
7474 struct vcpu_vmx *vmx = to_vmx(vcpu);
7475 int old = vmx->ple_window;
7476
Babu Mogerc8e88712018-03-16 16:37:24 -04007477 vmx->ple_window = __shrink_ple_window(old, ple_window,
7478 ple_window_shrink,
7479 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007480
7481 if (vmx->ple_window != old)
7482 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007483
7484 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007485}
7486
7487/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007488 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7489 */
7490static void wakeup_handler(void)
7491{
7492 struct kvm_vcpu *vcpu;
7493 int cpu = smp_processor_id();
7494
7495 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7496 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7497 blocked_vcpu_list) {
7498 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7499
7500 if (pi_test_on(pi_desc) == 1)
7501 kvm_vcpu_kick(vcpu);
7502 }
7503 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7504}
7505
Peng Haoe01bca22018-04-07 05:47:32 +08007506static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007507{
7508 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7509 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7510 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7511 0ull, VMX_EPT_EXECUTABLE_MASK,
7512 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007513 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007514
7515 ept_set_mmio_spte_mask();
7516 kvm_enable_tdp();
7517}
7518
Tiejun Chenf2c76482014-10-28 10:14:47 +08007519static __init int hardware_setup(void)
7520{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007521 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007522
7523 rdmsrl_safe(MSR_EFER, &host_efer);
7524
7525 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7526 kvm_define_shared_msr(i, vmx_msr_index[i]);
7527
Radim Krčmář23611332016-09-29 22:41:33 +02007528 for (i = 0; i < VMX_BITMAP_NR; i++) {
7529 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7530 if (!vmx_bitmap[i])
7531 goto out;
7532 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007533
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007534 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7535 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7536
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007537 if (setup_vmcs_config(&vmcs_config) < 0) {
7538 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007539 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007540 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007541
7542 if (boot_cpu_has(X86_FEATURE_NX))
7543 kvm_enable_efer_bits(EFER_NX);
7544
Wanpeng Li08d839c2017-03-23 05:30:08 -07007545 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7546 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007547 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007548
Tiejun Chenf2c76482014-10-28 10:14:47 +08007549 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007550 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007551 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007552 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007553 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007554
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007555 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007556 enable_ept_ad_bits = 0;
7557
Wanpeng Li8ad81822017-10-09 15:51:53 -07007558 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007559 enable_unrestricted_guest = 0;
7560
Paolo Bonziniad15a292015-01-30 16:18:49 +01007561 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007562 flexpriority_enabled = 0;
7563
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007564 if (!cpu_has_virtual_nmis())
7565 enable_vnmi = 0;
7566
Paolo Bonziniad15a292015-01-30 16:18:49 +01007567 /*
7568 * set_apic_access_page_addr() is used to reload apic access
7569 * page upon invalidation. No need to do anything if not
7570 * using the APIC_ACCESS_ADDR VMCS field.
7571 */
7572 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007573 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007574
7575 if (!cpu_has_vmx_tpr_shadow())
7576 kvm_x86_ops->update_cr8_intercept = NULL;
7577
7578 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7579 kvm_disable_largepages();
7580
Wanpeng Li0f107682017-09-28 18:06:24 -07007581 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007582 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007583 ple_window = 0;
7584 ple_window_grow = 0;
7585 ple_window_max = 0;
7586 ple_window_shrink = 0;
7587 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007588
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007589 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007590 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007591 kvm_x86_ops->sync_pir_to_irr = NULL;
7592 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007593
Haozhong Zhang64903d62015-10-20 15:39:09 +08007594 if (cpu_has_vmx_tsc_scaling()) {
7595 kvm_has_tsc_control = true;
7596 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7597 kvm_tsc_scaling_ratio_frac_bits = 48;
7598 }
7599
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007600 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7601
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007602 if (enable_ept)
7603 vmx_enable_tdp();
7604 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007605 kvm_disable_tdp();
7606
Kai Huang843e4332015-01-28 10:54:28 +08007607 /*
7608 * Only enable PML when hardware supports PML feature, and both EPT
7609 * and EPT A/D bit features are enabled -- PML depends on them to work.
7610 */
7611 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7612 enable_pml = 0;
7613
7614 if (!enable_pml) {
7615 kvm_x86_ops->slot_enable_log_dirty = NULL;
7616 kvm_x86_ops->slot_disable_log_dirty = NULL;
7617 kvm_x86_ops->flush_log_dirty = NULL;
7618 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7619 }
7620
Yunhong Jiang64672c92016-06-13 14:19:59 -07007621 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7622 u64 vmx_msr;
7623
7624 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7625 cpu_preemption_timer_multi =
7626 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7627 } else {
7628 kvm_x86_ops->set_hv_timer = NULL;
7629 kvm_x86_ops->cancel_hv_timer = NULL;
7630 }
7631
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007632 if (!cpu_has_vmx_shadow_vmcs())
7633 enable_shadow_vmcs = 0;
7634 if (enable_shadow_vmcs)
7635 init_vmcs_shadow_fields();
7636
Feng Wubf9f6ac2015-09-18 22:29:55 +08007637 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007638 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007639
Ashok Rajc45dcc72016-06-22 14:59:56 +08007640 kvm_mce_cap_supported |= MCG_LMCE_P;
7641
Tiejun Chenf2c76482014-10-28 10:14:47 +08007642 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007643
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007644out:
Radim Krčmář23611332016-09-29 22:41:33 +02007645 for (i = 0; i < VMX_BITMAP_NR; i++)
7646 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007647
7648 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007649}
7650
7651static __exit void hardware_unsetup(void)
7652{
Radim Krčmář23611332016-09-29 22:41:33 +02007653 int i;
7654
7655 for (i = 0; i < VMX_BITMAP_NR; i++)
7656 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007657
Tiejun Chenf2c76482014-10-28 10:14:47 +08007658 free_kvm_area();
7659}
7660
Avi Kivity6aa8b732006-12-10 02:21:36 -08007661/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007662 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7663 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7664 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007665static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007666{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007667 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007668 grow_ple_window(vcpu);
7669
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007670 /*
7671 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7672 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7673 * never set PAUSE_EXITING and just set PLE if supported,
7674 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7675 */
7676 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007677 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007678}
7679
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007680static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007681{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007682 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007683}
7684
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007685static int handle_mwait(struct kvm_vcpu *vcpu)
7686{
7687 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7688 return handle_nop(vcpu);
7689}
7690
Jim Mattson45ec3682017-08-23 16:32:04 -07007691static int handle_invalid_op(struct kvm_vcpu *vcpu)
7692{
7693 kvm_queue_exception(vcpu, UD_VECTOR);
7694 return 1;
7695}
7696
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007697static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7698{
7699 return 1;
7700}
7701
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007702static int handle_monitor(struct kvm_vcpu *vcpu)
7703{
7704 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7705 return handle_nop(vcpu);
7706}
7707
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007708/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007709 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7710 * set the success or error code of an emulated VMX instruction, as specified
7711 * by Vol 2B, VMX Instruction Reference, "Conventions".
7712 */
7713static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7714{
7715 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7716 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7717 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7718}
7719
7720static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7721{
7722 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7723 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7724 X86_EFLAGS_SF | X86_EFLAGS_OF))
7725 | X86_EFLAGS_CF);
7726}
7727
Abel Gordon145c28d2013-04-18 14:36:55 +03007728static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007729 u32 vm_instruction_error)
7730{
7731 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7732 /*
7733 * failValid writes the error number to the current VMCS, which
7734 * can't be done there isn't a current VMCS.
7735 */
7736 nested_vmx_failInvalid(vcpu);
7737 return;
7738 }
7739 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7740 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7741 X86_EFLAGS_SF | X86_EFLAGS_OF))
7742 | X86_EFLAGS_ZF);
7743 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7744 /*
7745 * We don't need to force a shadow sync because
7746 * VM_INSTRUCTION_ERROR is not shadowed
7747 */
7748}
Abel Gordon145c28d2013-04-18 14:36:55 +03007749
Wincy Vanff651cb2014-12-11 08:52:58 +03007750static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7751{
7752 /* TODO: not to reset guest simply here. */
7753 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007754 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007755}
7756
Jan Kiszkaf41245002014-03-07 20:03:13 +01007757static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7758{
7759 struct vcpu_vmx *vmx =
7760 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7761
7762 vmx->nested.preemption_timer_expired = true;
7763 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7764 kvm_vcpu_kick(&vmx->vcpu);
7765
7766 return HRTIMER_NORESTART;
7767}
7768
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007769/*
Bandan Das19677e32014-05-06 02:19:15 -04007770 * Decode the memory-address operand of a vmx instruction, as recorded on an
7771 * exit caused by such an instruction (run by a guest hypervisor).
7772 * On success, returns 0. When the operand is invalid, returns 1 and throws
7773 * #UD or #GP.
7774 */
7775static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7776 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007777 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007778{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007779 gva_t off;
7780 bool exn;
7781 struct kvm_segment s;
7782
Bandan Das19677e32014-05-06 02:19:15 -04007783 /*
7784 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7785 * Execution", on an exit, vmx_instruction_info holds most of the
7786 * addressing components of the operand. Only the displacement part
7787 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7788 * For how an actual address is calculated from all these components,
7789 * refer to Vol. 1, "Operand Addressing".
7790 */
7791 int scaling = vmx_instruction_info & 3;
7792 int addr_size = (vmx_instruction_info >> 7) & 7;
7793 bool is_reg = vmx_instruction_info & (1u << 10);
7794 int seg_reg = (vmx_instruction_info >> 15) & 7;
7795 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7796 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7797 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7798 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7799
7800 if (is_reg) {
7801 kvm_queue_exception(vcpu, UD_VECTOR);
7802 return 1;
7803 }
7804
7805 /* Addr = segment_base + offset */
7806 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007807 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007808 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007809 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007810 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007811 off += kvm_register_read(vcpu, index_reg)<<scaling;
7812 vmx_get_segment(vcpu, &s, seg_reg);
7813 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007814
7815 if (addr_size == 1) /* 32 bit */
7816 *ret &= 0xffffffff;
7817
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007818 /* Checks for #GP/#SS exceptions. */
7819 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007820 if (is_long_mode(vcpu)) {
7821 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7822 * non-canonical form. This is the only check on the memory
7823 * destination for long mode!
7824 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007825 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007826 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007827 /* Protected mode: apply checks for segment validity in the
7828 * following order:
7829 * - segment type check (#GP(0) may be thrown)
7830 * - usability check (#GP(0)/#SS(0))
7831 * - limit check (#GP(0)/#SS(0))
7832 */
7833 if (wr)
7834 /* #GP(0) if the destination operand is located in a
7835 * read-only data segment or any code segment.
7836 */
7837 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7838 else
7839 /* #GP(0) if the source operand is located in an
7840 * execute-only code segment
7841 */
7842 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007843 if (exn) {
7844 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7845 return 1;
7846 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007847 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7848 */
7849 exn = (s.unusable != 0);
7850 /* Protected mode: #GP(0)/#SS(0) if the memory
7851 * operand is outside the segment limit.
7852 */
7853 exn = exn || (off + sizeof(u64) > s.limit);
7854 }
7855 if (exn) {
7856 kvm_queue_exception_e(vcpu,
7857 seg_reg == VCPU_SREG_SS ?
7858 SS_VECTOR : GP_VECTOR,
7859 0);
7860 return 1;
7861 }
7862
Bandan Das19677e32014-05-06 02:19:15 -04007863 return 0;
7864}
7865
Radim Krčmářcbf71272017-05-19 15:48:51 +02007866static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007867{
7868 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007869 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007870
7871 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007872 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007873 return 1;
7874
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007875 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007876 kvm_inject_page_fault(vcpu, &e);
7877 return 1;
7878 }
7879
Bandan Das3573e222014-05-06 02:19:16 -04007880 return 0;
7881}
7882
Jim Mattsone29acc52016-11-30 12:03:43 -08007883static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7884{
7885 struct vcpu_vmx *vmx = to_vmx(vcpu);
7886 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007887 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007888
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007889 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7890 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007891 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007892
7893 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7894 if (!vmx->nested.cached_vmcs12)
7895 goto out_cached_vmcs12;
7896
7897 if (enable_shadow_vmcs) {
7898 shadow_vmcs = alloc_vmcs();
7899 if (!shadow_vmcs)
7900 goto out_shadow_vmcs;
7901 /* mark vmcs as shadow */
7902 shadow_vmcs->revision_id |= (1u << 31);
7903 /* init shadow vmcs */
7904 vmcs_clear(shadow_vmcs);
7905 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7906 }
7907
Jim Mattsone29acc52016-11-30 12:03:43 -08007908 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7909 HRTIMER_MODE_REL_PINNED);
7910 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7911
7912 vmx->nested.vmxon = true;
7913 return 0;
7914
7915out_shadow_vmcs:
7916 kfree(vmx->nested.cached_vmcs12);
7917
7918out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007919 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007920
Jim Mattsonde3a0022017-11-27 17:22:25 -06007921out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007922 return -ENOMEM;
7923}
7924
Bandan Das3573e222014-05-06 02:19:16 -04007925/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007926 * Emulate the VMXON instruction.
7927 * Currently, we just remember that VMX is active, and do not save or even
7928 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7929 * do not currently need to store anything in that guest-allocated memory
7930 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7931 * argument is different from the VMXON pointer (which the spec says they do).
7932 */
7933static int handle_vmon(struct kvm_vcpu *vcpu)
7934{
Jim Mattsone29acc52016-11-30 12:03:43 -08007935 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007936 gpa_t vmptr;
7937 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007938 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007939 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7940 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007941
Jim Mattson70f3aac2017-04-26 08:53:46 -07007942 /*
7943 * The Intel VMX Instruction Reference lists a bunch of bits that are
7944 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7945 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7946 * Otherwise, we should fail with #UD. But most faulting conditions
7947 * have already been checked by hardware, prior to the VM-exit for
7948 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7949 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007950 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007951 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007952 kvm_queue_exception(vcpu, UD_VECTOR);
7953 return 1;
7954 }
7955
Felix Wilhelm727ba742018-06-11 09:43:44 +02007956 /* CPL=0 must be checked manually. */
7957 if (vmx_get_cpl(vcpu)) {
7958 kvm_queue_exception(vcpu, UD_VECTOR);
7959 return 1;
7960 }
7961
Abel Gordon145c28d2013-04-18 14:36:55 +03007962 if (vmx->nested.vmxon) {
7963 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007964 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007965 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007966
Haozhong Zhang3b840802016-06-22 14:59:54 +08007967 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007968 != VMXON_NEEDED_FEATURES) {
7969 kvm_inject_gp(vcpu, 0);
7970 return 1;
7971 }
7972
Radim Krčmářcbf71272017-05-19 15:48:51 +02007973 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007974 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007975
7976 /*
7977 * SDM 3: 24.11.5
7978 * The first 4 bytes of VMXON region contain the supported
7979 * VMCS revision identifier
7980 *
7981 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7982 * which replaces physical address width with 32
7983 */
7984 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7985 nested_vmx_failInvalid(vcpu);
7986 return kvm_skip_emulated_instruction(vcpu);
7987 }
7988
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007989 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7990 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007991 nested_vmx_failInvalid(vcpu);
7992 return kvm_skip_emulated_instruction(vcpu);
7993 }
7994 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7995 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007996 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007997 nested_vmx_failInvalid(vcpu);
7998 return kvm_skip_emulated_instruction(vcpu);
7999 }
8000 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008001 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008002
8003 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008004 ret = enter_vmx_operation(vcpu);
8005 if (ret)
8006 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008007
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008008 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008009 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008010}
8011
8012/*
8013 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8014 * for running VMX instructions (except VMXON, whose prerequisites are
8015 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008016 * Note that many of these exceptions have priority over VM exits, so they
8017 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008018 */
8019static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8020{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008021 if (vmx_get_cpl(vcpu)) {
8022 kvm_queue_exception(vcpu, UD_VECTOR);
8023 return 0;
8024 }
8025
Jim Mattson70f3aac2017-04-26 08:53:46 -07008026 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008027 kvm_queue_exception(vcpu, UD_VECTOR);
8028 return 0;
8029 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008030 return 1;
8031}
8032
David Matlack8ca44e82017-08-01 14:00:39 -07008033static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8034{
8035 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8036 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8037}
8038
Abel Gordone7953d72013-04-18 14:37:55 +03008039static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8040{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008041 if (vmx->nested.current_vmptr == -1ull)
8042 return;
8043
Abel Gordon012f83c2013-04-18 14:39:25 +03008044 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008045 /* copy to memory all shadowed fields in case
8046 they were modified */
8047 copy_shadow_to_vmcs12(vmx);
8048 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008049 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008050 }
Wincy Van705699a2015-02-03 23:58:17 +08008051 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008052
8053 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008054 kvm_vcpu_write_guest_page(&vmx->vcpu,
8055 vmx->nested.current_vmptr >> PAGE_SHIFT,
8056 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008057
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008058 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008059}
8060
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008061/*
8062 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8063 * just stops using VMX.
8064 */
8065static void free_nested(struct vcpu_vmx *vmx)
8066{
Wanpeng Lib7455822017-11-22 14:04:00 -08008067 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008068 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008069
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008070 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008071 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008072 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008073 vmx->nested.posted_intr_nv = -1;
8074 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008075 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008076 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008077 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8078 free_vmcs(vmx->vmcs01.shadow_vmcs);
8079 vmx->vmcs01.shadow_vmcs = NULL;
8080 }
David Matlack4f2777b2016-07-13 17:16:37 -07008081 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008082 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008083 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008084 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008085 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008086 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008087 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008088 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008089 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008090 }
Wincy Van705699a2015-02-03 23:58:17 +08008091 if (vmx->nested.pi_desc_page) {
8092 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008093 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008094 vmx->nested.pi_desc_page = NULL;
8095 vmx->nested.pi_desc = NULL;
8096 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008097
Jim Mattsonde3a0022017-11-27 17:22:25 -06008098 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008099}
8100
8101/* Emulate the VMXOFF instruction */
8102static int handle_vmoff(struct kvm_vcpu *vcpu)
8103{
8104 if (!nested_vmx_check_permission(vcpu))
8105 return 1;
8106 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008107 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008108 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008109}
8110
Nadav Har'El27d6c862011-05-25 23:06:59 +03008111/* Emulate the VMCLEAR instruction */
8112static int handle_vmclear(struct kvm_vcpu *vcpu)
8113{
8114 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008115 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008116 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008117
8118 if (!nested_vmx_check_permission(vcpu))
8119 return 1;
8120
Radim Krčmářcbf71272017-05-19 15:48:51 +02008121 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008122 return 1;
8123
Radim Krčmářcbf71272017-05-19 15:48:51 +02008124 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8125 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8126 return kvm_skip_emulated_instruction(vcpu);
8127 }
8128
8129 if (vmptr == vmx->nested.vmxon_ptr) {
8130 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8131 return kvm_skip_emulated_instruction(vcpu);
8132 }
8133
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008134 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008135 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008136
Jim Mattson587d7e722017-03-02 12:41:48 -08008137 kvm_vcpu_write_guest(vcpu,
8138 vmptr + offsetof(struct vmcs12, launch_state),
8139 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008140
Nadav Har'El27d6c862011-05-25 23:06:59 +03008141 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008142 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008143}
8144
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008145static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8146
8147/* Emulate the VMLAUNCH instruction */
8148static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8149{
8150 return nested_vmx_run(vcpu, true);
8151}
8152
8153/* Emulate the VMRESUME instruction */
8154static int handle_vmresume(struct kvm_vcpu *vcpu)
8155{
8156
8157 return nested_vmx_run(vcpu, false);
8158}
8159
Nadav Har'El49f705c2011-05-25 23:08:30 +03008160/*
8161 * Read a vmcs12 field. Since these can have varying lengths and we return
8162 * one type, we chose the biggest type (u64) and zero-extend the return value
8163 * to that size. Note that the caller, handle_vmread, might need to use only
8164 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8165 * 64-bit fields are to be returned).
8166 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008167static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8168 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008169{
8170 short offset = vmcs_field_to_offset(field);
8171 char *p;
8172
8173 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008174 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008175
8176 p = ((char *)(get_vmcs12(vcpu))) + offset;
8177
Jim Mattsond37f4262017-12-22 12:12:16 -08008178 switch (vmcs_field_width(field)) {
8179 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008180 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008181 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008182 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008183 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008184 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008185 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008186 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008187 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008188 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008189 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008190 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008191 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008192 WARN_ON(1);
8193 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008194 }
8195}
8196
Abel Gordon20b97fe2013-04-18 14:36:25 +03008197
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008198static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8199 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008200 short offset = vmcs_field_to_offset(field);
8201 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8202 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008203 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008204
Jim Mattsond37f4262017-12-22 12:12:16 -08008205 switch (vmcs_field_width(field)) {
8206 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008207 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008208 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008209 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008210 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008211 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008212 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008213 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008214 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008215 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008216 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008217 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008218 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008219 WARN_ON(1);
8220 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008221 }
8222
8223}
8224
Jim Mattsonf4160e42018-05-29 09:11:33 -07008225/*
8226 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8227 * they have been modified by the L1 guest. Note that the "read-only"
8228 * VM-exit information fields are actually writable if the vCPU is
8229 * configured to support "VMWRITE to any supported field in the VMCS."
8230 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008231static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8232{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008233 const u16 *fields[] = {
8234 shadow_read_write_fields,
8235 shadow_read_only_fields
8236 };
8237 const int max_fields[] = {
8238 max_shadow_read_write_fields,
8239 max_shadow_read_only_fields
8240 };
8241 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008242 unsigned long field;
8243 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008244 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008245
Jan Kiszka282da872014-10-08 18:05:39 +02008246 preempt_disable();
8247
Abel Gordon16f5b902013-04-18 14:38:25 +03008248 vmcs_load(shadow_vmcs);
8249
Jim Mattsonf4160e42018-05-29 09:11:33 -07008250 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8251 for (i = 0; i < max_fields[q]; i++) {
8252 field = fields[q][i];
8253 field_value = __vmcs_readl(field);
8254 vmcs12_write_any(&vmx->vcpu, field, field_value);
8255 }
8256 /*
8257 * Skip the VM-exit information fields if they are read-only.
8258 */
8259 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8260 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008261 }
8262
8263 vmcs_clear(shadow_vmcs);
8264 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008265
8266 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008267}
8268
Abel Gordonc3114422013-04-18 14:38:55 +03008269static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8270{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008271 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008272 shadow_read_write_fields,
8273 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008274 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008275 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008276 max_shadow_read_write_fields,
8277 max_shadow_read_only_fields
8278 };
8279 int i, q;
8280 unsigned long field;
8281 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008282 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008283
8284 vmcs_load(shadow_vmcs);
8285
Mathias Krausec2bae892013-06-26 20:36:21 +02008286 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008287 for (i = 0; i < max_fields[q]; i++) {
8288 field = fields[q][i];
8289 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008290 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008291 }
8292 }
8293
8294 vmcs_clear(shadow_vmcs);
8295 vmcs_load(vmx->loaded_vmcs->vmcs);
8296}
8297
Nadav Har'El49f705c2011-05-25 23:08:30 +03008298/*
8299 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8300 * used before) all generate the same failure when it is missing.
8301 */
8302static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8303{
8304 struct vcpu_vmx *vmx = to_vmx(vcpu);
8305 if (vmx->nested.current_vmptr == -1ull) {
8306 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008307 return 0;
8308 }
8309 return 1;
8310}
8311
8312static int handle_vmread(struct kvm_vcpu *vcpu)
8313{
8314 unsigned long field;
8315 u64 field_value;
8316 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8317 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8318 gva_t gva = 0;
8319
Kyle Hueyeb277562016-11-29 12:40:39 -08008320 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008321 return 1;
8322
Kyle Huey6affcbe2016-11-29 12:40:40 -08008323 if (!nested_vmx_check_vmcs12(vcpu))
8324 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008325
Nadav Har'El49f705c2011-05-25 23:08:30 +03008326 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008327 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008328 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008329 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008330 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008331 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 }
8333 /*
8334 * Now copy part of this value to register or memory, as requested.
8335 * Note that the number of bits actually copied is 32 or 64 depending
8336 * on the guest's mode (32 or 64 bit), not on the given field's length.
8337 */
8338 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008339 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008340 field_value);
8341 } else {
8342 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008343 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008344 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008345 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008346 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8347 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008348 }
8349
8350 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008351 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008352}
8353
8354
8355static int handle_vmwrite(struct kvm_vcpu *vcpu)
8356{
8357 unsigned long field;
8358 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008359 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008360 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8361 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008362
Nadav Har'El49f705c2011-05-25 23:08:30 +03008363 /* The value to write might be 32 or 64 bits, depending on L1's long
8364 * mode, and eventually we need to write that into a field of several
8365 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008366 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008367 * bits into the vmcs12 field.
8368 */
8369 u64 field_value = 0;
8370 struct x86_exception e;
8371
Kyle Hueyeb277562016-11-29 12:40:39 -08008372 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008373 return 1;
8374
Kyle Huey6affcbe2016-11-29 12:40:40 -08008375 if (!nested_vmx_check_vmcs12(vcpu))
8376 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008377
Nadav Har'El49f705c2011-05-25 23:08:30 +03008378 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008379 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008380 (((vmx_instruction_info) >> 3) & 0xf));
8381 else {
8382 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008383 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008384 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008385 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8386 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008387 kvm_inject_page_fault(vcpu, &e);
8388 return 1;
8389 }
8390 }
8391
8392
Nadav Amit27e6fb52014-06-18 17:19:26 +03008393 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008394 /*
8395 * If the vCPU supports "VMWRITE to any supported field in the
8396 * VMCS," then the "read-only" fields are actually read/write.
8397 */
8398 if (vmcs_field_readonly(field) &&
8399 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008400 nested_vmx_failValid(vcpu,
8401 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008402 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008403 }
8404
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008405 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008406 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008407 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008408 }
8409
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008410 switch (field) {
8411#define SHADOW_FIELD_RW(x) case x:
8412#include "vmx_shadow_fields.h"
8413 /*
8414 * The fields that can be updated by L1 without a vmexit are
8415 * always updated in the vmcs02, the others go down the slow
8416 * path of prepare_vmcs02.
8417 */
8418 break;
8419 default:
8420 vmx->nested.dirty_vmcs12 = true;
8421 break;
8422 }
8423
Nadav Har'El49f705c2011-05-25 23:08:30 +03008424 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008425 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008426}
8427
Jim Mattsona8bc2842016-11-30 12:03:44 -08008428static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8429{
8430 vmx->nested.current_vmptr = vmptr;
8431 if (enable_shadow_vmcs) {
8432 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8433 SECONDARY_EXEC_SHADOW_VMCS);
8434 vmcs_write64(VMCS_LINK_POINTER,
8435 __pa(vmx->vmcs01.shadow_vmcs));
8436 vmx->nested.sync_shadow_vmcs = true;
8437 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008438 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008439}
8440
Nadav Har'El63846662011-05-25 23:07:29 +03008441/* Emulate the VMPTRLD instruction */
8442static int handle_vmptrld(struct kvm_vcpu *vcpu)
8443{
8444 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008445 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008446
8447 if (!nested_vmx_check_permission(vcpu))
8448 return 1;
8449
Radim Krčmářcbf71272017-05-19 15:48:51 +02008450 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008451 return 1;
8452
Radim Krčmářcbf71272017-05-19 15:48:51 +02008453 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8454 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8455 return kvm_skip_emulated_instruction(vcpu);
8456 }
8457
8458 if (vmptr == vmx->nested.vmxon_ptr) {
8459 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8460 return kvm_skip_emulated_instruction(vcpu);
8461 }
8462
Nadav Har'El63846662011-05-25 23:07:29 +03008463 if (vmx->nested.current_vmptr != vmptr) {
8464 struct vmcs12 *new_vmcs12;
8465 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008466 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8467 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008468 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008469 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008470 }
8471 new_vmcs12 = kmap(page);
8472 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8473 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008474 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008475 nested_vmx_failValid(vcpu,
8476 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008477 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008478 }
Nadav Har'El63846662011-05-25 23:07:29 +03008479
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008480 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008481 /*
8482 * Load VMCS12 from guest memory since it is not already
8483 * cached.
8484 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008485 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8486 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008487 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008488
Jim Mattsona8bc2842016-11-30 12:03:44 -08008489 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008490 }
8491
8492 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008493 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008494}
8495
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008496/* Emulate the VMPTRST instruction */
8497static int handle_vmptrst(struct kvm_vcpu *vcpu)
8498{
8499 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8500 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8501 gva_t vmcs_gva;
8502 struct x86_exception e;
8503
8504 if (!nested_vmx_check_permission(vcpu))
8505 return 1;
8506
8507 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008508 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008509 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008510 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008511 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8512 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8513 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008514 kvm_inject_page_fault(vcpu, &e);
8515 return 1;
8516 }
8517 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008518 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008519}
8520
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008521/* Emulate the INVEPT instruction */
8522static int handle_invept(struct kvm_vcpu *vcpu)
8523{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008524 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008525 u32 vmx_instruction_info, types;
8526 unsigned long type;
8527 gva_t gva;
8528 struct x86_exception e;
8529 struct {
8530 u64 eptp, gpa;
8531 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008532
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008533 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008534 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008535 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008536 kvm_queue_exception(vcpu, UD_VECTOR);
8537 return 1;
8538 }
8539
8540 if (!nested_vmx_check_permission(vcpu))
8541 return 1;
8542
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008543 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008544 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008545
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008546 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008547
Jim Mattson85c856b2016-10-26 08:38:38 -07008548 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008549 nested_vmx_failValid(vcpu,
8550 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008551 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008552 }
8553
8554 /* According to the Intel VMX instruction reference, the memory
8555 * operand is read even if it isn't needed (e.g., for type==global)
8556 */
8557 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008558 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008559 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008560 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008561 kvm_inject_page_fault(vcpu, &e);
8562 return 1;
8563 }
8564
8565 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008566 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008567 /*
8568 * TODO: track mappings and invalidate
8569 * single context requests appropriately
8570 */
8571 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008572 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008573 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008574 nested_vmx_succeed(vcpu);
8575 break;
8576 default:
8577 BUG_ON(1);
8578 break;
8579 }
8580
Kyle Huey6affcbe2016-11-29 12:40:40 -08008581 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008582}
8583
Petr Matouseka642fc32014-09-23 20:22:30 +02008584static int handle_invvpid(struct kvm_vcpu *vcpu)
8585{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008586 struct vcpu_vmx *vmx = to_vmx(vcpu);
8587 u32 vmx_instruction_info;
8588 unsigned long type, types;
8589 gva_t gva;
8590 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008591 struct {
8592 u64 vpid;
8593 u64 gla;
8594 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008595
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008596 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008597 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008598 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008599 kvm_queue_exception(vcpu, UD_VECTOR);
8600 return 1;
8601 }
8602
8603 if (!nested_vmx_check_permission(vcpu))
8604 return 1;
8605
8606 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8607 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8608
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008609 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008610 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008611
Jim Mattson85c856b2016-10-26 08:38:38 -07008612 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008613 nested_vmx_failValid(vcpu,
8614 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008615 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008616 }
8617
8618 /* according to the intel vmx instruction reference, the memory
8619 * operand is read even if it isn't needed (e.g., for type==global)
8620 */
8621 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8622 vmx_instruction_info, false, &gva))
8623 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008624 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008625 kvm_inject_page_fault(vcpu, &e);
8626 return 1;
8627 }
Jim Mattson40352602017-06-28 09:37:37 -07008628 if (operand.vpid >> 16) {
8629 nested_vmx_failValid(vcpu,
8630 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8631 return kvm_skip_emulated_instruction(vcpu);
8632 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008633
8634 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008635 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008636 if (!operand.vpid ||
8637 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008638 nested_vmx_failValid(vcpu,
8639 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8640 return kvm_skip_emulated_instruction(vcpu);
8641 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008642 if (cpu_has_vmx_invvpid_individual_addr() &&
8643 vmx->nested.vpid02) {
8644 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8645 vmx->nested.vpid02, operand.gla);
8646 } else
8647 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8648 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008649 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008650 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008651 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008652 nested_vmx_failValid(vcpu,
8653 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008654 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008655 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008656 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008657 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008658 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008659 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008660 break;
8661 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008662 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008663 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008664 }
8665
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008666 nested_vmx_succeed(vcpu);
8667
Kyle Huey6affcbe2016-11-29 12:40:40 -08008668 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008669}
8670
Kai Huang843e4332015-01-28 10:54:28 +08008671static int handle_pml_full(struct kvm_vcpu *vcpu)
8672{
8673 unsigned long exit_qualification;
8674
8675 trace_kvm_pml_full(vcpu->vcpu_id);
8676
8677 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8678
8679 /*
8680 * PML buffer FULL happened while executing iret from NMI,
8681 * "blocked by NMI" bit has to be set before next VM entry.
8682 */
8683 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008684 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008685 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8686 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8687 GUEST_INTR_STATE_NMI);
8688
8689 /*
8690 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8691 * here.., and there's no userspace involvement needed for PML.
8692 */
8693 return 1;
8694}
8695
Yunhong Jiang64672c92016-06-13 14:19:59 -07008696static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8697{
8698 kvm_lapic_expired_hv_timer(vcpu);
8699 return 1;
8700}
8701
Bandan Das41ab9372017-08-03 15:54:43 -04008702static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8703{
8704 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008705 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8706
8707 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008708 switch (address & VMX_EPTP_MT_MASK) {
8709 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008710 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008711 return false;
8712 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008713 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008714 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008715 return false;
8716 break;
8717 default:
8718 return false;
8719 }
8720
David Hildenbrandbb97a012017-08-10 23:15:28 +02008721 /* only 4 levels page-walk length are valid */
8722 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008723 return false;
8724
8725 /* Reserved bits should not be set */
8726 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8727 return false;
8728
8729 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008730 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008731 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008732 return false;
8733 }
8734
8735 return true;
8736}
8737
8738static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8739 struct vmcs12 *vmcs12)
8740{
8741 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8742 u64 address;
8743 bool accessed_dirty;
8744 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8745
8746 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8747 !nested_cpu_has_ept(vmcs12))
8748 return 1;
8749
8750 if (index >= VMFUNC_EPTP_ENTRIES)
8751 return 1;
8752
8753
8754 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8755 &address, index * 8, 8))
8756 return 1;
8757
David Hildenbrandbb97a012017-08-10 23:15:28 +02008758 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008759
8760 /*
8761 * If the (L2) guest does a vmfunc to the currently
8762 * active ept pointer, we don't have to do anything else
8763 */
8764 if (vmcs12->ept_pointer != address) {
8765 if (!valid_ept_address(vcpu, address))
8766 return 1;
8767
8768 kvm_mmu_unload(vcpu);
8769 mmu->ept_ad = accessed_dirty;
8770 mmu->base_role.ad_disabled = !accessed_dirty;
8771 vmcs12->ept_pointer = address;
8772 /*
8773 * TODO: Check what's the correct approach in case
8774 * mmu reload fails. Currently, we just let the next
8775 * reload potentially fail
8776 */
8777 kvm_mmu_reload(vcpu);
8778 }
8779
8780 return 0;
8781}
8782
Bandan Das2a499e42017-08-03 15:54:41 -04008783static int handle_vmfunc(struct kvm_vcpu *vcpu)
8784{
Bandan Das27c42a12017-08-03 15:54:42 -04008785 struct vcpu_vmx *vmx = to_vmx(vcpu);
8786 struct vmcs12 *vmcs12;
8787 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8788
8789 /*
8790 * VMFUNC is only supported for nested guests, but we always enable the
8791 * secondary control for simplicity; for non-nested mode, fake that we
8792 * didn't by injecting #UD.
8793 */
8794 if (!is_guest_mode(vcpu)) {
8795 kvm_queue_exception(vcpu, UD_VECTOR);
8796 return 1;
8797 }
8798
8799 vmcs12 = get_vmcs12(vcpu);
8800 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8801 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008802
8803 switch (function) {
8804 case 0:
8805 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8806 goto fail;
8807 break;
8808 default:
8809 goto fail;
8810 }
8811 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008812
8813fail:
8814 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8815 vmcs_read32(VM_EXIT_INTR_INFO),
8816 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008817 return 1;
8818}
8819
Nadav Har'El0140cae2011-05-25 23:06:28 +03008820/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008821 * The exit handlers return 1 if the exit was handled fully and guest execution
8822 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8823 * to be done to userspace and return 0.
8824 */
Mathias Krause772e0312012-08-30 01:30:19 +02008825static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008826 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8827 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008828 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008829 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008830 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008831 [EXIT_REASON_CR_ACCESS] = handle_cr,
8832 [EXIT_REASON_DR_ACCESS] = handle_dr,
8833 [EXIT_REASON_CPUID] = handle_cpuid,
8834 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8835 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8836 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8837 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008838 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008839 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008840 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008841 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008842 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008843 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008844 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008845 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008846 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008847 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008848 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008849 [EXIT_REASON_VMOFF] = handle_vmoff,
8850 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008851 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8852 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008853 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008854 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008855 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008856 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008857 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008858 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008859 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8860 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008861 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8862 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008863 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008864 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008865 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008866 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008867 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008868 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008869 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008870 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008871 [EXIT_REASON_XSAVES] = handle_xsaves,
8872 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008873 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008874 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008875 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008876};
8877
8878static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008879 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008880
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008881static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8882 struct vmcs12 *vmcs12)
8883{
8884 unsigned long exit_qualification;
8885 gpa_t bitmap, last_bitmap;
8886 unsigned int port;
8887 int size;
8888 u8 b;
8889
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008890 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008891 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008892
8893 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8894
8895 port = exit_qualification >> 16;
8896 size = (exit_qualification & 7) + 1;
8897
8898 last_bitmap = (gpa_t)-1;
8899 b = -1;
8900
8901 while (size > 0) {
8902 if (port < 0x8000)
8903 bitmap = vmcs12->io_bitmap_a;
8904 else if (port < 0x10000)
8905 bitmap = vmcs12->io_bitmap_b;
8906 else
Joe Perches1d804d02015-03-30 16:46:09 -07008907 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008908 bitmap += (port & 0x7fff) / 8;
8909
8910 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008911 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008912 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008913 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008914 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008915
8916 port++;
8917 size--;
8918 last_bitmap = bitmap;
8919 }
8920
Joe Perches1d804d02015-03-30 16:46:09 -07008921 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008922}
8923
Nadav Har'El644d7112011-05-25 23:12:35 +03008924/*
8925 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8926 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8927 * disinterest in the current event (read or write a specific MSR) by using an
8928 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8929 */
8930static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8931 struct vmcs12 *vmcs12, u32 exit_reason)
8932{
8933 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8934 gpa_t bitmap;
8935
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008936 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008937 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008938
8939 /*
8940 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8941 * for the four combinations of read/write and low/high MSR numbers.
8942 * First we need to figure out which of the four to use:
8943 */
8944 bitmap = vmcs12->msr_bitmap;
8945 if (exit_reason == EXIT_REASON_MSR_WRITE)
8946 bitmap += 2048;
8947 if (msr_index >= 0xc0000000) {
8948 msr_index -= 0xc0000000;
8949 bitmap += 1024;
8950 }
8951
8952 /* Then read the msr_index'th bit from this bitmap: */
8953 if (msr_index < 1024*8) {
8954 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008955 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008956 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008957 return 1 & (b >> (msr_index & 7));
8958 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008959 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008960}
8961
8962/*
8963 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8964 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8965 * intercept (via guest_host_mask etc.) the current event.
8966 */
8967static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8968 struct vmcs12 *vmcs12)
8969{
8970 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8971 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008972 int reg;
8973 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008974
8975 switch ((exit_qualification >> 4) & 3) {
8976 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008977 reg = (exit_qualification >> 8) & 15;
8978 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008979 switch (cr) {
8980 case 0:
8981 if (vmcs12->cr0_guest_host_mask &
8982 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008983 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008984 break;
8985 case 3:
8986 if ((vmcs12->cr3_target_count >= 1 &&
8987 vmcs12->cr3_target_value0 == val) ||
8988 (vmcs12->cr3_target_count >= 2 &&
8989 vmcs12->cr3_target_value1 == val) ||
8990 (vmcs12->cr3_target_count >= 3 &&
8991 vmcs12->cr3_target_value2 == val) ||
8992 (vmcs12->cr3_target_count >= 4 &&
8993 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008994 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008995 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008996 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008997 break;
8998 case 4:
8999 if (vmcs12->cr4_guest_host_mask &
9000 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009001 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009002 break;
9003 case 8:
9004 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009005 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009006 break;
9007 }
9008 break;
9009 case 2: /* clts */
9010 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9011 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009012 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009013 break;
9014 case 1: /* mov from cr */
9015 switch (cr) {
9016 case 3:
9017 if (vmcs12->cpu_based_vm_exec_control &
9018 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009019 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009020 break;
9021 case 8:
9022 if (vmcs12->cpu_based_vm_exec_control &
9023 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009024 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009025 break;
9026 }
9027 break;
9028 case 3: /* lmsw */
9029 /*
9030 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9031 * cr0. Other attempted changes are ignored, with no exit.
9032 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009033 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009034 if (vmcs12->cr0_guest_host_mask & 0xe &
9035 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009036 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009037 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9038 !(vmcs12->cr0_read_shadow & 0x1) &&
9039 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009040 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009041 break;
9042 }
Joe Perches1d804d02015-03-30 16:46:09 -07009043 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009044}
9045
9046/*
9047 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9048 * should handle it ourselves in L0 (and then continue L2). Only call this
9049 * when in is_guest_mode (L2).
9050 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009051static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009052{
Nadav Har'El644d7112011-05-25 23:12:35 +03009053 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9054 struct vcpu_vmx *vmx = to_vmx(vcpu);
9055 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9056
Jim Mattson4f350c62017-09-14 16:31:44 -07009057 if (vmx->nested.nested_run_pending)
9058 return false;
9059
9060 if (unlikely(vmx->fail)) {
9061 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9062 vmcs_read32(VM_INSTRUCTION_ERROR));
9063 return true;
9064 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009065
David Matlackc9f04402017-08-01 14:00:40 -07009066 /*
9067 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009068 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9069 * Page). The CPU may write to these pages via their host
9070 * physical address while L2 is running, bypassing any
9071 * address-translation-based dirty tracking (e.g. EPT write
9072 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009073 *
9074 * Mark them dirty on every exit from L2 to prevent them from
9075 * getting out of sync with dirty tracking.
9076 */
9077 nested_mark_vmcs12_pages_dirty(vcpu);
9078
Jim Mattson4f350c62017-09-14 16:31:44 -07009079 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9080 vmcs_readl(EXIT_QUALIFICATION),
9081 vmx->idt_vectoring_info,
9082 intr_info,
9083 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9084 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009085
9086 switch (exit_reason) {
9087 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009088 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009089 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009090 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009091 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009092 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009093 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009094 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009095 else if (is_debug(intr_info) &&
9096 vcpu->guest_debug &
9097 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9098 return false;
9099 else if (is_breakpoint(intr_info) &&
9100 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9101 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009102 return vmcs12->exception_bitmap &
9103 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9104 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009105 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009106 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009107 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009108 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009109 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009110 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009111 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009112 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009113 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009114 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009115 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009116 case EXIT_REASON_HLT:
9117 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9118 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009119 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009120 case EXIT_REASON_INVLPG:
9121 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9122 case EXIT_REASON_RDPMC:
9123 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009124 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009125 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009126 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009127 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009128 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009129 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9130 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9131 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9132 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9133 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9134 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009135 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009136 /*
9137 * VMX instructions trap unconditionally. This allows L1 to
9138 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9139 */
Joe Perches1d804d02015-03-30 16:46:09 -07009140 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009141 case EXIT_REASON_CR_ACCESS:
9142 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9143 case EXIT_REASON_DR_ACCESS:
9144 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9145 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009146 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009147 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9148 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009149 case EXIT_REASON_MSR_READ:
9150 case EXIT_REASON_MSR_WRITE:
9151 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9152 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009153 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009154 case EXIT_REASON_MWAIT_INSTRUCTION:
9155 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009156 case EXIT_REASON_MONITOR_TRAP_FLAG:
9157 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009158 case EXIT_REASON_MONITOR_INSTRUCTION:
9159 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9160 case EXIT_REASON_PAUSE_INSTRUCTION:
9161 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9162 nested_cpu_has2(vmcs12,
9163 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9164 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009165 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009167 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009168 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009169 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009170 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009171 /*
9172 * The controls for "virtualize APIC accesses," "APIC-
9173 * register virtualization," and "virtual-interrupt
9174 * delivery" only come from vmcs12.
9175 */
Joe Perches1d804d02015-03-30 16:46:09 -07009176 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009177 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009178 /*
9179 * L0 always deals with the EPT violation. If nested EPT is
9180 * used, and the nested mmu code discovers that the address is
9181 * missing in the guest EPT table (EPT12), the EPT violation
9182 * will be injected with nested_ept_inject_page_fault()
9183 */
Joe Perches1d804d02015-03-30 16:46:09 -07009184 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009185 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009186 /*
9187 * L2 never uses directly L1's EPT, but rather L0's own EPT
9188 * table (shadow on EPT) or a merged EPT table that L0 built
9189 * (EPT on EPT). So any problems with the structure of the
9190 * table is L0's fault.
9191 */
Joe Perches1d804d02015-03-30 16:46:09 -07009192 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009193 case EXIT_REASON_INVPCID:
9194 return
9195 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9196 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009197 case EXIT_REASON_WBINVD:
9198 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9199 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009200 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009201 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9202 /*
9203 * This should never happen, since it is not possible to
9204 * set XSS to a non-zero value---neither in L1 nor in L2.
9205 * If if it were, XSS would have to be checked against
9206 * the XSS exit bitmap in vmcs12.
9207 */
9208 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009209 case EXIT_REASON_PREEMPTION_TIMER:
9210 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009211 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009212 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009213 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009214 case EXIT_REASON_VMFUNC:
9215 /* VM functions are emulated through L2->L0 vmexits. */
9216 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009217 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009218 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009219 }
9220}
9221
Paolo Bonzini7313c692017-07-27 10:31:25 +02009222static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9223{
9224 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9225
9226 /*
9227 * At this point, the exit interruption info in exit_intr_info
9228 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9229 * we need to query the in-kernel LAPIC.
9230 */
9231 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9232 if ((exit_intr_info &
9233 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9234 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9235 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9236 vmcs12->vm_exit_intr_error_code =
9237 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9238 }
9239
9240 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9241 vmcs_readl(EXIT_QUALIFICATION));
9242 return 1;
9243}
9244
Avi Kivity586f9602010-11-18 13:09:54 +02009245static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9246{
9247 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9248 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9249}
9250
Kai Huanga3eaa862015-11-04 13:46:05 +08009251static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009252{
Kai Huanga3eaa862015-11-04 13:46:05 +08009253 if (vmx->pml_pg) {
9254 __free_page(vmx->pml_pg);
9255 vmx->pml_pg = NULL;
9256 }
Kai Huang843e4332015-01-28 10:54:28 +08009257}
9258
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009259static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009260{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009261 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009262 u64 *pml_buf;
9263 u16 pml_idx;
9264
9265 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9266
9267 /* Do nothing if PML buffer is empty */
9268 if (pml_idx == (PML_ENTITY_NUM - 1))
9269 return;
9270
9271 /* PML index always points to next available PML buffer entity */
9272 if (pml_idx >= PML_ENTITY_NUM)
9273 pml_idx = 0;
9274 else
9275 pml_idx++;
9276
9277 pml_buf = page_address(vmx->pml_pg);
9278 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9279 u64 gpa;
9280
9281 gpa = pml_buf[pml_idx];
9282 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009283 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009284 }
9285
9286 /* reset PML index */
9287 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9288}
9289
9290/*
9291 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9292 * Called before reporting dirty_bitmap to userspace.
9293 */
9294static void kvm_flush_pml_buffers(struct kvm *kvm)
9295{
9296 int i;
9297 struct kvm_vcpu *vcpu;
9298 /*
9299 * We only need to kick vcpu out of guest mode here, as PML buffer
9300 * is flushed at beginning of all VMEXITs, and it's obvious that only
9301 * vcpus running in guest are possible to have unflushed GPAs in PML
9302 * buffer.
9303 */
9304 kvm_for_each_vcpu(i, vcpu, kvm)
9305 kvm_vcpu_kick(vcpu);
9306}
9307
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009308static void vmx_dump_sel(char *name, uint32_t sel)
9309{
9310 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009311 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009312 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9313 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9314 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9315}
9316
9317static void vmx_dump_dtsel(char *name, uint32_t limit)
9318{
9319 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9320 name, vmcs_read32(limit),
9321 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9322}
9323
9324static void dump_vmcs(void)
9325{
9326 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9327 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9328 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9329 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9330 u32 secondary_exec_control = 0;
9331 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009332 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009333 int i, n;
9334
9335 if (cpu_has_secondary_exec_ctrls())
9336 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9337
9338 pr_err("*** Guest State ***\n");
9339 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9340 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9341 vmcs_readl(CR0_GUEST_HOST_MASK));
9342 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9343 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9344 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9345 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9346 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9347 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009348 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9349 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9350 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9351 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009352 }
9353 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9354 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9355 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9356 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9357 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9358 vmcs_readl(GUEST_SYSENTER_ESP),
9359 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9360 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9361 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9362 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9363 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9364 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9365 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9366 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9367 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9368 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9369 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9370 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9371 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009372 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9373 efer, vmcs_read64(GUEST_IA32_PAT));
9374 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9375 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009376 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009377 if (cpu_has_load_perf_global_ctrl &&
9378 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009379 pr_err("PerfGlobCtl = 0x%016llx\n",
9380 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009381 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009382 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009383 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9384 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9385 vmcs_read32(GUEST_ACTIVITY_STATE));
9386 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9387 pr_err("InterruptStatus = %04x\n",
9388 vmcs_read16(GUEST_INTR_STATUS));
9389
9390 pr_err("*** Host State ***\n");
9391 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9392 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9393 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9394 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9395 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9396 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9397 vmcs_read16(HOST_TR_SELECTOR));
9398 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9399 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9400 vmcs_readl(HOST_TR_BASE));
9401 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9402 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9403 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9404 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9405 vmcs_readl(HOST_CR4));
9406 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9407 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9408 vmcs_read32(HOST_IA32_SYSENTER_CS),
9409 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9410 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009411 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9412 vmcs_read64(HOST_IA32_EFER),
9413 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009414 if (cpu_has_load_perf_global_ctrl &&
9415 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009416 pr_err("PerfGlobCtl = 0x%016llx\n",
9417 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009418
9419 pr_err("*** Control State ***\n");
9420 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9421 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9422 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9423 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9424 vmcs_read32(EXCEPTION_BITMAP),
9425 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9426 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9427 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9428 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9429 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9430 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9431 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9432 vmcs_read32(VM_EXIT_INTR_INFO),
9433 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9434 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9435 pr_err(" reason=%08x qualification=%016lx\n",
9436 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9437 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9438 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9439 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009440 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009441 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009442 pr_err("TSC Multiplier = 0x%016llx\n",
9443 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009444 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9445 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9446 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9447 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9448 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009449 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009450 n = vmcs_read32(CR3_TARGET_COUNT);
9451 for (i = 0; i + 1 < n; i += 4)
9452 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9453 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9454 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9455 if (i < n)
9456 pr_err("CR3 target%u=%016lx\n",
9457 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9458 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9459 pr_err("PLE Gap=%08x Window=%08x\n",
9460 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9461 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9462 pr_err("Virtual processor ID = 0x%04x\n",
9463 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9464}
9465
Avi Kivity6aa8b732006-12-10 02:21:36 -08009466/*
9467 * The guest has exited. See if we can fix it or if we need userspace
9468 * assistance.
9469 */
Avi Kivity851ba692009-08-24 11:10:17 +03009470static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009471{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009472 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009473 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009474 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009475
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009476 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9477
Kai Huang843e4332015-01-28 10:54:28 +08009478 /*
9479 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9480 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9481 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9482 * mode as if vcpus is in root mode, the PML buffer must has been
9483 * flushed already.
9484 */
9485 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009486 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009487
Mohammed Gamal80ced182009-09-01 12:48:18 +02009488 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009489 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009490 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009491
Paolo Bonzini7313c692017-07-27 10:31:25 +02009492 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9493 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009494
Mohammed Gamal51207022010-05-31 22:40:54 +03009495 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009496 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009497 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9498 vcpu->run->fail_entry.hardware_entry_failure_reason
9499 = exit_reason;
9500 return 0;
9501 }
9502
Avi Kivity29bd8a72007-09-10 17:27:03 +03009503 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009504 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9505 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009506 = vmcs_read32(VM_INSTRUCTION_ERROR);
9507 return 0;
9508 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009509
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009510 /*
9511 * Note:
9512 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9513 * delivery event since it indicates guest is accessing MMIO.
9514 * The vm-exit can be triggered again after return to guest that
9515 * will cause infinite loop.
9516 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009517 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009518 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009519 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009520 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009521 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9522 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9523 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009524 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009525 vcpu->run->internal.data[0] = vectoring_info;
9526 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009527 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9528 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9529 vcpu->run->internal.ndata++;
9530 vcpu->run->internal.data[3] =
9531 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9532 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009533 return 0;
9534 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009535
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009536 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009537 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9538 if (vmx_interrupt_allowed(vcpu)) {
9539 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9540 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9541 vcpu->arch.nmi_pending) {
9542 /*
9543 * This CPU don't support us in finding the end of an
9544 * NMI-blocked window if the guest runs with IRQs
9545 * disabled. So we pull the trigger after 1 s of
9546 * futile waiting, but inform the user about this.
9547 */
9548 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9549 "state on VCPU %d after 1 s timeout\n",
9550 __func__, vcpu->vcpu_id);
9551 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9552 }
9553 }
9554
Avi Kivity6aa8b732006-12-10 02:21:36 -08009555 if (exit_reason < kvm_vmx_max_exit_handlers
9556 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009557 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009558 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009559 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9560 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009561 kvm_queue_exception(vcpu, UD_VECTOR);
9562 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009563 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009564}
9565
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009566/*
9567 * Software based L1D cache flush which is used when microcode providing
9568 * the cache control MSR is not loaded.
9569 *
9570 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9571 * flush it is required to read in 64 KiB because the replacement algorithm
9572 * is not exactly LRU. This could be sized at runtime via topology
9573 * information but as all relevant affected CPUs have 32KiB L1D cache size
9574 * there is no point in doing so.
9575 */
9576#define L1D_CACHE_ORDER 4
9577static void *vmx_l1d_flush_pages;
9578
9579static void __maybe_unused vmx_l1d_flush(void)
9580{
9581 int size = PAGE_SIZE << L1D_CACHE_ORDER;
9582
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009583 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9584 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9585 return;
9586 }
9587
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009588 asm volatile(
9589 /* First ensure the pages are in the TLB */
9590 "xorl %%eax, %%eax\n"
9591 ".Lpopulate_tlb:\n\t"
9592 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9593 "addl $4096, %%eax\n\t"
9594 "cmpl %%eax, %[size]\n\t"
9595 "jne .Lpopulate_tlb\n\t"
9596 "xorl %%eax, %%eax\n\t"
9597 "cpuid\n\t"
9598 /* Now fill the cache */
9599 "xorl %%eax, %%eax\n"
9600 ".Lfill_cache:\n"
9601 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9602 "addl $64, %%eax\n\t"
9603 "cmpl %%eax, %[size]\n\t"
9604 "jne .Lfill_cache\n\t"
9605 "lfence\n"
9606 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9607 [size] "r" (size)
9608 : "eax", "ebx", "ecx", "edx");
9609}
9610
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009611static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009612{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009613 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9614
9615 if (is_guest_mode(vcpu) &&
9616 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9617 return;
9618
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009619 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009620 vmcs_write32(TPR_THRESHOLD, 0);
9621 return;
9622 }
9623
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009624 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009625}
9626
Jim Mattson8d860bb2018-05-09 16:56:05 -04009627static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009628{
9629 u32 sec_exec_control;
9630
Jim Mattson8d860bb2018-05-09 16:56:05 -04009631 if (!lapic_in_kernel(vcpu))
9632 return;
9633
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009634 /* Postpone execution until vmcs01 is the current VMCS. */
9635 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009636 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009637 return;
9638 }
9639
Paolo Bonzini35754c92015-07-29 12:05:37 +02009640 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009641 return;
9642
9643 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009644 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9645 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009646
Jim Mattson8d860bb2018-05-09 16:56:05 -04009647 switch (kvm_get_apic_mode(vcpu)) {
9648 case LAPIC_MODE_INVALID:
9649 WARN_ONCE(true, "Invalid local APIC state");
9650 case LAPIC_MODE_DISABLED:
9651 break;
9652 case LAPIC_MODE_XAPIC:
9653 if (flexpriority_enabled) {
9654 sec_exec_control |=
9655 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9656 vmx_flush_tlb(vcpu, true);
9657 }
9658 break;
9659 case LAPIC_MODE_X2APIC:
9660 if (cpu_has_vmx_virtualize_x2apic_mode())
9661 sec_exec_control |=
9662 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9663 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009664 }
9665 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9666
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009667 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009668}
9669
Tang Chen38b99172014-09-24 15:57:54 +08009670static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9671{
Jim Mattsonab5df312018-05-09 17:02:03 -04009672 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009673 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009674 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009675 }
Tang Chen38b99172014-09-24 15:57:54 +08009676}
9677
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009678static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009679{
9680 u16 status;
9681 u8 old;
9682
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009683 if (max_isr == -1)
9684 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009685
9686 status = vmcs_read16(GUEST_INTR_STATUS);
9687 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009688 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009689 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009690 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009691 vmcs_write16(GUEST_INTR_STATUS, status);
9692 }
9693}
9694
9695static void vmx_set_rvi(int vector)
9696{
9697 u16 status;
9698 u8 old;
9699
Wei Wang4114c272014-11-05 10:53:43 +08009700 if (vector == -1)
9701 vector = 0;
9702
Yang Zhangc7c9c562013-01-25 10:18:51 +08009703 status = vmcs_read16(GUEST_INTR_STATUS);
9704 old = (u8)status & 0xff;
9705 if ((u8)vector != old) {
9706 status &= ~0xff;
9707 status |= (u8)vector;
9708 vmcs_write16(GUEST_INTR_STATUS, status);
9709 }
9710}
9711
9712static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9713{
Liran Alon851c1a182017-12-24 18:12:56 +02009714 /*
9715 * When running L2, updating RVI is only relevant when
9716 * vmcs12 virtual-interrupt-delivery enabled.
9717 * However, it can be enabled only when L1 also
9718 * intercepts external-interrupts and in that case
9719 * we should not update vmcs02 RVI but instead intercept
9720 * interrupt. Therefore, do nothing when running L2.
9721 */
9722 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009723 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009724}
9725
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009726static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009727{
9728 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009729 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009730 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009731
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009732 WARN_ON(!vcpu->arch.apicv_active);
9733 if (pi_test_on(&vmx->pi_desc)) {
9734 pi_clear_on(&vmx->pi_desc);
9735 /*
9736 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9737 * But on x86 this is just a compiler barrier anyway.
9738 */
9739 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009740 max_irr_updated =
9741 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9742
9743 /*
9744 * If we are running L2 and L1 has a new pending interrupt
9745 * which can be injected, we should re-evaluate
9746 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009747 * If L1 intercepts external-interrupts, we should
9748 * exit from L2 to L1. Otherwise, interrupt should be
9749 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009750 */
Liran Alon851c1a182017-12-24 18:12:56 +02009751 if (is_guest_mode(vcpu) && max_irr_updated) {
9752 if (nested_exit_on_intr(vcpu))
9753 kvm_vcpu_exiting_guest_mode(vcpu);
9754 else
9755 kvm_make_request(KVM_REQ_EVENT, vcpu);
9756 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009757 } else {
9758 max_irr = kvm_lapic_find_highest_irr(vcpu);
9759 }
9760 vmx_hwapic_irr_update(vcpu, max_irr);
9761 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009762}
9763
Andrey Smetanin63086302015-11-10 15:36:32 +03009764static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009765{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009766 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009767 return;
9768
Yang Zhangc7c9c562013-01-25 10:18:51 +08009769 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9770 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9771 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9772 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9773}
9774
Paolo Bonzini967235d2016-12-19 14:03:45 +01009775static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9776{
9777 struct vcpu_vmx *vmx = to_vmx(vcpu);
9778
9779 pi_clear_on(&vmx->pi_desc);
9780 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9781}
9782
Avi Kivity51aa01d2010-07-20 14:31:20 +03009783static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009784{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009785 u32 exit_intr_info = 0;
9786 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009787
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009788 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9789 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009790 return;
9791
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009792 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9793 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9794 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009795
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009796 /* if exit due to PF check for async PF */
9797 if (is_page_fault(exit_intr_info))
9798 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9799
Andi Kleena0861c02009-06-08 17:37:09 +08009800 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009801 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9802 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009803 kvm_machine_check();
9804
Gleb Natapov20f65982009-05-11 13:35:55 +03009805 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009806 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009807 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009808 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009809 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009810 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009811}
Gleb Natapov20f65982009-05-11 13:35:55 +03009812
Yang Zhanga547c6d2013-04-11 19:25:10 +08009813static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9814{
9815 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9816
Yang Zhanga547c6d2013-04-11 19:25:10 +08009817 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9818 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9819 unsigned int vector;
9820 unsigned long entry;
9821 gate_desc *desc;
9822 struct vcpu_vmx *vmx = to_vmx(vcpu);
9823#ifdef CONFIG_X86_64
9824 unsigned long tmp;
9825#endif
9826
9827 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9828 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009829 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009830 asm volatile(
9831#ifdef CONFIG_X86_64
9832 "mov %%" _ASM_SP ", %[sp]\n\t"
9833 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9834 "push $%c[ss]\n\t"
9835 "push %[sp]\n\t"
9836#endif
9837 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009838 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009839 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009840 :
9841#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009842 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009843#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009844 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009845 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009846 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009847 [ss]"i"(__KERNEL_DS),
9848 [cs]"i"(__KERNEL_CS)
9849 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009850 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009851}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009852STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009853
Tom Lendackybc226f02018-05-10 22:06:39 +02009854static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009855{
Tom Lendackybc226f02018-05-10 22:06:39 +02009856 switch (index) {
9857 case MSR_IA32_SMBASE:
9858 /*
9859 * We cannot do SMM unless we can run the guest in big
9860 * real mode.
9861 */
9862 return enable_unrestricted_guest || emulate_invalid_guest_state;
9863 case MSR_AMD64_VIRT_SPEC_CTRL:
9864 /* This is AMD only. */
9865 return false;
9866 default:
9867 return true;
9868 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009869}
9870
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009871static bool vmx_mpx_supported(void)
9872{
9873 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9874 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9875}
9876
Wanpeng Li55412b22014-12-02 19:21:30 +08009877static bool vmx_xsaves_supported(void)
9878{
9879 return vmcs_config.cpu_based_2nd_exec_ctrl &
9880 SECONDARY_EXEC_XSAVES;
9881}
9882
Avi Kivity51aa01d2010-07-20 14:31:20 +03009883static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9884{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009885 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009886 bool unblock_nmi;
9887 u8 vector;
9888 bool idtv_info_valid;
9889
9890 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009891
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009892 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009893 if (vmx->loaded_vmcs->nmi_known_unmasked)
9894 return;
9895 /*
9896 * Can't use vmx->exit_intr_info since we're not sure what
9897 * the exit reason is.
9898 */
9899 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9900 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9901 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9902 /*
9903 * SDM 3: 27.7.1.2 (September 2008)
9904 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9905 * a guest IRET fault.
9906 * SDM 3: 23.2.2 (September 2008)
9907 * Bit 12 is undefined in any of the following cases:
9908 * If the VM exit sets the valid bit in the IDT-vectoring
9909 * information field.
9910 * If the VM exit is due to a double fault.
9911 */
9912 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9913 vector != DF_VECTOR && !idtv_info_valid)
9914 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9915 GUEST_INTR_STATE_NMI);
9916 else
9917 vmx->loaded_vmcs->nmi_known_unmasked =
9918 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9919 & GUEST_INTR_STATE_NMI);
9920 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9921 vmx->loaded_vmcs->vnmi_blocked_time +=
9922 ktime_to_ns(ktime_sub(ktime_get(),
9923 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009924}
9925
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009926static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009927 u32 idt_vectoring_info,
9928 int instr_len_field,
9929 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009930{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009931 u8 vector;
9932 int type;
9933 bool idtv_info_valid;
9934
9935 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009936
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009937 vcpu->arch.nmi_injected = false;
9938 kvm_clear_exception_queue(vcpu);
9939 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009940
9941 if (!idtv_info_valid)
9942 return;
9943
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009944 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009945
Avi Kivity668f6122008-07-02 09:28:55 +03009946 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9947 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009948
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009949 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009950 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009951 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009952 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009953 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009954 * Clear bit "block by NMI" before VM entry if a NMI
9955 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009956 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009957 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009958 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009959 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009960 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009961 /* fall through */
9962 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009963 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009964 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009965 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009966 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009967 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009968 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009969 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009970 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009971 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009972 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009973 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009974 break;
9975 default:
9976 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009977 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009978}
9979
Avi Kivity83422e12010-07-20 14:43:23 +03009980static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9981{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009982 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009983 VM_EXIT_INSTRUCTION_LEN,
9984 IDT_VECTORING_ERROR_CODE);
9985}
9986
Avi Kivityb463a6f2010-07-20 15:06:17 +03009987static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9988{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009989 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009990 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9991 VM_ENTRY_INSTRUCTION_LEN,
9992 VM_ENTRY_EXCEPTION_ERROR_CODE);
9993
9994 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9995}
9996
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009997static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9998{
9999 int i, nr_msrs;
10000 struct perf_guest_switch_msr *msrs;
10001
10002 msrs = perf_guest_get_msrs(&nr_msrs);
10003
10004 if (!msrs)
10005 return;
10006
10007 for (i = 0; i < nr_msrs; i++)
10008 if (msrs[i].host == msrs[i].guest)
10009 clear_atomic_switch_msr(vmx, msrs[i].msr);
10010 else
10011 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10012 msrs[i].host);
10013}
10014
Jiang Biao33365e72016-11-03 15:03:37 +080010015static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010016{
10017 struct vcpu_vmx *vmx = to_vmx(vcpu);
10018 u64 tscl;
10019 u32 delta_tsc;
10020
10021 if (vmx->hv_deadline_tsc == -1)
10022 return;
10023
10024 tscl = rdtsc();
10025 if (vmx->hv_deadline_tsc > tscl)
10026 /* sure to be 32 bit only because checked on set_hv_timer */
10027 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10028 cpu_preemption_timer_multi);
10029 else
10030 delta_tsc = 0;
10031
10032 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10033}
10034
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010035static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010036{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010038 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010039
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010040 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010041 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010042 vmx->loaded_vmcs->soft_vnmi_blocked))
10043 vmx->loaded_vmcs->entry_time = ktime_get();
10044
Avi Kivity104f2262010-11-18 13:12:52 +020010045 /* Don't enter VMX if guest state is invalid, let the exit handler
10046 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010047 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010048 return;
10049
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010050 if (vmx->ple_window_dirty) {
10051 vmx->ple_window_dirty = false;
10052 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10053 }
10054
Abel Gordon012f83c2013-04-18 14:39:25 +030010055 if (vmx->nested.sync_shadow_vmcs) {
10056 copy_vmcs12_to_shadow(vmx);
10057 vmx->nested.sync_shadow_vmcs = false;
10058 }
10059
Avi Kivity104f2262010-11-18 13:12:52 +020010060 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10061 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10062 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10063 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10064
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010065 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010066 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010067 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010068 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010069 }
10070
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010071 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010072 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010073 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010074 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010075 }
10076
Avi Kivity104f2262010-11-18 13:12:52 +020010077 /* When single-stepping over STI and MOV SS, we must clear the
10078 * corresponding interruptibility bits in the guest state. Otherwise
10079 * vmentry fails as it then expects bit 14 (BS) in pending debug
10080 * exceptions being set, but that's not correct for the guest debugging
10081 * case. */
10082 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10083 vmx_set_interrupt_shadow(vcpu, 0);
10084
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010085 if (static_cpu_has(X86_FEATURE_PKU) &&
10086 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10087 vcpu->arch.pkru != vmx->host_pkru)
10088 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010089
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010090 atomic_switch_perf_msrs(vmx);
10091
Yunhong Jiang64672c92016-06-13 14:19:59 -070010092 vmx_arm_hv_timer(vcpu);
10093
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010094 /*
10095 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10096 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10097 * is no need to worry about the conditional branch over the wrmsr
10098 * being speculatively taken.
10099 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010100 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010101
Nadav Har'Eld462b812011-05-24 15:26:10 +030010102 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010103
10104 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10105 (unsigned long)&current_evmcs->host_rsp : 0;
10106
Avi Kivity104f2262010-11-18 13:12:52 +020010107 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010108 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010109 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10110 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10111 "push %%" _ASM_CX " \n\t"
10112 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010113 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010114 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010115 /* Avoid VMWRITE when Enlightened VMCS is in use */
10116 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10117 "jz 2f \n\t"
10118 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10119 "jmp 1f \n\t"
10120 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010121 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010122 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010123 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010124 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10125 "mov %%cr2, %%" _ASM_DX " \n\t"
10126 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010127 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010128 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010129 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010130 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010131 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010132 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010133 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10134 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10135 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10136 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10137 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10138 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010139#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010140 "mov %c[r8](%0), %%r8 \n\t"
10141 "mov %c[r9](%0), %%r9 \n\t"
10142 "mov %c[r10](%0), %%r10 \n\t"
10143 "mov %c[r11](%0), %%r11 \n\t"
10144 "mov %c[r12](%0), %%r12 \n\t"
10145 "mov %c[r13](%0), %%r13 \n\t"
10146 "mov %c[r14](%0), %%r14 \n\t"
10147 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010148#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010149 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010150
Avi Kivity6aa8b732006-12-10 02:21:36 -080010151 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010152 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010153 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010154 "jmp 2f \n\t"
10155 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10156 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010157 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010158 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010159 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010160 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010161 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10162 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10163 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10164 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10165 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10166 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10167 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010168#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010169 "mov %%r8, %c[r8](%0) \n\t"
10170 "mov %%r9, %c[r9](%0) \n\t"
10171 "mov %%r10, %c[r10](%0) \n\t"
10172 "mov %%r11, %c[r11](%0) \n\t"
10173 "mov %%r12, %c[r12](%0) \n\t"
10174 "mov %%r13, %c[r13](%0) \n\t"
10175 "mov %%r14, %c[r14](%0) \n\t"
10176 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010177 "xor %%r8d, %%r8d \n\t"
10178 "xor %%r9d, %%r9d \n\t"
10179 "xor %%r10d, %%r10d \n\t"
10180 "xor %%r11d, %%r11d \n\t"
10181 "xor %%r12d, %%r12d \n\t"
10182 "xor %%r13d, %%r13d \n\t"
10183 "xor %%r14d, %%r14d \n\t"
10184 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010185#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010186 "mov %%cr2, %%" _ASM_AX " \n\t"
10187 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010188
Jim Mattson0cb5b302018-01-03 14:31:38 -080010189 "xor %%eax, %%eax \n\t"
10190 "xor %%ebx, %%ebx \n\t"
10191 "xor %%esi, %%esi \n\t"
10192 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010193 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010194 ".pushsection .rodata \n\t"
10195 ".global vmx_return \n\t"
10196 "vmx_return: " _ASM_PTR " 2b \n\t"
10197 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010198 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010199 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010200 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010201 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010202 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10203 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10204 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10205 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10206 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10207 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10208 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010209#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010210 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10211 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10212 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10213 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10214 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10215 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10216 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10217 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010218#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010219 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10220 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010221 : "cc", "memory"
10222#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010223 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010224 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010225#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010226 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010227#endif
10228 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010229
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010230 /*
10231 * We do not use IBRS in the kernel. If this vCPU has used the
10232 * SPEC_CTRL MSR it may have left it on; save the value and
10233 * turn it off. This is much more efficient than blindly adding
10234 * it to the atomic save/restore list. Especially as the former
10235 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10236 *
10237 * For non-nested case:
10238 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10239 * save it.
10240 *
10241 * For nested case:
10242 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10243 * save it.
10244 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010245 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010246 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010247
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010248 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010249
David Woodhouse117cc7a2018-01-12 11:11:27 +000010250 /* Eliminate branch target predictions from guest mode */
10251 vmexit_fill_RSB();
10252
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010253 /* All fields are clean at this point */
10254 if (static_branch_unlikely(&enable_evmcs))
10255 current_evmcs->hv_clean_fields |=
10256 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10257
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010258 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010259 if (vmx->host_debugctlmsr)
10260 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010261
Avi Kivityaa67f602012-08-01 16:48:03 +030010262#ifndef CONFIG_X86_64
10263 /*
10264 * The sysexit path does not restore ds/es, so we must set them to
10265 * a reasonable value ourselves.
10266 *
10267 * We can't defer this to vmx_load_host_state() since that function
10268 * may be executed in interrupt context, which saves and restore segments
10269 * around it, nullifying its effect.
10270 */
10271 loadsegment(ds, __USER_DS);
10272 loadsegment(es, __USER_DS);
10273#endif
10274
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010275 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010276 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010277 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010278 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010279 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010280 vcpu->arch.regs_dirty = 0;
10281
Gleb Natapove0b890d2013-09-25 12:51:33 +030010282 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010283 * eager fpu is enabled if PKEY is supported and CR4 is switched
10284 * back on host, so it is safe to read guest PKRU from current
10285 * XSAVE.
10286 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010287 if (static_cpu_has(X86_FEATURE_PKU) &&
10288 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10289 vcpu->arch.pkru = __read_pkru();
10290 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010291 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010292 }
10293
Gleb Natapove0b890d2013-09-25 12:51:33 +030010294 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010295 vmx->idt_vectoring_info = 0;
10296
10297 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10298 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10299 return;
10300
10301 vmx->loaded_vmcs->launched = 1;
10302 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010303
Avi Kivity51aa01d2010-07-20 14:31:20 +030010304 vmx_complete_atomic_exit(vmx);
10305 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010306 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010307}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010308STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010309
Sean Christopherson434a1e92018-03-20 12:17:18 -070010310static struct kvm *vmx_vm_alloc(void)
10311{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010312 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010313 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010314}
10315
10316static void vmx_vm_free(struct kvm *kvm)
10317{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010318 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010319}
10320
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010321static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010322{
10323 struct vcpu_vmx *vmx = to_vmx(vcpu);
10324 int cpu;
10325
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010326 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010327 return;
10328
10329 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010330 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010331 vmx_vcpu_put(vcpu);
10332 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010333 put_cpu();
10334}
10335
Jim Mattson2f1fe812016-07-08 15:36:06 -070010336/*
10337 * Ensure that the current vmcs of the logical processor is the
10338 * vmcs01 of the vcpu before calling free_nested().
10339 */
10340static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10341{
10342 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010343
Christoffer Dallec7660c2017-12-04 21:35:23 +010010344 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010345 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010346 free_nested(vmx);
10347 vcpu_put(vcpu);
10348}
10349
Avi Kivity6aa8b732006-12-10 02:21:36 -080010350static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10351{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010352 struct vcpu_vmx *vmx = to_vmx(vcpu);
10353
Kai Huang843e4332015-01-28 10:54:28 +080010354 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010355 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010356 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010357 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010358 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010359 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010360 kfree(vmx->guest_msrs);
10361 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010362 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010363}
10364
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010365static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010366{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010367 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010368 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010369 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010370 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010371
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010372 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010373 return ERR_PTR(-ENOMEM);
10374
Wanpeng Li991e7a02015-09-16 17:30:05 +080010375 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010376
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010377 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10378 if (err)
10379 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010380
Peter Feiner4e595162016-07-07 14:49:58 -070010381 err = -ENOMEM;
10382
10383 /*
10384 * If PML is turned on, failure on enabling PML just results in failure
10385 * of creating the vcpu, therefore we can simplify PML logic (by
10386 * avoiding dealing with cases, such as enabling PML partially on vcpus
10387 * for the guest, etc.
10388 */
10389 if (enable_pml) {
10390 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10391 if (!vmx->pml_pg)
10392 goto uninit_vcpu;
10393 }
10394
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010395 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010396 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10397 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010398
Peter Feiner4e595162016-07-07 14:49:58 -070010399 if (!vmx->guest_msrs)
10400 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010401
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010402 err = alloc_loaded_vmcs(&vmx->vmcs01);
10403 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010404 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010405
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010406 msr_bitmap = vmx->vmcs01.msr_bitmap;
10407 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10408 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10409 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10410 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10411 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10412 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10413 vmx->msr_bitmap_mode = 0;
10414
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010415 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010416 cpu = get_cpu();
10417 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010418 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010419 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010420 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010421 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010422 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010423 err = alloc_apic_access_page(kvm);
10424 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010425 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010426 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010427
Sean Christophersone90008d2018-03-05 12:04:37 -080010428 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010429 err = init_rmode_identity_map(kvm);
10430 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010431 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010432 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010433
Wanpeng Li5c614b32015-10-13 09:18:36 -070010434 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010435 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10436 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010437 vmx->nested.vpid02 = allocate_vpid();
10438 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010439
Wincy Van705699a2015-02-03 23:58:17 +080010440 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010441 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010442
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010443 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10444
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010445 /*
10446 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10447 * or POSTED_INTR_WAKEUP_VECTOR.
10448 */
10449 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10450 vmx->pi_desc.sn = 1;
10451
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010452 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010453
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010454free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010455 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010456 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010457free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010458 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010459free_pml:
10460 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010461uninit_vcpu:
10462 kvm_vcpu_uninit(&vmx->vcpu);
10463free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010464 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010465 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010466 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010467}
10468
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010469#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10470
Wanpeng Lib31c1142018-03-12 04:53:04 -070010471static int vmx_vm_init(struct kvm *kvm)
10472{
10473 if (!ple_gap)
10474 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010475
10476 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10477 if (nosmt) {
10478 pr_err(L1TF_MSG);
10479 return -EOPNOTSUPP;
10480 }
10481 pr_warn(L1TF_MSG);
10482 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010483 return 0;
10484}
10485
Yang, Sheng002c7f72007-07-31 14:23:01 +030010486static void __init vmx_check_processor_compat(void *rtn)
10487{
10488 struct vmcs_config vmcs_conf;
10489
10490 *(int *)rtn = 0;
10491 if (setup_vmcs_config(&vmcs_conf) < 0)
10492 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010493 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010494 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10495 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10496 smp_processor_id());
10497 *(int *)rtn = -EIO;
10498 }
10499}
10500
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010501static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010502{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010503 u8 cache;
10504 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010505
Sheng Yang522c68c2009-04-27 20:35:43 +080010506 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010507 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010508 * 2. EPT with VT-d:
10509 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010510 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010511 * b. VT-d with snooping control feature: snooping control feature of
10512 * VT-d engine can guarantee the cache correctness. Just set it
10513 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010514 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010515 * consistent with host MTRR
10516 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010517 if (is_mmio) {
10518 cache = MTRR_TYPE_UNCACHABLE;
10519 goto exit;
10520 }
10521
10522 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010523 ipat = VMX_EPT_IPAT_BIT;
10524 cache = MTRR_TYPE_WRBACK;
10525 goto exit;
10526 }
10527
10528 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10529 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010530 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010531 cache = MTRR_TYPE_WRBACK;
10532 else
10533 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010534 goto exit;
10535 }
10536
Xiao Guangrongff536042015-06-15 16:55:22 +080010537 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010538
10539exit:
10540 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010541}
10542
Sheng Yang17cc3932010-01-05 19:02:27 +080010543static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010544{
Sheng Yang878403b2010-01-05 19:02:29 +080010545 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10546 return PT_DIRECTORY_LEVEL;
10547 else
10548 /* For shadow and EPT supported 1GB page */
10549 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010550}
10551
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010552static void vmcs_set_secondary_exec_control(u32 new_ctl)
10553{
10554 /*
10555 * These bits in the secondary execution controls field
10556 * are dynamic, the others are mostly based on the hypervisor
10557 * architecture and the guest's CPUID. Do not touch the
10558 * dynamic bits.
10559 */
10560 u32 mask =
10561 SECONDARY_EXEC_SHADOW_VMCS |
10562 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010563 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10564 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010565
10566 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10567
10568 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10569 (new_ctl & ~mask) | (cur_ctl & mask));
10570}
10571
David Matlack8322ebb2016-11-29 18:14:09 -080010572/*
10573 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10574 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10575 */
10576static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10577{
10578 struct vcpu_vmx *vmx = to_vmx(vcpu);
10579 struct kvm_cpuid_entry2 *entry;
10580
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010581 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10582 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010583
10584#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10585 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010586 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010587} while (0)
10588
10589 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10590 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10591 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10592 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10593 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10594 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10595 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10596 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10597 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10598 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10599 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10600 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10601 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10602 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10603 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10604
10605 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10606 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10607 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10608 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10609 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010610 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010611
10612#undef cr4_fixed1_update
10613}
10614
Sheng Yang0e851882009-12-18 16:48:46 +080010615static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10616{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010617 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010618
Paolo Bonzini80154d72017-08-24 13:55:35 +020010619 if (cpu_has_secondary_exec_ctrls()) {
10620 vmx_compute_secondary_exec_control(vmx);
10621 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010622 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010623
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010624 if (nested_vmx_allowed(vcpu))
10625 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10626 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10627 else
10628 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10629 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010630
10631 if (nested_vmx_allowed(vcpu))
10632 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010633}
10634
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010635static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10636{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010637 if (func == 1 && nested)
10638 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010639}
10640
Yang Zhang25d92082013-08-06 12:00:32 +030010641static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10642 struct x86_exception *fault)
10643{
Jan Kiszka533558b2014-01-04 18:47:20 +010010644 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010645 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010646 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010647 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010648
Bandan Dasc5f983f2017-05-05 15:25:14 -040010649 if (vmx->nested.pml_full) {
10650 exit_reason = EXIT_REASON_PML_FULL;
10651 vmx->nested.pml_full = false;
10652 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10653 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010654 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010655 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010656 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010657
10658 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010659 vmcs12->guest_physical_address = fault->address;
10660}
10661
Peter Feiner995f00a2017-06-30 17:26:32 -070010662static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10663{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010664 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010665}
10666
Nadav Har'El155a97a2013-08-05 11:07:16 +030010667/* Callbacks for nested_ept_init_mmu_context: */
10668
10669static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10670{
10671 /* return the page table to be shadowed - in our case, EPT12 */
10672 return get_vmcs12(vcpu)->ept_pointer;
10673}
10674
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010675static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010676{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010677 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010678 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010679 return 1;
10680
10681 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010682 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010683 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010684 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010685 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010686 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10687 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10688 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10689
10690 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010691 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010692}
10693
10694static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10695{
10696 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10697}
10698
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010699static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10700 u16 error_code)
10701{
10702 bool inequality, bit;
10703
10704 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10705 inequality =
10706 (error_code & vmcs12->page_fault_error_code_mask) !=
10707 vmcs12->page_fault_error_code_match;
10708 return inequality ^ bit;
10709}
10710
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010711static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10712 struct x86_exception *fault)
10713{
10714 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10715
10716 WARN_ON(!is_guest_mode(vcpu));
10717
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010718 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10719 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010720 vmcs12->vm_exit_intr_error_code = fault->error_code;
10721 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10722 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10723 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10724 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010725 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010726 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010727 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010728}
10729
Paolo Bonzinic9923842017-12-13 14:16:30 +010010730static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10731 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010732
10733static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010734 struct vmcs12 *vmcs12)
10735{
10736 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010737 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010738 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010739
10740 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010741 /*
10742 * Translate L1 physical address to host physical
10743 * address for vmcs02. Keep the page pinned, so this
10744 * physical address remains valid. We keep a reference
10745 * to it so we can release it later.
10746 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010747 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010748 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010749 vmx->nested.apic_access_page = NULL;
10750 }
10751 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010752 /*
10753 * If translation failed, no matter: This feature asks
10754 * to exit when accessing the given address, and if it
10755 * can never be accessed, this feature won't do
10756 * anything anyway.
10757 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010758 if (!is_error_page(page)) {
10759 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010760 hpa = page_to_phys(vmx->nested.apic_access_page);
10761 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10762 } else {
10763 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10764 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10765 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010766 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010767
10768 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010769 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010770 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010771 vmx->nested.virtual_apic_page = NULL;
10772 }
10773 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010774
10775 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010776 * If translation failed, VM entry will fail because
10777 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10778 * Failing the vm entry is _not_ what the processor
10779 * does but it's basically the only possibility we
10780 * have. We could still enter the guest if CR8 load
10781 * exits are enabled, CR8 store exits are enabled, and
10782 * virtualize APIC access is disabled; in this case
10783 * the processor would never use the TPR shadow and we
10784 * could simply clear the bit from the execution
10785 * control. But such a configuration is useless, so
10786 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010787 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010788 if (!is_error_page(page)) {
10789 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010790 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10791 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10792 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010793 }
10794
Wincy Van705699a2015-02-03 23:58:17 +080010795 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010796 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10797 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010798 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010799 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010800 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010801 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10802 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010803 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010804 vmx->nested.pi_desc_page = page;
10805 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010806 vmx->nested.pi_desc =
10807 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10808 (unsigned long)(vmcs12->posted_intr_desc_addr &
10809 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010810 vmcs_write64(POSTED_INTR_DESC_ADDR,
10811 page_to_phys(vmx->nested.pi_desc_page) +
10812 (unsigned long)(vmcs12->posted_intr_desc_addr &
10813 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010814 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010815 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010816 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10817 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010818 else
10819 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10820 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010821}
10822
Jan Kiszkaf41245002014-03-07 20:03:13 +010010823static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10824{
10825 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10826 struct vcpu_vmx *vmx = to_vmx(vcpu);
10827
10828 if (vcpu->arch.virtual_tsc_khz == 0)
10829 return;
10830
10831 /* Make sure short timeouts reliably trigger an immediate vmexit.
10832 * hrtimer_start does not guarantee this. */
10833 if (preemption_timeout <= 1) {
10834 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10835 return;
10836 }
10837
10838 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10839 preemption_timeout *= 1000000;
10840 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10841 hrtimer_start(&vmx->nested.preemption_timer,
10842 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10843}
10844
Jim Mattson56a20512017-07-06 16:33:06 -070010845static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10846 struct vmcs12 *vmcs12)
10847{
10848 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10849 return 0;
10850
10851 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10852 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10853 return -EINVAL;
10854
10855 return 0;
10856}
10857
Wincy Van3af18d92015-02-03 23:49:31 +080010858static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10859 struct vmcs12 *vmcs12)
10860{
Wincy Van3af18d92015-02-03 23:49:31 +080010861 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10862 return 0;
10863
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010864 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010865 return -EINVAL;
10866
10867 return 0;
10868}
10869
Jim Mattson712b12d2017-08-24 13:24:47 -070010870static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10871 struct vmcs12 *vmcs12)
10872{
10873 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10874 return 0;
10875
10876 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10877 return -EINVAL;
10878
10879 return 0;
10880}
10881
Wincy Van3af18d92015-02-03 23:49:31 +080010882/*
10883 * Merge L0's and L1's MSR bitmap, return false to indicate that
10884 * we do not use the hardware.
10885 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010886static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10887 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010888{
Wincy Van82f0dd42015-02-03 23:57:18 +080010889 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010890 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010891 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010892 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010893 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010894 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010895 *
10896 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10897 * ensures that we do not accidentally generate an L02 MSR bitmap
10898 * from the L12 MSR bitmap that is too permissive.
10899 * 2. That L1 or L2s have actually used the MSR. This avoids
10900 * unnecessarily merging of the bitmap if the MSR is unused. This
10901 * works properly because we only update the L01 MSR bitmap lazily.
10902 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10903 * updated to reflect this when L1 (or its L2s) actually write to
10904 * the MSR.
10905 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010906 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10907 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010908
Paolo Bonzinic9923842017-12-13 14:16:30 +010010909 /* Nothing to do if the MSR bitmap is not in use. */
10910 if (!cpu_has_vmx_msr_bitmap() ||
10911 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10912 return false;
10913
Ashok Raj15d45072018-02-01 22:59:43 +010010914 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010915 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010916 return false;
10917
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010918 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10919 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010920 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010921
Radim Krčmářd048c092016-08-08 20:16:22 +020010922 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010923 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10924 /*
10925 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10926 * just lets the processor take the value from the virtual-APIC page;
10927 * take those 256 bits directly from the L1 bitmap.
10928 */
10929 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10930 unsigned word = msr / BITS_PER_LONG;
10931 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10932 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010933 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010934 } else {
10935 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10936 unsigned word = msr / BITS_PER_LONG;
10937 msr_bitmap_l0[word] = ~0;
10938 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10939 }
10940 }
10941
10942 nested_vmx_disable_intercept_for_msr(
10943 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010944 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010945 MSR_TYPE_W);
10946
10947 if (nested_cpu_has_vid(vmcs12)) {
10948 nested_vmx_disable_intercept_for_msr(
10949 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010950 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010951 MSR_TYPE_W);
10952 nested_vmx_disable_intercept_for_msr(
10953 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010954 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010955 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010956 }
Ashok Raj15d45072018-02-01 22:59:43 +010010957
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010958 if (spec_ctrl)
10959 nested_vmx_disable_intercept_for_msr(
10960 msr_bitmap_l1, msr_bitmap_l0,
10961 MSR_IA32_SPEC_CTRL,
10962 MSR_TYPE_R | MSR_TYPE_W);
10963
Ashok Raj15d45072018-02-01 22:59:43 +010010964 if (pred_cmd)
10965 nested_vmx_disable_intercept_for_msr(
10966 msr_bitmap_l1, msr_bitmap_l0,
10967 MSR_IA32_PRED_CMD,
10968 MSR_TYPE_W);
10969
Wincy Vanf2b93282015-02-03 23:56:03 +080010970 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010971 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010972
10973 return true;
10974}
10975
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010976static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10977 struct vmcs12 *vmcs12)
10978{
10979 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10980 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10981 return -EINVAL;
10982 else
10983 return 0;
10984}
10985
Wincy Vanf2b93282015-02-03 23:56:03 +080010986static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10987 struct vmcs12 *vmcs12)
10988{
Wincy Van82f0dd42015-02-03 23:57:18 +080010989 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010990 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010991 !nested_cpu_has_vid(vmcs12) &&
10992 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010993 return 0;
10994
10995 /*
10996 * If virtualize x2apic mode is enabled,
10997 * virtualize apic access must be disabled.
10998 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010999 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11000 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011001 return -EINVAL;
11002
Wincy Van608406e2015-02-03 23:57:51 +080011003 /*
11004 * If virtual interrupt delivery is enabled,
11005 * we must exit on external interrupts.
11006 */
11007 if (nested_cpu_has_vid(vmcs12) &&
11008 !nested_exit_on_intr(vcpu))
11009 return -EINVAL;
11010
Wincy Van705699a2015-02-03 23:58:17 +080011011 /*
11012 * bits 15:8 should be zero in posted_intr_nv,
11013 * the descriptor address has been already checked
11014 * in nested_get_vmcs12_pages.
11015 */
11016 if (nested_cpu_has_posted_intr(vmcs12) &&
11017 (!nested_cpu_has_vid(vmcs12) ||
11018 !nested_exit_intr_ack_set(vcpu) ||
11019 vmcs12->posted_intr_nv & 0xff00))
11020 return -EINVAL;
11021
Wincy Vanf2b93282015-02-03 23:56:03 +080011022 /* tpr shadow is needed by all apicv features. */
11023 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11024 return -EINVAL;
11025
11026 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011027}
11028
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011029static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11030 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011031 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011032{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011033 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011034 u64 count, addr;
11035
11036 if (vmcs12_read_any(vcpu, count_field, &count) ||
11037 vmcs12_read_any(vcpu, addr_field, &addr)) {
11038 WARN_ON(1);
11039 return -EINVAL;
11040 }
11041 if (count == 0)
11042 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011043 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011044 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11045 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011046 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011047 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11048 addr_field, maxphyaddr, count, addr);
11049 return -EINVAL;
11050 }
11051 return 0;
11052}
11053
11054static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11055 struct vmcs12 *vmcs12)
11056{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011057 if (vmcs12->vm_exit_msr_load_count == 0 &&
11058 vmcs12->vm_exit_msr_store_count == 0 &&
11059 vmcs12->vm_entry_msr_load_count == 0)
11060 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011061 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011062 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011063 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011064 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011065 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011066 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011067 return -EINVAL;
11068 return 0;
11069}
11070
Bandan Dasc5f983f2017-05-05 15:25:14 -040011071static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11072 struct vmcs12 *vmcs12)
11073{
11074 u64 address = vmcs12->pml_address;
11075 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11076
11077 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11078 if (!nested_cpu_has_ept(vmcs12) ||
11079 !IS_ALIGNED(address, 4096) ||
11080 address >> maxphyaddr)
11081 return -EINVAL;
11082 }
11083
11084 return 0;
11085}
11086
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011087static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11088 struct vmx_msr_entry *e)
11089{
11090 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011091 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011092 return -EINVAL;
11093 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11094 e->index == MSR_IA32_UCODE_REV)
11095 return -EINVAL;
11096 if (e->reserved != 0)
11097 return -EINVAL;
11098 return 0;
11099}
11100
11101static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11102 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011103{
11104 if (e->index == MSR_FS_BASE ||
11105 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011106 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11107 nested_vmx_msr_check_common(vcpu, e))
11108 return -EINVAL;
11109 return 0;
11110}
11111
11112static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11113 struct vmx_msr_entry *e)
11114{
11115 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11116 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011117 return -EINVAL;
11118 return 0;
11119}
11120
11121/*
11122 * Load guest's/host's msr at nested entry/exit.
11123 * return 0 for success, entry index for failure.
11124 */
11125static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11126{
11127 u32 i;
11128 struct vmx_msr_entry e;
11129 struct msr_data msr;
11130
11131 msr.host_initiated = false;
11132 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011133 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11134 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011135 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011136 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11137 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011138 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011139 }
11140 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011141 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011142 "%s check failed (%u, 0x%x, 0x%x)\n",
11143 __func__, i, e.index, e.reserved);
11144 goto fail;
11145 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011146 msr.index = e.index;
11147 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011148 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011149 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011150 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11151 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011152 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011153 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011154 }
11155 return 0;
11156fail:
11157 return i + 1;
11158}
11159
11160static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11161{
11162 u32 i;
11163 struct vmx_msr_entry e;
11164
11165 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011166 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011167 if (kvm_vcpu_read_guest(vcpu,
11168 gpa + i * sizeof(e),
11169 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011170 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011171 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11172 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011173 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011174 }
11175 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011176 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011177 "%s check failed (%u, 0x%x, 0x%x)\n",
11178 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011179 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011180 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011181 msr_info.host_initiated = false;
11182 msr_info.index = e.index;
11183 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011184 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011185 "%s cannot read MSR (%u, 0x%x)\n",
11186 __func__, i, e.index);
11187 return -EINVAL;
11188 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011189 if (kvm_vcpu_write_guest(vcpu,
11190 gpa + i * sizeof(e) +
11191 offsetof(struct vmx_msr_entry, value),
11192 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011193 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011194 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011195 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011196 return -EINVAL;
11197 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011198 }
11199 return 0;
11200}
11201
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011202static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11203{
11204 unsigned long invalid_mask;
11205
11206 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11207 return (val & invalid_mask) == 0;
11208}
11209
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011210/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011211 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11212 * emulating VM entry into a guest with EPT enabled.
11213 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11214 * is assigned to entry_failure_code on failure.
11215 */
11216static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011217 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011218{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011219 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011220 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011221 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11222 return 1;
11223 }
11224
11225 /*
11226 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11227 * must not be dereferenced.
11228 */
11229 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11230 !nested_ept) {
11231 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11232 *entry_failure_code = ENTRY_FAIL_PDPTE;
11233 return 1;
11234 }
11235 }
11236
11237 vcpu->arch.cr3 = cr3;
11238 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11239 }
11240
11241 kvm_mmu_reset_context(vcpu);
11242 return 0;
11243}
11244
Jim Mattson6514dc32018-04-26 16:09:12 -070011245static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011246{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011247 struct vcpu_vmx *vmx = to_vmx(vcpu);
11248
11249 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11250 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11251 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11252 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11253 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11254 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11255 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11256 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11257 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11258 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11259 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11260 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11261 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11262 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11263 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11264 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11265 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11266 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11267 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11268 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11269 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11270 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11271 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11272 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11273 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11274 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11275 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11276 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11277 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11278 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11279 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011280
11281 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11282 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11283 vmcs12->guest_pending_dbg_exceptions);
11284 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11285 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11286
11287 if (nested_cpu_has_xsaves(vmcs12))
11288 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11289 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11290
11291 if (cpu_has_vmx_posted_intr())
11292 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11293
11294 /*
11295 * Whether page-faults are trapped is determined by a combination of
11296 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11297 * If enable_ept, L0 doesn't care about page faults and we should
11298 * set all of these to L1's desires. However, if !enable_ept, L0 does
11299 * care about (at least some) page faults, and because it is not easy
11300 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11301 * to exit on each and every L2 page fault. This is done by setting
11302 * MASK=MATCH=0 and (see below) EB.PF=1.
11303 * Note that below we don't need special code to set EB.PF beyond the
11304 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11305 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11306 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11307 */
11308 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11309 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11310 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11311 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11312
11313 /* All VMFUNCs are currently emulated through L0 vmexits. */
11314 if (cpu_has_vmx_vmfunc())
11315 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11316
11317 if (cpu_has_vmx_apicv()) {
11318 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11319 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11320 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11321 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11322 }
11323
11324 /*
11325 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11326 * Some constant fields are set here by vmx_set_constant_host_state().
11327 * Other fields are different per CPU, and will be set later when
11328 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11329 */
11330 vmx_set_constant_host_state(vmx);
11331
11332 /*
11333 * Set the MSR load/store lists to match L0's settings.
11334 */
11335 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11336 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11337 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11338 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11339 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11340
11341 set_cr4_guest_host_mask(vmx);
11342
11343 if (vmx_mpx_supported())
11344 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11345
11346 if (enable_vpid) {
11347 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11348 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11349 else
11350 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11351 }
11352
11353 /*
11354 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11355 */
11356 if (enable_ept) {
11357 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11358 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11359 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11360 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11361 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011362
11363 if (cpu_has_vmx_msr_bitmap())
11364 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011365}
11366
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011367/*
11368 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11369 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011370 * 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 +030011371 * guest in a way that will both be appropriate to L1's requests, and our
11372 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11373 * function also has additional necessary side-effects, like setting various
11374 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011375 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11376 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011377 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011378static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011379 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011380{
11381 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011382 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011383
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011384 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011385 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011386 vmx->nested.dirty_vmcs12 = false;
11387 }
11388
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011389 /*
11390 * First, the fields that are shadowed. This must be kept in sync
11391 * with vmx_shadow_fields.h.
11392 */
11393
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011394 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011395 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011396 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011397 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11398 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011399
11400 /*
11401 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11402 * HOST_FS_BASE, HOST_GS_BASE.
11403 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011404
Jim Mattson6514dc32018-04-26 16:09:12 -070011405 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011406 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011407 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11408 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11409 } else {
11410 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11411 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11412 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011413 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011414 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11415 vmcs12->vm_entry_intr_info_field);
11416 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11417 vmcs12->vm_entry_exception_error_code);
11418 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11419 vmcs12->vm_entry_instruction_len);
11420 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11421 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011422 vmx->loaded_vmcs->nmi_known_unmasked =
11423 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011424 } else {
11425 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11426 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011427 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011428
Jan Kiszkaf41245002014-03-07 20:03:13 +010011429 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011430
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011431 /* Preemption timer setting is only taken from vmcs01. */
11432 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11433 exec_control |= vmcs_config.pin_based_exec_ctrl;
11434 if (vmx->hv_deadline_tsc == -1)
11435 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11436
11437 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011438 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011439 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11440 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011441 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011442 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011443 }
Wincy Van705699a2015-02-03 23:58:17 +080011444
Jan Kiszkaf41245002014-03-07 20:03:13 +010011445 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011446
Jan Kiszkaf41245002014-03-07 20:03:13 +010011447 vmx->nested.preemption_timer_expired = false;
11448 if (nested_cpu_has_preemption_timer(vmcs12))
11449 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011450
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011451 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011452 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011453
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011454 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011455 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011456 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011457 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011458 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011459 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011460 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11461 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011462 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011463 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11464 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11465 ~SECONDARY_EXEC_ENABLE_PML;
11466 exec_control |= vmcs12_exec_ctrl;
11467 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011468
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011469 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011470 vmcs_write16(GUEST_INTR_STATUS,
11471 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011472
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011473 /*
11474 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11475 * nested_get_vmcs12_pages will either fix it up or
11476 * remove the VM execution control.
11477 */
11478 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11479 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11480
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011481 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11482 }
11483
Jim Mattson83bafef2016-10-04 10:48:38 -070011484 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011485 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11486 * entry, but only if the current (host) sp changed from the value
11487 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11488 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11489 * here we just force the write to happen on entry.
11490 */
11491 vmx->host_rsp = 0;
11492
11493 exec_control = vmx_exec_control(vmx); /* L0's desires */
11494 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11495 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11496 exec_control &= ~CPU_BASED_TPR_SHADOW;
11497 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011498
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011499 /*
11500 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11501 * nested_get_vmcs12_pages can't fix it up, the illegal value
11502 * will result in a VM entry failure.
11503 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011504 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011505 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011506 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011507 } else {
11508#ifdef CONFIG_X86_64
11509 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11510 CPU_BASED_CR8_STORE_EXITING;
11511#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011512 }
11513
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011514 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011515 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11516 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011517 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011518 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11519 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11520
11521 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11522
11523 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11524 * bitwise-or of what L1 wants to trap for L2, and what we want to
11525 * trap. Note that CR0.TS also needs updating - we do this later.
11526 */
11527 update_exception_bitmap(vcpu);
11528 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11529 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11530
Nadav Har'El8049d652013-08-05 11:07:06 +030011531 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11532 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11533 * bits are further modified by vmx_set_efer() below.
11534 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011535 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011536
11537 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11538 * emulated by vmx_set_efer(), below.
11539 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011540 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011541 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11542 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011543 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11544
Jim Mattson6514dc32018-04-26 16:09:12 -070011545 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011546 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011547 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011548 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011549 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011550 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011551 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011552
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011553 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11554
Peter Feinerc95ba922016-08-17 09:36:47 -070011555 if (kvm_has_tsc_control)
11556 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011557
11558 if (enable_vpid) {
11559 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011560 * There is no direct mapping between vpid02 and vpid12, the
11561 * vpid02 is per-vCPU for L0 and reused while the value of
11562 * vpid12 is changed w/ one invvpid during nested vmentry.
11563 * The vpid12 is allocated by L1 for L2, so it will not
11564 * influence global bitmap(for vpid01 and vpid02 allocation)
11565 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011566 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011567 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011568 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11569 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011570 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011571 }
11572 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011573 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011574 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011575 }
11576
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011577 if (enable_pml) {
11578 /*
11579 * Conceptually we want to copy the PML address and index from
11580 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11581 * since we always flush the log on each vmexit, this happens
11582 * to be equivalent to simply resetting the fields in vmcs02.
11583 */
11584 ASSERT(vmx->pml_pg);
11585 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11586 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11587 }
11588
Nadav Har'El155a97a2013-08-05 11:07:16 +030011589 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011590 if (nested_ept_init_mmu_context(vcpu)) {
11591 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11592 return 1;
11593 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011594 } else if (nested_cpu_has2(vmcs12,
11595 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011596 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011597 }
11598
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011599 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011600 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11601 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011602 * The CR0_READ_SHADOW is what L2 should have expected to read given
11603 * the specifications by L1; It's not enough to take
11604 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11605 * have more bits than L1 expected.
11606 */
11607 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11608 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11609
11610 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11611 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11612
Jim Mattson6514dc32018-04-26 16:09:12 -070011613 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011614 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011615 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11616 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11617 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11618 else
11619 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11620 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11621 vmx_set_efer(vcpu, vcpu->arch.efer);
11622
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011623 /*
11624 * Guest state is invalid and unrestricted guest is disabled,
11625 * which means L1 attempted VMEntry to L2 with invalid state.
11626 * Fail the VMEntry.
11627 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011628 if (vmx->emulation_required) {
11629 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011630 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011631 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011632
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011633 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011634 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011635 entry_failure_code))
11636 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011637
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011638 if (!enable_ept)
11639 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11640
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011641 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11642 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011643 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011644}
11645
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011646static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11647{
11648 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11649 nested_cpu_has_virtual_nmis(vmcs12))
11650 return -EINVAL;
11651
11652 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11653 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11654 return -EINVAL;
11655
11656 return 0;
11657}
11658
Jim Mattsonca0bde22016-11-30 12:03:46 -080011659static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11660{
11661 struct vcpu_vmx *vmx = to_vmx(vcpu);
11662
11663 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11664 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11665 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11666
Jim Mattson56a20512017-07-06 16:33:06 -070011667 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11668 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11669
Jim Mattsonca0bde22016-11-30 12:03:46 -080011670 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11671 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11672
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011673 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11674 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11675
Jim Mattson712b12d2017-08-24 13:24:47 -070011676 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11677 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11678
Jim Mattsonca0bde22016-11-30 12:03:46 -080011679 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11680 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11681
11682 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11683 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11684
Bandan Dasc5f983f2017-05-05 15:25:14 -040011685 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11686 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11687
Jim Mattsonca0bde22016-11-30 12:03:46 -080011688 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011689 vmx->nested.msrs.procbased_ctls_low,
11690 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011691 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11692 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011693 vmx->nested.msrs.secondary_ctls_low,
11694 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011695 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011696 vmx->nested.msrs.pinbased_ctls_low,
11697 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011698 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011699 vmx->nested.msrs.exit_ctls_low,
11700 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011701 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011702 vmx->nested.msrs.entry_ctls_low,
11703 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011704 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11705
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011706 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011707 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11708
Bandan Das41ab9372017-08-03 15:54:43 -040011709 if (nested_cpu_has_vmfunc(vmcs12)) {
11710 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011711 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011712 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11713
11714 if (nested_cpu_has_eptp_switching(vmcs12)) {
11715 if (!nested_cpu_has_ept(vmcs12) ||
11716 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11717 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11718 }
11719 }
Bandan Das27c42a12017-08-03 15:54:42 -040011720
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011721 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11722 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11723
Jim Mattsonca0bde22016-11-30 12:03:46 -080011724 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11725 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11726 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11727 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11728
11729 return 0;
11730}
11731
11732static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11733 u32 *exit_qual)
11734{
11735 bool ia32e;
11736
11737 *exit_qual = ENTRY_FAIL_DEFAULT;
11738
11739 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11740 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11741 return 1;
11742
11743 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11744 vmcs12->vmcs_link_pointer != -1ull) {
11745 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11746 return 1;
11747 }
11748
11749 /*
11750 * If the load IA32_EFER VM-entry control is 1, the following checks
11751 * are performed on the field for the IA32_EFER MSR:
11752 * - Bits reserved in the IA32_EFER MSR must be 0.
11753 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11754 * the IA-32e mode guest VM-exit control. It must also be identical
11755 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11756 * CR0.PG) is 1.
11757 */
11758 if (to_vmx(vcpu)->nested.nested_run_pending &&
11759 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11760 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11761 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11762 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11763 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11764 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11765 return 1;
11766 }
11767
11768 /*
11769 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11770 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11771 * the values of the LMA and LME bits in the field must each be that of
11772 * the host address-space size VM-exit control.
11773 */
11774 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11775 ia32e = (vmcs12->vm_exit_controls &
11776 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11777 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11778 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11779 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11780 return 1;
11781 }
11782
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011783 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11784 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11785 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11786 return 1;
11787
Jim Mattsonca0bde22016-11-30 12:03:46 -080011788 return 0;
11789}
11790
Jim Mattson6514dc32018-04-26 16:09:12 -070011791static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011792{
11793 struct vcpu_vmx *vmx = to_vmx(vcpu);
11794 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011795 u32 msr_entry_idx;
11796 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011797 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011798
Jim Mattson858e25c2016-11-30 12:03:47 -080011799 enter_guest_mode(vcpu);
11800
11801 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11802 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11803
Jim Mattsonde3a0022017-11-27 17:22:25 -060011804 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011805 vmx_segment_cache_clear(vmx);
11806
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011807 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11808 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11809
11810 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011811 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011812 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011813
11814 nested_get_vmcs12_pages(vcpu, vmcs12);
11815
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011816 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011817 msr_entry_idx = nested_vmx_load_msr(vcpu,
11818 vmcs12->vm_entry_msr_load_addr,
11819 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011820 if (msr_entry_idx)
11821 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011822
Jim Mattson858e25c2016-11-30 12:03:47 -080011823 /*
11824 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11825 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11826 * returned as far as L1 is concerned. It will only return (and set
11827 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11828 */
11829 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011830
11831fail:
11832 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11833 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11834 leave_guest_mode(vcpu);
11835 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11836 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11837 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011838}
11839
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011840/*
11841 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11842 * for running an L2 nested guest.
11843 */
11844static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11845{
11846 struct vmcs12 *vmcs12;
11847 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011848 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011849 u32 exit_qual;
11850 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011851
Kyle Hueyeb277562016-11-29 12:40:39 -080011852 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011853 return 1;
11854
Kyle Hueyeb277562016-11-29 12:40:39 -080011855 if (!nested_vmx_check_vmcs12(vcpu))
11856 goto out;
11857
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011858 vmcs12 = get_vmcs12(vcpu);
11859
Abel Gordon012f83c2013-04-18 14:39:25 +030011860 if (enable_shadow_vmcs)
11861 copy_shadow_to_vmcs12(vmx);
11862
Nadav Har'El7c177932011-05-25 23:12:04 +030011863 /*
11864 * The nested entry process starts with enforcing various prerequisites
11865 * on vmcs12 as required by the Intel SDM, and act appropriately when
11866 * they fail: As the SDM explains, some conditions should cause the
11867 * instruction to fail, while others will cause the instruction to seem
11868 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11869 * To speed up the normal (success) code path, we should avoid checking
11870 * for misconfigurations which will anyway be caught by the processor
11871 * when using the merged vmcs02.
11872 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011873 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11874 nested_vmx_failValid(vcpu,
11875 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11876 goto out;
11877 }
11878
Nadav Har'El7c177932011-05-25 23:12:04 +030011879 if (vmcs12->launch_state == launch) {
11880 nested_vmx_failValid(vcpu,
11881 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11882 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011883 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011884 }
11885
Jim Mattsonca0bde22016-11-30 12:03:46 -080011886 ret = check_vmentry_prereqs(vcpu, vmcs12);
11887 if (ret) {
11888 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011889 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011890 }
11891
Nadav Har'El7c177932011-05-25 23:12:04 +030011892 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011893 * After this point, the trap flag no longer triggers a singlestep trap
11894 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11895 * This is not 100% correct; for performance reasons, we delegate most
11896 * of the checks on host state to the processor. If those fail,
11897 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011898 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011899 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011900
Jim Mattsonca0bde22016-11-30 12:03:46 -080011901 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11902 if (ret) {
11903 nested_vmx_entry_failure(vcpu, vmcs12,
11904 EXIT_REASON_INVALID_STATE, exit_qual);
11905 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011906 }
11907
11908 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011909 * We're finally done with prerequisite checking, and can start with
11910 * the nested entry.
11911 */
11912
Jim Mattson6514dc32018-04-26 16:09:12 -070011913 vmx->nested.nested_run_pending = 1;
11914 ret = enter_vmx_non_root_mode(vcpu);
11915 if (ret) {
11916 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011917 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011918 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011919
Chao Gao135a06c2018-02-11 10:06:30 +080011920 /*
11921 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11922 * by event injection, halt vcpu.
11923 */
11924 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011925 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11926 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011927 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011928 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011929 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011930
11931out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011932 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011933}
11934
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011935/*
11936 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11937 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11938 * This function returns the new value we should put in vmcs12.guest_cr0.
11939 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11940 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11941 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11942 * didn't trap the bit, because if L1 did, so would L0).
11943 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11944 * been modified by L2, and L1 knows it. So just leave the old value of
11945 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11946 * isn't relevant, because if L0 traps this bit it can set it to anything.
11947 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11948 * changed these bits, and therefore they need to be updated, but L0
11949 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11950 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11951 */
11952static inline unsigned long
11953vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11954{
11955 return
11956 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11957 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11958 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11959 vcpu->arch.cr0_guest_owned_bits));
11960}
11961
11962static inline unsigned long
11963vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11964{
11965 return
11966 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11967 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11968 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11969 vcpu->arch.cr4_guest_owned_bits));
11970}
11971
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011972static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11973 struct vmcs12 *vmcs12)
11974{
11975 u32 idt_vectoring;
11976 unsigned int nr;
11977
Wanpeng Li664f8e22017-08-24 03:35:09 -070011978 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011979 nr = vcpu->arch.exception.nr;
11980 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11981
11982 if (kvm_exception_is_soft(nr)) {
11983 vmcs12->vm_exit_instruction_len =
11984 vcpu->arch.event_exit_inst_len;
11985 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11986 } else
11987 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11988
11989 if (vcpu->arch.exception.has_error_code) {
11990 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11991 vmcs12->idt_vectoring_error_code =
11992 vcpu->arch.exception.error_code;
11993 }
11994
11995 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011996 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011997 vmcs12->idt_vectoring_info_field =
11998 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011999 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012000 nr = vcpu->arch.interrupt.nr;
12001 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12002
12003 if (vcpu->arch.interrupt.soft) {
12004 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12005 vmcs12->vm_entry_instruction_len =
12006 vcpu->arch.event_exit_inst_len;
12007 } else
12008 idt_vectoring |= INTR_TYPE_EXT_INTR;
12009
12010 vmcs12->idt_vectoring_info_field = idt_vectoring;
12011 }
12012}
12013
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012014static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12015{
12016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012017 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012018 bool block_nested_events =
12019 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012020
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012021 if (vcpu->arch.exception.pending &&
12022 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012023 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012024 return -EBUSY;
12025 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012026 return 0;
12027 }
12028
Jan Kiszkaf41245002014-03-07 20:03:13 +010012029 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12030 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012031 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012032 return -EBUSY;
12033 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12034 return 0;
12035 }
12036
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012037 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012038 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012039 return -EBUSY;
12040 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12041 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12042 INTR_INFO_VALID_MASK, 0);
12043 /*
12044 * The NMI-triggered VM exit counts as injection:
12045 * clear this one and block further NMIs.
12046 */
12047 vcpu->arch.nmi_pending = 0;
12048 vmx_set_nmi_mask(vcpu, true);
12049 return 0;
12050 }
12051
12052 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12053 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012054 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012055 return -EBUSY;
12056 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012057 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012058 }
12059
David Hildenbrand6342c502017-01-25 11:58:58 +010012060 vmx_complete_nested_posted_interrupt(vcpu);
12061 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012062}
12063
Jan Kiszkaf41245002014-03-07 20:03:13 +010012064static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12065{
12066 ktime_t remaining =
12067 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12068 u64 value;
12069
12070 if (ktime_to_ns(remaining) <= 0)
12071 return 0;
12072
12073 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12074 do_div(value, 1000000);
12075 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12076}
12077
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012078/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012079 * Update the guest state fields of vmcs12 to reflect changes that
12080 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12081 * VM-entry controls is also updated, since this is really a guest
12082 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012083 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012084static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012085{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012086 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12087 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12088
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012089 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12090 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12091 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12092
12093 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12094 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12095 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12096 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12097 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12098 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12099 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12100 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12101 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12102 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12103 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12104 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12105 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12106 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12107 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12108 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12109 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12110 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12111 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12112 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12113 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12114 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12115 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12116 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12117 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12118 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12119 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12120 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12121 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12122 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12123 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12124 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12125 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12126 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12127 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12128 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12129
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012130 vmcs12->guest_interruptibility_info =
12131 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12132 vmcs12->guest_pending_dbg_exceptions =
12133 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012134 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12135 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12136 else
12137 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012138
Jan Kiszkaf41245002014-03-07 20:03:13 +010012139 if (nested_cpu_has_preemption_timer(vmcs12)) {
12140 if (vmcs12->vm_exit_controls &
12141 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12142 vmcs12->vmx_preemption_timer_value =
12143 vmx_get_preemption_timer_value(vcpu);
12144 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12145 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012146
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012147 /*
12148 * In some cases (usually, nested EPT), L2 is allowed to change its
12149 * own CR3 without exiting. If it has changed it, we must keep it.
12150 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12151 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12152 *
12153 * Additionally, restore L2's PDPTR to vmcs12.
12154 */
12155 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012156 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012157 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12158 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12159 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12160 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12161 }
12162
Jim Mattsond281e132017-06-01 12:44:46 -070012163 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012164
Wincy Van608406e2015-02-03 23:57:51 +080012165 if (nested_cpu_has_vid(vmcs12))
12166 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12167
Jan Kiszkac18911a2013-03-13 16:06:41 +010012168 vmcs12->vm_entry_controls =
12169 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012170 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012171
Jan Kiszka2996fca2014-06-16 13:59:43 +020012172 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12173 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12174 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12175 }
12176
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012177 /* TODO: These cannot have changed unless we have MSR bitmaps and
12178 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012179 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012180 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012181 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12182 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012183 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12184 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12185 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012186 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012187 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012188}
12189
12190/*
12191 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12192 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12193 * and this function updates it to reflect the changes to the guest state while
12194 * L2 was running (and perhaps made some exits which were handled directly by L0
12195 * without going back to L1), and to reflect the exit reason.
12196 * Note that we do not have to copy here all VMCS fields, just those that
12197 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12198 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12199 * which already writes to vmcs12 directly.
12200 */
12201static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12202 u32 exit_reason, u32 exit_intr_info,
12203 unsigned long exit_qualification)
12204{
12205 /* update guest state fields: */
12206 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012207
12208 /* update exit information fields: */
12209
Jan Kiszka533558b2014-01-04 18:47:20 +010012210 vmcs12->vm_exit_reason = exit_reason;
12211 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012212 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012213
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012214 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012215 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12216 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12217
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012218 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012219 vmcs12->launch_state = 1;
12220
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012221 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12222 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012223 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012224
12225 /*
12226 * Transfer the event that L0 or L1 may wanted to inject into
12227 * L2 to IDT_VECTORING_INFO_FIELD.
12228 */
12229 vmcs12_save_pending_event(vcpu, vmcs12);
12230 }
12231
12232 /*
12233 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12234 * preserved above and would only end up incorrectly in L1.
12235 */
12236 vcpu->arch.nmi_injected = false;
12237 kvm_clear_exception_queue(vcpu);
12238 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012239}
12240
Wanpeng Li5af41572017-11-05 16:54:49 -080012241static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12242 struct vmcs12 *vmcs12)
12243{
12244 u32 entry_failure_code;
12245
12246 nested_ept_uninit_mmu_context(vcpu);
12247
12248 /*
12249 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12250 * couldn't have changed.
12251 */
12252 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12253 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12254
12255 if (!enable_ept)
12256 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12257}
12258
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012259/*
12260 * A part of what we need to when the nested L2 guest exits and we want to
12261 * run its L1 parent, is to reset L1's guest state to the host state specified
12262 * in vmcs12.
12263 * This function is to be called not only on normal nested exit, but also on
12264 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12265 * Failures During or After Loading Guest State").
12266 * This function should be called when the active VMCS is L1's (vmcs01).
12267 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012268static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12269 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012270{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012271 struct kvm_segment seg;
12272
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012273 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12274 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012275 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012276 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12277 else
12278 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12279 vmx_set_efer(vcpu, vcpu->arch.efer);
12280
12281 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12282 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012283 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012284 /*
12285 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012286 * actually changed, because vmx_set_cr0 refers to efer set above.
12287 *
12288 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12289 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012290 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012291 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012292 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012293
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012294 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012295 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012296 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012297
Wanpeng Li5af41572017-11-05 16:54:49 -080012298 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012299
Liran Alon6f1e03b2018-05-22 17:16:14 +030012300 /*
12301 * If vmcs01 don't use VPID, CPU flushes TLB on every
12302 * VMEntry/VMExit. Thus, no need to flush TLB.
12303 *
12304 * If vmcs12 uses VPID, TLB entries populated by L2 are
12305 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12306 * with vmx->vpid. Thus, no need to flush TLB.
12307 *
12308 * Therefore, flush TLB only in case vmcs01 uses VPID and
12309 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12310 * are both tagged with vmx->vpid.
12311 */
12312 if (enable_vpid &&
12313 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012314 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012315 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012316
12317 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12318 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12319 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12320 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12321 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012322 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12323 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012324
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012325 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12326 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12327 vmcs_write64(GUEST_BNDCFGS, 0);
12328
Jan Kiszka44811c02013-08-04 17:17:27 +020012329 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012330 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012331 vcpu->arch.pat = vmcs12->host_ia32_pat;
12332 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012333 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12334 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12335 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012336
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012337 /* Set L1 segment info according to Intel SDM
12338 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12339 seg = (struct kvm_segment) {
12340 .base = 0,
12341 .limit = 0xFFFFFFFF,
12342 .selector = vmcs12->host_cs_selector,
12343 .type = 11,
12344 .present = 1,
12345 .s = 1,
12346 .g = 1
12347 };
12348 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12349 seg.l = 1;
12350 else
12351 seg.db = 1;
12352 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12353 seg = (struct kvm_segment) {
12354 .base = 0,
12355 .limit = 0xFFFFFFFF,
12356 .type = 3,
12357 .present = 1,
12358 .s = 1,
12359 .db = 1,
12360 .g = 1
12361 };
12362 seg.selector = vmcs12->host_ds_selector;
12363 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12364 seg.selector = vmcs12->host_es_selector;
12365 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12366 seg.selector = vmcs12->host_ss_selector;
12367 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12368 seg.selector = vmcs12->host_fs_selector;
12369 seg.base = vmcs12->host_fs_base;
12370 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12371 seg.selector = vmcs12->host_gs_selector;
12372 seg.base = vmcs12->host_gs_base;
12373 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12374 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012375 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012376 .limit = 0x67,
12377 .selector = vmcs12->host_tr_selector,
12378 .type = 11,
12379 .present = 1
12380 };
12381 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12382
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012383 kvm_set_dr(vcpu, 7, 0x400);
12384 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012385
Wincy Van3af18d92015-02-03 23:49:31 +080012386 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012387 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012388
Wincy Vanff651cb2014-12-11 08:52:58 +030012389 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12390 vmcs12->vm_exit_msr_load_count))
12391 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012392}
12393
12394/*
12395 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12396 * and modify vmcs12 to make it see what it would expect to see there if
12397 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12398 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012399static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12400 u32 exit_intr_info,
12401 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012402{
12403 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012404 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12405
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012406 /* trying to cancel vmlaunch/vmresume is a bug */
12407 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12408
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012409 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012410 * The only expected VM-instruction error is "VM entry with
12411 * invalid control field(s)." Anything else indicates a
12412 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012413 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012414 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12415 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12416
12417 leave_guest_mode(vcpu);
12418
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012419 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12420 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12421
Jim Mattson4f350c62017-09-14 16:31:44 -070012422 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012423 if (exit_reason == -1)
12424 sync_vmcs12(vcpu, vmcs12);
12425 else
12426 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12427 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012428
12429 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12430 vmcs12->vm_exit_msr_store_count))
12431 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012432 }
12433
Jim Mattson4f350c62017-09-14 16:31:44 -070012434 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012435 vm_entry_controls_reset_shadow(vmx);
12436 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012437 vmx_segment_cache_clear(vmx);
12438
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012439 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012440 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12441 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012442 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012443 if (vmx->hv_deadline_tsc == -1)
12444 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12445 PIN_BASED_VMX_PREEMPTION_TIMER);
12446 else
12447 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12448 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012449 if (kvm_has_tsc_control)
12450 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012451
Jim Mattson8d860bb2018-05-09 16:56:05 -040012452 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12453 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12454 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012455 } else if (!nested_cpu_has_ept(vmcs12) &&
12456 nested_cpu_has2(vmcs12,
12457 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012458 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012459 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012460
12461 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12462 vmx->host_rsp = 0;
12463
12464 /* Unpin physical memory we referred to in vmcs02 */
12465 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012466 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012467 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012468 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012469 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012470 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012471 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012472 }
Wincy Van705699a2015-02-03 23:58:17 +080012473 if (vmx->nested.pi_desc_page) {
12474 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012475 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012476 vmx->nested.pi_desc_page = NULL;
12477 vmx->nested.pi_desc = NULL;
12478 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012479
12480 /*
Tang Chen38b99172014-09-24 15:57:54 +080012481 * We are now running in L2, mmu_notifier will force to reload the
12482 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12483 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012484 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012485
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012486 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012487 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012488
12489 /* in case we halted in L2 */
12490 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012491
12492 if (likely(!vmx->fail)) {
12493 /*
12494 * TODO: SDM says that with acknowledge interrupt on
12495 * exit, bit 31 of the VM-exit interrupt information
12496 * (valid interrupt) is always set to 1 on
12497 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12498 * need kvm_cpu_has_interrupt(). See the commit
12499 * message for details.
12500 */
12501 if (nested_exit_intr_ack_set(vcpu) &&
12502 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12503 kvm_cpu_has_interrupt(vcpu)) {
12504 int irq = kvm_cpu_get_interrupt(vcpu);
12505 WARN_ON(irq < 0);
12506 vmcs12->vm_exit_intr_info = irq |
12507 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12508 }
12509
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012510 if (exit_reason != -1)
12511 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12512 vmcs12->exit_qualification,
12513 vmcs12->idt_vectoring_info_field,
12514 vmcs12->vm_exit_intr_info,
12515 vmcs12->vm_exit_intr_error_code,
12516 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012517
12518 load_vmcs12_host_state(vcpu, vmcs12);
12519
12520 return;
12521 }
12522
12523 /*
12524 * After an early L2 VM-entry failure, we're now back
12525 * in L1 which thinks it just finished a VMLAUNCH or
12526 * VMRESUME instruction, so we need to set the failure
12527 * flag and the VM-instruction error field of the VMCS
12528 * accordingly.
12529 */
12530 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012531
12532 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12533
Jim Mattson4f350c62017-09-14 16:31:44 -070012534 /*
12535 * The emulated instruction was already skipped in
12536 * nested_vmx_run, but the updated RIP was never
12537 * written back to the vmcs01.
12538 */
12539 skip_emulated_instruction(vcpu);
12540 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012541}
12542
Nadav Har'El7c177932011-05-25 23:12:04 +030012543/*
Jan Kiszka42124922014-01-04 18:47:19 +010012544 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12545 */
12546static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12547{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012548 if (is_guest_mode(vcpu)) {
12549 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012550 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012551 }
Jan Kiszka42124922014-01-04 18:47:19 +010012552 free_nested(to_vmx(vcpu));
12553}
12554
12555/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012556 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12557 * 23.7 "VM-entry failures during or after loading guest state" (this also
12558 * lists the acceptable exit-reason and exit-qualification parameters).
12559 * It should only be called before L2 actually succeeded to run, and when
12560 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12561 */
12562static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12563 struct vmcs12 *vmcs12,
12564 u32 reason, unsigned long qualification)
12565{
12566 load_vmcs12_host_state(vcpu, vmcs12);
12567 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12568 vmcs12->exit_qualification = qualification;
12569 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012570 if (enable_shadow_vmcs)
12571 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012572}
12573
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012574static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12575 struct x86_instruction_info *info,
12576 enum x86_intercept_stage stage)
12577{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012578 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12579 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12580
12581 /*
12582 * RDPID causes #UD if disabled through secondary execution controls.
12583 * Because it is marked as EmulateOnUD, we need to intercept it here.
12584 */
12585 if (info->intercept == x86_intercept_rdtscp &&
12586 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12587 ctxt->exception.vector = UD_VECTOR;
12588 ctxt->exception.error_code_valid = false;
12589 return X86EMUL_PROPAGATE_FAULT;
12590 }
12591
12592 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012593 return X86EMUL_CONTINUE;
12594}
12595
Yunhong Jiang64672c92016-06-13 14:19:59 -070012596#ifdef CONFIG_X86_64
12597/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12598static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12599 u64 divisor, u64 *result)
12600{
12601 u64 low = a << shift, high = a >> (64 - shift);
12602
12603 /* To avoid the overflow on divq */
12604 if (high >= divisor)
12605 return 1;
12606
12607 /* Low hold the result, high hold rem which is discarded */
12608 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12609 "rm" (divisor), "0" (low), "1" (high));
12610 *result = low;
12611
12612 return 0;
12613}
12614
12615static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12616{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012617 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012618 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012619
12620 if (kvm_mwait_in_guest(vcpu->kvm))
12621 return -EOPNOTSUPP;
12622
12623 vmx = to_vmx(vcpu);
12624 tscl = rdtsc();
12625 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12626 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012627 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12628
12629 if (delta_tsc > lapic_timer_advance_cycles)
12630 delta_tsc -= lapic_timer_advance_cycles;
12631 else
12632 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012633
12634 /* Convert to host delta tsc if tsc scaling is enabled */
12635 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12636 u64_shl_div_u64(delta_tsc,
12637 kvm_tsc_scaling_ratio_frac_bits,
12638 vcpu->arch.tsc_scaling_ratio,
12639 &delta_tsc))
12640 return -ERANGE;
12641
12642 /*
12643 * If the delta tsc can't fit in the 32 bit after the multi shift,
12644 * we can't use the preemption timer.
12645 * It's possible that it fits on later vmentries, but checking
12646 * on every vmentry is costly so we just use an hrtimer.
12647 */
12648 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12649 return -ERANGE;
12650
12651 vmx->hv_deadline_tsc = tscl + delta_tsc;
12652 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12653 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012654
12655 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012656}
12657
12658static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12659{
12660 struct vcpu_vmx *vmx = to_vmx(vcpu);
12661 vmx->hv_deadline_tsc = -1;
12662 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12663 PIN_BASED_VMX_PREEMPTION_TIMER);
12664}
12665#endif
12666
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012667static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012668{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012669 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012670 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012671}
12672
Kai Huang843e4332015-01-28 10:54:28 +080012673static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12674 struct kvm_memory_slot *slot)
12675{
12676 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12677 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12678}
12679
12680static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12681 struct kvm_memory_slot *slot)
12682{
12683 kvm_mmu_slot_set_dirty(kvm, slot);
12684}
12685
12686static void vmx_flush_log_dirty(struct kvm *kvm)
12687{
12688 kvm_flush_pml_buffers(kvm);
12689}
12690
Bandan Dasc5f983f2017-05-05 15:25:14 -040012691static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12692{
12693 struct vmcs12 *vmcs12;
12694 struct vcpu_vmx *vmx = to_vmx(vcpu);
12695 gpa_t gpa;
12696 struct page *page = NULL;
12697 u64 *pml_address;
12698
12699 if (is_guest_mode(vcpu)) {
12700 WARN_ON_ONCE(vmx->nested.pml_full);
12701
12702 /*
12703 * Check if PML is enabled for the nested guest.
12704 * Whether eptp bit 6 is set is already checked
12705 * as part of A/D emulation.
12706 */
12707 vmcs12 = get_vmcs12(vcpu);
12708 if (!nested_cpu_has_pml(vmcs12))
12709 return 0;
12710
Dan Carpenter47698862017-05-10 22:43:17 +030012711 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012712 vmx->nested.pml_full = true;
12713 return 1;
12714 }
12715
12716 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12717
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012718 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12719 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012720 return 0;
12721
12722 pml_address = kmap(page);
12723 pml_address[vmcs12->guest_pml_index--] = gpa;
12724 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012725 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012726 }
12727
12728 return 0;
12729}
12730
Kai Huang843e4332015-01-28 10:54:28 +080012731static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12732 struct kvm_memory_slot *memslot,
12733 gfn_t offset, unsigned long mask)
12734{
12735 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12736}
12737
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012738static void __pi_post_block(struct kvm_vcpu *vcpu)
12739{
12740 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12741 struct pi_desc old, new;
12742 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012743
12744 do {
12745 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012746 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12747 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012748
12749 dest = cpu_physical_id(vcpu->cpu);
12750
12751 if (x2apic_enabled())
12752 new.ndst = dest;
12753 else
12754 new.ndst = (dest << 8) & 0xFF00;
12755
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012756 /* set 'NV' to 'notification vector' */
12757 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012758 } while (cmpxchg64(&pi_desc->control, old.control,
12759 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012760
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012761 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12762 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012763 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012764 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012765 vcpu->pre_pcpu = -1;
12766 }
12767}
12768
Feng Wuefc64402015-09-18 22:29:51 +080012769/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012770 * This routine does the following things for vCPU which is going
12771 * to be blocked if VT-d PI is enabled.
12772 * - Store the vCPU to the wakeup list, so when interrupts happen
12773 * we can find the right vCPU to wake up.
12774 * - Change the Posted-interrupt descriptor as below:
12775 * 'NDST' <-- vcpu->pre_pcpu
12776 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12777 * - If 'ON' is set during this process, which means at least one
12778 * interrupt is posted for this vCPU, we cannot block it, in
12779 * this case, return 1, otherwise, return 0.
12780 *
12781 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012782static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012783{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012784 unsigned int dest;
12785 struct pi_desc old, new;
12786 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12787
12788 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012789 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12790 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012791 return 0;
12792
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012793 WARN_ON(irqs_disabled());
12794 local_irq_disable();
12795 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12796 vcpu->pre_pcpu = vcpu->cpu;
12797 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12798 list_add_tail(&vcpu->blocked_vcpu_list,
12799 &per_cpu(blocked_vcpu_on_cpu,
12800 vcpu->pre_pcpu));
12801 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12802 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012803
12804 do {
12805 old.control = new.control = pi_desc->control;
12806
Feng Wubf9f6ac2015-09-18 22:29:55 +080012807 WARN((pi_desc->sn == 1),
12808 "Warning: SN field of posted-interrupts "
12809 "is set before blocking\n");
12810
12811 /*
12812 * Since vCPU can be preempted during this process,
12813 * vcpu->cpu could be different with pre_pcpu, we
12814 * need to set pre_pcpu as the destination of wakeup
12815 * notification event, then we can find the right vCPU
12816 * to wakeup in wakeup handler if interrupts happen
12817 * when the vCPU is in blocked state.
12818 */
12819 dest = cpu_physical_id(vcpu->pre_pcpu);
12820
12821 if (x2apic_enabled())
12822 new.ndst = dest;
12823 else
12824 new.ndst = (dest << 8) & 0xFF00;
12825
12826 /* set 'NV' to 'wakeup vector' */
12827 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012828 } while (cmpxchg64(&pi_desc->control, old.control,
12829 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012830
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012831 /* We should not block the vCPU if an interrupt is posted for it. */
12832 if (pi_test_on(pi_desc) == 1)
12833 __pi_post_block(vcpu);
12834
12835 local_irq_enable();
12836 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012837}
12838
Yunhong Jiangbc225122016-06-13 14:19:58 -070012839static int vmx_pre_block(struct kvm_vcpu *vcpu)
12840{
12841 if (pi_pre_block(vcpu))
12842 return 1;
12843
Yunhong Jiang64672c92016-06-13 14:19:59 -070012844 if (kvm_lapic_hv_timer_in_use(vcpu))
12845 kvm_lapic_switch_to_sw_timer(vcpu);
12846
Yunhong Jiangbc225122016-06-13 14:19:58 -070012847 return 0;
12848}
12849
12850static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012851{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012852 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012853 return;
12854
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012855 WARN_ON(irqs_disabled());
12856 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012857 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012858 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012859}
12860
Yunhong Jiangbc225122016-06-13 14:19:58 -070012861static void vmx_post_block(struct kvm_vcpu *vcpu)
12862{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012863 if (kvm_x86_ops->set_hv_timer)
12864 kvm_lapic_switch_to_hv_timer(vcpu);
12865
Yunhong Jiangbc225122016-06-13 14:19:58 -070012866 pi_post_block(vcpu);
12867}
12868
Feng Wubf9f6ac2015-09-18 22:29:55 +080012869/*
Feng Wuefc64402015-09-18 22:29:51 +080012870 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12871 *
12872 * @kvm: kvm
12873 * @host_irq: host irq of the interrupt
12874 * @guest_irq: gsi of the interrupt
12875 * @set: set or unset PI
12876 * returns 0 on success, < 0 on failure
12877 */
12878static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12879 uint32_t guest_irq, bool set)
12880{
12881 struct kvm_kernel_irq_routing_entry *e;
12882 struct kvm_irq_routing_table *irq_rt;
12883 struct kvm_lapic_irq irq;
12884 struct kvm_vcpu *vcpu;
12885 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012886 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012887
12888 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012889 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12890 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012891 return 0;
12892
12893 idx = srcu_read_lock(&kvm->irq_srcu);
12894 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012895 if (guest_irq >= irq_rt->nr_rt_entries ||
12896 hlist_empty(&irq_rt->map[guest_irq])) {
12897 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12898 guest_irq, irq_rt->nr_rt_entries);
12899 goto out;
12900 }
Feng Wuefc64402015-09-18 22:29:51 +080012901
12902 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12903 if (e->type != KVM_IRQ_ROUTING_MSI)
12904 continue;
12905 /*
12906 * VT-d PI cannot support posting multicast/broadcast
12907 * interrupts to a vCPU, we still use interrupt remapping
12908 * for these kind of interrupts.
12909 *
12910 * For lowest-priority interrupts, we only support
12911 * those with single CPU as the destination, e.g. user
12912 * configures the interrupts via /proc/irq or uses
12913 * irqbalance to make the interrupts single-CPU.
12914 *
12915 * We will support full lowest-priority interrupt later.
12916 */
12917
Radim Krčmář371313132016-07-12 22:09:27 +020012918 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012919 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12920 /*
12921 * Make sure the IRTE is in remapped mode if
12922 * we don't handle it in posted mode.
12923 */
12924 ret = irq_set_vcpu_affinity(host_irq, NULL);
12925 if (ret < 0) {
12926 printk(KERN_INFO
12927 "failed to back to remapped mode, irq: %u\n",
12928 host_irq);
12929 goto out;
12930 }
12931
Feng Wuefc64402015-09-18 22:29:51 +080012932 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012933 }
Feng Wuefc64402015-09-18 22:29:51 +080012934
12935 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12936 vcpu_info.vector = irq.vector;
12937
hu huajun2698d822018-04-11 15:16:40 +080012938 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012939 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12940
12941 if (set)
12942 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012943 else
Feng Wuefc64402015-09-18 22:29:51 +080012944 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012945
12946 if (ret < 0) {
12947 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12948 __func__);
12949 goto out;
12950 }
12951 }
12952
12953 ret = 0;
12954out:
12955 srcu_read_unlock(&kvm->irq_srcu, idx);
12956 return ret;
12957}
12958
Ashok Rajc45dcc72016-06-22 14:59:56 +080012959static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12960{
12961 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12962 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12963 FEATURE_CONTROL_LMCE;
12964 else
12965 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12966 ~FEATURE_CONTROL_LMCE;
12967}
12968
Ladi Prosek72d7b372017-10-11 16:54:41 +020012969static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12970{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012971 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12972 if (to_vmx(vcpu)->nested.nested_run_pending)
12973 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012974 return 1;
12975}
12976
Ladi Prosek0234bf82017-10-11 16:54:40 +020012977static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12978{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012979 struct vcpu_vmx *vmx = to_vmx(vcpu);
12980
12981 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12982 if (vmx->nested.smm.guest_mode)
12983 nested_vmx_vmexit(vcpu, -1, 0, 0);
12984
12985 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12986 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012987 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012988 return 0;
12989}
12990
12991static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12992{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012993 struct vcpu_vmx *vmx = to_vmx(vcpu);
12994 int ret;
12995
12996 if (vmx->nested.smm.vmxon) {
12997 vmx->nested.vmxon = true;
12998 vmx->nested.smm.vmxon = false;
12999 }
13000
13001 if (vmx->nested.smm.guest_mode) {
13002 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013003 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013004 vcpu->arch.hflags |= HF_SMM_MASK;
13005 if (ret)
13006 return ret;
13007
13008 vmx->nested.smm.guest_mode = false;
13009 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013010 return 0;
13011}
13012
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013013static int enable_smi_window(struct kvm_vcpu *vcpu)
13014{
13015 return 0;
13016}
13017
Kees Cook404f6aa2016-08-08 16:29:06 -070013018static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013019 .cpu_has_kvm_support = cpu_has_kvm_support,
13020 .disabled_by_bios = vmx_disabled_by_bios,
13021 .hardware_setup = hardware_setup,
13022 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013023 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013024 .hardware_enable = hardware_enable,
13025 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013026 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013027 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013028
Wanpeng Lib31c1142018-03-12 04:53:04 -070013029 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013030 .vm_alloc = vmx_vm_alloc,
13031 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013032
Avi Kivity6aa8b732006-12-10 02:21:36 -080013033 .vcpu_create = vmx_create_vcpu,
13034 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013035 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013036
Avi Kivity04d2cc72007-09-10 18:10:54 +030013037 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013038 .vcpu_load = vmx_vcpu_load,
13039 .vcpu_put = vmx_vcpu_put,
13040
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013041 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013042 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013043 .get_msr = vmx_get_msr,
13044 .set_msr = vmx_set_msr,
13045 .get_segment_base = vmx_get_segment_base,
13046 .get_segment = vmx_get_segment,
13047 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013048 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013049 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013050 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013051 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013052 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013053 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013054 .set_cr3 = vmx_set_cr3,
13055 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013056 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013057 .get_idt = vmx_get_idt,
13058 .set_idt = vmx_set_idt,
13059 .get_gdt = vmx_get_gdt,
13060 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013061 .get_dr6 = vmx_get_dr6,
13062 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013063 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013064 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013065 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013066 .get_rflags = vmx_get_rflags,
13067 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013068
Avi Kivity6aa8b732006-12-10 02:21:36 -080013069 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013070
Avi Kivity6aa8b732006-12-10 02:21:36 -080013071 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013072 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013073 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013074 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13075 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013076 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013077 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013078 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013079 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013080 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013081 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013082 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013083 .get_nmi_mask = vmx_get_nmi_mask,
13084 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013085 .enable_nmi_window = enable_nmi_window,
13086 .enable_irq_window = enable_irq_window,
13087 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013088 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013089 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013090 .get_enable_apicv = vmx_get_enable_apicv,
13091 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013092 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013093 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013094 .hwapic_irr_update = vmx_hwapic_irr_update,
13095 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013096 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13097 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013098
Izik Eiduscbc94022007-10-25 00:29:55 +020013099 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013100 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013101 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013102 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013103
Avi Kivity586f9602010-11-18 13:09:54 +020013104 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013105
Sheng Yang17cc3932010-01-05 19:02:27 +080013106 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013107
13108 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013109
13110 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013111 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013112
13113 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013114
13115 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013116
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013117 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013118 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013119
13120 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013121
13122 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013123 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013124 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013125 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013126 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013127
13128 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013129
13130 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013131
13132 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13133 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13134 .flush_log_dirty = vmx_flush_log_dirty,
13135 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013136 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013137
Feng Wubf9f6ac2015-09-18 22:29:55 +080013138 .pre_block = vmx_pre_block,
13139 .post_block = vmx_post_block,
13140
Wei Huang25462f72015-06-19 15:45:05 +020013141 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013142
13143 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013144
13145#ifdef CONFIG_X86_64
13146 .set_hv_timer = vmx_set_hv_timer,
13147 .cancel_hv_timer = vmx_cancel_hv_timer,
13148#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013149
13150 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013151
Ladi Prosek72d7b372017-10-11 16:54:41 +020013152 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013153 .pre_enter_smm = vmx_pre_enter_smm,
13154 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013155 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013156};
13157
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013158static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013159{
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013160 struct page *page;
13161
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013162 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER ||
13163 !boot_cpu_has_bug(X86_BUG_L1TF))
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013164 return 0;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013165
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020013166 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
13167 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
13168 if (!page)
13169 return -ENOMEM;
13170 vmx_l1d_flush_pages = page_address(page);
13171 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013172
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013173 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013174 return 0;
13175}
13176
13177static void vmx_free_l1d_flush_pages(void)
13178{
13179 if (vmx_l1d_flush_pages) {
13180 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13181 vmx_l1d_flush_pages = NULL;
13182 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013183}
13184
Avi Kivity6aa8b732006-12-10 02:21:36 -080013185static int __init vmx_init(void)
13186{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013187 int r;
13188
13189#if IS_ENABLED(CONFIG_HYPERV)
13190 /*
13191 * Enlightened VMCS usage should be recommended and the host needs
13192 * to support eVMCS v1 or above. We can also disable eVMCS support
13193 * with module parameter.
13194 */
13195 if (enlightened_vmcs &&
13196 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13197 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13198 KVM_EVMCS_VERSION) {
13199 int cpu;
13200
13201 /* Check that we have assist pages on all online CPUs */
13202 for_each_online_cpu(cpu) {
13203 if (!hv_get_vp_assist_page(cpu)) {
13204 enlightened_vmcs = false;
13205 break;
13206 }
13207 }
13208
13209 if (enlightened_vmcs) {
13210 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13211 static_branch_enable(&enable_evmcs);
13212 }
13213 } else {
13214 enlightened_vmcs = false;
13215 }
13216#endif
13217
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013218 r = vmx_setup_l1d_flush();
He, Qingfdef3ad2007-04-30 09:45:24 +030013219 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013220 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013221
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013222 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13223 __alignof__(struct vcpu_vmx), THIS_MODULE);
13224 if (r) {
13225 vmx_free_l1d_flush_pages();
13226 return r;
13227 }
13228
Dave Young2965faa2015-09-09 15:38:55 -070013229#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013230 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13231 crash_vmclear_local_loaded_vmcss);
13232#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013233 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013234
He, Qingfdef3ad2007-04-30 09:45:24 +030013235 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013236}
13237
13238static void __exit vmx_exit(void)
13239{
Dave Young2965faa2015-09-09 15:38:55 -070013240#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013241 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013242 synchronize_rcu();
13243#endif
13244
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013245 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013246
13247#if IS_ENABLED(CONFIG_HYPERV)
13248 if (static_branch_unlikely(&enable_evmcs)) {
13249 int cpu;
13250 struct hv_vp_assist_page *vp_ap;
13251 /*
13252 * Reset everything to support using non-enlightened VMCS
13253 * access later (e.g. when we reload the module with
13254 * enlightened_vmcs=0)
13255 */
13256 for_each_online_cpu(cpu) {
13257 vp_ap = hv_get_vp_assist_page(cpu);
13258
13259 if (!vp_ap)
13260 continue;
13261
13262 vp_ap->current_nested_vmcs = 0;
13263 vp_ap->enlighten_vmentry = 0;
13264 }
13265
13266 static_branch_disable(&enable_evmcs);
13267 }
13268#endif
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013269 vmx_free_l1d_flush_pages();
Avi Kivity6aa8b732006-12-10 02:21:36 -080013270}
13271
13272module_init(vmx_init)
13273module_exit(vmx_exit)