blob: 722026c9d4783c6c623425fc3930e2b8966e29c3 [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>
David Woodhouse117cc7a2018-01-12 11:11:27 +000054#include <asm/nospec-branch.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf41245002014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700191struct kvm_vmx {
192 struct kvm kvm;
193
194 unsigned int tss_addr;
195 bool ept_identity_pagetable_done;
196 gpa_t ept_identity_map_addr;
197};
198
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200199#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300200
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400201struct vmcs {
202 u32 revision_id;
203 u32 abort;
204 char data[0];
205};
206
Nadav Har'Eld462b812011-05-24 15:26:10 +0300207/*
208 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
209 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
210 * loaded on this CPU (so we can clear them if the CPU goes down).
211 */
212struct loaded_vmcs {
213 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700214 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300215 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200216 bool launched;
217 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200218 unsigned long vmcs_host_cr3; /* May not match real cr3 */
219 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100220 /* Support for vnmi-less CPUs */
221 int soft_vnmi_blocked;
222 ktime_t entry_time;
223 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100224 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300225 struct list_head loaded_vmcss_on_cpu_link;
226};
227
Avi Kivity26bb0982009-09-07 11:14:12 +0300228struct shared_msr_entry {
229 unsigned index;
230 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200231 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300232};
233
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300234/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300235 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
236 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
237 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
238 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
239 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
240 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600241 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300242 * underlying hardware which will be used to run L2.
243 * This structure is packed to ensure that its layout is identical across
244 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700245 *
246 * IMPORTANT: Changing the layout of existing fields in this structure
247 * will break save/restore compatibility with older kvm releases. When
248 * adding new fields, either use space in the reserved padding* arrays
249 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300250 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300251typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300252struct __packed vmcs12 {
253 /* According to the Intel spec, a VMCS region must start with the
254 * following two fields. Then follow implementation-specific data.
255 */
256 u32 revision_id;
257 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300258
Nadav Har'El27d6c862011-05-25 23:06:59 +0300259 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
260 u32 padding[7]; /* room for future expansion */
261
Nadav Har'El22bd0352011-05-25 23:05:57 +0300262 u64 io_bitmap_a;
263 u64 io_bitmap_b;
264 u64 msr_bitmap;
265 u64 vm_exit_msr_store_addr;
266 u64 vm_exit_msr_load_addr;
267 u64 vm_entry_msr_load_addr;
268 u64 tsc_offset;
269 u64 virtual_apic_page_addr;
270 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800271 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300272 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800273 u64 eoi_exit_bitmap0;
274 u64 eoi_exit_bitmap1;
275 u64 eoi_exit_bitmap2;
276 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800277 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300278 u64 guest_physical_address;
279 u64 vmcs_link_pointer;
280 u64 guest_ia32_debugctl;
281 u64 guest_ia32_pat;
282 u64 guest_ia32_efer;
283 u64 guest_ia32_perf_global_ctrl;
284 u64 guest_pdptr0;
285 u64 guest_pdptr1;
286 u64 guest_pdptr2;
287 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100288 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300289 u64 host_ia32_pat;
290 u64 host_ia32_efer;
291 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700292 u64 vmread_bitmap;
293 u64 vmwrite_bitmap;
294 u64 vm_function_control;
295 u64 eptp_list_address;
296 u64 pml_address;
297 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300298 /*
299 * To allow migration of L1 (complete with its L2 guests) between
300 * machines of different natural widths (32 or 64 bit), we cannot have
301 * unsigned long fields with no explict size. We use u64 (aliased
302 * natural_width) instead. Luckily, x86 is little-endian.
303 */
304 natural_width cr0_guest_host_mask;
305 natural_width cr4_guest_host_mask;
306 natural_width cr0_read_shadow;
307 natural_width cr4_read_shadow;
308 natural_width cr3_target_value0;
309 natural_width cr3_target_value1;
310 natural_width cr3_target_value2;
311 natural_width cr3_target_value3;
312 natural_width exit_qualification;
313 natural_width guest_linear_address;
314 natural_width guest_cr0;
315 natural_width guest_cr3;
316 natural_width guest_cr4;
317 natural_width guest_es_base;
318 natural_width guest_cs_base;
319 natural_width guest_ss_base;
320 natural_width guest_ds_base;
321 natural_width guest_fs_base;
322 natural_width guest_gs_base;
323 natural_width guest_ldtr_base;
324 natural_width guest_tr_base;
325 natural_width guest_gdtr_base;
326 natural_width guest_idtr_base;
327 natural_width guest_dr7;
328 natural_width guest_rsp;
329 natural_width guest_rip;
330 natural_width guest_rflags;
331 natural_width guest_pending_dbg_exceptions;
332 natural_width guest_sysenter_esp;
333 natural_width guest_sysenter_eip;
334 natural_width host_cr0;
335 natural_width host_cr3;
336 natural_width host_cr4;
337 natural_width host_fs_base;
338 natural_width host_gs_base;
339 natural_width host_tr_base;
340 natural_width host_gdtr_base;
341 natural_width host_idtr_base;
342 natural_width host_ia32_sysenter_esp;
343 natural_width host_ia32_sysenter_eip;
344 natural_width host_rsp;
345 natural_width host_rip;
346 natural_width paddingl[8]; /* room for future expansion */
347 u32 pin_based_vm_exec_control;
348 u32 cpu_based_vm_exec_control;
349 u32 exception_bitmap;
350 u32 page_fault_error_code_mask;
351 u32 page_fault_error_code_match;
352 u32 cr3_target_count;
353 u32 vm_exit_controls;
354 u32 vm_exit_msr_store_count;
355 u32 vm_exit_msr_load_count;
356 u32 vm_entry_controls;
357 u32 vm_entry_msr_load_count;
358 u32 vm_entry_intr_info_field;
359 u32 vm_entry_exception_error_code;
360 u32 vm_entry_instruction_len;
361 u32 tpr_threshold;
362 u32 secondary_vm_exec_control;
363 u32 vm_instruction_error;
364 u32 vm_exit_reason;
365 u32 vm_exit_intr_info;
366 u32 vm_exit_intr_error_code;
367 u32 idt_vectoring_info_field;
368 u32 idt_vectoring_error_code;
369 u32 vm_exit_instruction_len;
370 u32 vmx_instruction_info;
371 u32 guest_es_limit;
372 u32 guest_cs_limit;
373 u32 guest_ss_limit;
374 u32 guest_ds_limit;
375 u32 guest_fs_limit;
376 u32 guest_gs_limit;
377 u32 guest_ldtr_limit;
378 u32 guest_tr_limit;
379 u32 guest_gdtr_limit;
380 u32 guest_idtr_limit;
381 u32 guest_es_ar_bytes;
382 u32 guest_cs_ar_bytes;
383 u32 guest_ss_ar_bytes;
384 u32 guest_ds_ar_bytes;
385 u32 guest_fs_ar_bytes;
386 u32 guest_gs_ar_bytes;
387 u32 guest_ldtr_ar_bytes;
388 u32 guest_tr_ar_bytes;
389 u32 guest_interruptibility_info;
390 u32 guest_activity_state;
391 u32 guest_sysenter_cs;
392 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100393 u32 vmx_preemption_timer_value;
394 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300395 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800396 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300397 u16 guest_es_selector;
398 u16 guest_cs_selector;
399 u16 guest_ss_selector;
400 u16 guest_ds_selector;
401 u16 guest_fs_selector;
402 u16 guest_gs_selector;
403 u16 guest_ldtr_selector;
404 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800405 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300406 u16 host_es_selector;
407 u16 host_cs_selector;
408 u16 host_ss_selector;
409 u16 host_ds_selector;
410 u16 host_fs_selector;
411 u16 host_gs_selector;
412 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700413 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300414};
415
416/*
417 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
418 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
419 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700420 *
421 * IMPORTANT: Changing this value will break save/restore compatibility with
422 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300423 */
424#define VMCS12_REVISION 0x11e57ed0
425
426/*
427 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
428 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
429 * current implementation, 4K are reserved to avoid future complications.
430 */
431#define VMCS12_SIZE 0x1000
432
433/*
Jim Mattson5b157062017-12-22 12:11:12 -0800434 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
435 * supported VMCS12 field encoding.
436 */
437#define VMCS12_MAX_FIELD_INDEX 0x17
438
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100439struct nested_vmx_msrs {
440 /*
441 * We only store the "true" versions of the VMX capability MSRs. We
442 * generate the "non-true" versions by setting the must-be-1 bits
443 * according to the SDM.
444 */
445 u32 procbased_ctls_low;
446 u32 procbased_ctls_high;
447 u32 secondary_ctls_low;
448 u32 secondary_ctls_high;
449 u32 pinbased_ctls_low;
450 u32 pinbased_ctls_high;
451 u32 exit_ctls_low;
452 u32 exit_ctls_high;
453 u32 entry_ctls_low;
454 u32 entry_ctls_high;
455 u32 misc_low;
456 u32 misc_high;
457 u32 ept_caps;
458 u32 vpid_caps;
459 u64 basic;
460 u64 cr0_fixed0;
461 u64 cr0_fixed1;
462 u64 cr4_fixed0;
463 u64 cr4_fixed1;
464 u64 vmcs_enum;
465 u64 vmfunc_controls;
466};
467
Jim Mattson5b157062017-12-22 12:11:12 -0800468/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300469 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
470 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
471 */
472struct nested_vmx {
473 /* Has the level1 guest done vmxon? */
474 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400475 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400476 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300477
478 /* The guest-physical address of the current VMCS L1 keeps for L2 */
479 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700480 /*
481 * Cache of the guest's VMCS, existing outside of guest memory.
482 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700483 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700484 */
485 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300486 /*
487 * Indicates if the shadow vmcs must be updated with the
488 * data hold by vmcs12
489 */
490 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100491 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300492
Jim Mattson8d860bb2018-05-09 16:56:05 -0400493 bool change_vmcs01_virtual_apic_mode;
494
Nadav Har'El644d7112011-05-25 23:12:35 +0300495 /* L2 must run next, and mustn't decide to exit to L1. */
496 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600497
498 struct loaded_vmcs vmcs02;
499
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300500 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600501 * Guest pages referred to in the vmcs02 with host-physical
502 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300503 */
504 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800505 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800506 struct page *pi_desc_page;
507 struct pi_desc *pi_desc;
508 bool pi_pending;
509 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100510
511 struct hrtimer preemption_timer;
512 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200513
514 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
515 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800516
Wanpeng Li5c614b32015-10-13 09:18:36 -0700517 u16 vpid02;
518 u16 last_vpid;
519
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100520 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200521
522 /* SMM related state */
523 struct {
524 /* in VMX operation on SMM entry? */
525 bool vmxon;
526 /* in guest mode on SMM entry? */
527 bool guest_mode;
528 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300529};
530
Yang Zhang01e439b2013-04-11 19:25:12 +0800531#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800532#define POSTED_INTR_SN 1
533
Yang Zhang01e439b2013-04-11 19:25:12 +0800534/* Posted-Interrupt Descriptor */
535struct pi_desc {
536 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800537 union {
538 struct {
539 /* bit 256 - Outstanding Notification */
540 u16 on : 1,
541 /* bit 257 - Suppress Notification */
542 sn : 1,
543 /* bit 271:258 - Reserved */
544 rsvd_1 : 14;
545 /* bit 279:272 - Notification Vector */
546 u8 nv;
547 /* bit 287:280 - Reserved */
548 u8 rsvd_2;
549 /* bit 319:288 - Notification Destination */
550 u32 ndst;
551 };
552 u64 control;
553 };
554 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800555} __aligned(64);
556
Yang Zhanga20ed542013-04-11 19:25:15 +0800557static bool pi_test_and_set_on(struct pi_desc *pi_desc)
558{
559 return test_and_set_bit(POSTED_INTR_ON,
560 (unsigned long *)&pi_desc->control);
561}
562
563static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
564{
565 return test_and_clear_bit(POSTED_INTR_ON,
566 (unsigned long *)&pi_desc->control);
567}
568
569static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
570{
571 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
572}
573
Feng Wuebbfc762015-09-18 22:29:46 +0800574static inline void pi_clear_sn(struct pi_desc *pi_desc)
575{
576 return clear_bit(POSTED_INTR_SN,
577 (unsigned long *)&pi_desc->control);
578}
579
580static inline void pi_set_sn(struct pi_desc *pi_desc)
581{
582 return set_bit(POSTED_INTR_SN,
583 (unsigned long *)&pi_desc->control);
584}
585
Paolo Bonziniad361092016-09-20 16:15:05 +0200586static inline void pi_clear_on(struct pi_desc *pi_desc)
587{
588 clear_bit(POSTED_INTR_ON,
589 (unsigned long *)&pi_desc->control);
590}
591
Feng Wuebbfc762015-09-18 22:29:46 +0800592static inline int pi_test_on(struct pi_desc *pi_desc)
593{
594 return test_bit(POSTED_INTR_ON,
595 (unsigned long *)&pi_desc->control);
596}
597
598static inline int pi_test_sn(struct pi_desc *pi_desc)
599{
600 return test_bit(POSTED_INTR_SN,
601 (unsigned long *)&pi_desc->control);
602}
603
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400604struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000605 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300606 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300607 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100608 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300609 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200610 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200611 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300612 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613 int nmsrs;
614 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800615 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300617 u64 msr_host_kernel_gs_base;
618 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400619#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100620
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100621 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100622 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100623
Gleb Natapov2961e8762013-11-25 15:37:13 +0200624 u32 vm_entry_controls_shadow;
625 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200626 u32 secondary_exec_control;
627
Nadav Har'Eld462b812011-05-24 15:26:10 +0300628 /*
629 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
630 * non-nested (L1) guest, it always points to vmcs01. For a nested
631 * guest (L2), it points to a different VMCS.
632 */
633 struct loaded_vmcs vmcs01;
634 struct loaded_vmcs *loaded_vmcs;
635 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300636 struct msr_autoload {
637 unsigned nr;
638 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
639 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
640 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641 struct {
642 int loaded;
643 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300644#ifdef CONFIG_X86_64
645 u16 ds_sel, es_sel;
646#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200647 int gs_ldt_reload_needed;
648 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000649 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400650 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200651 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300652 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300653 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300654 struct kvm_segment segs[8];
655 } rmode;
656 struct {
657 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300658 struct kvm_save_segment {
659 u16 selector;
660 unsigned long base;
661 u32 limit;
662 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300663 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300664 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800665 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300666 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200667
Andi Kleena0861c02009-06-08 17:37:09 +0800668 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800669
Yang Zhang01e439b2013-04-11 19:25:12 +0800670 /* Posted interrupt descriptor */
671 struct pi_desc pi_desc;
672
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300673 /* Support for a guest hypervisor (nested VMX) */
674 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200675
676 /* Dynamic PLE window. */
677 int ple_window;
678 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800679
680 /* Support for PML */
681#define PML_ENTITY_NUM 512
682 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800683
Yunhong Jiang64672c92016-06-13 14:19:59 -0700684 /* apic deadline value in host tsc */
685 u64 hv_deadline_tsc;
686
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800687 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800688
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800689 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800690
Wanpeng Li74c55932017-11-29 01:31:20 -0800691 unsigned long host_debugctlmsr;
692
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800693 /*
694 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
695 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
696 * in msr_ia32_feature_control_valid_bits.
697 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800698 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800699 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400700};
701
Avi Kivity2fb92db2011-04-27 19:42:18 +0300702enum segment_cache_field {
703 SEG_FIELD_SEL = 0,
704 SEG_FIELD_BASE = 1,
705 SEG_FIELD_LIMIT = 2,
706 SEG_FIELD_AR = 3,
707
708 SEG_FIELD_NR = 4
709};
710
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700711static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
712{
713 return container_of(kvm, struct kvm_vmx, kvm);
714}
715
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400716static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
717{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000718 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400719}
720
Feng Wuefc64402015-09-18 22:29:51 +0800721static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
722{
723 return &(to_vmx(vcpu)->pi_desc);
724}
725
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800726#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300727#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800728#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
729#define FIELD64(number, name) \
730 FIELD(number, name), \
731 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300732
Abel Gordon4607c2d2013-04-18 14:35:55 +0300733
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100734static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100735#define SHADOW_FIELD_RO(x) x,
736#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300737};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400738static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300739 ARRAY_SIZE(shadow_read_only_fields);
740
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100741static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100742#define SHADOW_FIELD_RW(x) x,
743#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300744};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400745static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300746 ARRAY_SIZE(shadow_read_write_fields);
747
Mathias Krause772e0312012-08-30 01:30:19 +0200748static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300749 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800750 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300751 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
752 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
753 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
754 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
755 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
756 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
757 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
758 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800759 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400760 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300761 FIELD(HOST_ES_SELECTOR, host_es_selector),
762 FIELD(HOST_CS_SELECTOR, host_cs_selector),
763 FIELD(HOST_SS_SELECTOR, host_ss_selector),
764 FIELD(HOST_DS_SELECTOR, host_ds_selector),
765 FIELD(HOST_FS_SELECTOR, host_fs_selector),
766 FIELD(HOST_GS_SELECTOR, host_gs_selector),
767 FIELD(HOST_TR_SELECTOR, host_tr_selector),
768 FIELD64(IO_BITMAP_A, io_bitmap_a),
769 FIELD64(IO_BITMAP_B, io_bitmap_b),
770 FIELD64(MSR_BITMAP, msr_bitmap),
771 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
772 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
773 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700774 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300775 FIELD64(TSC_OFFSET, tsc_offset),
776 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
777 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800778 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400779 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300780 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800781 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
782 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
783 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
784 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400785 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700786 FIELD64(VMREAD_BITMAP, vmread_bitmap),
787 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800788 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300789 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
790 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
791 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
792 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
793 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
794 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
795 FIELD64(GUEST_PDPTR0, guest_pdptr0),
796 FIELD64(GUEST_PDPTR1, guest_pdptr1),
797 FIELD64(GUEST_PDPTR2, guest_pdptr2),
798 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100799 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300800 FIELD64(HOST_IA32_PAT, host_ia32_pat),
801 FIELD64(HOST_IA32_EFER, host_ia32_efer),
802 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
803 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
804 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
805 FIELD(EXCEPTION_BITMAP, exception_bitmap),
806 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
807 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
808 FIELD(CR3_TARGET_COUNT, cr3_target_count),
809 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
810 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
811 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
812 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
813 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
814 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
815 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
816 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
817 FIELD(TPR_THRESHOLD, tpr_threshold),
818 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
819 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
820 FIELD(VM_EXIT_REASON, vm_exit_reason),
821 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
822 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
823 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
824 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
825 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
826 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
827 FIELD(GUEST_ES_LIMIT, guest_es_limit),
828 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
829 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
830 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
831 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
832 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
833 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
834 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
835 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
836 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
837 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
838 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
839 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
840 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
841 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
842 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
843 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
844 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
845 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
846 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
847 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
848 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100849 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300850 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
851 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
852 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
853 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
854 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
855 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
856 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
857 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
858 FIELD(EXIT_QUALIFICATION, exit_qualification),
859 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
860 FIELD(GUEST_CR0, guest_cr0),
861 FIELD(GUEST_CR3, guest_cr3),
862 FIELD(GUEST_CR4, guest_cr4),
863 FIELD(GUEST_ES_BASE, guest_es_base),
864 FIELD(GUEST_CS_BASE, guest_cs_base),
865 FIELD(GUEST_SS_BASE, guest_ss_base),
866 FIELD(GUEST_DS_BASE, guest_ds_base),
867 FIELD(GUEST_FS_BASE, guest_fs_base),
868 FIELD(GUEST_GS_BASE, guest_gs_base),
869 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
870 FIELD(GUEST_TR_BASE, guest_tr_base),
871 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
872 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
873 FIELD(GUEST_DR7, guest_dr7),
874 FIELD(GUEST_RSP, guest_rsp),
875 FIELD(GUEST_RIP, guest_rip),
876 FIELD(GUEST_RFLAGS, guest_rflags),
877 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
878 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
879 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
880 FIELD(HOST_CR0, host_cr0),
881 FIELD(HOST_CR3, host_cr3),
882 FIELD(HOST_CR4, host_cr4),
883 FIELD(HOST_FS_BASE, host_fs_base),
884 FIELD(HOST_GS_BASE, host_gs_base),
885 FIELD(HOST_TR_BASE, host_tr_base),
886 FIELD(HOST_GDTR_BASE, host_gdtr_base),
887 FIELD(HOST_IDTR_BASE, host_idtr_base),
888 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
889 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
890 FIELD(HOST_RSP, host_rsp),
891 FIELD(HOST_RIP, host_rip),
892};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300893
894static inline short vmcs_field_to_offset(unsigned long field)
895{
Dan Williams085331d2018-01-31 17:47:03 -0800896 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
897 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800898 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100899
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800900 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800901 return -ENOENT;
902
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800903 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800904 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800905 return -ENOENT;
906
Linus Torvalds15303ba2018-02-10 13:16:35 -0800907 index = array_index_nospec(index, size);
908 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800909 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100910 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800911 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300912}
913
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300914static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
915{
David Matlack4f2777b2016-07-13 17:16:37 -0700916 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300917}
918
Peter Feiner995f00a2017-06-30 17:26:32 -0700919static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300920static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700921static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800922static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300923static void vmx_set_segment(struct kvm_vcpu *vcpu,
924 struct kvm_segment *var, int seg);
925static void vmx_get_segment(struct kvm_vcpu *vcpu,
926 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200927static bool guest_state_valid(struct kvm_vcpu *vcpu);
928static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300929static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200930static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
931static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
932static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
933 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100934static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100935static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
936 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938static DEFINE_PER_CPU(struct vmcs *, vmxarea);
939static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300940/*
941 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
942 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
943 */
944static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Feng Wubf9f6ac2015-09-18 22:29:55 +0800946/*
947 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
948 * can find which vCPU should be waken up.
949 */
950static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
951static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
952
Radim Krčmář23611332016-09-29 22:41:33 +0200953enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200954 VMX_VMREAD_BITMAP,
955 VMX_VMWRITE_BITMAP,
956 VMX_BITMAP_NR
957};
958
959static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
960
Radim Krčmář23611332016-09-29 22:41:33 +0200961#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
962#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300963
Avi Kivity110312c2010-12-21 12:54:20 +0200964static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200965static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200966
Sheng Yang2384d2b2008-01-17 15:14:33 +0800967static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
968static DEFINE_SPINLOCK(vmx_vpid_lock);
969
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 int size;
972 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300973 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300975 u32 pin_based_exec_ctrl;
976 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800977 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300978 u32 vmexit_ctrl;
979 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100980 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300981} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Hannes Ederefff9e52008-11-28 17:02:06 +0100983static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800984 u32 ept;
985 u32 vpid;
986} vmx_capability;
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988#define VMX_SEGMENT_FIELD(seg) \
989 [VCPU_SREG_##seg] = { \
990 .selector = GUEST_##seg##_SELECTOR, \
991 .base = GUEST_##seg##_BASE, \
992 .limit = GUEST_##seg##_LIMIT, \
993 .ar_bytes = GUEST_##seg##_AR_BYTES, \
994 }
995
Mathias Krause772e0312012-08-30 01:30:19 +0200996static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 unsigned selector;
998 unsigned base;
999 unsigned limit;
1000 unsigned ar_bytes;
1001} kvm_vmx_segment_fields[] = {
1002 VMX_SEGMENT_FIELD(CS),
1003 VMX_SEGMENT_FIELD(DS),
1004 VMX_SEGMENT_FIELD(ES),
1005 VMX_SEGMENT_FIELD(FS),
1006 VMX_SEGMENT_FIELD(GS),
1007 VMX_SEGMENT_FIELD(SS),
1008 VMX_SEGMENT_FIELD(TR),
1009 VMX_SEGMENT_FIELD(LDTR),
1010};
1011
Avi Kivity26bb0982009-09-07 11:14:12 +03001012static u64 host_efer;
1013
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001014static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1015
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001016/*
Brian Gerst8c065852010-07-17 09:03:26 -04001017 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001018 * away by decrementing the array size.
1019 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001021#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001022 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001024 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001027DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1028
1029#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1030
1031#define KVM_EVMCS_VERSION 1
1032
1033#if IS_ENABLED(CONFIG_HYPERV)
1034static bool __read_mostly enlightened_vmcs = true;
1035module_param(enlightened_vmcs, bool, 0444);
1036
1037static inline void evmcs_write64(unsigned long field, u64 value)
1038{
1039 u16 clean_field;
1040 int offset = get_evmcs_offset(field, &clean_field);
1041
1042 if (offset < 0)
1043 return;
1044
1045 *(u64 *)((char *)current_evmcs + offset) = value;
1046
1047 current_evmcs->hv_clean_fields &= ~clean_field;
1048}
1049
1050static inline void evmcs_write32(unsigned long field, u32 value)
1051{
1052 u16 clean_field;
1053 int offset = get_evmcs_offset(field, &clean_field);
1054
1055 if (offset < 0)
1056 return;
1057
1058 *(u32 *)((char *)current_evmcs + offset) = value;
1059 current_evmcs->hv_clean_fields &= ~clean_field;
1060}
1061
1062static inline void evmcs_write16(unsigned long field, u16 value)
1063{
1064 u16 clean_field;
1065 int offset = get_evmcs_offset(field, &clean_field);
1066
1067 if (offset < 0)
1068 return;
1069
1070 *(u16 *)((char *)current_evmcs + offset) = value;
1071 current_evmcs->hv_clean_fields &= ~clean_field;
1072}
1073
1074static inline u64 evmcs_read64(unsigned long field)
1075{
1076 int offset = get_evmcs_offset(field, NULL);
1077
1078 if (offset < 0)
1079 return 0;
1080
1081 return *(u64 *)((char *)current_evmcs + offset);
1082}
1083
1084static inline u32 evmcs_read32(unsigned long field)
1085{
1086 int offset = get_evmcs_offset(field, NULL);
1087
1088 if (offset < 0)
1089 return 0;
1090
1091 return *(u32 *)((char *)current_evmcs + offset);
1092}
1093
1094static inline u16 evmcs_read16(unsigned long field)
1095{
1096 int offset = get_evmcs_offset(field, NULL);
1097
1098 if (offset < 0)
1099 return 0;
1100
1101 return *(u16 *)((char *)current_evmcs + offset);
1102}
1103
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001104static inline void evmcs_touch_msr_bitmap(void)
1105{
1106 if (unlikely(!current_evmcs))
1107 return;
1108
1109 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1110 current_evmcs->hv_clean_fields &=
1111 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1112}
1113
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001114static void evmcs_load(u64 phys_addr)
1115{
1116 struct hv_vp_assist_page *vp_ap =
1117 hv_get_vp_assist_page(smp_processor_id());
1118
1119 vp_ap->current_nested_vmcs = phys_addr;
1120 vp_ap->enlighten_vmentry = 1;
1121}
1122
1123static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1124{
1125 /*
1126 * Enlightened VMCSv1 doesn't support these:
1127 *
1128 * POSTED_INTR_NV = 0x00000002,
1129 * GUEST_INTR_STATUS = 0x00000810,
1130 * APIC_ACCESS_ADDR = 0x00002014,
1131 * POSTED_INTR_DESC_ADDR = 0x00002016,
1132 * EOI_EXIT_BITMAP0 = 0x0000201c,
1133 * EOI_EXIT_BITMAP1 = 0x0000201e,
1134 * EOI_EXIT_BITMAP2 = 0x00002020,
1135 * EOI_EXIT_BITMAP3 = 0x00002022,
1136 */
1137 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1138 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1139 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1140 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1141 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1142 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1143 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1144
1145 /*
1146 * GUEST_PML_INDEX = 0x00000812,
1147 * PML_ADDRESS = 0x0000200e,
1148 */
1149 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1150
1151 /* VM_FUNCTION_CONTROL = 0x00002018, */
1152 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1153
1154 /*
1155 * EPTP_LIST_ADDRESS = 0x00002024,
1156 * VMREAD_BITMAP = 0x00002026,
1157 * VMWRITE_BITMAP = 0x00002028,
1158 */
1159 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1160
1161 /*
1162 * TSC_MULTIPLIER = 0x00002032,
1163 */
1164 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1165
1166 /*
1167 * PLE_GAP = 0x00004020,
1168 * PLE_WINDOW = 0x00004022,
1169 */
1170 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1171
1172 /*
1173 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1174 */
1175 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1176
1177 /*
1178 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1179 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1180 */
1181 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1182 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1183
1184 /*
1185 * Currently unsupported in KVM:
1186 * GUEST_IA32_RTIT_CTL = 0x00002814,
1187 */
1188}
1189#else /* !IS_ENABLED(CONFIG_HYPERV) */
1190static inline void evmcs_write64(unsigned long field, u64 value) {}
1191static inline void evmcs_write32(unsigned long field, u32 value) {}
1192static inline void evmcs_write16(unsigned long field, u16 value) {}
1193static inline u64 evmcs_read64(unsigned long field) { return 0; }
1194static inline u32 evmcs_read32(unsigned long field) { return 0; }
1195static inline u16 evmcs_read16(unsigned long field) { return 0; }
1196static inline void evmcs_load(u64 phys_addr) {}
1197static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001198static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001199#endif /* IS_ENABLED(CONFIG_HYPERV) */
1200
Jan Kiszka5bb16012016-02-09 20:14:21 +01001201static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202{
1203 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1204 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001205 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1206}
1207
Jan Kiszka6f054852016-02-09 20:15:18 +01001208static inline bool is_debug(u32 intr_info)
1209{
1210 return is_exception_n(intr_info, DB_VECTOR);
1211}
1212
1213static inline bool is_breakpoint(u32 intr_info)
1214{
1215 return is_exception_n(intr_info, BP_VECTOR);
1216}
1217
Jan Kiszka5bb16012016-02-09 20:14:21 +01001218static inline bool is_page_fault(u32 intr_info)
1219{
1220 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221}
1222
Gui Jianfeng31299942010-03-15 17:29:09 +08001223static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001224{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001225 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001226}
1227
Gui Jianfeng31299942010-03-15 17:29:09 +08001228static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001229{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001230 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001231}
1232
Liran Alon9e869482018-03-12 13:12:51 +02001233static inline bool is_gp_fault(u32 intr_info)
1234{
1235 return is_exception_n(intr_info, GP_VECTOR);
1236}
1237
Gui Jianfeng31299942010-03-15 17:29:09 +08001238static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239{
1240 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1241 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1242}
1243
Gui Jianfeng31299942010-03-15 17:29:09 +08001244static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001245{
1246 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1247 INTR_INFO_VALID_MASK)) ==
1248 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1249}
1250
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001251/* Undocumented: icebp/int1 */
1252static inline bool is_icebp(u32 intr_info)
1253{
1254 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1255 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1256}
1257
Gui Jianfeng31299942010-03-15 17:29:09 +08001258static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001259{
Sheng Yang04547152009-04-01 15:52:31 +08001260 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001261}
1262
Gui Jianfeng31299942010-03-15 17:29:09 +08001263static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001264{
Sheng Yang04547152009-04-01 15:52:31 +08001265 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001266}
1267
Paolo Bonzini35754c92015-07-29 12:05:37 +02001268static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001269{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001270 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001271}
1272
Gui Jianfeng31299942010-03-15 17:29:09 +08001273static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001274{
Sheng Yang04547152009-04-01 15:52:31 +08001275 return vmcs_config.cpu_based_exec_ctrl &
1276 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001277}
1278
Avi Kivity774ead32007-12-26 13:57:04 +02001279static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001280{
Sheng Yang04547152009-04-01 15:52:31 +08001281 return vmcs_config.cpu_based_2nd_exec_ctrl &
1282 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1283}
1284
Yang Zhang8d146952013-01-25 10:18:50 +08001285static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1286{
1287 return vmcs_config.cpu_based_2nd_exec_ctrl &
1288 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1289}
1290
Yang Zhang83d4c282013-01-25 10:18:49 +08001291static inline bool cpu_has_vmx_apic_register_virt(void)
1292{
1293 return vmcs_config.cpu_based_2nd_exec_ctrl &
1294 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1295}
1296
Yang Zhangc7c9c562013-01-25 10:18:51 +08001297static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1298{
1299 return vmcs_config.cpu_based_2nd_exec_ctrl &
1300 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1301}
1302
Yunhong Jiang64672c92016-06-13 14:19:59 -07001303/*
1304 * Comment's format: document - errata name - stepping - processor name.
1305 * Refer from
1306 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1307 */
1308static u32 vmx_preemption_cpu_tfms[] = {
1309/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
13100x000206E6,
1311/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1312/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1313/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
13140x00020652,
1315/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
13160x00020655,
1317/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1318/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1319/*
1320 * 320767.pdf - AAP86 - B1 -
1321 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1322 */
13230x000106E5,
1324/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
13250x000106A0,
1326/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
13270x000106A1,
1328/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
13290x000106A4,
1330 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1331 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1332 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
13330x000106A5,
1334};
1335
1336static inline bool cpu_has_broken_vmx_preemption_timer(void)
1337{
1338 u32 eax = cpuid_eax(0x00000001), i;
1339
1340 /* Clear the reserved bits */
1341 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001342 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001343 if (eax == vmx_preemption_cpu_tfms[i])
1344 return true;
1345
1346 return false;
1347}
1348
1349static inline bool cpu_has_vmx_preemption_timer(void)
1350{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001351 return vmcs_config.pin_based_exec_ctrl &
1352 PIN_BASED_VMX_PREEMPTION_TIMER;
1353}
1354
Yang Zhang01e439b2013-04-11 19:25:12 +08001355static inline bool cpu_has_vmx_posted_intr(void)
1356{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001357 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1358 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001359}
1360
1361static inline bool cpu_has_vmx_apicv(void)
1362{
1363 return cpu_has_vmx_apic_register_virt() &&
1364 cpu_has_vmx_virtual_intr_delivery() &&
1365 cpu_has_vmx_posted_intr();
1366}
1367
Sheng Yang04547152009-04-01 15:52:31 +08001368static inline bool cpu_has_vmx_flexpriority(void)
1369{
1370 return cpu_has_vmx_tpr_shadow() &&
1371 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001372}
1373
Marcelo Tosattie7997942009-06-11 12:07:40 -03001374static inline bool cpu_has_vmx_ept_execute_only(void)
1375{
Gui Jianfeng31299942010-03-15 17:29:09 +08001376 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001377}
1378
Marcelo Tosattie7997942009-06-11 12:07:40 -03001379static inline bool cpu_has_vmx_ept_2m_page(void)
1380{
Gui Jianfeng31299942010-03-15 17:29:09 +08001381 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001382}
1383
Sheng Yang878403b2010-01-05 19:02:29 +08001384static inline bool cpu_has_vmx_ept_1g_page(void)
1385{
Gui Jianfeng31299942010-03-15 17:29:09 +08001386 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001387}
1388
Sheng Yang4bc9b982010-06-02 14:05:24 +08001389static inline bool cpu_has_vmx_ept_4levels(void)
1390{
1391 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1392}
1393
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001394static inline bool cpu_has_vmx_ept_mt_wb(void)
1395{
1396 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1397}
1398
Yu Zhang855feb62017-08-24 20:27:55 +08001399static inline bool cpu_has_vmx_ept_5levels(void)
1400{
1401 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1402}
1403
Xudong Hao83c3a332012-05-28 19:33:35 +08001404static inline bool cpu_has_vmx_ept_ad_bits(void)
1405{
1406 return vmx_capability.ept & VMX_EPT_AD_BIT;
1407}
1408
Gui Jianfeng31299942010-03-15 17:29:09 +08001409static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001410{
Gui Jianfeng31299942010-03-15 17:29:09 +08001411 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001412}
1413
Gui Jianfeng31299942010-03-15 17:29:09 +08001414static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001415{
Gui Jianfeng31299942010-03-15 17:29:09 +08001416 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001417}
1418
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001419static inline bool cpu_has_vmx_invvpid_single(void)
1420{
1421 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1422}
1423
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001424static inline bool cpu_has_vmx_invvpid_global(void)
1425{
1426 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1427}
1428
Wanpeng Li08d839c2017-03-23 05:30:08 -07001429static inline bool cpu_has_vmx_invvpid(void)
1430{
1431 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1432}
1433
Gui Jianfeng31299942010-03-15 17:29:09 +08001434static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001435{
Sheng Yang04547152009-04-01 15:52:31 +08001436 return vmcs_config.cpu_based_2nd_exec_ctrl &
1437 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001438}
1439
Gui Jianfeng31299942010-03-15 17:29:09 +08001440static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001441{
1442 return vmcs_config.cpu_based_2nd_exec_ctrl &
1443 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1444}
1445
Gui Jianfeng31299942010-03-15 17:29:09 +08001446static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001447{
1448 return vmcs_config.cpu_based_2nd_exec_ctrl &
1449 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1450}
1451
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001452static inline bool cpu_has_vmx_basic_inout(void)
1453{
1454 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1455}
1456
Paolo Bonzini35754c92015-07-29 12:05:37 +02001457static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001458{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001459 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001460}
1461
Gui Jianfeng31299942010-03-15 17:29:09 +08001462static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001463{
Sheng Yang04547152009-04-01 15:52:31 +08001464 return vmcs_config.cpu_based_2nd_exec_ctrl &
1465 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001466}
1467
Gui Jianfeng31299942010-03-15 17:29:09 +08001468static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001469{
1470 return vmcs_config.cpu_based_2nd_exec_ctrl &
1471 SECONDARY_EXEC_RDTSCP;
1472}
1473
Mao, Junjiead756a12012-07-02 01:18:48 +00001474static inline bool cpu_has_vmx_invpcid(void)
1475{
1476 return vmcs_config.cpu_based_2nd_exec_ctrl &
1477 SECONDARY_EXEC_ENABLE_INVPCID;
1478}
1479
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001480static inline bool cpu_has_virtual_nmis(void)
1481{
1482 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1483}
1484
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001485static inline bool cpu_has_vmx_wbinvd_exit(void)
1486{
1487 return vmcs_config.cpu_based_2nd_exec_ctrl &
1488 SECONDARY_EXEC_WBINVD_EXITING;
1489}
1490
Abel Gordonabc4fc52013-04-18 14:35:25 +03001491static inline bool cpu_has_vmx_shadow_vmcs(void)
1492{
1493 u64 vmx_msr;
1494 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1495 /* check if the cpu supports writing r/o exit information fields */
1496 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1497 return false;
1498
1499 return vmcs_config.cpu_based_2nd_exec_ctrl &
1500 SECONDARY_EXEC_SHADOW_VMCS;
1501}
1502
Kai Huang843e4332015-01-28 10:54:28 +08001503static inline bool cpu_has_vmx_pml(void)
1504{
1505 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1506}
1507
Haozhong Zhang64903d62015-10-20 15:39:09 +08001508static inline bool cpu_has_vmx_tsc_scaling(void)
1509{
1510 return vmcs_config.cpu_based_2nd_exec_ctrl &
1511 SECONDARY_EXEC_TSC_SCALING;
1512}
1513
Bandan Das2a499e42017-08-03 15:54:41 -04001514static inline bool cpu_has_vmx_vmfunc(void)
1515{
1516 return vmcs_config.cpu_based_2nd_exec_ctrl &
1517 SECONDARY_EXEC_ENABLE_VMFUNC;
1518}
1519
Sean Christopherson64f7a112018-04-30 10:01:06 -07001520static bool vmx_umip_emulated(void)
1521{
1522 return vmcs_config.cpu_based_2nd_exec_ctrl &
1523 SECONDARY_EXEC_DESC;
1524}
1525
Sheng Yang04547152009-04-01 15:52:31 +08001526static inline bool report_flexpriority(void)
1527{
1528 return flexpriority_enabled;
1529}
1530
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001531static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1532{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001533 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001534}
1535
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001536static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1537{
1538 return vmcs12->cpu_based_vm_exec_control & bit;
1539}
1540
1541static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1542{
1543 return (vmcs12->cpu_based_vm_exec_control &
1544 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1545 (vmcs12->secondary_vm_exec_control & bit);
1546}
1547
Jan Kiszkaf41245002014-03-07 20:03:13 +01001548static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1549{
1550 return vmcs12->pin_based_vm_exec_control &
1551 PIN_BASED_VMX_PREEMPTION_TIMER;
1552}
1553
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001554static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1555{
1556 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1557}
1558
1559static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1560{
1561 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1562}
1563
Nadav Har'El155a97a2013-08-05 11:07:16 +03001564static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1565{
1566 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1567}
1568
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001569static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1570{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001571 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001572}
1573
Bandan Dasc5f983f2017-05-05 15:25:14 -04001574static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1575{
1576 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1577}
1578
Wincy Vanf2b93282015-02-03 23:56:03 +08001579static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1580{
1581 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1582}
1583
Wanpeng Li5c614b32015-10-13 09:18:36 -07001584static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1585{
1586 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1587}
1588
Wincy Van82f0dd42015-02-03 23:57:18 +08001589static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1590{
1591 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1592}
1593
Wincy Van608406e2015-02-03 23:57:51 +08001594static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1595{
1596 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1597}
1598
Wincy Van705699a2015-02-03 23:58:17 +08001599static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1600{
1601 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1602}
1603
Bandan Das27c42a12017-08-03 15:54:42 -04001604static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1605{
1606 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1607}
1608
Bandan Das41ab9372017-08-03 15:54:43 -04001609static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1610{
1611 return nested_cpu_has_vmfunc(vmcs12) &&
1612 (vmcs12->vm_function_control &
1613 VMX_VMFUNC_EPTP_SWITCHING);
1614}
1615
Jim Mattsonef85b672016-12-12 11:01:37 -08001616static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001617{
1618 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001619 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001620}
1621
Jan Kiszka533558b2014-01-04 18:47:20 +01001622static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1623 u32 exit_intr_info,
1624 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001625static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1626 struct vmcs12 *vmcs12,
1627 u32 reason, unsigned long qualification);
1628
Rusty Russell8b9cf982007-07-30 16:31:43 +10001629static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001630{
1631 int i;
1632
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001633 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001634 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001635 return i;
1636 return -1;
1637}
1638
Sheng Yang2384d2b2008-01-17 15:14:33 +08001639static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1640{
1641 struct {
1642 u64 vpid : 16;
1643 u64 rsvd : 48;
1644 u64 gva;
1645 } operand = { vpid, 0, gva };
1646
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001647 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001648 /* CF==1 or ZF==1 --> rc = -1 */
1649 "; ja 1f ; ud2 ; 1:"
1650 : : "a"(&operand), "c"(ext) : "cc", "memory");
1651}
1652
Sheng Yang14394422008-04-28 12:24:45 +08001653static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1654{
1655 struct {
1656 u64 eptp, gpa;
1657 } operand = {eptp, gpa};
1658
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001659 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001660 /* CF==1 or ZF==1 --> rc = -1 */
1661 "; ja 1f ; ud2 ; 1:\n"
1662 : : "a" (&operand), "c" (ext) : "cc", "memory");
1663}
1664
Avi Kivity26bb0982009-09-07 11:14:12 +03001665static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001666{
1667 int i;
1668
Rusty Russell8b9cf982007-07-30 16:31:43 +10001669 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001670 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001671 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001672 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001673}
1674
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675static void vmcs_clear(struct vmcs *vmcs)
1676{
1677 u64 phys_addr = __pa(vmcs);
1678 u8 error;
1679
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001680 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001681 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682 : "cc", "memory");
1683 if (error)
1684 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1685 vmcs, phys_addr);
1686}
1687
Nadav Har'Eld462b812011-05-24 15:26:10 +03001688static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1689{
1690 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001691 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1692 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001693 loaded_vmcs->cpu = -1;
1694 loaded_vmcs->launched = 0;
1695}
1696
Dongxiao Xu7725b892010-05-11 18:29:38 +08001697static void vmcs_load(struct vmcs *vmcs)
1698{
1699 u64 phys_addr = __pa(vmcs);
1700 u8 error;
1701
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001702 if (static_branch_unlikely(&enable_evmcs))
1703 return evmcs_load(phys_addr);
1704
Dongxiao Xu7725b892010-05-11 18:29:38 +08001705 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001706 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001707 : "cc", "memory");
1708 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001709 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001710 vmcs, phys_addr);
1711}
1712
Dave Young2965faa2015-09-09 15:38:55 -07001713#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001714/*
1715 * This bitmap is used to indicate whether the vmclear
1716 * operation is enabled on all cpus. All disabled by
1717 * default.
1718 */
1719static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1720
1721static inline void crash_enable_local_vmclear(int cpu)
1722{
1723 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1724}
1725
1726static inline void crash_disable_local_vmclear(int cpu)
1727{
1728 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1729}
1730
1731static inline int crash_local_vmclear_enabled(int cpu)
1732{
1733 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1734}
1735
1736static void crash_vmclear_local_loaded_vmcss(void)
1737{
1738 int cpu = raw_smp_processor_id();
1739 struct loaded_vmcs *v;
1740
1741 if (!crash_local_vmclear_enabled(cpu))
1742 return;
1743
1744 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1745 loaded_vmcss_on_cpu_link)
1746 vmcs_clear(v->vmcs);
1747}
1748#else
1749static inline void crash_enable_local_vmclear(int cpu) { }
1750static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001751#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001752
Nadav Har'Eld462b812011-05-24 15:26:10 +03001753static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001755 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001756 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757
Nadav Har'Eld462b812011-05-24 15:26:10 +03001758 if (loaded_vmcs->cpu != cpu)
1759 return; /* vcpu migration can race with cpu offline */
1760 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001762 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001763 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001764
1765 /*
1766 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1767 * is before setting loaded_vmcs->vcpu to -1 which is done in
1768 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1769 * then adds the vmcs into percpu list before it is deleted.
1770 */
1771 smp_wmb();
1772
Nadav Har'Eld462b812011-05-24 15:26:10 +03001773 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001774 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775}
1776
Nadav Har'Eld462b812011-05-24 15:26:10 +03001777static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001778{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001779 int cpu = loaded_vmcs->cpu;
1780
1781 if (cpu != -1)
1782 smp_call_function_single(cpu,
1783 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001784}
1785
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001786static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001787{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001788 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001789 return;
1790
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001791 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001792 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001793}
1794
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001795static inline void vpid_sync_vcpu_global(void)
1796{
1797 if (cpu_has_vmx_invvpid_global())
1798 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1799}
1800
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001801static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001802{
1803 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001804 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001805 else
1806 vpid_sync_vcpu_global();
1807}
1808
Sheng Yang14394422008-04-28 12:24:45 +08001809static inline void ept_sync_global(void)
1810{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001811 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001812}
1813
1814static inline void ept_sync_context(u64 eptp)
1815{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001816 if (cpu_has_vmx_invept_context())
1817 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1818 else
1819 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001820}
1821
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001822static __always_inline void vmcs_check16(unsigned long field)
1823{
1824 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1825 "16-bit accessor invalid for 64-bit field");
1826 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1827 "16-bit accessor invalid for 64-bit high field");
1828 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1829 "16-bit accessor invalid for 32-bit high field");
1830 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1831 "16-bit accessor invalid for natural width field");
1832}
1833
1834static __always_inline void vmcs_check32(unsigned long field)
1835{
1836 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1837 "32-bit accessor invalid for 16-bit field");
1838 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1839 "32-bit accessor invalid for natural width field");
1840}
1841
1842static __always_inline void vmcs_check64(unsigned long field)
1843{
1844 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1845 "64-bit accessor invalid for 16-bit field");
1846 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1847 "64-bit accessor invalid for 64-bit high field");
1848 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1849 "64-bit accessor invalid for 32-bit field");
1850 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1851 "64-bit accessor invalid for natural width field");
1852}
1853
1854static __always_inline void vmcs_checkl(unsigned long field)
1855{
1856 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1857 "Natural width accessor invalid for 16-bit field");
1858 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1859 "Natural width accessor invalid for 64-bit field");
1860 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1861 "Natural width accessor invalid for 64-bit high field");
1862 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1863 "Natural width accessor invalid for 32-bit field");
1864}
1865
1866static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867{
Avi Kivity5e520e62011-05-15 10:13:12 -04001868 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869
Avi Kivity5e520e62011-05-15 10:13:12 -04001870 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1871 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 return value;
1873}
1874
Avi Kivity96304212011-05-15 10:13:13 -04001875static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001877 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001878 if (static_branch_unlikely(&enable_evmcs))
1879 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001880 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881}
1882
Avi Kivity96304212011-05-15 10:13:13 -04001883static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001885 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001886 if (static_branch_unlikely(&enable_evmcs))
1887 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001888 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889}
1890
Avi Kivity96304212011-05-15 10:13:13 -04001891static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001893 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001894 if (static_branch_unlikely(&enable_evmcs))
1895 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001896#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001897 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001899 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900#endif
1901}
1902
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001903static __always_inline unsigned long vmcs_readl(unsigned long field)
1904{
1905 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001906 if (static_branch_unlikely(&enable_evmcs))
1907 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001908 return __vmcs_readl(field);
1909}
1910
Avi Kivitye52de1b2007-01-05 16:36:56 -08001911static noinline void vmwrite_error(unsigned long field, unsigned long value)
1912{
1913 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1914 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1915 dump_stack();
1916}
1917
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001918static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919{
1920 u8 error;
1921
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001922 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001923 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001924 if (unlikely(error))
1925 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926}
1927
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001928static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001930 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001931 if (static_branch_unlikely(&enable_evmcs))
1932 return evmcs_write16(field, value);
1933
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001934 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935}
1936
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001937static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001939 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001940 if (static_branch_unlikely(&enable_evmcs))
1941 return evmcs_write32(field, value);
1942
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001943 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944}
1945
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001946static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001948 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001949 if (static_branch_unlikely(&enable_evmcs))
1950 return evmcs_write64(field, value);
1951
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001952 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001953#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001955 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956#endif
1957}
1958
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001959static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001960{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001961 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001962 if (static_branch_unlikely(&enable_evmcs))
1963 return evmcs_write64(field, value);
1964
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001965 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001966}
1967
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001968static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001969{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001970 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1971 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001972 if (static_branch_unlikely(&enable_evmcs))
1973 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1974
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001975 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1976}
1977
1978static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1979{
1980 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1981 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001982 if (static_branch_unlikely(&enable_evmcs))
1983 return evmcs_write32(field, evmcs_read32(field) | mask);
1984
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001985 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001986}
1987
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001988static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1989{
1990 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1991}
1992
Gleb Natapov2961e8762013-11-25 15:37:13 +02001993static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1994{
1995 vmcs_write32(VM_ENTRY_CONTROLS, val);
1996 vmx->vm_entry_controls_shadow = val;
1997}
1998
1999static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2000{
2001 if (vmx->vm_entry_controls_shadow != val)
2002 vm_entry_controls_init(vmx, val);
2003}
2004
2005static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2006{
2007 return vmx->vm_entry_controls_shadow;
2008}
2009
2010
2011static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2012{
2013 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2014}
2015
2016static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2017{
2018 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2019}
2020
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002021static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2022{
2023 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2024}
2025
Gleb Natapov2961e8762013-11-25 15:37:13 +02002026static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2027{
2028 vmcs_write32(VM_EXIT_CONTROLS, val);
2029 vmx->vm_exit_controls_shadow = val;
2030}
2031
2032static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2033{
2034 if (vmx->vm_exit_controls_shadow != val)
2035 vm_exit_controls_init(vmx, val);
2036}
2037
2038static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2039{
2040 return vmx->vm_exit_controls_shadow;
2041}
2042
2043
2044static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2045{
2046 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2047}
2048
2049static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2050{
2051 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2052}
2053
Avi Kivity2fb92db2011-04-27 19:42:18 +03002054static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2055{
2056 vmx->segment_cache.bitmask = 0;
2057}
2058
2059static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2060 unsigned field)
2061{
2062 bool ret;
2063 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2064
2065 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2066 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2067 vmx->segment_cache.bitmask = 0;
2068 }
2069 ret = vmx->segment_cache.bitmask & mask;
2070 vmx->segment_cache.bitmask |= mask;
2071 return ret;
2072}
2073
2074static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2075{
2076 u16 *p = &vmx->segment_cache.seg[seg].selector;
2077
2078 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2079 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2080 return *p;
2081}
2082
2083static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2084{
2085 ulong *p = &vmx->segment_cache.seg[seg].base;
2086
2087 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2088 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2089 return *p;
2090}
2091
2092static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2093{
2094 u32 *p = &vmx->segment_cache.seg[seg].limit;
2095
2096 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2097 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2098 return *p;
2099}
2100
2101static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2102{
2103 u32 *p = &vmx->segment_cache.seg[seg].ar;
2104
2105 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2106 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2107 return *p;
2108}
2109
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002110static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2111{
2112 u32 eb;
2113
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002114 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002115 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002116 /*
2117 * Guest access to VMware backdoor ports could legitimately
2118 * trigger #GP because of TSS I/O permission bitmap.
2119 * We intercept those #GP and allow access to them anyway
2120 * as VMware does.
2121 */
2122 if (enable_vmware_backdoor)
2123 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002124 if ((vcpu->guest_debug &
2125 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2126 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2127 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002128 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002129 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002130 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002131 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002132
2133 /* When we are running a nested L2 guest and L1 specified for it a
2134 * certain exception bitmap, we must trap the same exceptions and pass
2135 * them to L1. When running L2, we will only handle the exceptions
2136 * specified above if L1 did not want them.
2137 */
2138 if (is_guest_mode(vcpu))
2139 eb |= get_vmcs12(vcpu)->exception_bitmap;
2140
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002141 vmcs_write32(EXCEPTION_BITMAP, eb);
2142}
2143
Ashok Raj15d45072018-02-01 22:59:43 +01002144/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002145 * Check if MSR is intercepted for currently loaded MSR bitmap.
2146 */
2147static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2148{
2149 unsigned long *msr_bitmap;
2150 int f = sizeof(unsigned long);
2151
2152 if (!cpu_has_vmx_msr_bitmap())
2153 return true;
2154
2155 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2156
2157 if (msr <= 0x1fff) {
2158 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2159 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2160 msr &= 0x1fff;
2161 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2162 }
2163
2164 return true;
2165}
2166
2167/*
Ashok Raj15d45072018-02-01 22:59:43 +01002168 * Check if MSR is intercepted for L01 MSR bitmap.
2169 */
2170static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2171{
2172 unsigned long *msr_bitmap;
2173 int f = sizeof(unsigned long);
2174
2175 if (!cpu_has_vmx_msr_bitmap())
2176 return true;
2177
2178 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2179
2180 if (msr <= 0x1fff) {
2181 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2182 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2183 msr &= 0x1fff;
2184 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2185 }
2186
2187 return true;
2188}
2189
Gleb Natapov2961e8762013-11-25 15:37:13 +02002190static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2191 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002192{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002193 vm_entry_controls_clearbit(vmx, entry);
2194 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002195}
2196
Avi Kivity61d2ef22010-04-28 16:40:38 +03002197static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2198{
2199 unsigned i;
2200 struct msr_autoload *m = &vmx->msr_autoload;
2201
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002202 switch (msr) {
2203 case MSR_EFER:
2204 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002205 clear_atomic_switch_msr_special(vmx,
2206 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002207 VM_EXIT_LOAD_IA32_EFER);
2208 return;
2209 }
2210 break;
2211 case MSR_CORE_PERF_GLOBAL_CTRL:
2212 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002213 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002214 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2215 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2216 return;
2217 }
2218 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002219 }
2220
Avi Kivity61d2ef22010-04-28 16:40:38 +03002221 for (i = 0; i < m->nr; ++i)
2222 if (m->guest[i].index == msr)
2223 break;
2224
2225 if (i == m->nr)
2226 return;
2227 --m->nr;
2228 m->guest[i] = m->guest[m->nr];
2229 m->host[i] = m->host[m->nr];
2230 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2231 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2232}
2233
Gleb Natapov2961e8762013-11-25 15:37:13 +02002234static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2235 unsigned long entry, unsigned long exit,
2236 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2237 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002238{
2239 vmcs_write64(guest_val_vmcs, guest_val);
2240 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002241 vm_entry_controls_setbit(vmx, entry);
2242 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002243}
2244
Avi Kivity61d2ef22010-04-28 16:40:38 +03002245static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2246 u64 guest_val, u64 host_val)
2247{
2248 unsigned i;
2249 struct msr_autoload *m = &vmx->msr_autoload;
2250
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002251 switch (msr) {
2252 case MSR_EFER:
2253 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002254 add_atomic_switch_msr_special(vmx,
2255 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002256 VM_EXIT_LOAD_IA32_EFER,
2257 GUEST_IA32_EFER,
2258 HOST_IA32_EFER,
2259 guest_val, host_val);
2260 return;
2261 }
2262 break;
2263 case MSR_CORE_PERF_GLOBAL_CTRL:
2264 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002265 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002266 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2267 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2268 GUEST_IA32_PERF_GLOBAL_CTRL,
2269 HOST_IA32_PERF_GLOBAL_CTRL,
2270 guest_val, host_val);
2271 return;
2272 }
2273 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002274 case MSR_IA32_PEBS_ENABLE:
2275 /* PEBS needs a quiescent period after being disabled (to write
2276 * a record). Disabling PEBS through VMX MSR swapping doesn't
2277 * provide that period, so a CPU could write host's record into
2278 * guest's memory.
2279 */
2280 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002281 }
2282
Avi Kivity61d2ef22010-04-28 16:40:38 +03002283 for (i = 0; i < m->nr; ++i)
2284 if (m->guest[i].index == msr)
2285 break;
2286
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002287 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002288 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002289 "Can't add msr %x\n", msr);
2290 return;
2291 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002292 ++m->nr;
2293 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2294 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2295 }
2296
2297 m->guest[i].index = msr;
2298 m->guest[i].value = guest_val;
2299 m->host[i].index = msr;
2300 m->host[i].value = host_val;
2301}
2302
Avi Kivity92c0d902009-10-29 11:00:16 +02002303static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002304{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002305 u64 guest_efer = vmx->vcpu.arch.efer;
2306 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002307
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002308 if (!enable_ept) {
2309 /*
2310 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2311 * host CPUID is more efficient than testing guest CPUID
2312 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2313 */
2314 if (boot_cpu_has(X86_FEATURE_SMEP))
2315 guest_efer |= EFER_NX;
2316 else if (!(guest_efer & EFER_NX))
2317 ignore_bits |= EFER_NX;
2318 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002319
Avi Kivity51c6cf62007-08-29 03:48:05 +03002320 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002321 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002322 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002323 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002324#ifdef CONFIG_X86_64
2325 ignore_bits |= EFER_LMA | EFER_LME;
2326 /* SCE is meaningful only in long mode on Intel */
2327 if (guest_efer & EFER_LMA)
2328 ignore_bits &= ~(u64)EFER_SCE;
2329#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002330
2331 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002332
2333 /*
2334 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2335 * On CPUs that support "load IA32_EFER", always switch EFER
2336 * atomically, since it's faster than switching it manually.
2337 */
2338 if (cpu_has_load_ia32_efer ||
2339 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002340 if (!(guest_efer & EFER_LMA))
2341 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002342 if (guest_efer != host_efer)
2343 add_atomic_switch_msr(vmx, MSR_EFER,
2344 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002345 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002346 } else {
2347 guest_efer &= ~ignore_bits;
2348 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002349
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002350 vmx->guest_msrs[efer_offset].data = guest_efer;
2351 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2352
2353 return true;
2354 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002355}
2356
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002357#ifdef CONFIG_X86_32
2358/*
2359 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2360 * VMCS rather than the segment table. KVM uses this helper to figure
2361 * out the current bases to poke them into the VMCS before entry.
2362 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002363static unsigned long segment_base(u16 selector)
2364{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002365 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002366 unsigned long v;
2367
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002368 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002369 return 0;
2370
Thomas Garnier45fc8752017-03-14 10:05:08 -07002371 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002372
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002373 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002374 u16 ldt_selector = kvm_read_ldt();
2375
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002376 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002377 return 0;
2378
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002379 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002380 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002381 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002382 return v;
2383}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002384#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002385
Avi Kivity04d2cc72007-09-10 18:10:54 +03002386static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002387{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002388 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002389#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002390 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002391#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002392 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002393
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002394 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002395 return;
2396
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002397 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002398 /*
2399 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2400 * allow segment selectors with cpl > 0 or ti == 1.
2401 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002402 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002403 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002404
2405#ifdef CONFIG_X86_64
2406 save_fsgs_for_kvm();
2407 vmx->host_state.fs_sel = current->thread.fsindex;
2408 vmx->host_state.gs_sel = current->thread.gsindex;
2409#else
Avi Kivity9581d442010-10-19 16:46:55 +02002410 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002411 savesegment(gs, vmx->host_state.gs_sel);
2412#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002413 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002414 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002415 vmx->host_state.fs_reload_needed = 0;
2416 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002417 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002418 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002419 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002420 if (!(vmx->host_state.gs_sel & 7))
2421 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002422 else {
2423 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002424 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002425 }
2426
2427#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002428 savesegment(ds, vmx->host_state.ds_sel);
2429 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002430
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002431 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002432 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002433
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002434 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002435 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002436 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002437#else
2438 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2439 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2440#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002441 if (boot_cpu_has(X86_FEATURE_MPX))
2442 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002443 for (i = 0; i < vmx->save_nmsrs; ++i)
2444 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002445 vmx->guest_msrs[i].data,
2446 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002447}
2448
Avi Kivitya9b21b62008-06-24 11:48:49 +03002449static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002450{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002451 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002452 return;
2453
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002454 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002455 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002456#ifdef CONFIG_X86_64
2457 if (is_long_mode(&vmx->vcpu))
2458 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2459#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002460 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002461 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002462#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002463 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002464#else
2465 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002466#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002467 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002468 if (vmx->host_state.fs_reload_needed)
2469 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002470#ifdef CONFIG_X86_64
2471 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2472 loadsegment(ds, vmx->host_state.ds_sel);
2473 loadsegment(es, vmx->host_state.es_sel);
2474 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002475#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002476 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002477#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002478 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002479#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002480 if (vmx->host_state.msr_host_bndcfgs)
2481 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002482 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002483}
2484
Avi Kivitya9b21b62008-06-24 11:48:49 +03002485static void vmx_load_host_state(struct vcpu_vmx *vmx)
2486{
2487 preempt_disable();
2488 __vmx_load_host_state(vmx);
2489 preempt_enable();
2490}
2491
Feng Wu28b835d2015-09-18 22:29:54 +08002492static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2493{
2494 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2495 struct pi_desc old, new;
2496 unsigned int dest;
2497
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002498 /*
2499 * In case of hot-plug or hot-unplug, we may have to undo
2500 * vmx_vcpu_pi_put even if there is no assigned device. And we
2501 * always keep PI.NDST up to date for simplicity: it makes the
2502 * code easier, and CPU migration is not a fast path.
2503 */
2504 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002505 return;
2506
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002507 /*
2508 * First handle the simple case where no cmpxchg is necessary; just
2509 * allow posting non-urgent interrupts.
2510 *
2511 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2512 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2513 * expects the VCPU to be on the blocked_vcpu_list that matches
2514 * PI.NDST.
2515 */
2516 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2517 vcpu->cpu == cpu) {
2518 pi_clear_sn(pi_desc);
2519 return;
2520 }
2521
2522 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002523 do {
2524 old.control = new.control = pi_desc->control;
2525
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002526 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002527
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002528 if (x2apic_enabled())
2529 new.ndst = dest;
2530 else
2531 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002532
Feng Wu28b835d2015-09-18 22:29:54 +08002533 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002534 } while (cmpxchg64(&pi_desc->control, old.control,
2535 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002536}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002537
Peter Feinerc95ba922016-08-17 09:36:47 -07002538static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2539{
2540 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2541 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2542}
2543
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544/*
2545 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2546 * vcpu mutex is already taken.
2547 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002548static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002550 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002551 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002553 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002554 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002555 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002556 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002557
2558 /*
2559 * Read loaded_vmcs->cpu should be before fetching
2560 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2561 * See the comments in __loaded_vmcs_clear().
2562 */
2563 smp_rmb();
2564
Nadav Har'Eld462b812011-05-24 15:26:10 +03002565 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2566 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002567 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002568 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002569 }
2570
2571 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2572 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2573 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002574 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002575 }
2576
2577 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002578 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002579 unsigned long sysenter_esp;
2580
2581 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002582
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 /*
2584 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002585 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002587 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002588 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002589 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002591 /*
2592 * VM exits change the host TR limit to 0x67 after a VM
2593 * exit. This is okay, since 0x67 covers everything except
2594 * the IO bitmap and have have code to handle the IO bitmap
2595 * being lost after a VM exit.
2596 */
2597 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2598
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2600 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002601
Nadav Har'Eld462b812011-05-24 15:26:10 +03002602 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603 }
Feng Wu28b835d2015-09-18 22:29:54 +08002604
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002605 /* Setup TSC multiplier */
2606 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002607 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2608 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002609
Feng Wu28b835d2015-09-18 22:29:54 +08002610 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002611 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002612 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002613}
2614
2615static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2616{
2617 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2618
2619 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002620 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2621 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002622 return;
2623
2624 /* Set SN when the vCPU is preempted */
2625 if (vcpu->preempted)
2626 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627}
2628
2629static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2630{
Feng Wu28b835d2015-09-18 22:29:54 +08002631 vmx_vcpu_pi_put(vcpu);
2632
Avi Kivitya9b21b62008-06-24 11:48:49 +03002633 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634}
2635
Wanpeng Lif244dee2017-07-20 01:11:54 -07002636static bool emulation_required(struct kvm_vcpu *vcpu)
2637{
2638 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2639}
2640
Avi Kivityedcafe32009-12-30 18:07:40 +02002641static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2642
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002643/*
2644 * Return the cr0 value that a nested guest would read. This is a combination
2645 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2646 * its hypervisor (cr0_read_shadow).
2647 */
2648static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2649{
2650 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2651 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2652}
2653static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2654{
2655 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2656 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2657}
2658
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2660{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002661 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002662
Avi Kivity6de12732011-03-07 12:51:22 +02002663 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2664 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2665 rflags = vmcs_readl(GUEST_RFLAGS);
2666 if (to_vmx(vcpu)->rmode.vm86_active) {
2667 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2668 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2669 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2670 }
2671 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002672 }
Avi Kivity6de12732011-03-07 12:51:22 +02002673 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674}
2675
2676static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2677{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002678 unsigned long old_rflags = vmx_get_rflags(vcpu);
2679
Avi Kivity6de12732011-03-07 12:51:22 +02002680 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2681 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002682 if (to_vmx(vcpu)->rmode.vm86_active) {
2683 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002684 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002685 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002687
2688 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2689 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690}
2691
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002692static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002693{
2694 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2695 int ret = 0;
2696
2697 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002698 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002699 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002700 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002701
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002702 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002703}
2704
2705static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2706{
2707 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2708 u32 interruptibility = interruptibility_old;
2709
2710 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2711
Jan Kiszka48005f62010-02-19 19:38:07 +01002712 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002713 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002714 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002715 interruptibility |= GUEST_INTR_STATE_STI;
2716
2717 if ((interruptibility != interruptibility_old))
2718 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2719}
2720
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2722{
2723 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002725 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002727 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728
Glauber Costa2809f5d2009-05-12 16:21:05 -04002729 /* skipping an emulated instruction also counts */
2730 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731}
2732
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002733static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2734 unsigned long exit_qual)
2735{
2736 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2737 unsigned int nr = vcpu->arch.exception.nr;
2738 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2739
2740 if (vcpu->arch.exception.has_error_code) {
2741 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2742 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2743 }
2744
2745 if (kvm_exception_is_soft(nr))
2746 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2747 else
2748 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2749
2750 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2751 vmx_get_nmi_mask(vcpu))
2752 intr_info |= INTR_INFO_UNBLOCK_NMI;
2753
2754 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2755}
2756
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002757/*
2758 * KVM wants to inject page-faults which it got to the guest. This function
2759 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002760 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002761static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002762{
2763 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002764 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002765
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002766 if (nr == PF_VECTOR) {
2767 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002768 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002769 return 1;
2770 }
2771 /*
2772 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2773 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2774 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2775 * can be written only when inject_pending_event runs. This should be
2776 * conditional on a new capability---if the capability is disabled,
2777 * kvm_multiple_exception would write the ancillary information to
2778 * CR2 or DR6, for backwards ABI-compatibility.
2779 */
2780 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2781 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002782 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002783 return 1;
2784 }
2785 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002786 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002787 if (nr == DB_VECTOR)
2788 *exit_qual = vcpu->arch.dr6;
2789 else
2790 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002791 return 1;
2792 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002793 }
2794
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002795 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002796}
2797
Wanpeng Licaa057a2018-03-12 04:53:03 -07002798static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2799{
2800 /*
2801 * Ensure that we clear the HLT state in the VMCS. We don't need to
2802 * explicitly skip the instruction because if the HLT state is set,
2803 * then the instruction is already executing and RIP has already been
2804 * advanced.
2805 */
2806 if (kvm_hlt_in_guest(vcpu->kvm) &&
2807 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2808 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2809}
2810
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002811static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002812{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002813 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002814 unsigned nr = vcpu->arch.exception.nr;
2815 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002816 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002817 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002818
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002819 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002820 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002821 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2822 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002823
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002824 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002825 int inc_eip = 0;
2826 if (kvm_exception_is_soft(nr))
2827 inc_eip = vcpu->arch.event_exit_inst_len;
2828 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002829 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002830 return;
2831 }
2832
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002833 WARN_ON_ONCE(vmx->emulation_required);
2834
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002835 if (kvm_exception_is_soft(nr)) {
2836 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2837 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002838 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2839 } else
2840 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2841
2842 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002843
2844 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002845}
2846
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002847static bool vmx_rdtscp_supported(void)
2848{
2849 return cpu_has_vmx_rdtscp();
2850}
2851
Mao, Junjiead756a12012-07-02 01:18:48 +00002852static bool vmx_invpcid_supported(void)
2853{
2854 return cpu_has_vmx_invpcid() && enable_ept;
2855}
2856
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857/*
Eddie Donga75beee2007-05-17 18:55:15 +03002858 * Swap MSR entry in host/guest MSR entry array.
2859 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002860static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002861{
Avi Kivity26bb0982009-09-07 11:14:12 +03002862 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002863
2864 tmp = vmx->guest_msrs[to];
2865 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2866 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002867}
2868
2869/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002870 * Set up the vmcs to automatically save and restore system
2871 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2872 * mode, as fiddling with msrs is very expensive.
2873 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002874static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002875{
Avi Kivity26bb0982009-09-07 11:14:12 +03002876 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002877
Eddie Donga75beee2007-05-17 18:55:15 +03002878 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002879#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002880 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002881 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002882 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002883 move_msr_up(vmx, index, save_nmsrs++);
2884 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002885 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002886 move_msr_up(vmx, index, save_nmsrs++);
2887 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002888 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002889 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002890 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002891 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002892 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002893 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002894 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002895 * if efer.sce is enabled.
2896 */
Brian Gerst8c065852010-07-17 09:03:26 -04002897 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002898 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002899 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002900 }
Eddie Donga75beee2007-05-17 18:55:15 +03002901#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002902 index = __find_msr_index(vmx, MSR_EFER);
2903 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002904 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002905
Avi Kivity26bb0982009-09-07 11:14:12 +03002906 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002907
Yang Zhang8d146952013-01-25 10:18:50 +08002908 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002909 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002910}
2911
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002912static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002914 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002916 if (is_guest_mode(vcpu) &&
2917 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2918 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2919
2920 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921}
2922
2923/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002924 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002926static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002928 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002929 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002930 * We're here if L1 chose not to trap WRMSR to TSC. According
2931 * to the spec, this should set L1's TSC; The offset that L1
2932 * set for L2 remains unchanged, and still needs to be added
2933 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002934 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002935 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002936 /* recalculate vmcs02.TSC_OFFSET: */
2937 vmcs12 = get_vmcs12(vcpu);
2938 vmcs_write64(TSC_OFFSET, offset +
2939 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2940 vmcs12->tsc_offset : 0));
2941 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002942 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2943 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002944 vmcs_write64(TSC_OFFSET, offset);
2945 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946}
2947
Nadav Har'El801d3422011-05-25 23:02:23 +03002948/*
2949 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2950 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2951 * all guests if the "nested" module option is off, and can also be disabled
2952 * for a single guest by disabling its VMX cpuid bit.
2953 */
2954static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2955{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002956 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002957}
2958
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002960 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2961 * returned for the various VMX controls MSRs when nested VMX is enabled.
2962 * The same values should also be used to verify that vmcs12 control fields are
2963 * valid during nested entry from L1 to L2.
2964 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2965 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2966 * bit in the high half is on if the corresponding bit in the control field
2967 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002968 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002969static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002970{
Paolo Bonzini13893092018-02-26 13:40:09 +01002971 if (!nested) {
2972 memset(msrs, 0, sizeof(*msrs));
2973 return;
2974 }
2975
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002976 /*
2977 * Note that as a general rule, the high half of the MSRs (bits in
2978 * the control fields which may be 1) should be initialized by the
2979 * intersection of the underlying hardware's MSR (i.e., features which
2980 * can be supported) and the list of features we want to expose -
2981 * because they are known to be properly supported in our code.
2982 * Also, usually, the low half of the MSRs (bits which must be 1) can
2983 * be set to 0, meaning that L1 may turn off any of these bits. The
2984 * reason is that if one of these bits is necessary, it will appear
2985 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2986 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002987 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002988 * These rules have exceptions below.
2989 */
2990
2991 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002992 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002993 msrs->pinbased_ctls_low,
2994 msrs->pinbased_ctls_high);
2995 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002996 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002997 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002998 PIN_BASED_EXT_INTR_MASK |
2999 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003000 PIN_BASED_VIRTUAL_NMIS |
3001 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003002 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003003 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003004 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003005
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003006 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003007 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003008 msrs->exit_ctls_low,
3009 msrs->exit_ctls_high);
3010 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003011 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003012
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003013 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003014#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003015 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003016#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003017 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003018 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003019 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003020 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003021 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3022
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003023 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003024 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003025
Jan Kiszka2996fca2014-06-16 13:59:43 +02003026 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003027 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003028
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003029 /* entry controls */
3030 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003031 msrs->entry_ctls_low,
3032 msrs->entry_ctls_high);
3033 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003034 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003035 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003036#ifdef CONFIG_X86_64
3037 VM_ENTRY_IA32E_MODE |
3038#endif
3039 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003040 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003041 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003042 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003043 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003044
Jan Kiszka2996fca2014-06-16 13:59:43 +02003045 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003046 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003047
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003048 /* cpu-based controls */
3049 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003050 msrs->procbased_ctls_low,
3051 msrs->procbased_ctls_high);
3052 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003053 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003054 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003055 CPU_BASED_VIRTUAL_INTR_PENDING |
3056 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003057 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3058 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3059 CPU_BASED_CR3_STORE_EXITING |
3060#ifdef CONFIG_X86_64
3061 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3062#endif
3063 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003064 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3065 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3066 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3067 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003068 /*
3069 * We can allow some features even when not supported by the
3070 * hardware. For example, L1 can specify an MSR bitmap - and we
3071 * can use it to avoid exits to L1 - even when L0 runs L2
3072 * without MSR bitmaps.
3073 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003074 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003075 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003076 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003077
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003078 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003079 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003080 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3081
Paolo Bonzini80154d72017-08-24 13:55:35 +02003082 /*
3083 * secondary cpu-based controls. Do not include those that
3084 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3085 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003086 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003087 msrs->secondary_ctls_low,
3088 msrs->secondary_ctls_high);
3089 msrs->secondary_ctls_low = 0;
3090 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003091 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003092 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003093 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003094 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003095 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003096 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003097
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003098 if (enable_ept) {
3099 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003100 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003101 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003102 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003103 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003104 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003105 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003106 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003107 msrs->ept_caps &= vmx_capability.ept;
3108 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003109 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3110 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003111 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003112 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003113 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003114 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003115 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003116 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003117
Bandan Das27c42a12017-08-03 15:54:42 -04003118 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003119 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003120 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003121 /*
3122 * Advertise EPTP switching unconditionally
3123 * since we emulate it
3124 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003125 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003126 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003127 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003128 }
3129
Paolo Bonzinief697a72016-03-18 16:58:38 +01003130 /*
3131 * Old versions of KVM use the single-context version without
3132 * checking for support, so declare that it is supported even
3133 * though it is treated as global context. The alternative is
3134 * not failing the single-context invvpid, and it is worse.
3135 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003136 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003137 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003138 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003139 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003140 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003141 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003142
Radim Krčmář0790ec12015-03-17 14:02:32 +01003143 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003144 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003145 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3146
Jan Kiszkac18911a2013-03-13 16:06:41 +01003147 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003148 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003149 msrs->misc_low,
3150 msrs->misc_high);
3151 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3152 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003153 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003154 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003155 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003156
3157 /*
3158 * This MSR reports some information about VMX support. We
3159 * should return information about the VMX we emulate for the
3160 * guest, and the VMCS structure we give it - not about the
3161 * VMX support of the underlying hardware.
3162 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003163 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003164 VMCS12_REVISION |
3165 VMX_BASIC_TRUE_CTLS |
3166 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3167 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3168
3169 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003170 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003171
3172 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003173 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003174 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3175 * We picked the standard core2 setting.
3176 */
3177#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3178#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003179 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3180 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003181
3182 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003183 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3184 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003185
3186 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003187 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003188}
3189
David Matlack38991522016-11-29 18:14:08 -08003190/*
3191 * if fixed0[i] == 1: val[i] must be 1
3192 * if fixed1[i] == 0: val[i] must be 0
3193 */
3194static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3195{
3196 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003197}
3198
3199static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3200{
David Matlack38991522016-11-29 18:14:08 -08003201 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202}
3203
3204static inline u64 vmx_control_msr(u32 low, u32 high)
3205{
3206 return low | ((u64)high << 32);
3207}
3208
David Matlack62cc6b9d2016-11-29 18:14:07 -08003209static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3210{
3211 superset &= mask;
3212 subset &= mask;
3213
3214 return (superset | subset) == superset;
3215}
3216
3217static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3218{
3219 const u64 feature_and_reserved =
3220 /* feature (except bit 48; see below) */
3221 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3222 /* reserved */
3223 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003224 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003225
3226 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3227 return -EINVAL;
3228
3229 /*
3230 * KVM does not emulate a version of VMX that constrains physical
3231 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3232 */
3233 if (data & BIT_ULL(48))
3234 return -EINVAL;
3235
3236 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3237 vmx_basic_vmcs_revision_id(data))
3238 return -EINVAL;
3239
3240 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3241 return -EINVAL;
3242
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003243 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003244 return 0;
3245}
3246
3247static int
3248vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3249{
3250 u64 supported;
3251 u32 *lowp, *highp;
3252
3253 switch (msr_index) {
3254 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003255 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3256 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003257 break;
3258 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003259 lowp = &vmx->nested.msrs.procbased_ctls_low;
3260 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003261 break;
3262 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003263 lowp = &vmx->nested.msrs.exit_ctls_low;
3264 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003265 break;
3266 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003267 lowp = &vmx->nested.msrs.entry_ctls_low;
3268 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003269 break;
3270 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003271 lowp = &vmx->nested.msrs.secondary_ctls_low;
3272 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003273 break;
3274 default:
3275 BUG();
3276 }
3277
3278 supported = vmx_control_msr(*lowp, *highp);
3279
3280 /* Check must-be-1 bits are still 1. */
3281 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3282 return -EINVAL;
3283
3284 /* Check must-be-0 bits are still 0. */
3285 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3286 return -EINVAL;
3287
3288 *lowp = data;
3289 *highp = data >> 32;
3290 return 0;
3291}
3292
3293static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3294{
3295 const u64 feature_and_reserved_bits =
3296 /* feature */
3297 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3298 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3299 /* reserved */
3300 GENMASK_ULL(13, 9) | BIT_ULL(31);
3301 u64 vmx_misc;
3302
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3304 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003305
3306 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3307 return -EINVAL;
3308
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003309 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003310 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3311 vmx_misc_preemption_timer_rate(data) !=
3312 vmx_misc_preemption_timer_rate(vmx_misc))
3313 return -EINVAL;
3314
3315 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3316 return -EINVAL;
3317
3318 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3319 return -EINVAL;
3320
3321 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3322 return -EINVAL;
3323
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003324 vmx->nested.msrs.misc_low = data;
3325 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003326 return 0;
3327}
3328
3329static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3330{
3331 u64 vmx_ept_vpid_cap;
3332
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3334 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003335
3336 /* Every bit is either reserved or a feature bit. */
3337 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3338 return -EINVAL;
3339
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 vmx->nested.msrs.ept_caps = data;
3341 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003342 return 0;
3343}
3344
3345static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3346{
3347 u64 *msr;
3348
3349 switch (msr_index) {
3350 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003351 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003352 break;
3353 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003354 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003355 break;
3356 default:
3357 BUG();
3358 }
3359
3360 /*
3361 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3362 * must be 1 in the restored value.
3363 */
3364 if (!is_bitwise_subset(data, *msr, -1ULL))
3365 return -EINVAL;
3366
3367 *msr = data;
3368 return 0;
3369}
3370
3371/*
3372 * Called when userspace is restoring VMX MSRs.
3373 *
3374 * Returns 0 on success, non-0 otherwise.
3375 */
3376static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3377{
3378 struct vcpu_vmx *vmx = to_vmx(vcpu);
3379
3380 switch (msr_index) {
3381 case MSR_IA32_VMX_BASIC:
3382 return vmx_restore_vmx_basic(vmx, data);
3383 case MSR_IA32_VMX_PINBASED_CTLS:
3384 case MSR_IA32_VMX_PROCBASED_CTLS:
3385 case MSR_IA32_VMX_EXIT_CTLS:
3386 case MSR_IA32_VMX_ENTRY_CTLS:
3387 /*
3388 * The "non-true" VMX capability MSRs are generated from the
3389 * "true" MSRs, so we do not support restoring them directly.
3390 *
3391 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3392 * should restore the "true" MSRs with the must-be-1 bits
3393 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3394 * DEFAULT SETTINGS".
3395 */
3396 return -EINVAL;
3397 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3398 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3399 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3400 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3401 case MSR_IA32_VMX_PROCBASED_CTLS2:
3402 return vmx_restore_control_msr(vmx, msr_index, data);
3403 case MSR_IA32_VMX_MISC:
3404 return vmx_restore_vmx_misc(vmx, data);
3405 case MSR_IA32_VMX_CR0_FIXED0:
3406 case MSR_IA32_VMX_CR4_FIXED0:
3407 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3408 case MSR_IA32_VMX_CR0_FIXED1:
3409 case MSR_IA32_VMX_CR4_FIXED1:
3410 /*
3411 * These MSRs are generated based on the vCPU's CPUID, so we
3412 * do not support restoring them directly.
3413 */
3414 return -EINVAL;
3415 case MSR_IA32_VMX_EPT_VPID_CAP:
3416 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3417 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003418 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003419 return 0;
3420 default:
3421 /*
3422 * The rest of the VMX capability MSRs do not support restore.
3423 */
3424 return -EINVAL;
3425 }
3426}
3427
Jan Kiszkacae50132014-01-04 18:47:22 +01003428/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003429static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003430{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003431 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003432 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003433 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003434 break;
3435 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3436 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003437 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003438 msrs->pinbased_ctls_low,
3439 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003440 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3441 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003442 break;
3443 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3444 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003445 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003446 msrs->procbased_ctls_low,
3447 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003448 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3449 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003450 break;
3451 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3452 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003453 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003454 msrs->exit_ctls_low,
3455 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003456 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3457 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003458 break;
3459 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3460 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003461 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003462 msrs->entry_ctls_low,
3463 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003464 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3465 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 break;
3467 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003468 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003469 msrs->misc_low,
3470 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003471 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003473 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474 break;
3475 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003476 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003477 break;
3478 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003479 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003480 break;
3481 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003482 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003483 break;
3484 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003485 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003486 break;
3487 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003488 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003489 msrs->secondary_ctls_low,
3490 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003491 break;
3492 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003493 *pdata = msrs->ept_caps |
3494 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003495 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003496 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003498 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003499 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003500 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003501 }
3502
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003503 return 0;
3504}
3505
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003506static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3507 uint64_t val)
3508{
3509 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3510
3511 return !(val & ~valid_bits);
3512}
3513
Tom Lendacky801e4592018-02-21 13:39:51 -06003514static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3515{
Paolo Bonzini13893092018-02-26 13:40:09 +01003516 switch (msr->index) {
3517 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3518 if (!nested)
3519 return 1;
3520 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3521 default:
3522 return 1;
3523 }
3524
3525 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003526}
3527
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003528/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529 * Reads an msr value (of 'msr_index') into 'pdata'.
3530 * Returns 0 on success, non-0 otherwise.
3531 * Assumes vcpu_load() was already called.
3532 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003533static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003535 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003536 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003538 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003539#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003541 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542 break;
3543 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003544 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003546 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003547 vmx_load_host_state(vmx);
3548 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003549 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003550#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003552 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003553 case MSR_IA32_SPEC_CTRL:
3554 if (!msr_info->host_initiated &&
3555 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3556 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3557 return 1;
3558
3559 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3560 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003561 case MSR_IA32_ARCH_CAPABILITIES:
3562 if (!msr_info->host_initiated &&
3563 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3564 return 1;
3565 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003568 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569 break;
3570 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003571 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572 break;
3573 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003574 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003576 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003577 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003578 (!msr_info->host_initiated &&
3579 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003580 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003581 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003582 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003583 case MSR_IA32_MCG_EXT_CTL:
3584 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003585 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003586 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003587 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003588 msr_info->data = vcpu->arch.mcg_ext_ctl;
3589 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003590 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003591 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003592 break;
3593 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3594 if (!nested_vmx_allowed(vcpu))
3595 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003596 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3597 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003598 case MSR_IA32_XSS:
3599 if (!vmx_xsaves_supported())
3600 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003601 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003602 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003603 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003604 if (!msr_info->host_initiated &&
3605 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003606 return 1;
3607 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003609 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003610 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003611 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003612 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003614 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615 }
3616
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617 return 0;
3618}
3619
Jan Kiszkacae50132014-01-04 18:47:22 +01003620static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3621
Avi Kivity6aa8b732006-12-10 02:21:36 -08003622/*
3623 * Writes msr value into into the appropriate "register".
3624 * Returns 0 on success, non-0 otherwise.
3625 * Assumes vcpu_load() was already called.
3626 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003627static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003629 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003630 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003631 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003632 u32 msr_index = msr_info->index;
3633 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003634
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003636 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003637 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003638 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003639#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003641 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642 vmcs_writel(GUEST_FS_BASE, data);
3643 break;
3644 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003645 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 vmcs_writel(GUEST_GS_BASE, data);
3647 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003648 case MSR_KERNEL_GS_BASE:
3649 vmx_load_host_state(vmx);
3650 vmx->msr_guest_kernel_gs_base = data;
3651 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652#endif
3653 case MSR_IA32_SYSENTER_CS:
3654 vmcs_write32(GUEST_SYSENTER_CS, data);
3655 break;
3656 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003657 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 break;
3659 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003660 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003662 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003663 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003664 (!msr_info->host_initiated &&
3665 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003666 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003667 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003668 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003670 vmcs_write64(GUEST_BNDCFGS, data);
3671 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003672 case MSR_IA32_SPEC_CTRL:
3673 if (!msr_info->host_initiated &&
3674 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3675 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3676 return 1;
3677
3678 /* The STIBP bit doesn't fault even if it's not advertised */
3679 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3680 return 1;
3681
3682 vmx->spec_ctrl = data;
3683
3684 if (!data)
3685 break;
3686
3687 /*
3688 * For non-nested:
3689 * When it's written (to non-zero) for the first time, pass
3690 * it through.
3691 *
3692 * For nested:
3693 * The handling of the MSR bitmap for L2 guests is done in
3694 * nested_vmx_merge_msr_bitmap. We should not touch the
3695 * vmcs02.msr_bitmap here since it gets completely overwritten
3696 * in the merging. We update the vmcs01 here for L1 as well
3697 * since it will end up touching the MSR anyway now.
3698 */
3699 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3700 MSR_IA32_SPEC_CTRL,
3701 MSR_TYPE_RW);
3702 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003703 case MSR_IA32_PRED_CMD:
3704 if (!msr_info->host_initiated &&
3705 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3706 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3707 return 1;
3708
3709 if (data & ~PRED_CMD_IBPB)
3710 return 1;
3711
3712 if (!data)
3713 break;
3714
3715 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3716
3717 /*
3718 * For non-nested:
3719 * When it's written (to non-zero) for the first time, pass
3720 * it through.
3721 *
3722 * For nested:
3723 * The handling of the MSR bitmap for L2 guests is done in
3724 * nested_vmx_merge_msr_bitmap. We should not touch the
3725 * vmcs02.msr_bitmap here since it gets completely overwritten
3726 * in the merging.
3727 */
3728 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3729 MSR_TYPE_W);
3730 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003731 case MSR_IA32_ARCH_CAPABILITIES:
3732 if (!msr_info->host_initiated)
3733 return 1;
3734 vmx->arch_capabilities = data;
3735 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003736 case MSR_IA32_CR_PAT:
3737 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003738 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3739 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003740 vmcs_write64(GUEST_IA32_PAT, data);
3741 vcpu->arch.pat = data;
3742 break;
3743 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003744 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003745 break;
Will Auldba904632012-11-29 12:42:50 -08003746 case MSR_IA32_TSC_ADJUST:
3747 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003748 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003749 case MSR_IA32_MCG_EXT_CTL:
3750 if ((!msr_info->host_initiated &&
3751 !(to_vmx(vcpu)->msr_ia32_feature_control &
3752 FEATURE_CONTROL_LMCE)) ||
3753 (data & ~MCG_EXT_CTL_LMCE_EN))
3754 return 1;
3755 vcpu->arch.mcg_ext_ctl = data;
3756 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003757 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003758 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003759 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003760 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3761 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003762 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003763 if (msr_info->host_initiated && data == 0)
3764 vmx_leave_nested(vcpu);
3765 break;
3766 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003767 if (!msr_info->host_initiated)
3768 return 1; /* they are read-only */
3769 if (!nested_vmx_allowed(vcpu))
3770 return 1;
3771 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003772 case MSR_IA32_XSS:
3773 if (!vmx_xsaves_supported())
3774 return 1;
3775 /*
3776 * The only supported bit as of Skylake is bit 8, but
3777 * it is not supported on KVM.
3778 */
3779 if (data != 0)
3780 return 1;
3781 vcpu->arch.ia32_xss = data;
3782 if (vcpu->arch.ia32_xss != host_xss)
3783 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3784 vcpu->arch.ia32_xss, host_xss);
3785 else
3786 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3787 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003788 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003789 if (!msr_info->host_initiated &&
3790 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003791 return 1;
3792 /* Check reserved bit, higher 32 bits should be zero */
3793 if ((data >> 32) != 0)
3794 return 1;
3795 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003797 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003798 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003799 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003800 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003801 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3802 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003803 ret = kvm_set_shared_msr(msr->index, msr->data,
3804 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003805 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003806 if (ret)
3807 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003808 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003809 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003811 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812 }
3813
Eddie Dong2cc51562007-05-21 07:28:09 +03003814 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815}
3816
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003817static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003819 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3820 switch (reg) {
3821 case VCPU_REGS_RSP:
3822 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3823 break;
3824 case VCPU_REGS_RIP:
3825 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3826 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003827 case VCPU_EXREG_PDPTR:
3828 if (enable_ept)
3829 ept_save_pdptrs(vcpu);
3830 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003831 default:
3832 break;
3833 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834}
3835
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836static __init int cpu_has_kvm_support(void)
3837{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003838 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839}
3840
3841static __init int vmx_disabled_by_bios(void)
3842{
3843 u64 msr;
3844
3845 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003846 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003847 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003848 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3849 && tboot_enabled())
3850 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003851 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003852 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003853 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003854 && !tboot_enabled()) {
3855 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003856 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003857 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003858 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003859 /* launched w/o TXT and VMX disabled */
3860 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3861 && !tboot_enabled())
3862 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003863 }
3864
3865 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866}
3867
Dongxiao Xu7725b892010-05-11 18:29:38 +08003868static void kvm_cpu_vmxon(u64 addr)
3869{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003870 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003871 intel_pt_handle_vmx(1);
3872
Dongxiao Xu7725b892010-05-11 18:29:38 +08003873 asm volatile (ASM_VMX_VMXON_RAX
3874 : : "a"(&addr), "m"(addr)
3875 : "memory", "cc");
3876}
3877
Radim Krčmář13a34e02014-08-28 15:13:03 +02003878static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879{
3880 int cpu = raw_smp_processor_id();
3881 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003882 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003884 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003885 return -EBUSY;
3886
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01003887 /*
3888 * This can happen if we hot-added a CPU but failed to allocate
3889 * VP assist page for it.
3890 */
3891 if (static_branch_unlikely(&enable_evmcs) &&
3892 !hv_get_vp_assist_page(cpu))
3893 return -EFAULT;
3894
Nadav Har'Eld462b812011-05-24 15:26:10 +03003895 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003896 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3897 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003898
3899 /*
3900 * Now we can enable the vmclear operation in kdump
3901 * since the loaded_vmcss_on_cpu list on this cpu
3902 * has been initialized.
3903 *
3904 * Though the cpu is not in VMX operation now, there
3905 * is no problem to enable the vmclear operation
3906 * for the loaded_vmcss_on_cpu list is empty!
3907 */
3908 crash_enable_local_vmclear(cpu);
3909
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003911
3912 test_bits = FEATURE_CONTROL_LOCKED;
3913 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3914 if (tboot_enabled())
3915 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3916
3917 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003919 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3920 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003921 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003922 if (enable_ept)
3923 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003924
3925 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926}
3927
Nadav Har'Eld462b812011-05-24 15:26:10 +03003928static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003929{
3930 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003931 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003932
Nadav Har'Eld462b812011-05-24 15:26:10 +03003933 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3934 loaded_vmcss_on_cpu_link)
3935 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003936}
3937
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003938
3939/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3940 * tricks.
3941 */
3942static void kvm_cpu_vmxoff(void)
3943{
3944 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003945
3946 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003947 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003948}
3949
Radim Krčmář13a34e02014-08-28 15:13:03 +02003950static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003952 vmclear_local_loaded_vmcss();
3953 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954}
3955
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003956static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003957 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958{
3959 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003960 u32 ctl = ctl_min | ctl_opt;
3961
3962 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3963
3964 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3965 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3966
3967 /* Ensure minimum (required) set of control bits are supported. */
3968 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003969 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003970
3971 *result = ctl;
3972 return 0;
3973}
3974
Avi Kivity110312c2010-12-21 12:54:20 +02003975static __init bool allow_1_setting(u32 msr, u32 ctl)
3976{
3977 u32 vmx_msr_low, vmx_msr_high;
3978
3979 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3980 return vmx_msr_high & ctl;
3981}
3982
Yang, Sheng002c7f72007-07-31 14:23:01 +03003983static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003984{
3985 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003986 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003987 u32 _pin_based_exec_control = 0;
3988 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003989 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003990 u32 _vmexit_control = 0;
3991 u32 _vmentry_control = 0;
3992
Paolo Bonzini13893092018-02-26 13:40:09 +01003993 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303994 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003995#ifdef CONFIG_X86_64
3996 CPU_BASED_CR8_LOAD_EXITING |
3997 CPU_BASED_CR8_STORE_EXITING |
3998#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003999 CPU_BASED_CR3_LOAD_EXITING |
4000 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004001 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004002 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004003 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004004 CPU_BASED_MWAIT_EXITING |
4005 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004006 CPU_BASED_INVLPG_EXITING |
4007 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004008
Sheng Yangf78e0e22007-10-29 09:40:42 +08004009 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004010 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004011 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004012 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4013 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004014 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004015#ifdef CONFIG_X86_64
4016 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4017 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4018 ~CPU_BASED_CR8_STORE_EXITING;
4019#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004020 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004021 min2 = 0;
4022 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004023 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004024 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004025 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004026 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004027 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004028 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004029 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004030 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004031 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004032 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004033 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004034 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004035 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004036 SECONDARY_EXEC_RDSEED_EXITING |
4037 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004038 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004039 SECONDARY_EXEC_TSC_SCALING |
4040 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004041 if (adjust_vmx_controls(min2, opt2,
4042 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004043 &_cpu_based_2nd_exec_control) < 0)
4044 return -EIO;
4045 }
4046#ifndef CONFIG_X86_64
4047 if (!(_cpu_based_2nd_exec_control &
4048 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4049 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4050#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004051
4052 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4053 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004054 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004055 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4056 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004057
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004058 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4059 &vmx_capability.ept, &vmx_capability.vpid);
4060
Sheng Yangd56f5462008-04-25 10:13:16 +08004061 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004062 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4063 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004064 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4065 CPU_BASED_CR3_STORE_EXITING |
4066 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004067 } else if (vmx_capability.ept) {
4068 vmx_capability.ept = 0;
4069 pr_warn_once("EPT CAP should not exist if not support "
4070 "1-setting enable EPT VM-execution control\n");
4071 }
4072 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4073 vmx_capability.vpid) {
4074 vmx_capability.vpid = 0;
4075 pr_warn_once("VPID CAP should not exist if not support "
4076 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004077 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004078
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004079 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004080#ifdef CONFIG_X86_64
4081 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4082#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004083 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004084 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004085 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4086 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004087 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004088
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004089 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4090 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4091 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004092 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4093 &_pin_based_exec_control) < 0)
4094 return -EIO;
4095
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004096 if (cpu_has_broken_vmx_preemption_timer())
4097 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004098 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004099 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004100 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4101
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004102 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004103 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004104 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4105 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004106 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004108 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004109
4110 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4111 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004112 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004113
4114#ifdef CONFIG_X86_64
4115 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4116 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004117 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004118#endif
4119
4120 /* Require Write-Back (WB) memory type for VMCS accesses. */
4121 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004122 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004123
Yang, Sheng002c7f72007-07-31 14:23:01 +03004124 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004125 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004126 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004127
4128 /* KVM supports Enlightened VMCS v1 only */
4129 if (static_branch_unlikely(&enable_evmcs))
4130 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4131 else
4132 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004133
Yang, Sheng002c7f72007-07-31 14:23:01 +03004134 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4135 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004136 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004137 vmcs_conf->vmexit_ctrl = _vmexit_control;
4138 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004139
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004140 if (static_branch_unlikely(&enable_evmcs))
4141 evmcs_sanitize_exec_ctrls(vmcs_conf);
4142
Avi Kivity110312c2010-12-21 12:54:20 +02004143 cpu_has_load_ia32_efer =
4144 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4145 VM_ENTRY_LOAD_IA32_EFER)
4146 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4147 VM_EXIT_LOAD_IA32_EFER);
4148
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004149 cpu_has_load_perf_global_ctrl =
4150 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4151 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4152 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4153 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4154
4155 /*
4156 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004157 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004158 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4159 *
4160 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4161 *
4162 * AAK155 (model 26)
4163 * AAP115 (model 30)
4164 * AAT100 (model 37)
4165 * BC86,AAY89,BD102 (model 44)
4166 * BA97 (model 46)
4167 *
4168 */
4169 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4170 switch (boot_cpu_data.x86_model) {
4171 case 26:
4172 case 30:
4173 case 37:
4174 case 44:
4175 case 46:
4176 cpu_has_load_perf_global_ctrl = false;
4177 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4178 "does not work properly. Using workaround\n");
4179 break;
4180 default:
4181 break;
4182 }
4183 }
4184
Borislav Petkov782511b2016-04-04 22:25:03 +02004185 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004186 rdmsrl(MSR_IA32_XSS, host_xss);
4187
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004188 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004189}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190
4191static struct vmcs *alloc_vmcs_cpu(int cpu)
4192{
4193 int node = cpu_to_node(cpu);
4194 struct page *pages;
4195 struct vmcs *vmcs;
4196
Vlastimil Babka96db8002015-09-08 15:03:50 -07004197 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198 if (!pages)
4199 return NULL;
4200 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004201 memset(vmcs, 0, vmcs_config.size);
4202 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203 return vmcs;
4204}
4205
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206static void free_vmcs(struct vmcs *vmcs)
4207{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004208 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209}
4210
Nadav Har'Eld462b812011-05-24 15:26:10 +03004211/*
4212 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4213 */
4214static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4215{
4216 if (!loaded_vmcs->vmcs)
4217 return;
4218 loaded_vmcs_clear(loaded_vmcs);
4219 free_vmcs(loaded_vmcs->vmcs);
4220 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004221 if (loaded_vmcs->msr_bitmap)
4222 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004223 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004224}
4225
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004226static struct vmcs *alloc_vmcs(void)
4227{
4228 return alloc_vmcs_cpu(raw_smp_processor_id());
4229}
4230
4231static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4232{
4233 loaded_vmcs->vmcs = alloc_vmcs();
4234 if (!loaded_vmcs->vmcs)
4235 return -ENOMEM;
4236
4237 loaded_vmcs->shadow_vmcs = NULL;
4238 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004239
4240 if (cpu_has_vmx_msr_bitmap()) {
4241 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4242 if (!loaded_vmcs->msr_bitmap)
4243 goto out_vmcs;
4244 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004245
4246 if (static_branch_unlikely(&enable_evmcs) &&
4247 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4248 struct hv_enlightened_vmcs *evmcs =
4249 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4250
4251 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4252 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004253 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004254 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004255
4256out_vmcs:
4257 free_loaded_vmcs(loaded_vmcs);
4258 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004259}
4260
Sam Ravnborg39959582007-06-01 00:47:13 -07004261static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262{
4263 int cpu;
4264
Zachary Amsden3230bb42009-09-29 11:38:37 -10004265 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004266 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004267 per_cpu(vmxarea, cpu) = NULL;
4268 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004269}
4270
Jim Mattsond37f4262017-12-22 12:12:16 -08004271enum vmcs_field_width {
4272 VMCS_FIELD_WIDTH_U16 = 0,
4273 VMCS_FIELD_WIDTH_U64 = 1,
4274 VMCS_FIELD_WIDTH_U32 = 2,
4275 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004276};
4277
Jim Mattsond37f4262017-12-22 12:12:16 -08004278static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004279{
4280 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004281 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004282 return (field >> 13) & 0x3 ;
4283}
4284
4285static inline int vmcs_field_readonly(unsigned long field)
4286{
4287 return (((field >> 10) & 0x3) == 1);
4288}
4289
Bandan Dasfe2b2012014-04-21 15:20:14 -04004290static void init_vmcs_shadow_fields(void)
4291{
4292 int i, j;
4293
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004294 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4295 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004296 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004297 (i + 1 == max_shadow_read_only_fields ||
4298 shadow_read_only_fields[i + 1] != field + 1))
4299 pr_err("Missing field from shadow_read_only_field %x\n",
4300 field + 1);
4301
4302 clear_bit(field, vmx_vmread_bitmap);
4303#ifdef CONFIG_X86_64
4304 if (field & 1)
4305 continue;
4306#endif
4307 if (j < i)
4308 shadow_read_only_fields[j] = field;
4309 j++;
4310 }
4311 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004312
4313 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004314 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004315 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004316 (i + 1 == max_shadow_read_write_fields ||
4317 shadow_read_write_fields[i + 1] != field + 1))
4318 pr_err("Missing field from shadow_read_write_field %x\n",
4319 field + 1);
4320
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004321 /*
4322 * PML and the preemption timer can be emulated, but the
4323 * processor cannot vmwrite to fields that don't exist
4324 * on bare metal.
4325 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004326 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004327 case GUEST_PML_INDEX:
4328 if (!cpu_has_vmx_pml())
4329 continue;
4330 break;
4331 case VMX_PREEMPTION_TIMER_VALUE:
4332 if (!cpu_has_vmx_preemption_timer())
4333 continue;
4334 break;
4335 case GUEST_INTR_STATUS:
4336 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004337 continue;
4338 break;
4339 default:
4340 break;
4341 }
4342
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004343 clear_bit(field, vmx_vmwrite_bitmap);
4344 clear_bit(field, vmx_vmread_bitmap);
4345#ifdef CONFIG_X86_64
4346 if (field & 1)
4347 continue;
4348#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004349 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004350 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004351 j++;
4352 }
4353 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004354}
4355
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356static __init int alloc_kvm_area(void)
4357{
4358 int cpu;
4359
Zachary Amsden3230bb42009-09-29 11:38:37 -10004360 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361 struct vmcs *vmcs;
4362
4363 vmcs = alloc_vmcs_cpu(cpu);
4364 if (!vmcs) {
4365 free_kvm_area();
4366 return -ENOMEM;
4367 }
4368
4369 per_cpu(vmxarea, cpu) = vmcs;
4370 }
4371 return 0;
4372}
4373
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004374static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004375 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004377 if (!emulate_invalid_guest_state) {
4378 /*
4379 * CS and SS RPL should be equal during guest entry according
4380 * to VMX spec, but in reality it is not always so. Since vcpu
4381 * is in the middle of the transition from real mode to
4382 * protected mode it is safe to assume that RPL 0 is a good
4383 * default value.
4384 */
4385 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004386 save->selector &= ~SEGMENT_RPL_MASK;
4387 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004388 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004390 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004391}
4392
4393static void enter_pmode(struct kvm_vcpu *vcpu)
4394{
4395 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004396 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397
Gleb Natapovd99e4152012-12-20 16:57:45 +02004398 /*
4399 * Update real mode segment cache. It may be not up-to-date if sement
4400 * register was written while vcpu was in a guest mode.
4401 */
4402 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4403 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4404 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4405 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4406 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4407 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4408
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004409 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410
Avi Kivity2fb92db2011-04-27 19:42:18 +03004411 vmx_segment_cache_clear(vmx);
4412
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004413 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414
4415 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004416 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4417 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418 vmcs_writel(GUEST_RFLAGS, flags);
4419
Rusty Russell66aee912007-07-17 23:34:16 +10004420 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4421 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422
4423 update_exception_bitmap(vcpu);
4424
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004425 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4426 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4427 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4428 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4429 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4430 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431}
4432
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004433static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004434{
Mathias Krause772e0312012-08-30 01:30:19 +02004435 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004436 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004437
Gleb Natapovd99e4152012-12-20 16:57:45 +02004438 var.dpl = 0x3;
4439 if (seg == VCPU_SREG_CS)
4440 var.type = 0x3;
4441
4442 if (!emulate_invalid_guest_state) {
4443 var.selector = var.base >> 4;
4444 var.base = var.base & 0xffff0;
4445 var.limit = 0xffff;
4446 var.g = 0;
4447 var.db = 0;
4448 var.present = 1;
4449 var.s = 1;
4450 var.l = 0;
4451 var.unusable = 0;
4452 var.type = 0x3;
4453 var.avl = 0;
4454 if (save->base & 0xf)
4455 printk_once(KERN_WARNING "kvm: segment base is not "
4456 "paragraph aligned when entering "
4457 "protected mode (seg=%d)", seg);
4458 }
4459
4460 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004461 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004462 vmcs_write32(sf->limit, var.limit);
4463 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464}
4465
4466static void enter_rmode(struct kvm_vcpu *vcpu)
4467{
4468 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004469 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004470 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004472 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4473 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4474 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4475 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4476 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004477 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4478 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004479
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004480 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481
Gleb Natapov776e58e2011-03-13 12:34:27 +02004482 /*
4483 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004484 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004485 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004486 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004487 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4488 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004489
Avi Kivity2fb92db2011-04-27 19:42:18 +03004490 vmx_segment_cache_clear(vmx);
4491
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004492 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004494 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4495
4496 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004497 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004498
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004499 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500
4501 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004502 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503 update_exception_bitmap(vcpu);
4504
Gleb Natapovd99e4152012-12-20 16:57:45 +02004505 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4506 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4507 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4508 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4509 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4510 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004511
Eddie Dong8668a3c2007-10-10 14:26:45 +08004512 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004513}
4514
Amit Shah401d10d2009-02-20 22:53:37 +05304515static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4516{
4517 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004518 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4519
4520 if (!msr)
4521 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304522
Avi Kivity44ea2b12009-09-06 15:55:37 +03004523 /*
4524 * Force kernel_gs_base reloading before EFER changes, as control
4525 * of this msr depends on is_long_mode().
4526 */
4527 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004528 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304529 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004530 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304531 msr->data = efer;
4532 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004533 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304534
4535 msr->data = efer & ~EFER_LME;
4536 }
4537 setup_msrs(vmx);
4538}
4539
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004540#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541
4542static void enter_lmode(struct kvm_vcpu *vcpu)
4543{
4544 u32 guest_tr_ar;
4545
Avi Kivity2fb92db2011-04-27 19:42:18 +03004546 vmx_segment_cache_clear(to_vmx(vcpu));
4547
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004549 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004550 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4551 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004552 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004553 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4554 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004555 }
Avi Kivityda38f432010-07-06 11:30:49 +03004556 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004557}
4558
4559static void exit_lmode(struct kvm_vcpu *vcpu)
4560{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004561 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004562 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004563}
4564
4565#endif
4566
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004567static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4568 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004569{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004570 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004571 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4572 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004573 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004574 } else {
4575 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004576 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004577}
4578
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004579static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004580{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004581 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004582}
4583
Avi Kivitye8467fd2009-12-29 18:43:06 +02004584static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4585{
4586 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4587
4588 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4589 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4590}
4591
Avi Kivityaff48ba2010-12-05 18:56:11 +02004592static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4593{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004594 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004595 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4596 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4597}
4598
Anthony Liguori25c4c272007-04-27 09:29:21 +03004599static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004600{
Avi Kivityfc78f512009-12-07 12:16:48 +02004601 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4602
4603 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4604 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004605}
4606
Sheng Yang14394422008-04-28 12:24:45 +08004607static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4608{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004609 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4610
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004611 if (!test_bit(VCPU_EXREG_PDPTR,
4612 (unsigned long *)&vcpu->arch.regs_dirty))
4613 return;
4614
Sheng Yang14394422008-04-28 12:24:45 +08004615 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004616 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4617 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4618 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4619 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004620 }
4621}
4622
Avi Kivity8f5d5492009-05-31 18:41:29 +03004623static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4624{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004625 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4626
Avi Kivity8f5d5492009-05-31 18:41:29 +03004627 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004628 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4629 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4630 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4631 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004632 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004633
4634 __set_bit(VCPU_EXREG_PDPTR,
4635 (unsigned long *)&vcpu->arch.regs_avail);
4636 __set_bit(VCPU_EXREG_PDPTR,
4637 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004638}
4639
David Matlack38991522016-11-29 18:14:08 -08004640static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4641{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004642 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4643 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004644 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4645
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004646 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004647 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4648 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4649 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4650
4651 return fixed_bits_valid(val, fixed0, fixed1);
4652}
4653
4654static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4655{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004656 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4657 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004658
4659 return fixed_bits_valid(val, fixed0, fixed1);
4660}
4661
4662static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4663{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004664 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4665 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004666
4667 return fixed_bits_valid(val, fixed0, fixed1);
4668}
4669
4670/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4671#define nested_guest_cr4_valid nested_cr4_valid
4672#define nested_host_cr4_valid nested_cr4_valid
4673
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004674static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004675
4676static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4677 unsigned long cr0,
4678 struct kvm_vcpu *vcpu)
4679{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004680 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4681 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004682 if (!(cr0 & X86_CR0_PG)) {
4683 /* From paging/starting to nonpaging */
4684 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004685 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004686 (CPU_BASED_CR3_LOAD_EXITING |
4687 CPU_BASED_CR3_STORE_EXITING));
4688 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004689 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004690 } else if (!is_paging(vcpu)) {
4691 /* From nonpaging to paging */
4692 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004693 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004694 ~(CPU_BASED_CR3_LOAD_EXITING |
4695 CPU_BASED_CR3_STORE_EXITING));
4696 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004697 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004698 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004699
4700 if (!(cr0 & X86_CR0_WP))
4701 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004702}
4703
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4705{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004707 unsigned long hw_cr0;
4708
Gleb Natapov50378782013-02-04 16:00:28 +02004709 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004710 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004711 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004712 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004713 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004714
Gleb Natapov218e7632013-01-21 15:36:45 +02004715 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4716 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004717
Gleb Natapov218e7632013-01-21 15:36:45 +02004718 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4719 enter_rmode(vcpu);
4720 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004722#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004723 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004724 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004726 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727 exit_lmode(vcpu);
4728 }
4729#endif
4730
Sean Christophersonb4d18512018-03-05 12:04:40 -08004731 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004732 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4733
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004735 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004736 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004737
4738 /* depends on vcpu->arch.cr0 to be set to a new value */
4739 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740}
4741
Yu Zhang855feb62017-08-24 20:27:55 +08004742static int get_ept_level(struct kvm_vcpu *vcpu)
4743{
4744 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4745 return 5;
4746 return 4;
4747}
4748
Peter Feiner995f00a2017-06-30 17:26:32 -07004749static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004750{
Yu Zhang855feb62017-08-24 20:27:55 +08004751 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004752
Yu Zhang855feb62017-08-24 20:27:55 +08004753 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004754
Peter Feiner995f00a2017-06-30 17:26:32 -07004755 if (enable_ept_ad_bits &&
4756 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004757 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004758 eptp |= (root_hpa & PAGE_MASK);
4759
4760 return eptp;
4761}
4762
Avi Kivity6aa8b732006-12-10 02:21:36 -08004763static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4764{
Sheng Yang14394422008-04-28 12:24:45 +08004765 unsigned long guest_cr3;
4766 u64 eptp;
4767
4768 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004769 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004770 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004771 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004772 if (enable_unrestricted_guest || is_paging(vcpu) ||
4773 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004774 guest_cr3 = kvm_read_cr3(vcpu);
4775 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004776 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004777 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004778 }
4779
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004780 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004781 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004782}
4783
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004784static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004786 /*
4787 * Pass through host's Machine Check Enable value to hw_cr4, which
4788 * is in force while we are in guest mode. Do not let guests control
4789 * this bit, even if host CR4.MCE == 0.
4790 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004791 unsigned long hw_cr4;
4792
4793 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4794 if (enable_unrestricted_guest)
4795 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4796 else if (to_vmx(vcpu)->rmode.vm86_active)
4797 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4798 else
4799 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004800
Sean Christopherson64f7a112018-04-30 10:01:06 -07004801 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4802 if (cr4 & X86_CR4_UMIP) {
4803 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02004804 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07004805 hw_cr4 &= ~X86_CR4_UMIP;
4806 } else if (!is_guest_mode(vcpu) ||
4807 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4808 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4809 SECONDARY_EXEC_DESC);
4810 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02004811
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004812 if (cr4 & X86_CR4_VMXE) {
4813 /*
4814 * To use VMXON (and later other VMX instructions), a guest
4815 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4816 * So basically the check on whether to allow nested VMX
4817 * is here.
4818 */
4819 if (!nested_vmx_allowed(vcpu))
4820 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004821 }
David Matlack38991522016-11-29 18:14:08 -08004822
4823 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004824 return 1;
4825
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004826 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004827
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004828 if (!enable_unrestricted_guest) {
4829 if (enable_ept) {
4830 if (!is_paging(vcpu)) {
4831 hw_cr4 &= ~X86_CR4_PAE;
4832 hw_cr4 |= X86_CR4_PSE;
4833 } else if (!(cr4 & X86_CR4_PAE)) {
4834 hw_cr4 &= ~X86_CR4_PAE;
4835 }
4836 }
4837
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004838 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004839 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4840 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4841 * to be manually disabled when guest switches to non-paging
4842 * mode.
4843 *
4844 * If !enable_unrestricted_guest, the CPU is always running
4845 * with CR0.PG=1 and CR4 needs to be modified.
4846 * If enable_unrestricted_guest, the CPU automatically
4847 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004848 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004849 if (!is_paging(vcpu))
4850 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4851 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004852
Sheng Yang14394422008-04-28 12:24:45 +08004853 vmcs_writel(CR4_READ_SHADOW, cr4);
4854 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004855 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856}
4857
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858static void vmx_get_segment(struct kvm_vcpu *vcpu,
4859 struct kvm_segment *var, int seg)
4860{
Avi Kivitya9179492011-01-03 14:28:52 +02004861 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 u32 ar;
4863
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004864 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004865 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004866 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004867 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004868 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004869 var->base = vmx_read_guest_seg_base(vmx, seg);
4870 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4871 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004872 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004873 var->base = vmx_read_guest_seg_base(vmx, seg);
4874 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4875 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4876 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004877 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878 var->type = ar & 15;
4879 var->s = (ar >> 4) & 1;
4880 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004881 /*
4882 * Some userspaces do not preserve unusable property. Since usable
4883 * segment has to be present according to VMX spec we can use present
4884 * property to amend userspace bug by making unusable segment always
4885 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4886 * segment as unusable.
4887 */
4888 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004889 var->avl = (ar >> 12) & 1;
4890 var->l = (ar >> 13) & 1;
4891 var->db = (ar >> 14) & 1;
4892 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893}
4894
Avi Kivitya9179492011-01-03 14:28:52 +02004895static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4896{
Avi Kivitya9179492011-01-03 14:28:52 +02004897 struct kvm_segment s;
4898
4899 if (to_vmx(vcpu)->rmode.vm86_active) {
4900 vmx_get_segment(vcpu, &s, seg);
4901 return s.base;
4902 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004903 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004904}
4905
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004906static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004907{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004908 struct vcpu_vmx *vmx = to_vmx(vcpu);
4909
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004910 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004911 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004912 else {
4913 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004914 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004915 }
Avi Kivity69c73022011-03-07 15:26:44 +02004916}
4917
Avi Kivity653e3102007-05-07 10:55:37 +03004918static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004920 u32 ar;
4921
Avi Kivityf0495f92012-06-07 17:06:10 +03004922 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004923 ar = 1 << 16;
4924 else {
4925 ar = var->type & 15;
4926 ar |= (var->s & 1) << 4;
4927 ar |= (var->dpl & 3) << 5;
4928 ar |= (var->present & 1) << 7;
4929 ar |= (var->avl & 1) << 12;
4930 ar |= (var->l & 1) << 13;
4931 ar |= (var->db & 1) << 14;
4932 ar |= (var->g & 1) << 15;
4933 }
Avi Kivity653e3102007-05-07 10:55:37 +03004934
4935 return ar;
4936}
4937
4938static void vmx_set_segment(struct kvm_vcpu *vcpu,
4939 struct kvm_segment *var, int seg)
4940{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004942 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004943
Avi Kivity2fb92db2011-04-27 19:42:18 +03004944 vmx_segment_cache_clear(vmx);
4945
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004946 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4947 vmx->rmode.segs[seg] = *var;
4948 if (seg == VCPU_SREG_TR)
4949 vmcs_write16(sf->selector, var->selector);
4950 else if (var->s)
4951 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004952 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004953 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004954
Avi Kivity653e3102007-05-07 10:55:37 +03004955 vmcs_writel(sf->base, var->base);
4956 vmcs_write32(sf->limit, var->limit);
4957 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004958
4959 /*
4960 * Fix the "Accessed" bit in AR field of segment registers for older
4961 * qemu binaries.
4962 * IA32 arch specifies that at the time of processor reset the
4963 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004964 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004965 * state vmexit when "unrestricted guest" mode is turned on.
4966 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4967 * tree. Newer qemu binaries with that qemu fix would not need this
4968 * kvm hack.
4969 */
4970 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004971 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004972
Gleb Natapovf924d662012-12-12 19:10:55 +02004973 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004974
4975out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004976 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977}
4978
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4980{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004981 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982
4983 *db = (ar >> 14) & 1;
4984 *l = (ar >> 13) & 1;
4985}
4986
Gleb Natapov89a27f42010-02-16 10:51:48 +02004987static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004989 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4990 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004991}
4992
Gleb Natapov89a27f42010-02-16 10:51:48 +02004993static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004995 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4996 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004997}
4998
Gleb Natapov89a27f42010-02-16 10:51:48 +02004999static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005001 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5002 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005003}
5004
Gleb Natapov89a27f42010-02-16 10:51:48 +02005005static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005007 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5008 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009}
5010
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005011static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5012{
5013 struct kvm_segment var;
5014 u32 ar;
5015
5016 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005017 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005018 if (seg == VCPU_SREG_CS)
5019 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005020 ar = vmx_segment_access_rights(&var);
5021
5022 if (var.base != (var.selector << 4))
5023 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005024 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005025 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005026 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005027 return false;
5028
5029 return true;
5030}
5031
5032static bool code_segment_valid(struct kvm_vcpu *vcpu)
5033{
5034 struct kvm_segment cs;
5035 unsigned int cs_rpl;
5036
5037 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005038 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005039
Avi Kivity1872a3f2009-01-04 23:26:52 +02005040 if (cs.unusable)
5041 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005042 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005043 return false;
5044 if (!cs.s)
5045 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005046 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005047 if (cs.dpl > cs_rpl)
5048 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005049 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005050 if (cs.dpl != cs_rpl)
5051 return false;
5052 }
5053 if (!cs.present)
5054 return false;
5055
5056 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5057 return true;
5058}
5059
5060static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5061{
5062 struct kvm_segment ss;
5063 unsigned int ss_rpl;
5064
5065 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005066 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005067
Avi Kivity1872a3f2009-01-04 23:26:52 +02005068 if (ss.unusable)
5069 return true;
5070 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005071 return false;
5072 if (!ss.s)
5073 return false;
5074 if (ss.dpl != ss_rpl) /* DPL != RPL */
5075 return false;
5076 if (!ss.present)
5077 return false;
5078
5079 return true;
5080}
5081
5082static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5083{
5084 struct kvm_segment var;
5085 unsigned int rpl;
5086
5087 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005088 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005089
Avi Kivity1872a3f2009-01-04 23:26:52 +02005090 if (var.unusable)
5091 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005092 if (!var.s)
5093 return false;
5094 if (!var.present)
5095 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005096 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005097 if (var.dpl < rpl) /* DPL < RPL */
5098 return false;
5099 }
5100
5101 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5102 * rights flags
5103 */
5104 return true;
5105}
5106
5107static bool tr_valid(struct kvm_vcpu *vcpu)
5108{
5109 struct kvm_segment tr;
5110
5111 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5112
Avi Kivity1872a3f2009-01-04 23:26:52 +02005113 if (tr.unusable)
5114 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005115 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005116 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005117 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005118 return false;
5119 if (!tr.present)
5120 return false;
5121
5122 return true;
5123}
5124
5125static bool ldtr_valid(struct kvm_vcpu *vcpu)
5126{
5127 struct kvm_segment ldtr;
5128
5129 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5130
Avi Kivity1872a3f2009-01-04 23:26:52 +02005131 if (ldtr.unusable)
5132 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005133 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005134 return false;
5135 if (ldtr.type != 2)
5136 return false;
5137 if (!ldtr.present)
5138 return false;
5139
5140 return true;
5141}
5142
5143static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5144{
5145 struct kvm_segment cs, ss;
5146
5147 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5148 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5149
Nadav Amitb32a9912015-03-29 16:33:04 +03005150 return ((cs.selector & SEGMENT_RPL_MASK) ==
5151 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005152}
5153
5154/*
5155 * Check if guest state is valid. Returns true if valid, false if
5156 * not.
5157 * We assume that registers are always usable
5158 */
5159static bool guest_state_valid(struct kvm_vcpu *vcpu)
5160{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005161 if (enable_unrestricted_guest)
5162 return true;
5163
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005164 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005165 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005166 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5167 return false;
5168 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5169 return false;
5170 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5171 return false;
5172 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5173 return false;
5174 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5175 return false;
5176 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5177 return false;
5178 } else {
5179 /* protected mode guest state checks */
5180 if (!cs_ss_rpl_check(vcpu))
5181 return false;
5182 if (!code_segment_valid(vcpu))
5183 return false;
5184 if (!stack_segment_valid(vcpu))
5185 return false;
5186 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5187 return false;
5188 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5189 return false;
5190 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5191 return false;
5192 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5193 return false;
5194 if (!tr_valid(vcpu))
5195 return false;
5196 if (!ldtr_valid(vcpu))
5197 return false;
5198 }
5199 /* TODO:
5200 * - Add checks on RIP
5201 * - Add checks on RFLAGS
5202 */
5203
5204 return true;
5205}
5206
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005207static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5208{
5209 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5210}
5211
Mike Dayd77c26f2007-10-08 09:02:08 -04005212static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005213{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005214 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005215 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005216 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005218 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005219 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005220 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5221 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005222 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005223 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005224 r = kvm_write_guest_page(kvm, fn++, &data,
5225 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005226 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005227 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005228 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5229 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005230 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005231 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5232 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005233 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005234 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005235 r = kvm_write_guest_page(kvm, fn, &data,
5236 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5237 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005238out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005239 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005240 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005241}
5242
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005243static int init_rmode_identity_map(struct kvm *kvm)
5244{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005245 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005246 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005247 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005248 u32 tmp;
5249
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005250 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005251 mutex_lock(&kvm->slots_lock);
5252
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005253 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005254 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005255
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005256 if (!kvm_vmx->ept_identity_map_addr)
5257 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5258 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005259
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005260 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005261 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005262 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005263 goto out2;
5264
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005265 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005266 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5267 if (r < 0)
5268 goto out;
5269 /* Set up identity-mapping pagetable for EPT in real mode */
5270 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5271 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5272 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5273 r = kvm_write_guest_page(kvm, identity_map_pfn,
5274 &tmp, i * sizeof(tmp), sizeof(tmp));
5275 if (r < 0)
5276 goto out;
5277 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005278 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005279
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005280out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005281 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005282
5283out2:
5284 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005285 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005286}
5287
Avi Kivity6aa8b732006-12-10 02:21:36 -08005288static void seg_setup(int seg)
5289{
Mathias Krause772e0312012-08-30 01:30:19 +02005290 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005291 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292
5293 vmcs_write16(sf->selector, 0);
5294 vmcs_writel(sf->base, 0);
5295 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005296 ar = 0x93;
5297 if (seg == VCPU_SREG_CS)
5298 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005299
5300 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301}
5302
Sheng Yangf78e0e22007-10-29 09:40:42 +08005303static int alloc_apic_access_page(struct kvm *kvm)
5304{
Xiao Guangrong44841412012-09-07 14:14:20 +08005305 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005306 int r = 0;
5307
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005308 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005309 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005310 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005311 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5312 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005313 if (r)
5314 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005315
Tang Chen73a6d942014-09-11 13:38:00 +08005316 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005317 if (is_error_page(page)) {
5318 r = -EFAULT;
5319 goto out;
5320 }
5321
Tang Chenc24ae0d2014-09-24 15:57:58 +08005322 /*
5323 * Do not pin the page in memory, so that memory hot-unplug
5324 * is able to migrate it.
5325 */
5326 put_page(page);
5327 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005328out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005329 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005330 return r;
5331}
5332
Wanpeng Li991e7a02015-09-16 17:30:05 +08005333static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005334{
5335 int vpid;
5336
Avi Kivity919818a2009-03-23 18:01:29 +02005337 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005338 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005339 spin_lock(&vmx_vpid_lock);
5340 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005341 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005342 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005343 else
5344 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005345 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005346 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005347}
5348
Wanpeng Li991e7a02015-09-16 17:30:05 +08005349static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005350{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005351 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005352 return;
5353 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005354 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005355 spin_unlock(&vmx_vpid_lock);
5356}
5357
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005358static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5359 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005360{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005361 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005362
5363 if (!cpu_has_vmx_msr_bitmap())
5364 return;
5365
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005366 if (static_branch_unlikely(&enable_evmcs))
5367 evmcs_touch_msr_bitmap();
5368
Sheng Yang25c5f222008-03-28 13:18:56 +08005369 /*
5370 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5371 * have the write-low and read-high bitmap offsets the wrong way round.
5372 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5373 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005374 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005375 if (type & MSR_TYPE_R)
5376 /* read-low */
5377 __clear_bit(msr, msr_bitmap + 0x000 / f);
5378
5379 if (type & MSR_TYPE_W)
5380 /* write-low */
5381 __clear_bit(msr, msr_bitmap + 0x800 / f);
5382
Sheng Yang25c5f222008-03-28 13:18:56 +08005383 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5384 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005385 if (type & MSR_TYPE_R)
5386 /* read-high */
5387 __clear_bit(msr, msr_bitmap + 0x400 / f);
5388
5389 if (type & MSR_TYPE_W)
5390 /* write-high */
5391 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5392
5393 }
5394}
5395
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005396static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5397 u32 msr, int type)
5398{
5399 int f = sizeof(unsigned long);
5400
5401 if (!cpu_has_vmx_msr_bitmap())
5402 return;
5403
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005404 if (static_branch_unlikely(&enable_evmcs))
5405 evmcs_touch_msr_bitmap();
5406
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005407 /*
5408 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5409 * have the write-low and read-high bitmap offsets the wrong way round.
5410 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5411 */
5412 if (msr <= 0x1fff) {
5413 if (type & MSR_TYPE_R)
5414 /* read-low */
5415 __set_bit(msr, msr_bitmap + 0x000 / f);
5416
5417 if (type & MSR_TYPE_W)
5418 /* write-low */
5419 __set_bit(msr, msr_bitmap + 0x800 / f);
5420
5421 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5422 msr &= 0x1fff;
5423 if (type & MSR_TYPE_R)
5424 /* read-high */
5425 __set_bit(msr, msr_bitmap + 0x400 / f);
5426
5427 if (type & MSR_TYPE_W)
5428 /* write-high */
5429 __set_bit(msr, msr_bitmap + 0xc00 / f);
5430
5431 }
5432}
5433
5434static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5435 u32 msr, int type, bool value)
5436{
5437 if (value)
5438 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5439 else
5440 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5441}
5442
Wincy Vanf2b93282015-02-03 23:56:03 +08005443/*
5444 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5445 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5446 */
5447static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5448 unsigned long *msr_bitmap_nested,
5449 u32 msr, int type)
5450{
5451 int f = sizeof(unsigned long);
5452
Wincy Vanf2b93282015-02-03 23:56:03 +08005453 /*
5454 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5455 * have the write-low and read-high bitmap offsets the wrong way round.
5456 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5457 */
5458 if (msr <= 0x1fff) {
5459 if (type & MSR_TYPE_R &&
5460 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5461 /* read-low */
5462 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5463
5464 if (type & MSR_TYPE_W &&
5465 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5466 /* write-low */
5467 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5468
5469 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5470 msr &= 0x1fff;
5471 if (type & MSR_TYPE_R &&
5472 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5473 /* read-high */
5474 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5475
5476 if (type & MSR_TYPE_W &&
5477 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5478 /* write-high */
5479 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5480
5481 }
5482}
5483
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005484static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005485{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005486 u8 mode = 0;
5487
5488 if (cpu_has_secondary_exec_ctrls() &&
5489 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5490 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5491 mode |= MSR_BITMAP_MODE_X2APIC;
5492 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5493 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5494 }
5495
5496 if (is_long_mode(vcpu))
5497 mode |= MSR_BITMAP_MODE_LM;
5498
5499 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005500}
5501
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005502#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5503
5504static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5505 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005506{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005507 int msr;
5508
5509 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5510 unsigned word = msr / BITS_PER_LONG;
5511 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5512 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005513 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005514
5515 if (mode & MSR_BITMAP_MODE_X2APIC) {
5516 /*
5517 * TPR reads and writes can be virtualized even if virtual interrupt
5518 * delivery is not in use.
5519 */
5520 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5521 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5522 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5523 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5524 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5525 }
5526 }
5527}
5528
5529static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5530{
5531 struct vcpu_vmx *vmx = to_vmx(vcpu);
5532 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5533 u8 mode = vmx_msr_bitmap_mode(vcpu);
5534 u8 changed = mode ^ vmx->msr_bitmap_mode;
5535
5536 if (!changed)
5537 return;
5538
5539 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5540 !(mode & MSR_BITMAP_MODE_LM));
5541
5542 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5543 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5544
5545 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005546}
5547
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005548static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005549{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005550 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005551}
5552
David Matlackc9f04402017-08-01 14:00:40 -07005553static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5554{
5555 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5556 gfn_t gfn;
5557
5558 /*
5559 * Don't need to mark the APIC access page dirty; it is never
5560 * written to by the CPU during APIC virtualization.
5561 */
5562
5563 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5564 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5565 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5566 }
5567
5568 if (nested_cpu_has_posted_intr(vmcs12)) {
5569 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5570 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5571 }
5572}
5573
5574
David Hildenbrand6342c502017-01-25 11:58:58 +01005575static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005576{
5577 struct vcpu_vmx *vmx = to_vmx(vcpu);
5578 int max_irr;
5579 void *vapic_page;
5580 u16 status;
5581
David Matlackc9f04402017-08-01 14:00:40 -07005582 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5583 return;
Wincy Van705699a2015-02-03 23:58:17 +08005584
David Matlackc9f04402017-08-01 14:00:40 -07005585 vmx->nested.pi_pending = false;
5586 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5587 return;
Wincy Van705699a2015-02-03 23:58:17 +08005588
David Matlackc9f04402017-08-01 14:00:40 -07005589 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5590 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005591 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005592 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5593 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005594 kunmap(vmx->nested.virtual_apic_page);
5595
5596 status = vmcs_read16(GUEST_INTR_STATUS);
5597 if ((u8)max_irr > ((u8)status & 0xff)) {
5598 status &= ~0xff;
5599 status |= (u8)max_irr;
5600 vmcs_write16(GUEST_INTR_STATUS, status);
5601 }
5602 }
David Matlackc9f04402017-08-01 14:00:40 -07005603
5604 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005605}
5606
Wincy Van06a55242017-04-28 13:13:59 +08005607static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5608 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005609{
5610#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005611 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5612
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005613 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005614 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005615 * The vector of interrupt to be delivered to vcpu had
5616 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005617 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005618 * Following cases will be reached in this block, and
5619 * we always send a notification event in all cases as
5620 * explained below.
5621 *
5622 * Case 1: vcpu keeps in non-root mode. Sending a
5623 * notification event posts the interrupt to vcpu.
5624 *
5625 * Case 2: vcpu exits to root mode and is still
5626 * runnable. PIR will be synced to vIRR before the
5627 * next vcpu entry. Sending a notification event in
5628 * this case has no effect, as vcpu is not in root
5629 * mode.
5630 *
5631 * Case 3: vcpu exits to root mode and is blocked.
5632 * vcpu_block() has already synced PIR to vIRR and
5633 * never blocks vcpu if vIRR is not cleared. Therefore,
5634 * a blocked vcpu here does not wait for any requested
5635 * interrupts in PIR, and sending a notification event
5636 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005637 */
Feng Wu28b835d2015-09-18 22:29:54 +08005638
Wincy Van06a55242017-04-28 13:13:59 +08005639 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005640 return true;
5641 }
5642#endif
5643 return false;
5644}
5645
Wincy Van705699a2015-02-03 23:58:17 +08005646static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5647 int vector)
5648{
5649 struct vcpu_vmx *vmx = to_vmx(vcpu);
5650
5651 if (is_guest_mode(vcpu) &&
5652 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005653 /*
5654 * If a posted intr is not recognized by hardware,
5655 * we will accomplish it in the next vmentry.
5656 */
5657 vmx->nested.pi_pending = true;
5658 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005659 /* the PIR and ON have been set by L1. */
5660 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5661 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005662 return 0;
5663 }
5664 return -1;
5665}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005666/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005667 * Send interrupt to vcpu via posted interrupt way.
5668 * 1. If target vcpu is running(non-root mode), send posted interrupt
5669 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5670 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5671 * interrupt from PIR in next vmentry.
5672 */
5673static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5674{
5675 struct vcpu_vmx *vmx = to_vmx(vcpu);
5676 int r;
5677
Wincy Van705699a2015-02-03 23:58:17 +08005678 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5679 if (!r)
5680 return;
5681
Yang Zhanga20ed542013-04-11 19:25:15 +08005682 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5683 return;
5684
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005685 /* If a previous notification has sent the IPI, nothing to do. */
5686 if (pi_test_and_set_on(&vmx->pi_desc))
5687 return;
5688
Wincy Van06a55242017-04-28 13:13:59 +08005689 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005690 kvm_vcpu_kick(vcpu);
5691}
5692
Avi Kivity6aa8b732006-12-10 02:21:36 -08005693/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005694 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5695 * will not change in the lifetime of the guest.
5696 * Note that host-state that does change is set elsewhere. E.g., host-state
5697 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5698 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005699static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005700{
5701 u32 low32, high32;
5702 unsigned long tmpl;
5703 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005704 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005705
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005706 cr0 = read_cr0();
5707 WARN_ON(cr0 & X86_CR0_TS);
5708 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005709
5710 /*
5711 * Save the most likely value for this task's CR3 in the VMCS.
5712 * We can't use __get_current_cr3_fast() because we're not atomic.
5713 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005714 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005715 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005716 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005717
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005718 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005719 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005720 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005721 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005722
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005723 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005724#ifdef CONFIG_X86_64
5725 /*
5726 * Load null selectors, so we can avoid reloading them in
5727 * __vmx_load_host_state(), in case userspace uses the null selectors
5728 * too (the expected case).
5729 */
5730 vmcs_write16(HOST_DS_SELECTOR, 0);
5731 vmcs_write16(HOST_ES_SELECTOR, 0);
5732#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005733 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5734 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005735#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005736 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5737 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5738
Juergen Gross87930012017-09-04 12:25:27 +02005739 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005740 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005741 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005742
Avi Kivity83287ea422012-09-16 15:10:57 +03005743 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005744
5745 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5746 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5747 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5748 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5749
5750 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5751 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5752 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5753 }
5754}
5755
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005756static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5757{
5758 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5759 if (enable_ept)
5760 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005761 if (is_guest_mode(&vmx->vcpu))
5762 vmx->vcpu.arch.cr4_guest_owned_bits &=
5763 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005764 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5765}
5766
Yang Zhang01e439b2013-04-11 19:25:12 +08005767static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5768{
5769 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5770
Andrey Smetanind62caab2015-11-10 15:36:33 +03005771 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005772 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005773
5774 if (!enable_vnmi)
5775 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5776
Yunhong Jiang64672c92016-06-13 14:19:59 -07005777 /* Enable the preemption timer dynamically */
5778 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005779 return pin_based_exec_ctrl;
5780}
5781
Andrey Smetanind62caab2015-11-10 15:36:33 +03005782static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5783{
5784 struct vcpu_vmx *vmx = to_vmx(vcpu);
5785
5786 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005787 if (cpu_has_secondary_exec_ctrls()) {
5788 if (kvm_vcpu_apicv_active(vcpu))
5789 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5790 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5791 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5792 else
5793 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5794 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5795 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5796 }
5797
5798 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005799 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005800}
5801
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005802static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5803{
5804 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005805
5806 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5807 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5808
Paolo Bonzini35754c92015-07-29 12:05:37 +02005809 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005810 exec_control &= ~CPU_BASED_TPR_SHADOW;
5811#ifdef CONFIG_X86_64
5812 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5813 CPU_BASED_CR8_LOAD_EXITING;
5814#endif
5815 }
5816 if (!enable_ept)
5817 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5818 CPU_BASED_CR3_LOAD_EXITING |
5819 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005820 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5821 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5822 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005823 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5824 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005825 return exec_control;
5826}
5827
Jim Mattson45ec3682017-08-23 16:32:04 -07005828static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005829{
Jim Mattson45ec3682017-08-23 16:32:04 -07005830 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005831 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005832}
5833
Jim Mattson75f4fc82017-08-23 16:32:03 -07005834static bool vmx_rdseed_supported(void)
5835{
5836 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005837 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005838}
5839
Paolo Bonzini80154d72017-08-24 13:55:35 +02005840static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005841{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005842 struct kvm_vcpu *vcpu = &vmx->vcpu;
5843
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005844 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005845
Paolo Bonzini80154d72017-08-24 13:55:35 +02005846 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005847 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5848 if (vmx->vpid == 0)
5849 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5850 if (!enable_ept) {
5851 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5852 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005853 /* Enable INVPCID for non-ept guests may cause performance regression. */
5854 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005855 }
5856 if (!enable_unrestricted_guest)
5857 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005858 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005859 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005860 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005861 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5862 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005863 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005864
5865 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5866 * in vmx_set_cr4. */
5867 exec_control &= ~SECONDARY_EXEC_DESC;
5868
Abel Gordonabc4fc52013-04-18 14:35:25 +03005869 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5870 (handle_vmptrld).
5871 We can NOT enable shadow_vmcs here because we don't have yet
5872 a current VMCS12
5873 */
5874 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005875
5876 if (!enable_pml)
5877 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005878
Paolo Bonzini3db13482017-08-24 14:48:03 +02005879 if (vmx_xsaves_supported()) {
5880 /* Exposing XSAVES only when XSAVE is exposed */
5881 bool xsaves_enabled =
5882 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5883 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5884
5885 if (!xsaves_enabled)
5886 exec_control &= ~SECONDARY_EXEC_XSAVES;
5887
5888 if (nested) {
5889 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005890 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005891 SECONDARY_EXEC_XSAVES;
5892 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005893 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005894 ~SECONDARY_EXEC_XSAVES;
5895 }
5896 }
5897
Paolo Bonzini80154d72017-08-24 13:55:35 +02005898 if (vmx_rdtscp_supported()) {
5899 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5900 if (!rdtscp_enabled)
5901 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5902
5903 if (nested) {
5904 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005905 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005906 SECONDARY_EXEC_RDTSCP;
5907 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005908 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005909 ~SECONDARY_EXEC_RDTSCP;
5910 }
5911 }
5912
5913 if (vmx_invpcid_supported()) {
5914 /* Exposing INVPCID only when PCID is exposed */
5915 bool invpcid_enabled =
5916 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5917 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5918
5919 if (!invpcid_enabled) {
5920 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5921 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5922 }
5923
5924 if (nested) {
5925 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005926 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005927 SECONDARY_EXEC_ENABLE_INVPCID;
5928 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005929 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005930 ~SECONDARY_EXEC_ENABLE_INVPCID;
5931 }
5932 }
5933
Jim Mattson45ec3682017-08-23 16:32:04 -07005934 if (vmx_rdrand_supported()) {
5935 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5936 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005937 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005938
5939 if (nested) {
5940 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005941 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005942 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005943 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005944 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005945 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005946 }
5947 }
5948
Jim Mattson75f4fc82017-08-23 16:32:03 -07005949 if (vmx_rdseed_supported()) {
5950 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5951 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005952 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005953
5954 if (nested) {
5955 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005956 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005957 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005958 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005959 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005960 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005961 }
5962 }
5963
Paolo Bonzini80154d72017-08-24 13:55:35 +02005964 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005965}
5966
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005967static void ept_set_mmio_spte_mask(void)
5968{
5969 /*
5970 * EPT Misconfigurations can be generated if the value of bits 2:0
5971 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005972 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005973 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5974 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005975}
5976
Wanpeng Lif53cd632014-12-02 19:14:58 +08005977#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005978/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005979 * Sets up the vmcs for emulated real mode.
5980 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005981static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005982{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005983#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005984 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005985#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005986 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005987
Abel Gordon4607c2d2013-04-18 14:35:55 +03005988 if (enable_shadow_vmcs) {
5989 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5990 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5991 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005992 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005993 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005994
Avi Kivity6aa8b732006-12-10 02:21:36 -08005995 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5996
Avi Kivity6aa8b732006-12-10 02:21:36 -08005997 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005998 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005999 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006000
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006001 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006002
Dan Williamsdfa169b2016-06-02 11:17:24 -07006003 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006004 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006005 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006006 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006007 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006008
Andrey Smetanind62caab2015-11-10 15:36:33 +03006009 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006010 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6011 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6012 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6013 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6014
6015 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006016
Li RongQing0bcf2612015-12-03 13:29:34 +08006017 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006018 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006019 }
6020
Wanpeng Lib31c1142018-03-12 04:53:04 -07006021 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006022 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006023 vmx->ple_window = ple_window;
6024 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006025 }
6026
Xiao Guangrongc3707952011-07-12 03:28:04 +08006027 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6028 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006029 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6030
Avi Kivity9581d442010-10-19 16:46:55 +02006031 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6032 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006033 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006034#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006035 rdmsrl(MSR_FS_BASE, a);
6036 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6037 rdmsrl(MSR_GS_BASE, a);
6038 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6039#else
6040 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6041 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6042#endif
6043
Bandan Das2a499e42017-08-03 15:54:41 -04006044 if (cpu_has_vmx_vmfunc())
6045 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6046
Eddie Dong2cc51562007-05-21 07:28:09 +03006047 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6048 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006049 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006050 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006051 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006052
Radim Krčmář74545702015-04-27 15:11:25 +02006053 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6054 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006055
Paolo Bonzini03916db2014-07-24 14:21:57 +02006056 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006057 u32 index = vmx_msr_index[i];
6058 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006059 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006060
6061 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6062 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006063 if (wrmsr_safe(index, data_low, data_high) < 0)
6064 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006065 vmx->guest_msrs[j].index = i;
6066 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006067 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006068 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006069 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006070
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006071 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6072 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006073
6074 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006075
6076 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006077 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006078
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006079 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6080 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6081
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006082 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006083
Wanpeng Lif53cd632014-12-02 19:14:58 +08006084 if (vmx_xsaves_supported())
6085 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6086
Peter Feiner4e595162016-07-07 14:49:58 -07006087 if (enable_pml) {
6088 ASSERT(vmx->pml_pg);
6089 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6090 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6091 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006092}
6093
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006094static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006095{
6096 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006097 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006098 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006099
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006100 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006101 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006102
Wanpeng Li518e7b92018-02-28 14:03:31 +08006103 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006104 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006105 kvm_set_cr8(vcpu, 0);
6106
6107 if (!init_event) {
6108 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6109 MSR_IA32_APICBASE_ENABLE;
6110 if (kvm_vcpu_is_reset_bsp(vcpu))
6111 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6112 apic_base_msr.host_initiated = true;
6113 kvm_set_apic_base(vcpu, &apic_base_msr);
6114 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006115
Avi Kivity2fb92db2011-04-27 19:42:18 +03006116 vmx_segment_cache_clear(vmx);
6117
Avi Kivity5706be02008-08-20 15:07:31 +03006118 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006119 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006120 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006121
6122 seg_setup(VCPU_SREG_DS);
6123 seg_setup(VCPU_SREG_ES);
6124 seg_setup(VCPU_SREG_FS);
6125 seg_setup(VCPU_SREG_GS);
6126 seg_setup(VCPU_SREG_SS);
6127
6128 vmcs_write16(GUEST_TR_SELECTOR, 0);
6129 vmcs_writel(GUEST_TR_BASE, 0);
6130 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6131 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6132
6133 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6134 vmcs_writel(GUEST_LDTR_BASE, 0);
6135 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6136 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6137
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006138 if (!init_event) {
6139 vmcs_write32(GUEST_SYSENTER_CS, 0);
6140 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6141 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6142 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6143 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006144
Wanpeng Lic37c2872017-11-20 14:52:21 -08006145 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006146 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006147
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006148 vmcs_writel(GUEST_GDTR_BASE, 0);
6149 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6150
6151 vmcs_writel(GUEST_IDTR_BASE, 0);
6152 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6153
Anthony Liguori443381a2010-12-06 10:53:38 -06006154 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006155 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006156 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006157 if (kvm_mpx_supported())
6158 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006159
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006160 setup_msrs(vmx);
6161
Avi Kivity6aa8b732006-12-10 02:21:36 -08006162 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6163
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006164 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006165 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006166 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006167 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006168 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006169 vmcs_write32(TPR_THRESHOLD, 0);
6170 }
6171
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006172 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006173
Sheng Yang2384d2b2008-01-17 15:14:33 +08006174 if (vmx->vpid != 0)
6175 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6176
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006177 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006178 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006179 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006180 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006181 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006182
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006183 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006184
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006185 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006186 if (init_event)
6187 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006188}
6189
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006190/*
6191 * In nested virtualization, check if L1 asked to exit on external interrupts.
6192 * For most existing hypervisors, this will always return true.
6193 */
6194static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6195{
6196 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6197 PIN_BASED_EXT_INTR_MASK;
6198}
6199
Bandan Das77b0f5d2014-04-19 18:17:45 -04006200/*
6201 * In nested virtualization, check if L1 has set
6202 * VM_EXIT_ACK_INTR_ON_EXIT
6203 */
6204static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6205{
6206 return get_vmcs12(vcpu)->vm_exit_controls &
6207 VM_EXIT_ACK_INTR_ON_EXIT;
6208}
6209
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006210static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6211{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006212 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006213}
6214
Jan Kiszkac9a79532014-03-07 20:03:15 +01006215static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006216{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006217 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6218 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006219}
6220
Jan Kiszkac9a79532014-03-07 20:03:15 +01006221static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006222{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006223 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006224 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006225 enable_irq_window(vcpu);
6226 return;
6227 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006228
Paolo Bonzini47c01522016-12-19 11:44:07 +01006229 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6230 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006231}
6232
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006233static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006234{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006235 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006236 uint32_t intr;
6237 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006238
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006239 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006240
Avi Kivityfa89a812008-09-01 15:57:51 +03006241 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006242 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006243 int inc_eip = 0;
6244 if (vcpu->arch.interrupt.soft)
6245 inc_eip = vcpu->arch.event_exit_inst_len;
6246 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006247 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006248 return;
6249 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006250 intr = irq | INTR_INFO_VALID_MASK;
6251 if (vcpu->arch.interrupt.soft) {
6252 intr |= INTR_TYPE_SOFT_INTR;
6253 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6254 vmx->vcpu.arch.event_exit_inst_len);
6255 } else
6256 intr |= INTR_TYPE_EXT_INTR;
6257 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006258
6259 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006260}
6261
Sheng Yangf08864b2008-05-15 18:23:25 +08006262static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6263{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006264 struct vcpu_vmx *vmx = to_vmx(vcpu);
6265
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006266 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006267 /*
6268 * Tracking the NMI-blocked state in software is built upon
6269 * finding the next open IRQ window. This, in turn, depends on
6270 * well-behaving guests: They have to keep IRQs disabled at
6271 * least as long as the NMI handler runs. Otherwise we may
6272 * cause NMI nesting, maybe breaking the guest. But as this is
6273 * highly unlikely, we can live with the residual risk.
6274 */
6275 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6276 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6277 }
6278
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006279 ++vcpu->stat.nmi_injections;
6280 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006281
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006282 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006283 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006284 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006285 return;
6286 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006287
Sheng Yangf08864b2008-05-15 18:23:25 +08006288 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6289 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006290
6291 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006292}
6293
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006294static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6295{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006296 struct vcpu_vmx *vmx = to_vmx(vcpu);
6297 bool masked;
6298
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006299 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006300 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006301 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006302 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006303 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6304 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6305 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006306}
6307
6308static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6309{
6310 struct vcpu_vmx *vmx = to_vmx(vcpu);
6311
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006312 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006313 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6314 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6315 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6316 }
6317 } else {
6318 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6319 if (masked)
6320 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6321 GUEST_INTR_STATE_NMI);
6322 else
6323 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6324 GUEST_INTR_STATE_NMI);
6325 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006326}
6327
Jan Kiszka2505dc92013-04-14 12:12:47 +02006328static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6329{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006330 if (to_vmx(vcpu)->nested.nested_run_pending)
6331 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006332
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006333 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006334 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6335 return 0;
6336
Jan Kiszka2505dc92013-04-14 12:12:47 +02006337 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6338 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6339 | GUEST_INTR_STATE_NMI));
6340}
6341
Gleb Natapov78646122009-03-23 12:12:11 +02006342static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6343{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006344 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6345 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006346 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6347 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006348}
6349
Izik Eiduscbc94022007-10-25 00:29:55 +02006350static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6351{
6352 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006353
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006354 if (enable_unrestricted_guest)
6355 return 0;
6356
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006357 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6358 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006359 if (ret)
6360 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006361 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006362 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006363}
6364
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006365static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6366{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006367 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006368 return 0;
6369}
6370
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006371static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006372{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006373 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006374 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006375 /*
6376 * Update instruction length as we may reinject the exception
6377 * from user space while in guest debugging mode.
6378 */
6379 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6380 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006381 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006382 return false;
6383 /* fall through */
6384 case DB_VECTOR:
6385 if (vcpu->guest_debug &
6386 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6387 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006388 /* fall through */
6389 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006390 case OF_VECTOR:
6391 case BR_VECTOR:
6392 case UD_VECTOR:
6393 case DF_VECTOR:
6394 case SS_VECTOR:
6395 case GP_VECTOR:
6396 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006397 return true;
6398 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006399 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006400 return false;
6401}
6402
6403static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6404 int vec, u32 err_code)
6405{
6406 /*
6407 * Instruction with address size override prefix opcode 0x67
6408 * Cause the #SS fault with 0 error code in VM86 mode.
6409 */
6410 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6411 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6412 if (vcpu->arch.halt_request) {
6413 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006414 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006415 }
6416 return 1;
6417 }
6418 return 0;
6419 }
6420
6421 /*
6422 * Forward all other exceptions that are valid in real mode.
6423 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6424 * the required debugging infrastructure rework.
6425 */
6426 kvm_queue_exception(vcpu, vec);
6427 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006428}
6429
Andi Kleena0861c02009-06-08 17:37:09 +08006430/*
6431 * Trigger machine check on the host. We assume all the MSRs are already set up
6432 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6433 * We pass a fake environment to the machine check handler because we want
6434 * the guest to be always treated like user space, no matter what context
6435 * it used internally.
6436 */
6437static void kvm_machine_check(void)
6438{
6439#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6440 struct pt_regs regs = {
6441 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6442 .flags = X86_EFLAGS_IF,
6443 };
6444
6445 do_machine_check(&regs, 0);
6446#endif
6447}
6448
Avi Kivity851ba692009-08-24 11:10:17 +03006449static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006450{
6451 /* already handled by vcpu_run */
6452 return 1;
6453}
6454
Avi Kivity851ba692009-08-24 11:10:17 +03006455static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006456{
Avi Kivity1155f762007-11-22 11:30:47 +02006457 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006458 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006459 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006460 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006461 u32 vect_info;
6462 enum emulation_result er;
6463
Avi Kivity1155f762007-11-22 11:30:47 +02006464 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006465 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006466
Andi Kleena0861c02009-06-08 17:37:09 +08006467 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006468 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006469
Jim Mattsonef85b672016-12-12 11:01:37 -08006470 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006471 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006472
Wanpeng Li082d06e2018-04-03 16:28:48 -07006473 if (is_invalid_opcode(intr_info))
6474 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006475
Avi Kivity6aa8b732006-12-10 02:21:36 -08006476 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006477 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006478 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006479
Liran Alon9e869482018-03-12 13:12:51 +02006480 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6481 WARN_ON_ONCE(!enable_vmware_backdoor);
6482 er = emulate_instruction(vcpu,
6483 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6484 if (er == EMULATE_USER_EXIT)
6485 return 0;
6486 else if (er != EMULATE_DONE)
6487 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6488 return 1;
6489 }
6490
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006491 /*
6492 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6493 * MMIO, it is better to report an internal error.
6494 * See the comments in vmx_handle_exit.
6495 */
6496 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6497 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6498 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6499 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006500 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006501 vcpu->run->internal.data[0] = vect_info;
6502 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006503 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006504 return 0;
6505 }
6506
Avi Kivity6aa8b732006-12-10 02:21:36 -08006507 if (is_page_fault(intr_info)) {
6508 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006509 /* EPT won't cause page fault directly */
6510 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006511 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006512 }
6513
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006514 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006515
6516 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6517 return handle_rmode_exception(vcpu, ex_no, error_code);
6518
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006519 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006520 case AC_VECTOR:
6521 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6522 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006523 case DB_VECTOR:
6524 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6525 if (!(vcpu->guest_debug &
6526 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006527 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006528 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006529 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006530 skip_emulated_instruction(vcpu);
6531
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006532 kvm_queue_exception(vcpu, DB_VECTOR);
6533 return 1;
6534 }
6535 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6536 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6537 /* fall through */
6538 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006539 /*
6540 * Update instruction length as we may reinject #BP from
6541 * user space while in guest debugging mode. Reading it for
6542 * #DB as well causes no harm, it is not used in that case.
6543 */
6544 vmx->vcpu.arch.event_exit_inst_len =
6545 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006546 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006547 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006548 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6549 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006550 break;
6551 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006552 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6553 kvm_run->ex.exception = ex_no;
6554 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006555 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006556 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006557 return 0;
6558}
6559
Avi Kivity851ba692009-08-24 11:10:17 +03006560static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006561{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006562 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006563 return 1;
6564}
6565
Avi Kivity851ba692009-08-24 11:10:17 +03006566static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006567{
Avi Kivity851ba692009-08-24 11:10:17 +03006568 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006569 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006570 return 0;
6571}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006572
Avi Kivity851ba692009-08-24 11:10:17 +03006573static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006574{
He, Qingbfdaab02007-09-12 14:18:28 +08006575 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006576 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006577 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006578
He, Qingbfdaab02007-09-12 14:18:28 +08006579 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006580 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006581
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006582 ++vcpu->stat.io_exits;
6583
Sean Christopherson432baf62018-03-08 08:57:26 -08006584 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006585 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006586
6587 port = exit_qualification >> 16;
6588 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006589 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006590
Sean Christophersondca7f122018-03-08 08:57:27 -08006591 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006592}
6593
Ingo Molnar102d8322007-02-19 14:37:47 +02006594static void
6595vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6596{
6597 /*
6598 * Patch in the VMCALL instruction:
6599 */
6600 hypercall[0] = 0x0f;
6601 hypercall[1] = 0x01;
6602 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006603}
6604
Guo Chao0fa06072012-06-28 15:16:19 +08006605/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006606static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6607{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006608 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006609 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6610 unsigned long orig_val = val;
6611
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006612 /*
6613 * We get here when L2 changed cr0 in a way that did not change
6614 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006615 * but did change L0 shadowed bits. So we first calculate the
6616 * effective cr0 value that L1 would like to write into the
6617 * hardware. It consists of the L2-owned bits from the new
6618 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006619 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006620 val = (val & ~vmcs12->cr0_guest_host_mask) |
6621 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6622
David Matlack38991522016-11-29 18:14:08 -08006623 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006624 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006625
6626 if (kvm_set_cr0(vcpu, val))
6627 return 1;
6628 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006629 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006630 } else {
6631 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006632 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006633 return 1;
David Matlack38991522016-11-29 18:14:08 -08006634
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006635 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006636 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006637}
6638
6639static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6640{
6641 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006642 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6643 unsigned long orig_val = val;
6644
6645 /* analogously to handle_set_cr0 */
6646 val = (val & ~vmcs12->cr4_guest_host_mask) |
6647 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6648 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006649 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006650 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006651 return 0;
6652 } else
6653 return kvm_set_cr4(vcpu, val);
6654}
6655
Paolo Bonzini0367f202016-07-12 10:44:55 +02006656static int handle_desc(struct kvm_vcpu *vcpu)
6657{
6658 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6659 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6660}
6661
Avi Kivity851ba692009-08-24 11:10:17 +03006662static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006663{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006664 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006665 int cr;
6666 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006667 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006668 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006669
He, Qingbfdaab02007-09-12 14:18:28 +08006670 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006671 cr = exit_qualification & 15;
6672 reg = (exit_qualification >> 8) & 15;
6673 switch ((exit_qualification >> 4) & 3) {
6674 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006675 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006676 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006677 switch (cr) {
6678 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006679 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006680 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006681 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006682 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006683 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006684 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006685 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006686 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006687 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006688 case 8: {
6689 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006690 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006691 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006692 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006693 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006694 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006695 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006696 return ret;
6697 /*
6698 * TODO: we might be squashing a
6699 * KVM_GUESTDBG_SINGLESTEP-triggered
6700 * KVM_EXIT_DEBUG here.
6701 */
Avi Kivity851ba692009-08-24 11:10:17 +03006702 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006703 return 0;
6704 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006705 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006706 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006707 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006708 WARN_ONCE(1, "Guest should always own CR0.TS");
6709 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006710 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006711 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006712 case 1: /*mov from cr*/
6713 switch (cr) {
6714 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006715 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006716 val = kvm_read_cr3(vcpu);
6717 kvm_register_write(vcpu, reg, val);
6718 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006719 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006720 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006721 val = kvm_get_cr8(vcpu);
6722 kvm_register_write(vcpu, reg, val);
6723 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006724 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006725 }
6726 break;
6727 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006728 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006729 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006730 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006731
Kyle Huey6affcbe2016-11-29 12:40:40 -08006732 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006733 default:
6734 break;
6735 }
Avi Kivity851ba692009-08-24 11:10:17 +03006736 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006737 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006738 (int)(exit_qualification >> 4) & 3, cr);
6739 return 0;
6740}
6741
Avi Kivity851ba692009-08-24 11:10:17 +03006742static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006743{
He, Qingbfdaab02007-09-12 14:18:28 +08006744 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006745 int dr, dr7, reg;
6746
6747 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6748 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6749
6750 /* First, if DR does not exist, trigger UD */
6751 if (!kvm_require_dr(vcpu, dr))
6752 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006753
Jan Kiszkaf2483412010-01-20 18:20:20 +01006754 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006755 if (!kvm_require_cpl(vcpu, 0))
6756 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006757 dr7 = vmcs_readl(GUEST_DR7);
6758 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006759 /*
6760 * As the vm-exit takes precedence over the debug trap, we
6761 * need to emulate the latter, either for the host or the
6762 * guest debugging itself.
6763 */
6764 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006765 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006766 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006767 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006768 vcpu->run->debug.arch.exception = DB_VECTOR;
6769 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006770 return 0;
6771 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006772 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006773 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006774 kvm_queue_exception(vcpu, DB_VECTOR);
6775 return 1;
6776 }
6777 }
6778
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006779 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006780 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6781 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006782
6783 /*
6784 * No more DR vmexits; force a reload of the debug registers
6785 * and reenter on this instruction. The next vmexit will
6786 * retrieve the full state of the debug registers.
6787 */
6788 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6789 return 1;
6790 }
6791
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006792 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6793 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006794 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006795
6796 if (kvm_get_dr(vcpu, dr, &val))
6797 return 1;
6798 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006799 } else
Nadav Amit57773922014-06-18 17:19:23 +03006800 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006801 return 1;
6802
Kyle Huey6affcbe2016-11-29 12:40:40 -08006803 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006804}
6805
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006806static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6807{
6808 return vcpu->arch.dr6;
6809}
6810
6811static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6812{
6813}
6814
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006815static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6816{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006817 get_debugreg(vcpu->arch.db[0], 0);
6818 get_debugreg(vcpu->arch.db[1], 1);
6819 get_debugreg(vcpu->arch.db[2], 2);
6820 get_debugreg(vcpu->arch.db[3], 3);
6821 get_debugreg(vcpu->arch.dr6, 6);
6822 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6823
6824 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006825 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006826}
6827
Gleb Natapov020df072010-04-13 10:05:23 +03006828static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6829{
6830 vmcs_writel(GUEST_DR7, val);
6831}
6832
Avi Kivity851ba692009-08-24 11:10:17 +03006833static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006834{
Kyle Huey6a908b62016-11-29 12:40:37 -08006835 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006836}
6837
Avi Kivity851ba692009-08-24 11:10:17 +03006838static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006839{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006840 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006841 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006842
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006843 msr_info.index = ecx;
6844 msr_info.host_initiated = false;
6845 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006846 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006847 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006848 return 1;
6849 }
6850
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006851 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006852
Avi Kivity6aa8b732006-12-10 02:21:36 -08006853 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006854 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6855 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006856 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006857}
6858
Avi Kivity851ba692009-08-24 11:10:17 +03006859static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006860{
Will Auld8fe8ab42012-11-29 12:42:12 -08006861 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006862 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6863 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6864 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006865
Will Auld8fe8ab42012-11-29 12:42:12 -08006866 msr.data = data;
6867 msr.index = ecx;
6868 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006869 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006870 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006871 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006872 return 1;
6873 }
6874
Avi Kivity59200272010-01-25 19:47:02 +02006875 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006876 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006877}
6878
Avi Kivity851ba692009-08-24 11:10:17 +03006879static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006880{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006881 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006882 return 1;
6883}
6884
Avi Kivity851ba692009-08-24 11:10:17 +03006885static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006886{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006887 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6888 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006889
Avi Kivity3842d132010-07-27 12:30:24 +03006890 kvm_make_request(KVM_REQ_EVENT, vcpu);
6891
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006892 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893 return 1;
6894}
6895
Avi Kivity851ba692009-08-24 11:10:17 +03006896static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006897{
Avi Kivityd3bef152007-06-05 15:53:05 +03006898 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006899}
6900
Avi Kivity851ba692009-08-24 11:10:17 +03006901static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006902{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006903 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006904}
6905
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006906static int handle_invd(struct kvm_vcpu *vcpu)
6907{
Andre Przywara51d8b662010-12-21 11:12:02 +01006908 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006909}
6910
Avi Kivity851ba692009-08-24 11:10:17 +03006911static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006912{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006913 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006914
6915 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006916 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006917}
6918
Avi Kivityfee84b02011-11-10 14:57:25 +02006919static int handle_rdpmc(struct kvm_vcpu *vcpu)
6920{
6921 int err;
6922
6923 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006924 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006925}
6926
Avi Kivity851ba692009-08-24 11:10:17 +03006927static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006928{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006929 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006930}
6931
Dexuan Cui2acf9232010-06-10 11:27:12 +08006932static int handle_xsetbv(struct kvm_vcpu *vcpu)
6933{
6934 u64 new_bv = kvm_read_edx_eax(vcpu);
6935 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6936
6937 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006938 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006939 return 1;
6940}
6941
Wanpeng Lif53cd632014-12-02 19:14:58 +08006942static int handle_xsaves(struct kvm_vcpu *vcpu)
6943{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006944 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006945 WARN(1, "this should never happen\n");
6946 return 1;
6947}
6948
6949static int handle_xrstors(struct kvm_vcpu *vcpu)
6950{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006951 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006952 WARN(1, "this should never happen\n");
6953 return 1;
6954}
6955
Avi Kivity851ba692009-08-24 11:10:17 +03006956static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006957{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006958 if (likely(fasteoi)) {
6959 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6960 int access_type, offset;
6961
6962 access_type = exit_qualification & APIC_ACCESS_TYPE;
6963 offset = exit_qualification & APIC_ACCESS_OFFSET;
6964 /*
6965 * Sane guest uses MOV to write EOI, with written value
6966 * not cared. So make a short-circuit here by avoiding
6967 * heavy instruction emulation.
6968 */
6969 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6970 (offset == APIC_EOI)) {
6971 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006972 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006973 }
6974 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006975 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006976}
6977
Yang Zhangc7c9c562013-01-25 10:18:51 +08006978static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6979{
6980 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6981 int vector = exit_qualification & 0xff;
6982
6983 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6984 kvm_apic_set_eoi_accelerated(vcpu, vector);
6985 return 1;
6986}
6987
Yang Zhang83d4c282013-01-25 10:18:49 +08006988static int handle_apic_write(struct kvm_vcpu *vcpu)
6989{
6990 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6991 u32 offset = exit_qualification & 0xfff;
6992
6993 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6994 kvm_apic_write_nodecode(vcpu, offset);
6995 return 1;
6996}
6997
Avi Kivity851ba692009-08-24 11:10:17 +03006998static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006999{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007000 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007001 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007002 bool has_error_code = false;
7003 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007004 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007005 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007006
7007 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007008 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007009 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007010
7011 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7012
7013 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007014 if (reason == TASK_SWITCH_GATE && idt_v) {
7015 switch (type) {
7016 case INTR_TYPE_NMI_INTR:
7017 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007018 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007019 break;
7020 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007021 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007022 kvm_clear_interrupt_queue(vcpu);
7023 break;
7024 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007025 if (vmx->idt_vectoring_info &
7026 VECTORING_INFO_DELIVER_CODE_MASK) {
7027 has_error_code = true;
7028 error_code =
7029 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7030 }
7031 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007032 case INTR_TYPE_SOFT_EXCEPTION:
7033 kvm_clear_exception_queue(vcpu);
7034 break;
7035 default:
7036 break;
7037 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007038 }
Izik Eidus37817f22008-03-24 23:14:53 +02007039 tss_selector = exit_qualification;
7040
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007041 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7042 type != INTR_TYPE_EXT_INTR &&
7043 type != INTR_TYPE_NMI_INTR))
7044 skip_emulated_instruction(vcpu);
7045
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007046 if (kvm_task_switch(vcpu, tss_selector,
7047 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7048 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007049 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7050 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7051 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007052 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007053 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007054
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007055 /*
7056 * TODO: What about debug traps on tss switch?
7057 * Are we supposed to inject them and update dr6?
7058 */
7059
7060 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007061}
7062
Avi Kivity851ba692009-08-24 11:10:17 +03007063static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007064{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007065 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007066 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007067 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007068
Sheng Yangf9c617f2009-03-25 10:08:52 +08007069 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007070
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007071 /*
7072 * EPT violation happened while executing iret from NMI,
7073 * "blocked by NMI" bit has to be set before next VM entry.
7074 * There are errata that may cause this bit to not be set:
7075 * AAK134, BY25.
7076 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007077 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007078 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007079 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007080 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7081
Sheng Yang14394422008-04-28 12:24:45 +08007082 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007083 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007084
Junaid Shahid27959a42016-12-06 16:46:10 -08007085 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007086 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007087 ? PFERR_USER_MASK : 0;
7088 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007089 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007090 ? PFERR_WRITE_MASK : 0;
7091 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007092 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007093 ? PFERR_FETCH_MASK : 0;
7094 /* ept page table entry is present? */
7095 error_code |= (exit_qualification &
7096 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7097 EPT_VIOLATION_EXECUTABLE))
7098 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007099
Paolo Bonzinieebed242016-11-28 14:39:58 +01007100 error_code |= (exit_qualification & 0x100) != 0 ?
7101 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007102
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007103 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007104 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007105}
7106
Avi Kivity851ba692009-08-24 11:10:17 +03007107static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007108{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007109 gpa_t gpa;
7110
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007111 /*
7112 * A nested guest cannot optimize MMIO vmexits, because we have an
7113 * nGPA here instead of the required GPA.
7114 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007115 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007116 if (!is_guest_mode(vcpu) &&
7117 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007118 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007119 /*
7120 * Doing kvm_skip_emulated_instruction() depends on undefined
7121 * behavior: Intel's manual doesn't mandate
7122 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7123 * occurs and while on real hardware it was observed to be set,
7124 * other hypervisors (namely Hyper-V) don't set it, we end up
7125 * advancing IP with some random value. Disable fast mmio when
7126 * running nested and keep it for real hardware in hope that
7127 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7128 */
7129 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7130 return kvm_skip_emulated_instruction(vcpu);
7131 else
7132 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7133 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007134 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007135
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007136 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007137}
7138
Avi Kivity851ba692009-08-24 11:10:17 +03007139static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007140{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007141 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007142 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7143 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007144 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007145 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007146
7147 return 1;
7148}
7149
Mohammed Gamal80ced182009-09-01 12:48:18 +02007150static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007151{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007152 struct vcpu_vmx *vmx = to_vmx(vcpu);
7153 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007154 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007155 u32 cpu_exec_ctrl;
7156 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007157 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007158
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007159 /*
7160 * We should never reach the point where we are emulating L2
7161 * due to invalid guest state as that means we incorrectly
7162 * allowed a nested VMEntry with an invalid vmcs12.
7163 */
7164 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7165
Avi Kivity49e9d552010-09-19 14:34:08 +02007166 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7167 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007168
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007169 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007170 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007171 return handle_interrupt_window(&vmx->vcpu);
7172
Radim Krčmář72875d82017-04-26 22:32:19 +02007173 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007174 return 1;
7175
Liran Alon9b8ae632017-11-05 16:56:34 +02007176 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007177
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007178 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007179 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007180 ret = 0;
7181 goto out;
7182 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007183
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007184 if (err != EMULATE_DONE)
7185 goto emulation_error;
7186
7187 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7188 vcpu->arch.exception.pending)
7189 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007190
Gleb Natapov8d76c492013-05-08 18:38:44 +03007191 if (vcpu->arch.halt_request) {
7192 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007193 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007194 goto out;
7195 }
7196
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007197 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007198 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007199 if (need_resched())
7200 schedule();
7201 }
7202
Mohammed Gamal80ced182009-09-01 12:48:18 +02007203out:
7204 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007205
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007206emulation_error:
7207 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7208 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7209 vcpu->run->internal.ndata = 0;
7210 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007211}
7212
7213static void grow_ple_window(struct kvm_vcpu *vcpu)
7214{
7215 struct vcpu_vmx *vmx = to_vmx(vcpu);
7216 int old = vmx->ple_window;
7217
Babu Mogerc8e88712018-03-16 16:37:24 -04007218 vmx->ple_window = __grow_ple_window(old, ple_window,
7219 ple_window_grow,
7220 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007221
7222 if (vmx->ple_window != old)
7223 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007224
7225 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007226}
7227
7228static void shrink_ple_window(struct kvm_vcpu *vcpu)
7229{
7230 struct vcpu_vmx *vmx = to_vmx(vcpu);
7231 int old = vmx->ple_window;
7232
Babu Mogerc8e88712018-03-16 16:37:24 -04007233 vmx->ple_window = __shrink_ple_window(old, ple_window,
7234 ple_window_shrink,
7235 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007236
7237 if (vmx->ple_window != old)
7238 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007239
7240 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007241}
7242
7243/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007244 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7245 */
7246static void wakeup_handler(void)
7247{
7248 struct kvm_vcpu *vcpu;
7249 int cpu = smp_processor_id();
7250
7251 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7252 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7253 blocked_vcpu_list) {
7254 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7255
7256 if (pi_test_on(pi_desc) == 1)
7257 kvm_vcpu_kick(vcpu);
7258 }
7259 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7260}
7261
Peng Haoe01bca22018-04-07 05:47:32 +08007262static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007263{
7264 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7265 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7266 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7267 0ull, VMX_EPT_EXECUTABLE_MASK,
7268 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007269 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007270
7271 ept_set_mmio_spte_mask();
7272 kvm_enable_tdp();
7273}
7274
Tiejun Chenf2c76482014-10-28 10:14:47 +08007275static __init int hardware_setup(void)
7276{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007277 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007278
7279 rdmsrl_safe(MSR_EFER, &host_efer);
7280
7281 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7282 kvm_define_shared_msr(i, vmx_msr_index[i]);
7283
Radim Krčmář23611332016-09-29 22:41:33 +02007284 for (i = 0; i < VMX_BITMAP_NR; i++) {
7285 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7286 if (!vmx_bitmap[i])
7287 goto out;
7288 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007289
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007290 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7291 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7292
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007293 if (setup_vmcs_config(&vmcs_config) < 0) {
7294 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007295 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007296 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007297
7298 if (boot_cpu_has(X86_FEATURE_NX))
7299 kvm_enable_efer_bits(EFER_NX);
7300
Wanpeng Li08d839c2017-03-23 05:30:08 -07007301 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7302 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007303 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007304
Tiejun Chenf2c76482014-10-28 10:14:47 +08007305 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007306 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007307 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007308 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007309 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007310
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007311 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007312 enable_ept_ad_bits = 0;
7313
Wanpeng Li8ad81822017-10-09 15:51:53 -07007314 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007315 enable_unrestricted_guest = 0;
7316
Paolo Bonziniad15a292015-01-30 16:18:49 +01007317 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007318 flexpriority_enabled = 0;
7319
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007320 if (!cpu_has_virtual_nmis())
7321 enable_vnmi = 0;
7322
Paolo Bonziniad15a292015-01-30 16:18:49 +01007323 /*
7324 * set_apic_access_page_addr() is used to reload apic access
7325 * page upon invalidation. No need to do anything if not
7326 * using the APIC_ACCESS_ADDR VMCS field.
7327 */
7328 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007329 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007330
7331 if (!cpu_has_vmx_tpr_shadow())
7332 kvm_x86_ops->update_cr8_intercept = NULL;
7333
7334 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7335 kvm_disable_largepages();
7336
Wanpeng Li0f107682017-09-28 18:06:24 -07007337 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007338 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007339 ple_window = 0;
7340 ple_window_grow = 0;
7341 ple_window_max = 0;
7342 ple_window_shrink = 0;
7343 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007344
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007345 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007346 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007347 kvm_x86_ops->sync_pir_to_irr = NULL;
7348 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007349
Haozhong Zhang64903d62015-10-20 15:39:09 +08007350 if (cpu_has_vmx_tsc_scaling()) {
7351 kvm_has_tsc_control = true;
7352 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7353 kvm_tsc_scaling_ratio_frac_bits = 48;
7354 }
7355
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007356 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7357
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007358 if (enable_ept)
7359 vmx_enable_tdp();
7360 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007361 kvm_disable_tdp();
7362
Kai Huang843e4332015-01-28 10:54:28 +08007363 /*
7364 * Only enable PML when hardware supports PML feature, and both EPT
7365 * and EPT A/D bit features are enabled -- PML depends on them to work.
7366 */
7367 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7368 enable_pml = 0;
7369
7370 if (!enable_pml) {
7371 kvm_x86_ops->slot_enable_log_dirty = NULL;
7372 kvm_x86_ops->slot_disable_log_dirty = NULL;
7373 kvm_x86_ops->flush_log_dirty = NULL;
7374 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7375 }
7376
Yunhong Jiang64672c92016-06-13 14:19:59 -07007377 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7378 u64 vmx_msr;
7379
7380 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7381 cpu_preemption_timer_multi =
7382 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7383 } else {
7384 kvm_x86_ops->set_hv_timer = NULL;
7385 kvm_x86_ops->cancel_hv_timer = NULL;
7386 }
7387
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007388 if (!cpu_has_vmx_shadow_vmcs())
7389 enable_shadow_vmcs = 0;
7390 if (enable_shadow_vmcs)
7391 init_vmcs_shadow_fields();
7392
Feng Wubf9f6ac2015-09-18 22:29:55 +08007393 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007394 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007395
Ashok Rajc45dcc72016-06-22 14:59:56 +08007396 kvm_mce_cap_supported |= MCG_LMCE_P;
7397
Tiejun Chenf2c76482014-10-28 10:14:47 +08007398 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007399
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007400out:
Radim Krčmář23611332016-09-29 22:41:33 +02007401 for (i = 0; i < VMX_BITMAP_NR; i++)
7402 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007403
7404 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007405}
7406
7407static __exit void hardware_unsetup(void)
7408{
Radim Krčmář23611332016-09-29 22:41:33 +02007409 int i;
7410
7411 for (i = 0; i < VMX_BITMAP_NR; i++)
7412 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007413
Tiejun Chenf2c76482014-10-28 10:14:47 +08007414 free_kvm_area();
7415}
7416
Avi Kivity6aa8b732006-12-10 02:21:36 -08007417/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007418 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7419 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7420 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007421static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007422{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007423 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007424 grow_ple_window(vcpu);
7425
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007426 /*
7427 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7428 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7429 * never set PAUSE_EXITING and just set PLE if supported,
7430 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7431 */
7432 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007433 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007434}
7435
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007436static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007437{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007438 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007439}
7440
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007441static int handle_mwait(struct kvm_vcpu *vcpu)
7442{
7443 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7444 return handle_nop(vcpu);
7445}
7446
Jim Mattson45ec3682017-08-23 16:32:04 -07007447static int handle_invalid_op(struct kvm_vcpu *vcpu)
7448{
7449 kvm_queue_exception(vcpu, UD_VECTOR);
7450 return 1;
7451}
7452
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007453static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7454{
7455 return 1;
7456}
7457
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007458static int handle_monitor(struct kvm_vcpu *vcpu)
7459{
7460 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7461 return handle_nop(vcpu);
7462}
7463
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007464/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007465 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7466 * set the success or error code of an emulated VMX instruction, as specified
7467 * by Vol 2B, VMX Instruction Reference, "Conventions".
7468 */
7469static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7470{
7471 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7472 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7473 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7474}
7475
7476static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7477{
7478 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7479 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7480 X86_EFLAGS_SF | X86_EFLAGS_OF))
7481 | X86_EFLAGS_CF);
7482}
7483
Abel Gordon145c28d2013-04-18 14:36:55 +03007484static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007485 u32 vm_instruction_error)
7486{
7487 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7488 /*
7489 * failValid writes the error number to the current VMCS, which
7490 * can't be done there isn't a current VMCS.
7491 */
7492 nested_vmx_failInvalid(vcpu);
7493 return;
7494 }
7495 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7496 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7497 X86_EFLAGS_SF | X86_EFLAGS_OF))
7498 | X86_EFLAGS_ZF);
7499 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7500 /*
7501 * We don't need to force a shadow sync because
7502 * VM_INSTRUCTION_ERROR is not shadowed
7503 */
7504}
Abel Gordon145c28d2013-04-18 14:36:55 +03007505
Wincy Vanff651cb2014-12-11 08:52:58 +03007506static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7507{
7508 /* TODO: not to reset guest simply here. */
7509 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007510 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007511}
7512
Jan Kiszkaf41245002014-03-07 20:03:13 +01007513static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7514{
7515 struct vcpu_vmx *vmx =
7516 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7517
7518 vmx->nested.preemption_timer_expired = true;
7519 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7520 kvm_vcpu_kick(&vmx->vcpu);
7521
7522 return HRTIMER_NORESTART;
7523}
7524
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007525/*
Bandan Das19677e32014-05-06 02:19:15 -04007526 * Decode the memory-address operand of a vmx instruction, as recorded on an
7527 * exit caused by such an instruction (run by a guest hypervisor).
7528 * On success, returns 0. When the operand is invalid, returns 1 and throws
7529 * #UD or #GP.
7530 */
7531static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7532 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007533 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007534{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007535 gva_t off;
7536 bool exn;
7537 struct kvm_segment s;
7538
Bandan Das19677e32014-05-06 02:19:15 -04007539 /*
7540 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7541 * Execution", on an exit, vmx_instruction_info holds most of the
7542 * addressing components of the operand. Only the displacement part
7543 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7544 * For how an actual address is calculated from all these components,
7545 * refer to Vol. 1, "Operand Addressing".
7546 */
7547 int scaling = vmx_instruction_info & 3;
7548 int addr_size = (vmx_instruction_info >> 7) & 7;
7549 bool is_reg = vmx_instruction_info & (1u << 10);
7550 int seg_reg = (vmx_instruction_info >> 15) & 7;
7551 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7552 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7553 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7554 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7555
7556 if (is_reg) {
7557 kvm_queue_exception(vcpu, UD_VECTOR);
7558 return 1;
7559 }
7560
7561 /* Addr = segment_base + offset */
7562 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007563 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007564 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007565 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007566 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007567 off += kvm_register_read(vcpu, index_reg)<<scaling;
7568 vmx_get_segment(vcpu, &s, seg_reg);
7569 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007570
7571 if (addr_size == 1) /* 32 bit */
7572 *ret &= 0xffffffff;
7573
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007574 /* Checks for #GP/#SS exceptions. */
7575 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007576 if (is_long_mode(vcpu)) {
7577 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7578 * non-canonical form. This is the only check on the memory
7579 * destination for long mode!
7580 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007581 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007582 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007583 /* Protected mode: apply checks for segment validity in the
7584 * following order:
7585 * - segment type check (#GP(0) may be thrown)
7586 * - usability check (#GP(0)/#SS(0))
7587 * - limit check (#GP(0)/#SS(0))
7588 */
7589 if (wr)
7590 /* #GP(0) if the destination operand is located in a
7591 * read-only data segment or any code segment.
7592 */
7593 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7594 else
7595 /* #GP(0) if the source operand is located in an
7596 * execute-only code segment
7597 */
7598 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007599 if (exn) {
7600 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7601 return 1;
7602 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007603 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7604 */
7605 exn = (s.unusable != 0);
7606 /* Protected mode: #GP(0)/#SS(0) if the memory
7607 * operand is outside the segment limit.
7608 */
7609 exn = exn || (off + sizeof(u64) > s.limit);
7610 }
7611 if (exn) {
7612 kvm_queue_exception_e(vcpu,
7613 seg_reg == VCPU_SREG_SS ?
7614 SS_VECTOR : GP_VECTOR,
7615 0);
7616 return 1;
7617 }
7618
Bandan Das19677e32014-05-06 02:19:15 -04007619 return 0;
7620}
7621
Radim Krčmářcbf71272017-05-19 15:48:51 +02007622static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007623{
7624 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007625 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007626
7627 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007628 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007629 return 1;
7630
Radim Krčmářcbf71272017-05-19 15:48:51 +02007631 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7632 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007633 kvm_inject_page_fault(vcpu, &e);
7634 return 1;
7635 }
7636
Bandan Das3573e222014-05-06 02:19:16 -04007637 return 0;
7638}
7639
Jim Mattsone29acc52016-11-30 12:03:43 -08007640static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7641{
7642 struct vcpu_vmx *vmx = to_vmx(vcpu);
7643 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007644 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007645
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007646 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7647 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007648 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007649
7650 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7651 if (!vmx->nested.cached_vmcs12)
7652 goto out_cached_vmcs12;
7653
7654 if (enable_shadow_vmcs) {
7655 shadow_vmcs = alloc_vmcs();
7656 if (!shadow_vmcs)
7657 goto out_shadow_vmcs;
7658 /* mark vmcs as shadow */
7659 shadow_vmcs->revision_id |= (1u << 31);
7660 /* init shadow vmcs */
7661 vmcs_clear(shadow_vmcs);
7662 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7663 }
7664
Jim Mattsone29acc52016-11-30 12:03:43 -08007665 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7666 HRTIMER_MODE_REL_PINNED);
7667 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7668
7669 vmx->nested.vmxon = true;
7670 return 0;
7671
7672out_shadow_vmcs:
7673 kfree(vmx->nested.cached_vmcs12);
7674
7675out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007676 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007677
Jim Mattsonde3a0022017-11-27 17:22:25 -06007678out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007679 return -ENOMEM;
7680}
7681
Bandan Das3573e222014-05-06 02:19:16 -04007682/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007683 * Emulate the VMXON instruction.
7684 * Currently, we just remember that VMX is active, and do not save or even
7685 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7686 * do not currently need to store anything in that guest-allocated memory
7687 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7688 * argument is different from the VMXON pointer (which the spec says they do).
7689 */
7690static int handle_vmon(struct kvm_vcpu *vcpu)
7691{
Jim Mattsone29acc52016-11-30 12:03:43 -08007692 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007693 gpa_t vmptr;
7694 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007695 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007696 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7697 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007698
Jim Mattson70f3aac2017-04-26 08:53:46 -07007699 /*
7700 * The Intel VMX Instruction Reference lists a bunch of bits that are
7701 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7702 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7703 * Otherwise, we should fail with #UD. But most faulting conditions
7704 * have already been checked by hardware, prior to the VM-exit for
7705 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7706 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007707 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007708 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007709 kvm_queue_exception(vcpu, UD_VECTOR);
7710 return 1;
7711 }
7712
Abel Gordon145c28d2013-04-18 14:36:55 +03007713 if (vmx->nested.vmxon) {
7714 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007715 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007716 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007717
Haozhong Zhang3b840802016-06-22 14:59:54 +08007718 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007719 != VMXON_NEEDED_FEATURES) {
7720 kvm_inject_gp(vcpu, 0);
7721 return 1;
7722 }
7723
Radim Krčmářcbf71272017-05-19 15:48:51 +02007724 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007725 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007726
7727 /*
7728 * SDM 3: 24.11.5
7729 * The first 4 bytes of VMXON region contain the supported
7730 * VMCS revision identifier
7731 *
7732 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7733 * which replaces physical address width with 32
7734 */
7735 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7736 nested_vmx_failInvalid(vcpu);
7737 return kvm_skip_emulated_instruction(vcpu);
7738 }
7739
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007740 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7741 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007742 nested_vmx_failInvalid(vcpu);
7743 return kvm_skip_emulated_instruction(vcpu);
7744 }
7745 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7746 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007747 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007748 nested_vmx_failInvalid(vcpu);
7749 return kvm_skip_emulated_instruction(vcpu);
7750 }
7751 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007752 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007753
7754 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007755 ret = enter_vmx_operation(vcpu);
7756 if (ret)
7757 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007758
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007759 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007760 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007761}
7762
7763/*
7764 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7765 * for running VMX instructions (except VMXON, whose prerequisites are
7766 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007767 * Note that many of these exceptions have priority over VM exits, so they
7768 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007769 */
7770static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7771{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007772 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007773 kvm_queue_exception(vcpu, UD_VECTOR);
7774 return 0;
7775 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007776 return 1;
7777}
7778
David Matlack8ca44e82017-08-01 14:00:39 -07007779static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7780{
7781 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7782 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7783}
7784
Abel Gordone7953d72013-04-18 14:37:55 +03007785static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7786{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007787 if (vmx->nested.current_vmptr == -1ull)
7788 return;
7789
Abel Gordon012f83c2013-04-18 14:39:25 +03007790 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007791 /* copy to memory all shadowed fields in case
7792 they were modified */
7793 copy_shadow_to_vmcs12(vmx);
7794 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007795 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007796 }
Wincy Van705699a2015-02-03 23:58:17 +08007797 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007798
7799 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007800 kvm_vcpu_write_guest_page(&vmx->vcpu,
7801 vmx->nested.current_vmptr >> PAGE_SHIFT,
7802 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007803
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007804 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007805}
7806
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007807/*
7808 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7809 * just stops using VMX.
7810 */
7811static void free_nested(struct vcpu_vmx *vmx)
7812{
Wanpeng Lib7455822017-11-22 14:04:00 -08007813 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007814 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007815
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007816 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007817 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007818 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007819 vmx->nested.posted_intr_nv = -1;
7820 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007821 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007822 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007823 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7824 free_vmcs(vmx->vmcs01.shadow_vmcs);
7825 vmx->vmcs01.shadow_vmcs = NULL;
7826 }
David Matlack4f2777b2016-07-13 17:16:37 -07007827 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007828 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007829 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007830 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007831 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007832 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007833 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007834 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007835 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007836 }
Wincy Van705699a2015-02-03 23:58:17 +08007837 if (vmx->nested.pi_desc_page) {
7838 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007839 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007840 vmx->nested.pi_desc_page = NULL;
7841 vmx->nested.pi_desc = NULL;
7842 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007843
Jim Mattsonde3a0022017-11-27 17:22:25 -06007844 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007845}
7846
7847/* Emulate the VMXOFF instruction */
7848static int handle_vmoff(struct kvm_vcpu *vcpu)
7849{
7850 if (!nested_vmx_check_permission(vcpu))
7851 return 1;
7852 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007853 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007854 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007855}
7856
Nadav Har'El27d6c862011-05-25 23:06:59 +03007857/* Emulate the VMCLEAR instruction */
7858static int handle_vmclear(struct kvm_vcpu *vcpu)
7859{
7860 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007861 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007862 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007863
7864 if (!nested_vmx_check_permission(vcpu))
7865 return 1;
7866
Radim Krčmářcbf71272017-05-19 15:48:51 +02007867 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007868 return 1;
7869
Radim Krčmářcbf71272017-05-19 15:48:51 +02007870 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7871 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7872 return kvm_skip_emulated_instruction(vcpu);
7873 }
7874
7875 if (vmptr == vmx->nested.vmxon_ptr) {
7876 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7877 return kvm_skip_emulated_instruction(vcpu);
7878 }
7879
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007880 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007881 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007882
Jim Mattson587d7e722017-03-02 12:41:48 -08007883 kvm_vcpu_write_guest(vcpu,
7884 vmptr + offsetof(struct vmcs12, launch_state),
7885 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007886
Nadav Har'El27d6c862011-05-25 23:06:59 +03007887 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007888 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007889}
7890
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007891static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7892
7893/* Emulate the VMLAUNCH instruction */
7894static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7895{
7896 return nested_vmx_run(vcpu, true);
7897}
7898
7899/* Emulate the VMRESUME instruction */
7900static int handle_vmresume(struct kvm_vcpu *vcpu)
7901{
7902
7903 return nested_vmx_run(vcpu, false);
7904}
7905
Nadav Har'El49f705c2011-05-25 23:08:30 +03007906/*
7907 * Read a vmcs12 field. Since these can have varying lengths and we return
7908 * one type, we chose the biggest type (u64) and zero-extend the return value
7909 * to that size. Note that the caller, handle_vmread, might need to use only
7910 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7911 * 64-bit fields are to be returned).
7912 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007913static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7914 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007915{
7916 short offset = vmcs_field_to_offset(field);
7917 char *p;
7918
7919 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007920 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007921
7922 p = ((char *)(get_vmcs12(vcpu))) + offset;
7923
Jim Mattsond37f4262017-12-22 12:12:16 -08007924 switch (vmcs_field_width(field)) {
7925 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007926 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007927 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007928 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007929 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007930 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007931 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007932 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007933 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007934 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007935 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007936 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007937 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007938 WARN_ON(1);
7939 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007940 }
7941}
7942
Abel Gordon20b97fe2013-04-18 14:36:25 +03007943
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007944static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7945 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007946 short offset = vmcs_field_to_offset(field);
7947 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7948 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007949 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007950
Jim Mattsond37f4262017-12-22 12:12:16 -08007951 switch (vmcs_field_width(field)) {
7952 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007953 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007954 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007955 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007956 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007957 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007958 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007959 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007960 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007961 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007962 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007963 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007964 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007965 WARN_ON(1);
7966 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007967 }
7968
7969}
7970
Abel Gordon16f5b902013-04-18 14:38:25 +03007971static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7972{
7973 int i;
7974 unsigned long field;
7975 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007976 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007977 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007978 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007979
Jan Kiszka282da872014-10-08 18:05:39 +02007980 preempt_disable();
7981
Abel Gordon16f5b902013-04-18 14:38:25 +03007982 vmcs_load(shadow_vmcs);
7983
7984 for (i = 0; i < num_fields; i++) {
7985 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007986 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007987 vmcs12_write_any(&vmx->vcpu, field, field_value);
7988 }
7989
7990 vmcs_clear(shadow_vmcs);
7991 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007992
7993 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007994}
7995
Abel Gordonc3114422013-04-18 14:38:55 +03007996static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7997{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007998 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007999 shadow_read_write_fields,
8000 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008001 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008002 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008003 max_shadow_read_write_fields,
8004 max_shadow_read_only_fields
8005 };
8006 int i, q;
8007 unsigned long field;
8008 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008009 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008010
8011 vmcs_load(shadow_vmcs);
8012
Mathias Krausec2bae892013-06-26 20:36:21 +02008013 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008014 for (i = 0; i < max_fields[q]; i++) {
8015 field = fields[q][i];
8016 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008017 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008018 }
8019 }
8020
8021 vmcs_clear(shadow_vmcs);
8022 vmcs_load(vmx->loaded_vmcs->vmcs);
8023}
8024
Nadav Har'El49f705c2011-05-25 23:08:30 +03008025/*
8026 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8027 * used before) all generate the same failure when it is missing.
8028 */
8029static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8030{
8031 struct vcpu_vmx *vmx = to_vmx(vcpu);
8032 if (vmx->nested.current_vmptr == -1ull) {
8033 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008034 return 0;
8035 }
8036 return 1;
8037}
8038
8039static int handle_vmread(struct kvm_vcpu *vcpu)
8040{
8041 unsigned long field;
8042 u64 field_value;
8043 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8044 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8045 gva_t gva = 0;
8046
Kyle Hueyeb277562016-11-29 12:40:39 -08008047 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008048 return 1;
8049
Kyle Huey6affcbe2016-11-29 12:40:40 -08008050 if (!nested_vmx_check_vmcs12(vcpu))
8051 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008052
Nadav Har'El49f705c2011-05-25 23:08:30 +03008053 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008054 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008055 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008056 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008057 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008058 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008059 }
8060 /*
8061 * Now copy part of this value to register or memory, as requested.
8062 * Note that the number of bits actually copied is 32 or 64 depending
8063 * on the guest's mode (32 or 64 bit), not on the given field's length.
8064 */
8065 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008066 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008067 field_value);
8068 } else {
8069 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008070 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008071 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008072 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008073 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8074 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8075 }
8076
8077 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008078 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008079}
8080
8081
8082static int handle_vmwrite(struct kvm_vcpu *vcpu)
8083{
8084 unsigned long field;
8085 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008086 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008087 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8088 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008089
Nadav Har'El49f705c2011-05-25 23:08:30 +03008090 /* The value to write might be 32 or 64 bits, depending on L1's long
8091 * mode, and eventually we need to write that into a field of several
8092 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008093 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008094 * bits into the vmcs12 field.
8095 */
8096 u64 field_value = 0;
8097 struct x86_exception e;
8098
Kyle Hueyeb277562016-11-29 12:40:39 -08008099 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008100 return 1;
8101
Kyle Huey6affcbe2016-11-29 12:40:40 -08008102 if (!nested_vmx_check_vmcs12(vcpu))
8103 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008104
Nadav Har'El49f705c2011-05-25 23:08:30 +03008105 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008106 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008107 (((vmx_instruction_info) >> 3) & 0xf));
8108 else {
8109 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008110 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008111 return 1;
8112 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008113 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008114 kvm_inject_page_fault(vcpu, &e);
8115 return 1;
8116 }
8117 }
8118
8119
Nadav Amit27e6fb52014-06-18 17:19:26 +03008120 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008121 if (vmcs_field_readonly(field)) {
8122 nested_vmx_failValid(vcpu,
8123 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008124 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008125 }
8126
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008127 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008128 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008129 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008130 }
8131
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008132 switch (field) {
8133#define SHADOW_FIELD_RW(x) case x:
8134#include "vmx_shadow_fields.h"
8135 /*
8136 * The fields that can be updated by L1 without a vmexit are
8137 * always updated in the vmcs02, the others go down the slow
8138 * path of prepare_vmcs02.
8139 */
8140 break;
8141 default:
8142 vmx->nested.dirty_vmcs12 = true;
8143 break;
8144 }
8145
Nadav Har'El49f705c2011-05-25 23:08:30 +03008146 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008147 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008148}
8149
Jim Mattsona8bc2842016-11-30 12:03:44 -08008150static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8151{
8152 vmx->nested.current_vmptr = vmptr;
8153 if (enable_shadow_vmcs) {
8154 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8155 SECONDARY_EXEC_SHADOW_VMCS);
8156 vmcs_write64(VMCS_LINK_POINTER,
8157 __pa(vmx->vmcs01.shadow_vmcs));
8158 vmx->nested.sync_shadow_vmcs = true;
8159 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008160 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008161}
8162
Nadav Har'El63846662011-05-25 23:07:29 +03008163/* Emulate the VMPTRLD instruction */
8164static int handle_vmptrld(struct kvm_vcpu *vcpu)
8165{
8166 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008167 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008168
8169 if (!nested_vmx_check_permission(vcpu))
8170 return 1;
8171
Radim Krčmářcbf71272017-05-19 15:48:51 +02008172 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008173 return 1;
8174
Radim Krčmářcbf71272017-05-19 15:48:51 +02008175 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8176 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8177 return kvm_skip_emulated_instruction(vcpu);
8178 }
8179
8180 if (vmptr == vmx->nested.vmxon_ptr) {
8181 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8182 return kvm_skip_emulated_instruction(vcpu);
8183 }
8184
Nadav Har'El63846662011-05-25 23:07:29 +03008185 if (vmx->nested.current_vmptr != vmptr) {
8186 struct vmcs12 *new_vmcs12;
8187 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008188 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8189 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008190 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008191 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008192 }
8193 new_vmcs12 = kmap(page);
8194 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8195 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008196 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008197 nested_vmx_failValid(vcpu,
8198 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008199 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008200 }
Nadav Har'El63846662011-05-25 23:07:29 +03008201
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008202 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008203 /*
8204 * Load VMCS12 from guest memory since it is not already
8205 * cached.
8206 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008207 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8208 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008209 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008210
Jim Mattsona8bc2842016-11-30 12:03:44 -08008211 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008212 }
8213
8214 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008215 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008216}
8217
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008218/* Emulate the VMPTRST instruction */
8219static int handle_vmptrst(struct kvm_vcpu *vcpu)
8220{
8221 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8222 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8223 gva_t vmcs_gva;
8224 struct x86_exception e;
8225
8226 if (!nested_vmx_check_permission(vcpu))
8227 return 1;
8228
8229 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008230 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008231 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008232 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008233 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8234 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8235 sizeof(u64), &e)) {
8236 kvm_inject_page_fault(vcpu, &e);
8237 return 1;
8238 }
8239 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008240 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008241}
8242
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008243/* Emulate the INVEPT instruction */
8244static int handle_invept(struct kvm_vcpu *vcpu)
8245{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008246 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008247 u32 vmx_instruction_info, types;
8248 unsigned long type;
8249 gva_t gva;
8250 struct x86_exception e;
8251 struct {
8252 u64 eptp, gpa;
8253 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008254
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008255 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008256 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008257 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008258 kvm_queue_exception(vcpu, UD_VECTOR);
8259 return 1;
8260 }
8261
8262 if (!nested_vmx_check_permission(vcpu))
8263 return 1;
8264
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008265 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008266 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008267
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008268 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008269
Jim Mattson85c856b2016-10-26 08:38:38 -07008270 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008271 nested_vmx_failValid(vcpu,
8272 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008273 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008274 }
8275
8276 /* According to the Intel VMX instruction reference, the memory
8277 * operand is read even if it isn't needed (e.g., for type==global)
8278 */
8279 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008280 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008281 return 1;
8282 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8283 sizeof(operand), &e)) {
8284 kvm_inject_page_fault(vcpu, &e);
8285 return 1;
8286 }
8287
8288 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008289 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008290 /*
8291 * TODO: track mappings and invalidate
8292 * single context requests appropriately
8293 */
8294 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008295 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008296 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008297 nested_vmx_succeed(vcpu);
8298 break;
8299 default:
8300 BUG_ON(1);
8301 break;
8302 }
8303
Kyle Huey6affcbe2016-11-29 12:40:40 -08008304 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008305}
8306
Petr Matouseka642fc32014-09-23 20:22:30 +02008307static int handle_invvpid(struct kvm_vcpu *vcpu)
8308{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008309 struct vcpu_vmx *vmx = to_vmx(vcpu);
8310 u32 vmx_instruction_info;
8311 unsigned long type, types;
8312 gva_t gva;
8313 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008314 struct {
8315 u64 vpid;
8316 u64 gla;
8317 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008318
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008319 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008320 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008321 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008322 kvm_queue_exception(vcpu, UD_VECTOR);
8323 return 1;
8324 }
8325
8326 if (!nested_vmx_check_permission(vcpu))
8327 return 1;
8328
8329 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8330 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8331
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008332 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008333 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008334
Jim Mattson85c856b2016-10-26 08:38:38 -07008335 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008336 nested_vmx_failValid(vcpu,
8337 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008338 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008339 }
8340
8341 /* according to the intel vmx instruction reference, the memory
8342 * operand is read even if it isn't needed (e.g., for type==global)
8343 */
8344 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8345 vmx_instruction_info, false, &gva))
8346 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008347 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8348 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008349 kvm_inject_page_fault(vcpu, &e);
8350 return 1;
8351 }
Jim Mattson40352602017-06-28 09:37:37 -07008352 if (operand.vpid >> 16) {
8353 nested_vmx_failValid(vcpu,
8354 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8355 return kvm_skip_emulated_instruction(vcpu);
8356 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008357
8358 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008359 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008360 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008361 nested_vmx_failValid(vcpu,
8362 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8363 return kvm_skip_emulated_instruction(vcpu);
8364 }
8365 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008366 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008367 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008368 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008369 nested_vmx_failValid(vcpu,
8370 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008371 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008372 }
8373 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008374 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008375 break;
8376 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008377 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008378 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008379 }
8380
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008381 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008382 nested_vmx_succeed(vcpu);
8383
Kyle Huey6affcbe2016-11-29 12:40:40 -08008384 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008385}
8386
Kai Huang843e4332015-01-28 10:54:28 +08008387static int handle_pml_full(struct kvm_vcpu *vcpu)
8388{
8389 unsigned long exit_qualification;
8390
8391 trace_kvm_pml_full(vcpu->vcpu_id);
8392
8393 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8394
8395 /*
8396 * PML buffer FULL happened while executing iret from NMI,
8397 * "blocked by NMI" bit has to be set before next VM entry.
8398 */
8399 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008400 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008401 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8402 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8403 GUEST_INTR_STATE_NMI);
8404
8405 /*
8406 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8407 * here.., and there's no userspace involvement needed for PML.
8408 */
8409 return 1;
8410}
8411
Yunhong Jiang64672c92016-06-13 14:19:59 -07008412static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8413{
8414 kvm_lapic_expired_hv_timer(vcpu);
8415 return 1;
8416}
8417
Bandan Das41ab9372017-08-03 15:54:43 -04008418static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8419{
8420 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008421 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8422
8423 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008424 switch (address & VMX_EPTP_MT_MASK) {
8425 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008426 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008427 return false;
8428 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008429 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008430 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008431 return false;
8432 break;
8433 default:
8434 return false;
8435 }
8436
David Hildenbrandbb97a012017-08-10 23:15:28 +02008437 /* only 4 levels page-walk length are valid */
8438 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008439 return false;
8440
8441 /* Reserved bits should not be set */
8442 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8443 return false;
8444
8445 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008446 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008447 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008448 return false;
8449 }
8450
8451 return true;
8452}
8453
8454static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8455 struct vmcs12 *vmcs12)
8456{
8457 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8458 u64 address;
8459 bool accessed_dirty;
8460 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8461
8462 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8463 !nested_cpu_has_ept(vmcs12))
8464 return 1;
8465
8466 if (index >= VMFUNC_EPTP_ENTRIES)
8467 return 1;
8468
8469
8470 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8471 &address, index * 8, 8))
8472 return 1;
8473
David Hildenbrandbb97a012017-08-10 23:15:28 +02008474 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008475
8476 /*
8477 * If the (L2) guest does a vmfunc to the currently
8478 * active ept pointer, we don't have to do anything else
8479 */
8480 if (vmcs12->ept_pointer != address) {
8481 if (!valid_ept_address(vcpu, address))
8482 return 1;
8483
8484 kvm_mmu_unload(vcpu);
8485 mmu->ept_ad = accessed_dirty;
8486 mmu->base_role.ad_disabled = !accessed_dirty;
8487 vmcs12->ept_pointer = address;
8488 /*
8489 * TODO: Check what's the correct approach in case
8490 * mmu reload fails. Currently, we just let the next
8491 * reload potentially fail
8492 */
8493 kvm_mmu_reload(vcpu);
8494 }
8495
8496 return 0;
8497}
8498
Bandan Das2a499e42017-08-03 15:54:41 -04008499static int handle_vmfunc(struct kvm_vcpu *vcpu)
8500{
Bandan Das27c42a12017-08-03 15:54:42 -04008501 struct vcpu_vmx *vmx = to_vmx(vcpu);
8502 struct vmcs12 *vmcs12;
8503 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8504
8505 /*
8506 * VMFUNC is only supported for nested guests, but we always enable the
8507 * secondary control for simplicity; for non-nested mode, fake that we
8508 * didn't by injecting #UD.
8509 */
8510 if (!is_guest_mode(vcpu)) {
8511 kvm_queue_exception(vcpu, UD_VECTOR);
8512 return 1;
8513 }
8514
8515 vmcs12 = get_vmcs12(vcpu);
8516 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8517 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008518
8519 switch (function) {
8520 case 0:
8521 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8522 goto fail;
8523 break;
8524 default:
8525 goto fail;
8526 }
8527 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008528
8529fail:
8530 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8531 vmcs_read32(VM_EXIT_INTR_INFO),
8532 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008533 return 1;
8534}
8535
Nadav Har'El0140cae2011-05-25 23:06:28 +03008536/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008537 * The exit handlers return 1 if the exit was handled fully and guest execution
8538 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8539 * to be done to userspace and return 0.
8540 */
Mathias Krause772e0312012-08-30 01:30:19 +02008541static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008542 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8543 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008544 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008545 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008546 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008547 [EXIT_REASON_CR_ACCESS] = handle_cr,
8548 [EXIT_REASON_DR_ACCESS] = handle_dr,
8549 [EXIT_REASON_CPUID] = handle_cpuid,
8550 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8551 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8552 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8553 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008554 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008555 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008556 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008557 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008558 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008559 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008560 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008561 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008563 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008565 [EXIT_REASON_VMOFF] = handle_vmoff,
8566 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008567 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8568 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008569 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008570 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008571 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008572 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008573 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008574 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008575 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8576 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008577 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8578 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008579 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008580 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008581 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008582 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008583 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008584 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008585 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008586 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008587 [EXIT_REASON_XSAVES] = handle_xsaves,
8588 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008589 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008590 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008591 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008592};
8593
8594static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008595 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008596
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008597static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8598 struct vmcs12 *vmcs12)
8599{
8600 unsigned long exit_qualification;
8601 gpa_t bitmap, last_bitmap;
8602 unsigned int port;
8603 int size;
8604 u8 b;
8605
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008606 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008607 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008608
8609 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8610
8611 port = exit_qualification >> 16;
8612 size = (exit_qualification & 7) + 1;
8613
8614 last_bitmap = (gpa_t)-1;
8615 b = -1;
8616
8617 while (size > 0) {
8618 if (port < 0x8000)
8619 bitmap = vmcs12->io_bitmap_a;
8620 else if (port < 0x10000)
8621 bitmap = vmcs12->io_bitmap_b;
8622 else
Joe Perches1d804d02015-03-30 16:46:09 -07008623 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008624 bitmap += (port & 0x7fff) / 8;
8625
8626 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008627 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008628 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008629 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008630 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008631
8632 port++;
8633 size--;
8634 last_bitmap = bitmap;
8635 }
8636
Joe Perches1d804d02015-03-30 16:46:09 -07008637 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008638}
8639
Nadav Har'El644d7112011-05-25 23:12:35 +03008640/*
8641 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8642 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8643 * disinterest in the current event (read or write a specific MSR) by using an
8644 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8645 */
8646static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8647 struct vmcs12 *vmcs12, u32 exit_reason)
8648{
8649 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8650 gpa_t bitmap;
8651
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008652 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008653 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008654
8655 /*
8656 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8657 * for the four combinations of read/write and low/high MSR numbers.
8658 * First we need to figure out which of the four to use:
8659 */
8660 bitmap = vmcs12->msr_bitmap;
8661 if (exit_reason == EXIT_REASON_MSR_WRITE)
8662 bitmap += 2048;
8663 if (msr_index >= 0xc0000000) {
8664 msr_index -= 0xc0000000;
8665 bitmap += 1024;
8666 }
8667
8668 /* Then read the msr_index'th bit from this bitmap: */
8669 if (msr_index < 1024*8) {
8670 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008671 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008672 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008673 return 1 & (b >> (msr_index & 7));
8674 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008675 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008676}
8677
8678/*
8679 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8680 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8681 * intercept (via guest_host_mask etc.) the current event.
8682 */
8683static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8684 struct vmcs12 *vmcs12)
8685{
8686 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8687 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008688 int reg;
8689 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008690
8691 switch ((exit_qualification >> 4) & 3) {
8692 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008693 reg = (exit_qualification >> 8) & 15;
8694 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008695 switch (cr) {
8696 case 0:
8697 if (vmcs12->cr0_guest_host_mask &
8698 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008699 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008700 break;
8701 case 3:
8702 if ((vmcs12->cr3_target_count >= 1 &&
8703 vmcs12->cr3_target_value0 == val) ||
8704 (vmcs12->cr3_target_count >= 2 &&
8705 vmcs12->cr3_target_value1 == val) ||
8706 (vmcs12->cr3_target_count >= 3 &&
8707 vmcs12->cr3_target_value2 == val) ||
8708 (vmcs12->cr3_target_count >= 4 &&
8709 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008710 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008711 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008712 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008713 break;
8714 case 4:
8715 if (vmcs12->cr4_guest_host_mask &
8716 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008717 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008718 break;
8719 case 8:
8720 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008721 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008722 break;
8723 }
8724 break;
8725 case 2: /* clts */
8726 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8727 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008728 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008729 break;
8730 case 1: /* mov from cr */
8731 switch (cr) {
8732 case 3:
8733 if (vmcs12->cpu_based_vm_exec_control &
8734 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008735 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008736 break;
8737 case 8:
8738 if (vmcs12->cpu_based_vm_exec_control &
8739 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008740 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008741 break;
8742 }
8743 break;
8744 case 3: /* lmsw */
8745 /*
8746 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8747 * cr0. Other attempted changes are ignored, with no exit.
8748 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008749 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008750 if (vmcs12->cr0_guest_host_mask & 0xe &
8751 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008752 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008753 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8754 !(vmcs12->cr0_read_shadow & 0x1) &&
8755 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008756 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008757 break;
8758 }
Joe Perches1d804d02015-03-30 16:46:09 -07008759 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008760}
8761
8762/*
8763 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8764 * should handle it ourselves in L0 (and then continue L2). Only call this
8765 * when in is_guest_mode (L2).
8766 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008767static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008768{
Nadav Har'El644d7112011-05-25 23:12:35 +03008769 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8770 struct vcpu_vmx *vmx = to_vmx(vcpu);
8771 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8772
Jim Mattson4f350c62017-09-14 16:31:44 -07008773 if (vmx->nested.nested_run_pending)
8774 return false;
8775
8776 if (unlikely(vmx->fail)) {
8777 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8778 vmcs_read32(VM_INSTRUCTION_ERROR));
8779 return true;
8780 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008781
David Matlackc9f04402017-08-01 14:00:40 -07008782 /*
8783 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008784 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8785 * Page). The CPU may write to these pages via their host
8786 * physical address while L2 is running, bypassing any
8787 * address-translation-based dirty tracking (e.g. EPT write
8788 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008789 *
8790 * Mark them dirty on every exit from L2 to prevent them from
8791 * getting out of sync with dirty tracking.
8792 */
8793 nested_mark_vmcs12_pages_dirty(vcpu);
8794
Jim Mattson4f350c62017-09-14 16:31:44 -07008795 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8796 vmcs_readl(EXIT_QUALIFICATION),
8797 vmx->idt_vectoring_info,
8798 intr_info,
8799 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8800 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008801
8802 switch (exit_reason) {
8803 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008804 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008805 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008806 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008807 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008808 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008809 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008810 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008811 else if (is_debug(intr_info) &&
8812 vcpu->guest_debug &
8813 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8814 return false;
8815 else if (is_breakpoint(intr_info) &&
8816 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8817 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008818 return vmcs12->exception_bitmap &
8819 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8820 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008821 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008822 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008823 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008824 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008825 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008826 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008827 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008828 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008829 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008830 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008831 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008832 case EXIT_REASON_HLT:
8833 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8834 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008835 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008836 case EXIT_REASON_INVLPG:
8837 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8838 case EXIT_REASON_RDPMC:
8839 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008840 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008841 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008842 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008843 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008844 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008845 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8846 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8847 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8848 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8849 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8850 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008851 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008852 /*
8853 * VMX instructions trap unconditionally. This allows L1 to
8854 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8855 */
Joe Perches1d804d02015-03-30 16:46:09 -07008856 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008857 case EXIT_REASON_CR_ACCESS:
8858 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8859 case EXIT_REASON_DR_ACCESS:
8860 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8861 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008862 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008863 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8864 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008865 case EXIT_REASON_MSR_READ:
8866 case EXIT_REASON_MSR_WRITE:
8867 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8868 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008869 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008870 case EXIT_REASON_MWAIT_INSTRUCTION:
8871 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008872 case EXIT_REASON_MONITOR_TRAP_FLAG:
8873 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008874 case EXIT_REASON_MONITOR_INSTRUCTION:
8875 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8876 case EXIT_REASON_PAUSE_INSTRUCTION:
8877 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8878 nested_cpu_has2(vmcs12,
8879 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8880 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008881 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008882 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008883 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008884 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08008885 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008886 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04008887 /*
8888 * The controls for "virtualize APIC accesses," "APIC-
8889 * register virtualization," and "virtual-interrupt
8890 * delivery" only come from vmcs12.
8891 */
Joe Perches1d804d02015-03-30 16:46:09 -07008892 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008893 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008894 /*
8895 * L0 always deals with the EPT violation. If nested EPT is
8896 * used, and the nested mmu code discovers that the address is
8897 * missing in the guest EPT table (EPT12), the EPT violation
8898 * will be injected with nested_ept_inject_page_fault()
8899 */
Joe Perches1d804d02015-03-30 16:46:09 -07008900 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008901 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008902 /*
8903 * L2 never uses directly L1's EPT, but rather L0's own EPT
8904 * table (shadow on EPT) or a merged EPT table that L0 built
8905 * (EPT on EPT). So any problems with the structure of the
8906 * table is L0's fault.
8907 */
Joe Perches1d804d02015-03-30 16:46:09 -07008908 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008909 case EXIT_REASON_INVPCID:
8910 return
8911 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8912 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008913 case EXIT_REASON_WBINVD:
8914 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8915 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008916 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008917 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8918 /*
8919 * This should never happen, since it is not possible to
8920 * set XSS to a non-zero value---neither in L1 nor in L2.
8921 * If if it were, XSS would have to be checked against
8922 * the XSS exit bitmap in vmcs12.
8923 */
8924 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008925 case EXIT_REASON_PREEMPTION_TIMER:
8926 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008927 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008928 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008929 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008930 case EXIT_REASON_VMFUNC:
8931 /* VM functions are emulated through L2->L0 vmexits. */
8932 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008933 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008934 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008935 }
8936}
8937
Paolo Bonzini7313c692017-07-27 10:31:25 +02008938static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8939{
8940 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8941
8942 /*
8943 * At this point, the exit interruption info in exit_intr_info
8944 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8945 * we need to query the in-kernel LAPIC.
8946 */
8947 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8948 if ((exit_intr_info &
8949 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8950 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8951 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8952 vmcs12->vm_exit_intr_error_code =
8953 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8954 }
8955
8956 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8957 vmcs_readl(EXIT_QUALIFICATION));
8958 return 1;
8959}
8960
Avi Kivity586f9602010-11-18 13:09:54 +02008961static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8962{
8963 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8964 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8965}
8966
Kai Huanga3eaa862015-11-04 13:46:05 +08008967static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008968{
Kai Huanga3eaa862015-11-04 13:46:05 +08008969 if (vmx->pml_pg) {
8970 __free_page(vmx->pml_pg);
8971 vmx->pml_pg = NULL;
8972 }
Kai Huang843e4332015-01-28 10:54:28 +08008973}
8974
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008975static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008976{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008977 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008978 u64 *pml_buf;
8979 u16 pml_idx;
8980
8981 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8982
8983 /* Do nothing if PML buffer is empty */
8984 if (pml_idx == (PML_ENTITY_NUM - 1))
8985 return;
8986
8987 /* PML index always points to next available PML buffer entity */
8988 if (pml_idx >= PML_ENTITY_NUM)
8989 pml_idx = 0;
8990 else
8991 pml_idx++;
8992
8993 pml_buf = page_address(vmx->pml_pg);
8994 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8995 u64 gpa;
8996
8997 gpa = pml_buf[pml_idx];
8998 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008999 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009000 }
9001
9002 /* reset PML index */
9003 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9004}
9005
9006/*
9007 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9008 * Called before reporting dirty_bitmap to userspace.
9009 */
9010static void kvm_flush_pml_buffers(struct kvm *kvm)
9011{
9012 int i;
9013 struct kvm_vcpu *vcpu;
9014 /*
9015 * We only need to kick vcpu out of guest mode here, as PML buffer
9016 * is flushed at beginning of all VMEXITs, and it's obvious that only
9017 * vcpus running in guest are possible to have unflushed GPAs in PML
9018 * buffer.
9019 */
9020 kvm_for_each_vcpu(i, vcpu, kvm)
9021 kvm_vcpu_kick(vcpu);
9022}
9023
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009024static void vmx_dump_sel(char *name, uint32_t sel)
9025{
9026 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009027 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009028 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9029 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9030 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9031}
9032
9033static void vmx_dump_dtsel(char *name, uint32_t limit)
9034{
9035 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9036 name, vmcs_read32(limit),
9037 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9038}
9039
9040static void dump_vmcs(void)
9041{
9042 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9043 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9044 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9045 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9046 u32 secondary_exec_control = 0;
9047 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009048 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009049 int i, n;
9050
9051 if (cpu_has_secondary_exec_ctrls())
9052 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9053
9054 pr_err("*** Guest State ***\n");
9055 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9056 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9057 vmcs_readl(CR0_GUEST_HOST_MASK));
9058 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9059 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9060 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9061 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9062 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9063 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009064 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9065 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9066 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9067 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009068 }
9069 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9070 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9071 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9072 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9073 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9074 vmcs_readl(GUEST_SYSENTER_ESP),
9075 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9076 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9077 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9078 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9079 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9080 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9081 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9082 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9083 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9084 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9085 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9086 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9087 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009088 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9089 efer, vmcs_read64(GUEST_IA32_PAT));
9090 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9091 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009092 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009093 if (cpu_has_load_perf_global_ctrl &&
9094 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009095 pr_err("PerfGlobCtl = 0x%016llx\n",
9096 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009097 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009098 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009099 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9100 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9101 vmcs_read32(GUEST_ACTIVITY_STATE));
9102 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9103 pr_err("InterruptStatus = %04x\n",
9104 vmcs_read16(GUEST_INTR_STATUS));
9105
9106 pr_err("*** Host State ***\n");
9107 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9108 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9109 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9110 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9111 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9112 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9113 vmcs_read16(HOST_TR_SELECTOR));
9114 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9115 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9116 vmcs_readl(HOST_TR_BASE));
9117 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9118 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9119 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9120 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9121 vmcs_readl(HOST_CR4));
9122 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9123 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9124 vmcs_read32(HOST_IA32_SYSENTER_CS),
9125 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9126 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009127 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9128 vmcs_read64(HOST_IA32_EFER),
9129 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009130 if (cpu_has_load_perf_global_ctrl &&
9131 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009132 pr_err("PerfGlobCtl = 0x%016llx\n",
9133 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009134
9135 pr_err("*** Control State ***\n");
9136 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9137 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9138 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9139 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9140 vmcs_read32(EXCEPTION_BITMAP),
9141 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9142 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9143 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9144 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9145 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9146 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9147 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9148 vmcs_read32(VM_EXIT_INTR_INFO),
9149 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9150 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9151 pr_err(" reason=%08x qualification=%016lx\n",
9152 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9153 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9154 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9155 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009156 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009157 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009158 pr_err("TSC Multiplier = 0x%016llx\n",
9159 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009160 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9161 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9162 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9163 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9164 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009165 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009166 n = vmcs_read32(CR3_TARGET_COUNT);
9167 for (i = 0; i + 1 < n; i += 4)
9168 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9169 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9170 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9171 if (i < n)
9172 pr_err("CR3 target%u=%016lx\n",
9173 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9174 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9175 pr_err("PLE Gap=%08x Window=%08x\n",
9176 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9177 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9178 pr_err("Virtual processor ID = 0x%04x\n",
9179 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9180}
9181
Avi Kivity6aa8b732006-12-10 02:21:36 -08009182/*
9183 * The guest has exited. See if we can fix it or if we need userspace
9184 * assistance.
9185 */
Avi Kivity851ba692009-08-24 11:10:17 +03009186static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009187{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009188 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009189 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009190 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009191
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009192 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9193
Kai Huang843e4332015-01-28 10:54:28 +08009194 /*
9195 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9196 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9197 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9198 * mode as if vcpus is in root mode, the PML buffer must has been
9199 * flushed already.
9200 */
9201 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009202 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009203
Mohammed Gamal80ced182009-09-01 12:48:18 +02009204 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009205 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009206 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009207
Paolo Bonzini7313c692017-07-27 10:31:25 +02009208 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9209 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009210
Mohammed Gamal51207022010-05-31 22:40:54 +03009211 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009212 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009213 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9214 vcpu->run->fail_entry.hardware_entry_failure_reason
9215 = exit_reason;
9216 return 0;
9217 }
9218
Avi Kivity29bd8a72007-09-10 17:27:03 +03009219 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009220 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9221 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009222 = vmcs_read32(VM_INSTRUCTION_ERROR);
9223 return 0;
9224 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009225
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009226 /*
9227 * Note:
9228 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9229 * delivery event since it indicates guest is accessing MMIO.
9230 * The vm-exit can be triggered again after return to guest that
9231 * will cause infinite loop.
9232 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009233 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009234 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009235 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009236 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009237 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9238 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9239 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009240 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009241 vcpu->run->internal.data[0] = vectoring_info;
9242 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009243 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9244 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9245 vcpu->run->internal.ndata++;
9246 vcpu->run->internal.data[3] =
9247 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9248 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009249 return 0;
9250 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009251
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009252 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009253 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9254 if (vmx_interrupt_allowed(vcpu)) {
9255 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9256 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9257 vcpu->arch.nmi_pending) {
9258 /*
9259 * This CPU don't support us in finding the end of an
9260 * NMI-blocked window if the guest runs with IRQs
9261 * disabled. So we pull the trigger after 1 s of
9262 * futile waiting, but inform the user about this.
9263 */
9264 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9265 "state on VCPU %d after 1 s timeout\n",
9266 __func__, vcpu->vcpu_id);
9267 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9268 }
9269 }
9270
Avi Kivity6aa8b732006-12-10 02:21:36 -08009271 if (exit_reason < kvm_vmx_max_exit_handlers
9272 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009273 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009274 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009275 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9276 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009277 kvm_queue_exception(vcpu, UD_VECTOR);
9278 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009279 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009280}
9281
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009282static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009283{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009284 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9285
9286 if (is_guest_mode(vcpu) &&
9287 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9288 return;
9289
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009290 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009291 vmcs_write32(TPR_THRESHOLD, 0);
9292 return;
9293 }
9294
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009295 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009296}
9297
Jim Mattson8d860bb2018-05-09 16:56:05 -04009298static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009299{
9300 u32 sec_exec_control;
9301
Jim Mattson8d860bb2018-05-09 16:56:05 -04009302 if (!lapic_in_kernel(vcpu))
9303 return;
9304
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009305 /* Postpone execution until vmcs01 is the current VMCS. */
9306 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009307 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009308 return;
9309 }
9310
Paolo Bonzini35754c92015-07-29 12:05:37 +02009311 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009312 return;
9313
9314 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009315 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9316 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009317
Jim Mattson8d860bb2018-05-09 16:56:05 -04009318 switch (kvm_get_apic_mode(vcpu)) {
9319 case LAPIC_MODE_INVALID:
9320 WARN_ONCE(true, "Invalid local APIC state");
9321 case LAPIC_MODE_DISABLED:
9322 break;
9323 case LAPIC_MODE_XAPIC:
9324 if (flexpriority_enabled) {
9325 sec_exec_control |=
9326 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9327 vmx_flush_tlb(vcpu, true);
9328 }
9329 break;
9330 case LAPIC_MODE_X2APIC:
9331 if (cpu_has_vmx_virtualize_x2apic_mode())
9332 sec_exec_control |=
9333 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9334 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009335 }
9336 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9337
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009338 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009339}
9340
Tang Chen38b99172014-09-24 15:57:54 +08009341static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9342{
Jim Mattsonab5df312018-05-09 17:02:03 -04009343 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009344 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009345 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009346 }
Tang Chen38b99172014-09-24 15:57:54 +08009347}
9348
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009349static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009350{
9351 u16 status;
9352 u8 old;
9353
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009354 if (max_isr == -1)
9355 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009356
9357 status = vmcs_read16(GUEST_INTR_STATUS);
9358 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009359 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009360 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009361 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009362 vmcs_write16(GUEST_INTR_STATUS, status);
9363 }
9364}
9365
9366static void vmx_set_rvi(int vector)
9367{
9368 u16 status;
9369 u8 old;
9370
Wei Wang4114c272014-11-05 10:53:43 +08009371 if (vector == -1)
9372 vector = 0;
9373
Yang Zhangc7c9c562013-01-25 10:18:51 +08009374 status = vmcs_read16(GUEST_INTR_STATUS);
9375 old = (u8)status & 0xff;
9376 if ((u8)vector != old) {
9377 status &= ~0xff;
9378 status |= (u8)vector;
9379 vmcs_write16(GUEST_INTR_STATUS, status);
9380 }
9381}
9382
9383static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9384{
Liran Alon851c1a182017-12-24 18:12:56 +02009385 /*
9386 * When running L2, updating RVI is only relevant when
9387 * vmcs12 virtual-interrupt-delivery enabled.
9388 * However, it can be enabled only when L1 also
9389 * intercepts external-interrupts and in that case
9390 * we should not update vmcs02 RVI but instead intercept
9391 * interrupt. Therefore, do nothing when running L2.
9392 */
9393 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009394 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009395}
9396
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009397static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009398{
9399 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009400 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009401 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009402
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009403 WARN_ON(!vcpu->arch.apicv_active);
9404 if (pi_test_on(&vmx->pi_desc)) {
9405 pi_clear_on(&vmx->pi_desc);
9406 /*
9407 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9408 * But on x86 this is just a compiler barrier anyway.
9409 */
9410 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009411 max_irr_updated =
9412 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9413
9414 /*
9415 * If we are running L2 and L1 has a new pending interrupt
9416 * which can be injected, we should re-evaluate
9417 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009418 * If L1 intercepts external-interrupts, we should
9419 * exit from L2 to L1. Otherwise, interrupt should be
9420 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009421 */
Liran Alon851c1a182017-12-24 18:12:56 +02009422 if (is_guest_mode(vcpu) && max_irr_updated) {
9423 if (nested_exit_on_intr(vcpu))
9424 kvm_vcpu_exiting_guest_mode(vcpu);
9425 else
9426 kvm_make_request(KVM_REQ_EVENT, vcpu);
9427 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009428 } else {
9429 max_irr = kvm_lapic_find_highest_irr(vcpu);
9430 }
9431 vmx_hwapic_irr_update(vcpu, max_irr);
9432 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009433}
9434
Andrey Smetanin63086302015-11-10 15:36:32 +03009435static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009436{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009437 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009438 return;
9439
Yang Zhangc7c9c562013-01-25 10:18:51 +08009440 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9441 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9442 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9443 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9444}
9445
Paolo Bonzini967235d2016-12-19 14:03:45 +01009446static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9447{
9448 struct vcpu_vmx *vmx = to_vmx(vcpu);
9449
9450 pi_clear_on(&vmx->pi_desc);
9451 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9452}
9453
Avi Kivity51aa01d2010-07-20 14:31:20 +03009454static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009455{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009456 u32 exit_intr_info = 0;
9457 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009458
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009459 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9460 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009461 return;
9462
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009463 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9464 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9465 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009466
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009467 /* if exit due to PF check for async PF */
9468 if (is_page_fault(exit_intr_info))
9469 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9470
Andi Kleena0861c02009-06-08 17:37:09 +08009471 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009472 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9473 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009474 kvm_machine_check();
9475
Gleb Natapov20f65982009-05-11 13:35:55 +03009476 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009477 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009478 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009479 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009480 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009481 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009482}
Gleb Natapov20f65982009-05-11 13:35:55 +03009483
Yang Zhanga547c6d2013-04-11 19:25:10 +08009484static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9485{
9486 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9487
Yang Zhanga547c6d2013-04-11 19:25:10 +08009488 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9489 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9490 unsigned int vector;
9491 unsigned long entry;
9492 gate_desc *desc;
9493 struct vcpu_vmx *vmx = to_vmx(vcpu);
9494#ifdef CONFIG_X86_64
9495 unsigned long tmp;
9496#endif
9497
9498 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9499 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009500 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009501 asm volatile(
9502#ifdef CONFIG_X86_64
9503 "mov %%" _ASM_SP ", %[sp]\n\t"
9504 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9505 "push $%c[ss]\n\t"
9506 "push %[sp]\n\t"
9507#endif
9508 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009509 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009510 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009511 :
9512#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009513 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009514#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009515 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009516 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009517 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009518 [ss]"i"(__KERNEL_DS),
9519 [cs]"i"(__KERNEL_CS)
9520 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009521 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009522}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009523STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009524
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009525static bool vmx_has_high_real_mode_segbase(void)
9526{
9527 return enable_unrestricted_guest || emulate_invalid_guest_state;
9528}
9529
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009530static bool vmx_mpx_supported(void)
9531{
9532 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9533 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9534}
9535
Wanpeng Li55412b22014-12-02 19:21:30 +08009536static bool vmx_xsaves_supported(void)
9537{
9538 return vmcs_config.cpu_based_2nd_exec_ctrl &
9539 SECONDARY_EXEC_XSAVES;
9540}
9541
Avi Kivity51aa01d2010-07-20 14:31:20 +03009542static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9543{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009544 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009545 bool unblock_nmi;
9546 u8 vector;
9547 bool idtv_info_valid;
9548
9549 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009550
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009551 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009552 if (vmx->loaded_vmcs->nmi_known_unmasked)
9553 return;
9554 /*
9555 * Can't use vmx->exit_intr_info since we're not sure what
9556 * the exit reason is.
9557 */
9558 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9559 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9560 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9561 /*
9562 * SDM 3: 27.7.1.2 (September 2008)
9563 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9564 * a guest IRET fault.
9565 * SDM 3: 23.2.2 (September 2008)
9566 * Bit 12 is undefined in any of the following cases:
9567 * If the VM exit sets the valid bit in the IDT-vectoring
9568 * information field.
9569 * If the VM exit is due to a double fault.
9570 */
9571 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9572 vector != DF_VECTOR && !idtv_info_valid)
9573 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9574 GUEST_INTR_STATE_NMI);
9575 else
9576 vmx->loaded_vmcs->nmi_known_unmasked =
9577 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9578 & GUEST_INTR_STATE_NMI);
9579 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9580 vmx->loaded_vmcs->vnmi_blocked_time +=
9581 ktime_to_ns(ktime_sub(ktime_get(),
9582 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009583}
9584
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009585static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009586 u32 idt_vectoring_info,
9587 int instr_len_field,
9588 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009589{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009590 u8 vector;
9591 int type;
9592 bool idtv_info_valid;
9593
9594 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009595
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009596 vcpu->arch.nmi_injected = false;
9597 kvm_clear_exception_queue(vcpu);
9598 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009599
9600 if (!idtv_info_valid)
9601 return;
9602
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009603 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009604
Avi Kivity668f6122008-07-02 09:28:55 +03009605 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9606 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009607
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009608 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009609 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009610 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009611 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009612 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009613 * Clear bit "block by NMI" before VM entry if a NMI
9614 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009615 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009616 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009617 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009618 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009619 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009620 /* fall through */
9621 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009622 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009623 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009624 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009625 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009626 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009627 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009628 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009629 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009630 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009631 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009632 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009633 break;
9634 default:
9635 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009636 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009637}
9638
Avi Kivity83422e12010-07-20 14:43:23 +03009639static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9640{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009641 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009642 VM_EXIT_INSTRUCTION_LEN,
9643 IDT_VECTORING_ERROR_CODE);
9644}
9645
Avi Kivityb463a6f2010-07-20 15:06:17 +03009646static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9647{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009648 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009649 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9650 VM_ENTRY_INSTRUCTION_LEN,
9651 VM_ENTRY_EXCEPTION_ERROR_CODE);
9652
9653 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9654}
9655
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009656static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9657{
9658 int i, nr_msrs;
9659 struct perf_guest_switch_msr *msrs;
9660
9661 msrs = perf_guest_get_msrs(&nr_msrs);
9662
9663 if (!msrs)
9664 return;
9665
9666 for (i = 0; i < nr_msrs; i++)
9667 if (msrs[i].host == msrs[i].guest)
9668 clear_atomic_switch_msr(vmx, msrs[i].msr);
9669 else
9670 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9671 msrs[i].host);
9672}
9673
Jiang Biao33365e72016-11-03 15:03:37 +08009674static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009675{
9676 struct vcpu_vmx *vmx = to_vmx(vcpu);
9677 u64 tscl;
9678 u32 delta_tsc;
9679
9680 if (vmx->hv_deadline_tsc == -1)
9681 return;
9682
9683 tscl = rdtsc();
9684 if (vmx->hv_deadline_tsc > tscl)
9685 /* sure to be 32 bit only because checked on set_hv_timer */
9686 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9687 cpu_preemption_timer_multi);
9688 else
9689 delta_tsc = 0;
9690
9691 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9692}
9693
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009694static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009695{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009696 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009697 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009698
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009699 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009700 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009701 vmx->loaded_vmcs->soft_vnmi_blocked))
9702 vmx->loaded_vmcs->entry_time = ktime_get();
9703
Avi Kivity104f2262010-11-18 13:12:52 +02009704 /* Don't enter VMX if guest state is invalid, let the exit handler
9705 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009706 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009707 return;
9708
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009709 if (vmx->ple_window_dirty) {
9710 vmx->ple_window_dirty = false;
9711 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9712 }
9713
Abel Gordon012f83c2013-04-18 14:39:25 +03009714 if (vmx->nested.sync_shadow_vmcs) {
9715 copy_vmcs12_to_shadow(vmx);
9716 vmx->nested.sync_shadow_vmcs = false;
9717 }
9718
Avi Kivity104f2262010-11-18 13:12:52 +02009719 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9720 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9721 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9722 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9723
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009724 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009725 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009726 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009727 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009728 }
9729
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009730 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009731 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009732 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009733 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009734 }
9735
Avi Kivity104f2262010-11-18 13:12:52 +02009736 /* When single-stepping over STI and MOV SS, we must clear the
9737 * corresponding interruptibility bits in the guest state. Otherwise
9738 * vmentry fails as it then expects bit 14 (BS) in pending debug
9739 * exceptions being set, but that's not correct for the guest debugging
9740 * case. */
9741 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9742 vmx_set_interrupt_shadow(vcpu, 0);
9743
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009744 if (static_cpu_has(X86_FEATURE_PKU) &&
9745 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9746 vcpu->arch.pkru != vmx->host_pkru)
9747 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009748
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009749 atomic_switch_perf_msrs(vmx);
9750
Yunhong Jiang64672c92016-06-13 14:19:59 -07009751 vmx_arm_hv_timer(vcpu);
9752
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009753 /*
9754 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9755 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9756 * is no need to worry about the conditional branch over the wrmsr
9757 * being speculatively taken.
9758 */
9759 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009760 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009761
Nadav Har'Eld462b812011-05-24 15:26:10 +03009762 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009763
9764 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9765 (unsigned long)&current_evmcs->host_rsp : 0;
9766
Avi Kivity104f2262010-11-18 13:12:52 +02009767 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009768 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009769 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9770 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9771 "push %%" _ASM_CX " \n\t"
9772 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009773 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009774 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009775 /* Avoid VMWRITE when Enlightened VMCS is in use */
9776 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9777 "jz 2f \n\t"
9778 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9779 "jmp 1f \n\t"
9780 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009781 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009782 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009783 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009784 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9785 "mov %%cr2, %%" _ASM_DX " \n\t"
9786 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009787 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009788 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009789 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009790 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009791 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009792 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009793 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9794 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9795 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9796 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9797 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9798 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009799#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009800 "mov %c[r8](%0), %%r8 \n\t"
9801 "mov %c[r9](%0), %%r9 \n\t"
9802 "mov %c[r10](%0), %%r10 \n\t"
9803 "mov %c[r11](%0), %%r11 \n\t"
9804 "mov %c[r12](%0), %%r12 \n\t"
9805 "mov %c[r13](%0), %%r13 \n\t"
9806 "mov %c[r14](%0), %%r14 \n\t"
9807 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009808#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009809 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009810
Avi Kivity6aa8b732006-12-10 02:21:36 -08009811 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009812 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009813 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009814 "jmp 2f \n\t"
9815 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9816 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009817 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009818 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009819 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009820 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009821 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9822 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9823 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9824 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9825 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9826 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9827 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009828#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009829 "mov %%r8, %c[r8](%0) \n\t"
9830 "mov %%r9, %c[r9](%0) \n\t"
9831 "mov %%r10, %c[r10](%0) \n\t"
9832 "mov %%r11, %c[r11](%0) \n\t"
9833 "mov %%r12, %c[r12](%0) \n\t"
9834 "mov %%r13, %c[r13](%0) \n\t"
9835 "mov %%r14, %c[r14](%0) \n\t"
9836 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009837 "xor %%r8d, %%r8d \n\t"
9838 "xor %%r9d, %%r9d \n\t"
9839 "xor %%r10d, %%r10d \n\t"
9840 "xor %%r11d, %%r11d \n\t"
9841 "xor %%r12d, %%r12d \n\t"
9842 "xor %%r13d, %%r13d \n\t"
9843 "xor %%r14d, %%r14d \n\t"
9844 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009845#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009846 "mov %%cr2, %%" _ASM_AX " \n\t"
9847 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009848
Jim Mattson0cb5b302018-01-03 14:31:38 -08009849 "xor %%eax, %%eax \n\t"
9850 "xor %%ebx, %%ebx \n\t"
9851 "xor %%esi, %%esi \n\t"
9852 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009853 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009854 ".pushsection .rodata \n\t"
9855 ".global vmx_return \n\t"
9856 "vmx_return: " _ASM_PTR " 2b \n\t"
9857 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009858 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009859 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009860 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009861 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009862 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9863 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9864 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9865 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9866 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9867 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9868 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009869#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009870 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9871 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9872 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9873 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9874 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9875 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9876 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9877 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009878#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009879 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9880 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009881 : "cc", "memory"
9882#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009883 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009884 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009885#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009886 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009887#endif
9888 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009889
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009890 /*
9891 * We do not use IBRS in the kernel. If this vCPU has used the
9892 * SPEC_CTRL MSR it may have left it on; save the value and
9893 * turn it off. This is much more efficient than blindly adding
9894 * it to the atomic save/restore list. Especially as the former
9895 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9896 *
9897 * For non-nested case:
9898 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9899 * save it.
9900 *
9901 * For nested case:
9902 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9903 * save it.
9904 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009905 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009906 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009907
9908 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009909 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009910
David Woodhouse117cc7a2018-01-12 11:11:27 +00009911 /* Eliminate branch target predictions from guest mode */
9912 vmexit_fill_RSB();
9913
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009914 /* All fields are clean at this point */
9915 if (static_branch_unlikely(&enable_evmcs))
9916 current_evmcs->hv_clean_fields |=
9917 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9918
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009919 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009920 if (vmx->host_debugctlmsr)
9921 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009922
Avi Kivityaa67f602012-08-01 16:48:03 +03009923#ifndef CONFIG_X86_64
9924 /*
9925 * The sysexit path does not restore ds/es, so we must set them to
9926 * a reasonable value ourselves.
9927 *
9928 * We can't defer this to vmx_load_host_state() since that function
9929 * may be executed in interrupt context, which saves and restore segments
9930 * around it, nullifying its effect.
9931 */
9932 loadsegment(ds, __USER_DS);
9933 loadsegment(es, __USER_DS);
9934#endif
9935
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009936 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009937 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009938 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009939 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009940 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009941 vcpu->arch.regs_dirty = 0;
9942
Gleb Natapove0b890d2013-09-25 12:51:33 +03009943 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009944 * eager fpu is enabled if PKEY is supported and CR4 is switched
9945 * back on host, so it is safe to read guest PKRU from current
9946 * XSAVE.
9947 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009948 if (static_cpu_has(X86_FEATURE_PKU) &&
9949 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9950 vcpu->arch.pkru = __read_pkru();
9951 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009952 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009953 }
9954
Gleb Natapove0b890d2013-09-25 12:51:33 +03009955 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009956 vmx->idt_vectoring_info = 0;
9957
9958 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9959 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9960 return;
9961
9962 vmx->loaded_vmcs->launched = 1;
9963 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009964
Avi Kivity51aa01d2010-07-20 14:31:20 +03009965 vmx_complete_atomic_exit(vmx);
9966 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009967 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009968}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009969STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009970
Sean Christopherson434a1e92018-03-20 12:17:18 -07009971static struct kvm *vmx_vm_alloc(void)
9972{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009973 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9974 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009975}
9976
9977static void vmx_vm_free(struct kvm *kvm)
9978{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009979 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009980}
9981
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009982static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009983{
9984 struct vcpu_vmx *vmx = to_vmx(vcpu);
9985 int cpu;
9986
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009987 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009988 return;
9989
9990 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009991 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009992 vmx_vcpu_put(vcpu);
9993 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009994 put_cpu();
9995}
9996
Jim Mattson2f1fe812016-07-08 15:36:06 -07009997/*
9998 * Ensure that the current vmcs of the logical processor is the
9999 * vmcs01 of the vcpu before calling free_nested().
10000 */
10001static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10002{
10003 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010004
Christoffer Dallec7660c2017-12-04 21:35:23 +010010005 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010006 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010007 free_nested(vmx);
10008 vcpu_put(vcpu);
10009}
10010
Avi Kivity6aa8b732006-12-10 02:21:36 -080010011static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10012{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010013 struct vcpu_vmx *vmx = to_vmx(vcpu);
10014
Kai Huang843e4332015-01-28 10:54:28 +080010015 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010016 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010017 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010018 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010019 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010020 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010021 kfree(vmx->guest_msrs);
10022 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010023 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010024}
10025
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010026static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010027{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010028 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010029 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010030 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010031 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010032
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010033 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010034 return ERR_PTR(-ENOMEM);
10035
Wanpeng Li991e7a02015-09-16 17:30:05 +080010036 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010037
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010038 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10039 if (err)
10040 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010041
Peter Feiner4e595162016-07-07 14:49:58 -070010042 err = -ENOMEM;
10043
10044 /*
10045 * If PML is turned on, failure on enabling PML just results in failure
10046 * of creating the vcpu, therefore we can simplify PML logic (by
10047 * avoiding dealing with cases, such as enabling PML partially on vcpus
10048 * for the guest, etc.
10049 */
10050 if (enable_pml) {
10051 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10052 if (!vmx->pml_pg)
10053 goto uninit_vcpu;
10054 }
10055
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010056 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010057 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10058 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010059
Peter Feiner4e595162016-07-07 14:49:58 -070010060 if (!vmx->guest_msrs)
10061 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010062
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010063 err = alloc_loaded_vmcs(&vmx->vmcs01);
10064 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010065 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010066
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010067 msr_bitmap = vmx->vmcs01.msr_bitmap;
10068 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10069 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10070 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10071 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10072 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10073 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10074 vmx->msr_bitmap_mode = 0;
10075
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010076 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010077 cpu = get_cpu();
10078 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010079 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010080 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010081 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010082 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010083 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010084 err = alloc_apic_access_page(kvm);
10085 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010086 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010087 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010088
Sean Christophersone90008d2018-03-05 12:04:37 -080010089 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010090 err = init_rmode_identity_map(kvm);
10091 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010092 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010093 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010094
Wanpeng Li5c614b32015-10-13 09:18:36 -070010095 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010096 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10097 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010098 vmx->nested.vpid02 = allocate_vpid();
10099 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010100
Wincy Van705699a2015-02-03 23:58:17 +080010101 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010102 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010103
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010104 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10105
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010106 /*
10107 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10108 * or POSTED_INTR_WAKEUP_VECTOR.
10109 */
10110 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10111 vmx->pi_desc.sn = 1;
10112
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010113 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010114
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010115free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010116 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010117 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010118free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010119 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010120free_pml:
10121 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010122uninit_vcpu:
10123 kvm_vcpu_uninit(&vmx->vcpu);
10124free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010125 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010126 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010127 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010128}
10129
Wanpeng Lib31c1142018-03-12 04:53:04 -070010130static int vmx_vm_init(struct kvm *kvm)
10131{
10132 if (!ple_gap)
10133 kvm->arch.pause_in_guest = true;
10134 return 0;
10135}
10136
Yang, Sheng002c7f72007-07-31 14:23:01 +030010137static void __init vmx_check_processor_compat(void *rtn)
10138{
10139 struct vmcs_config vmcs_conf;
10140
10141 *(int *)rtn = 0;
10142 if (setup_vmcs_config(&vmcs_conf) < 0)
10143 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010144 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010145 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10146 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10147 smp_processor_id());
10148 *(int *)rtn = -EIO;
10149 }
10150}
10151
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010152static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010153{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010154 u8 cache;
10155 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010156
Sheng Yang522c68c2009-04-27 20:35:43 +080010157 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010158 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010159 * 2. EPT with VT-d:
10160 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010161 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010162 * b. VT-d with snooping control feature: snooping control feature of
10163 * VT-d engine can guarantee the cache correctness. Just set it
10164 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010165 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010166 * consistent with host MTRR
10167 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010168 if (is_mmio) {
10169 cache = MTRR_TYPE_UNCACHABLE;
10170 goto exit;
10171 }
10172
10173 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010174 ipat = VMX_EPT_IPAT_BIT;
10175 cache = MTRR_TYPE_WRBACK;
10176 goto exit;
10177 }
10178
10179 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10180 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010181 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010182 cache = MTRR_TYPE_WRBACK;
10183 else
10184 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010185 goto exit;
10186 }
10187
Xiao Guangrongff536042015-06-15 16:55:22 +080010188 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010189
10190exit:
10191 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010192}
10193
Sheng Yang17cc3932010-01-05 19:02:27 +080010194static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010195{
Sheng Yang878403b2010-01-05 19:02:29 +080010196 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10197 return PT_DIRECTORY_LEVEL;
10198 else
10199 /* For shadow and EPT supported 1GB page */
10200 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010201}
10202
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010203static void vmcs_set_secondary_exec_control(u32 new_ctl)
10204{
10205 /*
10206 * These bits in the secondary execution controls field
10207 * are dynamic, the others are mostly based on the hypervisor
10208 * architecture and the guest's CPUID. Do not touch the
10209 * dynamic bits.
10210 */
10211 u32 mask =
10212 SECONDARY_EXEC_SHADOW_VMCS |
10213 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010214 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10215 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010216
10217 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10218
10219 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10220 (new_ctl & ~mask) | (cur_ctl & mask));
10221}
10222
David Matlack8322ebb2016-11-29 18:14:09 -080010223/*
10224 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10225 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10226 */
10227static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10228{
10229 struct vcpu_vmx *vmx = to_vmx(vcpu);
10230 struct kvm_cpuid_entry2 *entry;
10231
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010232 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10233 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010234
10235#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10236 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010237 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010238} while (0)
10239
10240 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10241 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10242 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10243 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10244 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10245 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10246 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10247 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10248 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10249 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10250 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10251 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10252 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10253 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10254 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10255
10256 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10257 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10258 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10259 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10260 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010261 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010262
10263#undef cr4_fixed1_update
10264}
10265
Sheng Yang0e851882009-12-18 16:48:46 +080010266static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10267{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010268 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010269
Paolo Bonzini80154d72017-08-24 13:55:35 +020010270 if (cpu_has_secondary_exec_ctrls()) {
10271 vmx_compute_secondary_exec_control(vmx);
10272 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010273 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010274
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010275 if (nested_vmx_allowed(vcpu))
10276 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10277 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10278 else
10279 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10280 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010281
10282 if (nested_vmx_allowed(vcpu))
10283 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010284}
10285
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010286static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10287{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010288 if (func == 1 && nested)
10289 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010290}
10291
Yang Zhang25d92082013-08-06 12:00:32 +030010292static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10293 struct x86_exception *fault)
10294{
Jan Kiszka533558b2014-01-04 18:47:20 +010010295 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010296 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010297 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010298 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010299
Bandan Dasc5f983f2017-05-05 15:25:14 -040010300 if (vmx->nested.pml_full) {
10301 exit_reason = EXIT_REASON_PML_FULL;
10302 vmx->nested.pml_full = false;
10303 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10304 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010305 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010306 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010307 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010308
10309 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010310 vmcs12->guest_physical_address = fault->address;
10311}
10312
Peter Feiner995f00a2017-06-30 17:26:32 -070010313static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10314{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010315 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010316}
10317
Nadav Har'El155a97a2013-08-05 11:07:16 +030010318/* Callbacks for nested_ept_init_mmu_context: */
10319
10320static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10321{
10322 /* return the page table to be shadowed - in our case, EPT12 */
10323 return get_vmcs12(vcpu)->ept_pointer;
10324}
10325
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010326static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010327{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010328 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010329 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010330 return 1;
10331
10332 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010333 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010334 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010335 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010336 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010337 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10338 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10339 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10340
10341 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010342 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010343}
10344
10345static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10346{
10347 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10348}
10349
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010350static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10351 u16 error_code)
10352{
10353 bool inequality, bit;
10354
10355 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10356 inequality =
10357 (error_code & vmcs12->page_fault_error_code_mask) !=
10358 vmcs12->page_fault_error_code_match;
10359 return inequality ^ bit;
10360}
10361
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010362static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10363 struct x86_exception *fault)
10364{
10365 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10366
10367 WARN_ON(!is_guest_mode(vcpu));
10368
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010369 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10370 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010371 vmcs12->vm_exit_intr_error_code = fault->error_code;
10372 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10373 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10374 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10375 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010376 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010377 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010378 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010379}
10380
Paolo Bonzinic9923842017-12-13 14:16:30 +010010381static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10382 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010383
10384static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010385 struct vmcs12 *vmcs12)
10386{
10387 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010388 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010389 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010390
10391 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010392 /*
10393 * Translate L1 physical address to host physical
10394 * address for vmcs02. Keep the page pinned, so this
10395 * physical address remains valid. We keep a reference
10396 * to it so we can release it later.
10397 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010398 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010399 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010400 vmx->nested.apic_access_page = NULL;
10401 }
10402 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010403 /*
10404 * If translation failed, no matter: This feature asks
10405 * to exit when accessing the given address, and if it
10406 * can never be accessed, this feature won't do
10407 * anything anyway.
10408 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010409 if (!is_error_page(page)) {
10410 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010411 hpa = page_to_phys(vmx->nested.apic_access_page);
10412 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10413 } else {
10414 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10415 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10416 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010417 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010418
10419 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010420 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010421 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010422 vmx->nested.virtual_apic_page = NULL;
10423 }
10424 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010425
10426 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010427 * If translation failed, VM entry will fail because
10428 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10429 * Failing the vm entry is _not_ what the processor
10430 * does but it's basically the only possibility we
10431 * have. We could still enter the guest if CR8 load
10432 * exits are enabled, CR8 store exits are enabled, and
10433 * virtualize APIC access is disabled; in this case
10434 * the processor would never use the TPR shadow and we
10435 * could simply clear the bit from the execution
10436 * control. But such a configuration is useless, so
10437 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010438 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010439 if (!is_error_page(page)) {
10440 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010441 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10442 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10443 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010444 }
10445
Wincy Van705699a2015-02-03 23:58:17 +080010446 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010447 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10448 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010449 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010450 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010451 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010452 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10453 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010454 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010455 vmx->nested.pi_desc_page = page;
10456 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010457 vmx->nested.pi_desc =
10458 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10459 (unsigned long)(vmcs12->posted_intr_desc_addr &
10460 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010461 vmcs_write64(POSTED_INTR_DESC_ADDR,
10462 page_to_phys(vmx->nested.pi_desc_page) +
10463 (unsigned long)(vmcs12->posted_intr_desc_addr &
10464 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010465 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010466 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010467 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10468 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010469 else
10470 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10471 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010472}
10473
Jan Kiszkaf41245002014-03-07 20:03:13 +010010474static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10475{
10476 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10477 struct vcpu_vmx *vmx = to_vmx(vcpu);
10478
10479 if (vcpu->arch.virtual_tsc_khz == 0)
10480 return;
10481
10482 /* Make sure short timeouts reliably trigger an immediate vmexit.
10483 * hrtimer_start does not guarantee this. */
10484 if (preemption_timeout <= 1) {
10485 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10486 return;
10487 }
10488
10489 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10490 preemption_timeout *= 1000000;
10491 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10492 hrtimer_start(&vmx->nested.preemption_timer,
10493 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10494}
10495
Jim Mattson56a20512017-07-06 16:33:06 -070010496static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10497 struct vmcs12 *vmcs12)
10498{
10499 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10500 return 0;
10501
10502 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10503 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10504 return -EINVAL;
10505
10506 return 0;
10507}
10508
Wincy Van3af18d92015-02-03 23:49:31 +080010509static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10510 struct vmcs12 *vmcs12)
10511{
Wincy Van3af18d92015-02-03 23:49:31 +080010512 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10513 return 0;
10514
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010515 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010516 return -EINVAL;
10517
10518 return 0;
10519}
10520
Jim Mattson712b12d2017-08-24 13:24:47 -070010521static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10522 struct vmcs12 *vmcs12)
10523{
10524 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10525 return 0;
10526
10527 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10528 return -EINVAL;
10529
10530 return 0;
10531}
10532
Wincy Van3af18d92015-02-03 23:49:31 +080010533/*
10534 * Merge L0's and L1's MSR bitmap, return false to indicate that
10535 * we do not use the hardware.
10536 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010537static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10538 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010539{
Wincy Van82f0dd42015-02-03 23:57:18 +080010540 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010541 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010542 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010543 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010544 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010545 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010546 *
10547 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10548 * ensures that we do not accidentally generate an L02 MSR bitmap
10549 * from the L12 MSR bitmap that is too permissive.
10550 * 2. That L1 or L2s have actually used the MSR. This avoids
10551 * unnecessarily merging of the bitmap if the MSR is unused. This
10552 * works properly because we only update the L01 MSR bitmap lazily.
10553 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10554 * updated to reflect this when L1 (or its L2s) actually write to
10555 * the MSR.
10556 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010557 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10558 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010559
Paolo Bonzinic9923842017-12-13 14:16:30 +010010560 /* Nothing to do if the MSR bitmap is not in use. */
10561 if (!cpu_has_vmx_msr_bitmap() ||
10562 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10563 return false;
10564
Ashok Raj15d45072018-02-01 22:59:43 +010010565 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010566 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010567 return false;
10568
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010569 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10570 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010571 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010572
Radim Krčmářd048c092016-08-08 20:16:22 +020010573 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010574 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10575 /*
10576 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10577 * just lets the processor take the value from the virtual-APIC page;
10578 * take those 256 bits directly from the L1 bitmap.
10579 */
10580 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10581 unsigned word = msr / BITS_PER_LONG;
10582 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10583 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010584 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010585 } else {
10586 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10587 unsigned word = msr / BITS_PER_LONG;
10588 msr_bitmap_l0[word] = ~0;
10589 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10590 }
10591 }
10592
10593 nested_vmx_disable_intercept_for_msr(
10594 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010595 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010596 MSR_TYPE_W);
10597
10598 if (nested_cpu_has_vid(vmcs12)) {
10599 nested_vmx_disable_intercept_for_msr(
10600 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010601 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010602 MSR_TYPE_W);
10603 nested_vmx_disable_intercept_for_msr(
10604 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010605 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010606 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010607 }
Ashok Raj15d45072018-02-01 22:59:43 +010010608
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010609 if (spec_ctrl)
10610 nested_vmx_disable_intercept_for_msr(
10611 msr_bitmap_l1, msr_bitmap_l0,
10612 MSR_IA32_SPEC_CTRL,
10613 MSR_TYPE_R | MSR_TYPE_W);
10614
Ashok Raj15d45072018-02-01 22:59:43 +010010615 if (pred_cmd)
10616 nested_vmx_disable_intercept_for_msr(
10617 msr_bitmap_l1, msr_bitmap_l0,
10618 MSR_IA32_PRED_CMD,
10619 MSR_TYPE_W);
10620
Wincy Vanf2b93282015-02-03 23:56:03 +080010621 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010622 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010623
10624 return true;
10625}
10626
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010627static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10628 struct vmcs12 *vmcs12)
10629{
10630 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10631 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10632 return -EINVAL;
10633 else
10634 return 0;
10635}
10636
Wincy Vanf2b93282015-02-03 23:56:03 +080010637static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10638 struct vmcs12 *vmcs12)
10639{
Wincy Van82f0dd42015-02-03 23:57:18 +080010640 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010641 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010642 !nested_cpu_has_vid(vmcs12) &&
10643 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010644 return 0;
10645
10646 /*
10647 * If virtualize x2apic mode is enabled,
10648 * virtualize apic access must be disabled.
10649 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010650 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10651 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010652 return -EINVAL;
10653
Wincy Van608406e2015-02-03 23:57:51 +080010654 /*
10655 * If virtual interrupt delivery is enabled,
10656 * we must exit on external interrupts.
10657 */
10658 if (nested_cpu_has_vid(vmcs12) &&
10659 !nested_exit_on_intr(vcpu))
10660 return -EINVAL;
10661
Wincy Van705699a2015-02-03 23:58:17 +080010662 /*
10663 * bits 15:8 should be zero in posted_intr_nv,
10664 * the descriptor address has been already checked
10665 * in nested_get_vmcs12_pages.
10666 */
10667 if (nested_cpu_has_posted_intr(vmcs12) &&
10668 (!nested_cpu_has_vid(vmcs12) ||
10669 !nested_exit_intr_ack_set(vcpu) ||
10670 vmcs12->posted_intr_nv & 0xff00))
10671 return -EINVAL;
10672
Wincy Vanf2b93282015-02-03 23:56:03 +080010673 /* tpr shadow is needed by all apicv features. */
10674 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10675 return -EINVAL;
10676
10677 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010678}
10679
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010680static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10681 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010682 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010683{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010684 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010685 u64 count, addr;
10686
10687 if (vmcs12_read_any(vcpu, count_field, &count) ||
10688 vmcs12_read_any(vcpu, addr_field, &addr)) {
10689 WARN_ON(1);
10690 return -EINVAL;
10691 }
10692 if (count == 0)
10693 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010694 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010695 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10696 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010697 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010698 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10699 addr_field, maxphyaddr, count, addr);
10700 return -EINVAL;
10701 }
10702 return 0;
10703}
10704
10705static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10706 struct vmcs12 *vmcs12)
10707{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010708 if (vmcs12->vm_exit_msr_load_count == 0 &&
10709 vmcs12->vm_exit_msr_store_count == 0 &&
10710 vmcs12->vm_entry_msr_load_count == 0)
10711 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010712 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010713 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010714 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010715 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010716 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010717 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010718 return -EINVAL;
10719 return 0;
10720}
10721
Bandan Dasc5f983f2017-05-05 15:25:14 -040010722static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10723 struct vmcs12 *vmcs12)
10724{
10725 u64 address = vmcs12->pml_address;
10726 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10727
10728 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10729 if (!nested_cpu_has_ept(vmcs12) ||
10730 !IS_ALIGNED(address, 4096) ||
10731 address >> maxphyaddr)
10732 return -EINVAL;
10733 }
10734
10735 return 0;
10736}
10737
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010738static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10739 struct vmx_msr_entry *e)
10740{
10741 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010742 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010743 return -EINVAL;
10744 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10745 e->index == MSR_IA32_UCODE_REV)
10746 return -EINVAL;
10747 if (e->reserved != 0)
10748 return -EINVAL;
10749 return 0;
10750}
10751
10752static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10753 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010754{
10755 if (e->index == MSR_FS_BASE ||
10756 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010757 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10758 nested_vmx_msr_check_common(vcpu, e))
10759 return -EINVAL;
10760 return 0;
10761}
10762
10763static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10764 struct vmx_msr_entry *e)
10765{
10766 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10767 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010768 return -EINVAL;
10769 return 0;
10770}
10771
10772/*
10773 * Load guest's/host's msr at nested entry/exit.
10774 * return 0 for success, entry index for failure.
10775 */
10776static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10777{
10778 u32 i;
10779 struct vmx_msr_entry e;
10780 struct msr_data msr;
10781
10782 msr.host_initiated = false;
10783 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010784 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10785 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010786 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010787 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10788 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010789 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010790 }
10791 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010792 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010793 "%s check failed (%u, 0x%x, 0x%x)\n",
10794 __func__, i, e.index, e.reserved);
10795 goto fail;
10796 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010797 msr.index = e.index;
10798 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010799 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010800 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010801 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10802 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010803 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010804 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010805 }
10806 return 0;
10807fail:
10808 return i + 1;
10809}
10810
10811static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10812{
10813 u32 i;
10814 struct vmx_msr_entry e;
10815
10816 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010817 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010818 if (kvm_vcpu_read_guest(vcpu,
10819 gpa + i * sizeof(e),
10820 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010821 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010822 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10823 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010824 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010825 }
10826 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010827 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010828 "%s check failed (%u, 0x%x, 0x%x)\n",
10829 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010830 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010831 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010832 msr_info.host_initiated = false;
10833 msr_info.index = e.index;
10834 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010835 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010836 "%s cannot read MSR (%u, 0x%x)\n",
10837 __func__, i, e.index);
10838 return -EINVAL;
10839 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010840 if (kvm_vcpu_write_guest(vcpu,
10841 gpa + i * sizeof(e) +
10842 offsetof(struct vmx_msr_entry, value),
10843 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010844 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010845 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010846 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010847 return -EINVAL;
10848 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010849 }
10850 return 0;
10851}
10852
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010853static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10854{
10855 unsigned long invalid_mask;
10856
10857 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10858 return (val & invalid_mask) == 0;
10859}
10860
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010861/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010862 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10863 * emulating VM entry into a guest with EPT enabled.
10864 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10865 * is assigned to entry_failure_code on failure.
10866 */
10867static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010868 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010869{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010870 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010871 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010872 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10873 return 1;
10874 }
10875
10876 /*
10877 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10878 * must not be dereferenced.
10879 */
10880 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10881 !nested_ept) {
10882 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10883 *entry_failure_code = ENTRY_FAIL_PDPTE;
10884 return 1;
10885 }
10886 }
10887
10888 vcpu->arch.cr3 = cr3;
10889 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10890 }
10891
10892 kvm_mmu_reset_context(vcpu);
10893 return 0;
10894}
10895
Jim Mattson6514dc32018-04-26 16:09:12 -070010896static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010897{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010898 struct vcpu_vmx *vmx = to_vmx(vcpu);
10899
10900 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10901 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10902 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10903 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10904 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10905 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10906 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10907 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10908 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10909 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10910 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10911 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10912 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10913 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10914 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10915 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10916 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10917 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10918 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10919 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10920 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10921 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10922 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10923 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10924 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10925 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10926 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10927 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10928 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10929 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10930 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010931
10932 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10933 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10934 vmcs12->guest_pending_dbg_exceptions);
10935 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10936 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10937
10938 if (nested_cpu_has_xsaves(vmcs12))
10939 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10940 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10941
10942 if (cpu_has_vmx_posted_intr())
10943 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10944
10945 /*
10946 * Whether page-faults are trapped is determined by a combination of
10947 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10948 * If enable_ept, L0 doesn't care about page faults and we should
10949 * set all of these to L1's desires. However, if !enable_ept, L0 does
10950 * care about (at least some) page faults, and because it is not easy
10951 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10952 * to exit on each and every L2 page fault. This is done by setting
10953 * MASK=MATCH=0 and (see below) EB.PF=1.
10954 * Note that below we don't need special code to set EB.PF beyond the
10955 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10956 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10957 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10958 */
10959 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10960 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10961 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10962 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10963
10964 /* All VMFUNCs are currently emulated through L0 vmexits. */
10965 if (cpu_has_vmx_vmfunc())
10966 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10967
10968 if (cpu_has_vmx_apicv()) {
10969 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10970 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10971 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10972 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10973 }
10974
10975 /*
10976 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10977 * Some constant fields are set here by vmx_set_constant_host_state().
10978 * Other fields are different per CPU, and will be set later when
10979 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10980 */
10981 vmx_set_constant_host_state(vmx);
10982
10983 /*
10984 * Set the MSR load/store lists to match L0's settings.
10985 */
10986 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10987 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10988 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10989 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10990 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10991
10992 set_cr4_guest_host_mask(vmx);
10993
10994 if (vmx_mpx_supported())
10995 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10996
10997 if (enable_vpid) {
10998 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10999 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11000 else
11001 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11002 }
11003
11004 /*
11005 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11006 */
11007 if (enable_ept) {
11008 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11009 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11010 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11011 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11012 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011013
11014 if (cpu_has_vmx_msr_bitmap())
11015 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011016}
11017
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011018/*
11019 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11020 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011021 * 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 +030011022 * guest in a way that will both be appropriate to L1's requests, and our
11023 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11024 * function also has additional necessary side-effects, like setting various
11025 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011026 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11027 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011028 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011029static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011030 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011031{
11032 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011033 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011034
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011035 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011036 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011037 vmx->nested.dirty_vmcs12 = false;
11038 }
11039
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011040 /*
11041 * First, the fields that are shadowed. This must be kept in sync
11042 * with vmx_shadow_fields.h.
11043 */
11044
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011045 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011046 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011047 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011048 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11049 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011050
11051 /*
11052 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11053 * HOST_FS_BASE, HOST_GS_BASE.
11054 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011055
Jim Mattson6514dc32018-04-26 16:09:12 -070011056 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011057 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011058 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11059 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11060 } else {
11061 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11062 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11063 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011064 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011065 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11066 vmcs12->vm_entry_intr_info_field);
11067 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11068 vmcs12->vm_entry_exception_error_code);
11069 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11070 vmcs12->vm_entry_instruction_len);
11071 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11072 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011073 vmx->loaded_vmcs->nmi_known_unmasked =
11074 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011075 } else {
11076 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11077 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011078 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011079
Jan Kiszkaf41245002014-03-07 20:03:13 +010011080 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011081
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011082 /* Preemption timer setting is only taken from vmcs01. */
11083 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11084 exec_control |= vmcs_config.pin_based_exec_ctrl;
11085 if (vmx->hv_deadline_tsc == -1)
11086 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11087
11088 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011089 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011090 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11091 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011092 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011093 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011094 }
Wincy Van705699a2015-02-03 23:58:17 +080011095
Jan Kiszkaf41245002014-03-07 20:03:13 +010011096 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011097
Jan Kiszkaf41245002014-03-07 20:03:13 +010011098 vmx->nested.preemption_timer_expired = false;
11099 if (nested_cpu_has_preemption_timer(vmcs12))
11100 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011101
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011102 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011103 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011104
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011105 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011106 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011107 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011108 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011109 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011110 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011111 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11112 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011113 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011114 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11115 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11116 ~SECONDARY_EXEC_ENABLE_PML;
11117 exec_control |= vmcs12_exec_ctrl;
11118 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011119
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011120 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011121 vmcs_write16(GUEST_INTR_STATUS,
11122 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011123
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011124 /*
11125 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11126 * nested_get_vmcs12_pages will either fix it up or
11127 * remove the VM execution control.
11128 */
11129 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11130 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11131
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011132 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11133 }
11134
Jim Mattson83bafef2016-10-04 10:48:38 -070011135 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011136 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11137 * entry, but only if the current (host) sp changed from the value
11138 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11139 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11140 * here we just force the write to happen on entry.
11141 */
11142 vmx->host_rsp = 0;
11143
11144 exec_control = vmx_exec_control(vmx); /* L0's desires */
11145 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11146 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11147 exec_control &= ~CPU_BASED_TPR_SHADOW;
11148 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011149
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011150 /*
11151 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11152 * nested_get_vmcs12_pages can't fix it up, the illegal value
11153 * will result in a VM entry failure.
11154 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011155 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011156 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011157 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011158 } else {
11159#ifdef CONFIG_X86_64
11160 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11161 CPU_BASED_CR8_STORE_EXITING;
11162#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011163 }
11164
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011165 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011166 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11167 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011168 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011169 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11170 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11171
11172 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11173
11174 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11175 * bitwise-or of what L1 wants to trap for L2, and what we want to
11176 * trap. Note that CR0.TS also needs updating - we do this later.
11177 */
11178 update_exception_bitmap(vcpu);
11179 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11180 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11181
Nadav Har'El8049d652013-08-05 11:07:06 +030011182 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11183 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11184 * bits are further modified by vmx_set_efer() below.
11185 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011186 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011187
11188 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11189 * emulated by vmx_set_efer(), below.
11190 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011191 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011192 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11193 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011194 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11195
Jim Mattson6514dc32018-04-26 16:09:12 -070011196 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011197 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011198 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011199 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011200 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011201 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011202 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011203
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011204 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11205
Peter Feinerc95ba922016-08-17 09:36:47 -070011206 if (kvm_has_tsc_control)
11207 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011208
11209 if (enable_vpid) {
11210 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011211 * There is no direct mapping between vpid02 and vpid12, the
11212 * vpid02 is per-vCPU for L0 and reused while the value of
11213 * vpid12 is changed w/ one invvpid during nested vmentry.
11214 * The vpid12 is allocated by L1 for L2, so it will not
11215 * influence global bitmap(for vpid01 and vpid02 allocation)
11216 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011217 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011218 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011219 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11220 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011221 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011222 }
11223 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011224 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011225 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011226 }
11227
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011228 if (enable_pml) {
11229 /*
11230 * Conceptually we want to copy the PML address and index from
11231 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11232 * since we always flush the log on each vmexit, this happens
11233 * to be equivalent to simply resetting the fields in vmcs02.
11234 */
11235 ASSERT(vmx->pml_pg);
11236 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11237 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11238 }
11239
Nadav Har'El155a97a2013-08-05 11:07:16 +030011240 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011241 if (nested_ept_init_mmu_context(vcpu)) {
11242 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11243 return 1;
11244 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011245 } else if (nested_cpu_has2(vmcs12,
11246 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011247 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011248 }
11249
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011250 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011251 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11252 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011253 * The CR0_READ_SHADOW is what L2 should have expected to read given
11254 * the specifications by L1; It's not enough to take
11255 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11256 * have more bits than L1 expected.
11257 */
11258 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11259 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11260
11261 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11262 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11263
Jim Mattson6514dc32018-04-26 16:09:12 -070011264 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011265 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011266 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11267 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11268 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11269 else
11270 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11271 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11272 vmx_set_efer(vcpu, vcpu->arch.efer);
11273
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011274 /*
11275 * Guest state is invalid and unrestricted guest is disabled,
11276 * which means L1 attempted VMEntry to L2 with invalid state.
11277 * Fail the VMEntry.
11278 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011279 if (vmx->emulation_required) {
11280 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011281 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011282 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011283
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011284 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011285 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011286 entry_failure_code))
11287 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011288
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011289 if (!enable_ept)
11290 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11291
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011292 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11293 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011294 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011295}
11296
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011297static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11298{
11299 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11300 nested_cpu_has_virtual_nmis(vmcs12))
11301 return -EINVAL;
11302
11303 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11304 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11305 return -EINVAL;
11306
11307 return 0;
11308}
11309
Jim Mattsonca0bde22016-11-30 12:03:46 -080011310static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11311{
11312 struct vcpu_vmx *vmx = to_vmx(vcpu);
11313
11314 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11315 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11316 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11317
Jim Mattson56a20512017-07-06 16:33:06 -070011318 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11319 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11320
Jim Mattsonca0bde22016-11-30 12:03:46 -080011321 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11322 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11323
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011324 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11325 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11326
Jim Mattson712b12d2017-08-24 13:24:47 -070011327 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11328 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11329
Jim Mattsonca0bde22016-11-30 12:03:46 -080011330 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11331 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11332
11333 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11334 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11335
Bandan Dasc5f983f2017-05-05 15:25:14 -040011336 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11337 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11338
Jim Mattsonca0bde22016-11-30 12:03:46 -080011339 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011340 vmx->nested.msrs.procbased_ctls_low,
11341 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011342 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11343 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011344 vmx->nested.msrs.secondary_ctls_low,
11345 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011346 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011347 vmx->nested.msrs.pinbased_ctls_low,
11348 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011349 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011350 vmx->nested.msrs.exit_ctls_low,
11351 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011352 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011353 vmx->nested.msrs.entry_ctls_low,
11354 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011355 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11356
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011357 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011358 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11359
Bandan Das41ab9372017-08-03 15:54:43 -040011360 if (nested_cpu_has_vmfunc(vmcs12)) {
11361 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011362 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011363 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11364
11365 if (nested_cpu_has_eptp_switching(vmcs12)) {
11366 if (!nested_cpu_has_ept(vmcs12) ||
11367 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11368 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11369 }
11370 }
Bandan Das27c42a12017-08-03 15:54:42 -040011371
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011372 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11373 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11374
Jim Mattsonca0bde22016-11-30 12:03:46 -080011375 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11376 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11377 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11378 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11379
11380 return 0;
11381}
11382
11383static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11384 u32 *exit_qual)
11385{
11386 bool ia32e;
11387
11388 *exit_qual = ENTRY_FAIL_DEFAULT;
11389
11390 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11391 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11392 return 1;
11393
11394 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11395 vmcs12->vmcs_link_pointer != -1ull) {
11396 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11397 return 1;
11398 }
11399
11400 /*
11401 * If the load IA32_EFER VM-entry control is 1, the following checks
11402 * are performed on the field for the IA32_EFER MSR:
11403 * - Bits reserved in the IA32_EFER MSR must be 0.
11404 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11405 * the IA-32e mode guest VM-exit control. It must also be identical
11406 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11407 * CR0.PG) is 1.
11408 */
11409 if (to_vmx(vcpu)->nested.nested_run_pending &&
11410 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11411 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11412 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11413 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11414 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11415 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11416 return 1;
11417 }
11418
11419 /*
11420 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11421 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11422 * the values of the LMA and LME bits in the field must each be that of
11423 * the host address-space size VM-exit control.
11424 */
11425 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11426 ia32e = (vmcs12->vm_exit_controls &
11427 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11428 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11429 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11430 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11431 return 1;
11432 }
11433
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011434 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11435 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11436 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11437 return 1;
11438
Jim Mattsonca0bde22016-11-30 12:03:46 -080011439 return 0;
11440}
11441
Jim Mattson6514dc32018-04-26 16:09:12 -070011442static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011443{
11444 struct vcpu_vmx *vmx = to_vmx(vcpu);
11445 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011446 u32 msr_entry_idx;
11447 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011448 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011449
Jim Mattson858e25c2016-11-30 12:03:47 -080011450 enter_guest_mode(vcpu);
11451
11452 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11453 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11454
Jim Mattsonde3a0022017-11-27 17:22:25 -060011455 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011456 vmx_segment_cache_clear(vmx);
11457
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011458 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11459 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11460
11461 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011462 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011463 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011464
11465 nested_get_vmcs12_pages(vcpu, vmcs12);
11466
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011467 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011468 msr_entry_idx = nested_vmx_load_msr(vcpu,
11469 vmcs12->vm_entry_msr_load_addr,
11470 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011471 if (msr_entry_idx)
11472 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011473
Jim Mattson858e25c2016-11-30 12:03:47 -080011474 /*
11475 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11476 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11477 * returned as far as L1 is concerned. It will only return (and set
11478 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11479 */
11480 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011481
11482fail:
11483 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11484 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11485 leave_guest_mode(vcpu);
11486 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11487 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11488 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011489}
11490
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011491/*
11492 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11493 * for running an L2 nested guest.
11494 */
11495static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11496{
11497 struct vmcs12 *vmcs12;
11498 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011499 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011500 u32 exit_qual;
11501 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011502
Kyle Hueyeb277562016-11-29 12:40:39 -080011503 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011504 return 1;
11505
Kyle Hueyeb277562016-11-29 12:40:39 -080011506 if (!nested_vmx_check_vmcs12(vcpu))
11507 goto out;
11508
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011509 vmcs12 = get_vmcs12(vcpu);
11510
Abel Gordon012f83c2013-04-18 14:39:25 +030011511 if (enable_shadow_vmcs)
11512 copy_shadow_to_vmcs12(vmx);
11513
Nadav Har'El7c177932011-05-25 23:12:04 +030011514 /*
11515 * The nested entry process starts with enforcing various prerequisites
11516 * on vmcs12 as required by the Intel SDM, and act appropriately when
11517 * they fail: As the SDM explains, some conditions should cause the
11518 * instruction to fail, while others will cause the instruction to seem
11519 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11520 * To speed up the normal (success) code path, we should avoid checking
11521 * for misconfigurations which will anyway be caught by the processor
11522 * when using the merged vmcs02.
11523 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011524 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11525 nested_vmx_failValid(vcpu,
11526 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11527 goto out;
11528 }
11529
Nadav Har'El7c177932011-05-25 23:12:04 +030011530 if (vmcs12->launch_state == launch) {
11531 nested_vmx_failValid(vcpu,
11532 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11533 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011534 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011535 }
11536
Jim Mattsonca0bde22016-11-30 12:03:46 -080011537 ret = check_vmentry_prereqs(vcpu, vmcs12);
11538 if (ret) {
11539 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011540 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011541 }
11542
Nadav Har'El7c177932011-05-25 23:12:04 +030011543 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011544 * After this point, the trap flag no longer triggers a singlestep trap
11545 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11546 * This is not 100% correct; for performance reasons, we delegate most
11547 * of the checks on host state to the processor. If those fail,
11548 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011549 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011550 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011551
Jim Mattsonca0bde22016-11-30 12:03:46 -080011552 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11553 if (ret) {
11554 nested_vmx_entry_failure(vcpu, vmcs12,
11555 EXIT_REASON_INVALID_STATE, exit_qual);
11556 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011557 }
11558
11559 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011560 * We're finally done with prerequisite checking, and can start with
11561 * the nested entry.
11562 */
11563
Jim Mattson6514dc32018-04-26 16:09:12 -070011564 vmx->nested.nested_run_pending = 1;
11565 ret = enter_vmx_non_root_mode(vcpu);
11566 if (ret) {
11567 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011568 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011569 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011570
Chao Gao135a06c2018-02-11 10:06:30 +080011571 /*
11572 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11573 * by event injection, halt vcpu.
11574 */
11575 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011576 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11577 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011578 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011579 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011580 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011581
11582out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011583 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011584}
11585
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011586/*
11587 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11588 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11589 * This function returns the new value we should put in vmcs12.guest_cr0.
11590 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11591 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11592 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11593 * didn't trap the bit, because if L1 did, so would L0).
11594 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11595 * been modified by L2, and L1 knows it. So just leave the old value of
11596 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11597 * isn't relevant, because if L0 traps this bit it can set it to anything.
11598 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11599 * changed these bits, and therefore they need to be updated, but L0
11600 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11601 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11602 */
11603static inline unsigned long
11604vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11605{
11606 return
11607 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11608 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11609 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11610 vcpu->arch.cr0_guest_owned_bits));
11611}
11612
11613static inline unsigned long
11614vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11615{
11616 return
11617 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11618 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11619 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11620 vcpu->arch.cr4_guest_owned_bits));
11621}
11622
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011623static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11624 struct vmcs12 *vmcs12)
11625{
11626 u32 idt_vectoring;
11627 unsigned int nr;
11628
Wanpeng Li664f8e22017-08-24 03:35:09 -070011629 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011630 nr = vcpu->arch.exception.nr;
11631 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11632
11633 if (kvm_exception_is_soft(nr)) {
11634 vmcs12->vm_exit_instruction_len =
11635 vcpu->arch.event_exit_inst_len;
11636 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11637 } else
11638 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11639
11640 if (vcpu->arch.exception.has_error_code) {
11641 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11642 vmcs12->idt_vectoring_error_code =
11643 vcpu->arch.exception.error_code;
11644 }
11645
11646 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011647 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011648 vmcs12->idt_vectoring_info_field =
11649 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011650 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011651 nr = vcpu->arch.interrupt.nr;
11652 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11653
11654 if (vcpu->arch.interrupt.soft) {
11655 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11656 vmcs12->vm_entry_instruction_len =
11657 vcpu->arch.event_exit_inst_len;
11658 } else
11659 idt_vectoring |= INTR_TYPE_EXT_INTR;
11660
11661 vmcs12->idt_vectoring_info_field = idt_vectoring;
11662 }
11663}
11664
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011665static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11666{
11667 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011668 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011669 bool block_nested_events =
11670 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011671
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011672 if (vcpu->arch.exception.pending &&
11673 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011674 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011675 return -EBUSY;
11676 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011677 return 0;
11678 }
11679
Jan Kiszkaf41245002014-03-07 20:03:13 +010011680 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11681 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011682 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011683 return -EBUSY;
11684 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11685 return 0;
11686 }
11687
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011688 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011689 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011690 return -EBUSY;
11691 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11692 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11693 INTR_INFO_VALID_MASK, 0);
11694 /*
11695 * The NMI-triggered VM exit counts as injection:
11696 * clear this one and block further NMIs.
11697 */
11698 vcpu->arch.nmi_pending = 0;
11699 vmx_set_nmi_mask(vcpu, true);
11700 return 0;
11701 }
11702
11703 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11704 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011705 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011706 return -EBUSY;
11707 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011708 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011709 }
11710
David Hildenbrand6342c502017-01-25 11:58:58 +010011711 vmx_complete_nested_posted_interrupt(vcpu);
11712 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011713}
11714
Jan Kiszkaf41245002014-03-07 20:03:13 +010011715static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11716{
11717 ktime_t remaining =
11718 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11719 u64 value;
11720
11721 if (ktime_to_ns(remaining) <= 0)
11722 return 0;
11723
11724 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11725 do_div(value, 1000000);
11726 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11727}
11728
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011729/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011730 * Update the guest state fields of vmcs12 to reflect changes that
11731 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11732 * VM-entry controls is also updated, since this is really a guest
11733 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011734 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011735static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011736{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011737 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11738 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11739
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011740 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11741 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11742 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11743
11744 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11745 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11746 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11747 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11748 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11749 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11750 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11751 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11752 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11753 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11754 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11755 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11756 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11757 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11758 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11759 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11760 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11761 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11762 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11763 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11764 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11765 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11766 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11767 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11768 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11769 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11770 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11771 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11772 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11773 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11774 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11775 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11776 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11777 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11778 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11779 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11780
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011781 vmcs12->guest_interruptibility_info =
11782 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11783 vmcs12->guest_pending_dbg_exceptions =
11784 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011785 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11786 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11787 else
11788 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011789
Jan Kiszkaf41245002014-03-07 20:03:13 +010011790 if (nested_cpu_has_preemption_timer(vmcs12)) {
11791 if (vmcs12->vm_exit_controls &
11792 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11793 vmcs12->vmx_preemption_timer_value =
11794 vmx_get_preemption_timer_value(vcpu);
11795 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11796 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011797
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011798 /*
11799 * In some cases (usually, nested EPT), L2 is allowed to change its
11800 * own CR3 without exiting. If it has changed it, we must keep it.
11801 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11802 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11803 *
11804 * Additionally, restore L2's PDPTR to vmcs12.
11805 */
11806 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011807 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011808 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11809 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11810 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11811 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11812 }
11813
Jim Mattsond281e132017-06-01 12:44:46 -070011814 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011815
Wincy Van608406e2015-02-03 23:57:51 +080011816 if (nested_cpu_has_vid(vmcs12))
11817 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11818
Jan Kiszkac18911a2013-03-13 16:06:41 +010011819 vmcs12->vm_entry_controls =
11820 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011821 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011822
Jan Kiszka2996fca2014-06-16 13:59:43 +020011823 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11824 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11825 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11826 }
11827
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011828 /* TODO: These cannot have changed unless we have MSR bitmaps and
11829 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011830 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011831 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011832 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11833 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011834 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11835 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11836 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011837 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011838 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011839}
11840
11841/*
11842 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11843 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11844 * and this function updates it to reflect the changes to the guest state while
11845 * L2 was running (and perhaps made some exits which were handled directly by L0
11846 * without going back to L1), and to reflect the exit reason.
11847 * Note that we do not have to copy here all VMCS fields, just those that
11848 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11849 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11850 * which already writes to vmcs12 directly.
11851 */
11852static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11853 u32 exit_reason, u32 exit_intr_info,
11854 unsigned long exit_qualification)
11855{
11856 /* update guest state fields: */
11857 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011858
11859 /* update exit information fields: */
11860
Jan Kiszka533558b2014-01-04 18:47:20 +010011861 vmcs12->vm_exit_reason = exit_reason;
11862 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011863 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011864
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011865 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011866 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11867 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11868
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011869 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011870 vmcs12->launch_state = 1;
11871
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011872 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11873 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011874 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011875
11876 /*
11877 * Transfer the event that L0 or L1 may wanted to inject into
11878 * L2 to IDT_VECTORING_INFO_FIELD.
11879 */
11880 vmcs12_save_pending_event(vcpu, vmcs12);
11881 }
11882
11883 /*
11884 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11885 * preserved above and would only end up incorrectly in L1.
11886 */
11887 vcpu->arch.nmi_injected = false;
11888 kvm_clear_exception_queue(vcpu);
11889 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011890}
11891
Wanpeng Li5af41572017-11-05 16:54:49 -080011892static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11893 struct vmcs12 *vmcs12)
11894{
11895 u32 entry_failure_code;
11896
11897 nested_ept_uninit_mmu_context(vcpu);
11898
11899 /*
11900 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11901 * couldn't have changed.
11902 */
11903 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11904 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11905
11906 if (!enable_ept)
11907 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11908}
11909
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011910/*
11911 * A part of what we need to when the nested L2 guest exits and we want to
11912 * run its L1 parent, is to reset L1's guest state to the host state specified
11913 * in vmcs12.
11914 * This function is to be called not only on normal nested exit, but also on
11915 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11916 * Failures During or After Loading Guest State").
11917 * This function should be called when the active VMCS is L1's (vmcs01).
11918 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011919static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11920 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011921{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011922 struct kvm_segment seg;
11923
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011924 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11925 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011926 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011927 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11928 else
11929 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11930 vmx_set_efer(vcpu, vcpu->arch.efer);
11931
11932 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11933 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011934 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011935 /*
11936 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011937 * actually changed, because vmx_set_cr0 refers to efer set above.
11938 *
11939 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11940 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011941 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011942 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011943 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011944
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011945 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011946 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011947 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011948
Wanpeng Li5af41572017-11-05 16:54:49 -080011949 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011950
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011951 if (enable_vpid) {
11952 /*
11953 * Trivially support vpid by letting L2s share their parent
11954 * L1's vpid. TODO: move to a more elaborate solution, giving
11955 * each L2 its own vpid and exposing the vpid feature to L1.
11956 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011957 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011958 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011959
11960 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11961 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11962 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11963 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11964 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011965 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11966 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011967
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011968 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11969 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11970 vmcs_write64(GUEST_BNDCFGS, 0);
11971
Jan Kiszka44811c02013-08-04 17:17:27 +020011972 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011973 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011974 vcpu->arch.pat = vmcs12->host_ia32_pat;
11975 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011976 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11977 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11978 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011979
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011980 /* Set L1 segment info according to Intel SDM
11981 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11982 seg = (struct kvm_segment) {
11983 .base = 0,
11984 .limit = 0xFFFFFFFF,
11985 .selector = vmcs12->host_cs_selector,
11986 .type = 11,
11987 .present = 1,
11988 .s = 1,
11989 .g = 1
11990 };
11991 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11992 seg.l = 1;
11993 else
11994 seg.db = 1;
11995 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11996 seg = (struct kvm_segment) {
11997 .base = 0,
11998 .limit = 0xFFFFFFFF,
11999 .type = 3,
12000 .present = 1,
12001 .s = 1,
12002 .db = 1,
12003 .g = 1
12004 };
12005 seg.selector = vmcs12->host_ds_selector;
12006 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12007 seg.selector = vmcs12->host_es_selector;
12008 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12009 seg.selector = vmcs12->host_ss_selector;
12010 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12011 seg.selector = vmcs12->host_fs_selector;
12012 seg.base = vmcs12->host_fs_base;
12013 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12014 seg.selector = vmcs12->host_gs_selector;
12015 seg.base = vmcs12->host_gs_base;
12016 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12017 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012018 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012019 .limit = 0x67,
12020 .selector = vmcs12->host_tr_selector,
12021 .type = 11,
12022 .present = 1
12023 };
12024 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12025
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012026 kvm_set_dr(vcpu, 7, 0x400);
12027 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012028
Wincy Van3af18d92015-02-03 23:49:31 +080012029 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012030 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012031
Wincy Vanff651cb2014-12-11 08:52:58 +030012032 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12033 vmcs12->vm_exit_msr_load_count))
12034 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012035}
12036
12037/*
12038 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12039 * and modify vmcs12 to make it see what it would expect to see there if
12040 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12041 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012042static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12043 u32 exit_intr_info,
12044 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012045{
12046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012047 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12048
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012049 /* trying to cancel vmlaunch/vmresume is a bug */
12050 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12051
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012052 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012053 * The only expected VM-instruction error is "VM entry with
12054 * invalid control field(s)." Anything else indicates a
12055 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012056 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012057 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12058 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12059
12060 leave_guest_mode(vcpu);
12061
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012062 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12063 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12064
Jim Mattson4f350c62017-09-14 16:31:44 -070012065 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012066 if (exit_reason == -1)
12067 sync_vmcs12(vcpu, vmcs12);
12068 else
12069 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12070 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012071
12072 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12073 vmcs12->vm_exit_msr_store_count))
12074 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012075 }
12076
Jim Mattson4f350c62017-09-14 16:31:44 -070012077 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012078 vm_entry_controls_reset_shadow(vmx);
12079 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012080 vmx_segment_cache_clear(vmx);
12081
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012082 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012083 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12084 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012085 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012086 if (vmx->hv_deadline_tsc == -1)
12087 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12088 PIN_BASED_VMX_PREEMPTION_TIMER);
12089 else
12090 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12091 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012092 if (kvm_has_tsc_control)
12093 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012094
Jim Mattson8d860bb2018-05-09 16:56:05 -040012095 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12096 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12097 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012098 } else if (!nested_cpu_has_ept(vmcs12) &&
12099 nested_cpu_has2(vmcs12,
12100 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012101 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012102 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012103
12104 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12105 vmx->host_rsp = 0;
12106
12107 /* Unpin physical memory we referred to in vmcs02 */
12108 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012109 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012110 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012111 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012112 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012113 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012114 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012115 }
Wincy Van705699a2015-02-03 23:58:17 +080012116 if (vmx->nested.pi_desc_page) {
12117 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012118 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012119 vmx->nested.pi_desc_page = NULL;
12120 vmx->nested.pi_desc = NULL;
12121 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012122
12123 /*
Tang Chen38b99172014-09-24 15:57:54 +080012124 * We are now running in L2, mmu_notifier will force to reload the
12125 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12126 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012127 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012128
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012129 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012130 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012131
12132 /* in case we halted in L2 */
12133 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012134
12135 if (likely(!vmx->fail)) {
12136 /*
12137 * TODO: SDM says that with acknowledge interrupt on
12138 * exit, bit 31 of the VM-exit interrupt information
12139 * (valid interrupt) is always set to 1 on
12140 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12141 * need kvm_cpu_has_interrupt(). See the commit
12142 * message for details.
12143 */
12144 if (nested_exit_intr_ack_set(vcpu) &&
12145 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12146 kvm_cpu_has_interrupt(vcpu)) {
12147 int irq = kvm_cpu_get_interrupt(vcpu);
12148 WARN_ON(irq < 0);
12149 vmcs12->vm_exit_intr_info = irq |
12150 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12151 }
12152
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012153 if (exit_reason != -1)
12154 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12155 vmcs12->exit_qualification,
12156 vmcs12->idt_vectoring_info_field,
12157 vmcs12->vm_exit_intr_info,
12158 vmcs12->vm_exit_intr_error_code,
12159 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012160
12161 load_vmcs12_host_state(vcpu, vmcs12);
12162
12163 return;
12164 }
12165
12166 /*
12167 * After an early L2 VM-entry failure, we're now back
12168 * in L1 which thinks it just finished a VMLAUNCH or
12169 * VMRESUME instruction, so we need to set the failure
12170 * flag and the VM-instruction error field of the VMCS
12171 * accordingly.
12172 */
12173 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012174
12175 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12176
Jim Mattson4f350c62017-09-14 16:31:44 -070012177 /*
12178 * The emulated instruction was already skipped in
12179 * nested_vmx_run, but the updated RIP was never
12180 * written back to the vmcs01.
12181 */
12182 skip_emulated_instruction(vcpu);
12183 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012184}
12185
Nadav Har'El7c177932011-05-25 23:12:04 +030012186/*
Jan Kiszka42124922014-01-04 18:47:19 +010012187 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12188 */
12189static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12190{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012191 if (is_guest_mode(vcpu)) {
12192 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012193 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012194 }
Jan Kiszka42124922014-01-04 18:47:19 +010012195 free_nested(to_vmx(vcpu));
12196}
12197
12198/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012199 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12200 * 23.7 "VM-entry failures during or after loading guest state" (this also
12201 * lists the acceptable exit-reason and exit-qualification parameters).
12202 * It should only be called before L2 actually succeeded to run, and when
12203 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12204 */
12205static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12206 struct vmcs12 *vmcs12,
12207 u32 reason, unsigned long qualification)
12208{
12209 load_vmcs12_host_state(vcpu, vmcs12);
12210 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12211 vmcs12->exit_qualification = qualification;
12212 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012213 if (enable_shadow_vmcs)
12214 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012215}
12216
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012217static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12218 struct x86_instruction_info *info,
12219 enum x86_intercept_stage stage)
12220{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012221 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12222 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12223
12224 /*
12225 * RDPID causes #UD if disabled through secondary execution controls.
12226 * Because it is marked as EmulateOnUD, we need to intercept it here.
12227 */
12228 if (info->intercept == x86_intercept_rdtscp &&
12229 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12230 ctxt->exception.vector = UD_VECTOR;
12231 ctxt->exception.error_code_valid = false;
12232 return X86EMUL_PROPAGATE_FAULT;
12233 }
12234
12235 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012236 return X86EMUL_CONTINUE;
12237}
12238
Yunhong Jiang64672c92016-06-13 14:19:59 -070012239#ifdef CONFIG_X86_64
12240/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12241static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12242 u64 divisor, u64 *result)
12243{
12244 u64 low = a << shift, high = a >> (64 - shift);
12245
12246 /* To avoid the overflow on divq */
12247 if (high >= divisor)
12248 return 1;
12249
12250 /* Low hold the result, high hold rem which is discarded */
12251 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12252 "rm" (divisor), "0" (low), "1" (high));
12253 *result = low;
12254
12255 return 0;
12256}
12257
12258static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12259{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012260 struct vcpu_vmx *vmx;
12261 u64 tscl, guest_tscl, delta_tsc;
12262
12263 if (kvm_mwait_in_guest(vcpu->kvm))
12264 return -EOPNOTSUPP;
12265
12266 vmx = to_vmx(vcpu);
12267 tscl = rdtsc();
12268 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12269 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012270
12271 /* Convert to host delta tsc if tsc scaling is enabled */
12272 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12273 u64_shl_div_u64(delta_tsc,
12274 kvm_tsc_scaling_ratio_frac_bits,
12275 vcpu->arch.tsc_scaling_ratio,
12276 &delta_tsc))
12277 return -ERANGE;
12278
12279 /*
12280 * If the delta tsc can't fit in the 32 bit after the multi shift,
12281 * we can't use the preemption timer.
12282 * It's possible that it fits on later vmentries, but checking
12283 * on every vmentry is costly so we just use an hrtimer.
12284 */
12285 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12286 return -ERANGE;
12287
12288 vmx->hv_deadline_tsc = tscl + delta_tsc;
12289 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12290 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012291
12292 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012293}
12294
12295static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12296{
12297 struct vcpu_vmx *vmx = to_vmx(vcpu);
12298 vmx->hv_deadline_tsc = -1;
12299 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12300 PIN_BASED_VMX_PREEMPTION_TIMER);
12301}
12302#endif
12303
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012304static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012305{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012306 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012307 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012308}
12309
Kai Huang843e4332015-01-28 10:54:28 +080012310static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12311 struct kvm_memory_slot *slot)
12312{
12313 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12314 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12315}
12316
12317static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12318 struct kvm_memory_slot *slot)
12319{
12320 kvm_mmu_slot_set_dirty(kvm, slot);
12321}
12322
12323static void vmx_flush_log_dirty(struct kvm *kvm)
12324{
12325 kvm_flush_pml_buffers(kvm);
12326}
12327
Bandan Dasc5f983f2017-05-05 15:25:14 -040012328static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12329{
12330 struct vmcs12 *vmcs12;
12331 struct vcpu_vmx *vmx = to_vmx(vcpu);
12332 gpa_t gpa;
12333 struct page *page = NULL;
12334 u64 *pml_address;
12335
12336 if (is_guest_mode(vcpu)) {
12337 WARN_ON_ONCE(vmx->nested.pml_full);
12338
12339 /*
12340 * Check if PML is enabled for the nested guest.
12341 * Whether eptp bit 6 is set is already checked
12342 * as part of A/D emulation.
12343 */
12344 vmcs12 = get_vmcs12(vcpu);
12345 if (!nested_cpu_has_pml(vmcs12))
12346 return 0;
12347
Dan Carpenter47698862017-05-10 22:43:17 +030012348 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012349 vmx->nested.pml_full = true;
12350 return 1;
12351 }
12352
12353 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12354
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012355 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12356 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012357 return 0;
12358
12359 pml_address = kmap(page);
12360 pml_address[vmcs12->guest_pml_index--] = gpa;
12361 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012362 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012363 }
12364
12365 return 0;
12366}
12367
Kai Huang843e4332015-01-28 10:54:28 +080012368static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12369 struct kvm_memory_slot *memslot,
12370 gfn_t offset, unsigned long mask)
12371{
12372 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12373}
12374
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012375static void __pi_post_block(struct kvm_vcpu *vcpu)
12376{
12377 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12378 struct pi_desc old, new;
12379 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012380
12381 do {
12382 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012383 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12384 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012385
12386 dest = cpu_physical_id(vcpu->cpu);
12387
12388 if (x2apic_enabled())
12389 new.ndst = dest;
12390 else
12391 new.ndst = (dest << 8) & 0xFF00;
12392
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012393 /* set 'NV' to 'notification vector' */
12394 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012395 } while (cmpxchg64(&pi_desc->control, old.control,
12396 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012397
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012398 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12399 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012400 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012401 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012402 vcpu->pre_pcpu = -1;
12403 }
12404}
12405
Feng Wuefc64402015-09-18 22:29:51 +080012406/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012407 * This routine does the following things for vCPU which is going
12408 * to be blocked if VT-d PI is enabled.
12409 * - Store the vCPU to the wakeup list, so when interrupts happen
12410 * we can find the right vCPU to wake up.
12411 * - Change the Posted-interrupt descriptor as below:
12412 * 'NDST' <-- vcpu->pre_pcpu
12413 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12414 * - If 'ON' is set during this process, which means at least one
12415 * interrupt is posted for this vCPU, we cannot block it, in
12416 * this case, return 1, otherwise, return 0.
12417 *
12418 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012419static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012420{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012421 unsigned int dest;
12422 struct pi_desc old, new;
12423 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12424
12425 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012426 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12427 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012428 return 0;
12429
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012430 WARN_ON(irqs_disabled());
12431 local_irq_disable();
12432 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12433 vcpu->pre_pcpu = vcpu->cpu;
12434 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12435 list_add_tail(&vcpu->blocked_vcpu_list,
12436 &per_cpu(blocked_vcpu_on_cpu,
12437 vcpu->pre_pcpu));
12438 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12439 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012440
12441 do {
12442 old.control = new.control = pi_desc->control;
12443
Feng Wubf9f6ac2015-09-18 22:29:55 +080012444 WARN((pi_desc->sn == 1),
12445 "Warning: SN field of posted-interrupts "
12446 "is set before blocking\n");
12447
12448 /*
12449 * Since vCPU can be preempted during this process,
12450 * vcpu->cpu could be different with pre_pcpu, we
12451 * need to set pre_pcpu as the destination of wakeup
12452 * notification event, then we can find the right vCPU
12453 * to wakeup in wakeup handler if interrupts happen
12454 * when the vCPU is in blocked state.
12455 */
12456 dest = cpu_physical_id(vcpu->pre_pcpu);
12457
12458 if (x2apic_enabled())
12459 new.ndst = dest;
12460 else
12461 new.ndst = (dest << 8) & 0xFF00;
12462
12463 /* set 'NV' to 'wakeup vector' */
12464 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012465 } while (cmpxchg64(&pi_desc->control, old.control,
12466 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012467
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012468 /* We should not block the vCPU if an interrupt is posted for it. */
12469 if (pi_test_on(pi_desc) == 1)
12470 __pi_post_block(vcpu);
12471
12472 local_irq_enable();
12473 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012474}
12475
Yunhong Jiangbc225122016-06-13 14:19:58 -070012476static int vmx_pre_block(struct kvm_vcpu *vcpu)
12477{
12478 if (pi_pre_block(vcpu))
12479 return 1;
12480
Yunhong Jiang64672c92016-06-13 14:19:59 -070012481 if (kvm_lapic_hv_timer_in_use(vcpu))
12482 kvm_lapic_switch_to_sw_timer(vcpu);
12483
Yunhong Jiangbc225122016-06-13 14:19:58 -070012484 return 0;
12485}
12486
12487static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012488{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012489 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012490 return;
12491
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012492 WARN_ON(irqs_disabled());
12493 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012494 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012495 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012496}
12497
Yunhong Jiangbc225122016-06-13 14:19:58 -070012498static void vmx_post_block(struct kvm_vcpu *vcpu)
12499{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012500 if (kvm_x86_ops->set_hv_timer)
12501 kvm_lapic_switch_to_hv_timer(vcpu);
12502
Yunhong Jiangbc225122016-06-13 14:19:58 -070012503 pi_post_block(vcpu);
12504}
12505
Feng Wubf9f6ac2015-09-18 22:29:55 +080012506/*
Feng Wuefc64402015-09-18 22:29:51 +080012507 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12508 *
12509 * @kvm: kvm
12510 * @host_irq: host irq of the interrupt
12511 * @guest_irq: gsi of the interrupt
12512 * @set: set or unset PI
12513 * returns 0 on success, < 0 on failure
12514 */
12515static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12516 uint32_t guest_irq, bool set)
12517{
12518 struct kvm_kernel_irq_routing_entry *e;
12519 struct kvm_irq_routing_table *irq_rt;
12520 struct kvm_lapic_irq irq;
12521 struct kvm_vcpu *vcpu;
12522 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012523 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012524
12525 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012526 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12527 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012528 return 0;
12529
12530 idx = srcu_read_lock(&kvm->irq_srcu);
12531 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012532 if (guest_irq >= irq_rt->nr_rt_entries ||
12533 hlist_empty(&irq_rt->map[guest_irq])) {
12534 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12535 guest_irq, irq_rt->nr_rt_entries);
12536 goto out;
12537 }
Feng Wuefc64402015-09-18 22:29:51 +080012538
12539 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12540 if (e->type != KVM_IRQ_ROUTING_MSI)
12541 continue;
12542 /*
12543 * VT-d PI cannot support posting multicast/broadcast
12544 * interrupts to a vCPU, we still use interrupt remapping
12545 * for these kind of interrupts.
12546 *
12547 * For lowest-priority interrupts, we only support
12548 * those with single CPU as the destination, e.g. user
12549 * configures the interrupts via /proc/irq or uses
12550 * irqbalance to make the interrupts single-CPU.
12551 *
12552 * We will support full lowest-priority interrupt later.
12553 */
12554
Radim Krčmář371313132016-07-12 22:09:27 +020012555 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012556 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12557 /*
12558 * Make sure the IRTE is in remapped mode if
12559 * we don't handle it in posted mode.
12560 */
12561 ret = irq_set_vcpu_affinity(host_irq, NULL);
12562 if (ret < 0) {
12563 printk(KERN_INFO
12564 "failed to back to remapped mode, irq: %u\n",
12565 host_irq);
12566 goto out;
12567 }
12568
Feng Wuefc64402015-09-18 22:29:51 +080012569 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012570 }
Feng Wuefc64402015-09-18 22:29:51 +080012571
12572 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12573 vcpu_info.vector = irq.vector;
12574
hu huajun2698d822018-04-11 15:16:40 +080012575 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012576 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12577
12578 if (set)
12579 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012580 else
Feng Wuefc64402015-09-18 22:29:51 +080012581 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012582
12583 if (ret < 0) {
12584 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12585 __func__);
12586 goto out;
12587 }
12588 }
12589
12590 ret = 0;
12591out:
12592 srcu_read_unlock(&kvm->irq_srcu, idx);
12593 return ret;
12594}
12595
Ashok Rajc45dcc72016-06-22 14:59:56 +080012596static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12597{
12598 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12599 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12600 FEATURE_CONTROL_LMCE;
12601 else
12602 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12603 ~FEATURE_CONTROL_LMCE;
12604}
12605
Ladi Prosek72d7b372017-10-11 16:54:41 +020012606static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12607{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012608 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12609 if (to_vmx(vcpu)->nested.nested_run_pending)
12610 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012611 return 1;
12612}
12613
Ladi Prosek0234bf82017-10-11 16:54:40 +020012614static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12615{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012616 struct vcpu_vmx *vmx = to_vmx(vcpu);
12617
12618 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12619 if (vmx->nested.smm.guest_mode)
12620 nested_vmx_vmexit(vcpu, -1, 0, 0);
12621
12622 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12623 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012624 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012625 return 0;
12626}
12627
12628static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12629{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012630 struct vcpu_vmx *vmx = to_vmx(vcpu);
12631 int ret;
12632
12633 if (vmx->nested.smm.vmxon) {
12634 vmx->nested.vmxon = true;
12635 vmx->nested.smm.vmxon = false;
12636 }
12637
12638 if (vmx->nested.smm.guest_mode) {
12639 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070012640 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012641 vcpu->arch.hflags |= HF_SMM_MASK;
12642 if (ret)
12643 return ret;
12644
12645 vmx->nested.smm.guest_mode = false;
12646 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012647 return 0;
12648}
12649
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012650static int enable_smi_window(struct kvm_vcpu *vcpu)
12651{
12652 return 0;
12653}
12654
Kees Cook404f6aa2016-08-08 16:29:06 -070012655static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012656 .cpu_has_kvm_support = cpu_has_kvm_support,
12657 .disabled_by_bios = vmx_disabled_by_bios,
12658 .hardware_setup = hardware_setup,
12659 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012660 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012661 .hardware_enable = hardware_enable,
12662 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012663 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012664 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012665
Wanpeng Lib31c1142018-03-12 04:53:04 -070012666 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012667 .vm_alloc = vmx_vm_alloc,
12668 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012669
Avi Kivity6aa8b732006-12-10 02:21:36 -080012670 .vcpu_create = vmx_create_vcpu,
12671 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012672 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012673
Avi Kivity04d2cc72007-09-10 18:10:54 +030012674 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012675 .vcpu_load = vmx_vcpu_load,
12676 .vcpu_put = vmx_vcpu_put,
12677
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012678 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012679 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012680 .get_msr = vmx_get_msr,
12681 .set_msr = vmx_set_msr,
12682 .get_segment_base = vmx_get_segment_base,
12683 .get_segment = vmx_get_segment,
12684 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012685 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012686 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012687 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012688 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012689 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012690 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012691 .set_cr3 = vmx_set_cr3,
12692 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012693 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012694 .get_idt = vmx_get_idt,
12695 .set_idt = vmx_set_idt,
12696 .get_gdt = vmx_get_gdt,
12697 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012698 .get_dr6 = vmx_get_dr6,
12699 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012700 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012701 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012702 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012703 .get_rflags = vmx_get_rflags,
12704 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012705
Avi Kivity6aa8b732006-12-10 02:21:36 -080012706 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012707
Avi Kivity6aa8b732006-12-10 02:21:36 -080012708 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012709 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012710 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012711 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12712 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012713 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012714 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012715 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012716 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012717 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012718 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012719 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012720 .get_nmi_mask = vmx_get_nmi_mask,
12721 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012722 .enable_nmi_window = enable_nmi_window,
12723 .enable_irq_window = enable_irq_window,
12724 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040012725 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012726 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012727 .get_enable_apicv = vmx_get_enable_apicv,
12728 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012729 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012730 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012731 .hwapic_irr_update = vmx_hwapic_irr_update,
12732 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012733 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12734 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012735
Izik Eiduscbc94022007-10-25 00:29:55 +020012736 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012737 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012738 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012739 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012740
Avi Kivity586f9602010-11-18 13:09:54 +020012741 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012742
Sheng Yang17cc3932010-01-05 19:02:27 +080012743 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012744
12745 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012746
12747 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012748 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012749
12750 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012751
12752 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012753
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012754 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012755 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012756
12757 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012758
12759 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012760 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012761 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012762 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012763 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012764
12765 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012766
12767 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012768
12769 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12770 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12771 .flush_log_dirty = vmx_flush_log_dirty,
12772 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012773 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012774
Feng Wubf9f6ac2015-09-18 22:29:55 +080012775 .pre_block = vmx_pre_block,
12776 .post_block = vmx_post_block,
12777
Wei Huang25462f72015-06-19 15:45:05 +020012778 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012779
12780 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012781
12782#ifdef CONFIG_X86_64
12783 .set_hv_timer = vmx_set_hv_timer,
12784 .cancel_hv_timer = vmx_cancel_hv_timer,
12785#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012786
12787 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012788
Ladi Prosek72d7b372017-10-11 16:54:41 +020012789 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012790 .pre_enter_smm = vmx_pre_enter_smm,
12791 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012792 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012793};
12794
12795static int __init vmx_init(void)
12796{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012797 int r;
12798
12799#if IS_ENABLED(CONFIG_HYPERV)
12800 /*
12801 * Enlightened VMCS usage should be recommended and the host needs
12802 * to support eVMCS v1 or above. We can also disable eVMCS support
12803 * with module parameter.
12804 */
12805 if (enlightened_vmcs &&
12806 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12807 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12808 KVM_EVMCS_VERSION) {
12809 int cpu;
12810
12811 /* Check that we have assist pages on all online CPUs */
12812 for_each_online_cpu(cpu) {
12813 if (!hv_get_vp_assist_page(cpu)) {
12814 enlightened_vmcs = false;
12815 break;
12816 }
12817 }
12818
12819 if (enlightened_vmcs) {
12820 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12821 static_branch_enable(&enable_evmcs);
12822 }
12823 } else {
12824 enlightened_vmcs = false;
12825 }
12826#endif
12827
12828 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012829 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012830 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012831 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012832
Dave Young2965faa2015-09-09 15:38:55 -070012833#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012834 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12835 crash_vmclear_local_loaded_vmcss);
12836#endif
12837
He, Qingfdef3ad2007-04-30 09:45:24 +030012838 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012839}
12840
12841static void __exit vmx_exit(void)
12842{
Dave Young2965faa2015-09-09 15:38:55 -070012843#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012844 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012845 synchronize_rcu();
12846#endif
12847
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012848 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012849
12850#if IS_ENABLED(CONFIG_HYPERV)
12851 if (static_branch_unlikely(&enable_evmcs)) {
12852 int cpu;
12853 struct hv_vp_assist_page *vp_ap;
12854 /*
12855 * Reset everything to support using non-enlightened VMCS
12856 * access later (e.g. when we reload the module with
12857 * enlightened_vmcs=0)
12858 */
12859 for_each_online_cpu(cpu) {
12860 vp_ap = hv_get_vp_assist_page(cpu);
12861
12862 if (!vp_ap)
12863 continue;
12864
12865 vp_ap->current_nested_vmcs = 0;
12866 vp_ap->enlighten_vmentry = 0;
12867 }
12868
12869 static_branch_disable(&enable_evmcs);
12870 }
12871#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080012872}
12873
12874module_init(vmx_init)
12875module_exit(vmx_exit)