blob: 5aea5af02386a64ce9d56b601f6c5d4c0b349cea [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
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
Liran Alon392b2f22018-06-23 02:35:01 +0300201struct vmcs_hdr {
202 u32 revision_id:31;
203 u32 shadow_vmcs:1;
204};
205
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400206struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300207 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400208 u32 abort;
209 char data[0];
210};
211
Nadav Har'Eld462b812011-05-24 15:26:10 +0300212/*
213 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
214 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
215 * loaded on this CPU (so we can clear them if the CPU goes down).
216 */
217struct loaded_vmcs {
218 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700219 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300220 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200221 bool launched;
222 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200223 unsigned long vmcs_host_cr3; /* May not match real cr3 */
224 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100225 /* Support for vnmi-less CPUs */
226 int soft_vnmi_blocked;
227 ktime_t entry_time;
228 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100229 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300230 struct list_head loaded_vmcss_on_cpu_link;
231};
232
Avi Kivity26bb0982009-09-07 11:14:12 +0300233struct shared_msr_entry {
234 unsigned index;
235 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200236 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300237};
238
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300239/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300240 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
241 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
242 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
243 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
244 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
245 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600246 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300247 * underlying hardware which will be used to run L2.
248 * This structure is packed to ensure that its layout is identical across
249 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700250 *
251 * IMPORTANT: Changing the layout of existing fields in this structure
252 * will break save/restore compatibility with older kvm releases. When
253 * adding new fields, either use space in the reserved padding* arrays
254 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300255 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300256typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300257struct __packed vmcs12 {
258 /* According to the Intel spec, a VMCS region must start with the
259 * following two fields. Then follow implementation-specific data.
260 */
Liran Alon392b2f22018-06-23 02:35:01 +0300261 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300262 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300263
Nadav Har'El27d6c862011-05-25 23:06:59 +0300264 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
265 u32 padding[7]; /* room for future expansion */
266
Nadav Har'El22bd0352011-05-25 23:05:57 +0300267 u64 io_bitmap_a;
268 u64 io_bitmap_b;
269 u64 msr_bitmap;
270 u64 vm_exit_msr_store_addr;
271 u64 vm_exit_msr_load_addr;
272 u64 vm_entry_msr_load_addr;
273 u64 tsc_offset;
274 u64 virtual_apic_page_addr;
275 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800276 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300277 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800278 u64 eoi_exit_bitmap0;
279 u64 eoi_exit_bitmap1;
280 u64 eoi_exit_bitmap2;
281 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800282 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300283 u64 guest_physical_address;
284 u64 vmcs_link_pointer;
285 u64 guest_ia32_debugctl;
286 u64 guest_ia32_pat;
287 u64 guest_ia32_efer;
288 u64 guest_ia32_perf_global_ctrl;
289 u64 guest_pdptr0;
290 u64 guest_pdptr1;
291 u64 guest_pdptr2;
292 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100293 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300294 u64 host_ia32_pat;
295 u64 host_ia32_efer;
296 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700297 u64 vmread_bitmap;
298 u64 vmwrite_bitmap;
299 u64 vm_function_control;
300 u64 eptp_list_address;
301 u64 pml_address;
302 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 /*
304 * To allow migration of L1 (complete with its L2 guests) between
305 * machines of different natural widths (32 or 64 bit), we cannot have
306 * unsigned long fields with no explict size. We use u64 (aliased
307 * natural_width) instead. Luckily, x86 is little-endian.
308 */
309 natural_width cr0_guest_host_mask;
310 natural_width cr4_guest_host_mask;
311 natural_width cr0_read_shadow;
312 natural_width cr4_read_shadow;
313 natural_width cr3_target_value0;
314 natural_width cr3_target_value1;
315 natural_width cr3_target_value2;
316 natural_width cr3_target_value3;
317 natural_width exit_qualification;
318 natural_width guest_linear_address;
319 natural_width guest_cr0;
320 natural_width guest_cr3;
321 natural_width guest_cr4;
322 natural_width guest_es_base;
323 natural_width guest_cs_base;
324 natural_width guest_ss_base;
325 natural_width guest_ds_base;
326 natural_width guest_fs_base;
327 natural_width guest_gs_base;
328 natural_width guest_ldtr_base;
329 natural_width guest_tr_base;
330 natural_width guest_gdtr_base;
331 natural_width guest_idtr_base;
332 natural_width guest_dr7;
333 natural_width guest_rsp;
334 natural_width guest_rip;
335 natural_width guest_rflags;
336 natural_width guest_pending_dbg_exceptions;
337 natural_width guest_sysenter_esp;
338 natural_width guest_sysenter_eip;
339 natural_width host_cr0;
340 natural_width host_cr3;
341 natural_width host_cr4;
342 natural_width host_fs_base;
343 natural_width host_gs_base;
344 natural_width host_tr_base;
345 natural_width host_gdtr_base;
346 natural_width host_idtr_base;
347 natural_width host_ia32_sysenter_esp;
348 natural_width host_ia32_sysenter_eip;
349 natural_width host_rsp;
350 natural_width host_rip;
351 natural_width paddingl[8]; /* room for future expansion */
352 u32 pin_based_vm_exec_control;
353 u32 cpu_based_vm_exec_control;
354 u32 exception_bitmap;
355 u32 page_fault_error_code_mask;
356 u32 page_fault_error_code_match;
357 u32 cr3_target_count;
358 u32 vm_exit_controls;
359 u32 vm_exit_msr_store_count;
360 u32 vm_exit_msr_load_count;
361 u32 vm_entry_controls;
362 u32 vm_entry_msr_load_count;
363 u32 vm_entry_intr_info_field;
364 u32 vm_entry_exception_error_code;
365 u32 vm_entry_instruction_len;
366 u32 tpr_threshold;
367 u32 secondary_vm_exec_control;
368 u32 vm_instruction_error;
369 u32 vm_exit_reason;
370 u32 vm_exit_intr_info;
371 u32 vm_exit_intr_error_code;
372 u32 idt_vectoring_info_field;
373 u32 idt_vectoring_error_code;
374 u32 vm_exit_instruction_len;
375 u32 vmx_instruction_info;
376 u32 guest_es_limit;
377 u32 guest_cs_limit;
378 u32 guest_ss_limit;
379 u32 guest_ds_limit;
380 u32 guest_fs_limit;
381 u32 guest_gs_limit;
382 u32 guest_ldtr_limit;
383 u32 guest_tr_limit;
384 u32 guest_gdtr_limit;
385 u32 guest_idtr_limit;
386 u32 guest_es_ar_bytes;
387 u32 guest_cs_ar_bytes;
388 u32 guest_ss_ar_bytes;
389 u32 guest_ds_ar_bytes;
390 u32 guest_fs_ar_bytes;
391 u32 guest_gs_ar_bytes;
392 u32 guest_ldtr_ar_bytes;
393 u32 guest_tr_ar_bytes;
394 u32 guest_interruptibility_info;
395 u32 guest_activity_state;
396 u32 guest_sysenter_cs;
397 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100398 u32 vmx_preemption_timer_value;
399 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300400 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800401 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300402 u16 guest_es_selector;
403 u16 guest_cs_selector;
404 u16 guest_ss_selector;
405 u16 guest_ds_selector;
406 u16 guest_fs_selector;
407 u16 guest_gs_selector;
408 u16 guest_ldtr_selector;
409 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800410 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300411 u16 host_es_selector;
412 u16 host_cs_selector;
413 u16 host_ss_selector;
414 u16 host_ds_selector;
415 u16 host_fs_selector;
416 u16 host_gs_selector;
417 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700418 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300419};
420
421/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700422 * For save/restore compatibility, the vmcs12 field offsets must not change.
423 */
424#define CHECK_OFFSET(field, loc) \
425 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
426 "Offset of " #field " in struct vmcs12 has changed.")
427
428static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300429 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700430 CHECK_OFFSET(abort, 4);
431 CHECK_OFFSET(launch_state, 8);
432 CHECK_OFFSET(io_bitmap_a, 40);
433 CHECK_OFFSET(io_bitmap_b, 48);
434 CHECK_OFFSET(msr_bitmap, 56);
435 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
436 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
437 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
438 CHECK_OFFSET(tsc_offset, 88);
439 CHECK_OFFSET(virtual_apic_page_addr, 96);
440 CHECK_OFFSET(apic_access_addr, 104);
441 CHECK_OFFSET(posted_intr_desc_addr, 112);
442 CHECK_OFFSET(ept_pointer, 120);
443 CHECK_OFFSET(eoi_exit_bitmap0, 128);
444 CHECK_OFFSET(eoi_exit_bitmap1, 136);
445 CHECK_OFFSET(eoi_exit_bitmap2, 144);
446 CHECK_OFFSET(eoi_exit_bitmap3, 152);
447 CHECK_OFFSET(xss_exit_bitmap, 160);
448 CHECK_OFFSET(guest_physical_address, 168);
449 CHECK_OFFSET(vmcs_link_pointer, 176);
450 CHECK_OFFSET(guest_ia32_debugctl, 184);
451 CHECK_OFFSET(guest_ia32_pat, 192);
452 CHECK_OFFSET(guest_ia32_efer, 200);
453 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
454 CHECK_OFFSET(guest_pdptr0, 216);
455 CHECK_OFFSET(guest_pdptr1, 224);
456 CHECK_OFFSET(guest_pdptr2, 232);
457 CHECK_OFFSET(guest_pdptr3, 240);
458 CHECK_OFFSET(guest_bndcfgs, 248);
459 CHECK_OFFSET(host_ia32_pat, 256);
460 CHECK_OFFSET(host_ia32_efer, 264);
461 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
462 CHECK_OFFSET(vmread_bitmap, 280);
463 CHECK_OFFSET(vmwrite_bitmap, 288);
464 CHECK_OFFSET(vm_function_control, 296);
465 CHECK_OFFSET(eptp_list_address, 304);
466 CHECK_OFFSET(pml_address, 312);
467 CHECK_OFFSET(cr0_guest_host_mask, 344);
468 CHECK_OFFSET(cr4_guest_host_mask, 352);
469 CHECK_OFFSET(cr0_read_shadow, 360);
470 CHECK_OFFSET(cr4_read_shadow, 368);
471 CHECK_OFFSET(cr3_target_value0, 376);
472 CHECK_OFFSET(cr3_target_value1, 384);
473 CHECK_OFFSET(cr3_target_value2, 392);
474 CHECK_OFFSET(cr3_target_value3, 400);
475 CHECK_OFFSET(exit_qualification, 408);
476 CHECK_OFFSET(guest_linear_address, 416);
477 CHECK_OFFSET(guest_cr0, 424);
478 CHECK_OFFSET(guest_cr3, 432);
479 CHECK_OFFSET(guest_cr4, 440);
480 CHECK_OFFSET(guest_es_base, 448);
481 CHECK_OFFSET(guest_cs_base, 456);
482 CHECK_OFFSET(guest_ss_base, 464);
483 CHECK_OFFSET(guest_ds_base, 472);
484 CHECK_OFFSET(guest_fs_base, 480);
485 CHECK_OFFSET(guest_gs_base, 488);
486 CHECK_OFFSET(guest_ldtr_base, 496);
487 CHECK_OFFSET(guest_tr_base, 504);
488 CHECK_OFFSET(guest_gdtr_base, 512);
489 CHECK_OFFSET(guest_idtr_base, 520);
490 CHECK_OFFSET(guest_dr7, 528);
491 CHECK_OFFSET(guest_rsp, 536);
492 CHECK_OFFSET(guest_rip, 544);
493 CHECK_OFFSET(guest_rflags, 552);
494 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
495 CHECK_OFFSET(guest_sysenter_esp, 568);
496 CHECK_OFFSET(guest_sysenter_eip, 576);
497 CHECK_OFFSET(host_cr0, 584);
498 CHECK_OFFSET(host_cr3, 592);
499 CHECK_OFFSET(host_cr4, 600);
500 CHECK_OFFSET(host_fs_base, 608);
501 CHECK_OFFSET(host_gs_base, 616);
502 CHECK_OFFSET(host_tr_base, 624);
503 CHECK_OFFSET(host_gdtr_base, 632);
504 CHECK_OFFSET(host_idtr_base, 640);
505 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
506 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
507 CHECK_OFFSET(host_rsp, 664);
508 CHECK_OFFSET(host_rip, 672);
509 CHECK_OFFSET(pin_based_vm_exec_control, 744);
510 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
511 CHECK_OFFSET(exception_bitmap, 752);
512 CHECK_OFFSET(page_fault_error_code_mask, 756);
513 CHECK_OFFSET(page_fault_error_code_match, 760);
514 CHECK_OFFSET(cr3_target_count, 764);
515 CHECK_OFFSET(vm_exit_controls, 768);
516 CHECK_OFFSET(vm_exit_msr_store_count, 772);
517 CHECK_OFFSET(vm_exit_msr_load_count, 776);
518 CHECK_OFFSET(vm_entry_controls, 780);
519 CHECK_OFFSET(vm_entry_msr_load_count, 784);
520 CHECK_OFFSET(vm_entry_intr_info_field, 788);
521 CHECK_OFFSET(vm_entry_exception_error_code, 792);
522 CHECK_OFFSET(vm_entry_instruction_len, 796);
523 CHECK_OFFSET(tpr_threshold, 800);
524 CHECK_OFFSET(secondary_vm_exec_control, 804);
525 CHECK_OFFSET(vm_instruction_error, 808);
526 CHECK_OFFSET(vm_exit_reason, 812);
527 CHECK_OFFSET(vm_exit_intr_info, 816);
528 CHECK_OFFSET(vm_exit_intr_error_code, 820);
529 CHECK_OFFSET(idt_vectoring_info_field, 824);
530 CHECK_OFFSET(idt_vectoring_error_code, 828);
531 CHECK_OFFSET(vm_exit_instruction_len, 832);
532 CHECK_OFFSET(vmx_instruction_info, 836);
533 CHECK_OFFSET(guest_es_limit, 840);
534 CHECK_OFFSET(guest_cs_limit, 844);
535 CHECK_OFFSET(guest_ss_limit, 848);
536 CHECK_OFFSET(guest_ds_limit, 852);
537 CHECK_OFFSET(guest_fs_limit, 856);
538 CHECK_OFFSET(guest_gs_limit, 860);
539 CHECK_OFFSET(guest_ldtr_limit, 864);
540 CHECK_OFFSET(guest_tr_limit, 868);
541 CHECK_OFFSET(guest_gdtr_limit, 872);
542 CHECK_OFFSET(guest_idtr_limit, 876);
543 CHECK_OFFSET(guest_es_ar_bytes, 880);
544 CHECK_OFFSET(guest_cs_ar_bytes, 884);
545 CHECK_OFFSET(guest_ss_ar_bytes, 888);
546 CHECK_OFFSET(guest_ds_ar_bytes, 892);
547 CHECK_OFFSET(guest_fs_ar_bytes, 896);
548 CHECK_OFFSET(guest_gs_ar_bytes, 900);
549 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
550 CHECK_OFFSET(guest_tr_ar_bytes, 908);
551 CHECK_OFFSET(guest_interruptibility_info, 912);
552 CHECK_OFFSET(guest_activity_state, 916);
553 CHECK_OFFSET(guest_sysenter_cs, 920);
554 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
555 CHECK_OFFSET(vmx_preemption_timer_value, 928);
556 CHECK_OFFSET(virtual_processor_id, 960);
557 CHECK_OFFSET(posted_intr_nv, 962);
558 CHECK_OFFSET(guest_es_selector, 964);
559 CHECK_OFFSET(guest_cs_selector, 966);
560 CHECK_OFFSET(guest_ss_selector, 968);
561 CHECK_OFFSET(guest_ds_selector, 970);
562 CHECK_OFFSET(guest_fs_selector, 972);
563 CHECK_OFFSET(guest_gs_selector, 974);
564 CHECK_OFFSET(guest_ldtr_selector, 976);
565 CHECK_OFFSET(guest_tr_selector, 978);
566 CHECK_OFFSET(guest_intr_status, 980);
567 CHECK_OFFSET(host_es_selector, 982);
568 CHECK_OFFSET(host_cs_selector, 984);
569 CHECK_OFFSET(host_ss_selector, 986);
570 CHECK_OFFSET(host_ds_selector, 988);
571 CHECK_OFFSET(host_fs_selector, 990);
572 CHECK_OFFSET(host_gs_selector, 992);
573 CHECK_OFFSET(host_tr_selector, 994);
574 CHECK_OFFSET(guest_pml_index, 996);
575}
576
577/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300578 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
579 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
580 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700581 *
582 * IMPORTANT: Changing this value will break save/restore compatibility with
583 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300584 */
585#define VMCS12_REVISION 0x11e57ed0
586
587/*
588 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
589 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
590 * current implementation, 4K are reserved to avoid future complications.
591 */
592#define VMCS12_SIZE 0x1000
593
594/*
Jim Mattson5b157062017-12-22 12:11:12 -0800595 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
596 * supported VMCS12 field encoding.
597 */
598#define VMCS12_MAX_FIELD_INDEX 0x17
599
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100600struct nested_vmx_msrs {
601 /*
602 * We only store the "true" versions of the VMX capability MSRs. We
603 * generate the "non-true" versions by setting the must-be-1 bits
604 * according to the SDM.
605 */
606 u32 procbased_ctls_low;
607 u32 procbased_ctls_high;
608 u32 secondary_ctls_low;
609 u32 secondary_ctls_high;
610 u32 pinbased_ctls_low;
611 u32 pinbased_ctls_high;
612 u32 exit_ctls_low;
613 u32 exit_ctls_high;
614 u32 entry_ctls_low;
615 u32 entry_ctls_high;
616 u32 misc_low;
617 u32 misc_high;
618 u32 ept_caps;
619 u32 vpid_caps;
620 u64 basic;
621 u64 cr0_fixed0;
622 u64 cr0_fixed1;
623 u64 cr4_fixed0;
624 u64 cr4_fixed1;
625 u64 vmcs_enum;
626 u64 vmfunc_controls;
627};
628
Jim Mattson5b157062017-12-22 12:11:12 -0800629/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300630 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
631 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
632 */
633struct nested_vmx {
634 /* Has the level1 guest done vmxon? */
635 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400636 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400637 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300638
639 /* The guest-physical address of the current VMCS L1 keeps for L2 */
640 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700641 /*
642 * Cache of the guest's VMCS, existing outside of guest memory.
643 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700644 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700645 */
646 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300647 /*
Liran Alon61ada742018-06-23 02:35:08 +0300648 * Cache of the guest's shadow VMCS, existing outside of guest
649 * memory. Loaded from guest memory during VM entry. Flushed
650 * to guest memory during VM exit.
651 */
652 struct vmcs12 *cached_shadow_vmcs12;
653 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300654 * Indicates if the shadow vmcs must be updated with the
655 * data hold by vmcs12
656 */
657 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100658 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300659
Jim Mattson8d860bb2018-05-09 16:56:05 -0400660 bool change_vmcs01_virtual_apic_mode;
661
Nadav Har'El644d7112011-05-25 23:12:35 +0300662 /* L2 must run next, and mustn't decide to exit to L1. */
663 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600664
665 struct loaded_vmcs vmcs02;
666
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300667 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600668 * Guest pages referred to in the vmcs02 with host-physical
669 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300670 */
671 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800672 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800673 struct page *pi_desc_page;
674 struct pi_desc *pi_desc;
675 bool pi_pending;
676 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100677
678 struct hrtimer preemption_timer;
679 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200680
681 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
682 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800683
Wanpeng Li5c614b32015-10-13 09:18:36 -0700684 u16 vpid02;
685 u16 last_vpid;
686
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100687 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200688
689 /* SMM related state */
690 struct {
691 /* in VMX operation on SMM entry? */
692 bool vmxon;
693 /* in guest mode on SMM entry? */
694 bool guest_mode;
695 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300696};
697
Yang Zhang01e439b2013-04-11 19:25:12 +0800698#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800699#define POSTED_INTR_SN 1
700
Yang Zhang01e439b2013-04-11 19:25:12 +0800701/* Posted-Interrupt Descriptor */
702struct pi_desc {
703 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800704 union {
705 struct {
706 /* bit 256 - Outstanding Notification */
707 u16 on : 1,
708 /* bit 257 - Suppress Notification */
709 sn : 1,
710 /* bit 271:258 - Reserved */
711 rsvd_1 : 14;
712 /* bit 279:272 - Notification Vector */
713 u8 nv;
714 /* bit 287:280 - Reserved */
715 u8 rsvd_2;
716 /* bit 319:288 - Notification Destination */
717 u32 ndst;
718 };
719 u64 control;
720 };
721 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800722} __aligned(64);
723
Yang Zhanga20ed542013-04-11 19:25:15 +0800724static bool pi_test_and_set_on(struct pi_desc *pi_desc)
725{
726 return test_and_set_bit(POSTED_INTR_ON,
727 (unsigned long *)&pi_desc->control);
728}
729
730static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
731{
732 return test_and_clear_bit(POSTED_INTR_ON,
733 (unsigned long *)&pi_desc->control);
734}
735
736static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
737{
738 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
739}
740
Feng Wuebbfc762015-09-18 22:29:46 +0800741static inline void pi_clear_sn(struct pi_desc *pi_desc)
742{
743 return clear_bit(POSTED_INTR_SN,
744 (unsigned long *)&pi_desc->control);
745}
746
747static inline void pi_set_sn(struct pi_desc *pi_desc)
748{
749 return set_bit(POSTED_INTR_SN,
750 (unsigned long *)&pi_desc->control);
751}
752
Paolo Bonziniad361092016-09-20 16:15:05 +0200753static inline void pi_clear_on(struct pi_desc *pi_desc)
754{
755 clear_bit(POSTED_INTR_ON,
756 (unsigned long *)&pi_desc->control);
757}
758
Feng Wuebbfc762015-09-18 22:29:46 +0800759static inline int pi_test_on(struct pi_desc *pi_desc)
760{
761 return test_bit(POSTED_INTR_ON,
762 (unsigned long *)&pi_desc->control);
763}
764
765static inline int pi_test_sn(struct pi_desc *pi_desc)
766{
767 return test_bit(POSTED_INTR_SN,
768 (unsigned long *)&pi_desc->control);
769}
770
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000772 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300773 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300774 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100775 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300776 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200777 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200778 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300779 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780 int nmsrs;
781 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800782 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400783#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300784 u64 msr_host_kernel_gs_base;
785 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400786#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100787
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100788 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100789 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100790
Gleb Natapov2961e8762013-11-25 15:37:13 +0200791 u32 vm_entry_controls_shadow;
792 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200793 u32 secondary_exec_control;
794
Nadav Har'Eld462b812011-05-24 15:26:10 +0300795 /*
796 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
797 * non-nested (L1) guest, it always points to vmcs01. For a nested
798 * guest (L2), it points to a different VMCS.
799 */
800 struct loaded_vmcs vmcs01;
801 struct loaded_vmcs *loaded_vmcs;
802 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300803 struct msr_autoload {
804 unsigned nr;
805 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
806 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
807 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808 struct {
809 int loaded;
810 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300811#ifdef CONFIG_X86_64
812 u16 ds_sel, es_sel;
813#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200814 int gs_ldt_reload_needed;
815 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400816 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200817 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300818 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300819 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300820 struct kvm_segment segs[8];
821 } rmode;
822 struct {
823 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300824 struct kvm_save_segment {
825 u16 selector;
826 unsigned long base;
827 u32 limit;
828 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300829 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300830 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800831 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300832 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200833
Andi Kleena0861c02009-06-08 17:37:09 +0800834 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800835
Yang Zhang01e439b2013-04-11 19:25:12 +0800836 /* Posted interrupt descriptor */
837 struct pi_desc pi_desc;
838
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300839 /* Support for a guest hypervisor (nested VMX) */
840 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200841
842 /* Dynamic PLE window. */
843 int ple_window;
844 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800845
846 /* Support for PML */
847#define PML_ENTITY_NUM 512
848 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800849
Yunhong Jiang64672c92016-06-13 14:19:59 -0700850 /* apic deadline value in host tsc */
851 u64 hv_deadline_tsc;
852
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800853 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800854
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800855 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800856
Wanpeng Li74c55932017-11-29 01:31:20 -0800857 unsigned long host_debugctlmsr;
858
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800859 /*
860 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
861 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
862 * in msr_ia32_feature_control_valid_bits.
863 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800864 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800865 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400866};
867
Avi Kivity2fb92db2011-04-27 19:42:18 +0300868enum segment_cache_field {
869 SEG_FIELD_SEL = 0,
870 SEG_FIELD_BASE = 1,
871 SEG_FIELD_LIMIT = 2,
872 SEG_FIELD_AR = 3,
873
874 SEG_FIELD_NR = 4
875};
876
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700877static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
878{
879 return container_of(kvm, struct kvm_vmx, kvm);
880}
881
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400882static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
883{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000884 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400885}
886
Feng Wuefc64402015-09-18 22:29:51 +0800887static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
888{
889 return &(to_vmx(vcpu)->pi_desc);
890}
891
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800892#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300893#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800894#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
895#define FIELD64(number, name) \
896 FIELD(number, name), \
897 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300898
Abel Gordon4607c2d2013-04-18 14:35:55 +0300899
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100900static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100901#define SHADOW_FIELD_RO(x) x,
902#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300903};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400904static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300905 ARRAY_SIZE(shadow_read_only_fields);
906
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100907static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100908#define SHADOW_FIELD_RW(x) x,
909#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300910};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400911static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300912 ARRAY_SIZE(shadow_read_write_fields);
913
Mathias Krause772e0312012-08-30 01:30:19 +0200914static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300915 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800916 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300917 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
918 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
919 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
920 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
921 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
922 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
923 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
924 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800925 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400926 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300927 FIELD(HOST_ES_SELECTOR, host_es_selector),
928 FIELD(HOST_CS_SELECTOR, host_cs_selector),
929 FIELD(HOST_SS_SELECTOR, host_ss_selector),
930 FIELD(HOST_DS_SELECTOR, host_ds_selector),
931 FIELD(HOST_FS_SELECTOR, host_fs_selector),
932 FIELD(HOST_GS_SELECTOR, host_gs_selector),
933 FIELD(HOST_TR_SELECTOR, host_tr_selector),
934 FIELD64(IO_BITMAP_A, io_bitmap_a),
935 FIELD64(IO_BITMAP_B, io_bitmap_b),
936 FIELD64(MSR_BITMAP, msr_bitmap),
937 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
938 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
939 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700940 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300941 FIELD64(TSC_OFFSET, tsc_offset),
942 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
943 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800944 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400945 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300946 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800947 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
948 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
949 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
950 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400951 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700952 FIELD64(VMREAD_BITMAP, vmread_bitmap),
953 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800954 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300955 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
956 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
957 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
958 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
959 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
960 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
961 FIELD64(GUEST_PDPTR0, guest_pdptr0),
962 FIELD64(GUEST_PDPTR1, guest_pdptr1),
963 FIELD64(GUEST_PDPTR2, guest_pdptr2),
964 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100965 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300966 FIELD64(HOST_IA32_PAT, host_ia32_pat),
967 FIELD64(HOST_IA32_EFER, host_ia32_efer),
968 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
969 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
970 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
971 FIELD(EXCEPTION_BITMAP, exception_bitmap),
972 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
973 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
974 FIELD(CR3_TARGET_COUNT, cr3_target_count),
975 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
976 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
977 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
978 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
979 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
980 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
981 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
982 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
983 FIELD(TPR_THRESHOLD, tpr_threshold),
984 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
985 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
986 FIELD(VM_EXIT_REASON, vm_exit_reason),
987 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
988 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
989 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
990 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
991 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
992 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
993 FIELD(GUEST_ES_LIMIT, guest_es_limit),
994 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
995 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
996 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
997 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
998 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
999 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1000 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1001 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1002 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1003 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1004 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1005 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1006 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1007 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1008 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1009 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1010 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1011 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1012 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1013 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1014 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001015 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001016 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1017 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1018 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1019 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1020 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1021 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1022 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1023 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1024 FIELD(EXIT_QUALIFICATION, exit_qualification),
1025 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1026 FIELD(GUEST_CR0, guest_cr0),
1027 FIELD(GUEST_CR3, guest_cr3),
1028 FIELD(GUEST_CR4, guest_cr4),
1029 FIELD(GUEST_ES_BASE, guest_es_base),
1030 FIELD(GUEST_CS_BASE, guest_cs_base),
1031 FIELD(GUEST_SS_BASE, guest_ss_base),
1032 FIELD(GUEST_DS_BASE, guest_ds_base),
1033 FIELD(GUEST_FS_BASE, guest_fs_base),
1034 FIELD(GUEST_GS_BASE, guest_gs_base),
1035 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1036 FIELD(GUEST_TR_BASE, guest_tr_base),
1037 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1038 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1039 FIELD(GUEST_DR7, guest_dr7),
1040 FIELD(GUEST_RSP, guest_rsp),
1041 FIELD(GUEST_RIP, guest_rip),
1042 FIELD(GUEST_RFLAGS, guest_rflags),
1043 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1044 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1045 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1046 FIELD(HOST_CR0, host_cr0),
1047 FIELD(HOST_CR3, host_cr3),
1048 FIELD(HOST_CR4, host_cr4),
1049 FIELD(HOST_FS_BASE, host_fs_base),
1050 FIELD(HOST_GS_BASE, host_gs_base),
1051 FIELD(HOST_TR_BASE, host_tr_base),
1052 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1053 FIELD(HOST_IDTR_BASE, host_idtr_base),
1054 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1055 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1056 FIELD(HOST_RSP, host_rsp),
1057 FIELD(HOST_RIP, host_rip),
1058};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001059
1060static inline short vmcs_field_to_offset(unsigned long field)
1061{
Dan Williams085331d2018-01-31 17:47:03 -08001062 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1063 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001064 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001065
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001066 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001067 return -ENOENT;
1068
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001069 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001070 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001071 return -ENOENT;
1072
Linus Torvalds15303ba2018-02-10 13:16:35 -08001073 index = array_index_nospec(index, size);
1074 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001075 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001076 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001077 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078}
1079
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001080static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1081{
David Matlack4f2777b2016-07-13 17:16:37 -07001082 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001083}
1084
Liran Alon61ada742018-06-23 02:35:08 +03001085static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1086{
1087 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1088}
1089
Peter Feiner995f00a2017-06-30 17:26:32 -07001090static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001091static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001092static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001093static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001094static void vmx_set_segment(struct kvm_vcpu *vcpu,
1095 struct kvm_segment *var, int seg);
1096static void vmx_get_segment(struct kvm_vcpu *vcpu,
1097 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001098static bool guest_state_valid(struct kvm_vcpu *vcpu);
1099static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001100static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001101static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1102static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1103static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1104 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001105static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001106static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1107 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1110static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001111/*
1112 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1113 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1114 */
1115static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116
Feng Wubf9f6ac2015-09-18 22:29:55 +08001117/*
1118 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1119 * can find which vCPU should be waken up.
1120 */
1121static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1122static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1123
Radim Krčmář23611332016-09-29 22:41:33 +02001124enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001125 VMX_VMREAD_BITMAP,
1126 VMX_VMWRITE_BITMAP,
1127 VMX_BITMAP_NR
1128};
1129
1130static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1131
Radim Krčmář23611332016-09-29 22:41:33 +02001132#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1133#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001134
Avi Kivity110312c2010-12-21 12:54:20 +02001135static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001136static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001137
Sheng Yang2384d2b2008-01-17 15:14:33 +08001138static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1139static DEFINE_SPINLOCK(vmx_vpid_lock);
1140
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001141static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 int size;
1143 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001144 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001146 u32 pin_based_exec_ctrl;
1147 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001148 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001149 u32 vmexit_ctrl;
1150 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001151 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001152} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153
Hannes Ederefff9e52008-11-28 17:02:06 +01001154static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001155 u32 ept;
1156 u32 vpid;
1157} vmx_capability;
1158
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159#define VMX_SEGMENT_FIELD(seg) \
1160 [VCPU_SREG_##seg] = { \
1161 .selector = GUEST_##seg##_SELECTOR, \
1162 .base = GUEST_##seg##_BASE, \
1163 .limit = GUEST_##seg##_LIMIT, \
1164 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1165 }
1166
Mathias Krause772e0312012-08-30 01:30:19 +02001167static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 unsigned selector;
1169 unsigned base;
1170 unsigned limit;
1171 unsigned ar_bytes;
1172} kvm_vmx_segment_fields[] = {
1173 VMX_SEGMENT_FIELD(CS),
1174 VMX_SEGMENT_FIELD(DS),
1175 VMX_SEGMENT_FIELD(ES),
1176 VMX_SEGMENT_FIELD(FS),
1177 VMX_SEGMENT_FIELD(GS),
1178 VMX_SEGMENT_FIELD(SS),
1179 VMX_SEGMENT_FIELD(TR),
1180 VMX_SEGMENT_FIELD(LDTR),
1181};
1182
Avi Kivity26bb0982009-09-07 11:14:12 +03001183static u64 host_efer;
1184
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001185static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1186
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001187/*
Brian Gerst8c065852010-07-17 09:03:26 -04001188 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001189 * away by decrementing the array size.
1190 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001192#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001193 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001195 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001198DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1199
1200#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1201
1202#define KVM_EVMCS_VERSION 1
1203
1204#if IS_ENABLED(CONFIG_HYPERV)
1205static bool __read_mostly enlightened_vmcs = true;
1206module_param(enlightened_vmcs, bool, 0444);
1207
1208static inline void evmcs_write64(unsigned long field, u64 value)
1209{
1210 u16 clean_field;
1211 int offset = get_evmcs_offset(field, &clean_field);
1212
1213 if (offset < 0)
1214 return;
1215
1216 *(u64 *)((char *)current_evmcs + offset) = value;
1217
1218 current_evmcs->hv_clean_fields &= ~clean_field;
1219}
1220
1221static inline void evmcs_write32(unsigned long field, u32 value)
1222{
1223 u16 clean_field;
1224 int offset = get_evmcs_offset(field, &clean_field);
1225
1226 if (offset < 0)
1227 return;
1228
1229 *(u32 *)((char *)current_evmcs + offset) = value;
1230 current_evmcs->hv_clean_fields &= ~clean_field;
1231}
1232
1233static inline void evmcs_write16(unsigned long field, u16 value)
1234{
1235 u16 clean_field;
1236 int offset = get_evmcs_offset(field, &clean_field);
1237
1238 if (offset < 0)
1239 return;
1240
1241 *(u16 *)((char *)current_evmcs + offset) = value;
1242 current_evmcs->hv_clean_fields &= ~clean_field;
1243}
1244
1245static inline u64 evmcs_read64(unsigned long field)
1246{
1247 int offset = get_evmcs_offset(field, NULL);
1248
1249 if (offset < 0)
1250 return 0;
1251
1252 return *(u64 *)((char *)current_evmcs + offset);
1253}
1254
1255static inline u32 evmcs_read32(unsigned long field)
1256{
1257 int offset = get_evmcs_offset(field, NULL);
1258
1259 if (offset < 0)
1260 return 0;
1261
1262 return *(u32 *)((char *)current_evmcs + offset);
1263}
1264
1265static inline u16 evmcs_read16(unsigned long field)
1266{
1267 int offset = get_evmcs_offset(field, NULL);
1268
1269 if (offset < 0)
1270 return 0;
1271
1272 return *(u16 *)((char *)current_evmcs + offset);
1273}
1274
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001275static inline void evmcs_touch_msr_bitmap(void)
1276{
1277 if (unlikely(!current_evmcs))
1278 return;
1279
1280 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1281 current_evmcs->hv_clean_fields &=
1282 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1283}
1284
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001285static void evmcs_load(u64 phys_addr)
1286{
1287 struct hv_vp_assist_page *vp_ap =
1288 hv_get_vp_assist_page(smp_processor_id());
1289
1290 vp_ap->current_nested_vmcs = phys_addr;
1291 vp_ap->enlighten_vmentry = 1;
1292}
1293
1294static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1295{
1296 /*
1297 * Enlightened VMCSv1 doesn't support these:
1298 *
1299 * POSTED_INTR_NV = 0x00000002,
1300 * GUEST_INTR_STATUS = 0x00000810,
1301 * APIC_ACCESS_ADDR = 0x00002014,
1302 * POSTED_INTR_DESC_ADDR = 0x00002016,
1303 * EOI_EXIT_BITMAP0 = 0x0000201c,
1304 * EOI_EXIT_BITMAP1 = 0x0000201e,
1305 * EOI_EXIT_BITMAP2 = 0x00002020,
1306 * EOI_EXIT_BITMAP3 = 0x00002022,
1307 */
1308 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1309 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1310 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1311 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1312 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1313 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1314 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1315
1316 /*
1317 * GUEST_PML_INDEX = 0x00000812,
1318 * PML_ADDRESS = 0x0000200e,
1319 */
1320 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1321
1322 /* VM_FUNCTION_CONTROL = 0x00002018, */
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1324
1325 /*
1326 * EPTP_LIST_ADDRESS = 0x00002024,
1327 * VMREAD_BITMAP = 0x00002026,
1328 * VMWRITE_BITMAP = 0x00002028,
1329 */
1330 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1331
1332 /*
1333 * TSC_MULTIPLIER = 0x00002032,
1334 */
1335 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1336
1337 /*
1338 * PLE_GAP = 0x00004020,
1339 * PLE_WINDOW = 0x00004022,
1340 */
1341 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1342
1343 /*
1344 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1345 */
1346 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1347
1348 /*
1349 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1350 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1351 */
1352 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1353 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1354
1355 /*
1356 * Currently unsupported in KVM:
1357 * GUEST_IA32_RTIT_CTL = 0x00002814,
1358 */
1359}
1360#else /* !IS_ENABLED(CONFIG_HYPERV) */
1361static inline void evmcs_write64(unsigned long field, u64 value) {}
1362static inline void evmcs_write32(unsigned long field, u32 value) {}
1363static inline void evmcs_write16(unsigned long field, u16 value) {}
1364static inline u64 evmcs_read64(unsigned long field) { return 0; }
1365static inline u32 evmcs_read32(unsigned long field) { return 0; }
1366static inline u16 evmcs_read16(unsigned long field) { return 0; }
1367static inline void evmcs_load(u64 phys_addr) {}
1368static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001369static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001370#endif /* IS_ENABLED(CONFIG_HYPERV) */
1371
Jan Kiszka5bb16012016-02-09 20:14:21 +01001372static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373{
1374 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1375 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001376 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1377}
1378
Jan Kiszka6f054852016-02-09 20:15:18 +01001379static inline bool is_debug(u32 intr_info)
1380{
1381 return is_exception_n(intr_info, DB_VECTOR);
1382}
1383
1384static inline bool is_breakpoint(u32 intr_info)
1385{
1386 return is_exception_n(intr_info, BP_VECTOR);
1387}
1388
Jan Kiszka5bb16012016-02-09 20:14:21 +01001389static inline bool is_page_fault(u32 intr_info)
1390{
1391 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392}
1393
Gui Jianfeng31299942010-03-15 17:29:09 +08001394static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001395{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001396 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001397}
1398
Gui Jianfeng31299942010-03-15 17:29:09 +08001399static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001400{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001401 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001402}
1403
Liran Alon9e869482018-03-12 13:12:51 +02001404static inline bool is_gp_fault(u32 intr_info)
1405{
1406 return is_exception_n(intr_info, GP_VECTOR);
1407}
1408
Gui Jianfeng31299942010-03-15 17:29:09 +08001409static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
1411 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1412 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1413}
1414
Gui Jianfeng31299942010-03-15 17:29:09 +08001415static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001416{
1417 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1418 INTR_INFO_VALID_MASK)) ==
1419 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1420}
1421
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001422/* Undocumented: icebp/int1 */
1423static inline bool is_icebp(u32 intr_info)
1424{
1425 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1426 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001430{
Sheng Yang04547152009-04-01 15:52:31 +08001431 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001432}
1433
Gui Jianfeng31299942010-03-15 17:29:09 +08001434static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001435{
Sheng Yang04547152009-04-01 15:52:31 +08001436 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001437}
1438
Paolo Bonzini35754c92015-07-29 12:05:37 +02001439static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001440{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001441 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001442}
1443
Gui Jianfeng31299942010-03-15 17:29:09 +08001444static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001445{
Sheng Yang04547152009-04-01 15:52:31 +08001446 return vmcs_config.cpu_based_exec_ctrl &
1447 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001448}
1449
Avi Kivity774ead32007-12-26 13:57:04 +02001450static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001451{
Sheng Yang04547152009-04-01 15:52:31 +08001452 return vmcs_config.cpu_based_2nd_exec_ctrl &
1453 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1454}
1455
Yang Zhang8d146952013-01-25 10:18:50 +08001456static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1457{
1458 return vmcs_config.cpu_based_2nd_exec_ctrl &
1459 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1460}
1461
Yang Zhang83d4c282013-01-25 10:18:49 +08001462static inline bool cpu_has_vmx_apic_register_virt(void)
1463{
1464 return vmcs_config.cpu_based_2nd_exec_ctrl &
1465 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1466}
1467
Yang Zhangc7c9c562013-01-25 10:18:51 +08001468static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1469{
1470 return vmcs_config.cpu_based_2nd_exec_ctrl &
1471 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1472}
1473
Yunhong Jiang64672c92016-06-13 14:19:59 -07001474/*
1475 * Comment's format: document - errata name - stepping - processor name.
1476 * Refer from
1477 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1478 */
1479static u32 vmx_preemption_cpu_tfms[] = {
1480/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
14810x000206E6,
1482/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1483/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1484/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
14850x00020652,
1486/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
14870x00020655,
1488/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1489/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1490/*
1491 * 320767.pdf - AAP86 - B1 -
1492 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1493 */
14940x000106E5,
1495/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
14960x000106A0,
1497/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
14980x000106A1,
1499/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15000x000106A4,
1501 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1502 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1503 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15040x000106A5,
1505};
1506
1507static inline bool cpu_has_broken_vmx_preemption_timer(void)
1508{
1509 u32 eax = cpuid_eax(0x00000001), i;
1510
1511 /* Clear the reserved bits */
1512 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001513 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001514 if (eax == vmx_preemption_cpu_tfms[i])
1515 return true;
1516
1517 return false;
1518}
1519
1520static inline bool cpu_has_vmx_preemption_timer(void)
1521{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001522 return vmcs_config.pin_based_exec_ctrl &
1523 PIN_BASED_VMX_PREEMPTION_TIMER;
1524}
1525
Yang Zhang01e439b2013-04-11 19:25:12 +08001526static inline bool cpu_has_vmx_posted_intr(void)
1527{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001528 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1529 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001530}
1531
1532static inline bool cpu_has_vmx_apicv(void)
1533{
1534 return cpu_has_vmx_apic_register_virt() &&
1535 cpu_has_vmx_virtual_intr_delivery() &&
1536 cpu_has_vmx_posted_intr();
1537}
1538
Sheng Yang04547152009-04-01 15:52:31 +08001539static inline bool cpu_has_vmx_flexpriority(void)
1540{
1541 return cpu_has_vmx_tpr_shadow() &&
1542 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001543}
1544
Marcelo Tosattie7997942009-06-11 12:07:40 -03001545static inline bool cpu_has_vmx_ept_execute_only(void)
1546{
Gui Jianfeng31299942010-03-15 17:29:09 +08001547 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001548}
1549
Marcelo Tosattie7997942009-06-11 12:07:40 -03001550static inline bool cpu_has_vmx_ept_2m_page(void)
1551{
Gui Jianfeng31299942010-03-15 17:29:09 +08001552 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001553}
1554
Sheng Yang878403b2010-01-05 19:02:29 +08001555static inline bool cpu_has_vmx_ept_1g_page(void)
1556{
Gui Jianfeng31299942010-03-15 17:29:09 +08001557 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001558}
1559
Sheng Yang4bc9b982010-06-02 14:05:24 +08001560static inline bool cpu_has_vmx_ept_4levels(void)
1561{
1562 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1563}
1564
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001565static inline bool cpu_has_vmx_ept_mt_wb(void)
1566{
1567 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1568}
1569
Yu Zhang855feb62017-08-24 20:27:55 +08001570static inline bool cpu_has_vmx_ept_5levels(void)
1571{
1572 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1573}
1574
Xudong Hao83c3a332012-05-28 19:33:35 +08001575static inline bool cpu_has_vmx_ept_ad_bits(void)
1576{
1577 return vmx_capability.ept & VMX_EPT_AD_BIT;
1578}
1579
Gui Jianfeng31299942010-03-15 17:29:09 +08001580static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001581{
Gui Jianfeng31299942010-03-15 17:29:09 +08001582 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001583}
1584
Gui Jianfeng31299942010-03-15 17:29:09 +08001585static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001586{
Gui Jianfeng31299942010-03-15 17:29:09 +08001587 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001588}
1589
Liran Aloncd9a4912018-05-22 17:16:15 +03001590static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1591{
1592 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1593}
1594
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001595static inline bool cpu_has_vmx_invvpid_single(void)
1596{
1597 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1598}
1599
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001600static inline bool cpu_has_vmx_invvpid_global(void)
1601{
1602 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1603}
1604
Wanpeng Li08d839c2017-03-23 05:30:08 -07001605static inline bool cpu_has_vmx_invvpid(void)
1606{
1607 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1608}
1609
Gui Jianfeng31299942010-03-15 17:29:09 +08001610static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001611{
Sheng Yang04547152009-04-01 15:52:31 +08001612 return vmcs_config.cpu_based_2nd_exec_ctrl &
1613 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001617{
1618 return vmcs_config.cpu_based_2nd_exec_ctrl &
1619 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1620}
1621
Gui Jianfeng31299942010-03-15 17:29:09 +08001622static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001623{
1624 return vmcs_config.cpu_based_2nd_exec_ctrl &
1625 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1626}
1627
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001628static inline bool cpu_has_vmx_basic_inout(void)
1629{
1630 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1631}
1632
Paolo Bonzini35754c92015-07-29 12:05:37 +02001633static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001634{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001635 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001636}
1637
Gui Jianfeng31299942010-03-15 17:29:09 +08001638static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001639{
Sheng Yang04547152009-04-01 15:52:31 +08001640 return vmcs_config.cpu_based_2nd_exec_ctrl &
1641 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001642}
1643
Gui Jianfeng31299942010-03-15 17:29:09 +08001644static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001645{
1646 return vmcs_config.cpu_based_2nd_exec_ctrl &
1647 SECONDARY_EXEC_RDTSCP;
1648}
1649
Mao, Junjiead756a12012-07-02 01:18:48 +00001650static inline bool cpu_has_vmx_invpcid(void)
1651{
1652 return vmcs_config.cpu_based_2nd_exec_ctrl &
1653 SECONDARY_EXEC_ENABLE_INVPCID;
1654}
1655
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001656static inline bool cpu_has_virtual_nmis(void)
1657{
1658 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1659}
1660
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001661static inline bool cpu_has_vmx_wbinvd_exit(void)
1662{
1663 return vmcs_config.cpu_based_2nd_exec_ctrl &
1664 SECONDARY_EXEC_WBINVD_EXITING;
1665}
1666
Abel Gordonabc4fc52013-04-18 14:35:25 +03001667static inline bool cpu_has_vmx_shadow_vmcs(void)
1668{
1669 u64 vmx_msr;
1670 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1671 /* check if the cpu supports writing r/o exit information fields */
1672 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1673 return false;
1674
1675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_SHADOW_VMCS;
1677}
1678
Kai Huang843e4332015-01-28 10:54:28 +08001679static inline bool cpu_has_vmx_pml(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1682}
1683
Haozhong Zhang64903d62015-10-20 15:39:09 +08001684static inline bool cpu_has_vmx_tsc_scaling(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_TSC_SCALING;
1688}
1689
Bandan Das2a499e42017-08-03 15:54:41 -04001690static inline bool cpu_has_vmx_vmfunc(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_ENABLE_VMFUNC;
1694}
1695
Sean Christopherson64f7a112018-04-30 10:01:06 -07001696static bool vmx_umip_emulated(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_DESC;
1700}
1701
Sheng Yang04547152009-04-01 15:52:31 +08001702static inline bool report_flexpriority(void)
1703{
1704 return flexpriority_enabled;
1705}
1706
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001707static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1708{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001709 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001710}
1711
Jim Mattsonf4160e42018-05-29 09:11:33 -07001712/*
1713 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1714 * to modify any valid field of the VMCS, or are the VM-exit
1715 * information fields read-only?
1716 */
1717static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1718{
1719 return to_vmx(vcpu)->nested.msrs.misc_low &
1720 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1721}
1722
Marc Orr04473782018-06-20 17:21:29 -07001723static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1724{
1725 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1726}
1727
1728static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1729{
1730 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1731 CPU_BASED_MONITOR_TRAP_FLAG;
1732}
1733
Liran Alonfa97d7d2018-07-18 14:07:59 +02001734static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1735{
1736 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1737 SECONDARY_EXEC_SHADOW_VMCS;
1738}
1739
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001740static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1741{
1742 return vmcs12->cpu_based_vm_exec_control & bit;
1743}
1744
1745static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1746{
1747 return (vmcs12->cpu_based_vm_exec_control &
1748 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1749 (vmcs12->secondary_vm_exec_control & bit);
1750}
1751
Jan Kiszkaf4124502014-03-07 20:03:13 +01001752static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1753{
1754 return vmcs12->pin_based_vm_exec_control &
1755 PIN_BASED_VMX_PREEMPTION_TIMER;
1756}
1757
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001758static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1759{
1760 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1761}
1762
1763static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1764{
1765 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1766}
1767
Nadav Har'El155a97a2013-08-05 11:07:16 +03001768static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1769{
1770 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1771}
1772
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001773static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1774{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001775 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001776}
1777
Bandan Dasc5f983f2017-05-05 15:25:14 -04001778static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1779{
1780 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1781}
1782
Wincy Vanf2b93282015-02-03 23:56:03 +08001783static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1784{
1785 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1786}
1787
Wanpeng Li5c614b32015-10-13 09:18:36 -07001788static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1789{
1790 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1791}
1792
Wincy Van82f0dd42015-02-03 23:57:18 +08001793static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1794{
1795 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1796}
1797
Wincy Van608406e2015-02-03 23:57:51 +08001798static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1799{
1800 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1801}
1802
Wincy Van705699a2015-02-03 23:58:17 +08001803static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1804{
1805 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1806}
1807
Bandan Das27c42a12017-08-03 15:54:42 -04001808static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1809{
1810 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1811}
1812
Bandan Das41ab9372017-08-03 15:54:43 -04001813static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1814{
1815 return nested_cpu_has_vmfunc(vmcs12) &&
1816 (vmcs12->vm_function_control &
1817 VMX_VMFUNC_EPTP_SWITCHING);
1818}
1819
Liran Alonf792d272018-06-23 02:35:05 +03001820static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1821{
1822 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1823}
1824
Jim Mattsonef85b672016-12-12 11:01:37 -08001825static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001826{
1827 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001828 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001829}
1830
Jan Kiszka533558b2014-01-04 18:47:20 +01001831static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1832 u32 exit_intr_info,
1833 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001834static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1835 struct vmcs12 *vmcs12,
1836 u32 reason, unsigned long qualification);
1837
Rusty Russell8b9cf982007-07-30 16:31:43 +10001838static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001839{
1840 int i;
1841
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001842 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001843 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001844 return i;
1845 return -1;
1846}
1847
Sheng Yang2384d2b2008-01-17 15:14:33 +08001848static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1849{
1850 struct {
1851 u64 vpid : 16;
1852 u64 rsvd : 48;
1853 u64 gva;
1854 } operand = { vpid, 0, gva };
1855
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001856 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001857 /* CF==1 or ZF==1 --> rc = -1 */
1858 "; ja 1f ; ud2 ; 1:"
1859 : : "a"(&operand), "c"(ext) : "cc", "memory");
1860}
1861
Sheng Yang14394422008-04-28 12:24:45 +08001862static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1863{
1864 struct {
1865 u64 eptp, gpa;
1866 } operand = {eptp, gpa};
1867
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001868 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001869 /* CF==1 or ZF==1 --> rc = -1 */
1870 "; ja 1f ; ud2 ; 1:\n"
1871 : : "a" (&operand), "c" (ext) : "cc", "memory");
1872}
1873
Avi Kivity26bb0982009-09-07 11:14:12 +03001874static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001875{
1876 int i;
1877
Rusty Russell8b9cf982007-07-30 16:31:43 +10001878 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001879 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001880 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001881 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001882}
1883
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884static void vmcs_clear(struct vmcs *vmcs)
1885{
1886 u64 phys_addr = __pa(vmcs);
1887 u8 error;
1888
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001889 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001890 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 : "cc", "memory");
1892 if (error)
1893 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1894 vmcs, phys_addr);
1895}
1896
Nadav Har'Eld462b812011-05-24 15:26:10 +03001897static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1898{
1899 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001900 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1901 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001902 loaded_vmcs->cpu = -1;
1903 loaded_vmcs->launched = 0;
1904}
1905
Dongxiao Xu7725b892010-05-11 18:29:38 +08001906static void vmcs_load(struct vmcs *vmcs)
1907{
1908 u64 phys_addr = __pa(vmcs);
1909 u8 error;
1910
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001911 if (static_branch_unlikely(&enable_evmcs))
1912 return evmcs_load(phys_addr);
1913
Dongxiao Xu7725b892010-05-11 18:29:38 +08001914 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001915 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001916 : "cc", "memory");
1917 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001918 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001919 vmcs, phys_addr);
1920}
1921
Dave Young2965faa2015-09-09 15:38:55 -07001922#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001923/*
1924 * This bitmap is used to indicate whether the vmclear
1925 * operation is enabled on all cpus. All disabled by
1926 * default.
1927 */
1928static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1929
1930static inline void crash_enable_local_vmclear(int cpu)
1931{
1932 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1933}
1934
1935static inline void crash_disable_local_vmclear(int cpu)
1936{
1937 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1938}
1939
1940static inline int crash_local_vmclear_enabled(int cpu)
1941{
1942 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1943}
1944
1945static void crash_vmclear_local_loaded_vmcss(void)
1946{
1947 int cpu = raw_smp_processor_id();
1948 struct loaded_vmcs *v;
1949
1950 if (!crash_local_vmclear_enabled(cpu))
1951 return;
1952
1953 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1954 loaded_vmcss_on_cpu_link)
1955 vmcs_clear(v->vmcs);
1956}
1957#else
1958static inline void crash_enable_local_vmclear(int cpu) { }
1959static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001960#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001961
Nadav Har'Eld462b812011-05-24 15:26:10 +03001962static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001964 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001965 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966
Nadav Har'Eld462b812011-05-24 15:26:10 +03001967 if (loaded_vmcs->cpu != cpu)
1968 return; /* vcpu migration can race with cpu offline */
1969 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001971 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001972 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001973
1974 /*
1975 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1976 * is before setting loaded_vmcs->vcpu to -1 which is done in
1977 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1978 * then adds the vmcs into percpu list before it is deleted.
1979 */
1980 smp_wmb();
1981
Nadav Har'Eld462b812011-05-24 15:26:10 +03001982 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001983 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984}
1985
Nadav Har'Eld462b812011-05-24 15:26:10 +03001986static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001987{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001988 int cpu = loaded_vmcs->cpu;
1989
1990 if (cpu != -1)
1991 smp_call_function_single(cpu,
1992 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001993}
1994
Junaid Shahidfaff8752018-06-29 13:10:05 -07001995static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
1996{
1997 if (vpid == 0)
1998 return true;
1999
2000 if (cpu_has_vmx_invvpid_individual_addr()) {
2001 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2002 return true;
2003 }
2004
2005 return false;
2006}
2007
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002008static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002009{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002010 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002011 return;
2012
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002013 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002014 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002015}
2016
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002017static inline void vpid_sync_vcpu_global(void)
2018{
2019 if (cpu_has_vmx_invvpid_global())
2020 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2021}
2022
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002023static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002024{
2025 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002026 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002027 else
2028 vpid_sync_vcpu_global();
2029}
2030
Sheng Yang14394422008-04-28 12:24:45 +08002031static inline void ept_sync_global(void)
2032{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002033 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002034}
2035
2036static inline void ept_sync_context(u64 eptp)
2037{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002038 if (cpu_has_vmx_invept_context())
2039 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2040 else
2041 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002042}
2043
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002044static __always_inline void vmcs_check16(unsigned long field)
2045{
2046 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2047 "16-bit accessor invalid for 64-bit field");
2048 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2049 "16-bit accessor invalid for 64-bit high field");
2050 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2051 "16-bit accessor invalid for 32-bit high field");
2052 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2053 "16-bit accessor invalid for natural width field");
2054}
2055
2056static __always_inline void vmcs_check32(unsigned long field)
2057{
2058 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2059 "32-bit accessor invalid for 16-bit field");
2060 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2061 "32-bit accessor invalid for natural width field");
2062}
2063
2064static __always_inline void vmcs_check64(unsigned long field)
2065{
2066 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2067 "64-bit accessor invalid for 16-bit field");
2068 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2069 "64-bit accessor invalid for 64-bit high field");
2070 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2071 "64-bit accessor invalid for 32-bit field");
2072 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2073 "64-bit accessor invalid for natural width field");
2074}
2075
2076static __always_inline void vmcs_checkl(unsigned long field)
2077{
2078 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2079 "Natural width accessor invalid for 16-bit field");
2080 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2081 "Natural width accessor invalid for 64-bit field");
2082 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2083 "Natural width accessor invalid for 64-bit high field");
2084 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2085 "Natural width accessor invalid for 32-bit field");
2086}
2087
2088static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089{
Avi Kivity5e520e62011-05-15 10:13:12 -04002090 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091
Avi Kivity5e520e62011-05-15 10:13:12 -04002092 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2093 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 return value;
2095}
2096
Avi Kivity96304212011-05-15 10:13:13 -04002097static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002099 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002100 if (static_branch_unlikely(&enable_evmcs))
2101 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002102 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103}
2104
Avi Kivity96304212011-05-15 10:13:13 -04002105static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002107 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002108 if (static_branch_unlikely(&enable_evmcs))
2109 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002110 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111}
2112
Avi Kivity96304212011-05-15 10:13:13 -04002113static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002115 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002116 if (static_branch_unlikely(&enable_evmcs))
2117 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002118#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002119 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002121 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122#endif
2123}
2124
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002125static __always_inline unsigned long vmcs_readl(unsigned long field)
2126{
2127 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002128 if (static_branch_unlikely(&enable_evmcs))
2129 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002130 return __vmcs_readl(field);
2131}
2132
Avi Kivitye52de1b2007-01-05 16:36:56 -08002133static noinline void vmwrite_error(unsigned long field, unsigned long value)
2134{
2135 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2136 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2137 dump_stack();
2138}
2139
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002140static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141{
2142 u8 error;
2143
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002144 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002145 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002146 if (unlikely(error))
2147 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148}
2149
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002150static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002152 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002153 if (static_branch_unlikely(&enable_evmcs))
2154 return evmcs_write16(field, value);
2155
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002156 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157}
2158
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002159static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002161 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002162 if (static_branch_unlikely(&enable_evmcs))
2163 return evmcs_write32(field, value);
2164
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002165 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166}
2167
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002168static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002170 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002171 if (static_branch_unlikely(&enable_evmcs))
2172 return evmcs_write64(field, value);
2173
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002174 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002175#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178#endif
2179}
2180
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002181static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002182{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002183 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002184 if (static_branch_unlikely(&enable_evmcs))
2185 return evmcs_write64(field, value);
2186
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002187 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002188}
2189
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002190static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002191{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002192 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2193 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002194 if (static_branch_unlikely(&enable_evmcs))
2195 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2196
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002197 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2198}
2199
2200static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2201{
2202 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2203 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002204 if (static_branch_unlikely(&enable_evmcs))
2205 return evmcs_write32(field, evmcs_read32(field) | mask);
2206
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002207 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002208}
2209
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002210static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2211{
2212 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2213}
2214
Gleb Natapov2961e8762013-11-25 15:37:13 +02002215static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2216{
2217 vmcs_write32(VM_ENTRY_CONTROLS, val);
2218 vmx->vm_entry_controls_shadow = val;
2219}
2220
2221static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2222{
2223 if (vmx->vm_entry_controls_shadow != val)
2224 vm_entry_controls_init(vmx, val);
2225}
2226
2227static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2228{
2229 return vmx->vm_entry_controls_shadow;
2230}
2231
2232
2233static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2234{
2235 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2236}
2237
2238static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2239{
2240 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2241}
2242
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002243static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2244{
2245 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2246}
2247
Gleb Natapov2961e8762013-11-25 15:37:13 +02002248static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2249{
2250 vmcs_write32(VM_EXIT_CONTROLS, val);
2251 vmx->vm_exit_controls_shadow = val;
2252}
2253
2254static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2255{
2256 if (vmx->vm_exit_controls_shadow != val)
2257 vm_exit_controls_init(vmx, val);
2258}
2259
2260static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2261{
2262 return vmx->vm_exit_controls_shadow;
2263}
2264
2265
2266static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2267{
2268 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2269}
2270
2271static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2272{
2273 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2274}
2275
Avi Kivity2fb92db2011-04-27 19:42:18 +03002276static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2277{
2278 vmx->segment_cache.bitmask = 0;
2279}
2280
2281static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2282 unsigned field)
2283{
2284 bool ret;
2285 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2286
2287 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2288 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2289 vmx->segment_cache.bitmask = 0;
2290 }
2291 ret = vmx->segment_cache.bitmask & mask;
2292 vmx->segment_cache.bitmask |= mask;
2293 return ret;
2294}
2295
2296static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2297{
2298 u16 *p = &vmx->segment_cache.seg[seg].selector;
2299
2300 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2301 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2302 return *p;
2303}
2304
2305static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2306{
2307 ulong *p = &vmx->segment_cache.seg[seg].base;
2308
2309 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2310 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2311 return *p;
2312}
2313
2314static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2315{
2316 u32 *p = &vmx->segment_cache.seg[seg].limit;
2317
2318 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2319 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2320 return *p;
2321}
2322
2323static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2324{
2325 u32 *p = &vmx->segment_cache.seg[seg].ar;
2326
2327 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2328 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2329 return *p;
2330}
2331
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002332static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2333{
2334 u32 eb;
2335
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002336 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002337 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002338 /*
2339 * Guest access to VMware backdoor ports could legitimately
2340 * trigger #GP because of TSS I/O permission bitmap.
2341 * We intercept those #GP and allow access to them anyway
2342 * as VMware does.
2343 */
2344 if (enable_vmware_backdoor)
2345 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002346 if ((vcpu->guest_debug &
2347 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2348 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2349 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002350 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002351 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002352 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002353 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002354
2355 /* When we are running a nested L2 guest and L1 specified for it a
2356 * certain exception bitmap, we must trap the same exceptions and pass
2357 * them to L1. When running L2, we will only handle the exceptions
2358 * specified above if L1 did not want them.
2359 */
2360 if (is_guest_mode(vcpu))
2361 eb |= get_vmcs12(vcpu)->exception_bitmap;
2362
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002363 vmcs_write32(EXCEPTION_BITMAP, eb);
2364}
2365
Ashok Raj15d45072018-02-01 22:59:43 +01002366/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002367 * Check if MSR is intercepted for currently loaded MSR bitmap.
2368 */
2369static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2370{
2371 unsigned long *msr_bitmap;
2372 int f = sizeof(unsigned long);
2373
2374 if (!cpu_has_vmx_msr_bitmap())
2375 return true;
2376
2377 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2378
2379 if (msr <= 0x1fff) {
2380 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2381 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2382 msr &= 0x1fff;
2383 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2384 }
2385
2386 return true;
2387}
2388
2389/*
Ashok Raj15d45072018-02-01 22:59:43 +01002390 * Check if MSR is intercepted for L01 MSR bitmap.
2391 */
2392static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2393{
2394 unsigned long *msr_bitmap;
2395 int f = sizeof(unsigned long);
2396
2397 if (!cpu_has_vmx_msr_bitmap())
2398 return true;
2399
2400 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2401
2402 if (msr <= 0x1fff) {
2403 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2404 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2405 msr &= 0x1fff;
2406 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2407 }
2408
2409 return true;
2410}
2411
Gleb Natapov2961e8762013-11-25 15:37:13 +02002412static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2413 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002414{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002415 vm_entry_controls_clearbit(vmx, entry);
2416 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002417}
2418
Avi Kivity61d2ef22010-04-28 16:40:38 +03002419static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2420{
2421 unsigned i;
2422 struct msr_autoload *m = &vmx->msr_autoload;
2423
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002424 switch (msr) {
2425 case MSR_EFER:
2426 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002427 clear_atomic_switch_msr_special(vmx,
2428 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002429 VM_EXIT_LOAD_IA32_EFER);
2430 return;
2431 }
2432 break;
2433 case MSR_CORE_PERF_GLOBAL_CTRL:
2434 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002435 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002436 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2437 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2438 return;
2439 }
2440 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002441 }
2442
Avi Kivity61d2ef22010-04-28 16:40:38 +03002443 for (i = 0; i < m->nr; ++i)
2444 if (m->guest[i].index == msr)
2445 break;
2446
2447 if (i == m->nr)
2448 return;
2449 --m->nr;
2450 m->guest[i] = m->guest[m->nr];
2451 m->host[i] = m->host[m->nr];
2452 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2453 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2454}
2455
Gleb Natapov2961e8762013-11-25 15:37:13 +02002456static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2457 unsigned long entry, unsigned long exit,
2458 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2459 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002460{
2461 vmcs_write64(guest_val_vmcs, guest_val);
2462 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002463 vm_entry_controls_setbit(vmx, entry);
2464 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002465}
2466
Avi Kivity61d2ef22010-04-28 16:40:38 +03002467static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2468 u64 guest_val, u64 host_val)
2469{
2470 unsigned i;
2471 struct msr_autoload *m = &vmx->msr_autoload;
2472
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002473 switch (msr) {
2474 case MSR_EFER:
2475 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002476 add_atomic_switch_msr_special(vmx,
2477 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002478 VM_EXIT_LOAD_IA32_EFER,
2479 GUEST_IA32_EFER,
2480 HOST_IA32_EFER,
2481 guest_val, host_val);
2482 return;
2483 }
2484 break;
2485 case MSR_CORE_PERF_GLOBAL_CTRL:
2486 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002487 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002488 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2489 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2490 GUEST_IA32_PERF_GLOBAL_CTRL,
2491 HOST_IA32_PERF_GLOBAL_CTRL,
2492 guest_val, host_val);
2493 return;
2494 }
2495 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002496 case MSR_IA32_PEBS_ENABLE:
2497 /* PEBS needs a quiescent period after being disabled (to write
2498 * a record). Disabling PEBS through VMX MSR swapping doesn't
2499 * provide that period, so a CPU could write host's record into
2500 * guest's memory.
2501 */
2502 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002503 }
2504
Avi Kivity61d2ef22010-04-28 16:40:38 +03002505 for (i = 0; i < m->nr; ++i)
2506 if (m->guest[i].index == msr)
2507 break;
2508
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002509 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002510 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002511 "Can't add msr %x\n", msr);
2512 return;
2513 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002514 ++m->nr;
2515 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2516 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2517 }
2518
2519 m->guest[i].index = msr;
2520 m->guest[i].value = guest_val;
2521 m->host[i].index = msr;
2522 m->host[i].value = host_val;
2523}
2524
Avi Kivity92c0d902009-10-29 11:00:16 +02002525static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002526{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002527 u64 guest_efer = vmx->vcpu.arch.efer;
2528 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002529
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002530 if (!enable_ept) {
2531 /*
2532 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2533 * host CPUID is more efficient than testing guest CPUID
2534 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2535 */
2536 if (boot_cpu_has(X86_FEATURE_SMEP))
2537 guest_efer |= EFER_NX;
2538 else if (!(guest_efer & EFER_NX))
2539 ignore_bits |= EFER_NX;
2540 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002541
Avi Kivity51c6cf62007-08-29 03:48:05 +03002542 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002543 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002544 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002545 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002546#ifdef CONFIG_X86_64
2547 ignore_bits |= EFER_LMA | EFER_LME;
2548 /* SCE is meaningful only in long mode on Intel */
2549 if (guest_efer & EFER_LMA)
2550 ignore_bits &= ~(u64)EFER_SCE;
2551#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002552
2553 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002554
2555 /*
2556 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2557 * On CPUs that support "load IA32_EFER", always switch EFER
2558 * atomically, since it's faster than switching it manually.
2559 */
2560 if (cpu_has_load_ia32_efer ||
2561 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002562 if (!(guest_efer & EFER_LMA))
2563 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002564 if (guest_efer != host_efer)
2565 add_atomic_switch_msr(vmx, MSR_EFER,
2566 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002567 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002568 } else {
2569 guest_efer &= ~ignore_bits;
2570 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002571
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002572 vmx->guest_msrs[efer_offset].data = guest_efer;
2573 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2574
2575 return true;
2576 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002577}
2578
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002579#ifdef CONFIG_X86_32
2580/*
2581 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2582 * VMCS rather than the segment table. KVM uses this helper to figure
2583 * out the current bases to poke them into the VMCS before entry.
2584 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002585static unsigned long segment_base(u16 selector)
2586{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002587 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002588 unsigned long v;
2589
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002590 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002591 return 0;
2592
Thomas Garnier45fc8752017-03-14 10:05:08 -07002593 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002594
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002595 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002596 u16 ldt_selector = kvm_read_ldt();
2597
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002598 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002599 return 0;
2600
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002601 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002602 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002603 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002604 return v;
2605}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002606#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002607
Avi Kivity04d2cc72007-09-10 18:10:54 +03002608static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002609{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002610 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002611#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002612 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002613 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002614#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002615 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002616
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002617 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002618 return;
2619
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002620 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002621 /*
2622 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2623 * allow segment selectors with cpl > 0 or ti == 1.
2624 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002625 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002626 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002627
2628#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002629 if (likely(is_64bit_mm(current->mm))) {
2630 save_fsgs_for_kvm();
2631 vmx->host_state.fs_sel = current->thread.fsindex;
2632 vmx->host_state.gs_sel = current->thread.gsindex;
2633 fs_base = current->thread.fsbase;
2634 kernel_gs_base = current->thread.gsbase;
2635 } else {
2636#endif
2637 savesegment(fs, vmx->host_state.fs_sel);
2638 savesegment(gs, vmx->host_state.gs_sel);
2639#ifdef CONFIG_X86_64
2640 fs_base = read_msr(MSR_FS_BASE);
2641 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2642 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002643#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002644 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002645 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002646 vmx->host_state.fs_reload_needed = 0;
2647 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002648 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002649 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002650 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002651 if (!(vmx->host_state.gs_sel & 7))
2652 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002653 else {
2654 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002655 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002656 }
2657
2658#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002659 savesegment(ds, vmx->host_state.ds_sel);
2660 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002661
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002662 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002663 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002664
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002665 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002666 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002667 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002668#else
2669 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2670 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2671#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002672 for (i = 0; i < vmx->save_nmsrs; ++i)
2673 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002674 vmx->guest_msrs[i].data,
2675 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002676}
2677
Avi Kivitya9b21b62008-06-24 11:48:49 +03002678static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002679{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002680 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002681 return;
2682
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002683 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002684 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002685#ifdef CONFIG_X86_64
2686 if (is_long_mode(&vmx->vcpu))
2687 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2688#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002689 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002690 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002691#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002692 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002693#else
2694 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002695#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002696 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002697 if (vmx->host_state.fs_reload_needed)
2698 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002699#ifdef CONFIG_X86_64
2700 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2701 loadsegment(ds, vmx->host_state.ds_sel);
2702 loadsegment(es, vmx->host_state.es_sel);
2703 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002704#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002705 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002706#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002707 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002708#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002709 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002710}
2711
Avi Kivitya9b21b62008-06-24 11:48:49 +03002712static void vmx_load_host_state(struct vcpu_vmx *vmx)
2713{
2714 preempt_disable();
2715 __vmx_load_host_state(vmx);
2716 preempt_enable();
2717}
2718
Feng Wu28b835d2015-09-18 22:29:54 +08002719static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2720{
2721 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2722 struct pi_desc old, new;
2723 unsigned int dest;
2724
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002725 /*
2726 * In case of hot-plug or hot-unplug, we may have to undo
2727 * vmx_vcpu_pi_put even if there is no assigned device. And we
2728 * always keep PI.NDST up to date for simplicity: it makes the
2729 * code easier, and CPU migration is not a fast path.
2730 */
2731 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002732 return;
2733
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002734 /*
2735 * First handle the simple case where no cmpxchg is necessary; just
2736 * allow posting non-urgent interrupts.
2737 *
2738 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2739 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2740 * expects the VCPU to be on the blocked_vcpu_list that matches
2741 * PI.NDST.
2742 */
2743 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2744 vcpu->cpu == cpu) {
2745 pi_clear_sn(pi_desc);
2746 return;
2747 }
2748
2749 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002750 do {
2751 old.control = new.control = pi_desc->control;
2752
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002753 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002754
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002755 if (x2apic_enabled())
2756 new.ndst = dest;
2757 else
2758 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002759
Feng Wu28b835d2015-09-18 22:29:54 +08002760 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002761 } while (cmpxchg64(&pi_desc->control, old.control,
2762 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002763}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002764
Peter Feinerc95ba922016-08-17 09:36:47 -07002765static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2766{
2767 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2768 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2769}
2770
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771/*
2772 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2773 * vcpu mutex is already taken.
2774 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002775static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002777 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002778 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002780 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002781 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002782 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002783 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002784
2785 /*
2786 * Read loaded_vmcs->cpu should be before fetching
2787 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2788 * See the comments in __loaded_vmcs_clear().
2789 */
2790 smp_rmb();
2791
Nadav Har'Eld462b812011-05-24 15:26:10 +03002792 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2793 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002794 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002795 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002796 }
2797
2798 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2799 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2800 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002801 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002802 }
2803
2804 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002805 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002806 unsigned long sysenter_esp;
2807
2808 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002809
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 /*
2811 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002812 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002814 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002815 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002816 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002818 /*
2819 * VM exits change the host TR limit to 0x67 after a VM
2820 * exit. This is okay, since 0x67 covers everything except
2821 * the IO bitmap and have have code to handle the IO bitmap
2822 * being lost after a VM exit.
2823 */
2824 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2825
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2827 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002828
Nadav Har'Eld462b812011-05-24 15:26:10 +03002829 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 }
Feng Wu28b835d2015-09-18 22:29:54 +08002831
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002832 /* Setup TSC multiplier */
2833 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002834 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2835 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002836
Feng Wu28b835d2015-09-18 22:29:54 +08002837 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002838 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002839 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002840}
2841
2842static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2843{
2844 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2845
2846 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002847 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2848 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002849 return;
2850
2851 /* Set SN when the vCPU is preempted */
2852 if (vcpu->preempted)
2853 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854}
2855
2856static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2857{
Feng Wu28b835d2015-09-18 22:29:54 +08002858 vmx_vcpu_pi_put(vcpu);
2859
Avi Kivitya9b21b62008-06-24 11:48:49 +03002860 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861}
2862
Wanpeng Lif244dee2017-07-20 01:11:54 -07002863static bool emulation_required(struct kvm_vcpu *vcpu)
2864{
2865 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2866}
2867
Avi Kivityedcafe32009-12-30 18:07:40 +02002868static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2869
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002870/*
2871 * Return the cr0 value that a nested guest would read. This is a combination
2872 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2873 * its hypervisor (cr0_read_shadow).
2874 */
2875static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2876{
2877 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2878 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2879}
2880static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2881{
2882 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2883 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2884}
2885
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2887{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002888 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002889
Avi Kivity6de12732011-03-07 12:51:22 +02002890 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2891 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2892 rflags = vmcs_readl(GUEST_RFLAGS);
2893 if (to_vmx(vcpu)->rmode.vm86_active) {
2894 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2895 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2896 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2897 }
2898 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002899 }
Avi Kivity6de12732011-03-07 12:51:22 +02002900 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
2902
2903static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2904{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002905 unsigned long old_rflags = vmx_get_rflags(vcpu);
2906
Avi Kivity6de12732011-03-07 12:51:22 +02002907 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2908 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002909 if (to_vmx(vcpu)->rmode.vm86_active) {
2910 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002911 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002912 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002914
2915 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2916 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917}
2918
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002919static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002920{
2921 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2922 int ret = 0;
2923
2924 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002925 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002926 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002927 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002928
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002929 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002930}
2931
2932static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2933{
2934 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2935 u32 interruptibility = interruptibility_old;
2936
2937 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2938
Jan Kiszka48005f62010-02-19 19:38:07 +01002939 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002940 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002941 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002942 interruptibility |= GUEST_INTR_STATE_STI;
2943
2944 if ((interruptibility != interruptibility_old))
2945 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2946}
2947
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2949{
2950 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002952 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002954 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955
Glauber Costa2809f5d2009-05-12 16:21:05 -04002956 /* skipping an emulated instruction also counts */
2957 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958}
2959
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002960static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2961 unsigned long exit_qual)
2962{
2963 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2964 unsigned int nr = vcpu->arch.exception.nr;
2965 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2966
2967 if (vcpu->arch.exception.has_error_code) {
2968 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2969 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2970 }
2971
2972 if (kvm_exception_is_soft(nr))
2973 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2974 else
2975 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2976
2977 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2978 vmx_get_nmi_mask(vcpu))
2979 intr_info |= INTR_INFO_UNBLOCK_NMI;
2980
2981 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2982}
2983
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002984/*
2985 * KVM wants to inject page-faults which it got to the guest. This function
2986 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002987 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002988static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002989{
2990 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002991 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002992
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002993 if (nr == PF_VECTOR) {
2994 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002995 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002996 return 1;
2997 }
2998 /*
2999 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3000 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3001 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3002 * can be written only when inject_pending_event runs. This should be
3003 * conditional on a new capability---if the capability is disabled,
3004 * kvm_multiple_exception would write the ancillary information to
3005 * CR2 or DR6, for backwards ABI-compatibility.
3006 */
3007 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3008 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003009 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003010 return 1;
3011 }
3012 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003013 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003014 if (nr == DB_VECTOR)
3015 *exit_qual = vcpu->arch.dr6;
3016 else
3017 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003018 return 1;
3019 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003020 }
3021
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003022 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003023}
3024
Wanpeng Licaa057a2018-03-12 04:53:03 -07003025static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3026{
3027 /*
3028 * Ensure that we clear the HLT state in the VMCS. We don't need to
3029 * explicitly skip the instruction because if the HLT state is set,
3030 * then the instruction is already executing and RIP has already been
3031 * advanced.
3032 */
3033 if (kvm_hlt_in_guest(vcpu->kvm) &&
3034 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3035 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3036}
3037
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003038static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003039{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003040 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003041 unsigned nr = vcpu->arch.exception.nr;
3042 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003043 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003044 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003045
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003046 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003047 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003048 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3049 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003050
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003051 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003052 int inc_eip = 0;
3053 if (kvm_exception_is_soft(nr))
3054 inc_eip = vcpu->arch.event_exit_inst_len;
3055 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003056 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003057 return;
3058 }
3059
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003060 WARN_ON_ONCE(vmx->emulation_required);
3061
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003062 if (kvm_exception_is_soft(nr)) {
3063 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3064 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003065 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3066 } else
3067 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3068
3069 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003070
3071 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003072}
3073
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003074static bool vmx_rdtscp_supported(void)
3075{
3076 return cpu_has_vmx_rdtscp();
3077}
3078
Mao, Junjiead756a12012-07-02 01:18:48 +00003079static bool vmx_invpcid_supported(void)
3080{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003081 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003082}
3083
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084/*
Eddie Donga75beee2007-05-17 18:55:15 +03003085 * Swap MSR entry in host/guest MSR entry array.
3086 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003087static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003088{
Avi Kivity26bb0982009-09-07 11:14:12 +03003089 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003090
3091 tmp = vmx->guest_msrs[to];
3092 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3093 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003094}
3095
3096/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003097 * Set up the vmcs to automatically save and restore system
3098 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3099 * mode, as fiddling with msrs is very expensive.
3100 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003101static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003102{
Avi Kivity26bb0982009-09-07 11:14:12 +03003103 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003104
Eddie Donga75beee2007-05-17 18:55:15 +03003105 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003106#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003107 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003108 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003109 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003110 move_msr_up(vmx, index, save_nmsrs++);
3111 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003112 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003113 move_msr_up(vmx, index, save_nmsrs++);
3114 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003115 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003116 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003117 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003118 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003119 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003120 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003121 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003122 * if efer.sce is enabled.
3123 */
Brian Gerst8c065852010-07-17 09:03:26 -04003124 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003125 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003126 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003127 }
Eddie Donga75beee2007-05-17 18:55:15 +03003128#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003129 index = __find_msr_index(vmx, MSR_EFER);
3130 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003131 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003132
Avi Kivity26bb0982009-09-07 11:14:12 +03003133 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003134
Yang Zhang8d146952013-01-25 10:18:50 +08003135 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003136 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003137}
3138
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003139static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003141 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003143 if (is_guest_mode(vcpu) &&
3144 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3145 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3146
3147 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148}
3149
3150/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003151 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003153static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003155 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003156 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003157 * We're here if L1 chose not to trap WRMSR to TSC. According
3158 * to the spec, this should set L1's TSC; The offset that L1
3159 * set for L2 remains unchanged, and still needs to be added
3160 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003161 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003162 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003163 /* recalculate vmcs02.TSC_OFFSET: */
3164 vmcs12 = get_vmcs12(vcpu);
3165 vmcs_write64(TSC_OFFSET, offset +
3166 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3167 vmcs12->tsc_offset : 0));
3168 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003169 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3170 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003171 vmcs_write64(TSC_OFFSET, offset);
3172 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173}
3174
Nadav Har'El801d3422011-05-25 23:02:23 +03003175/*
3176 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3177 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3178 * all guests if the "nested" module option is off, and can also be disabled
3179 * for a single guest by disabling its VMX cpuid bit.
3180 */
3181static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3182{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003183 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003184}
3185
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003187 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3188 * returned for the various VMX controls MSRs when nested VMX is enabled.
3189 * The same values should also be used to verify that vmcs12 control fields are
3190 * valid during nested entry from L1 to L2.
3191 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3192 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3193 * bit in the high half is on if the corresponding bit in the control field
3194 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003195 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003196static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003197{
Paolo Bonzini13893092018-02-26 13:40:09 +01003198 if (!nested) {
3199 memset(msrs, 0, sizeof(*msrs));
3200 return;
3201 }
3202
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003203 /*
3204 * Note that as a general rule, the high half of the MSRs (bits in
3205 * the control fields which may be 1) should be initialized by the
3206 * intersection of the underlying hardware's MSR (i.e., features which
3207 * can be supported) and the list of features we want to expose -
3208 * because they are known to be properly supported in our code.
3209 * Also, usually, the low half of the MSRs (bits which must be 1) can
3210 * be set to 0, meaning that L1 may turn off any of these bits. The
3211 * reason is that if one of these bits is necessary, it will appear
3212 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3213 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003214 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003215 * These rules have exceptions below.
3216 */
3217
3218 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003219 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003220 msrs->pinbased_ctls_low,
3221 msrs->pinbased_ctls_high);
3222 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003223 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003224 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003225 PIN_BASED_EXT_INTR_MASK |
3226 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003227 PIN_BASED_VIRTUAL_NMIS |
3228 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003229 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003230 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003231 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003232
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003233 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003234 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 msrs->exit_ctls_low,
3236 msrs->exit_ctls_high);
3237 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003238 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003239
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003240 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003241#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003242 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003243#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003244 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003245 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003246 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003247 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003248 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3249
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003250 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003251 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003252
Jan Kiszka2996fca2014-06-16 13:59:43 +02003253 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003254 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003255
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003256 /* entry controls */
3257 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003258 msrs->entry_ctls_low,
3259 msrs->entry_ctls_high);
3260 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003261 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003262 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003263#ifdef CONFIG_X86_64
3264 VM_ENTRY_IA32E_MODE |
3265#endif
3266 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003267 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003268 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003269 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003270 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003271
Jan Kiszka2996fca2014-06-16 13:59:43 +02003272 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003273 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003274
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003275 /* cpu-based controls */
3276 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003277 msrs->procbased_ctls_low,
3278 msrs->procbased_ctls_high);
3279 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003280 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003281 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003282 CPU_BASED_VIRTUAL_INTR_PENDING |
3283 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003284 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3285 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3286 CPU_BASED_CR3_STORE_EXITING |
3287#ifdef CONFIG_X86_64
3288 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3289#endif
3290 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003291 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3292 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3293 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3294 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003295 /*
3296 * We can allow some features even when not supported by the
3297 * hardware. For example, L1 can specify an MSR bitmap - and we
3298 * can use it to avoid exits to L1 - even when L0 runs L2
3299 * without MSR bitmaps.
3300 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003301 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003302 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003303 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003304
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003305 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003307 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3308
Paolo Bonzini80154d72017-08-24 13:55:35 +02003309 /*
3310 * secondary cpu-based controls. Do not include those that
3311 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3312 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003313 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->secondary_ctls_low,
3315 msrs->secondary_ctls_high);
3316 msrs->secondary_ctls_low = 0;
3317 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003318 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003319 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003320 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003321 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003322 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003323 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003324 /*
3325 * We can emulate "VMCS shadowing," even if the hardware
3326 * doesn't support it.
3327 */
3328 msrs->secondary_ctls_high |=
3329 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003330
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003331 if (enable_ept) {
3332 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003334 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003336 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003337 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003338 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003339 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->ept_caps &= vmx_capability.ept;
3341 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003342 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3343 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003344 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003345 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003346 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003347 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003348 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003349 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003350
Bandan Das27c42a12017-08-03 15:54:42 -04003351 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003352 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003353 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003354 /*
3355 * Advertise EPTP switching unconditionally
3356 * since we emulate it
3357 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003358 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003359 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003360 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003361 }
3362
Paolo Bonzinief697a72016-03-18 16:58:38 +01003363 /*
3364 * Old versions of KVM use the single-context version without
3365 * checking for support, so declare that it is supported even
3366 * though it is treated as global context. The alternative is
3367 * not failing the single-context invvpid, and it is worse.
3368 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003369 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003370 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003371 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003372 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003373 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003374 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003375
Radim Krčmář0790ec12015-03-17 14:02:32 +01003376 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003377 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003378 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3379
Jan Kiszkac18911a2013-03-13 16:06:41 +01003380 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003381 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003382 msrs->misc_low,
3383 msrs->misc_high);
3384 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3385 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003386 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003387 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003388 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003389 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003390
3391 /*
3392 * This MSR reports some information about VMX support. We
3393 * should return information about the VMX we emulate for the
3394 * guest, and the VMCS structure we give it - not about the
3395 * VMX support of the underlying hardware.
3396 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003398 VMCS12_REVISION |
3399 VMX_BASIC_TRUE_CTLS |
3400 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3401 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3402
3403 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003405
3406 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003407 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003408 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3409 * We picked the standard core2 setting.
3410 */
3411#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3412#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003413 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3414 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003415
3416 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003417 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3418 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003419
3420 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003421 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003422}
3423
David Matlack38991522016-11-29 18:14:08 -08003424/*
3425 * if fixed0[i] == 1: val[i] must be 1
3426 * if fixed1[i] == 0: val[i] must be 0
3427 */
3428static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3429{
3430 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003431}
3432
3433static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3434{
David Matlack38991522016-11-29 18:14:08 -08003435 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003436}
3437
3438static inline u64 vmx_control_msr(u32 low, u32 high)
3439{
3440 return low | ((u64)high << 32);
3441}
3442
David Matlack62cc6b9d2016-11-29 18:14:07 -08003443static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3444{
3445 superset &= mask;
3446 subset &= mask;
3447
3448 return (superset | subset) == superset;
3449}
3450
3451static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3452{
3453 const u64 feature_and_reserved =
3454 /* feature (except bit 48; see below) */
3455 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3456 /* reserved */
3457 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003458 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003459
3460 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3461 return -EINVAL;
3462
3463 /*
3464 * KVM does not emulate a version of VMX that constrains physical
3465 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3466 */
3467 if (data & BIT_ULL(48))
3468 return -EINVAL;
3469
3470 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3471 vmx_basic_vmcs_revision_id(data))
3472 return -EINVAL;
3473
3474 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3475 return -EINVAL;
3476
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003477 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003478 return 0;
3479}
3480
3481static int
3482vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3483{
3484 u64 supported;
3485 u32 *lowp, *highp;
3486
3487 switch (msr_index) {
3488 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003489 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3490 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003491 break;
3492 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003493 lowp = &vmx->nested.msrs.procbased_ctls_low;
3494 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003495 break;
3496 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 lowp = &vmx->nested.msrs.exit_ctls_low;
3498 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003499 break;
3500 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003501 lowp = &vmx->nested.msrs.entry_ctls_low;
3502 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003503 break;
3504 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003505 lowp = &vmx->nested.msrs.secondary_ctls_low;
3506 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003507 break;
3508 default:
3509 BUG();
3510 }
3511
3512 supported = vmx_control_msr(*lowp, *highp);
3513
3514 /* Check must-be-1 bits are still 1. */
3515 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3516 return -EINVAL;
3517
3518 /* Check must-be-0 bits are still 0. */
3519 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3520 return -EINVAL;
3521
3522 *lowp = data;
3523 *highp = data >> 32;
3524 return 0;
3525}
3526
3527static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3528{
3529 const u64 feature_and_reserved_bits =
3530 /* feature */
3531 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3532 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3533 /* reserved */
3534 GENMASK_ULL(13, 9) | BIT_ULL(31);
3535 u64 vmx_misc;
3536
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3538 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003539
3540 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3541 return -EINVAL;
3542
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003544 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3545 vmx_misc_preemption_timer_rate(data) !=
3546 vmx_misc_preemption_timer_rate(vmx_misc))
3547 return -EINVAL;
3548
3549 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3550 return -EINVAL;
3551
3552 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3553 return -EINVAL;
3554
3555 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3556 return -EINVAL;
3557
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003558 vmx->nested.msrs.misc_low = data;
3559 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003560
3561 /*
3562 * If L1 has read-only VM-exit information fields, use the
3563 * less permissive vmx_vmwrite_bitmap to specify write
3564 * permissions for the shadow VMCS.
3565 */
3566 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3567 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3568
David Matlack62cc6b9d2016-11-29 18:14:07 -08003569 return 0;
3570}
3571
3572static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3573{
3574 u64 vmx_ept_vpid_cap;
3575
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3577 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003578
3579 /* Every bit is either reserved or a feature bit. */
3580 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3581 return -EINVAL;
3582
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 vmx->nested.msrs.ept_caps = data;
3584 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003585 return 0;
3586}
3587
3588static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3589{
3590 u64 *msr;
3591
3592 switch (msr_index) {
3593 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003594 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003595 break;
3596 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003597 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003598 break;
3599 default:
3600 BUG();
3601 }
3602
3603 /*
3604 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3605 * must be 1 in the restored value.
3606 */
3607 if (!is_bitwise_subset(data, *msr, -1ULL))
3608 return -EINVAL;
3609
3610 *msr = data;
3611 return 0;
3612}
3613
3614/*
3615 * Called when userspace is restoring VMX MSRs.
3616 *
3617 * Returns 0 on success, non-0 otherwise.
3618 */
3619static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3620{
3621 struct vcpu_vmx *vmx = to_vmx(vcpu);
3622
Jim Mattsona943ac52018-05-29 09:11:32 -07003623 /*
3624 * Don't allow changes to the VMX capability MSRs while the vCPU
3625 * is in VMX operation.
3626 */
3627 if (vmx->nested.vmxon)
3628 return -EBUSY;
3629
David Matlack62cc6b9d2016-11-29 18:14:07 -08003630 switch (msr_index) {
3631 case MSR_IA32_VMX_BASIC:
3632 return vmx_restore_vmx_basic(vmx, data);
3633 case MSR_IA32_VMX_PINBASED_CTLS:
3634 case MSR_IA32_VMX_PROCBASED_CTLS:
3635 case MSR_IA32_VMX_EXIT_CTLS:
3636 case MSR_IA32_VMX_ENTRY_CTLS:
3637 /*
3638 * The "non-true" VMX capability MSRs are generated from the
3639 * "true" MSRs, so we do not support restoring them directly.
3640 *
3641 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3642 * should restore the "true" MSRs with the must-be-1 bits
3643 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3644 * DEFAULT SETTINGS".
3645 */
3646 return -EINVAL;
3647 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3648 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3649 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3650 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3651 case MSR_IA32_VMX_PROCBASED_CTLS2:
3652 return vmx_restore_control_msr(vmx, msr_index, data);
3653 case MSR_IA32_VMX_MISC:
3654 return vmx_restore_vmx_misc(vmx, data);
3655 case MSR_IA32_VMX_CR0_FIXED0:
3656 case MSR_IA32_VMX_CR4_FIXED0:
3657 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3658 case MSR_IA32_VMX_CR0_FIXED1:
3659 case MSR_IA32_VMX_CR4_FIXED1:
3660 /*
3661 * These MSRs are generated based on the vCPU's CPUID, so we
3662 * do not support restoring them directly.
3663 */
3664 return -EINVAL;
3665 case MSR_IA32_VMX_EPT_VPID_CAP:
3666 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3667 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003669 return 0;
3670 default:
3671 /*
3672 * The rest of the VMX capability MSRs do not support restore.
3673 */
3674 return -EINVAL;
3675 }
3676}
3677
Jan Kiszkacae50132014-01-04 18:47:22 +01003678/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003679static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003680{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003681 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003682 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003684 break;
3685 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3686 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003687 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003688 msrs->pinbased_ctls_low,
3689 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003690 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3691 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003692 break;
3693 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3694 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003695 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003696 msrs->procbased_ctls_low,
3697 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003698 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3699 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700 break;
3701 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3702 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003703 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003704 msrs->exit_ctls_low,
3705 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003706 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3707 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708 break;
3709 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3710 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003711 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003712 msrs->entry_ctls_low,
3713 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003714 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3715 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003716 break;
3717 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003718 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003719 msrs->misc_low,
3720 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003721 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003722 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003723 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003724 break;
3725 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003726 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003727 break;
3728 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003729 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003730 break;
3731 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003732 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003733 break;
3734 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 break;
3737 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003738 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 msrs->secondary_ctls_low,
3740 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003741 break;
3742 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003743 *pdata = msrs->ept_caps |
3744 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003745 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003746 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003747 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003748 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003749 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003750 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003751 }
3752
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003753 return 0;
3754}
3755
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003756static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3757 uint64_t val)
3758{
3759 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3760
3761 return !(val & ~valid_bits);
3762}
3763
Tom Lendacky801e4592018-02-21 13:39:51 -06003764static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3765{
Paolo Bonzini13893092018-02-26 13:40:09 +01003766 switch (msr->index) {
3767 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3768 if (!nested)
3769 return 1;
3770 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3771 default:
3772 return 1;
3773 }
3774
3775 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003776}
3777
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003778/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 * Reads an msr value (of 'msr_index') into 'pdata'.
3780 * Returns 0 on success, non-0 otherwise.
3781 * Assumes vcpu_load() was already called.
3782 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003783static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003785 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003786 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003788 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003789#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003791 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003792 break;
3793 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003794 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003796 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003797 vmx_load_host_state(vmx);
3798 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003799 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003800#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003802 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003803 case MSR_IA32_SPEC_CTRL:
3804 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003805 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3806 return 1;
3807
3808 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3809 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003810 case MSR_IA32_ARCH_CAPABILITIES:
3811 if (!msr_info->host_initiated &&
3812 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3813 return 1;
3814 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3815 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003817 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818 break;
3819 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003820 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003821 break;
3822 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003823 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003825 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003826 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003827 (!msr_info->host_initiated &&
3828 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003829 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003830 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003831 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003832 case MSR_IA32_MCG_EXT_CTL:
3833 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003834 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003835 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003836 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003837 msr_info->data = vcpu->arch.mcg_ext_ctl;
3838 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003839 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003840 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003841 break;
3842 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3843 if (!nested_vmx_allowed(vcpu))
3844 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003845 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3846 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003847 case MSR_IA32_XSS:
3848 if (!vmx_xsaves_supported())
3849 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003850 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003851 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003852 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003853 if (!msr_info->host_initiated &&
3854 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003855 return 1;
3856 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003858 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003859 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003860 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003863 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 }
3865
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 return 0;
3867}
3868
Jan Kiszkacae50132014-01-04 18:47:22 +01003869static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3870
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871/*
3872 * Writes msr value into into the appropriate "register".
3873 * Returns 0 on success, non-0 otherwise.
3874 * Assumes vcpu_load() was already called.
3875 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003876static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003878 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003879 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003880 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003881 u32 msr_index = msr_info->index;
3882 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003883
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003885 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003886 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003887 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003888#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003890 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891 vmcs_writel(GUEST_FS_BASE, data);
3892 break;
3893 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003894 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895 vmcs_writel(GUEST_GS_BASE, data);
3896 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003897 case MSR_KERNEL_GS_BASE:
3898 vmx_load_host_state(vmx);
3899 vmx->msr_guest_kernel_gs_base = data;
3900 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901#endif
3902 case MSR_IA32_SYSENTER_CS:
3903 vmcs_write32(GUEST_SYSENTER_CS, data);
3904 break;
3905 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003906 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 break;
3908 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003909 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003911 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003912 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003913 (!msr_info->host_initiated &&
3914 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003915 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003916 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003917 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003919 vmcs_write64(GUEST_BNDCFGS, data);
3920 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003921 case MSR_IA32_SPEC_CTRL:
3922 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003923 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3924 return 1;
3925
3926 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003927 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003928 return 1;
3929
3930 vmx->spec_ctrl = data;
3931
3932 if (!data)
3933 break;
3934
3935 /*
3936 * For non-nested:
3937 * When it's written (to non-zero) for the first time, pass
3938 * it through.
3939 *
3940 * For nested:
3941 * The handling of the MSR bitmap for L2 guests is done in
3942 * nested_vmx_merge_msr_bitmap. We should not touch the
3943 * vmcs02.msr_bitmap here since it gets completely overwritten
3944 * in the merging. We update the vmcs01 here for L1 as well
3945 * since it will end up touching the MSR anyway now.
3946 */
3947 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3948 MSR_IA32_SPEC_CTRL,
3949 MSR_TYPE_RW);
3950 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003951 case MSR_IA32_PRED_CMD:
3952 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003953 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3954 return 1;
3955
3956 if (data & ~PRED_CMD_IBPB)
3957 return 1;
3958
3959 if (!data)
3960 break;
3961
3962 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3963
3964 /*
3965 * For non-nested:
3966 * When it's written (to non-zero) for the first time, pass
3967 * it through.
3968 *
3969 * For nested:
3970 * The handling of the MSR bitmap for L2 guests is done in
3971 * nested_vmx_merge_msr_bitmap. We should not touch the
3972 * vmcs02.msr_bitmap here since it gets completely overwritten
3973 * in the merging.
3974 */
3975 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3976 MSR_TYPE_W);
3977 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003978 case MSR_IA32_ARCH_CAPABILITIES:
3979 if (!msr_info->host_initiated)
3980 return 1;
3981 vmx->arch_capabilities = data;
3982 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003983 case MSR_IA32_CR_PAT:
3984 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003985 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3986 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003987 vmcs_write64(GUEST_IA32_PAT, data);
3988 vcpu->arch.pat = data;
3989 break;
3990 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003991 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003992 break;
Will Auldba904632012-11-29 12:42:50 -08003993 case MSR_IA32_TSC_ADJUST:
3994 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003995 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003996 case MSR_IA32_MCG_EXT_CTL:
3997 if ((!msr_info->host_initiated &&
3998 !(to_vmx(vcpu)->msr_ia32_feature_control &
3999 FEATURE_CONTROL_LMCE)) ||
4000 (data & ~MCG_EXT_CTL_LMCE_EN))
4001 return 1;
4002 vcpu->arch.mcg_ext_ctl = data;
4003 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004004 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004005 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004006 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004007 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4008 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004009 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004010 if (msr_info->host_initiated && data == 0)
4011 vmx_leave_nested(vcpu);
4012 break;
4013 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004014 if (!msr_info->host_initiated)
4015 return 1; /* they are read-only */
4016 if (!nested_vmx_allowed(vcpu))
4017 return 1;
4018 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004019 case MSR_IA32_XSS:
4020 if (!vmx_xsaves_supported())
4021 return 1;
4022 /*
4023 * The only supported bit as of Skylake is bit 8, but
4024 * it is not supported on KVM.
4025 */
4026 if (data != 0)
4027 return 1;
4028 vcpu->arch.ia32_xss = data;
4029 if (vcpu->arch.ia32_xss != host_xss)
4030 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4031 vcpu->arch.ia32_xss, host_xss);
4032 else
4033 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4034 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004035 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004036 if (!msr_info->host_initiated &&
4037 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004038 return 1;
4039 /* Check reserved bit, higher 32 bits should be zero */
4040 if ((data >> 32) != 0)
4041 return 1;
4042 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004044 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004045 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004046 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004047 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004048 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4049 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004050 ret = kvm_set_shared_msr(msr->index, msr->data,
4051 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004052 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004053 if (ret)
4054 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004055 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004056 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004058 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 }
4060
Eddie Dong2cc51562007-05-21 07:28:09 +03004061 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062}
4063
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004064static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004066 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4067 switch (reg) {
4068 case VCPU_REGS_RSP:
4069 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4070 break;
4071 case VCPU_REGS_RIP:
4072 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4073 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004074 case VCPU_EXREG_PDPTR:
4075 if (enable_ept)
4076 ept_save_pdptrs(vcpu);
4077 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004078 default:
4079 break;
4080 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081}
4082
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083static __init int cpu_has_kvm_support(void)
4084{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004085 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086}
4087
4088static __init int vmx_disabled_by_bios(void)
4089{
4090 u64 msr;
4091
4092 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004093 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004094 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004095 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4096 && tboot_enabled())
4097 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004098 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004099 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004100 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004101 && !tboot_enabled()) {
4102 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004103 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004104 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004105 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004106 /* launched w/o TXT and VMX disabled */
4107 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4108 && !tboot_enabled())
4109 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004110 }
4111
4112 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113}
4114
Dongxiao Xu7725b892010-05-11 18:29:38 +08004115static void kvm_cpu_vmxon(u64 addr)
4116{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004117 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004118 intel_pt_handle_vmx(1);
4119
Dongxiao Xu7725b892010-05-11 18:29:38 +08004120 asm volatile (ASM_VMX_VMXON_RAX
4121 : : "a"(&addr), "m"(addr)
4122 : "memory", "cc");
4123}
4124
Radim Krčmář13a34e02014-08-28 15:13:03 +02004125static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126{
4127 int cpu = raw_smp_processor_id();
4128 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004129 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004131 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004132 return -EBUSY;
4133
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004134 /*
4135 * This can happen if we hot-added a CPU but failed to allocate
4136 * VP assist page for it.
4137 */
4138 if (static_branch_unlikely(&enable_evmcs) &&
4139 !hv_get_vp_assist_page(cpu))
4140 return -EFAULT;
4141
Nadav Har'Eld462b812011-05-24 15:26:10 +03004142 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004143 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4144 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004145
4146 /*
4147 * Now we can enable the vmclear operation in kdump
4148 * since the loaded_vmcss_on_cpu list on this cpu
4149 * has been initialized.
4150 *
4151 * Though the cpu is not in VMX operation now, there
4152 * is no problem to enable the vmclear operation
4153 * for the loaded_vmcss_on_cpu list is empty!
4154 */
4155 crash_enable_local_vmclear(cpu);
4156
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004158
4159 test_bits = FEATURE_CONTROL_LOCKED;
4160 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4161 if (tboot_enabled())
4162 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4163
4164 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004166 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4167 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004168 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004169 if (enable_ept)
4170 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004171
4172 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173}
4174
Nadav Har'Eld462b812011-05-24 15:26:10 +03004175static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004176{
4177 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004178 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004179
Nadav Har'Eld462b812011-05-24 15:26:10 +03004180 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4181 loaded_vmcss_on_cpu_link)
4182 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004183}
4184
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004185
4186/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4187 * tricks.
4188 */
4189static void kvm_cpu_vmxoff(void)
4190{
4191 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004192
4193 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004194 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004195}
4196
Radim Krčmář13a34e02014-08-28 15:13:03 +02004197static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004199 vmclear_local_loaded_vmcss();
4200 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201}
4202
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004203static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004204 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205{
4206 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004207 u32 ctl = ctl_min | ctl_opt;
4208
4209 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4210
4211 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4212 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4213
4214 /* Ensure minimum (required) set of control bits are supported. */
4215 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004216 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004217
4218 *result = ctl;
4219 return 0;
4220}
4221
Avi Kivity110312c2010-12-21 12:54:20 +02004222static __init bool allow_1_setting(u32 msr, u32 ctl)
4223{
4224 u32 vmx_msr_low, vmx_msr_high;
4225
4226 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4227 return vmx_msr_high & ctl;
4228}
4229
Yang, Sheng002c7f72007-07-31 14:23:01 +03004230static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004231{
4232 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004233 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004234 u32 _pin_based_exec_control = 0;
4235 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004236 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004237 u32 _vmexit_control = 0;
4238 u32 _vmentry_control = 0;
4239
Paolo Bonzini13893092018-02-26 13:40:09 +01004240 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304241 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004242#ifdef CONFIG_X86_64
4243 CPU_BASED_CR8_LOAD_EXITING |
4244 CPU_BASED_CR8_STORE_EXITING |
4245#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004246 CPU_BASED_CR3_LOAD_EXITING |
4247 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004248 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004249 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004250 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004251 CPU_BASED_MWAIT_EXITING |
4252 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004253 CPU_BASED_INVLPG_EXITING |
4254 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004255
Sheng Yangf78e0e22007-10-29 09:40:42 +08004256 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004257 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004258 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004259 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4260 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004261 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004262#ifdef CONFIG_X86_64
4263 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4264 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4265 ~CPU_BASED_CR8_STORE_EXITING;
4266#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004267 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004268 min2 = 0;
4269 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004270 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004271 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004272 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004273 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004274 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004275 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004276 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004277 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004278 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004279 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004280 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004281 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004282 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004283 SECONDARY_EXEC_RDSEED_EXITING |
4284 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004285 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004286 SECONDARY_EXEC_TSC_SCALING |
4287 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004288 if (adjust_vmx_controls(min2, opt2,
4289 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004290 &_cpu_based_2nd_exec_control) < 0)
4291 return -EIO;
4292 }
4293#ifndef CONFIG_X86_64
4294 if (!(_cpu_based_2nd_exec_control &
4295 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4296 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4297#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004298
4299 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4300 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004301 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004302 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4303 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004304
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004305 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4306 &vmx_capability.ept, &vmx_capability.vpid);
4307
Sheng Yangd56f5462008-04-25 10:13:16 +08004308 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004309 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4310 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004311 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4312 CPU_BASED_CR3_STORE_EXITING |
4313 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004314 } else if (vmx_capability.ept) {
4315 vmx_capability.ept = 0;
4316 pr_warn_once("EPT CAP should not exist if not support "
4317 "1-setting enable EPT VM-execution control\n");
4318 }
4319 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4320 vmx_capability.vpid) {
4321 vmx_capability.vpid = 0;
4322 pr_warn_once("VPID CAP should not exist if not support "
4323 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004324 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004325
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004326 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004327#ifdef CONFIG_X86_64
4328 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4329#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004330 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004331 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004332 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4333 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004335
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004336 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4337 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4338 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004339 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4340 &_pin_based_exec_control) < 0)
4341 return -EIO;
4342
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004343 if (cpu_has_broken_vmx_preemption_timer())
4344 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004345 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004346 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004347 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4348
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004349 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004350 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004351 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4352 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004353 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004355 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004356
4357 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4358 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004359 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004360
4361#ifdef CONFIG_X86_64
4362 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4363 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004364 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004365#endif
4366
4367 /* Require Write-Back (WB) memory type for VMCS accesses. */
4368 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004369 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004370
Yang, Sheng002c7f72007-07-31 14:23:01 +03004371 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004372 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004373 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004374
Liran Alon2307af12018-06-29 22:59:04 +03004375 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004376
Yang, Sheng002c7f72007-07-31 14:23:01 +03004377 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4378 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004379 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004380 vmcs_conf->vmexit_ctrl = _vmexit_control;
4381 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004382
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004383 if (static_branch_unlikely(&enable_evmcs))
4384 evmcs_sanitize_exec_ctrls(vmcs_conf);
4385
Avi Kivity110312c2010-12-21 12:54:20 +02004386 cpu_has_load_ia32_efer =
4387 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4388 VM_ENTRY_LOAD_IA32_EFER)
4389 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4390 VM_EXIT_LOAD_IA32_EFER);
4391
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004392 cpu_has_load_perf_global_ctrl =
4393 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4394 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4395 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4396 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4397
4398 /*
4399 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004400 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004401 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4402 *
4403 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4404 *
4405 * AAK155 (model 26)
4406 * AAP115 (model 30)
4407 * AAT100 (model 37)
4408 * BC86,AAY89,BD102 (model 44)
4409 * BA97 (model 46)
4410 *
4411 */
4412 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4413 switch (boot_cpu_data.x86_model) {
4414 case 26:
4415 case 30:
4416 case 37:
4417 case 44:
4418 case 46:
4419 cpu_has_load_perf_global_ctrl = false;
4420 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4421 "does not work properly. Using workaround\n");
4422 break;
4423 default:
4424 break;
4425 }
4426 }
4427
Borislav Petkov782511b2016-04-04 22:25:03 +02004428 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004429 rdmsrl(MSR_IA32_XSS, host_xss);
4430
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004431 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004432}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433
Liran Alon491a6032018-06-23 02:35:12 +03004434static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435{
4436 int node = cpu_to_node(cpu);
4437 struct page *pages;
4438 struct vmcs *vmcs;
4439
Vlastimil Babka96db8002015-09-08 15:03:50 -07004440 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441 if (!pages)
4442 return NULL;
4443 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004444 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004445
4446 /* KVM supports Enlightened VMCS v1 only */
4447 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004448 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004449 else
Liran Alon392b2f22018-06-23 02:35:01 +03004450 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004451
Liran Alon491a6032018-06-23 02:35:12 +03004452 if (shadow)
4453 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454 return vmcs;
4455}
4456
Avi Kivity6aa8b732006-12-10 02:21:36 -08004457static void free_vmcs(struct vmcs *vmcs)
4458{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004459 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460}
4461
Nadav Har'Eld462b812011-05-24 15:26:10 +03004462/*
4463 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4464 */
4465static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4466{
4467 if (!loaded_vmcs->vmcs)
4468 return;
4469 loaded_vmcs_clear(loaded_vmcs);
4470 free_vmcs(loaded_vmcs->vmcs);
4471 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004472 if (loaded_vmcs->msr_bitmap)
4473 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004474 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004475}
4476
Liran Alon491a6032018-06-23 02:35:12 +03004477static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004478{
Liran Alon491a6032018-06-23 02:35:12 +03004479 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004480}
4481
4482static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4483{
Liran Alon491a6032018-06-23 02:35:12 +03004484 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004485 if (!loaded_vmcs->vmcs)
4486 return -ENOMEM;
4487
4488 loaded_vmcs->shadow_vmcs = NULL;
4489 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004490
4491 if (cpu_has_vmx_msr_bitmap()) {
4492 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4493 if (!loaded_vmcs->msr_bitmap)
4494 goto out_vmcs;
4495 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004496
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004497 if (IS_ENABLED(CONFIG_HYPERV) &&
4498 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004499 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4500 struct hv_enlightened_vmcs *evmcs =
4501 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4502
4503 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4504 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004505 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004506 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004507
4508out_vmcs:
4509 free_loaded_vmcs(loaded_vmcs);
4510 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004511}
4512
Sam Ravnborg39959582007-06-01 00:47:13 -07004513static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004514{
4515 int cpu;
4516
Zachary Amsden3230bb42009-09-29 11:38:37 -10004517 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004518 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004519 per_cpu(vmxarea, cpu) = NULL;
4520 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004521}
4522
Jim Mattsond37f4262017-12-22 12:12:16 -08004523enum vmcs_field_width {
4524 VMCS_FIELD_WIDTH_U16 = 0,
4525 VMCS_FIELD_WIDTH_U64 = 1,
4526 VMCS_FIELD_WIDTH_U32 = 2,
4527 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004528};
4529
Jim Mattsond37f4262017-12-22 12:12:16 -08004530static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004531{
4532 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004533 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004534 return (field >> 13) & 0x3 ;
4535}
4536
4537static inline int vmcs_field_readonly(unsigned long field)
4538{
4539 return (((field >> 10) & 0x3) == 1);
4540}
4541
Bandan Dasfe2b2012014-04-21 15:20:14 -04004542static void init_vmcs_shadow_fields(void)
4543{
4544 int i, j;
4545
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004546 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4547 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004548 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004549 (i + 1 == max_shadow_read_only_fields ||
4550 shadow_read_only_fields[i + 1] != field + 1))
4551 pr_err("Missing field from shadow_read_only_field %x\n",
4552 field + 1);
4553
4554 clear_bit(field, vmx_vmread_bitmap);
4555#ifdef CONFIG_X86_64
4556 if (field & 1)
4557 continue;
4558#endif
4559 if (j < i)
4560 shadow_read_only_fields[j] = field;
4561 j++;
4562 }
4563 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004564
4565 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004566 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004567 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004568 (i + 1 == max_shadow_read_write_fields ||
4569 shadow_read_write_fields[i + 1] != field + 1))
4570 pr_err("Missing field from shadow_read_write_field %x\n",
4571 field + 1);
4572
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004573 /*
4574 * PML and the preemption timer can be emulated, but the
4575 * processor cannot vmwrite to fields that don't exist
4576 * on bare metal.
4577 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004578 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004579 case GUEST_PML_INDEX:
4580 if (!cpu_has_vmx_pml())
4581 continue;
4582 break;
4583 case VMX_PREEMPTION_TIMER_VALUE:
4584 if (!cpu_has_vmx_preemption_timer())
4585 continue;
4586 break;
4587 case GUEST_INTR_STATUS:
4588 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004589 continue;
4590 break;
4591 default:
4592 break;
4593 }
4594
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004595 clear_bit(field, vmx_vmwrite_bitmap);
4596 clear_bit(field, vmx_vmread_bitmap);
4597#ifdef CONFIG_X86_64
4598 if (field & 1)
4599 continue;
4600#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004601 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004602 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004603 j++;
4604 }
4605 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004606}
4607
Avi Kivity6aa8b732006-12-10 02:21:36 -08004608static __init int alloc_kvm_area(void)
4609{
4610 int cpu;
4611
Zachary Amsden3230bb42009-09-29 11:38:37 -10004612 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613 struct vmcs *vmcs;
4614
Liran Alon491a6032018-06-23 02:35:12 +03004615 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004616 if (!vmcs) {
4617 free_kvm_area();
4618 return -ENOMEM;
4619 }
4620
Liran Alon2307af12018-06-29 22:59:04 +03004621 /*
4622 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4623 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4624 * revision_id reported by MSR_IA32_VMX_BASIC.
4625 *
4626 * However, even though not explictly documented by
4627 * TLFS, VMXArea passed as VMXON argument should
4628 * still be marked with revision_id reported by
4629 * physical CPU.
4630 */
4631 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004632 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004633
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634 per_cpu(vmxarea, cpu) = vmcs;
4635 }
4636 return 0;
4637}
4638
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004639static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004640 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004642 if (!emulate_invalid_guest_state) {
4643 /*
4644 * CS and SS RPL should be equal during guest entry according
4645 * to VMX spec, but in reality it is not always so. Since vcpu
4646 * is in the middle of the transition from real mode to
4647 * protected mode it is safe to assume that RPL 0 is a good
4648 * default value.
4649 */
4650 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004651 save->selector &= ~SEGMENT_RPL_MASK;
4652 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004653 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004655 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656}
4657
4658static void enter_pmode(struct kvm_vcpu *vcpu)
4659{
4660 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004661 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662
Gleb Natapovd99e4152012-12-20 16:57:45 +02004663 /*
4664 * Update real mode segment cache. It may be not up-to-date if sement
4665 * register was written while vcpu was in a guest mode.
4666 */
4667 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4668 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4669 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4670 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4671 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4672 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4673
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004674 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675
Avi Kivity2fb92db2011-04-27 19:42:18 +03004676 vmx_segment_cache_clear(vmx);
4677
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004678 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004679
4680 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004681 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4682 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683 vmcs_writel(GUEST_RFLAGS, flags);
4684
Rusty Russell66aee912007-07-17 23:34:16 +10004685 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4686 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004687
4688 update_exception_bitmap(vcpu);
4689
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004690 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4691 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4692 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4693 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4694 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4695 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696}
4697
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004698static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699{
Mathias Krause772e0312012-08-30 01:30:19 +02004700 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004701 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702
Gleb Natapovd99e4152012-12-20 16:57:45 +02004703 var.dpl = 0x3;
4704 if (seg == VCPU_SREG_CS)
4705 var.type = 0x3;
4706
4707 if (!emulate_invalid_guest_state) {
4708 var.selector = var.base >> 4;
4709 var.base = var.base & 0xffff0;
4710 var.limit = 0xffff;
4711 var.g = 0;
4712 var.db = 0;
4713 var.present = 1;
4714 var.s = 1;
4715 var.l = 0;
4716 var.unusable = 0;
4717 var.type = 0x3;
4718 var.avl = 0;
4719 if (save->base & 0xf)
4720 printk_once(KERN_WARNING "kvm: segment base is not "
4721 "paragraph aligned when entering "
4722 "protected mode (seg=%d)", seg);
4723 }
4724
4725 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004726 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004727 vmcs_write32(sf->limit, var.limit);
4728 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729}
4730
4731static void enter_rmode(struct kvm_vcpu *vcpu)
4732{
4733 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004734 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004735 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004736
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004737 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4738 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4739 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4740 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4741 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004742 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4743 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004744
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004745 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746
Gleb Natapov776e58e2011-03-13 12:34:27 +02004747 /*
4748 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004749 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004750 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004751 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004752 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4753 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004754
Avi Kivity2fb92db2011-04-27 19:42:18 +03004755 vmx_segment_cache_clear(vmx);
4756
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004757 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4760
4761 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004762 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004763
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004764 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765
4766 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004767 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004768 update_exception_bitmap(vcpu);
4769
Gleb Natapovd99e4152012-12-20 16:57:45 +02004770 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4771 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4772 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4773 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4774 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4775 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004776
Eddie Dong8668a3c2007-10-10 14:26:45 +08004777 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004778}
4779
Amit Shah401d10d2009-02-20 22:53:37 +05304780static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4781{
4782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004783 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4784
4785 if (!msr)
4786 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304787
Avi Kivity44ea2b12009-09-06 15:55:37 +03004788 /*
4789 * Force kernel_gs_base reloading before EFER changes, as control
4790 * of this msr depends on is_long_mode().
4791 */
4792 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004793 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304794 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004795 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304796 msr->data = efer;
4797 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004798 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304799
4800 msr->data = efer & ~EFER_LME;
4801 }
4802 setup_msrs(vmx);
4803}
4804
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004805#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806
4807static void enter_lmode(struct kvm_vcpu *vcpu)
4808{
4809 u32 guest_tr_ar;
4810
Avi Kivity2fb92db2011-04-27 19:42:18 +03004811 vmx_segment_cache_clear(to_vmx(vcpu));
4812
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004814 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004815 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4816 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004818 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4819 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820 }
Avi Kivityda38f432010-07-06 11:30:49 +03004821 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822}
4823
4824static void exit_lmode(struct kvm_vcpu *vcpu)
4825{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004826 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004827 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828}
4829
4830#endif
4831
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004832static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4833 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004834{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004835 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004836 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4837 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004838 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004839 } else {
4840 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004841 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004842}
4843
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004844static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004845{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004846 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004847}
4848
Junaid Shahidfaff8752018-06-29 13:10:05 -07004849static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4850{
4851 int vpid = to_vmx(vcpu)->vpid;
4852
4853 if (!vpid_sync_vcpu_addr(vpid, addr))
4854 vpid_sync_context(vpid);
4855
4856 /*
4857 * If VPIDs are not supported or enabled, then the above is a no-op.
4858 * But we don't really need a TLB flush in that case anyway, because
4859 * each VM entry/exit includes an implicit flush when VPID is 0.
4860 */
4861}
4862
Avi Kivitye8467fd2009-12-29 18:43:06 +02004863static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4864{
4865 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4866
4867 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4868 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4869}
4870
Avi Kivityaff48ba2010-12-05 18:56:11 +02004871static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4872{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004873 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004874 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4875 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4876}
4877
Anthony Liguori25c4c272007-04-27 09:29:21 +03004878static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004879{
Avi Kivityfc78f512009-12-07 12:16:48 +02004880 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4881
4882 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4883 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004884}
4885
Sheng Yang14394422008-04-28 12:24:45 +08004886static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4887{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004888 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4889
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004890 if (!test_bit(VCPU_EXREG_PDPTR,
4891 (unsigned long *)&vcpu->arch.regs_dirty))
4892 return;
4893
Sheng Yang14394422008-04-28 12:24:45 +08004894 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004895 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4896 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4897 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4898 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004899 }
4900}
4901
Avi Kivity8f5d5492009-05-31 18:41:29 +03004902static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4903{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004904 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4905
Avi Kivity8f5d5492009-05-31 18:41:29 +03004906 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004907 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4908 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4909 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4910 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004911 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004912
4913 __set_bit(VCPU_EXREG_PDPTR,
4914 (unsigned long *)&vcpu->arch.regs_avail);
4915 __set_bit(VCPU_EXREG_PDPTR,
4916 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004917}
4918
David Matlack38991522016-11-29 18:14:08 -08004919static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4920{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004921 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4922 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004923 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4924
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004925 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004926 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4927 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4928 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4929
4930 return fixed_bits_valid(val, fixed0, fixed1);
4931}
4932
4933static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4934{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004935 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4936 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004937
4938 return fixed_bits_valid(val, fixed0, fixed1);
4939}
4940
4941static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4942{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004943 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4944 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004945
4946 return fixed_bits_valid(val, fixed0, fixed1);
4947}
4948
4949/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4950#define nested_guest_cr4_valid nested_cr4_valid
4951#define nested_host_cr4_valid nested_cr4_valid
4952
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004953static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004954
4955static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4956 unsigned long cr0,
4957 struct kvm_vcpu *vcpu)
4958{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004959 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4960 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004961 if (!(cr0 & X86_CR0_PG)) {
4962 /* From paging/starting to nonpaging */
4963 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004964 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004965 (CPU_BASED_CR3_LOAD_EXITING |
4966 CPU_BASED_CR3_STORE_EXITING));
4967 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004968 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004969 } else if (!is_paging(vcpu)) {
4970 /* From nonpaging to paging */
4971 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004972 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004973 ~(CPU_BASED_CR3_LOAD_EXITING |
4974 CPU_BASED_CR3_STORE_EXITING));
4975 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004976 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004977 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004978
4979 if (!(cr0 & X86_CR0_WP))
4980 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004981}
4982
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4984{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004985 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004986 unsigned long hw_cr0;
4987
Gleb Natapov50378782013-02-04 16:00:28 +02004988 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004989 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004990 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004991 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004992 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004993
Gleb Natapov218e7632013-01-21 15:36:45 +02004994 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4995 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996
Gleb Natapov218e7632013-01-21 15:36:45 +02004997 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4998 enter_rmode(vcpu);
4999 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005001#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005002 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005003 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005004 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005005 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006 exit_lmode(vcpu);
5007 }
5008#endif
5009
Sean Christophersonb4d18512018-03-05 12:04:40 -08005010 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005011 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5012
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005014 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005015 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005016
5017 /* depends on vcpu->arch.cr0 to be set to a new value */
5018 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019}
5020
Yu Zhang855feb62017-08-24 20:27:55 +08005021static int get_ept_level(struct kvm_vcpu *vcpu)
5022{
5023 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5024 return 5;
5025 return 4;
5026}
5027
Peter Feiner995f00a2017-06-30 17:26:32 -07005028static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005029{
Yu Zhang855feb62017-08-24 20:27:55 +08005030 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005031
Yu Zhang855feb62017-08-24 20:27:55 +08005032 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005033
Peter Feiner995f00a2017-06-30 17:26:32 -07005034 if (enable_ept_ad_bits &&
5035 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005036 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005037 eptp |= (root_hpa & PAGE_MASK);
5038
5039 return eptp;
5040}
5041
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5043{
Sheng Yang14394422008-04-28 12:24:45 +08005044 unsigned long guest_cr3;
5045 u64 eptp;
5046
5047 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005048 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005049 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005050 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005051 if (enable_unrestricted_guest || is_paging(vcpu) ||
5052 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005053 guest_cr3 = kvm_read_cr3(vcpu);
5054 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005055 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005056 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005057 }
5058
Sheng Yang14394422008-04-28 12:24:45 +08005059 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005060}
5061
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005062static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005064 /*
5065 * Pass through host's Machine Check Enable value to hw_cr4, which
5066 * is in force while we are in guest mode. Do not let guests control
5067 * this bit, even if host CR4.MCE == 0.
5068 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005069 unsigned long hw_cr4;
5070
5071 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5072 if (enable_unrestricted_guest)
5073 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5074 else if (to_vmx(vcpu)->rmode.vm86_active)
5075 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5076 else
5077 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005078
Sean Christopherson64f7a112018-04-30 10:01:06 -07005079 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5080 if (cr4 & X86_CR4_UMIP) {
5081 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005082 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005083 hw_cr4 &= ~X86_CR4_UMIP;
5084 } else if (!is_guest_mode(vcpu) ||
5085 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5086 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5087 SECONDARY_EXEC_DESC);
5088 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005089
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005090 if (cr4 & X86_CR4_VMXE) {
5091 /*
5092 * To use VMXON (and later other VMX instructions), a guest
5093 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5094 * So basically the check on whether to allow nested VMX
5095 * is here.
5096 */
5097 if (!nested_vmx_allowed(vcpu))
5098 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005099 }
David Matlack38991522016-11-29 18:14:08 -08005100
5101 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005102 return 1;
5103
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005104 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005105
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005106 if (!enable_unrestricted_guest) {
5107 if (enable_ept) {
5108 if (!is_paging(vcpu)) {
5109 hw_cr4 &= ~X86_CR4_PAE;
5110 hw_cr4 |= X86_CR4_PSE;
5111 } else if (!(cr4 & X86_CR4_PAE)) {
5112 hw_cr4 &= ~X86_CR4_PAE;
5113 }
5114 }
5115
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005116 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005117 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5118 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5119 * to be manually disabled when guest switches to non-paging
5120 * mode.
5121 *
5122 * If !enable_unrestricted_guest, the CPU is always running
5123 * with CR0.PG=1 and CR4 needs to be modified.
5124 * If enable_unrestricted_guest, the CPU automatically
5125 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005126 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005127 if (!is_paging(vcpu))
5128 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5129 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005130
Sheng Yang14394422008-04-28 12:24:45 +08005131 vmcs_writel(CR4_READ_SHADOW, cr4);
5132 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005133 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005134}
5135
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136static void vmx_get_segment(struct kvm_vcpu *vcpu,
5137 struct kvm_segment *var, int seg)
5138{
Avi Kivitya9179492011-01-03 14:28:52 +02005139 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 u32 ar;
5141
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005142 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005143 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005144 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005145 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005146 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005147 var->base = vmx_read_guest_seg_base(vmx, seg);
5148 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5149 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005150 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005151 var->base = vmx_read_guest_seg_base(vmx, seg);
5152 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5153 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5154 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005155 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005156 var->type = ar & 15;
5157 var->s = (ar >> 4) & 1;
5158 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005159 /*
5160 * Some userspaces do not preserve unusable property. Since usable
5161 * segment has to be present according to VMX spec we can use present
5162 * property to amend userspace bug by making unusable segment always
5163 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5164 * segment as unusable.
5165 */
5166 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005167 var->avl = (ar >> 12) & 1;
5168 var->l = (ar >> 13) & 1;
5169 var->db = (ar >> 14) & 1;
5170 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171}
5172
Avi Kivitya9179492011-01-03 14:28:52 +02005173static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5174{
Avi Kivitya9179492011-01-03 14:28:52 +02005175 struct kvm_segment s;
5176
5177 if (to_vmx(vcpu)->rmode.vm86_active) {
5178 vmx_get_segment(vcpu, &s, seg);
5179 return s.base;
5180 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005181 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005182}
5183
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005184static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005185{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005186 struct vcpu_vmx *vmx = to_vmx(vcpu);
5187
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005188 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005189 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005190 else {
5191 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005192 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005193 }
Avi Kivity69c73022011-03-07 15:26:44 +02005194}
5195
Avi Kivity653e3102007-05-07 10:55:37 +03005196static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005197{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198 u32 ar;
5199
Avi Kivityf0495f92012-06-07 17:06:10 +03005200 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005201 ar = 1 << 16;
5202 else {
5203 ar = var->type & 15;
5204 ar |= (var->s & 1) << 4;
5205 ar |= (var->dpl & 3) << 5;
5206 ar |= (var->present & 1) << 7;
5207 ar |= (var->avl & 1) << 12;
5208 ar |= (var->l & 1) << 13;
5209 ar |= (var->db & 1) << 14;
5210 ar |= (var->g & 1) << 15;
5211 }
Avi Kivity653e3102007-05-07 10:55:37 +03005212
5213 return ar;
5214}
5215
5216static void vmx_set_segment(struct kvm_vcpu *vcpu,
5217 struct kvm_segment *var, int seg)
5218{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005220 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005221
Avi Kivity2fb92db2011-04-27 19:42:18 +03005222 vmx_segment_cache_clear(vmx);
5223
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005224 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5225 vmx->rmode.segs[seg] = *var;
5226 if (seg == VCPU_SREG_TR)
5227 vmcs_write16(sf->selector, var->selector);
5228 else if (var->s)
5229 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005230 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005231 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005232
Avi Kivity653e3102007-05-07 10:55:37 +03005233 vmcs_writel(sf->base, var->base);
5234 vmcs_write32(sf->limit, var->limit);
5235 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005236
5237 /*
5238 * Fix the "Accessed" bit in AR field of segment registers for older
5239 * qemu binaries.
5240 * IA32 arch specifies that at the time of processor reset the
5241 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005242 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005243 * state vmexit when "unrestricted guest" mode is turned on.
5244 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5245 * tree. Newer qemu binaries with that qemu fix would not need this
5246 * kvm hack.
5247 */
5248 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005249 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005250
Gleb Natapovf924d662012-12-12 19:10:55 +02005251 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005252
5253out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005254 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005255}
5256
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5258{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005259 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260
5261 *db = (ar >> 14) & 1;
5262 *l = (ar >> 13) & 1;
5263}
5264
Gleb Natapov89a27f42010-02-16 10:51:48 +02005265static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005267 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5268 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005269}
5270
Gleb Natapov89a27f42010-02-16 10:51:48 +02005271static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005272{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005273 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5274 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275}
5276
Gleb Natapov89a27f42010-02-16 10:51:48 +02005277static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005279 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5280 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005281}
5282
Gleb Natapov89a27f42010-02-16 10:51:48 +02005283static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005284{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005285 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5286 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005287}
5288
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005289static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5290{
5291 struct kvm_segment var;
5292 u32 ar;
5293
5294 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005295 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005296 if (seg == VCPU_SREG_CS)
5297 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005298 ar = vmx_segment_access_rights(&var);
5299
5300 if (var.base != (var.selector << 4))
5301 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005302 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005303 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005304 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005305 return false;
5306
5307 return true;
5308}
5309
5310static bool code_segment_valid(struct kvm_vcpu *vcpu)
5311{
5312 struct kvm_segment cs;
5313 unsigned int cs_rpl;
5314
5315 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005316 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005317
Avi Kivity1872a3f2009-01-04 23:26:52 +02005318 if (cs.unusable)
5319 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005320 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005321 return false;
5322 if (!cs.s)
5323 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005324 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005325 if (cs.dpl > cs_rpl)
5326 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005327 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005328 if (cs.dpl != cs_rpl)
5329 return false;
5330 }
5331 if (!cs.present)
5332 return false;
5333
5334 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5335 return true;
5336}
5337
5338static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5339{
5340 struct kvm_segment ss;
5341 unsigned int ss_rpl;
5342
5343 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005344 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005345
Avi Kivity1872a3f2009-01-04 23:26:52 +02005346 if (ss.unusable)
5347 return true;
5348 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005349 return false;
5350 if (!ss.s)
5351 return false;
5352 if (ss.dpl != ss_rpl) /* DPL != RPL */
5353 return false;
5354 if (!ss.present)
5355 return false;
5356
5357 return true;
5358}
5359
5360static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5361{
5362 struct kvm_segment var;
5363 unsigned int rpl;
5364
5365 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005366 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005367
Avi Kivity1872a3f2009-01-04 23:26:52 +02005368 if (var.unusable)
5369 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005370 if (!var.s)
5371 return false;
5372 if (!var.present)
5373 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005374 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005375 if (var.dpl < rpl) /* DPL < RPL */
5376 return false;
5377 }
5378
5379 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5380 * rights flags
5381 */
5382 return true;
5383}
5384
5385static bool tr_valid(struct kvm_vcpu *vcpu)
5386{
5387 struct kvm_segment tr;
5388
5389 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5390
Avi Kivity1872a3f2009-01-04 23:26:52 +02005391 if (tr.unusable)
5392 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005393 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005394 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005395 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005396 return false;
5397 if (!tr.present)
5398 return false;
5399
5400 return true;
5401}
5402
5403static bool ldtr_valid(struct kvm_vcpu *vcpu)
5404{
5405 struct kvm_segment ldtr;
5406
5407 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5408
Avi Kivity1872a3f2009-01-04 23:26:52 +02005409 if (ldtr.unusable)
5410 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005411 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005412 return false;
5413 if (ldtr.type != 2)
5414 return false;
5415 if (!ldtr.present)
5416 return false;
5417
5418 return true;
5419}
5420
5421static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5422{
5423 struct kvm_segment cs, ss;
5424
5425 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5426 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5427
Nadav Amitb32a9912015-03-29 16:33:04 +03005428 return ((cs.selector & SEGMENT_RPL_MASK) ==
5429 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005430}
5431
5432/*
5433 * Check if guest state is valid. Returns true if valid, false if
5434 * not.
5435 * We assume that registers are always usable
5436 */
5437static bool guest_state_valid(struct kvm_vcpu *vcpu)
5438{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005439 if (enable_unrestricted_guest)
5440 return true;
5441
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005442 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005443 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005444 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5445 return false;
5446 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5447 return false;
5448 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5449 return false;
5450 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5451 return false;
5452 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5453 return false;
5454 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5455 return false;
5456 } else {
5457 /* protected mode guest state checks */
5458 if (!cs_ss_rpl_check(vcpu))
5459 return false;
5460 if (!code_segment_valid(vcpu))
5461 return false;
5462 if (!stack_segment_valid(vcpu))
5463 return false;
5464 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5465 return false;
5466 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5467 return false;
5468 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5469 return false;
5470 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5471 return false;
5472 if (!tr_valid(vcpu))
5473 return false;
5474 if (!ldtr_valid(vcpu))
5475 return false;
5476 }
5477 /* TODO:
5478 * - Add checks on RIP
5479 * - Add checks on RFLAGS
5480 */
5481
5482 return true;
5483}
5484
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005485static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5486{
5487 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5488}
5489
Mike Dayd77c26f2007-10-08 09:02:08 -04005490static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005491{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005492 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005493 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005494 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005495
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005496 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005497 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005498 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5499 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005500 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005501 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005502 r = kvm_write_guest_page(kvm, fn++, &data,
5503 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005504 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005505 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005506 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5507 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005508 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005509 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5510 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005511 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005512 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005513 r = kvm_write_guest_page(kvm, fn, &data,
5514 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5515 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005516out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005517 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005518 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005519}
5520
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005521static int init_rmode_identity_map(struct kvm *kvm)
5522{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005523 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005524 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005525 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005526 u32 tmp;
5527
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005528 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005529 mutex_lock(&kvm->slots_lock);
5530
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005531 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005532 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005533
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005534 if (!kvm_vmx->ept_identity_map_addr)
5535 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5536 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005537
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005538 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005539 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005540 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005541 goto out2;
5542
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005543 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005544 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5545 if (r < 0)
5546 goto out;
5547 /* Set up identity-mapping pagetable for EPT in real mode */
5548 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5549 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5550 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5551 r = kvm_write_guest_page(kvm, identity_map_pfn,
5552 &tmp, i * sizeof(tmp), sizeof(tmp));
5553 if (r < 0)
5554 goto out;
5555 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005556 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005557
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005558out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005559 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005560
5561out2:
5562 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005563 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005564}
5565
Avi Kivity6aa8b732006-12-10 02:21:36 -08005566static void seg_setup(int seg)
5567{
Mathias Krause772e0312012-08-30 01:30:19 +02005568 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005569 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570
5571 vmcs_write16(sf->selector, 0);
5572 vmcs_writel(sf->base, 0);
5573 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005574 ar = 0x93;
5575 if (seg == VCPU_SREG_CS)
5576 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005577
5578 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579}
5580
Sheng Yangf78e0e22007-10-29 09:40:42 +08005581static int alloc_apic_access_page(struct kvm *kvm)
5582{
Xiao Guangrong44841412012-09-07 14:14:20 +08005583 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005584 int r = 0;
5585
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005586 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005587 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005588 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005589 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5590 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005591 if (r)
5592 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005593
Tang Chen73a6d942014-09-11 13:38:00 +08005594 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005595 if (is_error_page(page)) {
5596 r = -EFAULT;
5597 goto out;
5598 }
5599
Tang Chenc24ae0d2014-09-24 15:57:58 +08005600 /*
5601 * Do not pin the page in memory, so that memory hot-unplug
5602 * is able to migrate it.
5603 */
5604 put_page(page);
5605 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005606out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005607 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005608 return r;
5609}
5610
Wanpeng Li991e7a02015-09-16 17:30:05 +08005611static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005612{
5613 int vpid;
5614
Avi Kivity919818a2009-03-23 18:01:29 +02005615 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005616 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005617 spin_lock(&vmx_vpid_lock);
5618 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005619 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005620 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005621 else
5622 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005623 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005624 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005625}
5626
Wanpeng Li991e7a02015-09-16 17:30:05 +08005627static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005628{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005629 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005630 return;
5631 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005632 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005633 spin_unlock(&vmx_vpid_lock);
5634}
5635
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005636static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5637 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005638{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005639 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005640
5641 if (!cpu_has_vmx_msr_bitmap())
5642 return;
5643
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005644 if (static_branch_unlikely(&enable_evmcs))
5645 evmcs_touch_msr_bitmap();
5646
Sheng Yang25c5f222008-03-28 13:18:56 +08005647 /*
5648 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5649 * have the write-low and read-high bitmap offsets the wrong way round.
5650 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5651 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005652 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005653 if (type & MSR_TYPE_R)
5654 /* read-low */
5655 __clear_bit(msr, msr_bitmap + 0x000 / f);
5656
5657 if (type & MSR_TYPE_W)
5658 /* write-low */
5659 __clear_bit(msr, msr_bitmap + 0x800 / f);
5660
Sheng Yang25c5f222008-03-28 13:18:56 +08005661 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5662 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005663 if (type & MSR_TYPE_R)
5664 /* read-high */
5665 __clear_bit(msr, msr_bitmap + 0x400 / f);
5666
5667 if (type & MSR_TYPE_W)
5668 /* write-high */
5669 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5670
5671 }
5672}
5673
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005674static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5675 u32 msr, int type)
5676{
5677 int f = sizeof(unsigned long);
5678
5679 if (!cpu_has_vmx_msr_bitmap())
5680 return;
5681
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005682 if (static_branch_unlikely(&enable_evmcs))
5683 evmcs_touch_msr_bitmap();
5684
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005685 /*
5686 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5687 * have the write-low and read-high bitmap offsets the wrong way round.
5688 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5689 */
5690 if (msr <= 0x1fff) {
5691 if (type & MSR_TYPE_R)
5692 /* read-low */
5693 __set_bit(msr, msr_bitmap + 0x000 / f);
5694
5695 if (type & MSR_TYPE_W)
5696 /* write-low */
5697 __set_bit(msr, msr_bitmap + 0x800 / f);
5698
5699 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5700 msr &= 0x1fff;
5701 if (type & MSR_TYPE_R)
5702 /* read-high */
5703 __set_bit(msr, msr_bitmap + 0x400 / f);
5704
5705 if (type & MSR_TYPE_W)
5706 /* write-high */
5707 __set_bit(msr, msr_bitmap + 0xc00 / f);
5708
5709 }
5710}
5711
5712static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5713 u32 msr, int type, bool value)
5714{
5715 if (value)
5716 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5717 else
5718 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5719}
5720
Wincy Vanf2b93282015-02-03 23:56:03 +08005721/*
5722 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5723 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5724 */
5725static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5726 unsigned long *msr_bitmap_nested,
5727 u32 msr, int type)
5728{
5729 int f = sizeof(unsigned long);
5730
Wincy Vanf2b93282015-02-03 23:56:03 +08005731 /*
5732 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5733 * have the write-low and read-high bitmap offsets the wrong way round.
5734 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5735 */
5736 if (msr <= 0x1fff) {
5737 if (type & MSR_TYPE_R &&
5738 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5739 /* read-low */
5740 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5741
5742 if (type & MSR_TYPE_W &&
5743 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5744 /* write-low */
5745 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5746
5747 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5748 msr &= 0x1fff;
5749 if (type & MSR_TYPE_R &&
5750 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5751 /* read-high */
5752 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5753
5754 if (type & MSR_TYPE_W &&
5755 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5756 /* write-high */
5757 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5758
5759 }
5760}
5761
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005762static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005763{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005764 u8 mode = 0;
5765
5766 if (cpu_has_secondary_exec_ctrls() &&
5767 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5768 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5769 mode |= MSR_BITMAP_MODE_X2APIC;
5770 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5771 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5772 }
5773
5774 if (is_long_mode(vcpu))
5775 mode |= MSR_BITMAP_MODE_LM;
5776
5777 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005778}
5779
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005780#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5781
5782static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5783 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005784{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005785 int msr;
5786
5787 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5788 unsigned word = msr / BITS_PER_LONG;
5789 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5790 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005791 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005792
5793 if (mode & MSR_BITMAP_MODE_X2APIC) {
5794 /*
5795 * TPR reads and writes can be virtualized even if virtual interrupt
5796 * delivery is not in use.
5797 */
5798 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5799 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5800 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5801 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5802 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5803 }
5804 }
5805}
5806
5807static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5808{
5809 struct vcpu_vmx *vmx = to_vmx(vcpu);
5810 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5811 u8 mode = vmx_msr_bitmap_mode(vcpu);
5812 u8 changed = mode ^ vmx->msr_bitmap_mode;
5813
5814 if (!changed)
5815 return;
5816
5817 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5818 !(mode & MSR_BITMAP_MODE_LM));
5819
5820 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5821 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5822
5823 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005824}
5825
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005826static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005827{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005828 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005829}
5830
David Matlackc9f04402017-08-01 14:00:40 -07005831static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5832{
5833 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5834 gfn_t gfn;
5835
5836 /*
5837 * Don't need to mark the APIC access page dirty; it is never
5838 * written to by the CPU during APIC virtualization.
5839 */
5840
5841 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5842 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5843 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5844 }
5845
5846 if (nested_cpu_has_posted_intr(vmcs12)) {
5847 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5848 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5849 }
5850}
5851
5852
David Hildenbrand6342c502017-01-25 11:58:58 +01005853static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005854{
5855 struct vcpu_vmx *vmx = to_vmx(vcpu);
5856 int max_irr;
5857 void *vapic_page;
5858 u16 status;
5859
David Matlackc9f04402017-08-01 14:00:40 -07005860 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5861 return;
Wincy Van705699a2015-02-03 23:58:17 +08005862
David Matlackc9f04402017-08-01 14:00:40 -07005863 vmx->nested.pi_pending = false;
5864 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5865 return;
Wincy Van705699a2015-02-03 23:58:17 +08005866
David Matlackc9f04402017-08-01 14:00:40 -07005867 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5868 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005869 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005870 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5871 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005872 kunmap(vmx->nested.virtual_apic_page);
5873
5874 status = vmcs_read16(GUEST_INTR_STATUS);
5875 if ((u8)max_irr > ((u8)status & 0xff)) {
5876 status &= ~0xff;
5877 status |= (u8)max_irr;
5878 vmcs_write16(GUEST_INTR_STATUS, status);
5879 }
5880 }
David Matlackc9f04402017-08-01 14:00:40 -07005881
5882 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005883}
5884
Wincy Van06a55242017-04-28 13:13:59 +08005885static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5886 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005887{
5888#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005889 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5890
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005891 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005892 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005893 * The vector of interrupt to be delivered to vcpu had
5894 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005895 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005896 * Following cases will be reached in this block, and
5897 * we always send a notification event in all cases as
5898 * explained below.
5899 *
5900 * Case 1: vcpu keeps in non-root mode. Sending a
5901 * notification event posts the interrupt to vcpu.
5902 *
5903 * Case 2: vcpu exits to root mode and is still
5904 * runnable. PIR will be synced to vIRR before the
5905 * next vcpu entry. Sending a notification event in
5906 * this case has no effect, as vcpu is not in root
5907 * mode.
5908 *
5909 * Case 3: vcpu exits to root mode and is blocked.
5910 * vcpu_block() has already synced PIR to vIRR and
5911 * never blocks vcpu if vIRR is not cleared. Therefore,
5912 * a blocked vcpu here does not wait for any requested
5913 * interrupts in PIR, and sending a notification event
5914 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005915 */
Feng Wu28b835d2015-09-18 22:29:54 +08005916
Wincy Van06a55242017-04-28 13:13:59 +08005917 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005918 return true;
5919 }
5920#endif
5921 return false;
5922}
5923
Wincy Van705699a2015-02-03 23:58:17 +08005924static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5925 int vector)
5926{
5927 struct vcpu_vmx *vmx = to_vmx(vcpu);
5928
5929 if (is_guest_mode(vcpu) &&
5930 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005931 /*
5932 * If a posted intr is not recognized by hardware,
5933 * we will accomplish it in the next vmentry.
5934 */
5935 vmx->nested.pi_pending = true;
5936 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005937 /* the PIR and ON have been set by L1. */
5938 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5939 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005940 return 0;
5941 }
5942 return -1;
5943}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005944/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005945 * Send interrupt to vcpu via posted interrupt way.
5946 * 1. If target vcpu is running(non-root mode), send posted interrupt
5947 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5948 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5949 * interrupt from PIR in next vmentry.
5950 */
5951static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5952{
5953 struct vcpu_vmx *vmx = to_vmx(vcpu);
5954 int r;
5955
Wincy Van705699a2015-02-03 23:58:17 +08005956 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5957 if (!r)
5958 return;
5959
Yang Zhanga20ed542013-04-11 19:25:15 +08005960 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5961 return;
5962
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005963 /* If a previous notification has sent the IPI, nothing to do. */
5964 if (pi_test_and_set_on(&vmx->pi_desc))
5965 return;
5966
Wincy Van06a55242017-04-28 13:13:59 +08005967 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005968 kvm_vcpu_kick(vcpu);
5969}
5970
Avi Kivity6aa8b732006-12-10 02:21:36 -08005971/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005972 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5973 * will not change in the lifetime of the guest.
5974 * Note that host-state that does change is set elsewhere. E.g., host-state
5975 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5976 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005977static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005978{
5979 u32 low32, high32;
5980 unsigned long tmpl;
5981 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005982 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005983
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005984 cr0 = read_cr0();
5985 WARN_ON(cr0 & X86_CR0_TS);
5986 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005987
5988 /*
5989 * Save the most likely value for this task's CR3 in the VMCS.
5990 * We can't use __get_current_cr3_fast() because we're not atomic.
5991 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005992 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005993 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005994 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005995
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005996 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005997 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005998 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005999 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006000
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006001 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006002#ifdef CONFIG_X86_64
6003 /*
6004 * Load null selectors, so we can avoid reloading them in
6005 * __vmx_load_host_state(), in case userspace uses the null selectors
6006 * too (the expected case).
6007 */
6008 vmcs_write16(HOST_DS_SELECTOR, 0);
6009 vmcs_write16(HOST_ES_SELECTOR, 0);
6010#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006011 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6012 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006013#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006014 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6015 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6016
Juergen Gross87930012017-09-04 12:25:27 +02006017 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006018 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006019 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006020
Avi Kivity83287ea422012-09-16 15:10:57 +03006021 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006022
6023 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6024 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6025 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6026 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6027
6028 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6029 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6030 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6031 }
6032}
6033
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006034static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6035{
6036 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6037 if (enable_ept)
6038 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006039 if (is_guest_mode(&vmx->vcpu))
6040 vmx->vcpu.arch.cr4_guest_owned_bits &=
6041 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006042 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6043}
6044
Yang Zhang01e439b2013-04-11 19:25:12 +08006045static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6046{
6047 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6048
Andrey Smetanind62caab2015-11-10 15:36:33 +03006049 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006050 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006051
6052 if (!enable_vnmi)
6053 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6054
Yunhong Jiang64672c92016-06-13 14:19:59 -07006055 /* Enable the preemption timer dynamically */
6056 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006057 return pin_based_exec_ctrl;
6058}
6059
Andrey Smetanind62caab2015-11-10 15:36:33 +03006060static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6061{
6062 struct vcpu_vmx *vmx = to_vmx(vcpu);
6063
6064 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006065 if (cpu_has_secondary_exec_ctrls()) {
6066 if (kvm_vcpu_apicv_active(vcpu))
6067 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6068 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6069 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6070 else
6071 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6072 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6073 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6074 }
6075
6076 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006077 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006078}
6079
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006080static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6081{
6082 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006083
6084 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6085 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6086
Paolo Bonzini35754c92015-07-29 12:05:37 +02006087 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006088 exec_control &= ~CPU_BASED_TPR_SHADOW;
6089#ifdef CONFIG_X86_64
6090 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6091 CPU_BASED_CR8_LOAD_EXITING;
6092#endif
6093 }
6094 if (!enable_ept)
6095 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6096 CPU_BASED_CR3_LOAD_EXITING |
6097 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006098 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6099 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6100 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006101 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6102 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006103 return exec_control;
6104}
6105
Jim Mattson45ec3682017-08-23 16:32:04 -07006106static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006107{
Jim Mattson45ec3682017-08-23 16:32:04 -07006108 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006109 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006110}
6111
Jim Mattson75f4fc82017-08-23 16:32:03 -07006112static bool vmx_rdseed_supported(void)
6113{
6114 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006115 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006116}
6117
Paolo Bonzini80154d72017-08-24 13:55:35 +02006118static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006119{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006120 struct kvm_vcpu *vcpu = &vmx->vcpu;
6121
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006122 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006123
Paolo Bonzini80154d72017-08-24 13:55:35 +02006124 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006125 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6126 if (vmx->vpid == 0)
6127 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6128 if (!enable_ept) {
6129 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6130 enable_unrestricted_guest = 0;
6131 }
6132 if (!enable_unrestricted_guest)
6133 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006134 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006135 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006136 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006137 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6138 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006139 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006140
6141 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6142 * in vmx_set_cr4. */
6143 exec_control &= ~SECONDARY_EXEC_DESC;
6144
Abel Gordonabc4fc52013-04-18 14:35:25 +03006145 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6146 (handle_vmptrld).
6147 We can NOT enable shadow_vmcs here because we don't have yet
6148 a current VMCS12
6149 */
6150 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006151
6152 if (!enable_pml)
6153 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006154
Paolo Bonzini3db13482017-08-24 14:48:03 +02006155 if (vmx_xsaves_supported()) {
6156 /* Exposing XSAVES only when XSAVE is exposed */
6157 bool xsaves_enabled =
6158 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6159 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6160
6161 if (!xsaves_enabled)
6162 exec_control &= ~SECONDARY_EXEC_XSAVES;
6163
6164 if (nested) {
6165 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006166 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006167 SECONDARY_EXEC_XSAVES;
6168 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006169 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006170 ~SECONDARY_EXEC_XSAVES;
6171 }
6172 }
6173
Paolo Bonzini80154d72017-08-24 13:55:35 +02006174 if (vmx_rdtscp_supported()) {
6175 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6176 if (!rdtscp_enabled)
6177 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6178
6179 if (nested) {
6180 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006181 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006182 SECONDARY_EXEC_RDTSCP;
6183 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006184 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006185 ~SECONDARY_EXEC_RDTSCP;
6186 }
6187 }
6188
6189 if (vmx_invpcid_supported()) {
6190 /* Exposing INVPCID only when PCID is exposed */
6191 bool invpcid_enabled =
6192 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6193 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6194
6195 if (!invpcid_enabled) {
6196 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6197 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6198 }
6199
6200 if (nested) {
6201 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006202 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006203 SECONDARY_EXEC_ENABLE_INVPCID;
6204 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006205 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006206 ~SECONDARY_EXEC_ENABLE_INVPCID;
6207 }
6208 }
6209
Jim Mattson45ec3682017-08-23 16:32:04 -07006210 if (vmx_rdrand_supported()) {
6211 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6212 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006213 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006214
6215 if (nested) {
6216 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006217 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006218 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006219 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006220 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006221 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006222 }
6223 }
6224
Jim Mattson75f4fc82017-08-23 16:32:03 -07006225 if (vmx_rdseed_supported()) {
6226 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6227 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006228 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006229
6230 if (nested) {
6231 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006232 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006233 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006234 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006235 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006236 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006237 }
6238 }
6239
Paolo Bonzini80154d72017-08-24 13:55:35 +02006240 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006241}
6242
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006243static void ept_set_mmio_spte_mask(void)
6244{
6245 /*
6246 * EPT Misconfigurations can be generated if the value of bits 2:0
6247 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006248 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006249 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6250 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006251}
6252
Wanpeng Lif53cd632014-12-02 19:14:58 +08006253#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006254/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006255 * Sets up the vmcs for emulated real mode.
6256 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006257static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006258{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006259#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006260 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006261#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006262 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006263
Abel Gordon4607c2d2013-04-18 14:35:55 +03006264 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006265 /*
6266 * At vCPU creation, "VMWRITE to any supported field
6267 * in the VMCS" is supported, so use the more
6268 * permissive vmx_vmread_bitmap to specify both read
6269 * and write permissions for the shadow VMCS.
6270 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006271 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006272 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006273 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006274 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006275 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006276
Avi Kivity6aa8b732006-12-10 02:21:36 -08006277 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6278
Avi Kivity6aa8b732006-12-10 02:21:36 -08006279 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006280 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006281 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006282
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006283 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006284
Dan Williamsdfa169b2016-06-02 11:17:24 -07006285 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006286 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006287 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006288 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006289 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006290
Andrey Smetanind62caab2015-11-10 15:36:33 +03006291 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006292 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6293 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6294 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6295 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6296
6297 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006298
Li RongQing0bcf2612015-12-03 13:29:34 +08006299 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006300 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006301 }
6302
Wanpeng Lib31c1142018-03-12 04:53:04 -07006303 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006304 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006305 vmx->ple_window = ple_window;
6306 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006307 }
6308
Xiao Guangrongc3707952011-07-12 03:28:04 +08006309 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6310 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006311 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6312
Avi Kivity9581d442010-10-19 16:46:55 +02006313 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6314 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006315 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006316#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317 rdmsrl(MSR_FS_BASE, a);
6318 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6319 rdmsrl(MSR_GS_BASE, a);
6320 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6321#else
6322 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6323 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6324#endif
6325
Bandan Das2a499e42017-08-03 15:54:41 -04006326 if (cpu_has_vmx_vmfunc())
6327 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6328
Eddie Dong2cc51562007-05-21 07:28:09 +03006329 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6330 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006331 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006332 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006333 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006334
Radim Krčmář74545702015-04-27 15:11:25 +02006335 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6336 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006337
Paolo Bonzini03916db2014-07-24 14:21:57 +02006338 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339 u32 index = vmx_msr_index[i];
6340 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006341 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006342
6343 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6344 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006345 if (wrmsr_safe(index, data_low, data_high) < 0)
6346 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006347 vmx->guest_msrs[j].index = i;
6348 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006349 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006350 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006351 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006352
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006353 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6354 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006355
6356 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006357
6358 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006359 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006360
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006361 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6362 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6363
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006364 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006365
Wanpeng Lif53cd632014-12-02 19:14:58 +08006366 if (vmx_xsaves_supported())
6367 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6368
Peter Feiner4e595162016-07-07 14:49:58 -07006369 if (enable_pml) {
6370 ASSERT(vmx->pml_pg);
6371 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6372 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6373 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006374}
6375
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006376static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006377{
6378 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006379 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006380 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006381
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006382 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006383 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006384
Wanpeng Li518e7b92018-02-28 14:03:31 +08006385 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006386 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006387 kvm_set_cr8(vcpu, 0);
6388
6389 if (!init_event) {
6390 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6391 MSR_IA32_APICBASE_ENABLE;
6392 if (kvm_vcpu_is_reset_bsp(vcpu))
6393 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6394 apic_base_msr.host_initiated = true;
6395 kvm_set_apic_base(vcpu, &apic_base_msr);
6396 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006397
Avi Kivity2fb92db2011-04-27 19:42:18 +03006398 vmx_segment_cache_clear(vmx);
6399
Avi Kivity5706be02008-08-20 15:07:31 +03006400 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006401 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006402 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006403
6404 seg_setup(VCPU_SREG_DS);
6405 seg_setup(VCPU_SREG_ES);
6406 seg_setup(VCPU_SREG_FS);
6407 seg_setup(VCPU_SREG_GS);
6408 seg_setup(VCPU_SREG_SS);
6409
6410 vmcs_write16(GUEST_TR_SELECTOR, 0);
6411 vmcs_writel(GUEST_TR_BASE, 0);
6412 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6413 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6414
6415 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6416 vmcs_writel(GUEST_LDTR_BASE, 0);
6417 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6418 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6419
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006420 if (!init_event) {
6421 vmcs_write32(GUEST_SYSENTER_CS, 0);
6422 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6423 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6424 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6425 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006426
Wanpeng Lic37c2872017-11-20 14:52:21 -08006427 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006428 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006429
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006430 vmcs_writel(GUEST_GDTR_BASE, 0);
6431 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6432
6433 vmcs_writel(GUEST_IDTR_BASE, 0);
6434 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6435
Anthony Liguori443381a2010-12-06 10:53:38 -06006436 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006437 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006438 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006439 if (kvm_mpx_supported())
6440 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006441
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006442 setup_msrs(vmx);
6443
Avi Kivity6aa8b732006-12-10 02:21:36 -08006444 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6445
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006446 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006447 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006448 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006449 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006450 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006451 vmcs_write32(TPR_THRESHOLD, 0);
6452 }
6453
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006454 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006455
Sheng Yang2384d2b2008-01-17 15:14:33 +08006456 if (vmx->vpid != 0)
6457 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6458
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006459 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006460 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006461 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006462 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006463 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006464
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006465 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006466
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006467 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006468 if (init_event)
6469 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006470}
6471
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006472/*
6473 * In nested virtualization, check if L1 asked to exit on external interrupts.
6474 * For most existing hypervisors, this will always return true.
6475 */
6476static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6477{
6478 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6479 PIN_BASED_EXT_INTR_MASK;
6480}
6481
Bandan Das77b0f5d2014-04-19 18:17:45 -04006482/*
6483 * In nested virtualization, check if L1 has set
6484 * VM_EXIT_ACK_INTR_ON_EXIT
6485 */
6486static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6487{
6488 return get_vmcs12(vcpu)->vm_exit_controls &
6489 VM_EXIT_ACK_INTR_ON_EXIT;
6490}
6491
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006492static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6493{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006494 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006495}
6496
Jan Kiszkac9a79532014-03-07 20:03:15 +01006497static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006498{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006499 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6500 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006501}
6502
Jan Kiszkac9a79532014-03-07 20:03:15 +01006503static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006504{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006505 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006506 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006507 enable_irq_window(vcpu);
6508 return;
6509 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006510
Paolo Bonzini47c01522016-12-19 11:44:07 +01006511 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6512 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006513}
6514
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006515static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006516{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006517 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006518 uint32_t intr;
6519 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006520
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006521 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006522
Avi Kivityfa89a812008-09-01 15:57:51 +03006523 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006524 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006525 int inc_eip = 0;
6526 if (vcpu->arch.interrupt.soft)
6527 inc_eip = vcpu->arch.event_exit_inst_len;
6528 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006529 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006530 return;
6531 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006532 intr = irq | INTR_INFO_VALID_MASK;
6533 if (vcpu->arch.interrupt.soft) {
6534 intr |= INTR_TYPE_SOFT_INTR;
6535 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6536 vmx->vcpu.arch.event_exit_inst_len);
6537 } else
6538 intr |= INTR_TYPE_EXT_INTR;
6539 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006540
6541 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006542}
6543
Sheng Yangf08864b2008-05-15 18:23:25 +08006544static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6545{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006546 struct vcpu_vmx *vmx = to_vmx(vcpu);
6547
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006548 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006549 /*
6550 * Tracking the NMI-blocked state in software is built upon
6551 * finding the next open IRQ window. This, in turn, depends on
6552 * well-behaving guests: They have to keep IRQs disabled at
6553 * least as long as the NMI handler runs. Otherwise we may
6554 * cause NMI nesting, maybe breaking the guest. But as this is
6555 * highly unlikely, we can live with the residual risk.
6556 */
6557 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6558 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6559 }
6560
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006561 ++vcpu->stat.nmi_injections;
6562 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006563
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006564 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006565 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006566 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006567 return;
6568 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006569
Sheng Yangf08864b2008-05-15 18:23:25 +08006570 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6571 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006572
6573 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006574}
6575
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006576static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6577{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006578 struct vcpu_vmx *vmx = to_vmx(vcpu);
6579 bool masked;
6580
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006581 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006582 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006583 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006584 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006585 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6586 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6587 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006588}
6589
6590static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6591{
6592 struct vcpu_vmx *vmx = to_vmx(vcpu);
6593
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006594 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006595 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6596 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6597 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6598 }
6599 } else {
6600 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6601 if (masked)
6602 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6603 GUEST_INTR_STATE_NMI);
6604 else
6605 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6606 GUEST_INTR_STATE_NMI);
6607 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006608}
6609
Jan Kiszka2505dc92013-04-14 12:12:47 +02006610static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6611{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006612 if (to_vmx(vcpu)->nested.nested_run_pending)
6613 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006614
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006615 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006616 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6617 return 0;
6618
Jan Kiszka2505dc92013-04-14 12:12:47 +02006619 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6620 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6621 | GUEST_INTR_STATE_NMI));
6622}
6623
Gleb Natapov78646122009-03-23 12:12:11 +02006624static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6625{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006626 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6627 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006628 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6629 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006630}
6631
Izik Eiduscbc94022007-10-25 00:29:55 +02006632static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6633{
6634 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006635
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006636 if (enable_unrestricted_guest)
6637 return 0;
6638
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006639 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6640 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006641 if (ret)
6642 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006643 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006644 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006645}
6646
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006647static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6648{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006649 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006650 return 0;
6651}
6652
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006653static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006654{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006655 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006656 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006657 /*
6658 * Update instruction length as we may reinject the exception
6659 * from user space while in guest debugging mode.
6660 */
6661 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6662 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006663 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006664 return false;
6665 /* fall through */
6666 case DB_VECTOR:
6667 if (vcpu->guest_debug &
6668 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6669 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006670 /* fall through */
6671 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006672 case OF_VECTOR:
6673 case BR_VECTOR:
6674 case UD_VECTOR:
6675 case DF_VECTOR:
6676 case SS_VECTOR:
6677 case GP_VECTOR:
6678 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006679 return true;
6680 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006681 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006682 return false;
6683}
6684
6685static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6686 int vec, u32 err_code)
6687{
6688 /*
6689 * Instruction with address size override prefix opcode 0x67
6690 * Cause the #SS fault with 0 error code in VM86 mode.
6691 */
6692 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6693 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6694 if (vcpu->arch.halt_request) {
6695 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006696 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006697 }
6698 return 1;
6699 }
6700 return 0;
6701 }
6702
6703 /*
6704 * Forward all other exceptions that are valid in real mode.
6705 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6706 * the required debugging infrastructure rework.
6707 */
6708 kvm_queue_exception(vcpu, vec);
6709 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006710}
6711
Andi Kleena0861c02009-06-08 17:37:09 +08006712/*
6713 * Trigger machine check on the host. We assume all the MSRs are already set up
6714 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6715 * We pass a fake environment to the machine check handler because we want
6716 * the guest to be always treated like user space, no matter what context
6717 * it used internally.
6718 */
6719static void kvm_machine_check(void)
6720{
6721#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6722 struct pt_regs regs = {
6723 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6724 .flags = X86_EFLAGS_IF,
6725 };
6726
6727 do_machine_check(&regs, 0);
6728#endif
6729}
6730
Avi Kivity851ba692009-08-24 11:10:17 +03006731static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006732{
6733 /* already handled by vcpu_run */
6734 return 1;
6735}
6736
Avi Kivity851ba692009-08-24 11:10:17 +03006737static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006738{
Avi Kivity1155f762007-11-22 11:30:47 +02006739 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006740 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006741 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006742 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006743 u32 vect_info;
6744 enum emulation_result er;
6745
Avi Kivity1155f762007-11-22 11:30:47 +02006746 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006747 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006748
Andi Kleena0861c02009-06-08 17:37:09 +08006749 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006750 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006751
Jim Mattsonef85b672016-12-12 11:01:37 -08006752 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006753 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006754
Wanpeng Li082d06e2018-04-03 16:28:48 -07006755 if (is_invalid_opcode(intr_info))
6756 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006757
Avi Kivity6aa8b732006-12-10 02:21:36 -08006758 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006759 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006760 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006761
Liran Alon9e869482018-03-12 13:12:51 +02006762 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6763 WARN_ON_ONCE(!enable_vmware_backdoor);
6764 er = emulate_instruction(vcpu,
6765 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6766 if (er == EMULATE_USER_EXIT)
6767 return 0;
6768 else if (er != EMULATE_DONE)
6769 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6770 return 1;
6771 }
6772
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006773 /*
6774 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6775 * MMIO, it is better to report an internal error.
6776 * See the comments in vmx_handle_exit.
6777 */
6778 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6779 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6780 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6781 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006782 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006783 vcpu->run->internal.data[0] = vect_info;
6784 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006785 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006786 return 0;
6787 }
6788
Avi Kivity6aa8b732006-12-10 02:21:36 -08006789 if (is_page_fault(intr_info)) {
6790 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006791 /* EPT won't cause page fault directly */
6792 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006793 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006794 }
6795
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006796 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006797
6798 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6799 return handle_rmode_exception(vcpu, ex_no, error_code);
6800
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006801 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006802 case AC_VECTOR:
6803 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6804 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006805 case DB_VECTOR:
6806 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6807 if (!(vcpu->guest_debug &
6808 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006809 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006810 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006811 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006812 skip_emulated_instruction(vcpu);
6813
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006814 kvm_queue_exception(vcpu, DB_VECTOR);
6815 return 1;
6816 }
6817 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6818 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6819 /* fall through */
6820 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006821 /*
6822 * Update instruction length as we may reinject #BP from
6823 * user space while in guest debugging mode. Reading it for
6824 * #DB as well causes no harm, it is not used in that case.
6825 */
6826 vmx->vcpu.arch.event_exit_inst_len =
6827 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006829 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006830 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6831 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006832 break;
6833 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006834 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6835 kvm_run->ex.exception = ex_no;
6836 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006837 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006838 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006839 return 0;
6840}
6841
Avi Kivity851ba692009-08-24 11:10:17 +03006842static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006843{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006844 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006845 return 1;
6846}
6847
Avi Kivity851ba692009-08-24 11:10:17 +03006848static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006849{
Avi Kivity851ba692009-08-24 11:10:17 +03006850 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006851 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006852 return 0;
6853}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854
Avi Kivity851ba692009-08-24 11:10:17 +03006855static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006856{
He, Qingbfdaab02007-09-12 14:18:28 +08006857 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006858 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006859 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006860
He, Qingbfdaab02007-09-12 14:18:28 +08006861 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006862 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006863
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006864 ++vcpu->stat.io_exits;
6865
Sean Christopherson432baf62018-03-08 08:57:26 -08006866 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006867 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006868
6869 port = exit_qualification >> 16;
6870 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006871 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006872
Sean Christophersondca7f122018-03-08 08:57:27 -08006873 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006874}
6875
Ingo Molnar102d8322007-02-19 14:37:47 +02006876static void
6877vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6878{
6879 /*
6880 * Patch in the VMCALL instruction:
6881 */
6882 hypercall[0] = 0x0f;
6883 hypercall[1] = 0x01;
6884 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006885}
6886
Guo Chao0fa06072012-06-28 15:16:19 +08006887/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006888static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6889{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006890 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006891 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6892 unsigned long orig_val = val;
6893
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006894 /*
6895 * We get here when L2 changed cr0 in a way that did not change
6896 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006897 * but did change L0 shadowed bits. So we first calculate the
6898 * effective cr0 value that L1 would like to write into the
6899 * hardware. It consists of the L2-owned bits from the new
6900 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006901 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006902 val = (val & ~vmcs12->cr0_guest_host_mask) |
6903 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6904
David Matlack38991522016-11-29 18:14:08 -08006905 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006906 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006907
6908 if (kvm_set_cr0(vcpu, val))
6909 return 1;
6910 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006911 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006912 } else {
6913 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006914 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006915 return 1;
David Matlack38991522016-11-29 18:14:08 -08006916
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006917 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006918 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006919}
6920
6921static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6922{
6923 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006924 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6925 unsigned long orig_val = val;
6926
6927 /* analogously to handle_set_cr0 */
6928 val = (val & ~vmcs12->cr4_guest_host_mask) |
6929 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6930 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006931 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006932 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006933 return 0;
6934 } else
6935 return kvm_set_cr4(vcpu, val);
6936}
6937
Paolo Bonzini0367f202016-07-12 10:44:55 +02006938static int handle_desc(struct kvm_vcpu *vcpu)
6939{
6940 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6941 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6942}
6943
Avi Kivity851ba692009-08-24 11:10:17 +03006944static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006945{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006946 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006947 int cr;
6948 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006949 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006950 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006951
He, Qingbfdaab02007-09-12 14:18:28 +08006952 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006953 cr = exit_qualification & 15;
6954 reg = (exit_qualification >> 8) & 15;
6955 switch ((exit_qualification >> 4) & 3) {
6956 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006957 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006958 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006959 switch (cr) {
6960 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006961 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006962 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006963 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006964 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006965 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006966 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006967 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006968 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006969 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006970 case 8: {
6971 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006972 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006973 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006974 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006975 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006976 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006977 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006978 return ret;
6979 /*
6980 * TODO: we might be squashing a
6981 * KVM_GUESTDBG_SINGLESTEP-triggered
6982 * KVM_EXIT_DEBUG here.
6983 */
Avi Kivity851ba692009-08-24 11:10:17 +03006984 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006985 return 0;
6986 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006987 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006989 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006990 WARN_ONCE(1, "Guest should always own CR0.TS");
6991 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006992 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006993 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006994 case 1: /*mov from cr*/
6995 switch (cr) {
6996 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006997 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006998 val = kvm_read_cr3(vcpu);
6999 kvm_register_write(vcpu, reg, val);
7000 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007001 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007002 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007003 val = kvm_get_cr8(vcpu);
7004 kvm_register_write(vcpu, reg, val);
7005 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007006 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007007 }
7008 break;
7009 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007010 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007011 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007012 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007013
Kyle Huey6affcbe2016-11-29 12:40:40 -08007014 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007015 default:
7016 break;
7017 }
Avi Kivity851ba692009-08-24 11:10:17 +03007018 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007019 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007020 (int)(exit_qualification >> 4) & 3, cr);
7021 return 0;
7022}
7023
Avi Kivity851ba692009-08-24 11:10:17 +03007024static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007025{
He, Qingbfdaab02007-09-12 14:18:28 +08007026 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007027 int dr, dr7, reg;
7028
7029 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7030 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7031
7032 /* First, if DR does not exist, trigger UD */
7033 if (!kvm_require_dr(vcpu, dr))
7034 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007035
Jan Kiszkaf2483412010-01-20 18:20:20 +01007036 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007037 if (!kvm_require_cpl(vcpu, 0))
7038 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007039 dr7 = vmcs_readl(GUEST_DR7);
7040 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007041 /*
7042 * As the vm-exit takes precedence over the debug trap, we
7043 * need to emulate the latter, either for the host or the
7044 * guest debugging itself.
7045 */
7046 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007047 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007048 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007049 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007050 vcpu->run->debug.arch.exception = DB_VECTOR;
7051 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007052 return 0;
7053 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007054 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007055 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007056 kvm_queue_exception(vcpu, DB_VECTOR);
7057 return 1;
7058 }
7059 }
7060
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007061 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007062 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7063 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007064
7065 /*
7066 * No more DR vmexits; force a reload of the debug registers
7067 * and reenter on this instruction. The next vmexit will
7068 * retrieve the full state of the debug registers.
7069 */
7070 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7071 return 1;
7072 }
7073
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007074 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7075 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007076 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007077
7078 if (kvm_get_dr(vcpu, dr, &val))
7079 return 1;
7080 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007081 } else
Nadav Amit57773922014-06-18 17:19:23 +03007082 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007083 return 1;
7084
Kyle Huey6affcbe2016-11-29 12:40:40 -08007085 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086}
7087
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007088static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7089{
7090 return vcpu->arch.dr6;
7091}
7092
7093static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7094{
7095}
7096
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007097static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7098{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007099 get_debugreg(vcpu->arch.db[0], 0);
7100 get_debugreg(vcpu->arch.db[1], 1);
7101 get_debugreg(vcpu->arch.db[2], 2);
7102 get_debugreg(vcpu->arch.db[3], 3);
7103 get_debugreg(vcpu->arch.dr6, 6);
7104 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7105
7106 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007107 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007108}
7109
Gleb Natapov020df072010-04-13 10:05:23 +03007110static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7111{
7112 vmcs_writel(GUEST_DR7, val);
7113}
7114
Avi Kivity851ba692009-08-24 11:10:17 +03007115static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116{
Kyle Huey6a908b62016-11-29 12:40:37 -08007117 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007118}
7119
Avi Kivity851ba692009-08-24 11:10:17 +03007120static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007121{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007122 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007123 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007124
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007125 msr_info.index = ecx;
7126 msr_info.host_initiated = false;
7127 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007128 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007129 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130 return 1;
7131 }
7132
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007133 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007134
Avi Kivity6aa8b732006-12-10 02:21:36 -08007135 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007136 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7137 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007138 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007139}
7140
Avi Kivity851ba692009-08-24 11:10:17 +03007141static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142{
Will Auld8fe8ab42012-11-29 12:42:12 -08007143 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007144 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7145 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7146 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147
Will Auld8fe8ab42012-11-29 12:42:12 -08007148 msr.data = data;
7149 msr.index = ecx;
7150 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007151 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007152 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007153 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007154 return 1;
7155 }
7156
Avi Kivity59200272010-01-25 19:47:02 +02007157 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007158 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159}
7160
Avi Kivity851ba692009-08-24 11:10:17 +03007161static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007162{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007163 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007164 return 1;
7165}
7166
Avi Kivity851ba692009-08-24 11:10:17 +03007167static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007168{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007169 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7170 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007171
Avi Kivity3842d132010-07-27 12:30:24 +03007172 kvm_make_request(KVM_REQ_EVENT, vcpu);
7173
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007174 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007175 return 1;
7176}
7177
Avi Kivity851ba692009-08-24 11:10:17 +03007178static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007179{
Avi Kivityd3bef152007-06-05 15:53:05 +03007180 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007181}
7182
Avi Kivity851ba692009-08-24 11:10:17 +03007183static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007184{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007185 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007186}
7187
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007188static int handle_invd(struct kvm_vcpu *vcpu)
7189{
Andre Przywara51d8b662010-12-21 11:12:02 +01007190 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007191}
7192
Avi Kivity851ba692009-08-24 11:10:17 +03007193static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007194{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007195 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007196
7197 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007198 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007199}
7200
Avi Kivityfee84b02011-11-10 14:57:25 +02007201static int handle_rdpmc(struct kvm_vcpu *vcpu)
7202{
7203 int err;
7204
7205 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007206 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007207}
7208
Avi Kivity851ba692009-08-24 11:10:17 +03007209static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007210{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007211 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007212}
7213
Dexuan Cui2acf9232010-06-10 11:27:12 +08007214static int handle_xsetbv(struct kvm_vcpu *vcpu)
7215{
7216 u64 new_bv = kvm_read_edx_eax(vcpu);
7217 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7218
7219 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007220 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007221 return 1;
7222}
7223
Wanpeng Lif53cd632014-12-02 19:14:58 +08007224static int handle_xsaves(struct kvm_vcpu *vcpu)
7225{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007226 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007227 WARN(1, "this should never happen\n");
7228 return 1;
7229}
7230
7231static int handle_xrstors(struct kvm_vcpu *vcpu)
7232{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007233 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007234 WARN(1, "this should never happen\n");
7235 return 1;
7236}
7237
Avi Kivity851ba692009-08-24 11:10:17 +03007238static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007239{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007240 if (likely(fasteoi)) {
7241 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7242 int access_type, offset;
7243
7244 access_type = exit_qualification & APIC_ACCESS_TYPE;
7245 offset = exit_qualification & APIC_ACCESS_OFFSET;
7246 /*
7247 * Sane guest uses MOV to write EOI, with written value
7248 * not cared. So make a short-circuit here by avoiding
7249 * heavy instruction emulation.
7250 */
7251 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7252 (offset == APIC_EOI)) {
7253 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007255 }
7256 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007257 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007258}
7259
Yang Zhangc7c9c562013-01-25 10:18:51 +08007260static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7261{
7262 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7263 int vector = exit_qualification & 0xff;
7264
7265 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7266 kvm_apic_set_eoi_accelerated(vcpu, vector);
7267 return 1;
7268}
7269
Yang Zhang83d4c282013-01-25 10:18:49 +08007270static int handle_apic_write(struct kvm_vcpu *vcpu)
7271{
7272 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7273 u32 offset = exit_qualification & 0xfff;
7274
7275 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7276 kvm_apic_write_nodecode(vcpu, offset);
7277 return 1;
7278}
7279
Avi Kivity851ba692009-08-24 11:10:17 +03007280static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007281{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007282 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007283 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007284 bool has_error_code = false;
7285 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007286 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007287 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007288
7289 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007290 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007291 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007292
7293 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7294
7295 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007296 if (reason == TASK_SWITCH_GATE && idt_v) {
7297 switch (type) {
7298 case INTR_TYPE_NMI_INTR:
7299 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007300 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007301 break;
7302 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007303 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007304 kvm_clear_interrupt_queue(vcpu);
7305 break;
7306 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007307 if (vmx->idt_vectoring_info &
7308 VECTORING_INFO_DELIVER_CODE_MASK) {
7309 has_error_code = true;
7310 error_code =
7311 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7312 }
7313 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007314 case INTR_TYPE_SOFT_EXCEPTION:
7315 kvm_clear_exception_queue(vcpu);
7316 break;
7317 default:
7318 break;
7319 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007320 }
Izik Eidus37817f22008-03-24 23:14:53 +02007321 tss_selector = exit_qualification;
7322
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007323 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7324 type != INTR_TYPE_EXT_INTR &&
7325 type != INTR_TYPE_NMI_INTR))
7326 skip_emulated_instruction(vcpu);
7327
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007328 if (kvm_task_switch(vcpu, tss_selector,
7329 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7330 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007331 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7332 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7333 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007334 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007335 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007336
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007337 /*
7338 * TODO: What about debug traps on tss switch?
7339 * Are we supposed to inject them and update dr6?
7340 */
7341
7342 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007343}
7344
Avi Kivity851ba692009-08-24 11:10:17 +03007345static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007346{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007347 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007348 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007349 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007350
Sheng Yangf9c617f2009-03-25 10:08:52 +08007351 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007352
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007353 /*
7354 * EPT violation happened while executing iret from NMI,
7355 * "blocked by NMI" bit has to be set before next VM entry.
7356 * There are errata that may cause this bit to not be set:
7357 * AAK134, BY25.
7358 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007359 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007360 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007361 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007362 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7363
Sheng Yang14394422008-04-28 12:24:45 +08007364 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007365 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007366
Junaid Shahid27959a42016-12-06 16:46:10 -08007367 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007368 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007369 ? PFERR_USER_MASK : 0;
7370 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007371 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007372 ? PFERR_WRITE_MASK : 0;
7373 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007374 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007375 ? PFERR_FETCH_MASK : 0;
7376 /* ept page table entry is present? */
7377 error_code |= (exit_qualification &
7378 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7379 EPT_VIOLATION_EXECUTABLE))
7380 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007381
Paolo Bonzinieebed242016-11-28 14:39:58 +01007382 error_code |= (exit_qualification & 0x100) != 0 ?
7383 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007384
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007385 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007386 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007387}
7388
Avi Kivity851ba692009-08-24 11:10:17 +03007389static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007390{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007391 gpa_t gpa;
7392
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007393 /*
7394 * A nested guest cannot optimize MMIO vmexits, because we have an
7395 * nGPA here instead of the required GPA.
7396 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007397 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007398 if (!is_guest_mode(vcpu) &&
7399 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007400 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007401 /*
7402 * Doing kvm_skip_emulated_instruction() depends on undefined
7403 * behavior: Intel's manual doesn't mandate
7404 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7405 * occurs and while on real hardware it was observed to be set,
7406 * other hypervisors (namely Hyper-V) don't set it, we end up
7407 * advancing IP with some random value. Disable fast mmio when
7408 * running nested and keep it for real hardware in hope that
7409 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7410 */
7411 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7412 return kvm_skip_emulated_instruction(vcpu);
7413 else
7414 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7415 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007416 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007417
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007418 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007419}
7420
Avi Kivity851ba692009-08-24 11:10:17 +03007421static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007422{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007423 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007424 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7425 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007426 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007427 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007428
7429 return 1;
7430}
7431
Mohammed Gamal80ced182009-09-01 12:48:18 +02007432static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007433{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007434 struct vcpu_vmx *vmx = to_vmx(vcpu);
7435 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007436 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007437 u32 cpu_exec_ctrl;
7438 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007439 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007440
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007441 /*
7442 * We should never reach the point where we are emulating L2
7443 * due to invalid guest state as that means we incorrectly
7444 * allowed a nested VMEntry with an invalid vmcs12.
7445 */
7446 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7447
Avi Kivity49e9d552010-09-19 14:34:08 +02007448 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7449 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007450
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007451 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007452 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007453 return handle_interrupt_window(&vmx->vcpu);
7454
Radim Krčmář72875d82017-04-26 22:32:19 +02007455 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007456 return 1;
7457
Liran Alon9b8ae632017-11-05 16:56:34 +02007458 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007459
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007460 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007461 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007462 ret = 0;
7463 goto out;
7464 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007465
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007466 if (err != EMULATE_DONE)
7467 goto emulation_error;
7468
7469 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7470 vcpu->arch.exception.pending)
7471 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007472
Gleb Natapov8d76c492013-05-08 18:38:44 +03007473 if (vcpu->arch.halt_request) {
7474 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007475 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007476 goto out;
7477 }
7478
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007479 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007480 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007481 if (need_resched())
7482 schedule();
7483 }
7484
Mohammed Gamal80ced182009-09-01 12:48:18 +02007485out:
7486 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007487
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007488emulation_error:
7489 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7490 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7491 vcpu->run->internal.ndata = 0;
7492 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007493}
7494
7495static void grow_ple_window(struct kvm_vcpu *vcpu)
7496{
7497 struct vcpu_vmx *vmx = to_vmx(vcpu);
7498 int old = vmx->ple_window;
7499
Babu Mogerc8e88712018-03-16 16:37:24 -04007500 vmx->ple_window = __grow_ple_window(old, ple_window,
7501 ple_window_grow,
7502 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007503
7504 if (vmx->ple_window != old)
7505 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007506
7507 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007508}
7509
7510static void shrink_ple_window(struct kvm_vcpu *vcpu)
7511{
7512 struct vcpu_vmx *vmx = to_vmx(vcpu);
7513 int old = vmx->ple_window;
7514
Babu Mogerc8e88712018-03-16 16:37:24 -04007515 vmx->ple_window = __shrink_ple_window(old, ple_window,
7516 ple_window_shrink,
7517 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007518
7519 if (vmx->ple_window != old)
7520 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007521
7522 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007523}
7524
7525/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007526 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7527 */
7528static void wakeup_handler(void)
7529{
7530 struct kvm_vcpu *vcpu;
7531 int cpu = smp_processor_id();
7532
7533 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7534 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7535 blocked_vcpu_list) {
7536 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7537
7538 if (pi_test_on(pi_desc) == 1)
7539 kvm_vcpu_kick(vcpu);
7540 }
7541 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7542}
7543
Peng Haoe01bca22018-04-07 05:47:32 +08007544static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007545{
7546 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7547 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7548 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7549 0ull, VMX_EPT_EXECUTABLE_MASK,
7550 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007551 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007552
7553 ept_set_mmio_spte_mask();
7554 kvm_enable_tdp();
7555}
7556
Tiejun Chenf2c76482014-10-28 10:14:47 +08007557static __init int hardware_setup(void)
7558{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007559 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007560 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007561
7562 rdmsrl_safe(MSR_EFER, &host_efer);
7563
7564 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7565 kvm_define_shared_msr(i, vmx_msr_index[i]);
7566
Radim Krčmář23611332016-09-29 22:41:33 +02007567 for (i = 0; i < VMX_BITMAP_NR; i++) {
7568 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7569 if (!vmx_bitmap[i])
7570 goto out;
7571 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007572
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007573 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7574 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7575
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007576 if (setup_vmcs_config(&vmcs_config) < 0) {
7577 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007578 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007579 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007580
7581 if (boot_cpu_has(X86_FEATURE_NX))
7582 kvm_enable_efer_bits(EFER_NX);
7583
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007584 if (boot_cpu_has(X86_FEATURE_MPX)) {
7585 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7586 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7587 }
7588
Wanpeng Li08d839c2017-03-23 05:30:08 -07007589 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7590 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007591 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007592
Tiejun Chenf2c76482014-10-28 10:14:47 +08007593 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007594 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007595 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007596 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007597 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007598
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007599 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600 enable_ept_ad_bits = 0;
7601
Wanpeng Li8ad81822017-10-09 15:51:53 -07007602 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007603 enable_unrestricted_guest = 0;
7604
Paolo Bonziniad15a292015-01-30 16:18:49 +01007605 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007606 flexpriority_enabled = 0;
7607
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007608 if (!cpu_has_virtual_nmis())
7609 enable_vnmi = 0;
7610
Paolo Bonziniad15a292015-01-30 16:18:49 +01007611 /*
7612 * set_apic_access_page_addr() is used to reload apic access
7613 * page upon invalidation. No need to do anything if not
7614 * using the APIC_ACCESS_ADDR VMCS field.
7615 */
7616 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007617 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007618
7619 if (!cpu_has_vmx_tpr_shadow())
7620 kvm_x86_ops->update_cr8_intercept = NULL;
7621
7622 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7623 kvm_disable_largepages();
7624
Wanpeng Li0f107682017-09-28 18:06:24 -07007625 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007626 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007627 ple_window = 0;
7628 ple_window_grow = 0;
7629 ple_window_max = 0;
7630 ple_window_shrink = 0;
7631 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007632
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007633 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007634 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007635 kvm_x86_ops->sync_pir_to_irr = NULL;
7636 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007637
Haozhong Zhang64903d62015-10-20 15:39:09 +08007638 if (cpu_has_vmx_tsc_scaling()) {
7639 kvm_has_tsc_control = true;
7640 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7641 kvm_tsc_scaling_ratio_frac_bits = 48;
7642 }
7643
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007644 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7645
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007646 if (enable_ept)
7647 vmx_enable_tdp();
7648 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007649 kvm_disable_tdp();
7650
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007651 if (!nested) {
7652 kvm_x86_ops->get_nested_state = NULL;
7653 kvm_x86_ops->set_nested_state = NULL;
7654 }
7655
Kai Huang843e4332015-01-28 10:54:28 +08007656 /*
7657 * Only enable PML when hardware supports PML feature, and both EPT
7658 * and EPT A/D bit features are enabled -- PML depends on them to work.
7659 */
7660 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7661 enable_pml = 0;
7662
7663 if (!enable_pml) {
7664 kvm_x86_ops->slot_enable_log_dirty = NULL;
7665 kvm_x86_ops->slot_disable_log_dirty = NULL;
7666 kvm_x86_ops->flush_log_dirty = NULL;
7667 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7668 }
7669
Yunhong Jiang64672c92016-06-13 14:19:59 -07007670 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7671 u64 vmx_msr;
7672
7673 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7674 cpu_preemption_timer_multi =
7675 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7676 } else {
7677 kvm_x86_ops->set_hv_timer = NULL;
7678 kvm_x86_ops->cancel_hv_timer = NULL;
7679 }
7680
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007681 if (!cpu_has_vmx_shadow_vmcs())
7682 enable_shadow_vmcs = 0;
7683 if (enable_shadow_vmcs)
7684 init_vmcs_shadow_fields();
7685
Feng Wubf9f6ac2015-09-18 22:29:55 +08007686 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007687 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007688
Ashok Rajc45dcc72016-06-22 14:59:56 +08007689 kvm_mce_cap_supported |= MCG_LMCE_P;
7690
Tiejun Chenf2c76482014-10-28 10:14:47 +08007691 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007692
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007693out:
Radim Krčmář23611332016-09-29 22:41:33 +02007694 for (i = 0; i < VMX_BITMAP_NR; i++)
7695 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007696
7697 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007698}
7699
7700static __exit void hardware_unsetup(void)
7701{
Radim Krčmář23611332016-09-29 22:41:33 +02007702 int i;
7703
7704 for (i = 0; i < VMX_BITMAP_NR; i++)
7705 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007706
Tiejun Chenf2c76482014-10-28 10:14:47 +08007707 free_kvm_area();
7708}
7709
Avi Kivity6aa8b732006-12-10 02:21:36 -08007710/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007711 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7712 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7713 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007714static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007715{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007716 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007717 grow_ple_window(vcpu);
7718
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007719 /*
7720 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7721 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7722 * never set PAUSE_EXITING and just set PLE if supported,
7723 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7724 */
7725 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007726 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007727}
7728
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007729static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007730{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007731 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007732}
7733
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007734static int handle_mwait(struct kvm_vcpu *vcpu)
7735{
7736 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7737 return handle_nop(vcpu);
7738}
7739
Jim Mattson45ec3682017-08-23 16:32:04 -07007740static int handle_invalid_op(struct kvm_vcpu *vcpu)
7741{
7742 kvm_queue_exception(vcpu, UD_VECTOR);
7743 return 1;
7744}
7745
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007746static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7747{
7748 return 1;
7749}
7750
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007751static int handle_monitor(struct kvm_vcpu *vcpu)
7752{
7753 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7754 return handle_nop(vcpu);
7755}
7756
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007757/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007758 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7759 * set the success or error code of an emulated VMX instruction, as specified
7760 * by Vol 2B, VMX Instruction Reference, "Conventions".
7761 */
7762static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7763{
7764 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7765 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7766 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7767}
7768
7769static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7770{
7771 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7772 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7773 X86_EFLAGS_SF | X86_EFLAGS_OF))
7774 | X86_EFLAGS_CF);
7775}
7776
Abel Gordon145c28d2013-04-18 14:36:55 +03007777static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007778 u32 vm_instruction_error)
7779{
7780 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7781 /*
7782 * failValid writes the error number to the current VMCS, which
7783 * can't be done there isn't a current VMCS.
7784 */
7785 nested_vmx_failInvalid(vcpu);
7786 return;
7787 }
7788 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7789 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7790 X86_EFLAGS_SF | X86_EFLAGS_OF))
7791 | X86_EFLAGS_ZF);
7792 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7793 /*
7794 * We don't need to force a shadow sync because
7795 * VM_INSTRUCTION_ERROR is not shadowed
7796 */
7797}
Abel Gordon145c28d2013-04-18 14:36:55 +03007798
Wincy Vanff651cb2014-12-11 08:52:58 +03007799static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7800{
7801 /* TODO: not to reset guest simply here. */
7802 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007803 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007804}
7805
Jan Kiszkaf4124502014-03-07 20:03:13 +01007806static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7807{
7808 struct vcpu_vmx *vmx =
7809 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7810
7811 vmx->nested.preemption_timer_expired = true;
7812 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7813 kvm_vcpu_kick(&vmx->vcpu);
7814
7815 return HRTIMER_NORESTART;
7816}
7817
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007818/*
Bandan Das19677e32014-05-06 02:19:15 -04007819 * Decode the memory-address operand of a vmx instruction, as recorded on an
7820 * exit caused by such an instruction (run by a guest hypervisor).
7821 * On success, returns 0. When the operand is invalid, returns 1 and throws
7822 * #UD or #GP.
7823 */
7824static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7825 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007826 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007827{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007828 gva_t off;
7829 bool exn;
7830 struct kvm_segment s;
7831
Bandan Das19677e32014-05-06 02:19:15 -04007832 /*
7833 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7834 * Execution", on an exit, vmx_instruction_info holds most of the
7835 * addressing components of the operand. Only the displacement part
7836 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7837 * For how an actual address is calculated from all these components,
7838 * refer to Vol. 1, "Operand Addressing".
7839 */
7840 int scaling = vmx_instruction_info & 3;
7841 int addr_size = (vmx_instruction_info >> 7) & 7;
7842 bool is_reg = vmx_instruction_info & (1u << 10);
7843 int seg_reg = (vmx_instruction_info >> 15) & 7;
7844 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7845 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7846 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7847 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7848
7849 if (is_reg) {
7850 kvm_queue_exception(vcpu, UD_VECTOR);
7851 return 1;
7852 }
7853
7854 /* Addr = segment_base + offset */
7855 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007856 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007857 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007858 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007859 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007860 off += kvm_register_read(vcpu, index_reg)<<scaling;
7861 vmx_get_segment(vcpu, &s, seg_reg);
7862 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007863
7864 if (addr_size == 1) /* 32 bit */
7865 *ret &= 0xffffffff;
7866
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007867 /* Checks for #GP/#SS exceptions. */
7868 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007869 if (is_long_mode(vcpu)) {
7870 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7871 * non-canonical form. This is the only check on the memory
7872 * destination for long mode!
7873 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007874 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007875 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007876 /* Protected mode: apply checks for segment validity in the
7877 * following order:
7878 * - segment type check (#GP(0) may be thrown)
7879 * - usability check (#GP(0)/#SS(0))
7880 * - limit check (#GP(0)/#SS(0))
7881 */
7882 if (wr)
7883 /* #GP(0) if the destination operand is located in a
7884 * read-only data segment or any code segment.
7885 */
7886 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7887 else
7888 /* #GP(0) if the source operand is located in an
7889 * execute-only code segment
7890 */
7891 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007892 if (exn) {
7893 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7894 return 1;
7895 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007896 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7897 */
7898 exn = (s.unusable != 0);
7899 /* Protected mode: #GP(0)/#SS(0) if the memory
7900 * operand is outside the segment limit.
7901 */
7902 exn = exn || (off + sizeof(u64) > s.limit);
7903 }
7904 if (exn) {
7905 kvm_queue_exception_e(vcpu,
7906 seg_reg == VCPU_SREG_SS ?
7907 SS_VECTOR : GP_VECTOR,
7908 0);
7909 return 1;
7910 }
7911
Bandan Das19677e32014-05-06 02:19:15 -04007912 return 0;
7913}
7914
Radim Krčmářcbf71272017-05-19 15:48:51 +02007915static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007916{
7917 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007918 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007919
7920 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007921 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007922 return 1;
7923
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007924 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007925 kvm_inject_page_fault(vcpu, &e);
7926 return 1;
7927 }
7928
Bandan Das3573e222014-05-06 02:19:16 -04007929 return 0;
7930}
7931
Liran Alonabfc52c2018-06-23 02:35:13 +03007932/*
7933 * Allocate a shadow VMCS and associate it with the currently loaded
7934 * VMCS, unless such a shadow VMCS already exists. The newly allocated
7935 * VMCS is also VMCLEARed, so that it is ready for use.
7936 */
7937static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
7938{
7939 struct vcpu_vmx *vmx = to_vmx(vcpu);
7940 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
7941
7942 /*
7943 * We should allocate a shadow vmcs for vmcs01 only when L1
7944 * executes VMXON and free it when L1 executes VMXOFF.
7945 * As it is invalid to execute VMXON twice, we shouldn't reach
7946 * here when vmcs01 already have an allocated shadow vmcs.
7947 */
7948 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
7949
7950 if (!loaded_vmcs->shadow_vmcs) {
7951 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
7952 if (loaded_vmcs->shadow_vmcs)
7953 vmcs_clear(loaded_vmcs->shadow_vmcs);
7954 }
7955 return loaded_vmcs->shadow_vmcs;
7956}
7957
Jim Mattsone29acc52016-11-30 12:03:43 -08007958static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7959{
7960 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007961 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007962
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007963 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7964 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007965 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007966
7967 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7968 if (!vmx->nested.cached_vmcs12)
7969 goto out_cached_vmcs12;
7970
Liran Alon61ada742018-06-23 02:35:08 +03007971 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7972 if (!vmx->nested.cached_shadow_vmcs12)
7973 goto out_cached_shadow_vmcs12;
7974
Liran Alonabfc52c2018-06-23 02:35:13 +03007975 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
7976 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08007977
Jim Mattsone29acc52016-11-30 12:03:43 -08007978 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7979 HRTIMER_MODE_REL_PINNED);
7980 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7981
7982 vmx->nested.vmxon = true;
7983 return 0;
7984
7985out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03007986 kfree(vmx->nested.cached_shadow_vmcs12);
7987
7988out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08007989 kfree(vmx->nested.cached_vmcs12);
7990
7991out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007992 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007993
Jim Mattsonde3a0022017-11-27 17:22:25 -06007994out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007995 return -ENOMEM;
7996}
7997
Bandan Das3573e222014-05-06 02:19:16 -04007998/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007999 * Emulate the VMXON instruction.
8000 * Currently, we just remember that VMX is active, and do not save or even
8001 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8002 * do not currently need to store anything in that guest-allocated memory
8003 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8004 * argument is different from the VMXON pointer (which the spec says they do).
8005 */
8006static int handle_vmon(struct kvm_vcpu *vcpu)
8007{
Jim Mattsone29acc52016-11-30 12:03:43 -08008008 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008009 gpa_t vmptr;
8010 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008011 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008012 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8013 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008014
Jim Mattson70f3aac2017-04-26 08:53:46 -07008015 /*
8016 * The Intel VMX Instruction Reference lists a bunch of bits that are
8017 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8018 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8019 * Otherwise, we should fail with #UD. But most faulting conditions
8020 * have already been checked by hardware, prior to the VM-exit for
8021 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8022 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008023 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008024 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008025 kvm_queue_exception(vcpu, UD_VECTOR);
8026 return 1;
8027 }
8028
Felix Wilhelm727ba742018-06-11 09:43:44 +02008029 /* CPL=0 must be checked manually. */
8030 if (vmx_get_cpl(vcpu)) {
8031 kvm_queue_exception(vcpu, UD_VECTOR);
8032 return 1;
8033 }
8034
Abel Gordon145c28d2013-04-18 14:36:55 +03008035 if (vmx->nested.vmxon) {
8036 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008037 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008038 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008039
Haozhong Zhang3b840802016-06-22 14:59:54 +08008040 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008041 != VMXON_NEEDED_FEATURES) {
8042 kvm_inject_gp(vcpu, 0);
8043 return 1;
8044 }
8045
Radim Krčmářcbf71272017-05-19 15:48:51 +02008046 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008047 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008048
8049 /*
8050 * SDM 3: 24.11.5
8051 * The first 4 bytes of VMXON region contain the supported
8052 * VMCS revision identifier
8053 *
8054 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8055 * which replaces physical address width with 32
8056 */
8057 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8058 nested_vmx_failInvalid(vcpu);
8059 return kvm_skip_emulated_instruction(vcpu);
8060 }
8061
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008062 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8063 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008064 nested_vmx_failInvalid(vcpu);
8065 return kvm_skip_emulated_instruction(vcpu);
8066 }
8067 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8068 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008069 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008070 nested_vmx_failInvalid(vcpu);
8071 return kvm_skip_emulated_instruction(vcpu);
8072 }
8073 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008074 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008075
8076 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008077 ret = enter_vmx_operation(vcpu);
8078 if (ret)
8079 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008080
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008081 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008082 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008083}
8084
8085/*
8086 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8087 * for running VMX instructions (except VMXON, whose prerequisites are
8088 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008089 * Note that many of these exceptions have priority over VM exits, so they
8090 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008091 */
8092static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8093{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008094 if (vmx_get_cpl(vcpu)) {
8095 kvm_queue_exception(vcpu, UD_VECTOR);
8096 return 0;
8097 }
8098
Jim Mattson70f3aac2017-04-26 08:53:46 -07008099 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008100 kvm_queue_exception(vcpu, UD_VECTOR);
8101 return 0;
8102 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008103 return 1;
8104}
8105
David Matlack8ca44e82017-08-01 14:00:39 -07008106static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8107{
8108 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8109 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8110}
8111
Abel Gordone7953d72013-04-18 14:37:55 +03008112static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8113{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008114 if (vmx->nested.current_vmptr == -1ull)
8115 return;
8116
Abel Gordon012f83c2013-04-18 14:39:25 +03008117 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008118 /* copy to memory all shadowed fields in case
8119 they were modified */
8120 copy_shadow_to_vmcs12(vmx);
8121 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008122 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008123 }
Wincy Van705699a2015-02-03 23:58:17 +08008124 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008125
8126 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008127 kvm_vcpu_write_guest_page(&vmx->vcpu,
8128 vmx->nested.current_vmptr >> PAGE_SHIFT,
8129 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008130
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008131 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008132}
8133
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008134/*
8135 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8136 * just stops using VMX.
8137 */
8138static void free_nested(struct vcpu_vmx *vmx)
8139{
Wanpeng Lib7455822017-11-22 14:04:00 -08008140 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008141 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008142
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008143 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008144 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008145 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008146 vmx->nested.posted_intr_nv = -1;
8147 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008148 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008149 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008150 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8151 free_vmcs(vmx->vmcs01.shadow_vmcs);
8152 vmx->vmcs01.shadow_vmcs = NULL;
8153 }
David Matlack4f2777b2016-07-13 17:16:37 -07008154 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008155 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008156 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008157 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008158 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008159 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008160 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008161 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008162 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008163 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008164 }
Wincy Van705699a2015-02-03 23:58:17 +08008165 if (vmx->nested.pi_desc_page) {
8166 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008167 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008168 vmx->nested.pi_desc_page = NULL;
8169 vmx->nested.pi_desc = NULL;
8170 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008171
Jim Mattsonde3a0022017-11-27 17:22:25 -06008172 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008173}
8174
8175/* Emulate the VMXOFF instruction */
8176static int handle_vmoff(struct kvm_vcpu *vcpu)
8177{
8178 if (!nested_vmx_check_permission(vcpu))
8179 return 1;
8180 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008181 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008182 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008183}
8184
Nadav Har'El27d6c862011-05-25 23:06:59 +03008185/* Emulate the VMCLEAR instruction */
8186static int handle_vmclear(struct kvm_vcpu *vcpu)
8187{
8188 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008189 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008190 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008191
8192 if (!nested_vmx_check_permission(vcpu))
8193 return 1;
8194
Radim Krčmářcbf71272017-05-19 15:48:51 +02008195 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008196 return 1;
8197
Radim Krčmářcbf71272017-05-19 15:48:51 +02008198 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8199 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8200 return kvm_skip_emulated_instruction(vcpu);
8201 }
8202
8203 if (vmptr == vmx->nested.vmxon_ptr) {
8204 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8205 return kvm_skip_emulated_instruction(vcpu);
8206 }
8207
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008208 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008209 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008210
Jim Mattson587d7e722017-03-02 12:41:48 -08008211 kvm_vcpu_write_guest(vcpu,
8212 vmptr + offsetof(struct vmcs12, launch_state),
8213 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008214
Nadav Har'El27d6c862011-05-25 23:06:59 +03008215 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008216 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008217}
8218
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008219static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8220
8221/* Emulate the VMLAUNCH instruction */
8222static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8223{
8224 return nested_vmx_run(vcpu, true);
8225}
8226
8227/* Emulate the VMRESUME instruction */
8228static int handle_vmresume(struct kvm_vcpu *vcpu)
8229{
8230
8231 return nested_vmx_run(vcpu, false);
8232}
8233
Nadav Har'El49f705c2011-05-25 23:08:30 +03008234/*
8235 * Read a vmcs12 field. Since these can have varying lengths and we return
8236 * one type, we chose the biggest type (u64) and zero-extend the return value
8237 * to that size. Note that the caller, handle_vmread, might need to use only
8238 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8239 * 64-bit fields are to be returned).
8240 */
Liran Alone2536742018-06-23 02:35:02 +03008241static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008242 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008243{
8244 short offset = vmcs_field_to_offset(field);
8245 char *p;
8246
8247 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008248 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008249
Liran Alone2536742018-06-23 02:35:02 +03008250 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008251
Jim Mattsond37f4262017-12-22 12:12:16 -08008252 switch (vmcs_field_width(field)) {
8253 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008254 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008255 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008256 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008257 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008258 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008259 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008260 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008261 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008262 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008263 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008264 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008265 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008266 WARN_ON(1);
8267 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008268 }
8269}
8270
Abel Gordon20b97fe2013-04-18 14:36:25 +03008271
Liran Alone2536742018-06-23 02:35:02 +03008272static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008273 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008274 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008275 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008276 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008277 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008278
Jim Mattsond37f4262017-12-22 12:12:16 -08008279 switch (vmcs_field_width(field)) {
8280 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008281 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008282 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008283 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008284 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008285 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008286 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008287 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008288 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008289 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008290 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008291 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008292 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008293 WARN_ON(1);
8294 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008295 }
8296
8297}
8298
Jim Mattsonf4160e42018-05-29 09:11:33 -07008299/*
8300 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8301 * they have been modified by the L1 guest. Note that the "read-only"
8302 * VM-exit information fields are actually writable if the vCPU is
8303 * configured to support "VMWRITE to any supported field in the VMCS."
8304 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008305static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8306{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008307 const u16 *fields[] = {
8308 shadow_read_write_fields,
8309 shadow_read_only_fields
8310 };
8311 const int max_fields[] = {
8312 max_shadow_read_write_fields,
8313 max_shadow_read_only_fields
8314 };
8315 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008316 unsigned long field;
8317 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008318 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008319
Jan Kiszka282da872014-10-08 18:05:39 +02008320 preempt_disable();
8321
Abel Gordon16f5b902013-04-18 14:38:25 +03008322 vmcs_load(shadow_vmcs);
8323
Jim Mattsonf4160e42018-05-29 09:11:33 -07008324 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8325 for (i = 0; i < max_fields[q]; i++) {
8326 field = fields[q][i];
8327 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008328 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008329 }
8330 /*
8331 * Skip the VM-exit information fields if they are read-only.
8332 */
8333 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8334 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008335 }
8336
8337 vmcs_clear(shadow_vmcs);
8338 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008339
8340 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008341}
8342
Abel Gordonc3114422013-04-18 14:38:55 +03008343static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8344{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008345 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008346 shadow_read_write_fields,
8347 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008348 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008349 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008350 max_shadow_read_write_fields,
8351 max_shadow_read_only_fields
8352 };
8353 int i, q;
8354 unsigned long field;
8355 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008356 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008357
8358 vmcs_load(shadow_vmcs);
8359
Mathias Krausec2bae892013-06-26 20:36:21 +02008360 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008361 for (i = 0; i < max_fields[q]; i++) {
8362 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008363 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008364 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008365 }
8366 }
8367
8368 vmcs_clear(shadow_vmcs);
8369 vmcs_load(vmx->loaded_vmcs->vmcs);
8370}
8371
Nadav Har'El49f705c2011-05-25 23:08:30 +03008372/*
8373 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8374 * used before) all generate the same failure when it is missing.
8375 */
8376static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8377{
8378 struct vcpu_vmx *vmx = to_vmx(vcpu);
8379 if (vmx->nested.current_vmptr == -1ull) {
8380 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008381 return 0;
8382 }
8383 return 1;
8384}
8385
8386static int handle_vmread(struct kvm_vcpu *vcpu)
8387{
8388 unsigned long field;
8389 u64 field_value;
8390 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8391 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8392 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008393 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008394
Kyle Hueyeb277562016-11-29 12:40:39 -08008395 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008396 return 1;
8397
Kyle Huey6affcbe2016-11-29 12:40:40 -08008398 if (!nested_vmx_check_vmcs12(vcpu))
8399 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008400
Liran Alon6d894f42018-06-23 02:35:09 +03008401 if (!is_guest_mode(vcpu))
8402 vmcs12 = get_vmcs12(vcpu);
8403 else {
8404 /*
8405 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8406 * to shadowed-field sets the ALU flags for VMfailInvalid.
8407 */
8408 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8409 nested_vmx_failInvalid(vcpu);
8410 return kvm_skip_emulated_instruction(vcpu);
8411 }
8412 vmcs12 = get_shadow_vmcs12(vcpu);
8413 }
8414
Nadav Har'El49f705c2011-05-25 23:08:30 +03008415 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008416 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008417 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008418 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008419 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008420 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008421 }
8422 /*
8423 * Now copy part of this value to register or memory, as requested.
8424 * Note that the number of bits actually copied is 32 or 64 depending
8425 * on the guest's mode (32 or 64 bit), not on the given field's length.
8426 */
8427 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008428 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008429 field_value);
8430 } else {
8431 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008432 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008433 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008434 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008435 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8436 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008437 }
8438
8439 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008440 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441}
8442
8443
8444static int handle_vmwrite(struct kvm_vcpu *vcpu)
8445{
8446 unsigned long field;
8447 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008448 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008449 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8450 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008451
Nadav Har'El49f705c2011-05-25 23:08:30 +03008452 /* The value to write might be 32 or 64 bits, depending on L1's long
8453 * mode, and eventually we need to write that into a field of several
8454 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008455 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008456 * bits into the vmcs12 field.
8457 */
8458 u64 field_value = 0;
8459 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008460 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008461
Kyle Hueyeb277562016-11-29 12:40:39 -08008462 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008463 return 1;
8464
Kyle Huey6affcbe2016-11-29 12:40:40 -08008465 if (!nested_vmx_check_vmcs12(vcpu))
8466 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008467
Nadav Har'El49f705c2011-05-25 23:08:30 +03008468 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008469 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008470 (((vmx_instruction_info) >> 3) & 0xf));
8471 else {
8472 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008473 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008474 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008475 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8476 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008477 kvm_inject_page_fault(vcpu, &e);
8478 return 1;
8479 }
8480 }
8481
8482
Nadav Amit27e6fb52014-06-18 17:19:26 +03008483 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008484 /*
8485 * If the vCPU supports "VMWRITE to any supported field in the
8486 * VMCS," then the "read-only" fields are actually read/write.
8487 */
8488 if (vmcs_field_readonly(field) &&
8489 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008490 nested_vmx_failValid(vcpu,
8491 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008492 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008493 }
8494
Liran Alon6d894f42018-06-23 02:35:09 +03008495 if (!is_guest_mode(vcpu))
8496 vmcs12 = get_vmcs12(vcpu);
8497 else {
8498 /*
8499 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8500 * to shadowed-field sets the ALU flags for VMfailInvalid.
8501 */
8502 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8503 nested_vmx_failInvalid(vcpu);
8504 return kvm_skip_emulated_instruction(vcpu);
8505 }
8506 vmcs12 = get_shadow_vmcs12(vcpu);
8507
8508 }
8509
8510 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008511 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008512 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008513 }
8514
Liran Alon6d894f42018-06-23 02:35:09 +03008515 /*
8516 * Do not track vmcs12 dirty-state if in guest-mode
8517 * as we actually dirty shadow vmcs12 instead of vmcs12.
8518 */
8519 if (!is_guest_mode(vcpu)) {
8520 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008521#define SHADOW_FIELD_RW(x) case x:
8522#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008523 /*
8524 * The fields that can be updated by L1 without a vmexit are
8525 * always updated in the vmcs02, the others go down the slow
8526 * path of prepare_vmcs02.
8527 */
8528 break;
8529 default:
8530 vmx->nested.dirty_vmcs12 = true;
8531 break;
8532 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008533 }
8534
Nadav Har'El49f705c2011-05-25 23:08:30 +03008535 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008536 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008537}
8538
Jim Mattsona8bc2842016-11-30 12:03:44 -08008539static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8540{
8541 vmx->nested.current_vmptr = vmptr;
8542 if (enable_shadow_vmcs) {
8543 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8544 SECONDARY_EXEC_SHADOW_VMCS);
8545 vmcs_write64(VMCS_LINK_POINTER,
8546 __pa(vmx->vmcs01.shadow_vmcs));
8547 vmx->nested.sync_shadow_vmcs = true;
8548 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008549 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008550}
8551
Nadav Har'El63846662011-05-25 23:07:29 +03008552/* Emulate the VMPTRLD instruction */
8553static int handle_vmptrld(struct kvm_vcpu *vcpu)
8554{
8555 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008556 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008557
8558 if (!nested_vmx_check_permission(vcpu))
8559 return 1;
8560
Radim Krčmářcbf71272017-05-19 15:48:51 +02008561 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008562 return 1;
8563
Radim Krčmářcbf71272017-05-19 15:48:51 +02008564 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8565 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8566 return kvm_skip_emulated_instruction(vcpu);
8567 }
8568
8569 if (vmptr == vmx->nested.vmxon_ptr) {
8570 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8571 return kvm_skip_emulated_instruction(vcpu);
8572 }
8573
Nadav Har'El63846662011-05-25 23:07:29 +03008574 if (vmx->nested.current_vmptr != vmptr) {
8575 struct vmcs12 *new_vmcs12;
8576 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008577 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8578 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008579 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008580 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008581 }
8582 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008583 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008584 (new_vmcs12->hdr.shadow_vmcs &&
8585 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008586 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008587 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008588 nested_vmx_failValid(vcpu,
8589 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008590 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008591 }
Nadav Har'El63846662011-05-25 23:07:29 +03008592
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008593 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008594 /*
8595 * Load VMCS12 from guest memory since it is not already
8596 * cached.
8597 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008598 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8599 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008600 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008601
Jim Mattsona8bc2842016-11-30 12:03:44 -08008602 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008603 }
8604
8605 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008606 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008607}
8608
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008609/* Emulate the VMPTRST instruction */
8610static int handle_vmptrst(struct kvm_vcpu *vcpu)
8611{
8612 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8613 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8614 gva_t vmcs_gva;
8615 struct x86_exception e;
8616
8617 if (!nested_vmx_check_permission(vcpu))
8618 return 1;
8619
8620 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008621 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008622 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008623 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008624 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8625 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8626 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008627 kvm_inject_page_fault(vcpu, &e);
8628 return 1;
8629 }
8630 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008631 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008632}
8633
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008634/* Emulate the INVEPT instruction */
8635static int handle_invept(struct kvm_vcpu *vcpu)
8636{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008637 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008638 u32 vmx_instruction_info, types;
8639 unsigned long type;
8640 gva_t gva;
8641 struct x86_exception e;
8642 struct {
8643 u64 eptp, gpa;
8644 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008645
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008646 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008647 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008648 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008649 kvm_queue_exception(vcpu, UD_VECTOR);
8650 return 1;
8651 }
8652
8653 if (!nested_vmx_check_permission(vcpu))
8654 return 1;
8655
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008656 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008657 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008658
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008659 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008660
Jim Mattson85c856b2016-10-26 08:38:38 -07008661 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008662 nested_vmx_failValid(vcpu,
8663 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008664 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008665 }
8666
8667 /* According to the Intel VMX instruction reference, the memory
8668 * operand is read even if it isn't needed (e.g., for type==global)
8669 */
8670 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008671 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008672 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008673 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008674 kvm_inject_page_fault(vcpu, &e);
8675 return 1;
8676 }
8677
8678 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008679 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008680 /*
8681 * TODO: track mappings and invalidate
8682 * single context requests appropriately
8683 */
8684 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008685 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008686 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008687 nested_vmx_succeed(vcpu);
8688 break;
8689 default:
8690 BUG_ON(1);
8691 break;
8692 }
8693
Kyle Huey6affcbe2016-11-29 12:40:40 -08008694 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008695}
8696
Petr Matouseka642fc32014-09-23 20:22:30 +02008697static int handle_invvpid(struct kvm_vcpu *vcpu)
8698{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008699 struct vcpu_vmx *vmx = to_vmx(vcpu);
8700 u32 vmx_instruction_info;
8701 unsigned long type, types;
8702 gva_t gva;
8703 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008704 struct {
8705 u64 vpid;
8706 u64 gla;
8707 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008708
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008709 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008710 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008711 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008712 kvm_queue_exception(vcpu, UD_VECTOR);
8713 return 1;
8714 }
8715
8716 if (!nested_vmx_check_permission(vcpu))
8717 return 1;
8718
8719 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8720 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8721
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008722 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008723 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008724
Jim Mattson85c856b2016-10-26 08:38:38 -07008725 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008726 nested_vmx_failValid(vcpu,
8727 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008728 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008729 }
8730
8731 /* according to the intel vmx instruction reference, the memory
8732 * operand is read even if it isn't needed (e.g., for type==global)
8733 */
8734 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8735 vmx_instruction_info, false, &gva))
8736 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008737 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008738 kvm_inject_page_fault(vcpu, &e);
8739 return 1;
8740 }
Jim Mattson40352602017-06-28 09:37:37 -07008741 if (operand.vpid >> 16) {
8742 nested_vmx_failValid(vcpu,
8743 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8744 return kvm_skip_emulated_instruction(vcpu);
8745 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008746
8747 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008748 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008749 if (!operand.vpid ||
8750 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008751 nested_vmx_failValid(vcpu,
8752 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8753 return kvm_skip_emulated_instruction(vcpu);
8754 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008755 if (cpu_has_vmx_invvpid_individual_addr() &&
8756 vmx->nested.vpid02) {
8757 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8758 vmx->nested.vpid02, operand.gla);
8759 } else
8760 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8761 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008762 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008763 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008764 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008765 nested_vmx_failValid(vcpu,
8766 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008767 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008768 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008769 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008770 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008771 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008772 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008773 break;
8774 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008775 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008776 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008777 }
8778
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008779 nested_vmx_succeed(vcpu);
8780
Kyle Huey6affcbe2016-11-29 12:40:40 -08008781 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008782}
8783
Junaid Shahideb4b2482018-06-27 14:59:14 -07008784static int handle_invpcid(struct kvm_vcpu *vcpu)
8785{
8786 u32 vmx_instruction_info;
8787 unsigned long type;
8788 bool pcid_enabled;
8789 gva_t gva;
8790 struct x86_exception e;
8791 struct {
8792 u64 pcid;
8793 u64 gla;
8794 } operand;
8795
8796 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8797 kvm_queue_exception(vcpu, UD_VECTOR);
8798 return 1;
8799 }
8800
8801 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8802 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8803
8804 if (type > 3) {
8805 kvm_inject_gp(vcpu, 0);
8806 return 1;
8807 }
8808
8809 /* According to the Intel instruction reference, the memory operand
8810 * is read even if it isn't needed (e.g., for type==all)
8811 */
8812 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8813 vmx_instruction_info, false, &gva))
8814 return 1;
8815
8816 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8817 kvm_inject_page_fault(vcpu, &e);
8818 return 1;
8819 }
8820
8821 if (operand.pcid >> 12 != 0) {
8822 kvm_inject_gp(vcpu, 0);
8823 return 1;
8824 }
8825
8826 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8827
8828 switch (type) {
8829 case INVPCID_TYPE_INDIV_ADDR:
8830 if ((!pcid_enabled && (operand.pcid != 0)) ||
8831 is_noncanonical_address(operand.gla, vcpu)) {
8832 kvm_inject_gp(vcpu, 0);
8833 return 1;
8834 }
8835 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8836 return kvm_skip_emulated_instruction(vcpu);
8837
8838 case INVPCID_TYPE_SINGLE_CTXT:
8839 if (!pcid_enabled && (operand.pcid != 0)) {
8840 kvm_inject_gp(vcpu, 0);
8841 return 1;
8842 }
8843
8844 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8845 kvm_mmu_sync_roots(vcpu);
8846 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8847 }
8848
Junaid Shahidade61e22018-06-27 14:59:15 -07008849 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_root.cr3)
8850 == operand.pcid)
Junaid Shahid956bf352018-06-27 14:59:18 -07008851 kvm_mmu_free_roots(vcpu, KVM_MMU_ROOT_PREVIOUS);
Junaid Shahidade61e22018-06-27 14:59:15 -07008852
Junaid Shahideb4b2482018-06-27 14:59:14 -07008853 /*
Junaid Shahidade61e22018-06-27 14:59:15 -07008854 * If neither the current cr3 nor the prev_root.cr3 use the
8855 * given PCID, then nothing needs to be done here because a
8856 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008857 */
8858
8859 return kvm_skip_emulated_instruction(vcpu);
8860
8861 case INVPCID_TYPE_ALL_NON_GLOBAL:
8862 /*
8863 * Currently, KVM doesn't mark global entries in the shadow
8864 * page tables, so a non-global flush just degenerates to a
8865 * global flush. If needed, we could optimize this later by
8866 * keeping track of global entries in shadow page tables.
8867 */
8868
8869 /* fall-through */
8870 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8871 kvm_mmu_unload(vcpu);
8872 return kvm_skip_emulated_instruction(vcpu);
8873
8874 default:
8875 BUG(); /* We have already checked above that type <= 3 */
8876 }
8877}
8878
Kai Huang843e4332015-01-28 10:54:28 +08008879static int handle_pml_full(struct kvm_vcpu *vcpu)
8880{
8881 unsigned long exit_qualification;
8882
8883 trace_kvm_pml_full(vcpu->vcpu_id);
8884
8885 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8886
8887 /*
8888 * PML buffer FULL happened while executing iret from NMI,
8889 * "blocked by NMI" bit has to be set before next VM entry.
8890 */
8891 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008892 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008893 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8894 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8895 GUEST_INTR_STATE_NMI);
8896
8897 /*
8898 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8899 * here.., and there's no userspace involvement needed for PML.
8900 */
8901 return 1;
8902}
8903
Yunhong Jiang64672c92016-06-13 14:19:59 -07008904static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8905{
8906 kvm_lapic_expired_hv_timer(vcpu);
8907 return 1;
8908}
8909
Bandan Das41ab9372017-08-03 15:54:43 -04008910static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8911{
8912 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008913 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8914
8915 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008916 switch (address & VMX_EPTP_MT_MASK) {
8917 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008918 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008919 return false;
8920 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008921 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008922 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008923 return false;
8924 break;
8925 default:
8926 return false;
8927 }
8928
David Hildenbrandbb97a012017-08-10 23:15:28 +02008929 /* only 4 levels page-walk length are valid */
8930 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008931 return false;
8932
8933 /* Reserved bits should not be set */
8934 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8935 return false;
8936
8937 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008938 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008939 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008940 return false;
8941 }
8942
8943 return true;
8944}
8945
8946static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8947 struct vmcs12 *vmcs12)
8948{
8949 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8950 u64 address;
8951 bool accessed_dirty;
8952 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8953
8954 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8955 !nested_cpu_has_ept(vmcs12))
8956 return 1;
8957
8958 if (index >= VMFUNC_EPTP_ENTRIES)
8959 return 1;
8960
8961
8962 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8963 &address, index * 8, 8))
8964 return 1;
8965
David Hildenbrandbb97a012017-08-10 23:15:28 +02008966 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008967
8968 /*
8969 * If the (L2) guest does a vmfunc to the currently
8970 * active ept pointer, we don't have to do anything else
8971 */
8972 if (vmcs12->ept_pointer != address) {
8973 if (!valid_ept_address(vcpu, address))
8974 return 1;
8975
8976 kvm_mmu_unload(vcpu);
8977 mmu->ept_ad = accessed_dirty;
8978 mmu->base_role.ad_disabled = !accessed_dirty;
8979 vmcs12->ept_pointer = address;
8980 /*
8981 * TODO: Check what's the correct approach in case
8982 * mmu reload fails. Currently, we just let the next
8983 * reload potentially fail
8984 */
8985 kvm_mmu_reload(vcpu);
8986 }
8987
8988 return 0;
8989}
8990
Bandan Das2a499e42017-08-03 15:54:41 -04008991static int handle_vmfunc(struct kvm_vcpu *vcpu)
8992{
Bandan Das27c42a12017-08-03 15:54:42 -04008993 struct vcpu_vmx *vmx = to_vmx(vcpu);
8994 struct vmcs12 *vmcs12;
8995 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8996
8997 /*
8998 * VMFUNC is only supported for nested guests, but we always enable the
8999 * secondary control for simplicity; for non-nested mode, fake that we
9000 * didn't by injecting #UD.
9001 */
9002 if (!is_guest_mode(vcpu)) {
9003 kvm_queue_exception(vcpu, UD_VECTOR);
9004 return 1;
9005 }
9006
9007 vmcs12 = get_vmcs12(vcpu);
9008 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9009 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009010
9011 switch (function) {
9012 case 0:
9013 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9014 goto fail;
9015 break;
9016 default:
9017 goto fail;
9018 }
9019 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009020
9021fail:
9022 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9023 vmcs_read32(VM_EXIT_INTR_INFO),
9024 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009025 return 1;
9026}
9027
Nadav Har'El0140cae2011-05-25 23:06:28 +03009028/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009029 * The exit handlers return 1 if the exit was handled fully and guest execution
9030 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9031 * to be done to userspace and return 0.
9032 */
Mathias Krause772e0312012-08-30 01:30:19 +02009033static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009034 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9035 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009036 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009037 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009038 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009039 [EXIT_REASON_CR_ACCESS] = handle_cr,
9040 [EXIT_REASON_DR_ACCESS] = handle_dr,
9041 [EXIT_REASON_CPUID] = handle_cpuid,
9042 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9043 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9044 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9045 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009046 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009047 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009048 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009049 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009050 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009051 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009052 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009053 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009054 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009055 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009056 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009057 [EXIT_REASON_VMOFF] = handle_vmoff,
9058 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009059 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9060 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009061 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009062 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009063 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009064 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009065 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009066 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009067 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9068 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009069 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9070 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009071 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009072 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009073 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009074 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009075 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009076 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009077 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009078 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009079 [EXIT_REASON_XSAVES] = handle_xsaves,
9080 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009081 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009082 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009083 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009084 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009085};
9086
9087static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009088 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009089
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009090static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9091 struct vmcs12 *vmcs12)
9092{
9093 unsigned long exit_qualification;
9094 gpa_t bitmap, last_bitmap;
9095 unsigned int port;
9096 int size;
9097 u8 b;
9098
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009099 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009100 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009101
9102 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9103
9104 port = exit_qualification >> 16;
9105 size = (exit_qualification & 7) + 1;
9106
9107 last_bitmap = (gpa_t)-1;
9108 b = -1;
9109
9110 while (size > 0) {
9111 if (port < 0x8000)
9112 bitmap = vmcs12->io_bitmap_a;
9113 else if (port < 0x10000)
9114 bitmap = vmcs12->io_bitmap_b;
9115 else
Joe Perches1d804d02015-03-30 16:46:09 -07009116 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009117 bitmap += (port & 0x7fff) / 8;
9118
9119 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009120 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009121 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009122 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009123 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009124
9125 port++;
9126 size--;
9127 last_bitmap = bitmap;
9128 }
9129
Joe Perches1d804d02015-03-30 16:46:09 -07009130 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009131}
9132
Nadav Har'El644d7112011-05-25 23:12:35 +03009133/*
9134 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9135 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9136 * disinterest in the current event (read or write a specific MSR) by using an
9137 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9138 */
9139static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9140 struct vmcs12 *vmcs12, u32 exit_reason)
9141{
9142 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9143 gpa_t bitmap;
9144
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009145 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009146 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009147
9148 /*
9149 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9150 * for the four combinations of read/write and low/high MSR numbers.
9151 * First we need to figure out which of the four to use:
9152 */
9153 bitmap = vmcs12->msr_bitmap;
9154 if (exit_reason == EXIT_REASON_MSR_WRITE)
9155 bitmap += 2048;
9156 if (msr_index >= 0xc0000000) {
9157 msr_index -= 0xc0000000;
9158 bitmap += 1024;
9159 }
9160
9161 /* Then read the msr_index'th bit from this bitmap: */
9162 if (msr_index < 1024*8) {
9163 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009164 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009165 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 return 1 & (b >> (msr_index & 7));
9167 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009168 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009169}
9170
9171/*
9172 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9173 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9174 * intercept (via guest_host_mask etc.) the current event.
9175 */
9176static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9177 struct vmcs12 *vmcs12)
9178{
9179 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9180 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009181 int reg;
9182 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009183
9184 switch ((exit_qualification >> 4) & 3) {
9185 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009186 reg = (exit_qualification >> 8) & 15;
9187 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009188 switch (cr) {
9189 case 0:
9190 if (vmcs12->cr0_guest_host_mask &
9191 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009192 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 break;
9194 case 3:
9195 if ((vmcs12->cr3_target_count >= 1 &&
9196 vmcs12->cr3_target_value0 == val) ||
9197 (vmcs12->cr3_target_count >= 2 &&
9198 vmcs12->cr3_target_value1 == val) ||
9199 (vmcs12->cr3_target_count >= 3 &&
9200 vmcs12->cr3_target_value2 == val) ||
9201 (vmcs12->cr3_target_count >= 4 &&
9202 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009203 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009204 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009205 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009206 break;
9207 case 4:
9208 if (vmcs12->cr4_guest_host_mask &
9209 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009210 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009211 break;
9212 case 8:
9213 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009214 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009215 break;
9216 }
9217 break;
9218 case 2: /* clts */
9219 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9220 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009221 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009222 break;
9223 case 1: /* mov from cr */
9224 switch (cr) {
9225 case 3:
9226 if (vmcs12->cpu_based_vm_exec_control &
9227 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009228 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009229 break;
9230 case 8:
9231 if (vmcs12->cpu_based_vm_exec_control &
9232 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009233 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009234 break;
9235 }
9236 break;
9237 case 3: /* lmsw */
9238 /*
9239 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9240 * cr0. Other attempted changes are ignored, with no exit.
9241 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009242 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009243 if (vmcs12->cr0_guest_host_mask & 0xe &
9244 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009245 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009246 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9247 !(vmcs12->cr0_read_shadow & 0x1) &&
9248 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009249 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009250 break;
9251 }
Joe Perches1d804d02015-03-30 16:46:09 -07009252 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009253}
9254
Liran Alona7cde482018-06-23 02:35:10 +03009255static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9256 struct vmcs12 *vmcs12, gpa_t bitmap)
9257{
9258 u32 vmx_instruction_info;
9259 unsigned long field;
9260 u8 b;
9261
9262 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9263 return true;
9264
9265 /* Decode instruction info and find the field to access */
9266 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9267 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9268
9269 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9270 if (field >> 15)
9271 return true;
9272
9273 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9274 return true;
9275
9276 return 1 & (b >> (field & 7));
9277}
9278
Nadav Har'El644d7112011-05-25 23:12:35 +03009279/*
9280 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9281 * should handle it ourselves in L0 (and then continue L2). Only call this
9282 * when in is_guest_mode (L2).
9283 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009284static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009285{
Nadav Har'El644d7112011-05-25 23:12:35 +03009286 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9287 struct vcpu_vmx *vmx = to_vmx(vcpu);
9288 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9289
Jim Mattson4f350c62017-09-14 16:31:44 -07009290 if (vmx->nested.nested_run_pending)
9291 return false;
9292
9293 if (unlikely(vmx->fail)) {
9294 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9295 vmcs_read32(VM_INSTRUCTION_ERROR));
9296 return true;
9297 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009298
David Matlackc9f04402017-08-01 14:00:40 -07009299 /*
9300 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009301 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9302 * Page). The CPU may write to these pages via their host
9303 * physical address while L2 is running, bypassing any
9304 * address-translation-based dirty tracking (e.g. EPT write
9305 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009306 *
9307 * Mark them dirty on every exit from L2 to prevent them from
9308 * getting out of sync with dirty tracking.
9309 */
9310 nested_mark_vmcs12_pages_dirty(vcpu);
9311
Jim Mattson4f350c62017-09-14 16:31:44 -07009312 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9313 vmcs_readl(EXIT_QUALIFICATION),
9314 vmx->idt_vectoring_info,
9315 intr_info,
9316 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9317 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009318
9319 switch (exit_reason) {
9320 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009321 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009322 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009323 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009324 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009325 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009326 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009327 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009328 else if (is_debug(intr_info) &&
9329 vcpu->guest_debug &
9330 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9331 return false;
9332 else if (is_breakpoint(intr_info) &&
9333 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9334 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009335 return vmcs12->exception_bitmap &
9336 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9337 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009338 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009339 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009340 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009341 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009342 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009343 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009344 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009345 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009346 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009347 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009348 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009349 case EXIT_REASON_HLT:
9350 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9351 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009352 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009353 case EXIT_REASON_INVLPG:
9354 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9355 case EXIT_REASON_RDPMC:
9356 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009357 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009358 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009359 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009360 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009361 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009362 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009363 case EXIT_REASON_VMREAD:
9364 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9365 vmcs12->vmread_bitmap);
9366 case EXIT_REASON_VMWRITE:
9367 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9368 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009369 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9370 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009371 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009372 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009373 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009374 /*
9375 * VMX instructions trap unconditionally. This allows L1 to
9376 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9377 */
Joe Perches1d804d02015-03-30 16:46:09 -07009378 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009379 case EXIT_REASON_CR_ACCESS:
9380 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9381 case EXIT_REASON_DR_ACCESS:
9382 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9383 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009384 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009385 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9386 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009387 case EXIT_REASON_MSR_READ:
9388 case EXIT_REASON_MSR_WRITE:
9389 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9390 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009391 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009392 case EXIT_REASON_MWAIT_INSTRUCTION:
9393 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009394 case EXIT_REASON_MONITOR_TRAP_FLAG:
9395 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009396 case EXIT_REASON_MONITOR_INSTRUCTION:
9397 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9398 case EXIT_REASON_PAUSE_INSTRUCTION:
9399 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9400 nested_cpu_has2(vmcs12,
9401 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9402 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009403 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009404 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009405 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009406 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009407 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009408 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009409 /*
9410 * The controls for "virtualize APIC accesses," "APIC-
9411 * register virtualization," and "virtual-interrupt
9412 * delivery" only come from vmcs12.
9413 */
Joe Perches1d804d02015-03-30 16:46:09 -07009414 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009415 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009416 /*
9417 * L0 always deals with the EPT violation. If nested EPT is
9418 * used, and the nested mmu code discovers that the address is
9419 * missing in the guest EPT table (EPT12), the EPT violation
9420 * will be injected with nested_ept_inject_page_fault()
9421 */
Joe Perches1d804d02015-03-30 16:46:09 -07009422 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009423 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009424 /*
9425 * L2 never uses directly L1's EPT, but rather L0's own EPT
9426 * table (shadow on EPT) or a merged EPT table that L0 built
9427 * (EPT on EPT). So any problems with the structure of the
9428 * table is L0's fault.
9429 */
Joe Perches1d804d02015-03-30 16:46:09 -07009430 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009431 case EXIT_REASON_INVPCID:
9432 return
9433 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9434 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009435 case EXIT_REASON_WBINVD:
9436 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9437 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009438 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009439 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9440 /*
9441 * This should never happen, since it is not possible to
9442 * set XSS to a non-zero value---neither in L1 nor in L2.
9443 * If if it were, XSS would have to be checked against
9444 * the XSS exit bitmap in vmcs12.
9445 */
9446 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009447 case EXIT_REASON_PREEMPTION_TIMER:
9448 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009449 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009450 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009451 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009452 case EXIT_REASON_VMFUNC:
9453 /* VM functions are emulated through L2->L0 vmexits. */
9454 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009455 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009456 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009457 }
9458}
9459
Paolo Bonzini7313c692017-07-27 10:31:25 +02009460static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9461{
9462 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9463
9464 /*
9465 * At this point, the exit interruption info in exit_intr_info
9466 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9467 * we need to query the in-kernel LAPIC.
9468 */
9469 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9470 if ((exit_intr_info &
9471 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9472 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9473 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9474 vmcs12->vm_exit_intr_error_code =
9475 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9476 }
9477
9478 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9479 vmcs_readl(EXIT_QUALIFICATION));
9480 return 1;
9481}
9482
Avi Kivity586f9602010-11-18 13:09:54 +02009483static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9484{
9485 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9486 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9487}
9488
Kai Huanga3eaa862015-11-04 13:46:05 +08009489static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009490{
Kai Huanga3eaa862015-11-04 13:46:05 +08009491 if (vmx->pml_pg) {
9492 __free_page(vmx->pml_pg);
9493 vmx->pml_pg = NULL;
9494 }
Kai Huang843e4332015-01-28 10:54:28 +08009495}
9496
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009497static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009498{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009499 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009500 u64 *pml_buf;
9501 u16 pml_idx;
9502
9503 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9504
9505 /* Do nothing if PML buffer is empty */
9506 if (pml_idx == (PML_ENTITY_NUM - 1))
9507 return;
9508
9509 /* PML index always points to next available PML buffer entity */
9510 if (pml_idx >= PML_ENTITY_NUM)
9511 pml_idx = 0;
9512 else
9513 pml_idx++;
9514
9515 pml_buf = page_address(vmx->pml_pg);
9516 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9517 u64 gpa;
9518
9519 gpa = pml_buf[pml_idx];
9520 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009521 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009522 }
9523
9524 /* reset PML index */
9525 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9526}
9527
9528/*
9529 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9530 * Called before reporting dirty_bitmap to userspace.
9531 */
9532static void kvm_flush_pml_buffers(struct kvm *kvm)
9533{
9534 int i;
9535 struct kvm_vcpu *vcpu;
9536 /*
9537 * We only need to kick vcpu out of guest mode here, as PML buffer
9538 * is flushed at beginning of all VMEXITs, and it's obvious that only
9539 * vcpus running in guest are possible to have unflushed GPAs in PML
9540 * buffer.
9541 */
9542 kvm_for_each_vcpu(i, vcpu, kvm)
9543 kvm_vcpu_kick(vcpu);
9544}
9545
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009546static void vmx_dump_sel(char *name, uint32_t sel)
9547{
9548 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009549 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009550 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9551 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9552 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9553}
9554
9555static void vmx_dump_dtsel(char *name, uint32_t limit)
9556{
9557 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9558 name, vmcs_read32(limit),
9559 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9560}
9561
9562static void dump_vmcs(void)
9563{
9564 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9565 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9566 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9567 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9568 u32 secondary_exec_control = 0;
9569 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009570 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009571 int i, n;
9572
9573 if (cpu_has_secondary_exec_ctrls())
9574 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9575
9576 pr_err("*** Guest State ***\n");
9577 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9578 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9579 vmcs_readl(CR0_GUEST_HOST_MASK));
9580 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9581 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9582 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9583 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9584 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9585 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009586 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9587 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9588 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9589 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009590 }
9591 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9592 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9593 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9594 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9595 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9596 vmcs_readl(GUEST_SYSENTER_ESP),
9597 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9598 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9599 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9600 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9601 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9602 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9603 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9604 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9605 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9606 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9607 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9608 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9609 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009610 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9611 efer, vmcs_read64(GUEST_IA32_PAT));
9612 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9613 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009614 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009615 if (cpu_has_load_perf_global_ctrl &&
9616 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009617 pr_err("PerfGlobCtl = 0x%016llx\n",
9618 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009619 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009620 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009621 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9622 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9623 vmcs_read32(GUEST_ACTIVITY_STATE));
9624 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9625 pr_err("InterruptStatus = %04x\n",
9626 vmcs_read16(GUEST_INTR_STATUS));
9627
9628 pr_err("*** Host State ***\n");
9629 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9630 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9631 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9632 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9633 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9634 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9635 vmcs_read16(HOST_TR_SELECTOR));
9636 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9637 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9638 vmcs_readl(HOST_TR_BASE));
9639 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9640 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9641 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9642 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9643 vmcs_readl(HOST_CR4));
9644 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9645 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9646 vmcs_read32(HOST_IA32_SYSENTER_CS),
9647 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9648 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009649 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9650 vmcs_read64(HOST_IA32_EFER),
9651 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009652 if (cpu_has_load_perf_global_ctrl &&
9653 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009654 pr_err("PerfGlobCtl = 0x%016llx\n",
9655 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009656
9657 pr_err("*** Control State ***\n");
9658 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9659 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9660 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9661 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9662 vmcs_read32(EXCEPTION_BITMAP),
9663 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9664 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9665 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9666 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9667 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9668 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9669 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9670 vmcs_read32(VM_EXIT_INTR_INFO),
9671 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9672 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9673 pr_err(" reason=%08x qualification=%016lx\n",
9674 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9675 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9676 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9677 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009678 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009679 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009680 pr_err("TSC Multiplier = 0x%016llx\n",
9681 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009682 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9683 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9684 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9685 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9686 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009687 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009688 n = vmcs_read32(CR3_TARGET_COUNT);
9689 for (i = 0; i + 1 < n; i += 4)
9690 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9691 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9692 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9693 if (i < n)
9694 pr_err("CR3 target%u=%016lx\n",
9695 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9696 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9697 pr_err("PLE Gap=%08x Window=%08x\n",
9698 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9699 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9700 pr_err("Virtual processor ID = 0x%04x\n",
9701 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9702}
9703
Avi Kivity6aa8b732006-12-10 02:21:36 -08009704/*
9705 * The guest has exited. See if we can fix it or if we need userspace
9706 * assistance.
9707 */
Avi Kivity851ba692009-08-24 11:10:17 +03009708static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009709{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009710 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009711 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009712 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009713
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009714 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9715
Kai Huang843e4332015-01-28 10:54:28 +08009716 /*
9717 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9718 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9719 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9720 * mode as if vcpus is in root mode, the PML buffer must has been
9721 * flushed already.
9722 */
9723 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009724 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009725
Mohammed Gamal80ced182009-09-01 12:48:18 +02009726 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009727 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009728 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009729
Paolo Bonzini7313c692017-07-27 10:31:25 +02009730 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9731 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009732
Mohammed Gamal51207022010-05-31 22:40:54 +03009733 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009734 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009735 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9736 vcpu->run->fail_entry.hardware_entry_failure_reason
9737 = exit_reason;
9738 return 0;
9739 }
9740
Avi Kivity29bd8a72007-09-10 17:27:03 +03009741 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009742 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9743 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009744 = vmcs_read32(VM_INSTRUCTION_ERROR);
9745 return 0;
9746 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009747
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009748 /*
9749 * Note:
9750 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9751 * delivery event since it indicates guest is accessing MMIO.
9752 * The vm-exit can be triggered again after return to guest that
9753 * will cause infinite loop.
9754 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009755 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009756 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009757 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009758 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009759 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9760 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9761 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009762 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009763 vcpu->run->internal.data[0] = vectoring_info;
9764 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009765 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9766 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9767 vcpu->run->internal.ndata++;
9768 vcpu->run->internal.data[3] =
9769 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9770 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009771 return 0;
9772 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009773
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009774 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009775 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9776 if (vmx_interrupt_allowed(vcpu)) {
9777 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9778 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9779 vcpu->arch.nmi_pending) {
9780 /*
9781 * This CPU don't support us in finding the end of an
9782 * NMI-blocked window if the guest runs with IRQs
9783 * disabled. So we pull the trigger after 1 s of
9784 * futile waiting, but inform the user about this.
9785 */
9786 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9787 "state on VCPU %d after 1 s timeout\n",
9788 __func__, vcpu->vcpu_id);
9789 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9790 }
9791 }
9792
Avi Kivity6aa8b732006-12-10 02:21:36 -08009793 if (exit_reason < kvm_vmx_max_exit_handlers
9794 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009795 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009796 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009797 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9798 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009799 kvm_queue_exception(vcpu, UD_VECTOR);
9800 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009801 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009802}
9803
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009804static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009805{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009806 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9807
9808 if (is_guest_mode(vcpu) &&
9809 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9810 return;
9811
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009812 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009813 vmcs_write32(TPR_THRESHOLD, 0);
9814 return;
9815 }
9816
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009817 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009818}
9819
Jim Mattson8d860bb2018-05-09 16:56:05 -04009820static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009821{
9822 u32 sec_exec_control;
9823
Jim Mattson8d860bb2018-05-09 16:56:05 -04009824 if (!lapic_in_kernel(vcpu))
9825 return;
9826
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009827 /* Postpone execution until vmcs01 is the current VMCS. */
9828 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009829 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009830 return;
9831 }
9832
Paolo Bonzini35754c92015-07-29 12:05:37 +02009833 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009834 return;
9835
9836 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009837 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9838 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009839
Jim Mattson8d860bb2018-05-09 16:56:05 -04009840 switch (kvm_get_apic_mode(vcpu)) {
9841 case LAPIC_MODE_INVALID:
9842 WARN_ONCE(true, "Invalid local APIC state");
9843 case LAPIC_MODE_DISABLED:
9844 break;
9845 case LAPIC_MODE_XAPIC:
9846 if (flexpriority_enabled) {
9847 sec_exec_control |=
9848 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9849 vmx_flush_tlb(vcpu, true);
9850 }
9851 break;
9852 case LAPIC_MODE_X2APIC:
9853 if (cpu_has_vmx_virtualize_x2apic_mode())
9854 sec_exec_control |=
9855 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9856 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009857 }
9858 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9859
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009860 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009861}
9862
Tang Chen38b99172014-09-24 15:57:54 +08009863static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9864{
Jim Mattsonab5df312018-05-09 17:02:03 -04009865 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009866 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009867 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009868 }
Tang Chen38b99172014-09-24 15:57:54 +08009869}
9870
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009871static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009872{
9873 u16 status;
9874 u8 old;
9875
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009876 if (max_isr == -1)
9877 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009878
9879 status = vmcs_read16(GUEST_INTR_STATUS);
9880 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009881 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009882 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009883 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009884 vmcs_write16(GUEST_INTR_STATUS, status);
9885 }
9886}
9887
9888static void vmx_set_rvi(int vector)
9889{
9890 u16 status;
9891 u8 old;
9892
Wei Wang4114c272014-11-05 10:53:43 +08009893 if (vector == -1)
9894 vector = 0;
9895
Yang Zhangc7c9c562013-01-25 10:18:51 +08009896 status = vmcs_read16(GUEST_INTR_STATUS);
9897 old = (u8)status & 0xff;
9898 if ((u8)vector != old) {
9899 status &= ~0xff;
9900 status |= (u8)vector;
9901 vmcs_write16(GUEST_INTR_STATUS, status);
9902 }
9903}
9904
9905static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9906{
Liran Alon851c1a182017-12-24 18:12:56 +02009907 /*
9908 * When running L2, updating RVI is only relevant when
9909 * vmcs12 virtual-interrupt-delivery enabled.
9910 * However, it can be enabled only when L1 also
9911 * intercepts external-interrupts and in that case
9912 * we should not update vmcs02 RVI but instead intercept
9913 * interrupt. Therefore, do nothing when running L2.
9914 */
9915 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009916 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009917}
9918
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009919static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009920{
9921 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009922 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009923 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009924
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009925 WARN_ON(!vcpu->arch.apicv_active);
9926 if (pi_test_on(&vmx->pi_desc)) {
9927 pi_clear_on(&vmx->pi_desc);
9928 /*
9929 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9930 * But on x86 this is just a compiler barrier anyway.
9931 */
9932 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009933 max_irr_updated =
9934 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9935
9936 /*
9937 * If we are running L2 and L1 has a new pending interrupt
9938 * which can be injected, we should re-evaluate
9939 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009940 * If L1 intercepts external-interrupts, we should
9941 * exit from L2 to L1. Otherwise, interrupt should be
9942 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009943 */
Liran Alon851c1a182017-12-24 18:12:56 +02009944 if (is_guest_mode(vcpu) && max_irr_updated) {
9945 if (nested_exit_on_intr(vcpu))
9946 kvm_vcpu_exiting_guest_mode(vcpu);
9947 else
9948 kvm_make_request(KVM_REQ_EVENT, vcpu);
9949 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009950 } else {
9951 max_irr = kvm_lapic_find_highest_irr(vcpu);
9952 }
9953 vmx_hwapic_irr_update(vcpu, max_irr);
9954 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009955}
9956
Andrey Smetanin63086302015-11-10 15:36:32 +03009957static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009958{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009959 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009960 return;
9961
Yang Zhangc7c9c562013-01-25 10:18:51 +08009962 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9963 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9964 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9965 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9966}
9967
Paolo Bonzini967235d2016-12-19 14:03:45 +01009968static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9969{
9970 struct vcpu_vmx *vmx = to_vmx(vcpu);
9971
9972 pi_clear_on(&vmx->pi_desc);
9973 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9974}
9975
Avi Kivity51aa01d2010-07-20 14:31:20 +03009976static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009977{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009978 u32 exit_intr_info = 0;
9979 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009980
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009981 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9982 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009983 return;
9984
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009985 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9986 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9987 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009988
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009989 /* if exit due to PF check for async PF */
9990 if (is_page_fault(exit_intr_info))
9991 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9992
Andi Kleena0861c02009-06-08 17:37:09 +08009993 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009994 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9995 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009996 kvm_machine_check();
9997
Gleb Natapov20f65982009-05-11 13:35:55 +03009998 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009999 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010000 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010001 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010002 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010003 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010004}
Gleb Natapov20f65982009-05-11 13:35:55 +030010005
Yang Zhanga547c6d2013-04-11 19:25:10 +080010006static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10007{
10008 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10009
Yang Zhanga547c6d2013-04-11 19:25:10 +080010010 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10011 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10012 unsigned int vector;
10013 unsigned long entry;
10014 gate_desc *desc;
10015 struct vcpu_vmx *vmx = to_vmx(vcpu);
10016#ifdef CONFIG_X86_64
10017 unsigned long tmp;
10018#endif
10019
10020 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10021 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010022 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010023 asm volatile(
10024#ifdef CONFIG_X86_64
10025 "mov %%" _ASM_SP ", %[sp]\n\t"
10026 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10027 "push $%c[ss]\n\t"
10028 "push %[sp]\n\t"
10029#endif
10030 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010031 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010032 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010033 :
10034#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010035 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010036#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010037 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010038 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010039 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010040 [ss]"i"(__KERNEL_DS),
10041 [cs]"i"(__KERNEL_CS)
10042 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010043 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010044}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010045STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010046
Tom Lendackybc226f02018-05-10 22:06:39 +020010047static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010048{
Tom Lendackybc226f02018-05-10 22:06:39 +020010049 switch (index) {
10050 case MSR_IA32_SMBASE:
10051 /*
10052 * We cannot do SMM unless we can run the guest in big
10053 * real mode.
10054 */
10055 return enable_unrestricted_guest || emulate_invalid_guest_state;
10056 case MSR_AMD64_VIRT_SPEC_CTRL:
10057 /* This is AMD only. */
10058 return false;
10059 default:
10060 return true;
10061 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010062}
10063
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010064static bool vmx_mpx_supported(void)
10065{
10066 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10067 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10068}
10069
Wanpeng Li55412b22014-12-02 19:21:30 +080010070static bool vmx_xsaves_supported(void)
10071{
10072 return vmcs_config.cpu_based_2nd_exec_ctrl &
10073 SECONDARY_EXEC_XSAVES;
10074}
10075
Avi Kivity51aa01d2010-07-20 14:31:20 +030010076static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10077{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010078 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010079 bool unblock_nmi;
10080 u8 vector;
10081 bool idtv_info_valid;
10082
10083 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010084
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010085 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010086 if (vmx->loaded_vmcs->nmi_known_unmasked)
10087 return;
10088 /*
10089 * Can't use vmx->exit_intr_info since we're not sure what
10090 * the exit reason is.
10091 */
10092 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10093 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10094 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10095 /*
10096 * SDM 3: 27.7.1.2 (September 2008)
10097 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10098 * a guest IRET fault.
10099 * SDM 3: 23.2.2 (September 2008)
10100 * Bit 12 is undefined in any of the following cases:
10101 * If the VM exit sets the valid bit in the IDT-vectoring
10102 * information field.
10103 * If the VM exit is due to a double fault.
10104 */
10105 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10106 vector != DF_VECTOR && !idtv_info_valid)
10107 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10108 GUEST_INTR_STATE_NMI);
10109 else
10110 vmx->loaded_vmcs->nmi_known_unmasked =
10111 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10112 & GUEST_INTR_STATE_NMI);
10113 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10114 vmx->loaded_vmcs->vnmi_blocked_time +=
10115 ktime_to_ns(ktime_sub(ktime_get(),
10116 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010117}
10118
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010119static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010120 u32 idt_vectoring_info,
10121 int instr_len_field,
10122 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010123{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010124 u8 vector;
10125 int type;
10126 bool idtv_info_valid;
10127
10128 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010129
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010130 vcpu->arch.nmi_injected = false;
10131 kvm_clear_exception_queue(vcpu);
10132 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010133
10134 if (!idtv_info_valid)
10135 return;
10136
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010137 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010138
Avi Kivity668f6122008-07-02 09:28:55 +030010139 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10140 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010141
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010142 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010143 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010144 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010145 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010146 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010147 * Clear bit "block by NMI" before VM entry if a NMI
10148 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010149 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010150 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010151 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010152 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010153 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010154 /* fall through */
10155 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010156 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010157 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010158 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010159 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010160 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010161 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010162 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010163 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010164 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010165 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010166 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010167 break;
10168 default:
10169 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010170 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010171}
10172
Avi Kivity83422e12010-07-20 14:43:23 +030010173static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10174{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010175 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010176 VM_EXIT_INSTRUCTION_LEN,
10177 IDT_VECTORING_ERROR_CODE);
10178}
10179
Avi Kivityb463a6f2010-07-20 15:06:17 +030010180static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10181{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010182 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010183 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10184 VM_ENTRY_INSTRUCTION_LEN,
10185 VM_ENTRY_EXCEPTION_ERROR_CODE);
10186
10187 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10188}
10189
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010190static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10191{
10192 int i, nr_msrs;
10193 struct perf_guest_switch_msr *msrs;
10194
10195 msrs = perf_guest_get_msrs(&nr_msrs);
10196
10197 if (!msrs)
10198 return;
10199
10200 for (i = 0; i < nr_msrs; i++)
10201 if (msrs[i].host == msrs[i].guest)
10202 clear_atomic_switch_msr(vmx, msrs[i].msr);
10203 else
10204 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10205 msrs[i].host);
10206}
10207
Jiang Biao33365e72016-11-03 15:03:37 +080010208static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010209{
10210 struct vcpu_vmx *vmx = to_vmx(vcpu);
10211 u64 tscl;
10212 u32 delta_tsc;
10213
10214 if (vmx->hv_deadline_tsc == -1)
10215 return;
10216
10217 tscl = rdtsc();
10218 if (vmx->hv_deadline_tsc > tscl)
10219 /* sure to be 32 bit only because checked on set_hv_timer */
10220 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10221 cpu_preemption_timer_multi);
10222 else
10223 delta_tsc = 0;
10224
10225 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10226}
10227
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010228static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010229{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010230 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010231 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010232
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010233 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010234 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010235 vmx->loaded_vmcs->soft_vnmi_blocked))
10236 vmx->loaded_vmcs->entry_time = ktime_get();
10237
Avi Kivity104f2262010-11-18 13:12:52 +020010238 /* Don't enter VMX if guest state is invalid, let the exit handler
10239 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010240 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010241 return;
10242
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010243 if (vmx->ple_window_dirty) {
10244 vmx->ple_window_dirty = false;
10245 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10246 }
10247
Abel Gordon012f83c2013-04-18 14:39:25 +030010248 if (vmx->nested.sync_shadow_vmcs) {
10249 copy_vmcs12_to_shadow(vmx);
10250 vmx->nested.sync_shadow_vmcs = false;
10251 }
10252
Avi Kivity104f2262010-11-18 13:12:52 +020010253 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10254 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10255 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10256 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10257
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010258 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010259 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010260 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010261 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010262 }
10263
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010264 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010265 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010266 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010267 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010268 }
10269
Avi Kivity104f2262010-11-18 13:12:52 +020010270 /* When single-stepping over STI and MOV SS, we must clear the
10271 * corresponding interruptibility bits in the guest state. Otherwise
10272 * vmentry fails as it then expects bit 14 (BS) in pending debug
10273 * exceptions being set, but that's not correct for the guest debugging
10274 * case. */
10275 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10276 vmx_set_interrupt_shadow(vcpu, 0);
10277
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010278 if (static_cpu_has(X86_FEATURE_PKU) &&
10279 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10280 vcpu->arch.pkru != vmx->host_pkru)
10281 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010282
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010283 atomic_switch_perf_msrs(vmx);
10284
Yunhong Jiang64672c92016-06-13 14:19:59 -070010285 vmx_arm_hv_timer(vcpu);
10286
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010287 /*
10288 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10289 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10290 * is no need to worry about the conditional branch over the wrmsr
10291 * being speculatively taken.
10292 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010293 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010294
Nadav Har'Eld462b812011-05-24 15:26:10 +030010295 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010296
10297 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10298 (unsigned long)&current_evmcs->host_rsp : 0;
10299
Avi Kivity104f2262010-11-18 13:12:52 +020010300 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010301 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010302 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10303 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10304 "push %%" _ASM_CX " \n\t"
10305 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010306 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010307 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010308 /* Avoid VMWRITE when Enlightened VMCS is in use */
10309 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10310 "jz 2f \n\t"
10311 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10312 "jmp 1f \n\t"
10313 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010314 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010315 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010316 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010317 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10318 "mov %%cr2, %%" _ASM_DX " \n\t"
10319 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010320 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010321 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010322 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010323 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010324 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010325 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010326 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10327 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10328 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10329 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10330 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10331 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010332#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010333 "mov %c[r8](%0), %%r8 \n\t"
10334 "mov %c[r9](%0), %%r9 \n\t"
10335 "mov %c[r10](%0), %%r10 \n\t"
10336 "mov %c[r11](%0), %%r11 \n\t"
10337 "mov %c[r12](%0), %%r12 \n\t"
10338 "mov %c[r13](%0), %%r13 \n\t"
10339 "mov %c[r14](%0), %%r14 \n\t"
10340 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010341#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010342 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010343
Avi Kivity6aa8b732006-12-10 02:21:36 -080010344 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010345 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010346 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010347 "jmp 2f \n\t"
10348 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10349 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010350 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010351 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010352 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010353 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010354 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10355 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10356 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10357 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10358 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10359 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10360 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010361#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010362 "mov %%r8, %c[r8](%0) \n\t"
10363 "mov %%r9, %c[r9](%0) \n\t"
10364 "mov %%r10, %c[r10](%0) \n\t"
10365 "mov %%r11, %c[r11](%0) \n\t"
10366 "mov %%r12, %c[r12](%0) \n\t"
10367 "mov %%r13, %c[r13](%0) \n\t"
10368 "mov %%r14, %c[r14](%0) \n\t"
10369 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010370 "xor %%r8d, %%r8d \n\t"
10371 "xor %%r9d, %%r9d \n\t"
10372 "xor %%r10d, %%r10d \n\t"
10373 "xor %%r11d, %%r11d \n\t"
10374 "xor %%r12d, %%r12d \n\t"
10375 "xor %%r13d, %%r13d \n\t"
10376 "xor %%r14d, %%r14d \n\t"
10377 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010378#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010379 "mov %%cr2, %%" _ASM_AX " \n\t"
10380 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010381
Jim Mattson0cb5b302018-01-03 14:31:38 -080010382 "xor %%eax, %%eax \n\t"
10383 "xor %%ebx, %%ebx \n\t"
10384 "xor %%esi, %%esi \n\t"
10385 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010386 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010387 ".pushsection .rodata \n\t"
10388 ".global vmx_return \n\t"
10389 "vmx_return: " _ASM_PTR " 2b \n\t"
10390 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010391 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010392 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010393 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010394 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010395 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10396 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10397 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10398 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10399 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10400 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10401 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010402#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010403 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10404 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10405 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10406 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10407 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10408 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10409 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10410 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010411#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010412 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10413 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010414 : "cc", "memory"
10415#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010416 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010417 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010418#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010419 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010420#endif
10421 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010422
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010423 /*
10424 * We do not use IBRS in the kernel. If this vCPU has used the
10425 * SPEC_CTRL MSR it may have left it on; save the value and
10426 * turn it off. This is much more efficient than blindly adding
10427 * it to the atomic save/restore list. Especially as the former
10428 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10429 *
10430 * For non-nested case:
10431 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10432 * save it.
10433 *
10434 * For nested case:
10435 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10436 * save it.
10437 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010438 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010439 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010440
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010441 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010442
David Woodhouse117cc7a2018-01-12 11:11:27 +000010443 /* Eliminate branch target predictions from guest mode */
10444 vmexit_fill_RSB();
10445
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010446 /* All fields are clean at this point */
10447 if (static_branch_unlikely(&enable_evmcs))
10448 current_evmcs->hv_clean_fields |=
10449 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10450
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010451 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010452 if (vmx->host_debugctlmsr)
10453 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010454
Avi Kivityaa67f602012-08-01 16:48:03 +030010455#ifndef CONFIG_X86_64
10456 /*
10457 * The sysexit path does not restore ds/es, so we must set them to
10458 * a reasonable value ourselves.
10459 *
10460 * We can't defer this to vmx_load_host_state() since that function
10461 * may be executed in interrupt context, which saves and restore segments
10462 * around it, nullifying its effect.
10463 */
10464 loadsegment(ds, __USER_DS);
10465 loadsegment(es, __USER_DS);
10466#endif
10467
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010468 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010469 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010470 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010471 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010472 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010473 vcpu->arch.regs_dirty = 0;
10474
Gleb Natapove0b890d2013-09-25 12:51:33 +030010475 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010476 * eager fpu is enabled if PKEY is supported and CR4 is switched
10477 * back on host, so it is safe to read guest PKRU from current
10478 * XSAVE.
10479 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010480 if (static_cpu_has(X86_FEATURE_PKU) &&
10481 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10482 vcpu->arch.pkru = __read_pkru();
10483 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010484 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010485 }
10486
Gleb Natapove0b890d2013-09-25 12:51:33 +030010487 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010488 vmx->idt_vectoring_info = 0;
10489
10490 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10491 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10492 return;
10493
10494 vmx->loaded_vmcs->launched = 1;
10495 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010496
Avi Kivity51aa01d2010-07-20 14:31:20 +030010497 vmx_complete_atomic_exit(vmx);
10498 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010499 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010500}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010501STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010502
Sean Christopherson434a1e92018-03-20 12:17:18 -070010503static struct kvm *vmx_vm_alloc(void)
10504{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010505 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010506 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010507}
10508
10509static void vmx_vm_free(struct kvm *kvm)
10510{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010511 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010512}
10513
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010514static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010515{
10516 struct vcpu_vmx *vmx = to_vmx(vcpu);
10517 int cpu;
10518
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010519 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010520 return;
10521
10522 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010523 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010524 vmx_vcpu_put(vcpu);
10525 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010526 put_cpu();
10527}
10528
Jim Mattson2f1fe812016-07-08 15:36:06 -070010529/*
10530 * Ensure that the current vmcs of the logical processor is the
10531 * vmcs01 of the vcpu before calling free_nested().
10532 */
10533static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10534{
10535 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010536
Christoffer Dallec7660c2017-12-04 21:35:23 +010010537 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010538 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010539 free_nested(vmx);
10540 vcpu_put(vcpu);
10541}
10542
Avi Kivity6aa8b732006-12-10 02:21:36 -080010543static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10544{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010545 struct vcpu_vmx *vmx = to_vmx(vcpu);
10546
Kai Huang843e4332015-01-28 10:54:28 +080010547 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010548 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010549 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010550 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010551 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010552 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010553 kfree(vmx->guest_msrs);
10554 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010555 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010556}
10557
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010558static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010559{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010560 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010561 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010562 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010563 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010564
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010565 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010566 return ERR_PTR(-ENOMEM);
10567
Wanpeng Li991e7a02015-09-16 17:30:05 +080010568 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010569
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010570 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10571 if (err)
10572 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010573
Peter Feiner4e595162016-07-07 14:49:58 -070010574 err = -ENOMEM;
10575
10576 /*
10577 * If PML is turned on, failure on enabling PML just results in failure
10578 * of creating the vcpu, therefore we can simplify PML logic (by
10579 * avoiding dealing with cases, such as enabling PML partially on vcpus
10580 * for the guest, etc.
10581 */
10582 if (enable_pml) {
10583 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10584 if (!vmx->pml_pg)
10585 goto uninit_vcpu;
10586 }
10587
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010588 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010589 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10590 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010591
Peter Feiner4e595162016-07-07 14:49:58 -070010592 if (!vmx->guest_msrs)
10593 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010594
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010595 err = alloc_loaded_vmcs(&vmx->vmcs01);
10596 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010597 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010598
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010599 msr_bitmap = vmx->vmcs01.msr_bitmap;
10600 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10601 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10602 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10603 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10604 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10605 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10606 vmx->msr_bitmap_mode = 0;
10607
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010608 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010609 cpu = get_cpu();
10610 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010611 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010612 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010613 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010614 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010615 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010616 err = alloc_apic_access_page(kvm);
10617 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010618 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010619 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010620
Sean Christophersone90008d2018-03-05 12:04:37 -080010621 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010622 err = init_rmode_identity_map(kvm);
10623 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010624 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010625 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010626
Wanpeng Li5c614b32015-10-13 09:18:36 -070010627 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010628 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10629 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010630 vmx->nested.vpid02 = allocate_vpid();
10631 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010632
Wincy Van705699a2015-02-03 23:58:17 +080010633 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010634 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010635
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010636 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10637
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010638 /*
10639 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10640 * or POSTED_INTR_WAKEUP_VECTOR.
10641 */
10642 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10643 vmx->pi_desc.sn = 1;
10644
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010645 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010646
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010647free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010648 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010649 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010650free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010651 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010652free_pml:
10653 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010654uninit_vcpu:
10655 kvm_vcpu_uninit(&vmx->vcpu);
10656free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010657 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010658 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010659 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010660}
10661
Wanpeng Lib31c1142018-03-12 04:53:04 -070010662static int vmx_vm_init(struct kvm *kvm)
10663{
10664 if (!ple_gap)
10665 kvm->arch.pause_in_guest = true;
10666 return 0;
10667}
10668
Yang, Sheng002c7f72007-07-31 14:23:01 +030010669static void __init vmx_check_processor_compat(void *rtn)
10670{
10671 struct vmcs_config vmcs_conf;
10672
10673 *(int *)rtn = 0;
10674 if (setup_vmcs_config(&vmcs_conf) < 0)
10675 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010676 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010677 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10678 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10679 smp_processor_id());
10680 *(int *)rtn = -EIO;
10681 }
10682}
10683
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010684static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010685{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010686 u8 cache;
10687 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010688
Sheng Yang522c68c2009-04-27 20:35:43 +080010689 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010690 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010691 * 2. EPT with VT-d:
10692 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010693 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010694 * b. VT-d with snooping control feature: snooping control feature of
10695 * VT-d engine can guarantee the cache correctness. Just set it
10696 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010697 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010698 * consistent with host MTRR
10699 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010700 if (is_mmio) {
10701 cache = MTRR_TYPE_UNCACHABLE;
10702 goto exit;
10703 }
10704
10705 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010706 ipat = VMX_EPT_IPAT_BIT;
10707 cache = MTRR_TYPE_WRBACK;
10708 goto exit;
10709 }
10710
10711 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10712 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010713 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010714 cache = MTRR_TYPE_WRBACK;
10715 else
10716 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010717 goto exit;
10718 }
10719
Xiao Guangrongff536042015-06-15 16:55:22 +080010720 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010721
10722exit:
10723 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010724}
10725
Sheng Yang17cc3932010-01-05 19:02:27 +080010726static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010727{
Sheng Yang878403b2010-01-05 19:02:29 +080010728 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10729 return PT_DIRECTORY_LEVEL;
10730 else
10731 /* For shadow and EPT supported 1GB page */
10732 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010733}
10734
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010735static void vmcs_set_secondary_exec_control(u32 new_ctl)
10736{
10737 /*
10738 * These bits in the secondary execution controls field
10739 * are dynamic, the others are mostly based on the hypervisor
10740 * architecture and the guest's CPUID. Do not touch the
10741 * dynamic bits.
10742 */
10743 u32 mask =
10744 SECONDARY_EXEC_SHADOW_VMCS |
10745 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010746 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10747 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010748
10749 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10750
10751 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10752 (new_ctl & ~mask) | (cur_ctl & mask));
10753}
10754
David Matlack8322ebb2016-11-29 18:14:09 -080010755/*
10756 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10757 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10758 */
10759static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10760{
10761 struct vcpu_vmx *vmx = to_vmx(vcpu);
10762 struct kvm_cpuid_entry2 *entry;
10763
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010764 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10765 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010766
10767#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10768 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010769 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010770} while (0)
10771
10772 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10773 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10774 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10775 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10776 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10777 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10778 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10779 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10780 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10781 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10782 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10783 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10784 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10785 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10786 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10787
10788 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10789 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10790 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10791 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10792 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010793 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010794
10795#undef cr4_fixed1_update
10796}
10797
Sheng Yang0e851882009-12-18 16:48:46 +080010798static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10799{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010800 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010801
Paolo Bonzini80154d72017-08-24 13:55:35 +020010802 if (cpu_has_secondary_exec_ctrls()) {
10803 vmx_compute_secondary_exec_control(vmx);
10804 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010805 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010806
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010807 if (nested_vmx_allowed(vcpu))
10808 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10809 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10810 else
10811 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10812 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010813
10814 if (nested_vmx_allowed(vcpu))
10815 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010816}
10817
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010818static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10819{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010820 if (func == 1 && nested)
10821 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010822}
10823
Yang Zhang25d92082013-08-06 12:00:32 +030010824static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10825 struct x86_exception *fault)
10826{
Jan Kiszka533558b2014-01-04 18:47:20 +010010827 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010828 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010829 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010830 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010831
Bandan Dasc5f983f2017-05-05 15:25:14 -040010832 if (vmx->nested.pml_full) {
10833 exit_reason = EXIT_REASON_PML_FULL;
10834 vmx->nested.pml_full = false;
10835 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10836 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010837 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010838 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010839 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010840
10841 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010842 vmcs12->guest_physical_address = fault->address;
10843}
10844
Peter Feiner995f00a2017-06-30 17:26:32 -070010845static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10846{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010847 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010848}
10849
Nadav Har'El155a97a2013-08-05 11:07:16 +030010850/* Callbacks for nested_ept_init_mmu_context: */
10851
10852static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10853{
10854 /* return the page table to be shadowed - in our case, EPT12 */
10855 return get_vmcs12(vcpu)->ept_pointer;
10856}
10857
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010858static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010859{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010860 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010861 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010862 return 1;
10863
Paolo Bonziniad896af2013-10-02 16:56:14 +020010864 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010865 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010866 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010867 nested_ept_ad_enabled(vcpu),
10868 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010869 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10870 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10871 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10872
10873 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010874 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010875}
10876
10877static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10878{
10879 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10880}
10881
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010882static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10883 u16 error_code)
10884{
10885 bool inequality, bit;
10886
10887 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10888 inequality =
10889 (error_code & vmcs12->page_fault_error_code_mask) !=
10890 vmcs12->page_fault_error_code_match;
10891 return inequality ^ bit;
10892}
10893
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010894static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10895 struct x86_exception *fault)
10896{
10897 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10898
10899 WARN_ON(!is_guest_mode(vcpu));
10900
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010901 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10902 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010903 vmcs12->vm_exit_intr_error_code = fault->error_code;
10904 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10905 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10906 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10907 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010908 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010909 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010910 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010911}
10912
Paolo Bonzinic9923842017-12-13 14:16:30 +010010913static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10914 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010915
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010916static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010917{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010918 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010919 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010920 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010921 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010922
10923 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010924 /*
10925 * Translate L1 physical address to host physical
10926 * address for vmcs02. Keep the page pinned, so this
10927 * physical address remains valid. We keep a reference
10928 * to it so we can release it later.
10929 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010930 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010931 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010932 vmx->nested.apic_access_page = NULL;
10933 }
10934 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010935 /*
10936 * If translation failed, no matter: This feature asks
10937 * to exit when accessing the given address, and if it
10938 * can never be accessed, this feature won't do
10939 * anything anyway.
10940 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010941 if (!is_error_page(page)) {
10942 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010943 hpa = page_to_phys(vmx->nested.apic_access_page);
10944 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10945 } else {
10946 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10947 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10948 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010949 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010950
10951 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010952 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010953 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010954 vmx->nested.virtual_apic_page = NULL;
10955 }
10956 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010957
10958 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010959 * If translation failed, VM entry will fail because
10960 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10961 * Failing the vm entry is _not_ what the processor
10962 * does but it's basically the only possibility we
10963 * have. We could still enter the guest if CR8 load
10964 * exits are enabled, CR8 store exits are enabled, and
10965 * virtualize APIC access is disabled; in this case
10966 * the processor would never use the TPR shadow and we
10967 * could simply clear the bit from the execution
10968 * control. But such a configuration is useless, so
10969 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010970 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010971 if (!is_error_page(page)) {
10972 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010973 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10974 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10975 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010976 }
10977
Wincy Van705699a2015-02-03 23:58:17 +080010978 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010979 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10980 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010981 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010982 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010983 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010984 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10985 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010986 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010987 vmx->nested.pi_desc_page = page;
10988 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010989 vmx->nested.pi_desc =
10990 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10991 (unsigned long)(vmcs12->posted_intr_desc_addr &
10992 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010993 vmcs_write64(POSTED_INTR_DESC_ADDR,
10994 page_to_phys(vmx->nested.pi_desc_page) +
10995 (unsigned long)(vmcs12->posted_intr_desc_addr &
10996 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010997 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010998 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010999 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11000 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011001 else
11002 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11003 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011004}
11005
Jan Kiszkaf4124502014-03-07 20:03:13 +010011006static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11007{
11008 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11009 struct vcpu_vmx *vmx = to_vmx(vcpu);
11010
11011 if (vcpu->arch.virtual_tsc_khz == 0)
11012 return;
11013
11014 /* Make sure short timeouts reliably trigger an immediate vmexit.
11015 * hrtimer_start does not guarantee this. */
11016 if (preemption_timeout <= 1) {
11017 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11018 return;
11019 }
11020
11021 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11022 preemption_timeout *= 1000000;
11023 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11024 hrtimer_start(&vmx->nested.preemption_timer,
11025 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11026}
11027
Jim Mattson56a20512017-07-06 16:33:06 -070011028static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11029 struct vmcs12 *vmcs12)
11030{
11031 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11032 return 0;
11033
11034 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11035 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11036 return -EINVAL;
11037
11038 return 0;
11039}
11040
Wincy Van3af18d92015-02-03 23:49:31 +080011041static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11042 struct vmcs12 *vmcs12)
11043{
Wincy Van3af18d92015-02-03 23:49:31 +080011044 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11045 return 0;
11046
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011047 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011048 return -EINVAL;
11049
11050 return 0;
11051}
11052
Jim Mattson712b12d2017-08-24 13:24:47 -070011053static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11054 struct vmcs12 *vmcs12)
11055{
11056 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11057 return 0;
11058
11059 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11060 return -EINVAL;
11061
11062 return 0;
11063}
11064
Wincy Van3af18d92015-02-03 23:49:31 +080011065/*
11066 * Merge L0's and L1's MSR bitmap, return false to indicate that
11067 * we do not use the hardware.
11068 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011069static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11070 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011071{
Wincy Van82f0dd42015-02-03 23:57:18 +080011072 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011073 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011074 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011075 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011076 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011077 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011078 *
11079 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11080 * ensures that we do not accidentally generate an L02 MSR bitmap
11081 * from the L12 MSR bitmap that is too permissive.
11082 * 2. That L1 or L2s have actually used the MSR. This avoids
11083 * unnecessarily merging of the bitmap if the MSR is unused. This
11084 * works properly because we only update the L01 MSR bitmap lazily.
11085 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11086 * updated to reflect this when L1 (or its L2s) actually write to
11087 * the MSR.
11088 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011089 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11090 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011091
Paolo Bonzinic9923842017-12-13 14:16:30 +010011092 /* Nothing to do if the MSR bitmap is not in use. */
11093 if (!cpu_has_vmx_msr_bitmap() ||
11094 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11095 return false;
11096
Ashok Raj15d45072018-02-01 22:59:43 +010011097 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011098 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011099 return false;
11100
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011101 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11102 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011103 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011104
Radim Krčmářd048c092016-08-08 20:16:22 +020011105 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011106 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11107 /*
11108 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11109 * just lets the processor take the value from the virtual-APIC page;
11110 * take those 256 bits directly from the L1 bitmap.
11111 */
11112 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11113 unsigned word = msr / BITS_PER_LONG;
11114 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11115 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011116 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011117 } else {
11118 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11119 unsigned word = msr / BITS_PER_LONG;
11120 msr_bitmap_l0[word] = ~0;
11121 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11122 }
11123 }
11124
11125 nested_vmx_disable_intercept_for_msr(
11126 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011127 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011128 MSR_TYPE_W);
11129
11130 if (nested_cpu_has_vid(vmcs12)) {
11131 nested_vmx_disable_intercept_for_msr(
11132 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011133 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011134 MSR_TYPE_W);
11135 nested_vmx_disable_intercept_for_msr(
11136 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011137 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011138 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011139 }
Ashok Raj15d45072018-02-01 22:59:43 +010011140
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011141 if (spec_ctrl)
11142 nested_vmx_disable_intercept_for_msr(
11143 msr_bitmap_l1, msr_bitmap_l0,
11144 MSR_IA32_SPEC_CTRL,
11145 MSR_TYPE_R | MSR_TYPE_W);
11146
Ashok Raj15d45072018-02-01 22:59:43 +010011147 if (pred_cmd)
11148 nested_vmx_disable_intercept_for_msr(
11149 msr_bitmap_l1, msr_bitmap_l0,
11150 MSR_IA32_PRED_CMD,
11151 MSR_TYPE_W);
11152
Wincy Vanf2b93282015-02-03 23:56:03 +080011153 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011154 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011155
11156 return true;
11157}
11158
Liran Alon61ada742018-06-23 02:35:08 +030011159static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11160 struct vmcs12 *vmcs12)
11161{
11162 struct vmcs12 *shadow;
11163 struct page *page;
11164
11165 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11166 vmcs12->vmcs_link_pointer == -1ull)
11167 return;
11168
11169 shadow = get_shadow_vmcs12(vcpu);
11170 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11171
11172 memcpy(shadow, kmap(page), VMCS12_SIZE);
11173
11174 kunmap(page);
11175 kvm_release_page_clean(page);
11176}
11177
11178static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11179 struct vmcs12 *vmcs12)
11180{
11181 struct vcpu_vmx *vmx = to_vmx(vcpu);
11182
11183 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11184 vmcs12->vmcs_link_pointer == -1ull)
11185 return;
11186
11187 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11188 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11189}
11190
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011191static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11192 struct vmcs12 *vmcs12)
11193{
11194 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11195 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11196 return -EINVAL;
11197 else
11198 return 0;
11199}
11200
Wincy Vanf2b93282015-02-03 23:56:03 +080011201static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11202 struct vmcs12 *vmcs12)
11203{
Wincy Van82f0dd42015-02-03 23:57:18 +080011204 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011205 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011206 !nested_cpu_has_vid(vmcs12) &&
11207 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011208 return 0;
11209
11210 /*
11211 * If virtualize x2apic mode is enabled,
11212 * virtualize apic access must be disabled.
11213 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011214 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11215 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011216 return -EINVAL;
11217
Wincy Van608406e2015-02-03 23:57:51 +080011218 /*
11219 * If virtual interrupt delivery is enabled,
11220 * we must exit on external interrupts.
11221 */
11222 if (nested_cpu_has_vid(vmcs12) &&
11223 !nested_exit_on_intr(vcpu))
11224 return -EINVAL;
11225
Wincy Van705699a2015-02-03 23:58:17 +080011226 /*
11227 * bits 15:8 should be zero in posted_intr_nv,
11228 * the descriptor address has been already checked
11229 * in nested_get_vmcs12_pages.
11230 */
11231 if (nested_cpu_has_posted_intr(vmcs12) &&
11232 (!nested_cpu_has_vid(vmcs12) ||
11233 !nested_exit_intr_ack_set(vcpu) ||
11234 vmcs12->posted_intr_nv & 0xff00))
11235 return -EINVAL;
11236
Wincy Vanf2b93282015-02-03 23:56:03 +080011237 /* tpr shadow is needed by all apicv features. */
11238 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11239 return -EINVAL;
11240
11241 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011242}
11243
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011244static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11245 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011246 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011247{
Liran Alone2536742018-06-23 02:35:02 +030011248 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011249 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011250 u64 count, addr;
11251
Liran Alone2536742018-06-23 02:35:02 +030011252 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11253 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011254 WARN_ON(1);
11255 return -EINVAL;
11256 }
11257 if (count == 0)
11258 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011259 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011260 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11261 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011262 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011263 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11264 addr_field, maxphyaddr, count, addr);
11265 return -EINVAL;
11266 }
11267 return 0;
11268}
11269
11270static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11271 struct vmcs12 *vmcs12)
11272{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011273 if (vmcs12->vm_exit_msr_load_count == 0 &&
11274 vmcs12->vm_exit_msr_store_count == 0 &&
11275 vmcs12->vm_entry_msr_load_count == 0)
11276 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011277 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011278 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011279 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011280 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011281 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011282 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011283 return -EINVAL;
11284 return 0;
11285}
11286
Bandan Dasc5f983f2017-05-05 15:25:14 -040011287static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11288 struct vmcs12 *vmcs12)
11289{
11290 u64 address = vmcs12->pml_address;
11291 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11292
11293 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11294 if (!nested_cpu_has_ept(vmcs12) ||
11295 !IS_ALIGNED(address, 4096) ||
11296 address >> maxphyaddr)
11297 return -EINVAL;
11298 }
11299
11300 return 0;
11301}
11302
Liran Alona8a7c022018-06-23 02:35:06 +030011303static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11304 struct vmcs12 *vmcs12)
11305{
11306 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11307 return 0;
11308
11309 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11310 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11311 return -EINVAL;
11312
11313 return 0;
11314}
11315
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011316static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11317 struct vmx_msr_entry *e)
11318{
11319 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011320 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011321 return -EINVAL;
11322 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11323 e->index == MSR_IA32_UCODE_REV)
11324 return -EINVAL;
11325 if (e->reserved != 0)
11326 return -EINVAL;
11327 return 0;
11328}
11329
11330static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11331 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011332{
11333 if (e->index == MSR_FS_BASE ||
11334 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011335 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11336 nested_vmx_msr_check_common(vcpu, e))
11337 return -EINVAL;
11338 return 0;
11339}
11340
11341static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11342 struct vmx_msr_entry *e)
11343{
11344 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11345 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011346 return -EINVAL;
11347 return 0;
11348}
11349
11350/*
11351 * Load guest's/host's msr at nested entry/exit.
11352 * return 0 for success, entry index for failure.
11353 */
11354static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11355{
11356 u32 i;
11357 struct vmx_msr_entry e;
11358 struct msr_data msr;
11359
11360 msr.host_initiated = false;
11361 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011362 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11363 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011364 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011365 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11366 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011367 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011368 }
11369 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011370 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011371 "%s check failed (%u, 0x%x, 0x%x)\n",
11372 __func__, i, e.index, e.reserved);
11373 goto fail;
11374 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011375 msr.index = e.index;
11376 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011377 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011378 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011379 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11380 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011381 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011382 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011383 }
11384 return 0;
11385fail:
11386 return i + 1;
11387}
11388
11389static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11390{
11391 u32 i;
11392 struct vmx_msr_entry e;
11393
11394 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011395 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011396 if (kvm_vcpu_read_guest(vcpu,
11397 gpa + i * sizeof(e),
11398 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011399 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011400 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11401 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011402 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011403 }
11404 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011405 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011406 "%s check failed (%u, 0x%x, 0x%x)\n",
11407 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011408 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011409 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011410 msr_info.host_initiated = false;
11411 msr_info.index = e.index;
11412 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011413 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011414 "%s cannot read MSR (%u, 0x%x)\n",
11415 __func__, i, e.index);
11416 return -EINVAL;
11417 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011418 if (kvm_vcpu_write_guest(vcpu,
11419 gpa + i * sizeof(e) +
11420 offsetof(struct vmx_msr_entry, value),
11421 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011422 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011423 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011424 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011425 return -EINVAL;
11426 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011427 }
11428 return 0;
11429}
11430
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011431static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11432{
11433 unsigned long invalid_mask;
11434
11435 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11436 return (val & invalid_mask) == 0;
11437}
11438
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011439/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011440 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11441 * emulating VM entry into a guest with EPT enabled.
11442 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11443 * is assigned to entry_failure_code on failure.
11444 */
11445static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011446 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011447{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011448 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011449 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011450 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11451 return 1;
11452 }
11453
11454 /*
11455 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11456 * must not be dereferenced.
11457 */
11458 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11459 !nested_ept) {
11460 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11461 *entry_failure_code = ENTRY_FAIL_PDPTE;
11462 return 1;
11463 }
11464 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011465 }
11466
Junaid Shahid50c28f22018-06-27 14:59:11 -070011467 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011468 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011469
11470 vcpu->arch.cr3 = cr3;
11471 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11472
11473 kvm_init_mmu(vcpu, false);
11474
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011475 return 0;
11476}
11477
Jim Mattson6514dc32018-04-26 16:09:12 -070011478static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011479{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011480 struct vcpu_vmx *vmx = to_vmx(vcpu);
11481
11482 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11483 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11484 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11485 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11486 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11487 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11488 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11489 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11490 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11491 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11492 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11493 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11494 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11495 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11496 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11497 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11498 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11499 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11500 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11501 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11502 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11503 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11504 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11505 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11506 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11507 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11508 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11509 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11510 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11511 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11512 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011513
11514 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11515 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11516 vmcs12->guest_pending_dbg_exceptions);
11517 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11518 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11519
11520 if (nested_cpu_has_xsaves(vmcs12))
11521 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11522 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11523
11524 if (cpu_has_vmx_posted_intr())
11525 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11526
11527 /*
11528 * Whether page-faults are trapped is determined by a combination of
11529 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11530 * If enable_ept, L0 doesn't care about page faults and we should
11531 * set all of these to L1's desires. However, if !enable_ept, L0 does
11532 * care about (at least some) page faults, and because it is not easy
11533 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11534 * to exit on each and every L2 page fault. This is done by setting
11535 * MASK=MATCH=0 and (see below) EB.PF=1.
11536 * Note that below we don't need special code to set EB.PF beyond the
11537 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11538 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11539 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11540 */
11541 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11542 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11543 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11544 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11545
11546 /* All VMFUNCs are currently emulated through L0 vmexits. */
11547 if (cpu_has_vmx_vmfunc())
11548 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11549
11550 if (cpu_has_vmx_apicv()) {
11551 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11552 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11553 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11554 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11555 }
11556
11557 /*
11558 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11559 * Some constant fields are set here by vmx_set_constant_host_state().
11560 * Other fields are different per CPU, and will be set later when
11561 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11562 */
11563 vmx_set_constant_host_state(vmx);
11564
11565 /*
11566 * Set the MSR load/store lists to match L0's settings.
11567 */
11568 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11569 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11570 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11571 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11572 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11573
11574 set_cr4_guest_host_mask(vmx);
11575
11576 if (vmx_mpx_supported())
11577 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11578
11579 if (enable_vpid) {
11580 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11581 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11582 else
11583 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11584 }
11585
11586 /*
11587 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11588 */
11589 if (enable_ept) {
11590 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11591 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11592 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11593 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11594 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011595
11596 if (cpu_has_vmx_msr_bitmap())
11597 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011598}
11599
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011600/*
11601 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11602 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011603 * 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 +030011604 * guest in a way that will both be appropriate to L1's requests, and our
11605 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11606 * function also has additional necessary side-effects, like setting various
11607 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011608 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11609 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011610 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011611static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011612 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011613{
11614 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011615 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011616
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011617 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011618 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011619 vmx->nested.dirty_vmcs12 = false;
11620 }
11621
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011622 /*
11623 * First, the fields that are shadowed. This must be kept in sync
11624 * with vmx_shadow_fields.h.
11625 */
11626
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011627 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011628 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011629 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011630 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11631 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011632
11633 /*
11634 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11635 * HOST_FS_BASE, HOST_GS_BASE.
11636 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011637
Jim Mattson6514dc32018-04-26 16:09:12 -070011638 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011639 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011640 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11641 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11642 } else {
11643 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11644 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11645 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011646 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011647 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11648 vmcs12->vm_entry_intr_info_field);
11649 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11650 vmcs12->vm_entry_exception_error_code);
11651 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11652 vmcs12->vm_entry_instruction_len);
11653 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11654 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011655 vmx->loaded_vmcs->nmi_known_unmasked =
11656 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011657 } else {
11658 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11659 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011660 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011661
Jan Kiszkaf4124502014-03-07 20:03:13 +010011662 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011663
Paolo Bonzini93140062016-07-06 13:23:51 +020011664 /* Preemption timer setting is only taken from vmcs01. */
11665 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11666 exec_control |= vmcs_config.pin_based_exec_ctrl;
11667 if (vmx->hv_deadline_tsc == -1)
11668 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11669
11670 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011671 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011672 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11673 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011674 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011675 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011676 }
Wincy Van705699a2015-02-03 23:58:17 +080011677
Jan Kiszkaf4124502014-03-07 20:03:13 +010011678 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011679
Jan Kiszkaf4124502014-03-07 20:03:13 +010011680 vmx->nested.preemption_timer_expired = false;
11681 if (nested_cpu_has_preemption_timer(vmcs12))
11682 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011683
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011684 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011685 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011686
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011687 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011688 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011689 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011690 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011691 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011692 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011693 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11694 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011695 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011696 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11697 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11698 ~SECONDARY_EXEC_ENABLE_PML;
11699 exec_control |= vmcs12_exec_ctrl;
11700 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011701
Liran Alon32c7acf2018-06-23 02:35:11 +030011702 /* VMCS shadowing for L2 is emulated for now */
11703 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11704
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011705 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011706 vmcs_write16(GUEST_INTR_STATUS,
11707 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011708
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011709 /*
11710 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11711 * nested_get_vmcs12_pages will either fix it up or
11712 * remove the VM execution control.
11713 */
11714 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11715 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11716
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011717 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11718 }
11719
Jim Mattson83bafef2016-10-04 10:48:38 -070011720 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011721 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11722 * entry, but only if the current (host) sp changed from the value
11723 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11724 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11725 * here we just force the write to happen on entry.
11726 */
11727 vmx->host_rsp = 0;
11728
11729 exec_control = vmx_exec_control(vmx); /* L0's desires */
11730 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11731 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11732 exec_control &= ~CPU_BASED_TPR_SHADOW;
11733 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011734
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011735 /*
11736 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11737 * nested_get_vmcs12_pages can't fix it up, the illegal value
11738 * will result in a VM entry failure.
11739 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011740 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011741 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011742 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011743 } else {
11744#ifdef CONFIG_X86_64
11745 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11746 CPU_BASED_CR8_STORE_EXITING;
11747#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011748 }
11749
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011750 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011751 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11752 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011753 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011754 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11755 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11756
11757 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11758
11759 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11760 * bitwise-or of what L1 wants to trap for L2, and what we want to
11761 * trap. Note that CR0.TS also needs updating - we do this later.
11762 */
11763 update_exception_bitmap(vcpu);
11764 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11765 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11766
Nadav Har'El8049d652013-08-05 11:07:06 +030011767 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11768 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11769 * bits are further modified by vmx_set_efer() below.
11770 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011771 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011772
11773 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11774 * emulated by vmx_set_efer(), below.
11775 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011776 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011777 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11778 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011779 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11780
Jim Mattson6514dc32018-04-26 16:09:12 -070011781 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011782 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011783 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011784 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011785 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011786 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011787 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011788
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011789 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11790
Peter Feinerc95ba922016-08-17 09:36:47 -070011791 if (kvm_has_tsc_control)
11792 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011793
11794 if (enable_vpid) {
11795 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011796 * There is no direct mapping between vpid02 and vpid12, the
11797 * vpid02 is per-vCPU for L0 and reused while the value of
11798 * vpid12 is changed w/ one invvpid during nested vmentry.
11799 * The vpid12 is allocated by L1 for L2, so it will not
11800 * influence global bitmap(for vpid01 and vpid02 allocation)
11801 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011802 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011803 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011804 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11805 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011806 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011807 }
11808 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011809 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011810 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011811 }
11812
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011813 if (enable_pml) {
11814 /*
11815 * Conceptually we want to copy the PML address and index from
11816 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11817 * since we always flush the log on each vmexit, this happens
11818 * to be equivalent to simply resetting the fields in vmcs02.
11819 */
11820 ASSERT(vmx->pml_pg);
11821 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11822 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11823 }
11824
Nadav Har'El155a97a2013-08-05 11:07:16 +030011825 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011826 if (nested_ept_init_mmu_context(vcpu)) {
11827 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11828 return 1;
11829 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011830 } else if (nested_cpu_has2(vmcs12,
11831 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011832 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011833 }
11834
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011835 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011836 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11837 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011838 * The CR0_READ_SHADOW is what L2 should have expected to read given
11839 * the specifications by L1; It's not enough to take
11840 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11841 * have more bits than L1 expected.
11842 */
11843 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11844 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11845
11846 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11847 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11848
Jim Mattson6514dc32018-04-26 16:09:12 -070011849 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011850 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011851 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11852 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11853 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11854 else
11855 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11856 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11857 vmx_set_efer(vcpu, vcpu->arch.efer);
11858
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011859 /*
11860 * Guest state is invalid and unrestricted guest is disabled,
11861 * which means L1 attempted VMEntry to L2 with invalid state.
11862 * Fail the VMEntry.
11863 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011864 if (vmx->emulation_required) {
11865 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011866 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011867 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011868
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011869 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011870 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011871 entry_failure_code))
11872 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011873
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011874 if (!enable_ept)
11875 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11876
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011877 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11878 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011879 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011880}
11881
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011882static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11883{
11884 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11885 nested_cpu_has_virtual_nmis(vmcs12))
11886 return -EINVAL;
11887
11888 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11889 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11890 return -EINVAL;
11891
11892 return 0;
11893}
11894
Jim Mattsonca0bde22016-11-30 12:03:46 -080011895static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11896{
11897 struct vcpu_vmx *vmx = to_vmx(vcpu);
11898
11899 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11900 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11901 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11902
Jim Mattson56a20512017-07-06 16:33:06 -070011903 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11904 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11905
Jim Mattsonca0bde22016-11-30 12:03:46 -080011906 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11907 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11908
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011909 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11910 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11911
Jim Mattson712b12d2017-08-24 13:24:47 -070011912 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11913 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11914
Jim Mattsonca0bde22016-11-30 12:03:46 -080011915 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11916 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11917
11918 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11919 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11920
Bandan Dasc5f983f2017-05-05 15:25:14 -040011921 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11922 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11923
Liran Alona8a7c022018-06-23 02:35:06 +030011924 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11925 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11926
Jim Mattsonca0bde22016-11-30 12:03:46 -080011927 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011928 vmx->nested.msrs.procbased_ctls_low,
11929 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011930 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11931 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011932 vmx->nested.msrs.secondary_ctls_low,
11933 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011934 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011935 vmx->nested.msrs.pinbased_ctls_low,
11936 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011937 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011938 vmx->nested.msrs.exit_ctls_low,
11939 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011940 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011941 vmx->nested.msrs.entry_ctls_low,
11942 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011943 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11944
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011945 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011946 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11947
Bandan Das41ab9372017-08-03 15:54:43 -040011948 if (nested_cpu_has_vmfunc(vmcs12)) {
11949 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011950 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011951 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11952
11953 if (nested_cpu_has_eptp_switching(vmcs12)) {
11954 if (!nested_cpu_has_ept(vmcs12) ||
11955 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11956 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11957 }
11958 }
Bandan Das27c42a12017-08-03 15:54:42 -040011959
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011960 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11961 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11962
Jim Mattsonca0bde22016-11-30 12:03:46 -080011963 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11964 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11965 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11966 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11967
Marc Orr04473782018-06-20 17:21:29 -070011968 /*
11969 * From the Intel SDM, volume 3:
11970 * Fields relevant to VM-entry event injection must be set properly.
11971 * These fields are the VM-entry interruption-information field, the
11972 * VM-entry exception error code, and the VM-entry instruction length.
11973 */
11974 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11975 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11976 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11977 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11978 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11979 bool should_have_error_code;
11980 bool urg = nested_cpu_has2(vmcs12,
11981 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11982 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11983
11984 /* VM-entry interruption-info field: interruption type */
11985 if (intr_type == INTR_TYPE_RESERVED ||
11986 (intr_type == INTR_TYPE_OTHER_EVENT &&
11987 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11988 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11989
11990 /* VM-entry interruption-info field: vector */
11991 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11992 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11993 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11994 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11995
11996 /* VM-entry interruption-info field: deliver error code */
11997 should_have_error_code =
11998 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11999 x86_exception_has_error_code(vector);
12000 if (has_error_code != should_have_error_code)
12001 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12002
12003 /* VM-entry exception error code */
12004 if (has_error_code &&
12005 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12006 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12007
12008 /* VM-entry interruption-info field: reserved bits */
12009 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12010 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12011
12012 /* VM-entry instruction length */
12013 switch (intr_type) {
12014 case INTR_TYPE_SOFT_EXCEPTION:
12015 case INTR_TYPE_SOFT_INTR:
12016 case INTR_TYPE_PRIV_SW_EXCEPTION:
12017 if ((vmcs12->vm_entry_instruction_len > 15) ||
12018 (vmcs12->vm_entry_instruction_len == 0 &&
12019 !nested_cpu_has_zero_length_injection(vcpu)))
12020 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12021 }
12022 }
12023
Jim Mattsonca0bde22016-11-30 12:03:46 -080012024 return 0;
12025}
12026
Liran Alonf145d902018-06-23 02:35:07 +030012027static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12028 struct vmcs12 *vmcs12)
12029{
12030 int r;
12031 struct page *page;
12032 struct vmcs12 *shadow;
12033
12034 if (vmcs12->vmcs_link_pointer == -1ull)
12035 return 0;
12036
12037 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12038 return -EINVAL;
12039
12040 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12041 if (is_error_page(page))
12042 return -EINVAL;
12043
12044 r = 0;
12045 shadow = kmap(page);
12046 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12047 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12048 r = -EINVAL;
12049 kunmap(page);
12050 kvm_release_page_clean(page);
12051 return r;
12052}
12053
Jim Mattsonca0bde22016-11-30 12:03:46 -080012054static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12055 u32 *exit_qual)
12056{
12057 bool ia32e;
12058
12059 *exit_qual = ENTRY_FAIL_DEFAULT;
12060
12061 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12062 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12063 return 1;
12064
Liran Alonf145d902018-06-23 02:35:07 +030012065 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012066 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12067 return 1;
12068 }
12069
12070 /*
12071 * If the load IA32_EFER VM-entry control is 1, the following checks
12072 * are performed on the field for the IA32_EFER MSR:
12073 * - Bits reserved in the IA32_EFER MSR must be 0.
12074 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12075 * the IA-32e mode guest VM-exit control. It must also be identical
12076 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12077 * CR0.PG) is 1.
12078 */
12079 if (to_vmx(vcpu)->nested.nested_run_pending &&
12080 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12081 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12082 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12083 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12084 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12085 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12086 return 1;
12087 }
12088
12089 /*
12090 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12091 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12092 * the values of the LMA and LME bits in the field must each be that of
12093 * the host address-space size VM-exit control.
12094 */
12095 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12096 ia32e = (vmcs12->vm_exit_controls &
12097 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12098 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12099 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12100 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12101 return 1;
12102 }
12103
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012104 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12105 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12106 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12107 return 1;
12108
Jim Mattsonca0bde22016-11-30 12:03:46 -080012109 return 0;
12110}
12111
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012112/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012113 * If exit_qual is NULL, this is being called from state restore (either RSM
12114 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012115 */
12116static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012117{
12118 struct vcpu_vmx *vmx = to_vmx(vcpu);
12119 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012120 bool from_vmentry = !!exit_qual;
12121 u32 dummy_exit_qual;
12122 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012123
Jim Mattson858e25c2016-11-30 12:03:47 -080012124 enter_guest_mode(vcpu);
12125
12126 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12127 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12128
Jim Mattsonde3a0022017-11-27 17:22:25 -060012129 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012130 vmx_segment_cache_clear(vmx);
12131
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012132 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12133 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12134
12135 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012136 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012137 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012138
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012139 if (from_vmentry) {
12140 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012141
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012142 r = EXIT_REASON_MSR_LOAD_FAIL;
12143 *exit_qual = nested_vmx_load_msr(vcpu,
12144 vmcs12->vm_entry_msr_load_addr,
12145 vmcs12->vm_entry_msr_load_count);
12146 if (*exit_qual)
12147 goto fail;
12148 } else {
12149 /*
12150 * The MMU is not initialized to point at the right entities yet and
12151 * "get pages" would need to read data from the guest (i.e. we will
12152 * need to perform gpa to hpa translation). Request a call
12153 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12154 * have already been set at vmentry time and should not be reset.
12155 */
12156 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12157 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012158
Jim Mattson858e25c2016-11-30 12:03:47 -080012159 /*
12160 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12161 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12162 * returned as far as L1 is concerned. It will only return (and set
12163 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12164 */
12165 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012166
12167fail:
12168 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12169 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12170 leave_guest_mode(vcpu);
12171 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012172 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012173}
12174
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012175/*
12176 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12177 * for running an L2 nested guest.
12178 */
12179static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12180{
12181 struct vmcs12 *vmcs12;
12182 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012183 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012184 u32 exit_qual;
12185 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012186
Kyle Hueyeb277562016-11-29 12:40:39 -080012187 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012188 return 1;
12189
Kyle Hueyeb277562016-11-29 12:40:39 -080012190 if (!nested_vmx_check_vmcs12(vcpu))
12191 goto out;
12192
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012193 vmcs12 = get_vmcs12(vcpu);
12194
Liran Alona6192d42018-06-23 02:35:04 +030012195 /*
12196 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12197 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12198 * rather than RFLAGS.ZF, and no error number is stored to the
12199 * VM-instruction error field.
12200 */
12201 if (vmcs12->hdr.shadow_vmcs) {
12202 nested_vmx_failInvalid(vcpu);
12203 goto out;
12204 }
12205
Abel Gordon012f83c2013-04-18 14:39:25 +030012206 if (enable_shadow_vmcs)
12207 copy_shadow_to_vmcs12(vmx);
12208
Nadav Har'El7c177932011-05-25 23:12:04 +030012209 /*
12210 * The nested entry process starts with enforcing various prerequisites
12211 * on vmcs12 as required by the Intel SDM, and act appropriately when
12212 * they fail: As the SDM explains, some conditions should cause the
12213 * instruction to fail, while others will cause the instruction to seem
12214 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12215 * To speed up the normal (success) code path, we should avoid checking
12216 * for misconfigurations which will anyway be caught by the processor
12217 * when using the merged vmcs02.
12218 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012219 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12220 nested_vmx_failValid(vcpu,
12221 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12222 goto out;
12223 }
12224
Nadav Har'El7c177932011-05-25 23:12:04 +030012225 if (vmcs12->launch_state == launch) {
12226 nested_vmx_failValid(vcpu,
12227 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12228 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012229 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012230 }
12231
Jim Mattsonca0bde22016-11-30 12:03:46 -080012232 ret = check_vmentry_prereqs(vcpu, vmcs12);
12233 if (ret) {
12234 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012235 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012236 }
12237
Nadav Har'El7c177932011-05-25 23:12:04 +030012238 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012239 * After this point, the trap flag no longer triggers a singlestep trap
12240 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12241 * This is not 100% correct; for performance reasons, we delegate most
12242 * of the checks on host state to the processor. If those fail,
12243 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012244 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012245 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012246
Jim Mattsonca0bde22016-11-30 12:03:46 -080012247 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12248 if (ret) {
12249 nested_vmx_entry_failure(vcpu, vmcs12,
12250 EXIT_REASON_INVALID_STATE, exit_qual);
12251 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012252 }
12253
12254 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012255 * We're finally done with prerequisite checking, and can start with
12256 * the nested entry.
12257 */
12258
Jim Mattson6514dc32018-04-26 16:09:12 -070012259 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012260 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012261 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012262 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012263 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012264 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012265 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012266
Chao Gao135a06c2018-02-11 10:06:30 +080012267 /*
Liran Alon61ada742018-06-23 02:35:08 +030012268 * Must happen outside of enter_vmx_non_root_mode() as it will
12269 * also be used as part of restoring nVMX state for
12270 * snapshot restore (migration).
12271 *
12272 * In this flow, it is assumed that vmcs12 cache was
12273 * trasferred as part of captured nVMX state and should
12274 * therefore not be read from guest memory (which may not
12275 * exist on destination host yet).
12276 */
12277 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12278
12279 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012280 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12281 * by event injection, halt vcpu.
12282 */
12283 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012284 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12285 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012286 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012287 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012288 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012289
12290out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012291 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012292}
12293
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012294/*
12295 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12296 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12297 * This function returns the new value we should put in vmcs12.guest_cr0.
12298 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12299 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12300 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12301 * didn't trap the bit, because if L1 did, so would L0).
12302 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12303 * been modified by L2, and L1 knows it. So just leave the old value of
12304 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12305 * isn't relevant, because if L0 traps this bit it can set it to anything.
12306 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12307 * changed these bits, and therefore they need to be updated, but L0
12308 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12309 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12310 */
12311static inline unsigned long
12312vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12313{
12314 return
12315 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12316 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12317 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12318 vcpu->arch.cr0_guest_owned_bits));
12319}
12320
12321static inline unsigned long
12322vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12323{
12324 return
12325 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12326 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12327 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12328 vcpu->arch.cr4_guest_owned_bits));
12329}
12330
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012331static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12332 struct vmcs12 *vmcs12)
12333{
12334 u32 idt_vectoring;
12335 unsigned int nr;
12336
Wanpeng Li664f8e22017-08-24 03:35:09 -070012337 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012338 nr = vcpu->arch.exception.nr;
12339 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12340
12341 if (kvm_exception_is_soft(nr)) {
12342 vmcs12->vm_exit_instruction_len =
12343 vcpu->arch.event_exit_inst_len;
12344 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12345 } else
12346 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12347
12348 if (vcpu->arch.exception.has_error_code) {
12349 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12350 vmcs12->idt_vectoring_error_code =
12351 vcpu->arch.exception.error_code;
12352 }
12353
12354 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012355 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012356 vmcs12->idt_vectoring_info_field =
12357 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012358 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012359 nr = vcpu->arch.interrupt.nr;
12360 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12361
12362 if (vcpu->arch.interrupt.soft) {
12363 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12364 vmcs12->vm_entry_instruction_len =
12365 vcpu->arch.event_exit_inst_len;
12366 } else
12367 idt_vectoring |= INTR_TYPE_EXT_INTR;
12368
12369 vmcs12->idt_vectoring_info_field = idt_vectoring;
12370 }
12371}
12372
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012373static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12374{
12375 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012376 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012377 bool block_nested_events =
12378 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012379
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012380 if (vcpu->arch.exception.pending &&
12381 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012382 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012383 return -EBUSY;
12384 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012385 return 0;
12386 }
12387
Jan Kiszkaf4124502014-03-07 20:03:13 +010012388 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12389 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012390 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012391 return -EBUSY;
12392 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12393 return 0;
12394 }
12395
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012396 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012397 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012398 return -EBUSY;
12399 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12400 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12401 INTR_INFO_VALID_MASK, 0);
12402 /*
12403 * The NMI-triggered VM exit counts as injection:
12404 * clear this one and block further NMIs.
12405 */
12406 vcpu->arch.nmi_pending = 0;
12407 vmx_set_nmi_mask(vcpu, true);
12408 return 0;
12409 }
12410
12411 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12412 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012413 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012414 return -EBUSY;
12415 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012416 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012417 }
12418
David Hildenbrand6342c502017-01-25 11:58:58 +010012419 vmx_complete_nested_posted_interrupt(vcpu);
12420 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012421}
12422
Jan Kiszkaf4124502014-03-07 20:03:13 +010012423static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12424{
12425 ktime_t remaining =
12426 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12427 u64 value;
12428
12429 if (ktime_to_ns(remaining) <= 0)
12430 return 0;
12431
12432 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12433 do_div(value, 1000000);
12434 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12435}
12436
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012437/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012438 * Update the guest state fields of vmcs12 to reflect changes that
12439 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12440 * VM-entry controls is also updated, since this is really a guest
12441 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012442 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012443static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012444{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012445 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12446 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12447
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012448 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12449 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12450 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12451
12452 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12453 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12454 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12455 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12456 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12457 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12458 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12459 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12460 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12461 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12462 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12463 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12464 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12465 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12466 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12467 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12468 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12469 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12470 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12471 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12472 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12473 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12474 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12475 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12476 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12477 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12478 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12479 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12480 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12481 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12482 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12483 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12484 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12485 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12486 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12487 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12488
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012489 vmcs12->guest_interruptibility_info =
12490 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12491 vmcs12->guest_pending_dbg_exceptions =
12492 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012493 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12494 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12495 else
12496 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012497
Jan Kiszkaf4124502014-03-07 20:03:13 +010012498 if (nested_cpu_has_preemption_timer(vmcs12)) {
12499 if (vmcs12->vm_exit_controls &
12500 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12501 vmcs12->vmx_preemption_timer_value =
12502 vmx_get_preemption_timer_value(vcpu);
12503 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12504 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012505
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012506 /*
12507 * In some cases (usually, nested EPT), L2 is allowed to change its
12508 * own CR3 without exiting. If it has changed it, we must keep it.
12509 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12510 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12511 *
12512 * Additionally, restore L2's PDPTR to vmcs12.
12513 */
12514 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012515 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012516 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12517 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12518 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12519 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12520 }
12521
Jim Mattsond281e132017-06-01 12:44:46 -070012522 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012523
Wincy Van608406e2015-02-03 23:57:51 +080012524 if (nested_cpu_has_vid(vmcs12))
12525 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12526
Jan Kiszkac18911a2013-03-13 16:06:41 +010012527 vmcs12->vm_entry_controls =
12528 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012529 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012530
Jan Kiszka2996fca2014-06-16 13:59:43 +020012531 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12532 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12533 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12534 }
12535
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012536 /* TODO: These cannot have changed unless we have MSR bitmaps and
12537 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012538 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012539 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012540 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12541 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012542 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12543 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12544 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012545 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012546 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012547}
12548
12549/*
12550 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12551 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12552 * and this function updates it to reflect the changes to the guest state while
12553 * L2 was running (and perhaps made some exits which were handled directly by L0
12554 * without going back to L1), and to reflect the exit reason.
12555 * Note that we do not have to copy here all VMCS fields, just those that
12556 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12557 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12558 * which already writes to vmcs12 directly.
12559 */
12560static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12561 u32 exit_reason, u32 exit_intr_info,
12562 unsigned long exit_qualification)
12563{
12564 /* update guest state fields: */
12565 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012566
12567 /* update exit information fields: */
12568
Jan Kiszka533558b2014-01-04 18:47:20 +010012569 vmcs12->vm_exit_reason = exit_reason;
12570 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012571 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012572
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012573 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012574 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12575 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12576
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012577 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012578 vmcs12->launch_state = 1;
12579
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012580 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12581 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012582 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012583
12584 /*
12585 * Transfer the event that L0 or L1 may wanted to inject into
12586 * L2 to IDT_VECTORING_INFO_FIELD.
12587 */
12588 vmcs12_save_pending_event(vcpu, vmcs12);
12589 }
12590
12591 /*
12592 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12593 * preserved above and would only end up incorrectly in L1.
12594 */
12595 vcpu->arch.nmi_injected = false;
12596 kvm_clear_exception_queue(vcpu);
12597 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012598}
12599
Wanpeng Li5af41572017-11-05 16:54:49 -080012600static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12601 struct vmcs12 *vmcs12)
12602{
12603 u32 entry_failure_code;
12604
12605 nested_ept_uninit_mmu_context(vcpu);
12606
12607 /*
12608 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12609 * couldn't have changed.
12610 */
12611 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12612 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12613
12614 if (!enable_ept)
12615 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12616}
12617
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012618/*
12619 * A part of what we need to when the nested L2 guest exits and we want to
12620 * run its L1 parent, is to reset L1's guest state to the host state specified
12621 * in vmcs12.
12622 * This function is to be called not only on normal nested exit, but also on
12623 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12624 * Failures During or After Loading Guest State").
12625 * This function should be called when the active VMCS is L1's (vmcs01).
12626 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012627static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12628 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012629{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012630 struct kvm_segment seg;
12631
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012632 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12633 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012634 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012635 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12636 else
12637 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12638 vmx_set_efer(vcpu, vcpu->arch.efer);
12639
12640 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12641 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012642 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012643 /*
12644 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012645 * actually changed, because vmx_set_cr0 refers to efer set above.
12646 *
12647 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12648 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012649 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012650 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012651 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012652
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012653 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012654 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012655 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012656
Wanpeng Li5af41572017-11-05 16:54:49 -080012657 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012658
Liran Alon6f1e03b2018-05-22 17:16:14 +030012659 /*
12660 * If vmcs01 don't use VPID, CPU flushes TLB on every
12661 * VMEntry/VMExit. Thus, no need to flush TLB.
12662 *
12663 * If vmcs12 uses VPID, TLB entries populated by L2 are
12664 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12665 * with vmx->vpid. Thus, no need to flush TLB.
12666 *
12667 * Therefore, flush TLB only in case vmcs01 uses VPID and
12668 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12669 * are both tagged with vmx->vpid.
12670 */
12671 if (enable_vpid &&
12672 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012673 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012674 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012675
12676 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12677 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12678 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12679 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12680 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012681 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12682 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012683
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012684 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12685 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12686 vmcs_write64(GUEST_BNDCFGS, 0);
12687
Jan Kiszka44811c02013-08-04 17:17:27 +020012688 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012689 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012690 vcpu->arch.pat = vmcs12->host_ia32_pat;
12691 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012692 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12693 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12694 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012695
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012696 /* Set L1 segment info according to Intel SDM
12697 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12698 seg = (struct kvm_segment) {
12699 .base = 0,
12700 .limit = 0xFFFFFFFF,
12701 .selector = vmcs12->host_cs_selector,
12702 .type = 11,
12703 .present = 1,
12704 .s = 1,
12705 .g = 1
12706 };
12707 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12708 seg.l = 1;
12709 else
12710 seg.db = 1;
12711 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12712 seg = (struct kvm_segment) {
12713 .base = 0,
12714 .limit = 0xFFFFFFFF,
12715 .type = 3,
12716 .present = 1,
12717 .s = 1,
12718 .db = 1,
12719 .g = 1
12720 };
12721 seg.selector = vmcs12->host_ds_selector;
12722 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12723 seg.selector = vmcs12->host_es_selector;
12724 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12725 seg.selector = vmcs12->host_ss_selector;
12726 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12727 seg.selector = vmcs12->host_fs_selector;
12728 seg.base = vmcs12->host_fs_base;
12729 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12730 seg.selector = vmcs12->host_gs_selector;
12731 seg.base = vmcs12->host_gs_base;
12732 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12733 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012734 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012735 .limit = 0x67,
12736 .selector = vmcs12->host_tr_selector,
12737 .type = 11,
12738 .present = 1
12739 };
12740 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12741
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012742 kvm_set_dr(vcpu, 7, 0x400);
12743 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012744
Wincy Van3af18d92015-02-03 23:49:31 +080012745 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012746 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012747
Wincy Vanff651cb2014-12-11 08:52:58 +030012748 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12749 vmcs12->vm_exit_msr_load_count))
12750 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012751}
12752
12753/*
12754 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12755 * and modify vmcs12 to make it see what it would expect to see there if
12756 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12757 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012758static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12759 u32 exit_intr_info,
12760 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012761{
12762 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012763 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12764
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012765 /* trying to cancel vmlaunch/vmresume is a bug */
12766 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12767
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012768 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012769 * The only expected VM-instruction error is "VM entry with
12770 * invalid control field(s)." Anything else indicates a
12771 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012772 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012773 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12774 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12775
12776 leave_guest_mode(vcpu);
12777
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012778 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12779 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12780
Jim Mattson4f350c62017-09-14 16:31:44 -070012781 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012782 if (exit_reason == -1)
12783 sync_vmcs12(vcpu, vmcs12);
12784 else
12785 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12786 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012787
Liran Alon61ada742018-06-23 02:35:08 +030012788 /*
12789 * Must happen outside of sync_vmcs12() as it will
12790 * also be used to capture vmcs12 cache as part of
12791 * capturing nVMX state for snapshot (migration).
12792 *
12793 * Otherwise, this flush will dirty guest memory at a
12794 * point it is already assumed by user-space to be
12795 * immutable.
12796 */
12797 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12798
Jim Mattson4f350c62017-09-14 16:31:44 -070012799 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12800 vmcs12->vm_exit_msr_store_count))
12801 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012802 }
12803
Jim Mattson4f350c62017-09-14 16:31:44 -070012804 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012805 vm_entry_controls_reset_shadow(vmx);
12806 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012807 vmx_segment_cache_clear(vmx);
12808
Paolo Bonzini93140062016-07-06 13:23:51 +020012809 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012810 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12811 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012812 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012813 if (vmx->hv_deadline_tsc == -1)
12814 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12815 PIN_BASED_VMX_PREEMPTION_TIMER);
12816 else
12817 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12818 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012819 if (kvm_has_tsc_control)
12820 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012821
Jim Mattson8d860bb2018-05-09 16:56:05 -040012822 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12823 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12824 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012825 } else if (!nested_cpu_has_ept(vmcs12) &&
12826 nested_cpu_has2(vmcs12,
12827 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012828 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012829 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012830
12831 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12832 vmx->host_rsp = 0;
12833
12834 /* Unpin physical memory we referred to in vmcs02 */
12835 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012836 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012837 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012838 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012839 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012840 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012841 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012842 }
Wincy Van705699a2015-02-03 23:58:17 +080012843 if (vmx->nested.pi_desc_page) {
12844 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012845 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012846 vmx->nested.pi_desc_page = NULL;
12847 vmx->nested.pi_desc = NULL;
12848 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012849
12850 /*
Tang Chen38b99172014-09-24 15:57:54 +080012851 * We are now running in L2, mmu_notifier will force to reload the
12852 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12853 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012854 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012855
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012856 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012857 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012858
12859 /* in case we halted in L2 */
12860 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012861
12862 if (likely(!vmx->fail)) {
12863 /*
12864 * TODO: SDM says that with acknowledge interrupt on
12865 * exit, bit 31 of the VM-exit interrupt information
12866 * (valid interrupt) is always set to 1 on
12867 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12868 * need kvm_cpu_has_interrupt(). See the commit
12869 * message for details.
12870 */
12871 if (nested_exit_intr_ack_set(vcpu) &&
12872 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12873 kvm_cpu_has_interrupt(vcpu)) {
12874 int irq = kvm_cpu_get_interrupt(vcpu);
12875 WARN_ON(irq < 0);
12876 vmcs12->vm_exit_intr_info = irq |
12877 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12878 }
12879
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012880 if (exit_reason != -1)
12881 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12882 vmcs12->exit_qualification,
12883 vmcs12->idt_vectoring_info_field,
12884 vmcs12->vm_exit_intr_info,
12885 vmcs12->vm_exit_intr_error_code,
12886 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012887
12888 load_vmcs12_host_state(vcpu, vmcs12);
12889
12890 return;
12891 }
12892
12893 /*
12894 * After an early L2 VM-entry failure, we're now back
12895 * in L1 which thinks it just finished a VMLAUNCH or
12896 * VMRESUME instruction, so we need to set the failure
12897 * flag and the VM-instruction error field of the VMCS
12898 * accordingly.
12899 */
12900 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012901
12902 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12903
Jim Mattson4f350c62017-09-14 16:31:44 -070012904 /*
12905 * The emulated instruction was already skipped in
12906 * nested_vmx_run, but the updated RIP was never
12907 * written back to the vmcs01.
12908 */
12909 skip_emulated_instruction(vcpu);
12910 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012911}
12912
Nadav Har'El7c177932011-05-25 23:12:04 +030012913/*
Jan Kiszka42124922014-01-04 18:47:19 +010012914 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12915 */
12916static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12917{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012918 if (is_guest_mode(vcpu)) {
12919 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012920 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012921 }
Jan Kiszka42124922014-01-04 18:47:19 +010012922 free_nested(to_vmx(vcpu));
12923}
12924
12925/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012926 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12927 * 23.7 "VM-entry failures during or after loading guest state" (this also
12928 * lists the acceptable exit-reason and exit-qualification parameters).
12929 * It should only be called before L2 actually succeeded to run, and when
12930 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12931 */
12932static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12933 struct vmcs12 *vmcs12,
12934 u32 reason, unsigned long qualification)
12935{
12936 load_vmcs12_host_state(vcpu, vmcs12);
12937 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12938 vmcs12->exit_qualification = qualification;
12939 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012940 if (enable_shadow_vmcs)
12941 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012942}
12943
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012944static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12945 struct x86_instruction_info *info,
12946 enum x86_intercept_stage stage)
12947{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012948 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12949 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12950
12951 /*
12952 * RDPID causes #UD if disabled through secondary execution controls.
12953 * Because it is marked as EmulateOnUD, we need to intercept it here.
12954 */
12955 if (info->intercept == x86_intercept_rdtscp &&
12956 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12957 ctxt->exception.vector = UD_VECTOR;
12958 ctxt->exception.error_code_valid = false;
12959 return X86EMUL_PROPAGATE_FAULT;
12960 }
12961
12962 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012963 return X86EMUL_CONTINUE;
12964}
12965
Yunhong Jiang64672c92016-06-13 14:19:59 -070012966#ifdef CONFIG_X86_64
12967/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12968static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12969 u64 divisor, u64 *result)
12970{
12971 u64 low = a << shift, high = a >> (64 - shift);
12972
12973 /* To avoid the overflow on divq */
12974 if (high >= divisor)
12975 return 1;
12976
12977 /* Low hold the result, high hold rem which is discarded */
12978 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12979 "rm" (divisor), "0" (low), "1" (high));
12980 *result = low;
12981
12982 return 0;
12983}
12984
12985static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12986{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012987 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012988 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012989
12990 if (kvm_mwait_in_guest(vcpu->kvm))
12991 return -EOPNOTSUPP;
12992
12993 vmx = to_vmx(vcpu);
12994 tscl = rdtsc();
12995 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12996 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012997 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12998
12999 if (delta_tsc > lapic_timer_advance_cycles)
13000 delta_tsc -= lapic_timer_advance_cycles;
13001 else
13002 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013003
13004 /* Convert to host delta tsc if tsc scaling is enabled */
13005 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13006 u64_shl_div_u64(delta_tsc,
13007 kvm_tsc_scaling_ratio_frac_bits,
13008 vcpu->arch.tsc_scaling_ratio,
13009 &delta_tsc))
13010 return -ERANGE;
13011
13012 /*
13013 * If the delta tsc can't fit in the 32 bit after the multi shift,
13014 * we can't use the preemption timer.
13015 * It's possible that it fits on later vmentries, but checking
13016 * on every vmentry is costly so we just use an hrtimer.
13017 */
13018 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13019 return -ERANGE;
13020
13021 vmx->hv_deadline_tsc = tscl + delta_tsc;
13022 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13023 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013024
13025 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013026}
13027
13028static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13029{
13030 struct vcpu_vmx *vmx = to_vmx(vcpu);
13031 vmx->hv_deadline_tsc = -1;
13032 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13033 PIN_BASED_VMX_PREEMPTION_TIMER);
13034}
13035#endif
13036
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013037static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013038{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013039 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013040 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013041}
13042
Kai Huang843e4332015-01-28 10:54:28 +080013043static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13044 struct kvm_memory_slot *slot)
13045{
13046 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13047 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13048}
13049
13050static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13051 struct kvm_memory_slot *slot)
13052{
13053 kvm_mmu_slot_set_dirty(kvm, slot);
13054}
13055
13056static void vmx_flush_log_dirty(struct kvm *kvm)
13057{
13058 kvm_flush_pml_buffers(kvm);
13059}
13060
Bandan Dasc5f983f2017-05-05 15:25:14 -040013061static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13062{
13063 struct vmcs12 *vmcs12;
13064 struct vcpu_vmx *vmx = to_vmx(vcpu);
13065 gpa_t gpa;
13066 struct page *page = NULL;
13067 u64 *pml_address;
13068
13069 if (is_guest_mode(vcpu)) {
13070 WARN_ON_ONCE(vmx->nested.pml_full);
13071
13072 /*
13073 * Check if PML is enabled for the nested guest.
13074 * Whether eptp bit 6 is set is already checked
13075 * as part of A/D emulation.
13076 */
13077 vmcs12 = get_vmcs12(vcpu);
13078 if (!nested_cpu_has_pml(vmcs12))
13079 return 0;
13080
Dan Carpenter47698862017-05-10 22:43:17 +030013081 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013082 vmx->nested.pml_full = true;
13083 return 1;
13084 }
13085
13086 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13087
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013088 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13089 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013090 return 0;
13091
13092 pml_address = kmap(page);
13093 pml_address[vmcs12->guest_pml_index--] = gpa;
13094 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013095 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013096 }
13097
13098 return 0;
13099}
13100
Kai Huang843e4332015-01-28 10:54:28 +080013101static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13102 struct kvm_memory_slot *memslot,
13103 gfn_t offset, unsigned long mask)
13104{
13105 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13106}
13107
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013108static void __pi_post_block(struct kvm_vcpu *vcpu)
13109{
13110 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13111 struct pi_desc old, new;
13112 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013113
13114 do {
13115 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013116 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13117 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013118
13119 dest = cpu_physical_id(vcpu->cpu);
13120
13121 if (x2apic_enabled())
13122 new.ndst = dest;
13123 else
13124 new.ndst = (dest << 8) & 0xFF00;
13125
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013126 /* set 'NV' to 'notification vector' */
13127 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013128 } while (cmpxchg64(&pi_desc->control, old.control,
13129 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013130
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013131 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13132 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013133 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013134 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013135 vcpu->pre_pcpu = -1;
13136 }
13137}
13138
Feng Wuefc64402015-09-18 22:29:51 +080013139/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013140 * This routine does the following things for vCPU which is going
13141 * to be blocked if VT-d PI is enabled.
13142 * - Store the vCPU to the wakeup list, so when interrupts happen
13143 * we can find the right vCPU to wake up.
13144 * - Change the Posted-interrupt descriptor as below:
13145 * 'NDST' <-- vcpu->pre_pcpu
13146 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13147 * - If 'ON' is set during this process, which means at least one
13148 * interrupt is posted for this vCPU, we cannot block it, in
13149 * this case, return 1, otherwise, return 0.
13150 *
13151 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013152static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013153{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013154 unsigned int dest;
13155 struct pi_desc old, new;
13156 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13157
13158 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013159 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13160 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013161 return 0;
13162
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013163 WARN_ON(irqs_disabled());
13164 local_irq_disable();
13165 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13166 vcpu->pre_pcpu = vcpu->cpu;
13167 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13168 list_add_tail(&vcpu->blocked_vcpu_list,
13169 &per_cpu(blocked_vcpu_on_cpu,
13170 vcpu->pre_pcpu));
13171 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13172 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013173
13174 do {
13175 old.control = new.control = pi_desc->control;
13176
Feng Wubf9f6ac2015-09-18 22:29:55 +080013177 WARN((pi_desc->sn == 1),
13178 "Warning: SN field of posted-interrupts "
13179 "is set before blocking\n");
13180
13181 /*
13182 * Since vCPU can be preempted during this process,
13183 * vcpu->cpu could be different with pre_pcpu, we
13184 * need to set pre_pcpu as the destination of wakeup
13185 * notification event, then we can find the right vCPU
13186 * to wakeup in wakeup handler if interrupts happen
13187 * when the vCPU is in blocked state.
13188 */
13189 dest = cpu_physical_id(vcpu->pre_pcpu);
13190
13191 if (x2apic_enabled())
13192 new.ndst = dest;
13193 else
13194 new.ndst = (dest << 8) & 0xFF00;
13195
13196 /* set 'NV' to 'wakeup vector' */
13197 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013198 } while (cmpxchg64(&pi_desc->control, old.control,
13199 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013200
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013201 /* We should not block the vCPU if an interrupt is posted for it. */
13202 if (pi_test_on(pi_desc) == 1)
13203 __pi_post_block(vcpu);
13204
13205 local_irq_enable();
13206 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013207}
13208
Yunhong Jiangbc225122016-06-13 14:19:58 -070013209static int vmx_pre_block(struct kvm_vcpu *vcpu)
13210{
13211 if (pi_pre_block(vcpu))
13212 return 1;
13213
Yunhong Jiang64672c92016-06-13 14:19:59 -070013214 if (kvm_lapic_hv_timer_in_use(vcpu))
13215 kvm_lapic_switch_to_sw_timer(vcpu);
13216
Yunhong Jiangbc225122016-06-13 14:19:58 -070013217 return 0;
13218}
13219
13220static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013221{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013222 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013223 return;
13224
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013225 WARN_ON(irqs_disabled());
13226 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013227 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013228 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013229}
13230
Yunhong Jiangbc225122016-06-13 14:19:58 -070013231static void vmx_post_block(struct kvm_vcpu *vcpu)
13232{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013233 if (kvm_x86_ops->set_hv_timer)
13234 kvm_lapic_switch_to_hv_timer(vcpu);
13235
Yunhong Jiangbc225122016-06-13 14:19:58 -070013236 pi_post_block(vcpu);
13237}
13238
Feng Wubf9f6ac2015-09-18 22:29:55 +080013239/*
Feng Wuefc64402015-09-18 22:29:51 +080013240 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13241 *
13242 * @kvm: kvm
13243 * @host_irq: host irq of the interrupt
13244 * @guest_irq: gsi of the interrupt
13245 * @set: set or unset PI
13246 * returns 0 on success, < 0 on failure
13247 */
13248static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13249 uint32_t guest_irq, bool set)
13250{
13251 struct kvm_kernel_irq_routing_entry *e;
13252 struct kvm_irq_routing_table *irq_rt;
13253 struct kvm_lapic_irq irq;
13254 struct kvm_vcpu *vcpu;
13255 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013256 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013257
13258 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013259 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13260 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013261 return 0;
13262
13263 idx = srcu_read_lock(&kvm->irq_srcu);
13264 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013265 if (guest_irq >= irq_rt->nr_rt_entries ||
13266 hlist_empty(&irq_rt->map[guest_irq])) {
13267 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13268 guest_irq, irq_rt->nr_rt_entries);
13269 goto out;
13270 }
Feng Wuefc64402015-09-18 22:29:51 +080013271
13272 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13273 if (e->type != KVM_IRQ_ROUTING_MSI)
13274 continue;
13275 /*
13276 * VT-d PI cannot support posting multicast/broadcast
13277 * interrupts to a vCPU, we still use interrupt remapping
13278 * for these kind of interrupts.
13279 *
13280 * For lowest-priority interrupts, we only support
13281 * those with single CPU as the destination, e.g. user
13282 * configures the interrupts via /proc/irq or uses
13283 * irqbalance to make the interrupts single-CPU.
13284 *
13285 * We will support full lowest-priority interrupt later.
13286 */
13287
Radim Krčmář371313132016-07-12 22:09:27 +020013288 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013289 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13290 /*
13291 * Make sure the IRTE is in remapped mode if
13292 * we don't handle it in posted mode.
13293 */
13294 ret = irq_set_vcpu_affinity(host_irq, NULL);
13295 if (ret < 0) {
13296 printk(KERN_INFO
13297 "failed to back to remapped mode, irq: %u\n",
13298 host_irq);
13299 goto out;
13300 }
13301
Feng Wuefc64402015-09-18 22:29:51 +080013302 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013303 }
Feng Wuefc64402015-09-18 22:29:51 +080013304
13305 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13306 vcpu_info.vector = irq.vector;
13307
hu huajun2698d822018-04-11 15:16:40 +080013308 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013309 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13310
13311 if (set)
13312 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013313 else
Feng Wuefc64402015-09-18 22:29:51 +080013314 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013315
13316 if (ret < 0) {
13317 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13318 __func__);
13319 goto out;
13320 }
13321 }
13322
13323 ret = 0;
13324out:
13325 srcu_read_unlock(&kvm->irq_srcu, idx);
13326 return ret;
13327}
13328
Ashok Rajc45dcc72016-06-22 14:59:56 +080013329static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13330{
13331 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13332 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13333 FEATURE_CONTROL_LMCE;
13334 else
13335 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13336 ~FEATURE_CONTROL_LMCE;
13337}
13338
Ladi Prosek72d7b372017-10-11 16:54:41 +020013339static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13340{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013341 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13342 if (to_vmx(vcpu)->nested.nested_run_pending)
13343 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013344 return 1;
13345}
13346
Ladi Prosek0234bf82017-10-11 16:54:40 +020013347static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13348{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013349 struct vcpu_vmx *vmx = to_vmx(vcpu);
13350
13351 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13352 if (vmx->nested.smm.guest_mode)
13353 nested_vmx_vmexit(vcpu, -1, 0, 0);
13354
13355 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13356 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013357 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013358 return 0;
13359}
13360
13361static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13362{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013363 struct vcpu_vmx *vmx = to_vmx(vcpu);
13364 int ret;
13365
13366 if (vmx->nested.smm.vmxon) {
13367 vmx->nested.vmxon = true;
13368 vmx->nested.smm.vmxon = false;
13369 }
13370
13371 if (vmx->nested.smm.guest_mode) {
13372 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013373 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013374 vcpu->arch.hflags |= HF_SMM_MASK;
13375 if (ret)
13376 return ret;
13377
13378 vmx->nested.smm.guest_mode = false;
13379 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013380 return 0;
13381}
13382
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013383static int enable_smi_window(struct kvm_vcpu *vcpu)
13384{
13385 return 0;
13386}
13387
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013388static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13389 struct kvm_nested_state __user *user_kvm_nested_state,
13390 u32 user_data_size)
13391{
13392 struct vcpu_vmx *vmx;
13393 struct vmcs12 *vmcs12;
13394 struct kvm_nested_state kvm_state = {
13395 .flags = 0,
13396 .format = 0,
13397 .size = sizeof(kvm_state),
13398 .vmx.vmxon_pa = -1ull,
13399 .vmx.vmcs_pa = -1ull,
13400 };
13401
13402 if (!vcpu)
13403 return kvm_state.size + 2 * VMCS12_SIZE;
13404
13405 vmx = to_vmx(vcpu);
13406 vmcs12 = get_vmcs12(vcpu);
13407 if (nested_vmx_allowed(vcpu) &&
13408 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13409 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13410 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13411
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013412 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013413 kvm_state.size += VMCS12_SIZE;
13414
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013415 if (is_guest_mode(vcpu) &&
13416 nested_cpu_has_shadow_vmcs(vmcs12) &&
13417 vmcs12->vmcs_link_pointer != -1ull)
13418 kvm_state.size += VMCS12_SIZE;
13419 }
13420
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013421 if (vmx->nested.smm.vmxon)
13422 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13423
13424 if (vmx->nested.smm.guest_mode)
13425 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13426
13427 if (is_guest_mode(vcpu)) {
13428 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13429
13430 if (vmx->nested.nested_run_pending)
13431 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13432 }
13433 }
13434
13435 if (user_data_size < kvm_state.size)
13436 goto out;
13437
13438 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13439 return -EFAULT;
13440
13441 if (vmx->nested.current_vmptr == -1ull)
13442 goto out;
13443
13444 /*
13445 * When running L2, the authoritative vmcs12 state is in the
13446 * vmcs02. When running L1, the authoritative vmcs12 state is
13447 * in the shadow vmcs linked to vmcs01, unless
13448 * sync_shadow_vmcs is set, in which case, the authoritative
13449 * vmcs12 state is in the vmcs12 already.
13450 */
13451 if (is_guest_mode(vcpu))
13452 sync_vmcs12(vcpu, vmcs12);
13453 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13454 copy_shadow_to_vmcs12(vmx);
13455
13456 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13457 return -EFAULT;
13458
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013459 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13460 vmcs12->vmcs_link_pointer != -1ull) {
13461 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13462 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13463 return -EFAULT;
13464 }
13465
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013466out:
13467 return kvm_state.size;
13468}
13469
13470static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13471 struct kvm_nested_state __user *user_kvm_nested_state,
13472 struct kvm_nested_state *kvm_state)
13473{
13474 struct vcpu_vmx *vmx = to_vmx(vcpu);
13475 struct vmcs12 *vmcs12;
13476 u32 exit_qual;
13477 int ret;
13478
13479 if (kvm_state->format != 0)
13480 return -EINVAL;
13481
13482 if (!nested_vmx_allowed(vcpu))
13483 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13484
13485 if (kvm_state->vmx.vmxon_pa == -1ull) {
13486 if (kvm_state->vmx.smm.flags)
13487 return -EINVAL;
13488
13489 if (kvm_state->vmx.vmcs_pa != -1ull)
13490 return -EINVAL;
13491
13492 vmx_leave_nested(vcpu);
13493 return 0;
13494 }
13495
13496 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13497 return -EINVAL;
13498
13499 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13500 return -EINVAL;
13501
13502 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13503 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13504 return -EINVAL;
13505
13506 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13507 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13508 return -EINVAL;
13509
13510 if (kvm_state->vmx.smm.flags &
13511 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13512 return -EINVAL;
13513
13514 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13515 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13516 return -EINVAL;
13517
13518 vmx_leave_nested(vcpu);
13519 if (kvm_state->vmx.vmxon_pa == -1ull)
13520 return 0;
13521
13522 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13523 ret = enter_vmx_operation(vcpu);
13524 if (ret)
13525 return ret;
13526
13527 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13528
13529 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13530 vmx->nested.smm.vmxon = true;
13531 vmx->nested.vmxon = false;
13532
13533 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13534 vmx->nested.smm.guest_mode = true;
13535 }
13536
13537 vmcs12 = get_vmcs12(vcpu);
13538 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13539 return -EFAULT;
13540
Liran Alon392b2f22018-06-23 02:35:01 +030013541 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013542 return -EINVAL;
13543
13544 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13545 return 0;
13546
13547 vmx->nested.nested_run_pending =
13548 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13549
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013550 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13551 vmcs12->vmcs_link_pointer != -1ull) {
13552 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13553 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13554 return -EINVAL;
13555
13556 if (copy_from_user(shadow_vmcs12,
13557 user_kvm_nested_state->data + VMCS12_SIZE,
13558 sizeof(*vmcs12)))
13559 return -EFAULT;
13560
13561 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13562 !shadow_vmcs12->hdr.shadow_vmcs)
13563 return -EINVAL;
13564 }
13565
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013566 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13567 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13568 return -EINVAL;
13569
13570 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13571 vmx->nested.nested_run_pending = 1;
13572
13573 vmx->nested.dirty_vmcs12 = true;
13574 ret = enter_vmx_non_root_mode(vcpu, NULL);
13575 if (ret)
13576 return -EINVAL;
13577
13578 return 0;
13579}
13580
Kees Cook404f6aa2016-08-08 16:29:06 -070013581static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013582 .cpu_has_kvm_support = cpu_has_kvm_support,
13583 .disabled_by_bios = vmx_disabled_by_bios,
13584 .hardware_setup = hardware_setup,
13585 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013586 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013587 .hardware_enable = hardware_enable,
13588 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013589 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013590 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013591
Wanpeng Lib31c1142018-03-12 04:53:04 -070013592 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013593 .vm_alloc = vmx_vm_alloc,
13594 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013595
Avi Kivity6aa8b732006-12-10 02:21:36 -080013596 .vcpu_create = vmx_create_vcpu,
13597 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013598 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013599
Avi Kivity04d2cc72007-09-10 18:10:54 +030013600 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013601 .vcpu_load = vmx_vcpu_load,
13602 .vcpu_put = vmx_vcpu_put,
13603
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013604 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013605 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013606 .get_msr = vmx_get_msr,
13607 .set_msr = vmx_set_msr,
13608 .get_segment_base = vmx_get_segment_base,
13609 .get_segment = vmx_get_segment,
13610 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013611 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013612 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013613 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013614 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013615 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013616 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013617 .set_cr3 = vmx_set_cr3,
13618 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013619 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013620 .get_idt = vmx_get_idt,
13621 .set_idt = vmx_set_idt,
13622 .get_gdt = vmx_get_gdt,
13623 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013624 .get_dr6 = vmx_get_dr6,
13625 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013626 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013627 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013628 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013629 .get_rflags = vmx_get_rflags,
13630 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013631
Avi Kivity6aa8b732006-12-10 02:21:36 -080013632 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013633 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013634
Avi Kivity6aa8b732006-12-10 02:21:36 -080013635 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013636 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013637 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013638 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13639 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013640 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013641 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013642 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013643 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013644 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013645 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013646 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013647 .get_nmi_mask = vmx_get_nmi_mask,
13648 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013649 .enable_nmi_window = enable_nmi_window,
13650 .enable_irq_window = enable_irq_window,
13651 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013652 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013653 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013654 .get_enable_apicv = vmx_get_enable_apicv,
13655 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013656 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013657 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013658 .hwapic_irr_update = vmx_hwapic_irr_update,
13659 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013660 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13661 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013662
Izik Eiduscbc94022007-10-25 00:29:55 +020013663 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013664 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013665 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013666 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013667
Avi Kivity586f9602010-11-18 13:09:54 +020013668 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013669
Sheng Yang17cc3932010-01-05 19:02:27 +080013670 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013671
13672 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013673
13674 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013675 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013676
13677 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013678
13679 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013680
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013681 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013682 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013683
13684 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013685
13686 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013687 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013688 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013689 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013690 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013691
13692 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013693
13694 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013695
13696 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13697 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13698 .flush_log_dirty = vmx_flush_log_dirty,
13699 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013700 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013701
Feng Wubf9f6ac2015-09-18 22:29:55 +080013702 .pre_block = vmx_pre_block,
13703 .post_block = vmx_post_block,
13704
Wei Huang25462f72015-06-19 15:45:05 +020013705 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013706
13707 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013708
13709#ifdef CONFIG_X86_64
13710 .set_hv_timer = vmx_set_hv_timer,
13711 .cancel_hv_timer = vmx_cancel_hv_timer,
13712#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013713
13714 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013715
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013716 .get_nested_state = vmx_get_nested_state,
13717 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013718 .get_vmcs12_pages = nested_get_vmcs12_pages,
13719
Ladi Prosek72d7b372017-10-11 16:54:41 +020013720 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013721 .pre_enter_smm = vmx_pre_enter_smm,
13722 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013723 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013724};
13725
13726static int __init vmx_init(void)
13727{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013728 int r;
13729
13730#if IS_ENABLED(CONFIG_HYPERV)
13731 /*
13732 * Enlightened VMCS usage should be recommended and the host needs
13733 * to support eVMCS v1 or above. We can also disable eVMCS support
13734 * with module parameter.
13735 */
13736 if (enlightened_vmcs &&
13737 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13738 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13739 KVM_EVMCS_VERSION) {
13740 int cpu;
13741
13742 /* Check that we have assist pages on all online CPUs */
13743 for_each_online_cpu(cpu) {
13744 if (!hv_get_vp_assist_page(cpu)) {
13745 enlightened_vmcs = false;
13746 break;
13747 }
13748 }
13749
13750 if (enlightened_vmcs) {
13751 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13752 static_branch_enable(&enable_evmcs);
13753 }
13754 } else {
13755 enlightened_vmcs = false;
13756 }
13757#endif
13758
13759 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013760 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013761 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013762 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013763
Dave Young2965faa2015-09-09 15:38:55 -070013764#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013765 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13766 crash_vmclear_local_loaded_vmcss);
13767#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013768 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013769
He, Qingfdef3ad2007-04-30 09:45:24 +030013770 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013771}
13772
13773static void __exit vmx_exit(void)
13774{
Dave Young2965faa2015-09-09 15:38:55 -070013775#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013776 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013777 synchronize_rcu();
13778#endif
13779
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013780 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013781
13782#if IS_ENABLED(CONFIG_HYPERV)
13783 if (static_branch_unlikely(&enable_evmcs)) {
13784 int cpu;
13785 struct hv_vp_assist_page *vp_ap;
13786 /*
13787 * Reset everything to support using non-enlightened VMCS
13788 * access later (e.g. when we reload the module with
13789 * enlightened_vmcs=0)
13790 */
13791 for_each_online_cpu(cpu) {
13792 vp_ap = hv_get_vp_assist_page(cpu);
13793
13794 if (!vp_ap)
13795 continue;
13796
13797 vp_ap->current_nested_vmcs = 0;
13798 vp_ap->enlighten_vmentry = 0;
13799 }
13800
13801 static_branch_disable(&enable_evmcs);
13802 }
13803#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013804}
13805
13806module_init(vmx_init)
13807module_exit(vmx_exit)