blob: 37944b79da4593bf199685eedef9e5172ad7043e [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 /*
648 * Indicates if the shadow vmcs must be updated with the
649 * data hold by vmcs12
650 */
651 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100652 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300653
Jim Mattson8d860bb2018-05-09 16:56:05 -0400654 bool change_vmcs01_virtual_apic_mode;
655
Nadav Har'El644d7112011-05-25 23:12:35 +0300656 /* L2 must run next, and mustn't decide to exit to L1. */
657 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600658
659 struct loaded_vmcs vmcs02;
660
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300661 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600662 * Guest pages referred to in the vmcs02 with host-physical
663 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300664 */
665 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800666 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800667 struct page *pi_desc_page;
668 struct pi_desc *pi_desc;
669 bool pi_pending;
670 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100671
672 struct hrtimer preemption_timer;
673 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200674
675 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
676 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800677
Wanpeng Li5c614b32015-10-13 09:18:36 -0700678 u16 vpid02;
679 u16 last_vpid;
680
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100681 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200682
683 /* SMM related state */
684 struct {
685 /* in VMX operation on SMM entry? */
686 bool vmxon;
687 /* in guest mode on SMM entry? */
688 bool guest_mode;
689 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300690};
691
Yang Zhang01e439b2013-04-11 19:25:12 +0800692#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800693#define POSTED_INTR_SN 1
694
Yang Zhang01e439b2013-04-11 19:25:12 +0800695/* Posted-Interrupt Descriptor */
696struct pi_desc {
697 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800698 union {
699 struct {
700 /* bit 256 - Outstanding Notification */
701 u16 on : 1,
702 /* bit 257 - Suppress Notification */
703 sn : 1,
704 /* bit 271:258 - Reserved */
705 rsvd_1 : 14;
706 /* bit 279:272 - Notification Vector */
707 u8 nv;
708 /* bit 287:280 - Reserved */
709 u8 rsvd_2;
710 /* bit 319:288 - Notification Destination */
711 u32 ndst;
712 };
713 u64 control;
714 };
715 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800716} __aligned(64);
717
Yang Zhanga20ed542013-04-11 19:25:15 +0800718static bool pi_test_and_set_on(struct pi_desc *pi_desc)
719{
720 return test_and_set_bit(POSTED_INTR_ON,
721 (unsigned long *)&pi_desc->control);
722}
723
724static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
725{
726 return test_and_clear_bit(POSTED_INTR_ON,
727 (unsigned long *)&pi_desc->control);
728}
729
730static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
731{
732 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
733}
734
Feng Wuebbfc762015-09-18 22:29:46 +0800735static inline void pi_clear_sn(struct pi_desc *pi_desc)
736{
737 return clear_bit(POSTED_INTR_SN,
738 (unsigned long *)&pi_desc->control);
739}
740
741static inline void pi_set_sn(struct pi_desc *pi_desc)
742{
743 return set_bit(POSTED_INTR_SN,
744 (unsigned long *)&pi_desc->control);
745}
746
Paolo Bonziniad361092016-09-20 16:15:05 +0200747static inline void pi_clear_on(struct pi_desc *pi_desc)
748{
749 clear_bit(POSTED_INTR_ON,
750 (unsigned long *)&pi_desc->control);
751}
752
Feng Wuebbfc762015-09-18 22:29:46 +0800753static inline int pi_test_on(struct pi_desc *pi_desc)
754{
755 return test_bit(POSTED_INTR_ON,
756 (unsigned long *)&pi_desc->control);
757}
758
759static inline int pi_test_sn(struct pi_desc *pi_desc)
760{
761 return test_bit(POSTED_INTR_SN,
762 (unsigned long *)&pi_desc->control);
763}
764
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400765struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000766 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300767 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300768 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100769 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300770 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200771 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200772 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300773 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400774 int nmsrs;
775 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800776 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400777#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300778 u64 msr_host_kernel_gs_base;
779 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100781
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100782 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100783 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100784
Gleb Natapov2961e8762013-11-25 15:37:13 +0200785 u32 vm_entry_controls_shadow;
786 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200787 u32 secondary_exec_control;
788
Nadav Har'Eld462b812011-05-24 15:26:10 +0300789 /*
790 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
791 * non-nested (L1) guest, it always points to vmcs01. For a nested
792 * guest (L2), it points to a different VMCS.
793 */
794 struct loaded_vmcs vmcs01;
795 struct loaded_vmcs *loaded_vmcs;
796 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300797 struct msr_autoload {
798 unsigned nr;
799 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
800 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
801 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400802 struct {
803 int loaded;
804 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300805#ifdef CONFIG_X86_64
806 u16 ds_sel, es_sel;
807#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200808 int gs_ldt_reload_needed;
809 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400810 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200811 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300812 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300813 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300814 struct kvm_segment segs[8];
815 } rmode;
816 struct {
817 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300818 struct kvm_save_segment {
819 u16 selector;
820 unsigned long base;
821 u32 limit;
822 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300823 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300824 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800825 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300826 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200827
Andi Kleena0861c02009-06-08 17:37:09 +0800828 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800829
Yang Zhang01e439b2013-04-11 19:25:12 +0800830 /* Posted interrupt descriptor */
831 struct pi_desc pi_desc;
832
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300833 /* Support for a guest hypervisor (nested VMX) */
834 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200835
836 /* Dynamic PLE window. */
837 int ple_window;
838 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800839
840 /* Support for PML */
841#define PML_ENTITY_NUM 512
842 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800843
Yunhong Jiang64672c92016-06-13 14:19:59 -0700844 /* apic deadline value in host tsc */
845 u64 hv_deadline_tsc;
846
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800847 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800848
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800849 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800850
Wanpeng Li74c55932017-11-29 01:31:20 -0800851 unsigned long host_debugctlmsr;
852
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800853 /*
854 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
855 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
856 * in msr_ia32_feature_control_valid_bits.
857 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800858 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800859 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400860};
861
Avi Kivity2fb92db2011-04-27 19:42:18 +0300862enum segment_cache_field {
863 SEG_FIELD_SEL = 0,
864 SEG_FIELD_BASE = 1,
865 SEG_FIELD_LIMIT = 2,
866 SEG_FIELD_AR = 3,
867
868 SEG_FIELD_NR = 4
869};
870
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700871static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
872{
873 return container_of(kvm, struct kvm_vmx, kvm);
874}
875
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400876static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
877{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000878 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400879}
880
Feng Wuefc64402015-09-18 22:29:51 +0800881static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
882{
883 return &(to_vmx(vcpu)->pi_desc);
884}
885
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800886#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300887#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800888#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
889#define FIELD64(number, name) \
890 FIELD(number, name), \
891 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300892
Abel Gordon4607c2d2013-04-18 14:35:55 +0300893
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100894static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100895#define SHADOW_FIELD_RO(x) x,
896#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300897};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400898static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300899 ARRAY_SIZE(shadow_read_only_fields);
900
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100901static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100902#define SHADOW_FIELD_RW(x) x,
903#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300904};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400905static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300906 ARRAY_SIZE(shadow_read_write_fields);
907
Mathias Krause772e0312012-08-30 01:30:19 +0200908static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300909 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800910 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300911 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
912 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
913 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
914 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
915 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
916 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
917 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
918 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800919 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400920 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300921 FIELD(HOST_ES_SELECTOR, host_es_selector),
922 FIELD(HOST_CS_SELECTOR, host_cs_selector),
923 FIELD(HOST_SS_SELECTOR, host_ss_selector),
924 FIELD(HOST_DS_SELECTOR, host_ds_selector),
925 FIELD(HOST_FS_SELECTOR, host_fs_selector),
926 FIELD(HOST_GS_SELECTOR, host_gs_selector),
927 FIELD(HOST_TR_SELECTOR, host_tr_selector),
928 FIELD64(IO_BITMAP_A, io_bitmap_a),
929 FIELD64(IO_BITMAP_B, io_bitmap_b),
930 FIELD64(MSR_BITMAP, msr_bitmap),
931 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
932 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
933 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700934 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300935 FIELD64(TSC_OFFSET, tsc_offset),
936 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
937 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800938 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400939 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300940 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800941 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
942 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
943 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
944 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400945 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700946 FIELD64(VMREAD_BITMAP, vmread_bitmap),
947 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800948 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300949 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
950 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
951 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
952 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
953 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
954 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
955 FIELD64(GUEST_PDPTR0, guest_pdptr0),
956 FIELD64(GUEST_PDPTR1, guest_pdptr1),
957 FIELD64(GUEST_PDPTR2, guest_pdptr2),
958 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100959 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300960 FIELD64(HOST_IA32_PAT, host_ia32_pat),
961 FIELD64(HOST_IA32_EFER, host_ia32_efer),
962 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
963 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
964 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
965 FIELD(EXCEPTION_BITMAP, exception_bitmap),
966 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
967 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
968 FIELD(CR3_TARGET_COUNT, cr3_target_count),
969 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
970 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
971 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
972 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
973 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
974 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
975 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
976 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
977 FIELD(TPR_THRESHOLD, tpr_threshold),
978 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
979 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
980 FIELD(VM_EXIT_REASON, vm_exit_reason),
981 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
982 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
983 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
984 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
985 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
986 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
987 FIELD(GUEST_ES_LIMIT, guest_es_limit),
988 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
989 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
990 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
991 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
992 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
993 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
994 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
995 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
996 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
997 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
998 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
999 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1000 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1001 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1002 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1003 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1004 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1005 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1006 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1007 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1008 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001009 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001010 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1011 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1012 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1013 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1014 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1015 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1016 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1017 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1018 FIELD(EXIT_QUALIFICATION, exit_qualification),
1019 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1020 FIELD(GUEST_CR0, guest_cr0),
1021 FIELD(GUEST_CR3, guest_cr3),
1022 FIELD(GUEST_CR4, guest_cr4),
1023 FIELD(GUEST_ES_BASE, guest_es_base),
1024 FIELD(GUEST_CS_BASE, guest_cs_base),
1025 FIELD(GUEST_SS_BASE, guest_ss_base),
1026 FIELD(GUEST_DS_BASE, guest_ds_base),
1027 FIELD(GUEST_FS_BASE, guest_fs_base),
1028 FIELD(GUEST_GS_BASE, guest_gs_base),
1029 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1030 FIELD(GUEST_TR_BASE, guest_tr_base),
1031 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1032 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1033 FIELD(GUEST_DR7, guest_dr7),
1034 FIELD(GUEST_RSP, guest_rsp),
1035 FIELD(GUEST_RIP, guest_rip),
1036 FIELD(GUEST_RFLAGS, guest_rflags),
1037 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1038 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1039 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1040 FIELD(HOST_CR0, host_cr0),
1041 FIELD(HOST_CR3, host_cr3),
1042 FIELD(HOST_CR4, host_cr4),
1043 FIELD(HOST_FS_BASE, host_fs_base),
1044 FIELD(HOST_GS_BASE, host_gs_base),
1045 FIELD(HOST_TR_BASE, host_tr_base),
1046 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1047 FIELD(HOST_IDTR_BASE, host_idtr_base),
1048 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1049 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1050 FIELD(HOST_RSP, host_rsp),
1051 FIELD(HOST_RIP, host_rip),
1052};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001053
1054static inline short vmcs_field_to_offset(unsigned long field)
1055{
Dan Williams085331d2018-01-31 17:47:03 -08001056 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1057 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001058 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001059
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001060 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001061 return -ENOENT;
1062
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001063 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001064 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001065 return -ENOENT;
1066
Linus Torvalds15303ba2018-02-10 13:16:35 -08001067 index = array_index_nospec(index, size);
1068 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001069 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001070 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001071 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001072}
1073
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001074static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1075{
David Matlack4f2777b2016-07-13 17:16:37 -07001076 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001077}
1078
Peter Feiner995f00a2017-06-30 17:26:32 -07001079static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001080static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001081static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001082static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001083static void vmx_set_segment(struct kvm_vcpu *vcpu,
1084 struct kvm_segment *var, int seg);
1085static void vmx_get_segment(struct kvm_vcpu *vcpu,
1086 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001087static bool guest_state_valid(struct kvm_vcpu *vcpu);
1088static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001089static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001090static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1091static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1092static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1093 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001094static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001095static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1096 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001097
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1099static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001100/*
1101 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1102 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1103 */
1104static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105
Feng Wubf9f6ac2015-09-18 22:29:55 +08001106/*
1107 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1108 * can find which vCPU should be waken up.
1109 */
1110static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1111static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1112
Radim Krčmář23611332016-09-29 22:41:33 +02001113enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001114 VMX_VMREAD_BITMAP,
1115 VMX_VMWRITE_BITMAP,
1116 VMX_BITMAP_NR
1117};
1118
1119static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1120
Radim Krčmář23611332016-09-29 22:41:33 +02001121#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1122#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001123
Avi Kivity110312c2010-12-21 12:54:20 +02001124static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001125static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001126
Sheng Yang2384d2b2008-01-17 15:14:33 +08001127static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1128static DEFINE_SPINLOCK(vmx_vpid_lock);
1129
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001130static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 int size;
1132 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001133 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001135 u32 pin_based_exec_ctrl;
1136 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001137 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001138 u32 vmexit_ctrl;
1139 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001140 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001141} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142
Hannes Ederefff9e52008-11-28 17:02:06 +01001143static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001144 u32 ept;
1145 u32 vpid;
1146} vmx_capability;
1147
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148#define VMX_SEGMENT_FIELD(seg) \
1149 [VCPU_SREG_##seg] = { \
1150 .selector = GUEST_##seg##_SELECTOR, \
1151 .base = GUEST_##seg##_BASE, \
1152 .limit = GUEST_##seg##_LIMIT, \
1153 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1154 }
1155
Mathias Krause772e0312012-08-30 01:30:19 +02001156static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 unsigned selector;
1158 unsigned base;
1159 unsigned limit;
1160 unsigned ar_bytes;
1161} kvm_vmx_segment_fields[] = {
1162 VMX_SEGMENT_FIELD(CS),
1163 VMX_SEGMENT_FIELD(DS),
1164 VMX_SEGMENT_FIELD(ES),
1165 VMX_SEGMENT_FIELD(FS),
1166 VMX_SEGMENT_FIELD(GS),
1167 VMX_SEGMENT_FIELD(SS),
1168 VMX_SEGMENT_FIELD(TR),
1169 VMX_SEGMENT_FIELD(LDTR),
1170};
1171
Avi Kivity26bb0982009-09-07 11:14:12 +03001172static u64 host_efer;
1173
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001174static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1175
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001176/*
Brian Gerst8c065852010-07-17 09:03:26 -04001177 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001178 * away by decrementing the array size.
1179 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001181#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001182 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001184 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001187DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1188
1189#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1190
1191#define KVM_EVMCS_VERSION 1
1192
1193#if IS_ENABLED(CONFIG_HYPERV)
1194static bool __read_mostly enlightened_vmcs = true;
1195module_param(enlightened_vmcs, bool, 0444);
1196
1197static inline void evmcs_write64(unsigned long field, u64 value)
1198{
1199 u16 clean_field;
1200 int offset = get_evmcs_offset(field, &clean_field);
1201
1202 if (offset < 0)
1203 return;
1204
1205 *(u64 *)((char *)current_evmcs + offset) = value;
1206
1207 current_evmcs->hv_clean_fields &= ~clean_field;
1208}
1209
1210static inline void evmcs_write32(unsigned long field, u32 value)
1211{
1212 u16 clean_field;
1213 int offset = get_evmcs_offset(field, &clean_field);
1214
1215 if (offset < 0)
1216 return;
1217
1218 *(u32 *)((char *)current_evmcs + offset) = value;
1219 current_evmcs->hv_clean_fields &= ~clean_field;
1220}
1221
1222static inline void evmcs_write16(unsigned long field, u16 value)
1223{
1224 u16 clean_field;
1225 int offset = get_evmcs_offset(field, &clean_field);
1226
1227 if (offset < 0)
1228 return;
1229
1230 *(u16 *)((char *)current_evmcs + offset) = value;
1231 current_evmcs->hv_clean_fields &= ~clean_field;
1232}
1233
1234static inline u64 evmcs_read64(unsigned long field)
1235{
1236 int offset = get_evmcs_offset(field, NULL);
1237
1238 if (offset < 0)
1239 return 0;
1240
1241 return *(u64 *)((char *)current_evmcs + offset);
1242}
1243
1244static inline u32 evmcs_read32(unsigned long field)
1245{
1246 int offset = get_evmcs_offset(field, NULL);
1247
1248 if (offset < 0)
1249 return 0;
1250
1251 return *(u32 *)((char *)current_evmcs + offset);
1252}
1253
1254static inline u16 evmcs_read16(unsigned long field)
1255{
1256 int offset = get_evmcs_offset(field, NULL);
1257
1258 if (offset < 0)
1259 return 0;
1260
1261 return *(u16 *)((char *)current_evmcs + offset);
1262}
1263
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001264static inline void evmcs_touch_msr_bitmap(void)
1265{
1266 if (unlikely(!current_evmcs))
1267 return;
1268
1269 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1270 current_evmcs->hv_clean_fields &=
1271 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1272}
1273
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001274static void evmcs_load(u64 phys_addr)
1275{
1276 struct hv_vp_assist_page *vp_ap =
1277 hv_get_vp_assist_page(smp_processor_id());
1278
1279 vp_ap->current_nested_vmcs = phys_addr;
1280 vp_ap->enlighten_vmentry = 1;
1281}
1282
1283static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1284{
1285 /*
1286 * Enlightened VMCSv1 doesn't support these:
1287 *
1288 * POSTED_INTR_NV = 0x00000002,
1289 * GUEST_INTR_STATUS = 0x00000810,
1290 * APIC_ACCESS_ADDR = 0x00002014,
1291 * POSTED_INTR_DESC_ADDR = 0x00002016,
1292 * EOI_EXIT_BITMAP0 = 0x0000201c,
1293 * EOI_EXIT_BITMAP1 = 0x0000201e,
1294 * EOI_EXIT_BITMAP2 = 0x00002020,
1295 * EOI_EXIT_BITMAP3 = 0x00002022,
1296 */
1297 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1298 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1299 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1300 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1301 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1302 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1303 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1304
1305 /*
1306 * GUEST_PML_INDEX = 0x00000812,
1307 * PML_ADDRESS = 0x0000200e,
1308 */
1309 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1310
1311 /* VM_FUNCTION_CONTROL = 0x00002018, */
1312 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1313
1314 /*
1315 * EPTP_LIST_ADDRESS = 0x00002024,
1316 * VMREAD_BITMAP = 0x00002026,
1317 * VMWRITE_BITMAP = 0x00002028,
1318 */
1319 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1320
1321 /*
1322 * TSC_MULTIPLIER = 0x00002032,
1323 */
1324 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1325
1326 /*
1327 * PLE_GAP = 0x00004020,
1328 * PLE_WINDOW = 0x00004022,
1329 */
1330 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1331
1332 /*
1333 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1334 */
1335 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1336
1337 /*
1338 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1339 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1340 */
1341 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1342 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1343
1344 /*
1345 * Currently unsupported in KVM:
1346 * GUEST_IA32_RTIT_CTL = 0x00002814,
1347 */
1348}
1349#else /* !IS_ENABLED(CONFIG_HYPERV) */
1350static inline void evmcs_write64(unsigned long field, u64 value) {}
1351static inline void evmcs_write32(unsigned long field, u32 value) {}
1352static inline void evmcs_write16(unsigned long field, u16 value) {}
1353static inline u64 evmcs_read64(unsigned long field) { return 0; }
1354static inline u32 evmcs_read32(unsigned long field) { return 0; }
1355static inline u16 evmcs_read16(unsigned long field) { return 0; }
1356static inline void evmcs_load(u64 phys_addr) {}
1357static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001358static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001359#endif /* IS_ENABLED(CONFIG_HYPERV) */
1360
Jan Kiszka5bb16012016-02-09 20:14:21 +01001361static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362{
1363 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1364 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001365 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1366}
1367
Jan Kiszka6f054852016-02-09 20:15:18 +01001368static inline bool is_debug(u32 intr_info)
1369{
1370 return is_exception_n(intr_info, DB_VECTOR);
1371}
1372
1373static inline bool is_breakpoint(u32 intr_info)
1374{
1375 return is_exception_n(intr_info, BP_VECTOR);
1376}
1377
Jan Kiszka5bb16012016-02-09 20:14:21 +01001378static inline bool is_page_fault(u32 intr_info)
1379{
1380 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
1382
Gui Jianfeng31299942010-03-15 17:29:09 +08001383static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001384{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001385 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001386}
1387
Gui Jianfeng31299942010-03-15 17:29:09 +08001388static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001389{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001390 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001391}
1392
Liran Alon9e869482018-03-12 13:12:51 +02001393static inline bool is_gp_fault(u32 intr_info)
1394{
1395 return is_exception_n(intr_info, GP_VECTOR);
1396}
1397
Gui Jianfeng31299942010-03-15 17:29:09 +08001398static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399{
1400 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1401 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1402}
1403
Gui Jianfeng31299942010-03-15 17:29:09 +08001404static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001405{
1406 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1407 INTR_INFO_VALID_MASK)) ==
1408 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1409}
1410
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001411/* Undocumented: icebp/int1 */
1412static inline bool is_icebp(u32 intr_info)
1413{
1414 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1415 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1416}
1417
Gui Jianfeng31299942010-03-15 17:29:09 +08001418static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001419{
Sheng Yang04547152009-04-01 15:52:31 +08001420 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001421}
1422
Gui Jianfeng31299942010-03-15 17:29:09 +08001423static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001424{
Sheng Yang04547152009-04-01 15:52:31 +08001425 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001426}
1427
Paolo Bonzini35754c92015-07-29 12:05:37 +02001428static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001429{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001430 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001431}
1432
Gui Jianfeng31299942010-03-15 17:29:09 +08001433static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001434{
Sheng Yang04547152009-04-01 15:52:31 +08001435 return vmcs_config.cpu_based_exec_ctrl &
1436 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001437}
1438
Avi Kivity774ead32007-12-26 13:57:04 +02001439static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001440{
Sheng Yang04547152009-04-01 15:52:31 +08001441 return vmcs_config.cpu_based_2nd_exec_ctrl &
1442 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1443}
1444
Yang Zhang8d146952013-01-25 10:18:50 +08001445static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1446{
1447 return vmcs_config.cpu_based_2nd_exec_ctrl &
1448 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1449}
1450
Yang Zhang83d4c282013-01-25 10:18:49 +08001451static inline bool cpu_has_vmx_apic_register_virt(void)
1452{
1453 return vmcs_config.cpu_based_2nd_exec_ctrl &
1454 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1455}
1456
Yang Zhangc7c9c562013-01-25 10:18:51 +08001457static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1458{
1459 return vmcs_config.cpu_based_2nd_exec_ctrl &
1460 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1461}
1462
Yunhong Jiang64672c92016-06-13 14:19:59 -07001463/*
1464 * Comment's format: document - errata name - stepping - processor name.
1465 * Refer from
1466 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1467 */
1468static u32 vmx_preemption_cpu_tfms[] = {
1469/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
14700x000206E6,
1471/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1472/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1473/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
14740x00020652,
1475/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
14760x00020655,
1477/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1478/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1479/*
1480 * 320767.pdf - AAP86 - B1 -
1481 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1482 */
14830x000106E5,
1484/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
14850x000106A0,
1486/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
14870x000106A1,
1488/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
14890x000106A4,
1490 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1491 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1492 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
14930x000106A5,
1494};
1495
1496static inline bool cpu_has_broken_vmx_preemption_timer(void)
1497{
1498 u32 eax = cpuid_eax(0x00000001), i;
1499
1500 /* Clear the reserved bits */
1501 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001502 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001503 if (eax == vmx_preemption_cpu_tfms[i])
1504 return true;
1505
1506 return false;
1507}
1508
1509static inline bool cpu_has_vmx_preemption_timer(void)
1510{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001511 return vmcs_config.pin_based_exec_ctrl &
1512 PIN_BASED_VMX_PREEMPTION_TIMER;
1513}
1514
Yang Zhang01e439b2013-04-11 19:25:12 +08001515static inline bool cpu_has_vmx_posted_intr(void)
1516{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001517 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1518 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001519}
1520
1521static inline bool cpu_has_vmx_apicv(void)
1522{
1523 return cpu_has_vmx_apic_register_virt() &&
1524 cpu_has_vmx_virtual_intr_delivery() &&
1525 cpu_has_vmx_posted_intr();
1526}
1527
Sheng Yang04547152009-04-01 15:52:31 +08001528static inline bool cpu_has_vmx_flexpriority(void)
1529{
1530 return cpu_has_vmx_tpr_shadow() &&
1531 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001532}
1533
Marcelo Tosattie7997942009-06-11 12:07:40 -03001534static inline bool cpu_has_vmx_ept_execute_only(void)
1535{
Gui Jianfeng31299942010-03-15 17:29:09 +08001536 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001537}
1538
Marcelo Tosattie7997942009-06-11 12:07:40 -03001539static inline bool cpu_has_vmx_ept_2m_page(void)
1540{
Gui Jianfeng31299942010-03-15 17:29:09 +08001541 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001542}
1543
Sheng Yang878403b2010-01-05 19:02:29 +08001544static inline bool cpu_has_vmx_ept_1g_page(void)
1545{
Gui Jianfeng31299942010-03-15 17:29:09 +08001546 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001547}
1548
Sheng Yang4bc9b982010-06-02 14:05:24 +08001549static inline bool cpu_has_vmx_ept_4levels(void)
1550{
1551 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1552}
1553
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001554static inline bool cpu_has_vmx_ept_mt_wb(void)
1555{
1556 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1557}
1558
Yu Zhang855feb62017-08-24 20:27:55 +08001559static inline bool cpu_has_vmx_ept_5levels(void)
1560{
1561 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1562}
1563
Xudong Hao83c3a332012-05-28 19:33:35 +08001564static inline bool cpu_has_vmx_ept_ad_bits(void)
1565{
1566 return vmx_capability.ept & VMX_EPT_AD_BIT;
1567}
1568
Gui Jianfeng31299942010-03-15 17:29:09 +08001569static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001570{
Gui Jianfeng31299942010-03-15 17:29:09 +08001571 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001572}
1573
Gui Jianfeng31299942010-03-15 17:29:09 +08001574static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001575{
Gui Jianfeng31299942010-03-15 17:29:09 +08001576 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001577}
1578
Liran Aloncd9a4912018-05-22 17:16:15 +03001579static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1580{
1581 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1582}
1583
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001584static inline bool cpu_has_vmx_invvpid_single(void)
1585{
1586 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1587}
1588
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001589static inline bool cpu_has_vmx_invvpid_global(void)
1590{
1591 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1592}
1593
Wanpeng Li08d839c2017-03-23 05:30:08 -07001594static inline bool cpu_has_vmx_invvpid(void)
1595{
1596 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1597}
1598
Gui Jianfeng31299942010-03-15 17:29:09 +08001599static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001600{
Sheng Yang04547152009-04-01 15:52:31 +08001601 return vmcs_config.cpu_based_2nd_exec_ctrl &
1602 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001603}
1604
Gui Jianfeng31299942010-03-15 17:29:09 +08001605static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001606{
1607 return vmcs_config.cpu_based_2nd_exec_ctrl &
1608 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1609}
1610
Gui Jianfeng31299942010-03-15 17:29:09 +08001611static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001612{
1613 return vmcs_config.cpu_based_2nd_exec_ctrl &
1614 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1615}
1616
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001617static inline bool cpu_has_vmx_basic_inout(void)
1618{
1619 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1620}
1621
Paolo Bonzini35754c92015-07-29 12:05:37 +02001622static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001623{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001624 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001625}
1626
Gui Jianfeng31299942010-03-15 17:29:09 +08001627static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001628{
Sheng Yang04547152009-04-01 15:52:31 +08001629 return vmcs_config.cpu_based_2nd_exec_ctrl &
1630 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001631}
1632
Gui Jianfeng31299942010-03-15 17:29:09 +08001633static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001634{
1635 return vmcs_config.cpu_based_2nd_exec_ctrl &
1636 SECONDARY_EXEC_RDTSCP;
1637}
1638
Mao, Junjiead756a12012-07-02 01:18:48 +00001639static inline bool cpu_has_vmx_invpcid(void)
1640{
1641 return vmcs_config.cpu_based_2nd_exec_ctrl &
1642 SECONDARY_EXEC_ENABLE_INVPCID;
1643}
1644
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001645static inline bool cpu_has_virtual_nmis(void)
1646{
1647 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1648}
1649
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001650static inline bool cpu_has_vmx_wbinvd_exit(void)
1651{
1652 return vmcs_config.cpu_based_2nd_exec_ctrl &
1653 SECONDARY_EXEC_WBINVD_EXITING;
1654}
1655
Abel Gordonabc4fc52013-04-18 14:35:25 +03001656static inline bool cpu_has_vmx_shadow_vmcs(void)
1657{
1658 u64 vmx_msr;
1659 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1660 /* check if the cpu supports writing r/o exit information fields */
1661 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1662 return false;
1663
1664 return vmcs_config.cpu_based_2nd_exec_ctrl &
1665 SECONDARY_EXEC_SHADOW_VMCS;
1666}
1667
Kai Huang843e4332015-01-28 10:54:28 +08001668static inline bool cpu_has_vmx_pml(void)
1669{
1670 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1671}
1672
Haozhong Zhang64903d62015-10-20 15:39:09 +08001673static inline bool cpu_has_vmx_tsc_scaling(void)
1674{
1675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_TSC_SCALING;
1677}
1678
Bandan Das2a499e42017-08-03 15:54:41 -04001679static inline bool cpu_has_vmx_vmfunc(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl &
1682 SECONDARY_EXEC_ENABLE_VMFUNC;
1683}
1684
Sean Christopherson64f7a112018-04-30 10:01:06 -07001685static bool vmx_umip_emulated(void)
1686{
1687 return vmcs_config.cpu_based_2nd_exec_ctrl &
1688 SECONDARY_EXEC_DESC;
1689}
1690
Sheng Yang04547152009-04-01 15:52:31 +08001691static inline bool report_flexpriority(void)
1692{
1693 return flexpriority_enabled;
1694}
1695
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001696static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1697{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001698 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001699}
1700
Jim Mattsonf4160e42018-05-29 09:11:33 -07001701/*
1702 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1703 * to modify any valid field of the VMCS, or are the VM-exit
1704 * information fields read-only?
1705 */
1706static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1707{
1708 return to_vmx(vcpu)->nested.msrs.misc_low &
1709 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1710}
1711
Marc Orr04473782018-06-20 17:21:29 -07001712static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1713{
1714 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1715}
1716
1717static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1718{
1719 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1720 CPU_BASED_MONITOR_TRAP_FLAG;
1721}
1722
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001723static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1724{
1725 return vmcs12->cpu_based_vm_exec_control & bit;
1726}
1727
1728static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1729{
1730 return (vmcs12->cpu_based_vm_exec_control &
1731 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1732 (vmcs12->secondary_vm_exec_control & bit);
1733}
1734
Jan Kiszkaf4124502014-03-07 20:03:13 +01001735static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1736{
1737 return vmcs12->pin_based_vm_exec_control &
1738 PIN_BASED_VMX_PREEMPTION_TIMER;
1739}
1740
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001741static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1742{
1743 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1744}
1745
1746static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1747{
1748 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1749}
1750
Nadav Har'El155a97a2013-08-05 11:07:16 +03001751static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1752{
1753 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1754}
1755
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001756static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1757{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001758 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001759}
1760
Bandan Dasc5f983f2017-05-05 15:25:14 -04001761static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1762{
1763 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1764}
1765
Wincy Vanf2b93282015-02-03 23:56:03 +08001766static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1767{
1768 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1769}
1770
Wanpeng Li5c614b32015-10-13 09:18:36 -07001771static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1772{
1773 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1774}
1775
Wincy Van82f0dd42015-02-03 23:57:18 +08001776static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1777{
1778 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1779}
1780
Wincy Van608406e2015-02-03 23:57:51 +08001781static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1782{
1783 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1784}
1785
Wincy Van705699a2015-02-03 23:58:17 +08001786static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1787{
1788 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1789}
1790
Bandan Das27c42a12017-08-03 15:54:42 -04001791static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1792{
1793 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1794}
1795
Bandan Das41ab9372017-08-03 15:54:43 -04001796static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1797{
1798 return nested_cpu_has_vmfunc(vmcs12) &&
1799 (vmcs12->vm_function_control &
1800 VMX_VMFUNC_EPTP_SWITCHING);
1801}
1802
Jim Mattsonef85b672016-12-12 11:01:37 -08001803static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001804{
1805 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001806 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001807}
1808
Jan Kiszka533558b2014-01-04 18:47:20 +01001809static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1810 u32 exit_intr_info,
1811 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001812static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1813 struct vmcs12 *vmcs12,
1814 u32 reason, unsigned long qualification);
1815
Rusty Russell8b9cf982007-07-30 16:31:43 +10001816static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001817{
1818 int i;
1819
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001820 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001821 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001822 return i;
1823 return -1;
1824}
1825
Sheng Yang2384d2b2008-01-17 15:14:33 +08001826static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1827{
1828 struct {
1829 u64 vpid : 16;
1830 u64 rsvd : 48;
1831 u64 gva;
1832 } operand = { vpid, 0, gva };
1833
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001834 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001835 /* CF==1 or ZF==1 --> rc = -1 */
1836 "; ja 1f ; ud2 ; 1:"
1837 : : "a"(&operand), "c"(ext) : "cc", "memory");
1838}
1839
Sheng Yang14394422008-04-28 12:24:45 +08001840static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1841{
1842 struct {
1843 u64 eptp, gpa;
1844 } operand = {eptp, gpa};
1845
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001846 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001847 /* CF==1 or ZF==1 --> rc = -1 */
1848 "; ja 1f ; ud2 ; 1:\n"
1849 : : "a" (&operand), "c" (ext) : "cc", "memory");
1850}
1851
Avi Kivity26bb0982009-09-07 11:14:12 +03001852static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001853{
1854 int i;
1855
Rusty Russell8b9cf982007-07-30 16:31:43 +10001856 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001857 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001858 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001859 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001860}
1861
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862static void vmcs_clear(struct vmcs *vmcs)
1863{
1864 u64 phys_addr = __pa(vmcs);
1865 u8 error;
1866
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001867 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001868 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 : "cc", "memory");
1870 if (error)
1871 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1872 vmcs, phys_addr);
1873}
1874
Nadav Har'Eld462b812011-05-24 15:26:10 +03001875static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1876{
1877 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001878 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1879 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001880 loaded_vmcs->cpu = -1;
1881 loaded_vmcs->launched = 0;
1882}
1883
Dongxiao Xu7725b892010-05-11 18:29:38 +08001884static void vmcs_load(struct vmcs *vmcs)
1885{
1886 u64 phys_addr = __pa(vmcs);
1887 u8 error;
1888
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001889 if (static_branch_unlikely(&enable_evmcs))
1890 return evmcs_load(phys_addr);
1891
Dongxiao Xu7725b892010-05-11 18:29:38 +08001892 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001893 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001894 : "cc", "memory");
1895 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001896 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001897 vmcs, phys_addr);
1898}
1899
Dave Young2965faa2015-09-09 15:38:55 -07001900#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001901/*
1902 * This bitmap is used to indicate whether the vmclear
1903 * operation is enabled on all cpus. All disabled by
1904 * default.
1905 */
1906static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1907
1908static inline void crash_enable_local_vmclear(int cpu)
1909{
1910 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1911}
1912
1913static inline void crash_disable_local_vmclear(int cpu)
1914{
1915 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1916}
1917
1918static inline int crash_local_vmclear_enabled(int cpu)
1919{
1920 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1921}
1922
1923static void crash_vmclear_local_loaded_vmcss(void)
1924{
1925 int cpu = raw_smp_processor_id();
1926 struct loaded_vmcs *v;
1927
1928 if (!crash_local_vmclear_enabled(cpu))
1929 return;
1930
1931 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1932 loaded_vmcss_on_cpu_link)
1933 vmcs_clear(v->vmcs);
1934}
1935#else
1936static inline void crash_enable_local_vmclear(int cpu) { }
1937static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001938#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001939
Nadav Har'Eld462b812011-05-24 15:26:10 +03001940static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001942 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001943 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944
Nadav Har'Eld462b812011-05-24 15:26:10 +03001945 if (loaded_vmcs->cpu != cpu)
1946 return; /* vcpu migration can race with cpu offline */
1947 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001949 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001950 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001951
1952 /*
1953 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1954 * is before setting loaded_vmcs->vcpu to -1 which is done in
1955 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1956 * then adds the vmcs into percpu list before it is deleted.
1957 */
1958 smp_wmb();
1959
Nadav Har'Eld462b812011-05-24 15:26:10 +03001960 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001961 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962}
1963
Nadav Har'Eld462b812011-05-24 15:26:10 +03001964static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001965{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001966 int cpu = loaded_vmcs->cpu;
1967
1968 if (cpu != -1)
1969 smp_call_function_single(cpu,
1970 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001971}
1972
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001973static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001974{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001975 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001976 return;
1977
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001978 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001979 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001980}
1981
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001982static inline void vpid_sync_vcpu_global(void)
1983{
1984 if (cpu_has_vmx_invvpid_global())
1985 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1986}
1987
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001988static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001989{
1990 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001991 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001992 else
1993 vpid_sync_vcpu_global();
1994}
1995
Sheng Yang14394422008-04-28 12:24:45 +08001996static inline void ept_sync_global(void)
1997{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001998 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001999}
2000
2001static inline void ept_sync_context(u64 eptp)
2002{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002003 if (cpu_has_vmx_invept_context())
2004 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2005 else
2006 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002007}
2008
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002009static __always_inline void vmcs_check16(unsigned long field)
2010{
2011 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2012 "16-bit accessor invalid for 64-bit field");
2013 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2014 "16-bit accessor invalid for 64-bit high field");
2015 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2016 "16-bit accessor invalid for 32-bit high field");
2017 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2018 "16-bit accessor invalid for natural width field");
2019}
2020
2021static __always_inline void vmcs_check32(unsigned long field)
2022{
2023 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2024 "32-bit accessor invalid for 16-bit field");
2025 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2026 "32-bit accessor invalid for natural width field");
2027}
2028
2029static __always_inline void vmcs_check64(unsigned long field)
2030{
2031 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2032 "64-bit accessor invalid for 16-bit field");
2033 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2034 "64-bit accessor invalid for 64-bit high field");
2035 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2036 "64-bit accessor invalid for 32-bit field");
2037 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2038 "64-bit accessor invalid for natural width field");
2039}
2040
2041static __always_inline void vmcs_checkl(unsigned long field)
2042{
2043 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2044 "Natural width accessor invalid for 16-bit field");
2045 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2046 "Natural width accessor invalid for 64-bit field");
2047 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2048 "Natural width accessor invalid for 64-bit high field");
2049 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2050 "Natural width accessor invalid for 32-bit field");
2051}
2052
2053static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054{
Avi Kivity5e520e62011-05-15 10:13:12 -04002055 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056
Avi Kivity5e520e62011-05-15 10:13:12 -04002057 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2058 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059 return value;
2060}
2061
Avi Kivity96304212011-05-15 10:13:13 -04002062static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002064 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002065 if (static_branch_unlikely(&enable_evmcs))
2066 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002067 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068}
2069
Avi Kivity96304212011-05-15 10:13:13 -04002070static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002072 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002073 if (static_branch_unlikely(&enable_evmcs))
2074 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002075 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076}
2077
Avi Kivity96304212011-05-15 10:13:13 -04002078static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002080 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002081 if (static_branch_unlikely(&enable_evmcs))
2082 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002083#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002084 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002086 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087#endif
2088}
2089
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002090static __always_inline unsigned long vmcs_readl(unsigned long field)
2091{
2092 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002093 if (static_branch_unlikely(&enable_evmcs))
2094 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002095 return __vmcs_readl(field);
2096}
2097
Avi Kivitye52de1b2007-01-05 16:36:56 -08002098static noinline void vmwrite_error(unsigned long field, unsigned long value)
2099{
2100 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2101 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2102 dump_stack();
2103}
2104
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002105static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106{
2107 u8 error;
2108
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002109 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002110 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002111 if (unlikely(error))
2112 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113}
2114
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002115static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002117 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002118 if (static_branch_unlikely(&enable_evmcs))
2119 return evmcs_write16(field, value);
2120
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002121 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122}
2123
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002124static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002126 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002127 if (static_branch_unlikely(&enable_evmcs))
2128 return evmcs_write32(field, value);
2129
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002130 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131}
2132
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002133static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002135 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002136 if (static_branch_unlikely(&enable_evmcs))
2137 return evmcs_write64(field, value);
2138
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002139 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002140#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002142 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143#endif
2144}
2145
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002146static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002147{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002148 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002149 if (static_branch_unlikely(&enable_evmcs))
2150 return evmcs_write64(field, value);
2151
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002152 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002153}
2154
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002155static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002156{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2158 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002159 if (static_branch_unlikely(&enable_evmcs))
2160 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2161
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002162 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2163}
2164
2165static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2166{
2167 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2168 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002169 if (static_branch_unlikely(&enable_evmcs))
2170 return evmcs_write32(field, evmcs_read32(field) | mask);
2171
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002172 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002173}
2174
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002175static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2176{
2177 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2178}
2179
Gleb Natapov2961e8762013-11-25 15:37:13 +02002180static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2181{
2182 vmcs_write32(VM_ENTRY_CONTROLS, val);
2183 vmx->vm_entry_controls_shadow = val;
2184}
2185
2186static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2187{
2188 if (vmx->vm_entry_controls_shadow != val)
2189 vm_entry_controls_init(vmx, val);
2190}
2191
2192static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2193{
2194 return vmx->vm_entry_controls_shadow;
2195}
2196
2197
2198static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2199{
2200 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2201}
2202
2203static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2204{
2205 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2206}
2207
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002208static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2209{
2210 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2211}
2212
Gleb Natapov2961e8762013-11-25 15:37:13 +02002213static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2214{
2215 vmcs_write32(VM_EXIT_CONTROLS, val);
2216 vmx->vm_exit_controls_shadow = val;
2217}
2218
2219static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2220{
2221 if (vmx->vm_exit_controls_shadow != val)
2222 vm_exit_controls_init(vmx, val);
2223}
2224
2225static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2226{
2227 return vmx->vm_exit_controls_shadow;
2228}
2229
2230
2231static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2232{
2233 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2234}
2235
2236static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2237{
2238 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2239}
2240
Avi Kivity2fb92db2011-04-27 19:42:18 +03002241static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2242{
2243 vmx->segment_cache.bitmask = 0;
2244}
2245
2246static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2247 unsigned field)
2248{
2249 bool ret;
2250 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2251
2252 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2253 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2254 vmx->segment_cache.bitmask = 0;
2255 }
2256 ret = vmx->segment_cache.bitmask & mask;
2257 vmx->segment_cache.bitmask |= mask;
2258 return ret;
2259}
2260
2261static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2262{
2263 u16 *p = &vmx->segment_cache.seg[seg].selector;
2264
2265 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2266 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2267 return *p;
2268}
2269
2270static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2271{
2272 ulong *p = &vmx->segment_cache.seg[seg].base;
2273
2274 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2275 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2276 return *p;
2277}
2278
2279static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2280{
2281 u32 *p = &vmx->segment_cache.seg[seg].limit;
2282
2283 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2284 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2285 return *p;
2286}
2287
2288static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2289{
2290 u32 *p = &vmx->segment_cache.seg[seg].ar;
2291
2292 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2293 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2294 return *p;
2295}
2296
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002297static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2298{
2299 u32 eb;
2300
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002301 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002302 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002303 /*
2304 * Guest access to VMware backdoor ports could legitimately
2305 * trigger #GP because of TSS I/O permission bitmap.
2306 * We intercept those #GP and allow access to them anyway
2307 * as VMware does.
2308 */
2309 if (enable_vmware_backdoor)
2310 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002311 if ((vcpu->guest_debug &
2312 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2313 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2314 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002315 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002316 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002317 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002318 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002319
2320 /* When we are running a nested L2 guest and L1 specified for it a
2321 * certain exception bitmap, we must trap the same exceptions and pass
2322 * them to L1. When running L2, we will only handle the exceptions
2323 * specified above if L1 did not want them.
2324 */
2325 if (is_guest_mode(vcpu))
2326 eb |= get_vmcs12(vcpu)->exception_bitmap;
2327
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002328 vmcs_write32(EXCEPTION_BITMAP, eb);
2329}
2330
Ashok Raj15d45072018-02-01 22:59:43 +01002331/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002332 * Check if MSR is intercepted for currently loaded MSR bitmap.
2333 */
2334static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2335{
2336 unsigned long *msr_bitmap;
2337 int f = sizeof(unsigned long);
2338
2339 if (!cpu_has_vmx_msr_bitmap())
2340 return true;
2341
2342 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2343
2344 if (msr <= 0x1fff) {
2345 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2346 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2347 msr &= 0x1fff;
2348 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2349 }
2350
2351 return true;
2352}
2353
2354/*
Ashok Raj15d45072018-02-01 22:59:43 +01002355 * Check if MSR is intercepted for L01 MSR bitmap.
2356 */
2357static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2358{
2359 unsigned long *msr_bitmap;
2360 int f = sizeof(unsigned long);
2361
2362 if (!cpu_has_vmx_msr_bitmap())
2363 return true;
2364
2365 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2366
2367 if (msr <= 0x1fff) {
2368 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2369 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2370 msr &= 0x1fff;
2371 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2372 }
2373
2374 return true;
2375}
2376
Gleb Natapov2961e8762013-11-25 15:37:13 +02002377static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2378 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002379{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002380 vm_entry_controls_clearbit(vmx, entry);
2381 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002382}
2383
Avi Kivity61d2ef22010-04-28 16:40:38 +03002384static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2385{
2386 unsigned i;
2387 struct msr_autoload *m = &vmx->msr_autoload;
2388
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002389 switch (msr) {
2390 case MSR_EFER:
2391 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002392 clear_atomic_switch_msr_special(vmx,
2393 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002394 VM_EXIT_LOAD_IA32_EFER);
2395 return;
2396 }
2397 break;
2398 case MSR_CORE_PERF_GLOBAL_CTRL:
2399 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002400 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002401 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2402 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2403 return;
2404 }
2405 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002406 }
2407
Avi Kivity61d2ef22010-04-28 16:40:38 +03002408 for (i = 0; i < m->nr; ++i)
2409 if (m->guest[i].index == msr)
2410 break;
2411
2412 if (i == m->nr)
2413 return;
2414 --m->nr;
2415 m->guest[i] = m->guest[m->nr];
2416 m->host[i] = m->host[m->nr];
2417 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2418 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2419}
2420
Gleb Natapov2961e8762013-11-25 15:37:13 +02002421static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2422 unsigned long entry, unsigned long exit,
2423 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2424 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002425{
2426 vmcs_write64(guest_val_vmcs, guest_val);
2427 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002428 vm_entry_controls_setbit(vmx, entry);
2429 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002430}
2431
Avi Kivity61d2ef22010-04-28 16:40:38 +03002432static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2433 u64 guest_val, u64 host_val)
2434{
2435 unsigned i;
2436 struct msr_autoload *m = &vmx->msr_autoload;
2437
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002438 switch (msr) {
2439 case MSR_EFER:
2440 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002441 add_atomic_switch_msr_special(vmx,
2442 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002443 VM_EXIT_LOAD_IA32_EFER,
2444 GUEST_IA32_EFER,
2445 HOST_IA32_EFER,
2446 guest_val, host_val);
2447 return;
2448 }
2449 break;
2450 case MSR_CORE_PERF_GLOBAL_CTRL:
2451 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002452 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002453 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2454 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2455 GUEST_IA32_PERF_GLOBAL_CTRL,
2456 HOST_IA32_PERF_GLOBAL_CTRL,
2457 guest_val, host_val);
2458 return;
2459 }
2460 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002461 case MSR_IA32_PEBS_ENABLE:
2462 /* PEBS needs a quiescent period after being disabled (to write
2463 * a record). Disabling PEBS through VMX MSR swapping doesn't
2464 * provide that period, so a CPU could write host's record into
2465 * guest's memory.
2466 */
2467 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002468 }
2469
Avi Kivity61d2ef22010-04-28 16:40:38 +03002470 for (i = 0; i < m->nr; ++i)
2471 if (m->guest[i].index == msr)
2472 break;
2473
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002474 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002475 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002476 "Can't add msr %x\n", msr);
2477 return;
2478 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002479 ++m->nr;
2480 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2481 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2482 }
2483
2484 m->guest[i].index = msr;
2485 m->guest[i].value = guest_val;
2486 m->host[i].index = msr;
2487 m->host[i].value = host_val;
2488}
2489
Avi Kivity92c0d902009-10-29 11:00:16 +02002490static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002491{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002492 u64 guest_efer = vmx->vcpu.arch.efer;
2493 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002494
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002495 if (!enable_ept) {
2496 /*
2497 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2498 * host CPUID is more efficient than testing guest CPUID
2499 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2500 */
2501 if (boot_cpu_has(X86_FEATURE_SMEP))
2502 guest_efer |= EFER_NX;
2503 else if (!(guest_efer & EFER_NX))
2504 ignore_bits |= EFER_NX;
2505 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002506
Avi Kivity51c6cf62007-08-29 03:48:05 +03002507 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002508 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002509 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002510 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002511#ifdef CONFIG_X86_64
2512 ignore_bits |= EFER_LMA | EFER_LME;
2513 /* SCE is meaningful only in long mode on Intel */
2514 if (guest_efer & EFER_LMA)
2515 ignore_bits &= ~(u64)EFER_SCE;
2516#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002517
2518 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002519
2520 /*
2521 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2522 * On CPUs that support "load IA32_EFER", always switch EFER
2523 * atomically, since it's faster than switching it manually.
2524 */
2525 if (cpu_has_load_ia32_efer ||
2526 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002527 if (!(guest_efer & EFER_LMA))
2528 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002529 if (guest_efer != host_efer)
2530 add_atomic_switch_msr(vmx, MSR_EFER,
2531 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002532 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002533 } else {
2534 guest_efer &= ~ignore_bits;
2535 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002536
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002537 vmx->guest_msrs[efer_offset].data = guest_efer;
2538 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2539
2540 return true;
2541 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002542}
2543
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002544#ifdef CONFIG_X86_32
2545/*
2546 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2547 * VMCS rather than the segment table. KVM uses this helper to figure
2548 * out the current bases to poke them into the VMCS before entry.
2549 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002550static unsigned long segment_base(u16 selector)
2551{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002552 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002553 unsigned long v;
2554
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002555 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002556 return 0;
2557
Thomas Garnier45fc8752017-03-14 10:05:08 -07002558 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002559
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002560 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002561 u16 ldt_selector = kvm_read_ldt();
2562
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002563 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002564 return 0;
2565
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002566 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002567 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002568 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002569 return v;
2570}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002571#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002572
Avi Kivity04d2cc72007-09-10 18:10:54 +03002573static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002574{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002575 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002576#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002577 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002578 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002579#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002580 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002581
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002582 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002583 return;
2584
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002585 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002586 /*
2587 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2588 * allow segment selectors with cpl > 0 or ti == 1.
2589 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002590 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002591 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002592
2593#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002594 if (likely(is_64bit_mm(current->mm))) {
2595 save_fsgs_for_kvm();
2596 vmx->host_state.fs_sel = current->thread.fsindex;
2597 vmx->host_state.gs_sel = current->thread.gsindex;
2598 fs_base = current->thread.fsbase;
2599 kernel_gs_base = current->thread.gsbase;
2600 } else {
2601#endif
2602 savesegment(fs, vmx->host_state.fs_sel);
2603 savesegment(gs, vmx->host_state.gs_sel);
2604#ifdef CONFIG_X86_64
2605 fs_base = read_msr(MSR_FS_BASE);
2606 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2607 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002608#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002609 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002610 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002611 vmx->host_state.fs_reload_needed = 0;
2612 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002613 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002614 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002615 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002616 if (!(vmx->host_state.gs_sel & 7))
2617 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002618 else {
2619 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002620 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002621 }
2622
2623#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002624 savesegment(ds, vmx->host_state.ds_sel);
2625 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002626
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002627 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002628 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002629
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002630 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002631 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002632 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002633#else
2634 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2635 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2636#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002637 for (i = 0; i < vmx->save_nmsrs; ++i)
2638 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002639 vmx->guest_msrs[i].data,
2640 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002641}
2642
Avi Kivitya9b21b62008-06-24 11:48:49 +03002643static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002644{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002645 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002646 return;
2647
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002648 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002649 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002650#ifdef CONFIG_X86_64
2651 if (is_long_mode(&vmx->vcpu))
2652 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2653#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002654 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002655 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002656#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002657 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002658#else
2659 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002660#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002661 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002662 if (vmx->host_state.fs_reload_needed)
2663 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002664#ifdef CONFIG_X86_64
2665 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2666 loadsegment(ds, vmx->host_state.ds_sel);
2667 loadsegment(es, vmx->host_state.es_sel);
2668 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002669#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002670 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002671#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002672 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002673#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002674 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002675}
2676
Avi Kivitya9b21b62008-06-24 11:48:49 +03002677static void vmx_load_host_state(struct vcpu_vmx *vmx)
2678{
2679 preempt_disable();
2680 __vmx_load_host_state(vmx);
2681 preempt_enable();
2682}
2683
Feng Wu28b835d2015-09-18 22:29:54 +08002684static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2685{
2686 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2687 struct pi_desc old, new;
2688 unsigned int dest;
2689
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002690 /*
2691 * In case of hot-plug or hot-unplug, we may have to undo
2692 * vmx_vcpu_pi_put even if there is no assigned device. And we
2693 * always keep PI.NDST up to date for simplicity: it makes the
2694 * code easier, and CPU migration is not a fast path.
2695 */
2696 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002697 return;
2698
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002699 /*
2700 * First handle the simple case where no cmpxchg is necessary; just
2701 * allow posting non-urgent interrupts.
2702 *
2703 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2704 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2705 * expects the VCPU to be on the blocked_vcpu_list that matches
2706 * PI.NDST.
2707 */
2708 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2709 vcpu->cpu == cpu) {
2710 pi_clear_sn(pi_desc);
2711 return;
2712 }
2713
2714 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002715 do {
2716 old.control = new.control = pi_desc->control;
2717
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002718 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002719
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002720 if (x2apic_enabled())
2721 new.ndst = dest;
2722 else
2723 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002724
Feng Wu28b835d2015-09-18 22:29:54 +08002725 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002726 } while (cmpxchg64(&pi_desc->control, old.control,
2727 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002728}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002729
Peter Feinerc95ba922016-08-17 09:36:47 -07002730static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2731{
2732 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2733 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2734}
2735
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736/*
2737 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2738 * vcpu mutex is already taken.
2739 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002740static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002742 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002743 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002745 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002746 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002747 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002748 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002749
2750 /*
2751 * Read loaded_vmcs->cpu should be before fetching
2752 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2753 * See the comments in __loaded_vmcs_clear().
2754 */
2755 smp_rmb();
2756
Nadav Har'Eld462b812011-05-24 15:26:10 +03002757 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2758 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002759 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002760 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002761 }
2762
2763 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2764 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2765 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002766 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002767 }
2768
2769 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002770 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002771 unsigned long sysenter_esp;
2772
2773 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002774
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 /*
2776 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002777 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002779 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002780 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002781 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002783 /*
2784 * VM exits change the host TR limit to 0x67 after a VM
2785 * exit. This is okay, since 0x67 covers everything except
2786 * the IO bitmap and have have code to handle the IO bitmap
2787 * being lost after a VM exit.
2788 */
2789 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2790
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2792 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002793
Nadav Har'Eld462b812011-05-24 15:26:10 +03002794 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 }
Feng Wu28b835d2015-09-18 22:29:54 +08002796
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002797 /* Setup TSC multiplier */
2798 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002799 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2800 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002801
Feng Wu28b835d2015-09-18 22:29:54 +08002802 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002803 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002804 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002805}
2806
2807static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2808{
2809 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2810
2811 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002812 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2813 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002814 return;
2815
2816 /* Set SN when the vCPU is preempted */
2817 if (vcpu->preempted)
2818 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819}
2820
2821static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2822{
Feng Wu28b835d2015-09-18 22:29:54 +08002823 vmx_vcpu_pi_put(vcpu);
2824
Avi Kivitya9b21b62008-06-24 11:48:49 +03002825 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826}
2827
Wanpeng Lif244dee2017-07-20 01:11:54 -07002828static bool emulation_required(struct kvm_vcpu *vcpu)
2829{
2830 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2831}
2832
Avi Kivityedcafe32009-12-30 18:07:40 +02002833static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2834
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002835/*
2836 * Return the cr0 value that a nested guest would read. This is a combination
2837 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2838 * its hypervisor (cr0_read_shadow).
2839 */
2840static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2841{
2842 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2843 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2844}
2845static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2846{
2847 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2848 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2849}
2850
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2852{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002853 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002854
Avi Kivity6de12732011-03-07 12:51:22 +02002855 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2856 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2857 rflags = vmcs_readl(GUEST_RFLAGS);
2858 if (to_vmx(vcpu)->rmode.vm86_active) {
2859 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2860 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2861 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2862 }
2863 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002864 }
Avi Kivity6de12732011-03-07 12:51:22 +02002865 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866}
2867
2868static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2869{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002870 unsigned long old_rflags = vmx_get_rflags(vcpu);
2871
Avi Kivity6de12732011-03-07 12:51:22 +02002872 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2873 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002874 if (to_vmx(vcpu)->rmode.vm86_active) {
2875 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002876 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002879
2880 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2881 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002884static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002885{
2886 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2887 int ret = 0;
2888
2889 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002890 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002891 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002892 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002893
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002894 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002895}
2896
2897static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2898{
2899 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2900 u32 interruptibility = interruptibility_old;
2901
2902 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2903
Jan Kiszka48005f62010-02-19 19:38:07 +01002904 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002905 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002906 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002907 interruptibility |= GUEST_INTR_STATE_STI;
2908
2909 if ((interruptibility != interruptibility_old))
2910 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2911}
2912
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2914{
2915 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002917 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002919 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920
Glauber Costa2809f5d2009-05-12 16:21:05 -04002921 /* skipping an emulated instruction also counts */
2922 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923}
2924
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002925static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2926 unsigned long exit_qual)
2927{
2928 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2929 unsigned int nr = vcpu->arch.exception.nr;
2930 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2931
2932 if (vcpu->arch.exception.has_error_code) {
2933 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2934 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2935 }
2936
2937 if (kvm_exception_is_soft(nr))
2938 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2939 else
2940 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2941
2942 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2943 vmx_get_nmi_mask(vcpu))
2944 intr_info |= INTR_INFO_UNBLOCK_NMI;
2945
2946 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2947}
2948
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002949/*
2950 * KVM wants to inject page-faults which it got to the guest. This function
2951 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002952 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002953static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002954{
2955 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002956 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002957
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002958 if (nr == PF_VECTOR) {
2959 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002960 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002961 return 1;
2962 }
2963 /*
2964 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2965 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2966 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2967 * can be written only when inject_pending_event runs. This should be
2968 * conditional on a new capability---if the capability is disabled,
2969 * kvm_multiple_exception would write the ancillary information to
2970 * CR2 or DR6, for backwards ABI-compatibility.
2971 */
2972 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2973 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002974 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002975 return 1;
2976 }
2977 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002978 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002979 if (nr == DB_VECTOR)
2980 *exit_qual = vcpu->arch.dr6;
2981 else
2982 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002983 return 1;
2984 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002985 }
2986
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002987 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002988}
2989
Wanpeng Licaa057a2018-03-12 04:53:03 -07002990static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2991{
2992 /*
2993 * Ensure that we clear the HLT state in the VMCS. We don't need to
2994 * explicitly skip the instruction because if the HLT state is set,
2995 * then the instruction is already executing and RIP has already been
2996 * advanced.
2997 */
2998 if (kvm_hlt_in_guest(vcpu->kvm) &&
2999 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3000 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3001}
3002
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003003static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003004{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003006 unsigned nr = vcpu->arch.exception.nr;
3007 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003008 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003009 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003010
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003011 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003012 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003013 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3014 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003015
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003016 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003017 int inc_eip = 0;
3018 if (kvm_exception_is_soft(nr))
3019 inc_eip = vcpu->arch.event_exit_inst_len;
3020 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003021 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003022 return;
3023 }
3024
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003025 WARN_ON_ONCE(vmx->emulation_required);
3026
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003027 if (kvm_exception_is_soft(nr)) {
3028 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3029 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003030 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3031 } else
3032 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3033
3034 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003035
3036 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003037}
3038
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003039static bool vmx_rdtscp_supported(void)
3040{
3041 return cpu_has_vmx_rdtscp();
3042}
3043
Mao, Junjiead756a12012-07-02 01:18:48 +00003044static bool vmx_invpcid_supported(void)
3045{
3046 return cpu_has_vmx_invpcid() && enable_ept;
3047}
3048
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049/*
Eddie Donga75beee2007-05-17 18:55:15 +03003050 * Swap MSR entry in host/guest MSR entry array.
3051 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003052static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003053{
Avi Kivity26bb0982009-09-07 11:14:12 +03003054 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003055
3056 tmp = vmx->guest_msrs[to];
3057 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3058 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003059}
3060
3061/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003062 * Set up the vmcs to automatically save and restore system
3063 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3064 * mode, as fiddling with msrs is very expensive.
3065 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003066static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003067{
Avi Kivity26bb0982009-09-07 11:14:12 +03003068 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003069
Eddie Donga75beee2007-05-17 18:55:15 +03003070 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003071#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003072 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003073 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003074 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003075 move_msr_up(vmx, index, save_nmsrs++);
3076 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003077 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003078 move_msr_up(vmx, index, save_nmsrs++);
3079 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003080 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003081 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003082 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003083 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003084 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003085 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003086 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003087 * if efer.sce is enabled.
3088 */
Brian Gerst8c065852010-07-17 09:03:26 -04003089 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003090 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003091 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003092 }
Eddie Donga75beee2007-05-17 18:55:15 +03003093#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003094 index = __find_msr_index(vmx, MSR_EFER);
3095 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003096 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003097
Avi Kivity26bb0982009-09-07 11:14:12 +03003098 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003099
Yang Zhang8d146952013-01-25 10:18:50 +08003100 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003101 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003102}
3103
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003104static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003106 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003108 if (is_guest_mode(vcpu) &&
3109 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3110 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3111
3112 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113}
3114
3115/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003116 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003118static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003120 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003121 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003122 * We're here if L1 chose not to trap WRMSR to TSC. According
3123 * to the spec, this should set L1's TSC; The offset that L1
3124 * set for L2 remains unchanged, and still needs to be added
3125 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003126 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003127 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003128 /* recalculate vmcs02.TSC_OFFSET: */
3129 vmcs12 = get_vmcs12(vcpu);
3130 vmcs_write64(TSC_OFFSET, offset +
3131 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3132 vmcs12->tsc_offset : 0));
3133 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003134 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3135 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003136 vmcs_write64(TSC_OFFSET, offset);
3137 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138}
3139
Nadav Har'El801d3422011-05-25 23:02:23 +03003140/*
3141 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3142 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3143 * all guests if the "nested" module option is off, and can also be disabled
3144 * for a single guest by disabling its VMX cpuid bit.
3145 */
3146static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3147{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003148 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003149}
3150
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003152 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3153 * returned for the various VMX controls MSRs when nested VMX is enabled.
3154 * The same values should also be used to verify that vmcs12 control fields are
3155 * valid during nested entry from L1 to L2.
3156 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3157 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3158 * bit in the high half is on if the corresponding bit in the control field
3159 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003160 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003161static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003162{
Paolo Bonzini13893092018-02-26 13:40:09 +01003163 if (!nested) {
3164 memset(msrs, 0, sizeof(*msrs));
3165 return;
3166 }
3167
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003168 /*
3169 * Note that as a general rule, the high half of the MSRs (bits in
3170 * the control fields which may be 1) should be initialized by the
3171 * intersection of the underlying hardware's MSR (i.e., features which
3172 * can be supported) and the list of features we want to expose -
3173 * because they are known to be properly supported in our code.
3174 * Also, usually, the low half of the MSRs (bits which must be 1) can
3175 * be set to 0, meaning that L1 may turn off any of these bits. The
3176 * reason is that if one of these bits is necessary, it will appear
3177 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3178 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003179 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003180 * These rules have exceptions below.
3181 */
3182
3183 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003184 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003185 msrs->pinbased_ctls_low,
3186 msrs->pinbased_ctls_high);
3187 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003188 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003189 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003190 PIN_BASED_EXT_INTR_MASK |
3191 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003192 PIN_BASED_VIRTUAL_NMIS |
3193 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003194 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003195 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003196 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003197
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003198 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003199 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003200 msrs->exit_ctls_low,
3201 msrs->exit_ctls_high);
3202 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003203 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003204
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003205 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003206#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003207 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003208#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003209 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003210 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003211 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003212 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003213 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3214
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003215 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003217
Jan Kiszka2996fca2014-06-16 13:59:43 +02003218 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003219 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003220
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003221 /* entry controls */
3222 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003223 msrs->entry_ctls_low,
3224 msrs->entry_ctls_high);
3225 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003226 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003227 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003228#ifdef CONFIG_X86_64
3229 VM_ENTRY_IA32E_MODE |
3230#endif
3231 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003232 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003233 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003234 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003236
Jan Kiszka2996fca2014-06-16 13:59:43 +02003237 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003239
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003240 /* cpu-based controls */
3241 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003242 msrs->procbased_ctls_low,
3243 msrs->procbased_ctls_high);
3244 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003245 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003246 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003247 CPU_BASED_VIRTUAL_INTR_PENDING |
3248 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003249 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3250 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3251 CPU_BASED_CR3_STORE_EXITING |
3252#ifdef CONFIG_X86_64
3253 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3254#endif
3255 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003256 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3257 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3258 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3259 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003260 /*
3261 * We can allow some features even when not supported by the
3262 * hardware. For example, L1 can specify an MSR bitmap - and we
3263 * can use it to avoid exits to L1 - even when L0 runs L2
3264 * without MSR bitmaps.
3265 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003266 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003267 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003268 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003269
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003270 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003271 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003272 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3273
Paolo Bonzini80154d72017-08-24 13:55:35 +02003274 /*
3275 * secondary cpu-based controls. Do not include those that
3276 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3277 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003278 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003279 msrs->secondary_ctls_low,
3280 msrs->secondary_ctls_high);
3281 msrs->secondary_ctls_low = 0;
3282 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003283 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003284 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003285 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003286 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003287 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003288 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003289
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003290 if (enable_ept) {
3291 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003293 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003294 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003295 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003296 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003298 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003299 msrs->ept_caps &= vmx_capability.ept;
3300 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003301 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3302 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003303 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003304 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003305 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003307 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003308 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003309
Bandan Das27c42a12017-08-03 15:54:42 -04003310 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003311 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003312 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003313 /*
3314 * Advertise EPTP switching unconditionally
3315 * since we emulate it
3316 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003317 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003318 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003319 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003320 }
3321
Paolo Bonzinief697a72016-03-18 16:58:38 +01003322 /*
3323 * Old versions of KVM use the single-context version without
3324 * checking for support, so declare that it is supported even
3325 * though it is treated as global context. The alternative is
3326 * not failing the single-context invvpid, and it is worse.
3327 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003328 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003329 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003330 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003331 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003332 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003333 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003334
Radim Krčmář0790ec12015-03-17 14:02:32 +01003335 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003336 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003337 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3338
Jan Kiszkac18911a2013-03-13 16:06:41 +01003339 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003340 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003341 msrs->misc_low,
3342 msrs->misc_high);
3343 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3344 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003345 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003346 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003347 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003348 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003349
3350 /*
3351 * This MSR reports some information about VMX support. We
3352 * should return information about the VMX we emulate for the
3353 * guest, and the VMCS structure we give it - not about the
3354 * VMX support of the underlying hardware.
3355 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003356 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003357 VMCS12_REVISION |
3358 VMX_BASIC_TRUE_CTLS |
3359 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3360 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3361
3362 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003363 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003364
3365 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003366 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003367 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3368 * We picked the standard core2 setting.
3369 */
3370#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3371#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003372 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3373 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003374
3375 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003376 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3377 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003378
3379 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003380 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003381}
3382
David Matlack38991522016-11-29 18:14:08 -08003383/*
3384 * if fixed0[i] == 1: val[i] must be 1
3385 * if fixed1[i] == 0: val[i] must be 0
3386 */
3387static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3388{
3389 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003390}
3391
3392static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3393{
David Matlack38991522016-11-29 18:14:08 -08003394 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003395}
3396
3397static inline u64 vmx_control_msr(u32 low, u32 high)
3398{
3399 return low | ((u64)high << 32);
3400}
3401
David Matlack62cc6b9d2016-11-29 18:14:07 -08003402static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3403{
3404 superset &= mask;
3405 subset &= mask;
3406
3407 return (superset | subset) == superset;
3408}
3409
3410static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3411{
3412 const u64 feature_and_reserved =
3413 /* feature (except bit 48; see below) */
3414 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3415 /* reserved */
3416 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003417 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003418
3419 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3420 return -EINVAL;
3421
3422 /*
3423 * KVM does not emulate a version of VMX that constrains physical
3424 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3425 */
3426 if (data & BIT_ULL(48))
3427 return -EINVAL;
3428
3429 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3430 vmx_basic_vmcs_revision_id(data))
3431 return -EINVAL;
3432
3433 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3434 return -EINVAL;
3435
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003436 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003437 return 0;
3438}
3439
3440static int
3441vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3442{
3443 u64 supported;
3444 u32 *lowp, *highp;
3445
3446 switch (msr_index) {
3447 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003448 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3449 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003450 break;
3451 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003452 lowp = &vmx->nested.msrs.procbased_ctls_low;
3453 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003454 break;
3455 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003456 lowp = &vmx->nested.msrs.exit_ctls_low;
3457 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003458 break;
3459 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003460 lowp = &vmx->nested.msrs.entry_ctls_low;
3461 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003462 break;
3463 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003464 lowp = &vmx->nested.msrs.secondary_ctls_low;
3465 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003466 break;
3467 default:
3468 BUG();
3469 }
3470
3471 supported = vmx_control_msr(*lowp, *highp);
3472
3473 /* Check must-be-1 bits are still 1. */
3474 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3475 return -EINVAL;
3476
3477 /* Check must-be-0 bits are still 0. */
3478 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3479 return -EINVAL;
3480
3481 *lowp = data;
3482 *highp = data >> 32;
3483 return 0;
3484}
3485
3486static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3487{
3488 const u64 feature_and_reserved_bits =
3489 /* feature */
3490 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3491 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3492 /* reserved */
3493 GENMASK_ULL(13, 9) | BIT_ULL(31);
3494 u64 vmx_misc;
3495
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003496 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3497 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003498
3499 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3500 return -EINVAL;
3501
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003503 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3504 vmx_misc_preemption_timer_rate(data) !=
3505 vmx_misc_preemption_timer_rate(vmx_misc))
3506 return -EINVAL;
3507
3508 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3509 return -EINVAL;
3510
3511 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3512 return -EINVAL;
3513
3514 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3515 return -EINVAL;
3516
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003517 vmx->nested.msrs.misc_low = data;
3518 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003519
3520 /*
3521 * If L1 has read-only VM-exit information fields, use the
3522 * less permissive vmx_vmwrite_bitmap to specify write
3523 * permissions for the shadow VMCS.
3524 */
3525 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3526 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3527
David Matlack62cc6b9d2016-11-29 18:14:07 -08003528 return 0;
3529}
3530
3531static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3532{
3533 u64 vmx_ept_vpid_cap;
3534
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003535 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3536 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003537
3538 /* Every bit is either reserved or a feature bit. */
3539 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3540 return -EINVAL;
3541
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003542 vmx->nested.msrs.ept_caps = data;
3543 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003544 return 0;
3545}
3546
3547static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3548{
3549 u64 *msr;
3550
3551 switch (msr_index) {
3552 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003553 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003554 break;
3555 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003556 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003557 break;
3558 default:
3559 BUG();
3560 }
3561
3562 /*
3563 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3564 * must be 1 in the restored value.
3565 */
3566 if (!is_bitwise_subset(data, *msr, -1ULL))
3567 return -EINVAL;
3568
3569 *msr = data;
3570 return 0;
3571}
3572
3573/*
3574 * Called when userspace is restoring VMX MSRs.
3575 *
3576 * Returns 0 on success, non-0 otherwise.
3577 */
3578static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3579{
3580 struct vcpu_vmx *vmx = to_vmx(vcpu);
3581
Jim Mattsona943ac52018-05-29 09:11:32 -07003582 /*
3583 * Don't allow changes to the VMX capability MSRs while the vCPU
3584 * is in VMX operation.
3585 */
3586 if (vmx->nested.vmxon)
3587 return -EBUSY;
3588
David Matlack62cc6b9d2016-11-29 18:14:07 -08003589 switch (msr_index) {
3590 case MSR_IA32_VMX_BASIC:
3591 return vmx_restore_vmx_basic(vmx, data);
3592 case MSR_IA32_VMX_PINBASED_CTLS:
3593 case MSR_IA32_VMX_PROCBASED_CTLS:
3594 case MSR_IA32_VMX_EXIT_CTLS:
3595 case MSR_IA32_VMX_ENTRY_CTLS:
3596 /*
3597 * The "non-true" VMX capability MSRs are generated from the
3598 * "true" MSRs, so we do not support restoring them directly.
3599 *
3600 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3601 * should restore the "true" MSRs with the must-be-1 bits
3602 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3603 * DEFAULT SETTINGS".
3604 */
3605 return -EINVAL;
3606 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3607 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3608 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3609 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3610 case MSR_IA32_VMX_PROCBASED_CTLS2:
3611 return vmx_restore_control_msr(vmx, msr_index, data);
3612 case MSR_IA32_VMX_MISC:
3613 return vmx_restore_vmx_misc(vmx, data);
3614 case MSR_IA32_VMX_CR0_FIXED0:
3615 case MSR_IA32_VMX_CR4_FIXED0:
3616 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3617 case MSR_IA32_VMX_CR0_FIXED1:
3618 case MSR_IA32_VMX_CR4_FIXED1:
3619 /*
3620 * These MSRs are generated based on the vCPU's CPUID, so we
3621 * do not support restoring them directly.
3622 */
3623 return -EINVAL;
3624 case MSR_IA32_VMX_EPT_VPID_CAP:
3625 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3626 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003628 return 0;
3629 default:
3630 /*
3631 * The rest of the VMX capability MSRs do not support restore.
3632 */
3633 return -EINVAL;
3634 }
3635}
3636
Jan Kiszkacae50132014-01-04 18:47:22 +01003637/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003639{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003640 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003641 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003643 break;
3644 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3645 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003646 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->pinbased_ctls_low,
3648 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003649 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3650 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003651 break;
3652 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3653 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003654 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003655 msrs->procbased_ctls_low,
3656 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003657 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3658 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003659 break;
3660 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3661 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003662 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 msrs->exit_ctls_low,
3664 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003665 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3666 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003667 break;
3668 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3669 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003670 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003671 msrs->entry_ctls_low,
3672 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003673 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3674 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003675 break;
3676 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003677 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->misc_low,
3679 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003680 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003681 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003683 break;
3684 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003686 break;
3687 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003688 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003689 break;
3690 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003691 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003692 break;
3693 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003695 break;
3696 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003697 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003698 msrs->secondary_ctls_low,
3699 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700 break;
3701 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 *pdata = msrs->ept_caps |
3703 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003704 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003705 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003706 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003707 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003709 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003710 }
3711
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003712 return 0;
3713}
3714
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003715static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3716 uint64_t val)
3717{
3718 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3719
3720 return !(val & ~valid_bits);
3721}
3722
Tom Lendacky801e4592018-02-21 13:39:51 -06003723static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3724{
Paolo Bonzini13893092018-02-26 13:40:09 +01003725 switch (msr->index) {
3726 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3727 if (!nested)
3728 return 1;
3729 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3730 default:
3731 return 1;
3732 }
3733
3734 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003735}
3736
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003737/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003738 * Reads an msr value (of 'msr_index') into 'pdata'.
3739 * Returns 0 on success, non-0 otherwise.
3740 * Assumes vcpu_load() was already called.
3741 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003742static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003743{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003744 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003745 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003747 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003748#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003750 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003751 break;
3752 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003753 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003755 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003756 vmx_load_host_state(vmx);
3757 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003758 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003759#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003761 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003762 case MSR_IA32_SPEC_CTRL:
3763 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003764 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3765 return 1;
3766
3767 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3768 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003769 case MSR_IA32_ARCH_CAPABILITIES:
3770 if (!msr_info->host_initiated &&
3771 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3772 return 1;
3773 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3774 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003776 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777 break;
3778 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003779 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780 break;
3781 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003782 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003783 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003784 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003785 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003786 (!msr_info->host_initiated &&
3787 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003788 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003789 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003790 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003791 case MSR_IA32_MCG_EXT_CTL:
3792 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003793 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003794 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003795 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003796 msr_info->data = vcpu->arch.mcg_ext_ctl;
3797 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003798 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003799 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003800 break;
3801 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3802 if (!nested_vmx_allowed(vcpu))
3803 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003804 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3805 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003806 case MSR_IA32_XSS:
3807 if (!vmx_xsaves_supported())
3808 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003809 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003810 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003811 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003812 if (!msr_info->host_initiated &&
3813 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003814 return 1;
3815 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003817 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003818 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003819 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003820 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003821 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003822 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003823 }
3824
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825 return 0;
3826}
3827
Jan Kiszkacae50132014-01-04 18:47:22 +01003828static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3829
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830/*
3831 * Writes msr value into into the appropriate "register".
3832 * Returns 0 on success, non-0 otherwise.
3833 * Assumes vcpu_load() was already called.
3834 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003835static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003838 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003839 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003840 u32 msr_index = msr_info->index;
3841 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003842
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003844 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003845 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003846 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003847#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003849 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 vmcs_writel(GUEST_FS_BASE, data);
3851 break;
3852 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003853 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 vmcs_writel(GUEST_GS_BASE, data);
3855 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003856 case MSR_KERNEL_GS_BASE:
3857 vmx_load_host_state(vmx);
3858 vmx->msr_guest_kernel_gs_base = data;
3859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860#endif
3861 case MSR_IA32_SYSENTER_CS:
3862 vmcs_write32(GUEST_SYSENTER_CS, data);
3863 break;
3864 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003865 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 break;
3867 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003868 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003870 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003871 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003872 (!msr_info->host_initiated &&
3873 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003874 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003875 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003876 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003878 vmcs_write64(GUEST_BNDCFGS, data);
3879 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003880 case MSR_IA32_SPEC_CTRL:
3881 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003882 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3883 return 1;
3884
3885 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003886 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003887 return 1;
3888
3889 vmx->spec_ctrl = data;
3890
3891 if (!data)
3892 break;
3893
3894 /*
3895 * For non-nested:
3896 * When it's written (to non-zero) for the first time, pass
3897 * it through.
3898 *
3899 * For nested:
3900 * The handling of the MSR bitmap for L2 guests is done in
3901 * nested_vmx_merge_msr_bitmap. We should not touch the
3902 * vmcs02.msr_bitmap here since it gets completely overwritten
3903 * in the merging. We update the vmcs01 here for L1 as well
3904 * since it will end up touching the MSR anyway now.
3905 */
3906 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3907 MSR_IA32_SPEC_CTRL,
3908 MSR_TYPE_RW);
3909 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003910 case MSR_IA32_PRED_CMD:
3911 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003912 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3913 return 1;
3914
3915 if (data & ~PRED_CMD_IBPB)
3916 return 1;
3917
3918 if (!data)
3919 break;
3920
3921 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3922
3923 /*
3924 * For non-nested:
3925 * When it's written (to non-zero) for the first time, pass
3926 * it through.
3927 *
3928 * For nested:
3929 * The handling of the MSR bitmap for L2 guests is done in
3930 * nested_vmx_merge_msr_bitmap. We should not touch the
3931 * vmcs02.msr_bitmap here since it gets completely overwritten
3932 * in the merging.
3933 */
3934 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3935 MSR_TYPE_W);
3936 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003937 case MSR_IA32_ARCH_CAPABILITIES:
3938 if (!msr_info->host_initiated)
3939 return 1;
3940 vmx->arch_capabilities = data;
3941 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003942 case MSR_IA32_CR_PAT:
3943 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003944 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3945 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003946 vmcs_write64(GUEST_IA32_PAT, data);
3947 vcpu->arch.pat = data;
3948 break;
3949 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003950 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003951 break;
Will Auldba904632012-11-29 12:42:50 -08003952 case MSR_IA32_TSC_ADJUST:
3953 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003954 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003955 case MSR_IA32_MCG_EXT_CTL:
3956 if ((!msr_info->host_initiated &&
3957 !(to_vmx(vcpu)->msr_ia32_feature_control &
3958 FEATURE_CONTROL_LMCE)) ||
3959 (data & ~MCG_EXT_CTL_LMCE_EN))
3960 return 1;
3961 vcpu->arch.mcg_ext_ctl = data;
3962 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003963 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003964 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003965 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003966 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3967 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003968 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003969 if (msr_info->host_initiated && data == 0)
3970 vmx_leave_nested(vcpu);
3971 break;
3972 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003973 if (!msr_info->host_initiated)
3974 return 1; /* they are read-only */
3975 if (!nested_vmx_allowed(vcpu))
3976 return 1;
3977 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003978 case MSR_IA32_XSS:
3979 if (!vmx_xsaves_supported())
3980 return 1;
3981 /*
3982 * The only supported bit as of Skylake is bit 8, but
3983 * it is not supported on KVM.
3984 */
3985 if (data != 0)
3986 return 1;
3987 vcpu->arch.ia32_xss = data;
3988 if (vcpu->arch.ia32_xss != host_xss)
3989 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3990 vcpu->arch.ia32_xss, host_xss);
3991 else
3992 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3993 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003994 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003995 if (!msr_info->host_initiated &&
3996 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003997 return 1;
3998 /* Check reserved bit, higher 32 bits should be zero */
3999 if ((data >> 32) != 0)
4000 return 1;
4001 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004003 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004004 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004005 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004006 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004007 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4008 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004009 ret = kvm_set_shared_msr(msr->index, msr->data,
4010 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004011 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004012 if (ret)
4013 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004014 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004015 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004016 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004017 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004018 }
4019
Eddie Dong2cc51562007-05-21 07:28:09 +03004020 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021}
4022
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004023static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004025 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4026 switch (reg) {
4027 case VCPU_REGS_RSP:
4028 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4029 break;
4030 case VCPU_REGS_RIP:
4031 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4032 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004033 case VCPU_EXREG_PDPTR:
4034 if (enable_ept)
4035 ept_save_pdptrs(vcpu);
4036 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004037 default:
4038 break;
4039 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040}
4041
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042static __init int cpu_has_kvm_support(void)
4043{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004044 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004045}
4046
4047static __init int vmx_disabled_by_bios(void)
4048{
4049 u64 msr;
4050
4051 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004052 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004053 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004054 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4055 && tboot_enabled())
4056 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004057 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004058 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004059 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004060 && !tboot_enabled()) {
4061 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004062 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004063 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004064 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004065 /* launched w/o TXT and VMX disabled */
4066 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4067 && !tboot_enabled())
4068 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004069 }
4070
4071 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072}
4073
Dongxiao Xu7725b892010-05-11 18:29:38 +08004074static void kvm_cpu_vmxon(u64 addr)
4075{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004076 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004077 intel_pt_handle_vmx(1);
4078
Dongxiao Xu7725b892010-05-11 18:29:38 +08004079 asm volatile (ASM_VMX_VMXON_RAX
4080 : : "a"(&addr), "m"(addr)
4081 : "memory", "cc");
4082}
4083
Radim Krčmář13a34e02014-08-28 15:13:03 +02004084static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085{
4086 int cpu = raw_smp_processor_id();
4087 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004088 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004090 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004091 return -EBUSY;
4092
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004093 /*
4094 * This can happen if we hot-added a CPU but failed to allocate
4095 * VP assist page for it.
4096 */
4097 if (static_branch_unlikely(&enable_evmcs) &&
4098 !hv_get_vp_assist_page(cpu))
4099 return -EFAULT;
4100
Nadav Har'Eld462b812011-05-24 15:26:10 +03004101 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004102 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4103 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004104
4105 /*
4106 * Now we can enable the vmclear operation in kdump
4107 * since the loaded_vmcss_on_cpu list on this cpu
4108 * has been initialized.
4109 *
4110 * Though the cpu is not in VMX operation now, there
4111 * is no problem to enable the vmclear operation
4112 * for the loaded_vmcss_on_cpu list is empty!
4113 */
4114 crash_enable_local_vmclear(cpu);
4115
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004117
4118 test_bits = FEATURE_CONTROL_LOCKED;
4119 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4120 if (tboot_enabled())
4121 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4122
4123 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004125 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4126 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004127 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004128 if (enable_ept)
4129 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004130
4131 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132}
4133
Nadav Har'Eld462b812011-05-24 15:26:10 +03004134static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004135{
4136 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004137 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004138
Nadav Har'Eld462b812011-05-24 15:26:10 +03004139 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4140 loaded_vmcss_on_cpu_link)
4141 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004142}
4143
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004144
4145/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4146 * tricks.
4147 */
4148static void kvm_cpu_vmxoff(void)
4149{
4150 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004151
4152 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004153 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004154}
4155
Radim Krčmář13a34e02014-08-28 15:13:03 +02004156static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004158 vmclear_local_loaded_vmcss();
4159 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160}
4161
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004162static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004163 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164{
4165 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004166 u32 ctl = ctl_min | ctl_opt;
4167
4168 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4169
4170 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4171 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4172
4173 /* Ensure minimum (required) set of control bits are supported. */
4174 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004175 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004176
4177 *result = ctl;
4178 return 0;
4179}
4180
Avi Kivity110312c2010-12-21 12:54:20 +02004181static __init bool allow_1_setting(u32 msr, u32 ctl)
4182{
4183 u32 vmx_msr_low, vmx_msr_high;
4184
4185 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4186 return vmx_msr_high & ctl;
4187}
4188
Yang, Sheng002c7f72007-07-31 14:23:01 +03004189static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004190{
4191 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004192 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004193 u32 _pin_based_exec_control = 0;
4194 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004195 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004196 u32 _vmexit_control = 0;
4197 u32 _vmentry_control = 0;
4198
Paolo Bonzini13893092018-02-26 13:40:09 +01004199 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304200 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004201#ifdef CONFIG_X86_64
4202 CPU_BASED_CR8_LOAD_EXITING |
4203 CPU_BASED_CR8_STORE_EXITING |
4204#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004205 CPU_BASED_CR3_LOAD_EXITING |
4206 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004207 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004208 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004209 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004210 CPU_BASED_MWAIT_EXITING |
4211 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004212 CPU_BASED_INVLPG_EXITING |
4213 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004214
Sheng Yangf78e0e22007-10-29 09:40:42 +08004215 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004216 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004217 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004218 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4219 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004220 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004221#ifdef CONFIG_X86_64
4222 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4223 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4224 ~CPU_BASED_CR8_STORE_EXITING;
4225#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004226 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004227 min2 = 0;
4228 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004229 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004230 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004231 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004232 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004233 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004234 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004235 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004236 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004237 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004238 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004239 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004240 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004241 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004242 SECONDARY_EXEC_RDSEED_EXITING |
4243 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004244 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004245 SECONDARY_EXEC_TSC_SCALING |
4246 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004247 if (adjust_vmx_controls(min2, opt2,
4248 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004249 &_cpu_based_2nd_exec_control) < 0)
4250 return -EIO;
4251 }
4252#ifndef CONFIG_X86_64
4253 if (!(_cpu_based_2nd_exec_control &
4254 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4255 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4256#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004257
4258 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4259 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004260 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004261 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4262 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004263
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004264 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4265 &vmx_capability.ept, &vmx_capability.vpid);
4266
Sheng Yangd56f5462008-04-25 10:13:16 +08004267 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004268 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4269 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004270 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4271 CPU_BASED_CR3_STORE_EXITING |
4272 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004273 } else if (vmx_capability.ept) {
4274 vmx_capability.ept = 0;
4275 pr_warn_once("EPT CAP should not exist if not support "
4276 "1-setting enable EPT VM-execution control\n");
4277 }
4278 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4279 vmx_capability.vpid) {
4280 vmx_capability.vpid = 0;
4281 pr_warn_once("VPID CAP should not exist if not support "
4282 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004283 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004284
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004285 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004286#ifdef CONFIG_X86_64
4287 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4288#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004289 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004290 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004291 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4292 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004293 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004294
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004295 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4296 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4297 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004298 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4299 &_pin_based_exec_control) < 0)
4300 return -EIO;
4301
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004302 if (cpu_has_broken_vmx_preemption_timer())
4303 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004304 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004305 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004306 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4307
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004308 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004309 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004310 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4311 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004312 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004314 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004315
4316 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4317 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004318 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004319
4320#ifdef CONFIG_X86_64
4321 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4322 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004323 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004324#endif
4325
4326 /* Require Write-Back (WB) memory type for VMCS accesses. */
4327 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004328 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004329
Yang, Sheng002c7f72007-07-31 14:23:01 +03004330 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004331 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004332 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004333
Liran Alon2307af12018-06-29 22:59:04 +03004334 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004335
Yang, Sheng002c7f72007-07-31 14:23:01 +03004336 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4337 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004338 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004339 vmcs_conf->vmexit_ctrl = _vmexit_control;
4340 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004341
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004342 if (static_branch_unlikely(&enable_evmcs))
4343 evmcs_sanitize_exec_ctrls(vmcs_conf);
4344
Avi Kivity110312c2010-12-21 12:54:20 +02004345 cpu_has_load_ia32_efer =
4346 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4347 VM_ENTRY_LOAD_IA32_EFER)
4348 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4349 VM_EXIT_LOAD_IA32_EFER);
4350
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004351 cpu_has_load_perf_global_ctrl =
4352 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4353 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4354 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4355 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4356
4357 /*
4358 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004359 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004360 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4361 *
4362 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4363 *
4364 * AAK155 (model 26)
4365 * AAP115 (model 30)
4366 * AAT100 (model 37)
4367 * BC86,AAY89,BD102 (model 44)
4368 * BA97 (model 46)
4369 *
4370 */
4371 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4372 switch (boot_cpu_data.x86_model) {
4373 case 26:
4374 case 30:
4375 case 37:
4376 case 44:
4377 case 46:
4378 cpu_has_load_perf_global_ctrl = false;
4379 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4380 "does not work properly. Using workaround\n");
4381 break;
4382 default:
4383 break;
4384 }
4385 }
4386
Borislav Petkov782511b2016-04-04 22:25:03 +02004387 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004388 rdmsrl(MSR_IA32_XSS, host_xss);
4389
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004390 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004391}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392
4393static struct vmcs *alloc_vmcs_cpu(int cpu)
4394{
4395 int node = cpu_to_node(cpu);
4396 struct page *pages;
4397 struct vmcs *vmcs;
4398
Vlastimil Babka96db8002015-09-08 15:03:50 -07004399 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400 if (!pages)
4401 return NULL;
4402 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004403 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004404
4405 /* KVM supports Enlightened VMCS v1 only */
4406 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004407 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004408 else
Liran Alon392b2f22018-06-23 02:35:01 +03004409 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004410
Avi Kivity6aa8b732006-12-10 02:21:36 -08004411 return vmcs;
4412}
4413
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414static void free_vmcs(struct vmcs *vmcs)
4415{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004416 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004417}
4418
Nadav Har'Eld462b812011-05-24 15:26:10 +03004419/*
4420 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4421 */
4422static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4423{
4424 if (!loaded_vmcs->vmcs)
4425 return;
4426 loaded_vmcs_clear(loaded_vmcs);
4427 free_vmcs(loaded_vmcs->vmcs);
4428 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004429 if (loaded_vmcs->msr_bitmap)
4430 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004431 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004432}
4433
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004434static struct vmcs *alloc_vmcs(void)
4435{
4436 return alloc_vmcs_cpu(raw_smp_processor_id());
4437}
4438
4439static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4440{
4441 loaded_vmcs->vmcs = alloc_vmcs();
4442 if (!loaded_vmcs->vmcs)
4443 return -ENOMEM;
4444
4445 loaded_vmcs->shadow_vmcs = NULL;
4446 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004447
4448 if (cpu_has_vmx_msr_bitmap()) {
4449 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4450 if (!loaded_vmcs->msr_bitmap)
4451 goto out_vmcs;
4452 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004453
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004454 if (IS_ENABLED(CONFIG_HYPERV) &&
4455 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004456 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4457 struct hv_enlightened_vmcs *evmcs =
4458 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4459
4460 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4461 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004462 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004463 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004464
4465out_vmcs:
4466 free_loaded_vmcs(loaded_vmcs);
4467 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004468}
4469
Sam Ravnborg39959582007-06-01 00:47:13 -07004470static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471{
4472 int cpu;
4473
Zachary Amsden3230bb42009-09-29 11:38:37 -10004474 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004476 per_cpu(vmxarea, cpu) = NULL;
4477 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478}
4479
Jim Mattsond37f4262017-12-22 12:12:16 -08004480enum vmcs_field_width {
4481 VMCS_FIELD_WIDTH_U16 = 0,
4482 VMCS_FIELD_WIDTH_U64 = 1,
4483 VMCS_FIELD_WIDTH_U32 = 2,
4484 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004485};
4486
Jim Mattsond37f4262017-12-22 12:12:16 -08004487static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004488{
4489 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004490 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004491 return (field >> 13) & 0x3 ;
4492}
4493
4494static inline int vmcs_field_readonly(unsigned long field)
4495{
4496 return (((field >> 10) & 0x3) == 1);
4497}
4498
Bandan Dasfe2b2012014-04-21 15:20:14 -04004499static void init_vmcs_shadow_fields(void)
4500{
4501 int i, j;
4502
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004503 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4504 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004505 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004506 (i + 1 == max_shadow_read_only_fields ||
4507 shadow_read_only_fields[i + 1] != field + 1))
4508 pr_err("Missing field from shadow_read_only_field %x\n",
4509 field + 1);
4510
4511 clear_bit(field, vmx_vmread_bitmap);
4512#ifdef CONFIG_X86_64
4513 if (field & 1)
4514 continue;
4515#endif
4516 if (j < i)
4517 shadow_read_only_fields[j] = field;
4518 j++;
4519 }
4520 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004521
4522 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004523 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004524 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004525 (i + 1 == max_shadow_read_write_fields ||
4526 shadow_read_write_fields[i + 1] != field + 1))
4527 pr_err("Missing field from shadow_read_write_field %x\n",
4528 field + 1);
4529
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004530 /*
4531 * PML and the preemption timer can be emulated, but the
4532 * processor cannot vmwrite to fields that don't exist
4533 * on bare metal.
4534 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004535 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004536 case GUEST_PML_INDEX:
4537 if (!cpu_has_vmx_pml())
4538 continue;
4539 break;
4540 case VMX_PREEMPTION_TIMER_VALUE:
4541 if (!cpu_has_vmx_preemption_timer())
4542 continue;
4543 break;
4544 case GUEST_INTR_STATUS:
4545 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004546 continue;
4547 break;
4548 default:
4549 break;
4550 }
4551
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004552 clear_bit(field, vmx_vmwrite_bitmap);
4553 clear_bit(field, vmx_vmread_bitmap);
4554#ifdef CONFIG_X86_64
4555 if (field & 1)
4556 continue;
4557#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004558 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004559 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004560 j++;
4561 }
4562 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004563}
4564
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565static __init int alloc_kvm_area(void)
4566{
4567 int cpu;
4568
Zachary Amsden3230bb42009-09-29 11:38:37 -10004569 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570 struct vmcs *vmcs;
4571
4572 vmcs = alloc_vmcs_cpu(cpu);
4573 if (!vmcs) {
4574 free_kvm_area();
4575 return -ENOMEM;
4576 }
4577
Liran Alon2307af12018-06-29 22:59:04 +03004578 /*
4579 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4580 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4581 * revision_id reported by MSR_IA32_VMX_BASIC.
4582 *
4583 * However, even though not explictly documented by
4584 * TLFS, VMXArea passed as VMXON argument should
4585 * still be marked with revision_id reported by
4586 * physical CPU.
4587 */
4588 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004589 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004590
Avi Kivity6aa8b732006-12-10 02:21:36 -08004591 per_cpu(vmxarea, cpu) = vmcs;
4592 }
4593 return 0;
4594}
4595
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004596static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004597 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004598{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004599 if (!emulate_invalid_guest_state) {
4600 /*
4601 * CS and SS RPL should be equal during guest entry according
4602 * to VMX spec, but in reality it is not always so. Since vcpu
4603 * is in the middle of the transition from real mode to
4604 * protected mode it is safe to assume that RPL 0 is a good
4605 * default value.
4606 */
4607 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004608 save->selector &= ~SEGMENT_RPL_MASK;
4609 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004610 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004611 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004612 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613}
4614
4615static void enter_pmode(struct kvm_vcpu *vcpu)
4616{
4617 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004618 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004619
Gleb Natapovd99e4152012-12-20 16:57:45 +02004620 /*
4621 * Update real mode segment cache. It may be not up-to-date if sement
4622 * register was written while vcpu was in a guest mode.
4623 */
4624 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4625 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4626 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4627 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4628 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4629 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4630
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004631 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632
Avi Kivity2fb92db2011-04-27 19:42:18 +03004633 vmx_segment_cache_clear(vmx);
4634
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004635 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004636
4637 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004638 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4639 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004640 vmcs_writel(GUEST_RFLAGS, flags);
4641
Rusty Russell66aee912007-07-17 23:34:16 +10004642 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4643 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004644
4645 update_exception_bitmap(vcpu);
4646
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004647 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4648 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4649 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4650 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4651 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4652 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004653}
4654
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004655static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656{
Mathias Krause772e0312012-08-30 01:30:19 +02004657 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004658 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004659
Gleb Natapovd99e4152012-12-20 16:57:45 +02004660 var.dpl = 0x3;
4661 if (seg == VCPU_SREG_CS)
4662 var.type = 0x3;
4663
4664 if (!emulate_invalid_guest_state) {
4665 var.selector = var.base >> 4;
4666 var.base = var.base & 0xffff0;
4667 var.limit = 0xffff;
4668 var.g = 0;
4669 var.db = 0;
4670 var.present = 1;
4671 var.s = 1;
4672 var.l = 0;
4673 var.unusable = 0;
4674 var.type = 0x3;
4675 var.avl = 0;
4676 if (save->base & 0xf)
4677 printk_once(KERN_WARNING "kvm: segment base is not "
4678 "paragraph aligned when entering "
4679 "protected mode (seg=%d)", seg);
4680 }
4681
4682 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004683 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004684 vmcs_write32(sf->limit, var.limit);
4685 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686}
4687
4688static void enter_rmode(struct kvm_vcpu *vcpu)
4689{
4690 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004692 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004694 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4695 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4696 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4697 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4698 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004699 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4700 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004701
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004702 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004703
Gleb Natapov776e58e2011-03-13 12:34:27 +02004704 /*
4705 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004706 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004707 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004708 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004709 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4710 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004711
Avi Kivity2fb92db2011-04-27 19:42:18 +03004712 vmx_segment_cache_clear(vmx);
4713
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004714 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4717
4718 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004719 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004721 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004722
4723 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004724 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725 update_exception_bitmap(vcpu);
4726
Gleb Natapovd99e4152012-12-20 16:57:45 +02004727 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4728 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4729 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4730 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4731 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4732 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004733
Eddie Dong8668a3c2007-10-10 14:26:45 +08004734 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735}
4736
Amit Shah401d10d2009-02-20 22:53:37 +05304737static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4738{
4739 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004740 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4741
4742 if (!msr)
4743 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304744
Avi Kivity44ea2b12009-09-06 15:55:37 +03004745 /*
4746 * Force kernel_gs_base reloading before EFER changes, as control
4747 * of this msr depends on is_long_mode().
4748 */
4749 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004750 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304751 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004752 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304753 msr->data = efer;
4754 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004755 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304756
4757 msr->data = efer & ~EFER_LME;
4758 }
4759 setup_msrs(vmx);
4760}
4761
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004762#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004763
4764static void enter_lmode(struct kvm_vcpu *vcpu)
4765{
4766 u32 guest_tr_ar;
4767
Avi Kivity2fb92db2011-04-27 19:42:18 +03004768 vmx_segment_cache_clear(to_vmx(vcpu));
4769
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004771 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004772 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4773 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004775 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4776 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004777 }
Avi Kivityda38f432010-07-06 11:30:49 +03004778 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004779}
4780
4781static void exit_lmode(struct kvm_vcpu *vcpu)
4782{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004783 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004784 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785}
4786
4787#endif
4788
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004789static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4790 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004791{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004792 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004793 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4794 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004795 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004796 } else {
4797 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004798 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004799}
4800
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004801static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004802{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004803 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004804}
4805
Avi Kivitye8467fd2009-12-29 18:43:06 +02004806static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4807{
4808 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4809
4810 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4811 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4812}
4813
Avi Kivityaff48ba2010-12-05 18:56:11 +02004814static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4815{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004816 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004817 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4818 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4819}
4820
Anthony Liguori25c4c272007-04-27 09:29:21 +03004821static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004822{
Avi Kivityfc78f512009-12-07 12:16:48 +02004823 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4824
4825 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4826 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004827}
4828
Sheng Yang14394422008-04-28 12:24:45 +08004829static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4830{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004831 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4832
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004833 if (!test_bit(VCPU_EXREG_PDPTR,
4834 (unsigned long *)&vcpu->arch.regs_dirty))
4835 return;
4836
Sheng Yang14394422008-04-28 12:24:45 +08004837 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004838 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4839 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4840 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4841 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004842 }
4843}
4844
Avi Kivity8f5d5492009-05-31 18:41:29 +03004845static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4846{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004847 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4848
Avi Kivity8f5d5492009-05-31 18:41:29 +03004849 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004850 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4851 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4852 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4853 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004854 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004855
4856 __set_bit(VCPU_EXREG_PDPTR,
4857 (unsigned long *)&vcpu->arch.regs_avail);
4858 __set_bit(VCPU_EXREG_PDPTR,
4859 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004860}
4861
David Matlack38991522016-11-29 18:14:08 -08004862static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4863{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004864 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4865 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004866 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4867
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004868 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004869 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4870 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4871 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4872
4873 return fixed_bits_valid(val, fixed0, fixed1);
4874}
4875
4876static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4877{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004878 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4879 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004880
4881 return fixed_bits_valid(val, fixed0, fixed1);
4882}
4883
4884static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4885{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004886 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4887 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004888
4889 return fixed_bits_valid(val, fixed0, fixed1);
4890}
4891
4892/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4893#define nested_guest_cr4_valid nested_cr4_valid
4894#define nested_host_cr4_valid nested_cr4_valid
4895
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004896static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004897
4898static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4899 unsigned long cr0,
4900 struct kvm_vcpu *vcpu)
4901{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004902 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4903 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004904 if (!(cr0 & X86_CR0_PG)) {
4905 /* From paging/starting to nonpaging */
4906 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004907 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004908 (CPU_BASED_CR3_LOAD_EXITING |
4909 CPU_BASED_CR3_STORE_EXITING));
4910 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004911 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004912 } else if (!is_paging(vcpu)) {
4913 /* From nonpaging to paging */
4914 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004915 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004916 ~(CPU_BASED_CR3_LOAD_EXITING |
4917 CPU_BASED_CR3_STORE_EXITING));
4918 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004919 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004920 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004921
4922 if (!(cr0 & X86_CR0_WP))
4923 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004924}
4925
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4927{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004929 unsigned long hw_cr0;
4930
Gleb Natapov50378782013-02-04 16:00:28 +02004931 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004932 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004933 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004934 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004935 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004936
Gleb Natapov218e7632013-01-21 15:36:45 +02004937 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4938 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939
Gleb Natapov218e7632013-01-21 15:36:45 +02004940 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4941 enter_rmode(vcpu);
4942 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004943
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004944#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004945 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004946 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004947 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004948 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004949 exit_lmode(vcpu);
4950 }
4951#endif
4952
Sean Christophersonb4d18512018-03-05 12:04:40 -08004953 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004954 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4955
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004957 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004958 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004959
4960 /* depends on vcpu->arch.cr0 to be set to a new value */
4961 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962}
4963
Yu Zhang855feb62017-08-24 20:27:55 +08004964static int get_ept_level(struct kvm_vcpu *vcpu)
4965{
4966 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4967 return 5;
4968 return 4;
4969}
4970
Peter Feiner995f00a2017-06-30 17:26:32 -07004971static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004972{
Yu Zhang855feb62017-08-24 20:27:55 +08004973 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004974
Yu Zhang855feb62017-08-24 20:27:55 +08004975 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004976
Peter Feiner995f00a2017-06-30 17:26:32 -07004977 if (enable_ept_ad_bits &&
4978 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004979 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004980 eptp |= (root_hpa & PAGE_MASK);
4981
4982 return eptp;
4983}
4984
Avi Kivity6aa8b732006-12-10 02:21:36 -08004985static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4986{
Sheng Yang14394422008-04-28 12:24:45 +08004987 unsigned long guest_cr3;
4988 u64 eptp;
4989
4990 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004991 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004992 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004993 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004994 if (enable_unrestricted_guest || is_paging(vcpu) ||
4995 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004996 guest_cr3 = kvm_read_cr3(vcpu);
4997 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004998 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004999 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005000 }
5001
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005002 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005003 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005004}
5005
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005006static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005008 /*
5009 * Pass through host's Machine Check Enable value to hw_cr4, which
5010 * is in force while we are in guest mode. Do not let guests control
5011 * this bit, even if host CR4.MCE == 0.
5012 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005013 unsigned long hw_cr4;
5014
5015 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5016 if (enable_unrestricted_guest)
5017 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5018 else if (to_vmx(vcpu)->rmode.vm86_active)
5019 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5020 else
5021 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005022
Sean Christopherson64f7a112018-04-30 10:01:06 -07005023 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5024 if (cr4 & X86_CR4_UMIP) {
5025 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005026 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005027 hw_cr4 &= ~X86_CR4_UMIP;
5028 } else if (!is_guest_mode(vcpu) ||
5029 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5030 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5031 SECONDARY_EXEC_DESC);
5032 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005033
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005034 if (cr4 & X86_CR4_VMXE) {
5035 /*
5036 * To use VMXON (and later other VMX instructions), a guest
5037 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5038 * So basically the check on whether to allow nested VMX
5039 * is here.
5040 */
5041 if (!nested_vmx_allowed(vcpu))
5042 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005043 }
David Matlack38991522016-11-29 18:14:08 -08005044
5045 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005046 return 1;
5047
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005048 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005049
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005050 if (!enable_unrestricted_guest) {
5051 if (enable_ept) {
5052 if (!is_paging(vcpu)) {
5053 hw_cr4 &= ~X86_CR4_PAE;
5054 hw_cr4 |= X86_CR4_PSE;
5055 } else if (!(cr4 & X86_CR4_PAE)) {
5056 hw_cr4 &= ~X86_CR4_PAE;
5057 }
5058 }
5059
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005060 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005061 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5062 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5063 * to be manually disabled when guest switches to non-paging
5064 * mode.
5065 *
5066 * If !enable_unrestricted_guest, the CPU is always running
5067 * with CR0.PG=1 and CR4 needs to be modified.
5068 * If enable_unrestricted_guest, the CPU automatically
5069 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005070 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005071 if (!is_paging(vcpu))
5072 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5073 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005074
Sheng Yang14394422008-04-28 12:24:45 +08005075 vmcs_writel(CR4_READ_SHADOW, cr4);
5076 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005077 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078}
5079
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080static void vmx_get_segment(struct kvm_vcpu *vcpu,
5081 struct kvm_segment *var, int seg)
5082{
Avi Kivitya9179492011-01-03 14:28:52 +02005083 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084 u32 ar;
5085
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005086 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005087 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005088 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005089 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005090 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005091 var->base = vmx_read_guest_seg_base(vmx, seg);
5092 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5093 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005094 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005095 var->base = vmx_read_guest_seg_base(vmx, seg);
5096 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5097 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5098 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005099 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100 var->type = ar & 15;
5101 var->s = (ar >> 4) & 1;
5102 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005103 /*
5104 * Some userspaces do not preserve unusable property. Since usable
5105 * segment has to be present according to VMX spec we can use present
5106 * property to amend userspace bug by making unusable segment always
5107 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5108 * segment as unusable.
5109 */
5110 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005111 var->avl = (ar >> 12) & 1;
5112 var->l = (ar >> 13) & 1;
5113 var->db = (ar >> 14) & 1;
5114 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005115}
5116
Avi Kivitya9179492011-01-03 14:28:52 +02005117static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5118{
Avi Kivitya9179492011-01-03 14:28:52 +02005119 struct kvm_segment s;
5120
5121 if (to_vmx(vcpu)->rmode.vm86_active) {
5122 vmx_get_segment(vcpu, &s, seg);
5123 return s.base;
5124 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005125 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005126}
5127
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005128static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005129{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005130 struct vcpu_vmx *vmx = to_vmx(vcpu);
5131
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005132 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005133 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005134 else {
5135 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005136 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005137 }
Avi Kivity69c73022011-03-07 15:26:44 +02005138}
5139
Avi Kivity653e3102007-05-07 10:55:37 +03005140static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005142 u32 ar;
5143
Avi Kivityf0495f92012-06-07 17:06:10 +03005144 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005145 ar = 1 << 16;
5146 else {
5147 ar = var->type & 15;
5148 ar |= (var->s & 1) << 4;
5149 ar |= (var->dpl & 3) << 5;
5150 ar |= (var->present & 1) << 7;
5151 ar |= (var->avl & 1) << 12;
5152 ar |= (var->l & 1) << 13;
5153 ar |= (var->db & 1) << 14;
5154 ar |= (var->g & 1) << 15;
5155 }
Avi Kivity653e3102007-05-07 10:55:37 +03005156
5157 return ar;
5158}
5159
5160static void vmx_set_segment(struct kvm_vcpu *vcpu,
5161 struct kvm_segment *var, int seg)
5162{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005163 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005164 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005165
Avi Kivity2fb92db2011-04-27 19:42:18 +03005166 vmx_segment_cache_clear(vmx);
5167
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005168 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5169 vmx->rmode.segs[seg] = *var;
5170 if (seg == VCPU_SREG_TR)
5171 vmcs_write16(sf->selector, var->selector);
5172 else if (var->s)
5173 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005174 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005175 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005176
Avi Kivity653e3102007-05-07 10:55:37 +03005177 vmcs_writel(sf->base, var->base);
5178 vmcs_write32(sf->limit, var->limit);
5179 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005180
5181 /*
5182 * Fix the "Accessed" bit in AR field of segment registers for older
5183 * qemu binaries.
5184 * IA32 arch specifies that at the time of processor reset the
5185 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005186 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005187 * state vmexit when "unrestricted guest" mode is turned on.
5188 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5189 * tree. Newer qemu binaries with that qemu fix would not need this
5190 * kvm hack.
5191 */
5192 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005193 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005194
Gleb Natapovf924d662012-12-12 19:10:55 +02005195 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005196
5197out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005198 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199}
5200
Avi Kivity6aa8b732006-12-10 02:21:36 -08005201static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5202{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005203 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204
5205 *db = (ar >> 14) & 1;
5206 *l = (ar >> 13) & 1;
5207}
5208
Gleb Natapov89a27f42010-02-16 10:51:48 +02005209static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005210{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005211 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5212 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005213}
5214
Gleb Natapov89a27f42010-02-16 10:51:48 +02005215static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005216{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005217 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5218 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005219}
5220
Gleb Natapov89a27f42010-02-16 10:51:48 +02005221static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005222{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005223 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5224 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005225}
5226
Gleb Natapov89a27f42010-02-16 10:51:48 +02005227static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005229 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5230 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005231}
5232
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005233static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5234{
5235 struct kvm_segment var;
5236 u32 ar;
5237
5238 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005239 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005240 if (seg == VCPU_SREG_CS)
5241 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005242 ar = vmx_segment_access_rights(&var);
5243
5244 if (var.base != (var.selector << 4))
5245 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005246 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005247 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005248 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005249 return false;
5250
5251 return true;
5252}
5253
5254static bool code_segment_valid(struct kvm_vcpu *vcpu)
5255{
5256 struct kvm_segment cs;
5257 unsigned int cs_rpl;
5258
5259 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005260 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005261
Avi Kivity1872a3f2009-01-04 23:26:52 +02005262 if (cs.unusable)
5263 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005264 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005265 return false;
5266 if (!cs.s)
5267 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005268 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005269 if (cs.dpl > cs_rpl)
5270 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005271 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005272 if (cs.dpl != cs_rpl)
5273 return false;
5274 }
5275 if (!cs.present)
5276 return false;
5277
5278 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5279 return true;
5280}
5281
5282static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5283{
5284 struct kvm_segment ss;
5285 unsigned int ss_rpl;
5286
5287 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005288 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005289
Avi Kivity1872a3f2009-01-04 23:26:52 +02005290 if (ss.unusable)
5291 return true;
5292 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005293 return false;
5294 if (!ss.s)
5295 return false;
5296 if (ss.dpl != ss_rpl) /* DPL != RPL */
5297 return false;
5298 if (!ss.present)
5299 return false;
5300
5301 return true;
5302}
5303
5304static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5305{
5306 struct kvm_segment var;
5307 unsigned int rpl;
5308
5309 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005310 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005311
Avi Kivity1872a3f2009-01-04 23:26:52 +02005312 if (var.unusable)
5313 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005314 if (!var.s)
5315 return false;
5316 if (!var.present)
5317 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005318 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005319 if (var.dpl < rpl) /* DPL < RPL */
5320 return false;
5321 }
5322
5323 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5324 * rights flags
5325 */
5326 return true;
5327}
5328
5329static bool tr_valid(struct kvm_vcpu *vcpu)
5330{
5331 struct kvm_segment tr;
5332
5333 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5334
Avi Kivity1872a3f2009-01-04 23:26:52 +02005335 if (tr.unusable)
5336 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005337 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005338 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005339 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005340 return false;
5341 if (!tr.present)
5342 return false;
5343
5344 return true;
5345}
5346
5347static bool ldtr_valid(struct kvm_vcpu *vcpu)
5348{
5349 struct kvm_segment ldtr;
5350
5351 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5352
Avi Kivity1872a3f2009-01-04 23:26:52 +02005353 if (ldtr.unusable)
5354 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005355 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005356 return false;
5357 if (ldtr.type != 2)
5358 return false;
5359 if (!ldtr.present)
5360 return false;
5361
5362 return true;
5363}
5364
5365static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5366{
5367 struct kvm_segment cs, ss;
5368
5369 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5370 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5371
Nadav Amitb32a9912015-03-29 16:33:04 +03005372 return ((cs.selector & SEGMENT_RPL_MASK) ==
5373 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005374}
5375
5376/*
5377 * Check if guest state is valid. Returns true if valid, false if
5378 * not.
5379 * We assume that registers are always usable
5380 */
5381static bool guest_state_valid(struct kvm_vcpu *vcpu)
5382{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005383 if (enable_unrestricted_guest)
5384 return true;
5385
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005386 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005387 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005388 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5389 return false;
5390 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5391 return false;
5392 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5393 return false;
5394 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5395 return false;
5396 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5397 return false;
5398 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5399 return false;
5400 } else {
5401 /* protected mode guest state checks */
5402 if (!cs_ss_rpl_check(vcpu))
5403 return false;
5404 if (!code_segment_valid(vcpu))
5405 return false;
5406 if (!stack_segment_valid(vcpu))
5407 return false;
5408 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5409 return false;
5410 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5411 return false;
5412 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5413 return false;
5414 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5415 return false;
5416 if (!tr_valid(vcpu))
5417 return false;
5418 if (!ldtr_valid(vcpu))
5419 return false;
5420 }
5421 /* TODO:
5422 * - Add checks on RIP
5423 * - Add checks on RFLAGS
5424 */
5425
5426 return true;
5427}
5428
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005429static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5430{
5431 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5432}
5433
Mike Dayd77c26f2007-10-08 09:02:08 -04005434static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005435{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005436 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005437 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005438 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005439
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005440 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005441 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005442 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5443 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005444 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005445 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005446 r = kvm_write_guest_page(kvm, fn++, &data,
5447 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005448 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005449 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005450 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5451 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005452 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005453 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5454 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005455 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005456 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005457 r = kvm_write_guest_page(kvm, fn, &data,
5458 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5459 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005460out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005461 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005462 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005463}
5464
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005465static int init_rmode_identity_map(struct kvm *kvm)
5466{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005467 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005468 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005469 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005470 u32 tmp;
5471
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005472 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005473 mutex_lock(&kvm->slots_lock);
5474
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005475 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005476 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005477
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005478 if (!kvm_vmx->ept_identity_map_addr)
5479 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5480 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005481
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005482 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005483 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005484 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005485 goto out2;
5486
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005487 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005488 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5489 if (r < 0)
5490 goto out;
5491 /* Set up identity-mapping pagetable for EPT in real mode */
5492 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5493 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5494 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5495 r = kvm_write_guest_page(kvm, identity_map_pfn,
5496 &tmp, i * sizeof(tmp), sizeof(tmp));
5497 if (r < 0)
5498 goto out;
5499 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005500 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005501
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005502out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005503 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005504
5505out2:
5506 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005507 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005508}
5509
Avi Kivity6aa8b732006-12-10 02:21:36 -08005510static void seg_setup(int seg)
5511{
Mathias Krause772e0312012-08-30 01:30:19 +02005512 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005513 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005514
5515 vmcs_write16(sf->selector, 0);
5516 vmcs_writel(sf->base, 0);
5517 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005518 ar = 0x93;
5519 if (seg == VCPU_SREG_CS)
5520 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005521
5522 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005523}
5524
Sheng Yangf78e0e22007-10-29 09:40:42 +08005525static int alloc_apic_access_page(struct kvm *kvm)
5526{
Xiao Guangrong44841412012-09-07 14:14:20 +08005527 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005528 int r = 0;
5529
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005530 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005531 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005532 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005533 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5534 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005535 if (r)
5536 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005537
Tang Chen73a6d942014-09-11 13:38:00 +08005538 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005539 if (is_error_page(page)) {
5540 r = -EFAULT;
5541 goto out;
5542 }
5543
Tang Chenc24ae0d2014-09-24 15:57:58 +08005544 /*
5545 * Do not pin the page in memory, so that memory hot-unplug
5546 * is able to migrate it.
5547 */
5548 put_page(page);
5549 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005550out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005551 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005552 return r;
5553}
5554
Wanpeng Li991e7a02015-09-16 17:30:05 +08005555static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005556{
5557 int vpid;
5558
Avi Kivity919818a2009-03-23 18:01:29 +02005559 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005560 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005561 spin_lock(&vmx_vpid_lock);
5562 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005563 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005564 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005565 else
5566 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005567 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005568 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005569}
5570
Wanpeng Li991e7a02015-09-16 17:30:05 +08005571static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005572{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005573 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005574 return;
5575 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005576 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005577 spin_unlock(&vmx_vpid_lock);
5578}
5579
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005580static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5581 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005582{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005583 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005584
5585 if (!cpu_has_vmx_msr_bitmap())
5586 return;
5587
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005588 if (static_branch_unlikely(&enable_evmcs))
5589 evmcs_touch_msr_bitmap();
5590
Sheng Yang25c5f222008-03-28 13:18:56 +08005591 /*
5592 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5593 * have the write-low and read-high bitmap offsets the wrong way round.
5594 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5595 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005596 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005597 if (type & MSR_TYPE_R)
5598 /* read-low */
5599 __clear_bit(msr, msr_bitmap + 0x000 / f);
5600
5601 if (type & MSR_TYPE_W)
5602 /* write-low */
5603 __clear_bit(msr, msr_bitmap + 0x800 / f);
5604
Sheng Yang25c5f222008-03-28 13:18:56 +08005605 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5606 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005607 if (type & MSR_TYPE_R)
5608 /* read-high */
5609 __clear_bit(msr, msr_bitmap + 0x400 / f);
5610
5611 if (type & MSR_TYPE_W)
5612 /* write-high */
5613 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5614
5615 }
5616}
5617
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005618static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5619 u32 msr, int type)
5620{
5621 int f = sizeof(unsigned long);
5622
5623 if (!cpu_has_vmx_msr_bitmap())
5624 return;
5625
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005626 if (static_branch_unlikely(&enable_evmcs))
5627 evmcs_touch_msr_bitmap();
5628
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005629 /*
5630 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5631 * have the write-low and read-high bitmap offsets the wrong way round.
5632 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5633 */
5634 if (msr <= 0x1fff) {
5635 if (type & MSR_TYPE_R)
5636 /* read-low */
5637 __set_bit(msr, msr_bitmap + 0x000 / f);
5638
5639 if (type & MSR_TYPE_W)
5640 /* write-low */
5641 __set_bit(msr, msr_bitmap + 0x800 / f);
5642
5643 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5644 msr &= 0x1fff;
5645 if (type & MSR_TYPE_R)
5646 /* read-high */
5647 __set_bit(msr, msr_bitmap + 0x400 / f);
5648
5649 if (type & MSR_TYPE_W)
5650 /* write-high */
5651 __set_bit(msr, msr_bitmap + 0xc00 / f);
5652
5653 }
5654}
5655
5656static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5657 u32 msr, int type, bool value)
5658{
5659 if (value)
5660 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5661 else
5662 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5663}
5664
Wincy Vanf2b93282015-02-03 23:56:03 +08005665/*
5666 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5667 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5668 */
5669static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5670 unsigned long *msr_bitmap_nested,
5671 u32 msr, int type)
5672{
5673 int f = sizeof(unsigned long);
5674
Wincy Vanf2b93282015-02-03 23:56:03 +08005675 /*
5676 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5677 * have the write-low and read-high bitmap offsets the wrong way round.
5678 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5679 */
5680 if (msr <= 0x1fff) {
5681 if (type & MSR_TYPE_R &&
5682 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5683 /* read-low */
5684 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5685
5686 if (type & MSR_TYPE_W &&
5687 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5688 /* write-low */
5689 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5690
5691 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5692 msr &= 0x1fff;
5693 if (type & MSR_TYPE_R &&
5694 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5695 /* read-high */
5696 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5697
5698 if (type & MSR_TYPE_W &&
5699 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5700 /* write-high */
5701 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5702
5703 }
5704}
5705
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005706static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005707{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005708 u8 mode = 0;
5709
5710 if (cpu_has_secondary_exec_ctrls() &&
5711 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5712 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5713 mode |= MSR_BITMAP_MODE_X2APIC;
5714 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5715 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5716 }
5717
5718 if (is_long_mode(vcpu))
5719 mode |= MSR_BITMAP_MODE_LM;
5720
5721 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005722}
5723
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005724#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5725
5726static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5727 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005728{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005729 int msr;
5730
5731 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5732 unsigned word = msr / BITS_PER_LONG;
5733 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5734 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005735 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005736
5737 if (mode & MSR_BITMAP_MODE_X2APIC) {
5738 /*
5739 * TPR reads and writes can be virtualized even if virtual interrupt
5740 * delivery is not in use.
5741 */
5742 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5743 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5744 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5745 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5746 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5747 }
5748 }
5749}
5750
5751static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5752{
5753 struct vcpu_vmx *vmx = to_vmx(vcpu);
5754 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5755 u8 mode = vmx_msr_bitmap_mode(vcpu);
5756 u8 changed = mode ^ vmx->msr_bitmap_mode;
5757
5758 if (!changed)
5759 return;
5760
5761 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5762 !(mode & MSR_BITMAP_MODE_LM));
5763
5764 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5765 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5766
5767 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005768}
5769
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005770static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005771{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005772 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005773}
5774
David Matlackc9f04402017-08-01 14:00:40 -07005775static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5776{
5777 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5778 gfn_t gfn;
5779
5780 /*
5781 * Don't need to mark the APIC access page dirty; it is never
5782 * written to by the CPU during APIC virtualization.
5783 */
5784
5785 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5786 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5787 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5788 }
5789
5790 if (nested_cpu_has_posted_intr(vmcs12)) {
5791 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5792 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5793 }
5794}
5795
5796
David Hildenbrand6342c502017-01-25 11:58:58 +01005797static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005798{
5799 struct vcpu_vmx *vmx = to_vmx(vcpu);
5800 int max_irr;
5801 void *vapic_page;
5802 u16 status;
5803
David Matlackc9f04402017-08-01 14:00:40 -07005804 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5805 return;
Wincy Van705699a2015-02-03 23:58:17 +08005806
David Matlackc9f04402017-08-01 14:00:40 -07005807 vmx->nested.pi_pending = false;
5808 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5809 return;
Wincy Van705699a2015-02-03 23:58:17 +08005810
David Matlackc9f04402017-08-01 14:00:40 -07005811 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5812 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005813 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005814 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5815 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005816 kunmap(vmx->nested.virtual_apic_page);
5817
5818 status = vmcs_read16(GUEST_INTR_STATUS);
5819 if ((u8)max_irr > ((u8)status & 0xff)) {
5820 status &= ~0xff;
5821 status |= (u8)max_irr;
5822 vmcs_write16(GUEST_INTR_STATUS, status);
5823 }
5824 }
David Matlackc9f04402017-08-01 14:00:40 -07005825
5826 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005827}
5828
Wincy Van06a55242017-04-28 13:13:59 +08005829static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5830 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005831{
5832#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005833 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5834
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005835 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005836 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005837 * The vector of interrupt to be delivered to vcpu had
5838 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005839 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005840 * Following cases will be reached in this block, and
5841 * we always send a notification event in all cases as
5842 * explained below.
5843 *
5844 * Case 1: vcpu keeps in non-root mode. Sending a
5845 * notification event posts the interrupt to vcpu.
5846 *
5847 * Case 2: vcpu exits to root mode and is still
5848 * runnable. PIR will be synced to vIRR before the
5849 * next vcpu entry. Sending a notification event in
5850 * this case has no effect, as vcpu is not in root
5851 * mode.
5852 *
5853 * Case 3: vcpu exits to root mode and is blocked.
5854 * vcpu_block() has already synced PIR to vIRR and
5855 * never blocks vcpu if vIRR is not cleared. Therefore,
5856 * a blocked vcpu here does not wait for any requested
5857 * interrupts in PIR, and sending a notification event
5858 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005859 */
Feng Wu28b835d2015-09-18 22:29:54 +08005860
Wincy Van06a55242017-04-28 13:13:59 +08005861 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005862 return true;
5863 }
5864#endif
5865 return false;
5866}
5867
Wincy Van705699a2015-02-03 23:58:17 +08005868static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5869 int vector)
5870{
5871 struct vcpu_vmx *vmx = to_vmx(vcpu);
5872
5873 if (is_guest_mode(vcpu) &&
5874 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005875 /*
5876 * If a posted intr is not recognized by hardware,
5877 * we will accomplish it in the next vmentry.
5878 */
5879 vmx->nested.pi_pending = true;
5880 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005881 /* the PIR and ON have been set by L1. */
5882 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5883 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005884 return 0;
5885 }
5886 return -1;
5887}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005888/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005889 * Send interrupt to vcpu via posted interrupt way.
5890 * 1. If target vcpu is running(non-root mode), send posted interrupt
5891 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5892 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5893 * interrupt from PIR in next vmentry.
5894 */
5895static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5896{
5897 struct vcpu_vmx *vmx = to_vmx(vcpu);
5898 int r;
5899
Wincy Van705699a2015-02-03 23:58:17 +08005900 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5901 if (!r)
5902 return;
5903
Yang Zhanga20ed542013-04-11 19:25:15 +08005904 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5905 return;
5906
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005907 /* If a previous notification has sent the IPI, nothing to do. */
5908 if (pi_test_and_set_on(&vmx->pi_desc))
5909 return;
5910
Wincy Van06a55242017-04-28 13:13:59 +08005911 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005912 kvm_vcpu_kick(vcpu);
5913}
5914
Avi Kivity6aa8b732006-12-10 02:21:36 -08005915/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005916 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5917 * will not change in the lifetime of the guest.
5918 * Note that host-state that does change is set elsewhere. E.g., host-state
5919 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5920 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005921static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005922{
5923 u32 low32, high32;
5924 unsigned long tmpl;
5925 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005926 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005927
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005928 cr0 = read_cr0();
5929 WARN_ON(cr0 & X86_CR0_TS);
5930 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005931
5932 /*
5933 * Save the most likely value for this task's CR3 in the VMCS.
5934 * We can't use __get_current_cr3_fast() because we're not atomic.
5935 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005936 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005937 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005938 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005939
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005940 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005941 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005942 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005943 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005944
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005945 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005946#ifdef CONFIG_X86_64
5947 /*
5948 * Load null selectors, so we can avoid reloading them in
5949 * __vmx_load_host_state(), in case userspace uses the null selectors
5950 * too (the expected case).
5951 */
5952 vmcs_write16(HOST_DS_SELECTOR, 0);
5953 vmcs_write16(HOST_ES_SELECTOR, 0);
5954#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005955 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5956 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005957#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005958 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5959 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5960
Juergen Gross87930012017-09-04 12:25:27 +02005961 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005962 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005963 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005964
Avi Kivity83287ea422012-09-16 15:10:57 +03005965 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005966
5967 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5968 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5969 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5970 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5971
5972 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5973 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5974 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5975 }
5976}
5977
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005978static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5979{
5980 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5981 if (enable_ept)
5982 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005983 if (is_guest_mode(&vmx->vcpu))
5984 vmx->vcpu.arch.cr4_guest_owned_bits &=
5985 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005986 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5987}
5988
Yang Zhang01e439b2013-04-11 19:25:12 +08005989static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5990{
5991 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5992
Andrey Smetanind62caab2015-11-10 15:36:33 +03005993 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005994 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005995
5996 if (!enable_vnmi)
5997 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5998
Yunhong Jiang64672c92016-06-13 14:19:59 -07005999 /* Enable the preemption timer dynamically */
6000 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006001 return pin_based_exec_ctrl;
6002}
6003
Andrey Smetanind62caab2015-11-10 15:36:33 +03006004static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6005{
6006 struct vcpu_vmx *vmx = to_vmx(vcpu);
6007
6008 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006009 if (cpu_has_secondary_exec_ctrls()) {
6010 if (kvm_vcpu_apicv_active(vcpu))
6011 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6012 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6013 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6014 else
6015 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6016 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6017 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6018 }
6019
6020 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006021 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006022}
6023
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006024static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6025{
6026 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006027
6028 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6029 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6030
Paolo Bonzini35754c92015-07-29 12:05:37 +02006031 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006032 exec_control &= ~CPU_BASED_TPR_SHADOW;
6033#ifdef CONFIG_X86_64
6034 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6035 CPU_BASED_CR8_LOAD_EXITING;
6036#endif
6037 }
6038 if (!enable_ept)
6039 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6040 CPU_BASED_CR3_LOAD_EXITING |
6041 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006042 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6043 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6044 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006045 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6046 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006047 return exec_control;
6048}
6049
Jim Mattson45ec3682017-08-23 16:32:04 -07006050static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006051{
Jim Mattson45ec3682017-08-23 16:32:04 -07006052 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006053 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006054}
6055
Jim Mattson75f4fc82017-08-23 16:32:03 -07006056static bool vmx_rdseed_supported(void)
6057{
6058 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006059 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006060}
6061
Paolo Bonzini80154d72017-08-24 13:55:35 +02006062static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006063{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006064 struct kvm_vcpu *vcpu = &vmx->vcpu;
6065
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006066 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006067
Paolo Bonzini80154d72017-08-24 13:55:35 +02006068 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006069 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6070 if (vmx->vpid == 0)
6071 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6072 if (!enable_ept) {
6073 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6074 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006075 /* Enable INVPCID for non-ept guests may cause performance regression. */
6076 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006077 }
6078 if (!enable_unrestricted_guest)
6079 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006080 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006081 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006082 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006083 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6084 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006085 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006086
6087 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6088 * in vmx_set_cr4. */
6089 exec_control &= ~SECONDARY_EXEC_DESC;
6090
Abel Gordonabc4fc52013-04-18 14:35:25 +03006091 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6092 (handle_vmptrld).
6093 We can NOT enable shadow_vmcs here because we don't have yet
6094 a current VMCS12
6095 */
6096 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006097
6098 if (!enable_pml)
6099 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006100
Paolo Bonzini3db13482017-08-24 14:48:03 +02006101 if (vmx_xsaves_supported()) {
6102 /* Exposing XSAVES only when XSAVE is exposed */
6103 bool xsaves_enabled =
6104 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6105 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6106
6107 if (!xsaves_enabled)
6108 exec_control &= ~SECONDARY_EXEC_XSAVES;
6109
6110 if (nested) {
6111 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006112 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006113 SECONDARY_EXEC_XSAVES;
6114 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006115 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006116 ~SECONDARY_EXEC_XSAVES;
6117 }
6118 }
6119
Paolo Bonzini80154d72017-08-24 13:55:35 +02006120 if (vmx_rdtscp_supported()) {
6121 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6122 if (!rdtscp_enabled)
6123 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6124
6125 if (nested) {
6126 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006127 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006128 SECONDARY_EXEC_RDTSCP;
6129 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006130 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006131 ~SECONDARY_EXEC_RDTSCP;
6132 }
6133 }
6134
6135 if (vmx_invpcid_supported()) {
6136 /* Exposing INVPCID only when PCID is exposed */
6137 bool invpcid_enabled =
6138 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6139 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6140
6141 if (!invpcid_enabled) {
6142 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6143 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6144 }
6145
6146 if (nested) {
6147 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006148 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006149 SECONDARY_EXEC_ENABLE_INVPCID;
6150 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006151 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006152 ~SECONDARY_EXEC_ENABLE_INVPCID;
6153 }
6154 }
6155
Jim Mattson45ec3682017-08-23 16:32:04 -07006156 if (vmx_rdrand_supported()) {
6157 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6158 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006159 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006160
6161 if (nested) {
6162 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006163 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006164 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006165 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006166 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006167 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006168 }
6169 }
6170
Jim Mattson75f4fc82017-08-23 16:32:03 -07006171 if (vmx_rdseed_supported()) {
6172 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6173 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006174 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006175
6176 if (nested) {
6177 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006178 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006179 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006180 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006181 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006182 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006183 }
6184 }
6185
Paolo Bonzini80154d72017-08-24 13:55:35 +02006186 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006187}
6188
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006189static void ept_set_mmio_spte_mask(void)
6190{
6191 /*
6192 * EPT Misconfigurations can be generated if the value of bits 2:0
6193 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006194 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006195 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6196 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006197}
6198
Wanpeng Lif53cd632014-12-02 19:14:58 +08006199#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006200/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006201 * Sets up the vmcs for emulated real mode.
6202 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006203static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006204{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006205#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006206 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006207#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006208 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006209
Abel Gordon4607c2d2013-04-18 14:35:55 +03006210 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006211 /*
6212 * At vCPU creation, "VMWRITE to any supported field
6213 * in the VMCS" is supported, so use the more
6214 * permissive vmx_vmread_bitmap to specify both read
6215 * and write permissions for the shadow VMCS.
6216 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006217 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006218 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006219 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006220 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006221 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006222
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6224
Avi Kivity6aa8b732006-12-10 02:21:36 -08006225 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006226 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006227 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006228
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006229 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006230
Dan Williamsdfa169b2016-06-02 11:17:24 -07006231 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006232 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006233 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006234 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006235 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006236
Andrey Smetanind62caab2015-11-10 15:36:33 +03006237 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006238 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6239 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6240 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6241 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6242
6243 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006244
Li RongQing0bcf2612015-12-03 13:29:34 +08006245 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006246 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006247 }
6248
Wanpeng Lib31c1142018-03-12 04:53:04 -07006249 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006250 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006251 vmx->ple_window = ple_window;
6252 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006253 }
6254
Xiao Guangrongc3707952011-07-12 03:28:04 +08006255 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6256 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006257 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6258
Avi Kivity9581d442010-10-19 16:46:55 +02006259 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6260 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006261 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006262#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006263 rdmsrl(MSR_FS_BASE, a);
6264 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6265 rdmsrl(MSR_GS_BASE, a);
6266 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6267#else
6268 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6269 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6270#endif
6271
Bandan Das2a499e42017-08-03 15:54:41 -04006272 if (cpu_has_vmx_vmfunc())
6273 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6274
Eddie Dong2cc51562007-05-21 07:28:09 +03006275 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6276 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006277 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006278 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006279 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006280
Radim Krčmář74545702015-04-27 15:11:25 +02006281 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6282 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006283
Paolo Bonzini03916db2014-07-24 14:21:57 +02006284 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006285 u32 index = vmx_msr_index[i];
6286 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006287 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006288
6289 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6290 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006291 if (wrmsr_safe(index, data_low, data_high) < 0)
6292 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006293 vmx->guest_msrs[j].index = i;
6294 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006295 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006296 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006298
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006299 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6300 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006301
6302 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006303
6304 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006305 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006306
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006307 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6308 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6309
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006310 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006311
Wanpeng Lif53cd632014-12-02 19:14:58 +08006312 if (vmx_xsaves_supported())
6313 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6314
Peter Feiner4e595162016-07-07 14:49:58 -07006315 if (enable_pml) {
6316 ASSERT(vmx->pml_pg);
6317 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6318 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6319 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006320}
6321
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006322static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006323{
6324 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006325 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006326 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006327
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006328 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006329 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006330
Wanpeng Li518e7b92018-02-28 14:03:31 +08006331 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006332 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006333 kvm_set_cr8(vcpu, 0);
6334
6335 if (!init_event) {
6336 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6337 MSR_IA32_APICBASE_ENABLE;
6338 if (kvm_vcpu_is_reset_bsp(vcpu))
6339 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6340 apic_base_msr.host_initiated = true;
6341 kvm_set_apic_base(vcpu, &apic_base_msr);
6342 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006343
Avi Kivity2fb92db2011-04-27 19:42:18 +03006344 vmx_segment_cache_clear(vmx);
6345
Avi Kivity5706be02008-08-20 15:07:31 +03006346 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006347 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006348 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006349
6350 seg_setup(VCPU_SREG_DS);
6351 seg_setup(VCPU_SREG_ES);
6352 seg_setup(VCPU_SREG_FS);
6353 seg_setup(VCPU_SREG_GS);
6354 seg_setup(VCPU_SREG_SS);
6355
6356 vmcs_write16(GUEST_TR_SELECTOR, 0);
6357 vmcs_writel(GUEST_TR_BASE, 0);
6358 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6359 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6360
6361 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6362 vmcs_writel(GUEST_LDTR_BASE, 0);
6363 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6364 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6365
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006366 if (!init_event) {
6367 vmcs_write32(GUEST_SYSENTER_CS, 0);
6368 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6369 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6370 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6371 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006372
Wanpeng Lic37c2872017-11-20 14:52:21 -08006373 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006374 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006375
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006376 vmcs_writel(GUEST_GDTR_BASE, 0);
6377 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6378
6379 vmcs_writel(GUEST_IDTR_BASE, 0);
6380 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6381
Anthony Liguori443381a2010-12-06 10:53:38 -06006382 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006383 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006384 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006385 if (kvm_mpx_supported())
6386 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006387
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006388 setup_msrs(vmx);
6389
Avi Kivity6aa8b732006-12-10 02:21:36 -08006390 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6391
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006392 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006393 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006394 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006395 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006396 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006397 vmcs_write32(TPR_THRESHOLD, 0);
6398 }
6399
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006400 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006401
Sheng Yang2384d2b2008-01-17 15:14:33 +08006402 if (vmx->vpid != 0)
6403 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6404
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006405 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006406 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006407 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006408 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006409 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006410
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006411 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006413 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006414 if (init_event)
6415 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416}
6417
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006418/*
6419 * In nested virtualization, check if L1 asked to exit on external interrupts.
6420 * For most existing hypervisors, this will always return true.
6421 */
6422static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6423{
6424 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6425 PIN_BASED_EXT_INTR_MASK;
6426}
6427
Bandan Das77b0f5d2014-04-19 18:17:45 -04006428/*
6429 * In nested virtualization, check if L1 has set
6430 * VM_EXIT_ACK_INTR_ON_EXIT
6431 */
6432static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6433{
6434 return get_vmcs12(vcpu)->vm_exit_controls &
6435 VM_EXIT_ACK_INTR_ON_EXIT;
6436}
6437
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006438static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6439{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006440 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006441}
6442
Jan Kiszkac9a79532014-03-07 20:03:15 +01006443static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006444{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006445 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6446 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006447}
6448
Jan Kiszkac9a79532014-03-07 20:03:15 +01006449static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006450{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006451 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006452 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006453 enable_irq_window(vcpu);
6454 return;
6455 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006456
Paolo Bonzini47c01522016-12-19 11:44:07 +01006457 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6458 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006459}
6460
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006461static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006462{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006463 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006464 uint32_t intr;
6465 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006466
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006467 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006468
Avi Kivityfa89a812008-09-01 15:57:51 +03006469 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006470 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006471 int inc_eip = 0;
6472 if (vcpu->arch.interrupt.soft)
6473 inc_eip = vcpu->arch.event_exit_inst_len;
6474 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006475 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006476 return;
6477 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006478 intr = irq | INTR_INFO_VALID_MASK;
6479 if (vcpu->arch.interrupt.soft) {
6480 intr |= INTR_TYPE_SOFT_INTR;
6481 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6482 vmx->vcpu.arch.event_exit_inst_len);
6483 } else
6484 intr |= INTR_TYPE_EXT_INTR;
6485 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006486
6487 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006488}
6489
Sheng Yangf08864b2008-05-15 18:23:25 +08006490static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6491{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006492 struct vcpu_vmx *vmx = to_vmx(vcpu);
6493
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006494 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006495 /*
6496 * Tracking the NMI-blocked state in software is built upon
6497 * finding the next open IRQ window. This, in turn, depends on
6498 * well-behaving guests: They have to keep IRQs disabled at
6499 * least as long as the NMI handler runs. Otherwise we may
6500 * cause NMI nesting, maybe breaking the guest. But as this is
6501 * highly unlikely, we can live with the residual risk.
6502 */
6503 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6504 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6505 }
6506
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006507 ++vcpu->stat.nmi_injections;
6508 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006509
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006510 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006511 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006512 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006513 return;
6514 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006515
Sheng Yangf08864b2008-05-15 18:23:25 +08006516 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6517 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006518
6519 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006520}
6521
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006522static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6523{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006524 struct vcpu_vmx *vmx = to_vmx(vcpu);
6525 bool masked;
6526
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006527 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006528 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006529 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006530 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006531 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6532 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6533 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006534}
6535
6536static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6537{
6538 struct vcpu_vmx *vmx = to_vmx(vcpu);
6539
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006540 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006541 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6542 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6543 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6544 }
6545 } else {
6546 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6547 if (masked)
6548 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6549 GUEST_INTR_STATE_NMI);
6550 else
6551 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6552 GUEST_INTR_STATE_NMI);
6553 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006554}
6555
Jan Kiszka2505dc92013-04-14 12:12:47 +02006556static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6557{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006558 if (to_vmx(vcpu)->nested.nested_run_pending)
6559 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006560
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006561 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006562 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6563 return 0;
6564
Jan Kiszka2505dc92013-04-14 12:12:47 +02006565 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6566 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6567 | GUEST_INTR_STATE_NMI));
6568}
6569
Gleb Natapov78646122009-03-23 12:12:11 +02006570static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6571{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006572 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6573 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006574 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6575 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006576}
6577
Izik Eiduscbc94022007-10-25 00:29:55 +02006578static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6579{
6580 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006581
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006582 if (enable_unrestricted_guest)
6583 return 0;
6584
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006585 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6586 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006587 if (ret)
6588 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006589 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006590 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006591}
6592
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006593static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6594{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006595 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006596 return 0;
6597}
6598
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006599static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006600{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006601 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006602 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006603 /*
6604 * Update instruction length as we may reinject the exception
6605 * from user space while in guest debugging mode.
6606 */
6607 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6608 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006609 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006610 return false;
6611 /* fall through */
6612 case DB_VECTOR:
6613 if (vcpu->guest_debug &
6614 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6615 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006616 /* fall through */
6617 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006618 case OF_VECTOR:
6619 case BR_VECTOR:
6620 case UD_VECTOR:
6621 case DF_VECTOR:
6622 case SS_VECTOR:
6623 case GP_VECTOR:
6624 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006625 return true;
6626 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006627 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006628 return false;
6629}
6630
6631static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6632 int vec, u32 err_code)
6633{
6634 /*
6635 * Instruction with address size override prefix opcode 0x67
6636 * Cause the #SS fault with 0 error code in VM86 mode.
6637 */
6638 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6639 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6640 if (vcpu->arch.halt_request) {
6641 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006642 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006643 }
6644 return 1;
6645 }
6646 return 0;
6647 }
6648
6649 /*
6650 * Forward all other exceptions that are valid in real mode.
6651 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6652 * the required debugging infrastructure rework.
6653 */
6654 kvm_queue_exception(vcpu, vec);
6655 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006656}
6657
Andi Kleena0861c02009-06-08 17:37:09 +08006658/*
6659 * Trigger machine check on the host. We assume all the MSRs are already set up
6660 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6661 * We pass a fake environment to the machine check handler because we want
6662 * the guest to be always treated like user space, no matter what context
6663 * it used internally.
6664 */
6665static void kvm_machine_check(void)
6666{
6667#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6668 struct pt_regs regs = {
6669 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6670 .flags = X86_EFLAGS_IF,
6671 };
6672
6673 do_machine_check(&regs, 0);
6674#endif
6675}
6676
Avi Kivity851ba692009-08-24 11:10:17 +03006677static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006678{
6679 /* already handled by vcpu_run */
6680 return 1;
6681}
6682
Avi Kivity851ba692009-08-24 11:10:17 +03006683static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006684{
Avi Kivity1155f762007-11-22 11:30:47 +02006685 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006686 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006687 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006688 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006689 u32 vect_info;
6690 enum emulation_result er;
6691
Avi Kivity1155f762007-11-22 11:30:47 +02006692 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006693 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006694
Andi Kleena0861c02009-06-08 17:37:09 +08006695 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006696 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006697
Jim Mattsonef85b672016-12-12 11:01:37 -08006698 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006699 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006700
Wanpeng Li082d06e2018-04-03 16:28:48 -07006701 if (is_invalid_opcode(intr_info))
6702 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006703
Avi Kivity6aa8b732006-12-10 02:21:36 -08006704 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006705 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006706 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006707
Liran Alon9e869482018-03-12 13:12:51 +02006708 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6709 WARN_ON_ONCE(!enable_vmware_backdoor);
6710 er = emulate_instruction(vcpu,
6711 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6712 if (er == EMULATE_USER_EXIT)
6713 return 0;
6714 else if (er != EMULATE_DONE)
6715 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6716 return 1;
6717 }
6718
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006719 /*
6720 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6721 * MMIO, it is better to report an internal error.
6722 * See the comments in vmx_handle_exit.
6723 */
6724 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6725 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6726 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6727 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006728 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006729 vcpu->run->internal.data[0] = vect_info;
6730 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006731 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006732 return 0;
6733 }
6734
Avi Kivity6aa8b732006-12-10 02:21:36 -08006735 if (is_page_fault(intr_info)) {
6736 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006737 /* EPT won't cause page fault directly */
6738 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006739 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006740 }
6741
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006742 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006743
6744 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6745 return handle_rmode_exception(vcpu, ex_no, error_code);
6746
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006747 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006748 case AC_VECTOR:
6749 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6750 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006751 case DB_VECTOR:
6752 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6753 if (!(vcpu->guest_debug &
6754 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006755 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006756 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006757 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006758 skip_emulated_instruction(vcpu);
6759
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006760 kvm_queue_exception(vcpu, DB_VECTOR);
6761 return 1;
6762 }
6763 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6764 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6765 /* fall through */
6766 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006767 /*
6768 * Update instruction length as we may reinject #BP from
6769 * user space while in guest debugging mode. Reading it for
6770 * #DB as well causes no harm, it is not used in that case.
6771 */
6772 vmx->vcpu.arch.event_exit_inst_len =
6773 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006774 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006775 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006776 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6777 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006778 break;
6779 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006780 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6781 kvm_run->ex.exception = ex_no;
6782 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006783 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006784 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006785 return 0;
6786}
6787
Avi Kivity851ba692009-08-24 11:10:17 +03006788static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006789{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006790 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006791 return 1;
6792}
6793
Avi Kivity851ba692009-08-24 11:10:17 +03006794static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006795{
Avi Kivity851ba692009-08-24 11:10:17 +03006796 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006797 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006798 return 0;
6799}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800
Avi Kivity851ba692009-08-24 11:10:17 +03006801static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006802{
He, Qingbfdaab02007-09-12 14:18:28 +08006803 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006804 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006805 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006806
He, Qingbfdaab02007-09-12 14:18:28 +08006807 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006808 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006809
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006810 ++vcpu->stat.io_exits;
6811
Sean Christopherson432baf62018-03-08 08:57:26 -08006812 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006813 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006814
6815 port = exit_qualification >> 16;
6816 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006817 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006818
Sean Christophersondca7f122018-03-08 08:57:27 -08006819 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820}
6821
Ingo Molnar102d8322007-02-19 14:37:47 +02006822static void
6823vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6824{
6825 /*
6826 * Patch in the VMCALL instruction:
6827 */
6828 hypercall[0] = 0x0f;
6829 hypercall[1] = 0x01;
6830 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006831}
6832
Guo Chao0fa06072012-06-28 15:16:19 +08006833/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006834static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6835{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006836 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006837 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6838 unsigned long orig_val = val;
6839
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006840 /*
6841 * We get here when L2 changed cr0 in a way that did not change
6842 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006843 * but did change L0 shadowed bits. So we first calculate the
6844 * effective cr0 value that L1 would like to write into the
6845 * hardware. It consists of the L2-owned bits from the new
6846 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006847 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006848 val = (val & ~vmcs12->cr0_guest_host_mask) |
6849 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6850
David Matlack38991522016-11-29 18:14:08 -08006851 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006852 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006853
6854 if (kvm_set_cr0(vcpu, val))
6855 return 1;
6856 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006857 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006858 } else {
6859 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006860 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006861 return 1;
David Matlack38991522016-11-29 18:14:08 -08006862
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006863 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006864 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006865}
6866
6867static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6868{
6869 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006870 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6871 unsigned long orig_val = val;
6872
6873 /* analogously to handle_set_cr0 */
6874 val = (val & ~vmcs12->cr4_guest_host_mask) |
6875 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6876 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006877 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006878 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006879 return 0;
6880 } else
6881 return kvm_set_cr4(vcpu, val);
6882}
6883
Paolo Bonzini0367f202016-07-12 10:44:55 +02006884static int handle_desc(struct kvm_vcpu *vcpu)
6885{
6886 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6887 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6888}
6889
Avi Kivity851ba692009-08-24 11:10:17 +03006890static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006891{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006892 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893 int cr;
6894 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006895 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006896 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006897
He, Qingbfdaab02007-09-12 14:18:28 +08006898 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006899 cr = exit_qualification & 15;
6900 reg = (exit_qualification >> 8) & 15;
6901 switch ((exit_qualification >> 4) & 3) {
6902 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006903 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006904 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006905 switch (cr) {
6906 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006907 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006908 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006909 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006910 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006911 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006912 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006914 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006915 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006916 case 8: {
6917 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006918 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006919 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006920 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006921 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006922 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006923 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006924 return ret;
6925 /*
6926 * TODO: we might be squashing a
6927 * KVM_GUESTDBG_SINGLESTEP-triggered
6928 * KVM_EXIT_DEBUG here.
6929 */
Avi Kivity851ba692009-08-24 11:10:17 +03006930 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006931 return 0;
6932 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006933 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006934 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006935 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006936 WARN_ONCE(1, "Guest should always own CR0.TS");
6937 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006938 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006939 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006940 case 1: /*mov from cr*/
6941 switch (cr) {
6942 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006943 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006944 val = kvm_read_cr3(vcpu);
6945 kvm_register_write(vcpu, reg, val);
6946 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006947 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006948 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006949 val = kvm_get_cr8(vcpu);
6950 kvm_register_write(vcpu, reg, val);
6951 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006952 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006953 }
6954 break;
6955 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006956 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006957 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006958 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006959
Kyle Huey6affcbe2016-11-29 12:40:40 -08006960 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006961 default:
6962 break;
6963 }
Avi Kivity851ba692009-08-24 11:10:17 +03006964 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006965 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006966 (int)(exit_qualification >> 4) & 3, cr);
6967 return 0;
6968}
6969
Avi Kivity851ba692009-08-24 11:10:17 +03006970static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006971{
He, Qingbfdaab02007-09-12 14:18:28 +08006972 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006973 int dr, dr7, reg;
6974
6975 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6976 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6977
6978 /* First, if DR does not exist, trigger UD */
6979 if (!kvm_require_dr(vcpu, dr))
6980 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006981
Jan Kiszkaf2483412010-01-20 18:20:20 +01006982 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006983 if (!kvm_require_cpl(vcpu, 0))
6984 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006985 dr7 = vmcs_readl(GUEST_DR7);
6986 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006987 /*
6988 * As the vm-exit takes precedence over the debug trap, we
6989 * need to emulate the latter, either for the host or the
6990 * guest debugging itself.
6991 */
6992 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006993 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006994 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006995 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006996 vcpu->run->debug.arch.exception = DB_VECTOR;
6997 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006998 return 0;
6999 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007000 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007001 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007002 kvm_queue_exception(vcpu, DB_VECTOR);
7003 return 1;
7004 }
7005 }
7006
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007007 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007008 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7009 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007010
7011 /*
7012 * No more DR vmexits; force a reload of the debug registers
7013 * and reenter on this instruction. The next vmexit will
7014 * retrieve the full state of the debug registers.
7015 */
7016 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7017 return 1;
7018 }
7019
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007020 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7021 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007022 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007023
7024 if (kvm_get_dr(vcpu, dr, &val))
7025 return 1;
7026 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007027 } else
Nadav Amit57773922014-06-18 17:19:23 +03007028 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007029 return 1;
7030
Kyle Huey6affcbe2016-11-29 12:40:40 -08007031 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007032}
7033
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007034static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7035{
7036 return vcpu->arch.dr6;
7037}
7038
7039static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7040{
7041}
7042
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007043static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7044{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007045 get_debugreg(vcpu->arch.db[0], 0);
7046 get_debugreg(vcpu->arch.db[1], 1);
7047 get_debugreg(vcpu->arch.db[2], 2);
7048 get_debugreg(vcpu->arch.db[3], 3);
7049 get_debugreg(vcpu->arch.dr6, 6);
7050 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7051
7052 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007053 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007054}
7055
Gleb Natapov020df072010-04-13 10:05:23 +03007056static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7057{
7058 vmcs_writel(GUEST_DR7, val);
7059}
7060
Avi Kivity851ba692009-08-24 11:10:17 +03007061static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007062{
Kyle Huey6a908b62016-11-29 12:40:37 -08007063 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064}
7065
Avi Kivity851ba692009-08-24 11:10:17 +03007066static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007067{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007068 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007069 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007070
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007071 msr_info.index = ecx;
7072 msr_info.host_initiated = false;
7073 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007074 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007075 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007076 return 1;
7077 }
7078
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007079 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007080
Avi Kivity6aa8b732006-12-10 02:21:36 -08007081 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007082 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7083 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007084 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007085}
7086
Avi Kivity851ba692009-08-24 11:10:17 +03007087static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007088{
Will Auld8fe8ab42012-11-29 12:42:12 -08007089 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007090 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7091 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7092 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093
Will Auld8fe8ab42012-11-29 12:42:12 -08007094 msr.data = data;
7095 msr.index = ecx;
7096 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007097 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007098 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007099 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007100 return 1;
7101 }
7102
Avi Kivity59200272010-01-25 19:47:02 +02007103 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007104 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007105}
7106
Avi Kivity851ba692009-08-24 11:10:17 +03007107static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007108{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007109 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007110 return 1;
7111}
7112
Avi Kivity851ba692009-08-24 11:10:17 +03007113static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007114{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007115 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7116 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007117
Avi Kivity3842d132010-07-27 12:30:24 +03007118 kvm_make_request(KVM_REQ_EVENT, vcpu);
7119
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007120 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007121 return 1;
7122}
7123
Avi Kivity851ba692009-08-24 11:10:17 +03007124static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007125{
Avi Kivityd3bef152007-06-05 15:53:05 +03007126 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127}
7128
Avi Kivity851ba692009-08-24 11:10:17 +03007129static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007130{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007131 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007132}
7133
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007134static int handle_invd(struct kvm_vcpu *vcpu)
7135{
Andre Przywara51d8b662010-12-21 11:12:02 +01007136 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007137}
7138
Avi Kivity851ba692009-08-24 11:10:17 +03007139static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007140{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007141 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007142
7143 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007144 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007145}
7146
Avi Kivityfee84b02011-11-10 14:57:25 +02007147static int handle_rdpmc(struct kvm_vcpu *vcpu)
7148{
7149 int err;
7150
7151 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007152 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007153}
7154
Avi Kivity851ba692009-08-24 11:10:17 +03007155static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007156{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007157 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007158}
7159
Dexuan Cui2acf9232010-06-10 11:27:12 +08007160static int handle_xsetbv(struct kvm_vcpu *vcpu)
7161{
7162 u64 new_bv = kvm_read_edx_eax(vcpu);
7163 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7164
7165 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007166 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007167 return 1;
7168}
7169
Wanpeng Lif53cd632014-12-02 19:14:58 +08007170static int handle_xsaves(struct kvm_vcpu *vcpu)
7171{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007172 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007173 WARN(1, "this should never happen\n");
7174 return 1;
7175}
7176
7177static int handle_xrstors(struct kvm_vcpu *vcpu)
7178{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007179 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007180 WARN(1, "this should never happen\n");
7181 return 1;
7182}
7183
Avi Kivity851ba692009-08-24 11:10:17 +03007184static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007185{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007186 if (likely(fasteoi)) {
7187 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7188 int access_type, offset;
7189
7190 access_type = exit_qualification & APIC_ACCESS_TYPE;
7191 offset = exit_qualification & APIC_ACCESS_OFFSET;
7192 /*
7193 * Sane guest uses MOV to write EOI, with written value
7194 * not cared. So make a short-circuit here by avoiding
7195 * heavy instruction emulation.
7196 */
7197 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7198 (offset == APIC_EOI)) {
7199 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007200 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007201 }
7202 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007203 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007204}
7205
Yang Zhangc7c9c562013-01-25 10:18:51 +08007206static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7207{
7208 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7209 int vector = exit_qualification & 0xff;
7210
7211 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7212 kvm_apic_set_eoi_accelerated(vcpu, vector);
7213 return 1;
7214}
7215
Yang Zhang83d4c282013-01-25 10:18:49 +08007216static int handle_apic_write(struct kvm_vcpu *vcpu)
7217{
7218 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7219 u32 offset = exit_qualification & 0xfff;
7220
7221 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7222 kvm_apic_write_nodecode(vcpu, offset);
7223 return 1;
7224}
7225
Avi Kivity851ba692009-08-24 11:10:17 +03007226static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007227{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007228 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007229 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007230 bool has_error_code = false;
7231 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007232 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007233 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007234
7235 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007236 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007237 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007238
7239 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7240
7241 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007242 if (reason == TASK_SWITCH_GATE && idt_v) {
7243 switch (type) {
7244 case INTR_TYPE_NMI_INTR:
7245 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007246 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007247 break;
7248 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007249 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007250 kvm_clear_interrupt_queue(vcpu);
7251 break;
7252 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007253 if (vmx->idt_vectoring_info &
7254 VECTORING_INFO_DELIVER_CODE_MASK) {
7255 has_error_code = true;
7256 error_code =
7257 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7258 }
7259 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007260 case INTR_TYPE_SOFT_EXCEPTION:
7261 kvm_clear_exception_queue(vcpu);
7262 break;
7263 default:
7264 break;
7265 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007266 }
Izik Eidus37817f22008-03-24 23:14:53 +02007267 tss_selector = exit_qualification;
7268
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007269 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7270 type != INTR_TYPE_EXT_INTR &&
7271 type != INTR_TYPE_NMI_INTR))
7272 skip_emulated_instruction(vcpu);
7273
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007274 if (kvm_task_switch(vcpu, tss_selector,
7275 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7276 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007277 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7278 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7279 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007280 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007281 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007282
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007283 /*
7284 * TODO: What about debug traps on tss switch?
7285 * Are we supposed to inject them and update dr6?
7286 */
7287
7288 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007289}
7290
Avi Kivity851ba692009-08-24 11:10:17 +03007291static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007292{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007293 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007294 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007295 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007296
Sheng Yangf9c617f2009-03-25 10:08:52 +08007297 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007298
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007299 /*
7300 * EPT violation happened while executing iret from NMI,
7301 * "blocked by NMI" bit has to be set before next VM entry.
7302 * There are errata that may cause this bit to not be set:
7303 * AAK134, BY25.
7304 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007305 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007306 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007307 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007308 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7309
Sheng Yang14394422008-04-28 12:24:45 +08007310 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007311 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007312
Junaid Shahid27959a42016-12-06 16:46:10 -08007313 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007314 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007315 ? PFERR_USER_MASK : 0;
7316 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007317 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007318 ? PFERR_WRITE_MASK : 0;
7319 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007320 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007321 ? PFERR_FETCH_MASK : 0;
7322 /* ept page table entry is present? */
7323 error_code |= (exit_qualification &
7324 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7325 EPT_VIOLATION_EXECUTABLE))
7326 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007327
Paolo Bonzinieebed242016-11-28 14:39:58 +01007328 error_code |= (exit_qualification & 0x100) != 0 ?
7329 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007330
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007331 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007332 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007333}
7334
Avi Kivity851ba692009-08-24 11:10:17 +03007335static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007336{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007337 gpa_t gpa;
7338
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007339 /*
7340 * A nested guest cannot optimize MMIO vmexits, because we have an
7341 * nGPA here instead of the required GPA.
7342 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007343 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007344 if (!is_guest_mode(vcpu) &&
7345 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007346 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007347 /*
7348 * Doing kvm_skip_emulated_instruction() depends on undefined
7349 * behavior: Intel's manual doesn't mandate
7350 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7351 * occurs and while on real hardware it was observed to be set,
7352 * other hypervisors (namely Hyper-V) don't set it, we end up
7353 * advancing IP with some random value. Disable fast mmio when
7354 * running nested and keep it for real hardware in hope that
7355 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7356 */
7357 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7358 return kvm_skip_emulated_instruction(vcpu);
7359 else
7360 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7361 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007362 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007363
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007364 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007365}
7366
Avi Kivity851ba692009-08-24 11:10:17 +03007367static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007368{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007369 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007370 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7371 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007372 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007373 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007374
7375 return 1;
7376}
7377
Mohammed Gamal80ced182009-09-01 12:48:18 +02007378static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007379{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007380 struct vcpu_vmx *vmx = to_vmx(vcpu);
7381 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007382 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007383 u32 cpu_exec_ctrl;
7384 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007385 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007386
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007387 /*
7388 * We should never reach the point where we are emulating L2
7389 * due to invalid guest state as that means we incorrectly
7390 * allowed a nested VMEntry with an invalid vmcs12.
7391 */
7392 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7393
Avi Kivity49e9d552010-09-19 14:34:08 +02007394 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7395 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007396
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007397 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007398 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007399 return handle_interrupt_window(&vmx->vcpu);
7400
Radim Krčmář72875d82017-04-26 22:32:19 +02007401 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007402 return 1;
7403
Liran Alon9b8ae632017-11-05 16:56:34 +02007404 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007405
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007406 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007407 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007408 ret = 0;
7409 goto out;
7410 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007411
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007412 if (err != EMULATE_DONE)
7413 goto emulation_error;
7414
7415 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7416 vcpu->arch.exception.pending)
7417 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007418
Gleb Natapov8d76c492013-05-08 18:38:44 +03007419 if (vcpu->arch.halt_request) {
7420 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007421 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007422 goto out;
7423 }
7424
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007425 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007426 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007427 if (need_resched())
7428 schedule();
7429 }
7430
Mohammed Gamal80ced182009-09-01 12:48:18 +02007431out:
7432 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007433
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007434emulation_error:
7435 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7436 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7437 vcpu->run->internal.ndata = 0;
7438 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007439}
7440
7441static void grow_ple_window(struct kvm_vcpu *vcpu)
7442{
7443 struct vcpu_vmx *vmx = to_vmx(vcpu);
7444 int old = vmx->ple_window;
7445
Babu Mogerc8e88712018-03-16 16:37:24 -04007446 vmx->ple_window = __grow_ple_window(old, ple_window,
7447 ple_window_grow,
7448 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007449
7450 if (vmx->ple_window != old)
7451 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007452
7453 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007454}
7455
7456static void shrink_ple_window(struct kvm_vcpu *vcpu)
7457{
7458 struct vcpu_vmx *vmx = to_vmx(vcpu);
7459 int old = vmx->ple_window;
7460
Babu Mogerc8e88712018-03-16 16:37:24 -04007461 vmx->ple_window = __shrink_ple_window(old, ple_window,
7462 ple_window_shrink,
7463 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007464
7465 if (vmx->ple_window != old)
7466 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007467
7468 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007469}
7470
7471/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007472 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7473 */
7474static void wakeup_handler(void)
7475{
7476 struct kvm_vcpu *vcpu;
7477 int cpu = smp_processor_id();
7478
7479 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7480 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7481 blocked_vcpu_list) {
7482 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7483
7484 if (pi_test_on(pi_desc) == 1)
7485 kvm_vcpu_kick(vcpu);
7486 }
7487 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7488}
7489
Peng Haoe01bca22018-04-07 05:47:32 +08007490static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007491{
7492 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7493 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7494 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7495 0ull, VMX_EPT_EXECUTABLE_MASK,
7496 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007497 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007498
7499 ept_set_mmio_spte_mask();
7500 kvm_enable_tdp();
7501}
7502
Tiejun Chenf2c76482014-10-28 10:14:47 +08007503static __init int hardware_setup(void)
7504{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007505 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007506 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007507
7508 rdmsrl_safe(MSR_EFER, &host_efer);
7509
7510 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7511 kvm_define_shared_msr(i, vmx_msr_index[i]);
7512
Radim Krčmář23611332016-09-29 22:41:33 +02007513 for (i = 0; i < VMX_BITMAP_NR; i++) {
7514 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7515 if (!vmx_bitmap[i])
7516 goto out;
7517 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007518
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007519 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7520 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7521
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007522 if (setup_vmcs_config(&vmcs_config) < 0) {
7523 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007524 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007525 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007526
7527 if (boot_cpu_has(X86_FEATURE_NX))
7528 kvm_enable_efer_bits(EFER_NX);
7529
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007530 if (boot_cpu_has(X86_FEATURE_MPX)) {
7531 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7532 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7533 }
7534
Wanpeng Li08d839c2017-03-23 05:30:08 -07007535 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7536 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007537 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007538
Tiejun Chenf2c76482014-10-28 10:14:47 +08007539 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007540 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007541 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007542 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007543 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007544
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007545 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007546 enable_ept_ad_bits = 0;
7547
Wanpeng Li8ad81822017-10-09 15:51:53 -07007548 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007549 enable_unrestricted_guest = 0;
7550
Paolo Bonziniad15a292015-01-30 16:18:49 +01007551 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007552 flexpriority_enabled = 0;
7553
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007554 if (!cpu_has_virtual_nmis())
7555 enable_vnmi = 0;
7556
Paolo Bonziniad15a292015-01-30 16:18:49 +01007557 /*
7558 * set_apic_access_page_addr() is used to reload apic access
7559 * page upon invalidation. No need to do anything if not
7560 * using the APIC_ACCESS_ADDR VMCS field.
7561 */
7562 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007563 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007564
7565 if (!cpu_has_vmx_tpr_shadow())
7566 kvm_x86_ops->update_cr8_intercept = NULL;
7567
7568 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7569 kvm_disable_largepages();
7570
Wanpeng Li0f107682017-09-28 18:06:24 -07007571 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007572 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007573 ple_window = 0;
7574 ple_window_grow = 0;
7575 ple_window_max = 0;
7576 ple_window_shrink = 0;
7577 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007578
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007579 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007580 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007581 kvm_x86_ops->sync_pir_to_irr = NULL;
7582 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007583
Haozhong Zhang64903d62015-10-20 15:39:09 +08007584 if (cpu_has_vmx_tsc_scaling()) {
7585 kvm_has_tsc_control = true;
7586 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7587 kvm_tsc_scaling_ratio_frac_bits = 48;
7588 }
7589
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007590 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7591
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007592 if (enable_ept)
7593 vmx_enable_tdp();
7594 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007595 kvm_disable_tdp();
7596
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007597 if (!nested) {
7598 kvm_x86_ops->get_nested_state = NULL;
7599 kvm_x86_ops->set_nested_state = NULL;
7600 }
7601
Kai Huang843e4332015-01-28 10:54:28 +08007602 /*
7603 * Only enable PML when hardware supports PML feature, and both EPT
7604 * and EPT A/D bit features are enabled -- PML depends on them to work.
7605 */
7606 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7607 enable_pml = 0;
7608
7609 if (!enable_pml) {
7610 kvm_x86_ops->slot_enable_log_dirty = NULL;
7611 kvm_x86_ops->slot_disable_log_dirty = NULL;
7612 kvm_x86_ops->flush_log_dirty = NULL;
7613 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7614 }
7615
Yunhong Jiang64672c92016-06-13 14:19:59 -07007616 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7617 u64 vmx_msr;
7618
7619 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7620 cpu_preemption_timer_multi =
7621 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7622 } else {
7623 kvm_x86_ops->set_hv_timer = NULL;
7624 kvm_x86_ops->cancel_hv_timer = NULL;
7625 }
7626
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007627 if (!cpu_has_vmx_shadow_vmcs())
7628 enable_shadow_vmcs = 0;
7629 if (enable_shadow_vmcs)
7630 init_vmcs_shadow_fields();
7631
Feng Wubf9f6ac2015-09-18 22:29:55 +08007632 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007633 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007634
Ashok Rajc45dcc72016-06-22 14:59:56 +08007635 kvm_mce_cap_supported |= MCG_LMCE_P;
7636
Tiejun Chenf2c76482014-10-28 10:14:47 +08007637 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007638
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007639out:
Radim Krčmář23611332016-09-29 22:41:33 +02007640 for (i = 0; i < VMX_BITMAP_NR; i++)
7641 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007642
7643 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007644}
7645
7646static __exit void hardware_unsetup(void)
7647{
Radim Krčmář23611332016-09-29 22:41:33 +02007648 int i;
7649
7650 for (i = 0; i < VMX_BITMAP_NR; i++)
7651 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007652
Tiejun Chenf2c76482014-10-28 10:14:47 +08007653 free_kvm_area();
7654}
7655
Avi Kivity6aa8b732006-12-10 02:21:36 -08007656/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007657 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7658 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7659 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007660static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007661{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007662 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007663 grow_ple_window(vcpu);
7664
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007665 /*
7666 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7667 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7668 * never set PAUSE_EXITING and just set PLE if supported,
7669 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7670 */
7671 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007672 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007673}
7674
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007675static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007676{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007677 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007678}
7679
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007680static int handle_mwait(struct kvm_vcpu *vcpu)
7681{
7682 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7683 return handle_nop(vcpu);
7684}
7685
Jim Mattson45ec3682017-08-23 16:32:04 -07007686static int handle_invalid_op(struct kvm_vcpu *vcpu)
7687{
7688 kvm_queue_exception(vcpu, UD_VECTOR);
7689 return 1;
7690}
7691
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007692static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7693{
7694 return 1;
7695}
7696
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007697static int handle_monitor(struct kvm_vcpu *vcpu)
7698{
7699 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7700 return handle_nop(vcpu);
7701}
7702
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007703/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007704 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7705 * set the success or error code of an emulated VMX instruction, as specified
7706 * by Vol 2B, VMX Instruction Reference, "Conventions".
7707 */
7708static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7709{
7710 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7711 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7712 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7713}
7714
7715static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7716{
7717 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7718 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7719 X86_EFLAGS_SF | X86_EFLAGS_OF))
7720 | X86_EFLAGS_CF);
7721}
7722
Abel Gordon145c28d2013-04-18 14:36:55 +03007723static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007724 u32 vm_instruction_error)
7725{
7726 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7727 /*
7728 * failValid writes the error number to the current VMCS, which
7729 * can't be done there isn't a current VMCS.
7730 */
7731 nested_vmx_failInvalid(vcpu);
7732 return;
7733 }
7734 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7735 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7736 X86_EFLAGS_SF | X86_EFLAGS_OF))
7737 | X86_EFLAGS_ZF);
7738 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7739 /*
7740 * We don't need to force a shadow sync because
7741 * VM_INSTRUCTION_ERROR is not shadowed
7742 */
7743}
Abel Gordon145c28d2013-04-18 14:36:55 +03007744
Wincy Vanff651cb2014-12-11 08:52:58 +03007745static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7746{
7747 /* TODO: not to reset guest simply here. */
7748 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007749 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007750}
7751
Jan Kiszkaf4124502014-03-07 20:03:13 +01007752static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7753{
7754 struct vcpu_vmx *vmx =
7755 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7756
7757 vmx->nested.preemption_timer_expired = true;
7758 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7759 kvm_vcpu_kick(&vmx->vcpu);
7760
7761 return HRTIMER_NORESTART;
7762}
7763
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007764/*
Bandan Das19677e32014-05-06 02:19:15 -04007765 * Decode the memory-address operand of a vmx instruction, as recorded on an
7766 * exit caused by such an instruction (run by a guest hypervisor).
7767 * On success, returns 0. When the operand is invalid, returns 1 and throws
7768 * #UD or #GP.
7769 */
7770static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7771 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007772 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007773{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007774 gva_t off;
7775 bool exn;
7776 struct kvm_segment s;
7777
Bandan Das19677e32014-05-06 02:19:15 -04007778 /*
7779 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7780 * Execution", on an exit, vmx_instruction_info holds most of the
7781 * addressing components of the operand. Only the displacement part
7782 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7783 * For how an actual address is calculated from all these components,
7784 * refer to Vol. 1, "Operand Addressing".
7785 */
7786 int scaling = vmx_instruction_info & 3;
7787 int addr_size = (vmx_instruction_info >> 7) & 7;
7788 bool is_reg = vmx_instruction_info & (1u << 10);
7789 int seg_reg = (vmx_instruction_info >> 15) & 7;
7790 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7791 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7792 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7793 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7794
7795 if (is_reg) {
7796 kvm_queue_exception(vcpu, UD_VECTOR);
7797 return 1;
7798 }
7799
7800 /* Addr = segment_base + offset */
7801 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007802 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007803 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007804 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007805 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007806 off += kvm_register_read(vcpu, index_reg)<<scaling;
7807 vmx_get_segment(vcpu, &s, seg_reg);
7808 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007809
7810 if (addr_size == 1) /* 32 bit */
7811 *ret &= 0xffffffff;
7812
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007813 /* Checks for #GP/#SS exceptions. */
7814 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007815 if (is_long_mode(vcpu)) {
7816 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7817 * non-canonical form. This is the only check on the memory
7818 * destination for long mode!
7819 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007820 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007821 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007822 /* Protected mode: apply checks for segment validity in the
7823 * following order:
7824 * - segment type check (#GP(0) may be thrown)
7825 * - usability check (#GP(0)/#SS(0))
7826 * - limit check (#GP(0)/#SS(0))
7827 */
7828 if (wr)
7829 /* #GP(0) if the destination operand is located in a
7830 * read-only data segment or any code segment.
7831 */
7832 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7833 else
7834 /* #GP(0) if the source operand is located in an
7835 * execute-only code segment
7836 */
7837 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007838 if (exn) {
7839 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7840 return 1;
7841 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007842 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7843 */
7844 exn = (s.unusable != 0);
7845 /* Protected mode: #GP(0)/#SS(0) if the memory
7846 * operand is outside the segment limit.
7847 */
7848 exn = exn || (off + sizeof(u64) > s.limit);
7849 }
7850 if (exn) {
7851 kvm_queue_exception_e(vcpu,
7852 seg_reg == VCPU_SREG_SS ?
7853 SS_VECTOR : GP_VECTOR,
7854 0);
7855 return 1;
7856 }
7857
Bandan Das19677e32014-05-06 02:19:15 -04007858 return 0;
7859}
7860
Radim Krčmářcbf71272017-05-19 15:48:51 +02007861static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007862{
7863 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007864 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007865
7866 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007867 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007868 return 1;
7869
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007870 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007871 kvm_inject_page_fault(vcpu, &e);
7872 return 1;
7873 }
7874
Bandan Das3573e222014-05-06 02:19:16 -04007875 return 0;
7876}
7877
Jim Mattsone29acc52016-11-30 12:03:43 -08007878static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7879{
7880 struct vcpu_vmx *vmx = to_vmx(vcpu);
7881 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007882 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007883
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007884 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7885 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007886 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007887
7888 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7889 if (!vmx->nested.cached_vmcs12)
7890 goto out_cached_vmcs12;
7891
7892 if (enable_shadow_vmcs) {
7893 shadow_vmcs = alloc_vmcs();
7894 if (!shadow_vmcs)
7895 goto out_shadow_vmcs;
7896 /* mark vmcs as shadow */
Liran Alon392b2f22018-06-23 02:35:01 +03007897 shadow_vmcs->hdr.shadow_vmcs = 1;
Jim Mattsone29acc52016-11-30 12:03:43 -08007898 /* init shadow vmcs */
7899 vmcs_clear(shadow_vmcs);
7900 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7901 }
7902
Jim Mattsone29acc52016-11-30 12:03:43 -08007903 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7904 HRTIMER_MODE_REL_PINNED);
7905 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7906
7907 vmx->nested.vmxon = true;
7908 return 0;
7909
7910out_shadow_vmcs:
7911 kfree(vmx->nested.cached_vmcs12);
7912
7913out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007914 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007915
Jim Mattsonde3a0022017-11-27 17:22:25 -06007916out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007917 return -ENOMEM;
7918}
7919
Bandan Das3573e222014-05-06 02:19:16 -04007920/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007921 * Emulate the VMXON instruction.
7922 * Currently, we just remember that VMX is active, and do not save or even
7923 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7924 * do not currently need to store anything in that guest-allocated memory
7925 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7926 * argument is different from the VMXON pointer (which the spec says they do).
7927 */
7928static int handle_vmon(struct kvm_vcpu *vcpu)
7929{
Jim Mattsone29acc52016-11-30 12:03:43 -08007930 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007931 gpa_t vmptr;
7932 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007933 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007934 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7935 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007936
Jim Mattson70f3aac2017-04-26 08:53:46 -07007937 /*
7938 * The Intel VMX Instruction Reference lists a bunch of bits that are
7939 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7940 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7941 * Otherwise, we should fail with #UD. But most faulting conditions
7942 * have already been checked by hardware, prior to the VM-exit for
7943 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7944 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007945 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007946 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007947 kvm_queue_exception(vcpu, UD_VECTOR);
7948 return 1;
7949 }
7950
Felix Wilhelm727ba742018-06-11 09:43:44 +02007951 /* CPL=0 must be checked manually. */
7952 if (vmx_get_cpl(vcpu)) {
7953 kvm_queue_exception(vcpu, UD_VECTOR);
7954 return 1;
7955 }
7956
Abel Gordon145c28d2013-04-18 14:36:55 +03007957 if (vmx->nested.vmxon) {
7958 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007959 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007960 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007961
Haozhong Zhang3b840802016-06-22 14:59:54 +08007962 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007963 != VMXON_NEEDED_FEATURES) {
7964 kvm_inject_gp(vcpu, 0);
7965 return 1;
7966 }
7967
Radim Krčmářcbf71272017-05-19 15:48:51 +02007968 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007969 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007970
7971 /*
7972 * SDM 3: 24.11.5
7973 * The first 4 bytes of VMXON region contain the supported
7974 * VMCS revision identifier
7975 *
7976 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7977 * which replaces physical address width with 32
7978 */
7979 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7980 nested_vmx_failInvalid(vcpu);
7981 return kvm_skip_emulated_instruction(vcpu);
7982 }
7983
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007984 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7985 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007986 nested_vmx_failInvalid(vcpu);
7987 return kvm_skip_emulated_instruction(vcpu);
7988 }
7989 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7990 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007991 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007992 nested_vmx_failInvalid(vcpu);
7993 return kvm_skip_emulated_instruction(vcpu);
7994 }
7995 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007996 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007997
7998 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007999 ret = enter_vmx_operation(vcpu);
8000 if (ret)
8001 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008002
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008003 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008004 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008005}
8006
8007/*
8008 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8009 * for running VMX instructions (except VMXON, whose prerequisites are
8010 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008011 * Note that many of these exceptions have priority over VM exits, so they
8012 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008013 */
8014static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8015{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008016 if (vmx_get_cpl(vcpu)) {
8017 kvm_queue_exception(vcpu, UD_VECTOR);
8018 return 0;
8019 }
8020
Jim Mattson70f3aac2017-04-26 08:53:46 -07008021 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008022 kvm_queue_exception(vcpu, UD_VECTOR);
8023 return 0;
8024 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008025 return 1;
8026}
8027
David Matlack8ca44e82017-08-01 14:00:39 -07008028static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8029{
8030 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8031 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8032}
8033
Abel Gordone7953d72013-04-18 14:37:55 +03008034static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8035{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008036 if (vmx->nested.current_vmptr == -1ull)
8037 return;
8038
Abel Gordon012f83c2013-04-18 14:39:25 +03008039 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008040 /* copy to memory all shadowed fields in case
8041 they were modified */
8042 copy_shadow_to_vmcs12(vmx);
8043 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008044 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008045 }
Wincy Van705699a2015-02-03 23:58:17 +08008046 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008047
8048 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008049 kvm_vcpu_write_guest_page(&vmx->vcpu,
8050 vmx->nested.current_vmptr >> PAGE_SHIFT,
8051 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008052
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008053 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008054}
8055
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008056/*
8057 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8058 * just stops using VMX.
8059 */
8060static void free_nested(struct vcpu_vmx *vmx)
8061{
Wanpeng Lib7455822017-11-22 14:04:00 -08008062 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008063 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008064
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008065 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008066 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008067 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008068 vmx->nested.posted_intr_nv = -1;
8069 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008070 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008071 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008072 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8073 free_vmcs(vmx->vmcs01.shadow_vmcs);
8074 vmx->vmcs01.shadow_vmcs = NULL;
8075 }
David Matlack4f2777b2016-07-13 17:16:37 -07008076 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008077 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008078 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008079 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008080 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008081 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008082 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008083 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008084 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008085 }
Wincy Van705699a2015-02-03 23:58:17 +08008086 if (vmx->nested.pi_desc_page) {
8087 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008088 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008089 vmx->nested.pi_desc_page = NULL;
8090 vmx->nested.pi_desc = NULL;
8091 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008092
Jim Mattsonde3a0022017-11-27 17:22:25 -06008093 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008094}
8095
8096/* Emulate the VMXOFF instruction */
8097static int handle_vmoff(struct kvm_vcpu *vcpu)
8098{
8099 if (!nested_vmx_check_permission(vcpu))
8100 return 1;
8101 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008102 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008103 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008104}
8105
Nadav Har'El27d6c862011-05-25 23:06:59 +03008106/* Emulate the VMCLEAR instruction */
8107static int handle_vmclear(struct kvm_vcpu *vcpu)
8108{
8109 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008110 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008111 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008112
8113 if (!nested_vmx_check_permission(vcpu))
8114 return 1;
8115
Radim Krčmářcbf71272017-05-19 15:48:51 +02008116 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008117 return 1;
8118
Radim Krčmářcbf71272017-05-19 15:48:51 +02008119 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8120 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8121 return kvm_skip_emulated_instruction(vcpu);
8122 }
8123
8124 if (vmptr == vmx->nested.vmxon_ptr) {
8125 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8126 return kvm_skip_emulated_instruction(vcpu);
8127 }
8128
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008129 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008130 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008131
Jim Mattson587d7e722017-03-02 12:41:48 -08008132 kvm_vcpu_write_guest(vcpu,
8133 vmptr + offsetof(struct vmcs12, launch_state),
8134 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008135
Nadav Har'El27d6c862011-05-25 23:06:59 +03008136 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008137 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008138}
8139
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008140static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8141
8142/* Emulate the VMLAUNCH instruction */
8143static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8144{
8145 return nested_vmx_run(vcpu, true);
8146}
8147
8148/* Emulate the VMRESUME instruction */
8149static int handle_vmresume(struct kvm_vcpu *vcpu)
8150{
8151
8152 return nested_vmx_run(vcpu, false);
8153}
8154
Nadav Har'El49f705c2011-05-25 23:08:30 +03008155/*
8156 * Read a vmcs12 field. Since these can have varying lengths and we return
8157 * one type, we chose the biggest type (u64) and zero-extend the return value
8158 * to that size. Note that the caller, handle_vmread, might need to use only
8159 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8160 * 64-bit fields are to be returned).
8161 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008162static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8163 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008164{
8165 short offset = vmcs_field_to_offset(field);
8166 char *p;
8167
8168 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008169 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008170
8171 p = ((char *)(get_vmcs12(vcpu))) + offset;
8172
Jim Mattsond37f4262017-12-22 12:12:16 -08008173 switch (vmcs_field_width(field)) {
8174 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008175 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008176 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008177 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008178 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008179 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008180 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008181 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008182 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008183 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008184 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008185 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008186 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008187 WARN_ON(1);
8188 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008189 }
8190}
8191
Abel Gordon20b97fe2013-04-18 14:36:25 +03008192
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008193static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8194 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008195 short offset = vmcs_field_to_offset(field);
8196 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8197 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008198 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008199
Jim Mattsond37f4262017-12-22 12:12:16 -08008200 switch (vmcs_field_width(field)) {
8201 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008202 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008203 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008204 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008205 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008206 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008207 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008208 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008209 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008210 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008211 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008212 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008213 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008214 WARN_ON(1);
8215 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008216 }
8217
8218}
8219
Jim Mattsonf4160e42018-05-29 09:11:33 -07008220/*
8221 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8222 * they have been modified by the L1 guest. Note that the "read-only"
8223 * VM-exit information fields are actually writable if the vCPU is
8224 * configured to support "VMWRITE to any supported field in the VMCS."
8225 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008226static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8227{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008228 const u16 *fields[] = {
8229 shadow_read_write_fields,
8230 shadow_read_only_fields
8231 };
8232 const int max_fields[] = {
8233 max_shadow_read_write_fields,
8234 max_shadow_read_only_fields
8235 };
8236 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008237 unsigned long field;
8238 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008239 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008240
Jan Kiszka282da872014-10-08 18:05:39 +02008241 preempt_disable();
8242
Abel Gordon16f5b902013-04-18 14:38:25 +03008243 vmcs_load(shadow_vmcs);
8244
Jim Mattsonf4160e42018-05-29 09:11:33 -07008245 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8246 for (i = 0; i < max_fields[q]; i++) {
8247 field = fields[q][i];
8248 field_value = __vmcs_readl(field);
8249 vmcs12_write_any(&vmx->vcpu, field, field_value);
8250 }
8251 /*
8252 * Skip the VM-exit information fields if they are read-only.
8253 */
8254 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8255 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008256 }
8257
8258 vmcs_clear(shadow_vmcs);
8259 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008260
8261 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008262}
8263
Abel Gordonc3114422013-04-18 14:38:55 +03008264static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8265{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008266 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008267 shadow_read_write_fields,
8268 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008269 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008270 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008271 max_shadow_read_write_fields,
8272 max_shadow_read_only_fields
8273 };
8274 int i, q;
8275 unsigned long field;
8276 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008277 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008278
8279 vmcs_load(shadow_vmcs);
8280
Mathias Krausec2bae892013-06-26 20:36:21 +02008281 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008282 for (i = 0; i < max_fields[q]; i++) {
8283 field = fields[q][i];
8284 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008285 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008286 }
8287 }
8288
8289 vmcs_clear(shadow_vmcs);
8290 vmcs_load(vmx->loaded_vmcs->vmcs);
8291}
8292
Nadav Har'El49f705c2011-05-25 23:08:30 +03008293/*
8294 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8295 * used before) all generate the same failure when it is missing.
8296 */
8297static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8298{
8299 struct vcpu_vmx *vmx = to_vmx(vcpu);
8300 if (vmx->nested.current_vmptr == -1ull) {
8301 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008302 return 0;
8303 }
8304 return 1;
8305}
8306
8307static int handle_vmread(struct kvm_vcpu *vcpu)
8308{
8309 unsigned long field;
8310 u64 field_value;
8311 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8312 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8313 gva_t gva = 0;
8314
Kyle Hueyeb277562016-11-29 12:40:39 -08008315 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008316 return 1;
8317
Kyle Huey6affcbe2016-11-29 12:40:40 -08008318 if (!nested_vmx_check_vmcs12(vcpu))
8319 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008320
Nadav Har'El49f705c2011-05-25 23:08:30 +03008321 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008322 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008324 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008325 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008326 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008327 }
8328 /*
8329 * Now copy part of this value to register or memory, as requested.
8330 * Note that the number of bits actually copied is 32 or 64 depending
8331 * on the guest's mode (32 or 64 bit), not on the given field's length.
8332 */
8333 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008334 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008335 field_value);
8336 } else {
8337 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008338 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008339 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008340 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008341 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8342 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008343 }
8344
8345 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008346 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008347}
8348
8349
8350static int handle_vmwrite(struct kvm_vcpu *vcpu)
8351{
8352 unsigned long field;
8353 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008354 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008355 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8356 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008357
Nadav Har'El49f705c2011-05-25 23:08:30 +03008358 /* The value to write might be 32 or 64 bits, depending on L1's long
8359 * mode, and eventually we need to write that into a field of several
8360 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008361 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008362 * bits into the vmcs12 field.
8363 */
8364 u64 field_value = 0;
8365 struct x86_exception e;
8366
Kyle Hueyeb277562016-11-29 12:40:39 -08008367 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008368 return 1;
8369
Kyle Huey6affcbe2016-11-29 12:40:40 -08008370 if (!nested_vmx_check_vmcs12(vcpu))
8371 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008372
Nadav Har'El49f705c2011-05-25 23:08:30 +03008373 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008374 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008375 (((vmx_instruction_info) >> 3) & 0xf));
8376 else {
8377 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008378 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008379 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008380 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8381 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008382 kvm_inject_page_fault(vcpu, &e);
8383 return 1;
8384 }
8385 }
8386
8387
Nadav Amit27e6fb52014-06-18 17:19:26 +03008388 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008389 /*
8390 * If the vCPU supports "VMWRITE to any supported field in the
8391 * VMCS," then the "read-only" fields are actually read/write.
8392 */
8393 if (vmcs_field_readonly(field) &&
8394 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008395 nested_vmx_failValid(vcpu,
8396 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008397 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008398 }
8399
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008400 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008401 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008402 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008403 }
8404
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008405 switch (field) {
8406#define SHADOW_FIELD_RW(x) case x:
8407#include "vmx_shadow_fields.h"
8408 /*
8409 * The fields that can be updated by L1 without a vmexit are
8410 * always updated in the vmcs02, the others go down the slow
8411 * path of prepare_vmcs02.
8412 */
8413 break;
8414 default:
8415 vmx->nested.dirty_vmcs12 = true;
8416 break;
8417 }
8418
Nadav Har'El49f705c2011-05-25 23:08:30 +03008419 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008420 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008421}
8422
Jim Mattsona8bc2842016-11-30 12:03:44 -08008423static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8424{
8425 vmx->nested.current_vmptr = vmptr;
8426 if (enable_shadow_vmcs) {
8427 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8428 SECONDARY_EXEC_SHADOW_VMCS);
8429 vmcs_write64(VMCS_LINK_POINTER,
8430 __pa(vmx->vmcs01.shadow_vmcs));
8431 vmx->nested.sync_shadow_vmcs = true;
8432 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008433 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008434}
8435
Nadav Har'El63846662011-05-25 23:07:29 +03008436/* Emulate the VMPTRLD instruction */
8437static int handle_vmptrld(struct kvm_vcpu *vcpu)
8438{
8439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008440 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008441
8442 if (!nested_vmx_check_permission(vcpu))
8443 return 1;
8444
Radim Krčmářcbf71272017-05-19 15:48:51 +02008445 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008446 return 1;
8447
Radim Krčmářcbf71272017-05-19 15:48:51 +02008448 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8449 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8450 return kvm_skip_emulated_instruction(vcpu);
8451 }
8452
8453 if (vmptr == vmx->nested.vmxon_ptr) {
8454 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8455 return kvm_skip_emulated_instruction(vcpu);
8456 }
8457
Nadav Har'El63846662011-05-25 23:07:29 +03008458 if (vmx->nested.current_vmptr != vmptr) {
8459 struct vmcs12 *new_vmcs12;
8460 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008461 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8462 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008463 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008464 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008465 }
8466 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008467 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
8468 new_vmcs12->hdr.shadow_vmcs) {
Nadav Har'El63846662011-05-25 23:07:29 +03008469 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008470 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008471 nested_vmx_failValid(vcpu,
8472 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008473 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008474 }
Nadav Har'El63846662011-05-25 23:07:29 +03008475
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008476 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008477 /*
8478 * Load VMCS12 from guest memory since it is not already
8479 * cached.
8480 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008481 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8482 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008483 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008484
Jim Mattsona8bc2842016-11-30 12:03:44 -08008485 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008486 }
8487
8488 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008489 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008490}
8491
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008492/* Emulate the VMPTRST instruction */
8493static int handle_vmptrst(struct kvm_vcpu *vcpu)
8494{
8495 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8496 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8497 gva_t vmcs_gva;
8498 struct x86_exception e;
8499
8500 if (!nested_vmx_check_permission(vcpu))
8501 return 1;
8502
8503 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008504 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008505 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008506 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008507 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8508 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8509 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008510 kvm_inject_page_fault(vcpu, &e);
8511 return 1;
8512 }
8513 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008514 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008515}
8516
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008517/* Emulate the INVEPT instruction */
8518static int handle_invept(struct kvm_vcpu *vcpu)
8519{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008520 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008521 u32 vmx_instruction_info, types;
8522 unsigned long type;
8523 gva_t gva;
8524 struct x86_exception e;
8525 struct {
8526 u64 eptp, gpa;
8527 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008528
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008529 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008530 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008531 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008532 kvm_queue_exception(vcpu, UD_VECTOR);
8533 return 1;
8534 }
8535
8536 if (!nested_vmx_check_permission(vcpu))
8537 return 1;
8538
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008539 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008540 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008541
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008542 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008543
Jim Mattson85c856b2016-10-26 08:38:38 -07008544 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008545 nested_vmx_failValid(vcpu,
8546 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008547 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008548 }
8549
8550 /* According to the Intel VMX instruction reference, the memory
8551 * operand is read even if it isn't needed (e.g., for type==global)
8552 */
8553 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008554 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008555 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008556 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008557 kvm_inject_page_fault(vcpu, &e);
8558 return 1;
8559 }
8560
8561 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008562 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008563 /*
8564 * TODO: track mappings and invalidate
8565 * single context requests appropriately
8566 */
8567 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008568 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008569 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008570 nested_vmx_succeed(vcpu);
8571 break;
8572 default:
8573 BUG_ON(1);
8574 break;
8575 }
8576
Kyle Huey6affcbe2016-11-29 12:40:40 -08008577 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008578}
8579
Petr Matouseka642fc32014-09-23 20:22:30 +02008580static int handle_invvpid(struct kvm_vcpu *vcpu)
8581{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008582 struct vcpu_vmx *vmx = to_vmx(vcpu);
8583 u32 vmx_instruction_info;
8584 unsigned long type, types;
8585 gva_t gva;
8586 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008587 struct {
8588 u64 vpid;
8589 u64 gla;
8590 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008591
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008592 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008593 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008594 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008595 kvm_queue_exception(vcpu, UD_VECTOR);
8596 return 1;
8597 }
8598
8599 if (!nested_vmx_check_permission(vcpu))
8600 return 1;
8601
8602 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8603 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8604
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008605 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008606 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008607
Jim Mattson85c856b2016-10-26 08:38:38 -07008608 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008609 nested_vmx_failValid(vcpu,
8610 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008611 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008612 }
8613
8614 /* according to the intel vmx instruction reference, the memory
8615 * operand is read even if it isn't needed (e.g., for type==global)
8616 */
8617 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8618 vmx_instruction_info, false, &gva))
8619 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008620 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008621 kvm_inject_page_fault(vcpu, &e);
8622 return 1;
8623 }
Jim Mattson40352602017-06-28 09:37:37 -07008624 if (operand.vpid >> 16) {
8625 nested_vmx_failValid(vcpu,
8626 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8627 return kvm_skip_emulated_instruction(vcpu);
8628 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008629
8630 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008631 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008632 if (!operand.vpid ||
8633 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008634 nested_vmx_failValid(vcpu,
8635 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8636 return kvm_skip_emulated_instruction(vcpu);
8637 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008638 if (cpu_has_vmx_invvpid_individual_addr() &&
8639 vmx->nested.vpid02) {
8640 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8641 vmx->nested.vpid02, operand.gla);
8642 } else
8643 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8644 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008645 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008646 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008647 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008648 nested_vmx_failValid(vcpu,
8649 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008650 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008651 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008652 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008653 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008654 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008655 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008656 break;
8657 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008658 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008659 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008660 }
8661
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008662 nested_vmx_succeed(vcpu);
8663
Kyle Huey6affcbe2016-11-29 12:40:40 -08008664 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008665}
8666
Kai Huang843e4332015-01-28 10:54:28 +08008667static int handle_pml_full(struct kvm_vcpu *vcpu)
8668{
8669 unsigned long exit_qualification;
8670
8671 trace_kvm_pml_full(vcpu->vcpu_id);
8672
8673 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8674
8675 /*
8676 * PML buffer FULL happened while executing iret from NMI,
8677 * "blocked by NMI" bit has to be set before next VM entry.
8678 */
8679 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008680 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008681 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8682 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8683 GUEST_INTR_STATE_NMI);
8684
8685 /*
8686 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8687 * here.., and there's no userspace involvement needed for PML.
8688 */
8689 return 1;
8690}
8691
Yunhong Jiang64672c92016-06-13 14:19:59 -07008692static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8693{
8694 kvm_lapic_expired_hv_timer(vcpu);
8695 return 1;
8696}
8697
Bandan Das41ab9372017-08-03 15:54:43 -04008698static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8699{
8700 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008701 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8702
8703 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008704 switch (address & VMX_EPTP_MT_MASK) {
8705 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008706 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008707 return false;
8708 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008709 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008710 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008711 return false;
8712 break;
8713 default:
8714 return false;
8715 }
8716
David Hildenbrandbb97a012017-08-10 23:15:28 +02008717 /* only 4 levels page-walk length are valid */
8718 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008719 return false;
8720
8721 /* Reserved bits should not be set */
8722 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8723 return false;
8724
8725 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008726 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008727 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008728 return false;
8729 }
8730
8731 return true;
8732}
8733
8734static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8735 struct vmcs12 *vmcs12)
8736{
8737 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8738 u64 address;
8739 bool accessed_dirty;
8740 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8741
8742 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8743 !nested_cpu_has_ept(vmcs12))
8744 return 1;
8745
8746 if (index >= VMFUNC_EPTP_ENTRIES)
8747 return 1;
8748
8749
8750 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8751 &address, index * 8, 8))
8752 return 1;
8753
David Hildenbrandbb97a012017-08-10 23:15:28 +02008754 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008755
8756 /*
8757 * If the (L2) guest does a vmfunc to the currently
8758 * active ept pointer, we don't have to do anything else
8759 */
8760 if (vmcs12->ept_pointer != address) {
8761 if (!valid_ept_address(vcpu, address))
8762 return 1;
8763
8764 kvm_mmu_unload(vcpu);
8765 mmu->ept_ad = accessed_dirty;
8766 mmu->base_role.ad_disabled = !accessed_dirty;
8767 vmcs12->ept_pointer = address;
8768 /*
8769 * TODO: Check what's the correct approach in case
8770 * mmu reload fails. Currently, we just let the next
8771 * reload potentially fail
8772 */
8773 kvm_mmu_reload(vcpu);
8774 }
8775
8776 return 0;
8777}
8778
Bandan Das2a499e42017-08-03 15:54:41 -04008779static int handle_vmfunc(struct kvm_vcpu *vcpu)
8780{
Bandan Das27c42a12017-08-03 15:54:42 -04008781 struct vcpu_vmx *vmx = to_vmx(vcpu);
8782 struct vmcs12 *vmcs12;
8783 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8784
8785 /*
8786 * VMFUNC is only supported for nested guests, but we always enable the
8787 * secondary control for simplicity; for non-nested mode, fake that we
8788 * didn't by injecting #UD.
8789 */
8790 if (!is_guest_mode(vcpu)) {
8791 kvm_queue_exception(vcpu, UD_VECTOR);
8792 return 1;
8793 }
8794
8795 vmcs12 = get_vmcs12(vcpu);
8796 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8797 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008798
8799 switch (function) {
8800 case 0:
8801 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8802 goto fail;
8803 break;
8804 default:
8805 goto fail;
8806 }
8807 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008808
8809fail:
8810 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8811 vmcs_read32(VM_EXIT_INTR_INFO),
8812 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008813 return 1;
8814}
8815
Nadav Har'El0140cae2011-05-25 23:06:28 +03008816/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008817 * The exit handlers return 1 if the exit was handled fully and guest execution
8818 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8819 * to be done to userspace and return 0.
8820 */
Mathias Krause772e0312012-08-30 01:30:19 +02008821static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008822 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8823 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008824 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008825 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008826 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008827 [EXIT_REASON_CR_ACCESS] = handle_cr,
8828 [EXIT_REASON_DR_ACCESS] = handle_dr,
8829 [EXIT_REASON_CPUID] = handle_cpuid,
8830 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8831 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8832 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8833 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008834 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008835 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008836 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008837 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008838 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008839 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008840 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008841 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008842 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008843 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008844 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008845 [EXIT_REASON_VMOFF] = handle_vmoff,
8846 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008847 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8848 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008849 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008850 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008851 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008852 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008853 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008854 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008855 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8856 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008857 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8858 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008859 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008860 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008861 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008862 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008863 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008864 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008865 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008866 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008867 [EXIT_REASON_XSAVES] = handle_xsaves,
8868 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008869 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008870 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008871 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008872};
8873
8874static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008875 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008876
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008877static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8878 struct vmcs12 *vmcs12)
8879{
8880 unsigned long exit_qualification;
8881 gpa_t bitmap, last_bitmap;
8882 unsigned int port;
8883 int size;
8884 u8 b;
8885
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008886 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008887 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008888
8889 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8890
8891 port = exit_qualification >> 16;
8892 size = (exit_qualification & 7) + 1;
8893
8894 last_bitmap = (gpa_t)-1;
8895 b = -1;
8896
8897 while (size > 0) {
8898 if (port < 0x8000)
8899 bitmap = vmcs12->io_bitmap_a;
8900 else if (port < 0x10000)
8901 bitmap = vmcs12->io_bitmap_b;
8902 else
Joe Perches1d804d02015-03-30 16:46:09 -07008903 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008904 bitmap += (port & 0x7fff) / 8;
8905
8906 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008907 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008908 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008909 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008910 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008911
8912 port++;
8913 size--;
8914 last_bitmap = bitmap;
8915 }
8916
Joe Perches1d804d02015-03-30 16:46:09 -07008917 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008918}
8919
Nadav Har'El644d7112011-05-25 23:12:35 +03008920/*
8921 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8922 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8923 * disinterest in the current event (read or write a specific MSR) by using an
8924 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8925 */
8926static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8927 struct vmcs12 *vmcs12, u32 exit_reason)
8928{
8929 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8930 gpa_t bitmap;
8931
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008932 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008933 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008934
8935 /*
8936 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8937 * for the four combinations of read/write and low/high MSR numbers.
8938 * First we need to figure out which of the four to use:
8939 */
8940 bitmap = vmcs12->msr_bitmap;
8941 if (exit_reason == EXIT_REASON_MSR_WRITE)
8942 bitmap += 2048;
8943 if (msr_index >= 0xc0000000) {
8944 msr_index -= 0xc0000000;
8945 bitmap += 1024;
8946 }
8947
8948 /* Then read the msr_index'th bit from this bitmap: */
8949 if (msr_index < 1024*8) {
8950 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008951 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008952 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008953 return 1 & (b >> (msr_index & 7));
8954 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008955 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008956}
8957
8958/*
8959 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8960 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8961 * intercept (via guest_host_mask etc.) the current event.
8962 */
8963static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8964 struct vmcs12 *vmcs12)
8965{
8966 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8967 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008968 int reg;
8969 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008970
8971 switch ((exit_qualification >> 4) & 3) {
8972 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008973 reg = (exit_qualification >> 8) & 15;
8974 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008975 switch (cr) {
8976 case 0:
8977 if (vmcs12->cr0_guest_host_mask &
8978 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008979 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008980 break;
8981 case 3:
8982 if ((vmcs12->cr3_target_count >= 1 &&
8983 vmcs12->cr3_target_value0 == val) ||
8984 (vmcs12->cr3_target_count >= 2 &&
8985 vmcs12->cr3_target_value1 == val) ||
8986 (vmcs12->cr3_target_count >= 3 &&
8987 vmcs12->cr3_target_value2 == val) ||
8988 (vmcs12->cr3_target_count >= 4 &&
8989 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008990 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008991 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008992 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008993 break;
8994 case 4:
8995 if (vmcs12->cr4_guest_host_mask &
8996 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008997 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008998 break;
8999 case 8:
9000 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009001 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009002 break;
9003 }
9004 break;
9005 case 2: /* clts */
9006 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9007 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009008 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009009 break;
9010 case 1: /* mov from cr */
9011 switch (cr) {
9012 case 3:
9013 if (vmcs12->cpu_based_vm_exec_control &
9014 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009015 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009016 break;
9017 case 8:
9018 if (vmcs12->cpu_based_vm_exec_control &
9019 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009020 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009021 break;
9022 }
9023 break;
9024 case 3: /* lmsw */
9025 /*
9026 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9027 * cr0. Other attempted changes are ignored, with no exit.
9028 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009029 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009030 if (vmcs12->cr0_guest_host_mask & 0xe &
9031 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009032 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009033 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9034 !(vmcs12->cr0_read_shadow & 0x1) &&
9035 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009036 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009037 break;
9038 }
Joe Perches1d804d02015-03-30 16:46:09 -07009039 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009040}
9041
9042/*
9043 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9044 * should handle it ourselves in L0 (and then continue L2). Only call this
9045 * when in is_guest_mode (L2).
9046 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009047static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009048{
Nadav Har'El644d7112011-05-25 23:12:35 +03009049 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9050 struct vcpu_vmx *vmx = to_vmx(vcpu);
9051 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9052
Jim Mattson4f350c62017-09-14 16:31:44 -07009053 if (vmx->nested.nested_run_pending)
9054 return false;
9055
9056 if (unlikely(vmx->fail)) {
9057 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9058 vmcs_read32(VM_INSTRUCTION_ERROR));
9059 return true;
9060 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009061
David Matlackc9f04402017-08-01 14:00:40 -07009062 /*
9063 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009064 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9065 * Page). The CPU may write to these pages via their host
9066 * physical address while L2 is running, bypassing any
9067 * address-translation-based dirty tracking (e.g. EPT write
9068 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009069 *
9070 * Mark them dirty on every exit from L2 to prevent them from
9071 * getting out of sync with dirty tracking.
9072 */
9073 nested_mark_vmcs12_pages_dirty(vcpu);
9074
Jim Mattson4f350c62017-09-14 16:31:44 -07009075 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9076 vmcs_readl(EXIT_QUALIFICATION),
9077 vmx->idt_vectoring_info,
9078 intr_info,
9079 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9080 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009081
9082 switch (exit_reason) {
9083 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009084 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009085 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009086 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009087 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009088 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009089 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009090 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009091 else if (is_debug(intr_info) &&
9092 vcpu->guest_debug &
9093 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9094 return false;
9095 else if (is_breakpoint(intr_info) &&
9096 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9097 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009098 return vmcs12->exception_bitmap &
9099 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9100 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009101 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009102 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009103 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009104 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009105 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009106 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009107 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009108 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009110 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009111 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009112 case EXIT_REASON_HLT:
9113 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9114 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009115 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009116 case EXIT_REASON_INVLPG:
9117 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9118 case EXIT_REASON_RDPMC:
9119 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009120 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009121 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009122 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009123 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009124 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009125 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9126 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9127 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9128 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9129 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9130 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009131 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009132 /*
9133 * VMX instructions trap unconditionally. This allows L1 to
9134 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9135 */
Joe Perches1d804d02015-03-30 16:46:09 -07009136 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009137 case EXIT_REASON_CR_ACCESS:
9138 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9139 case EXIT_REASON_DR_ACCESS:
9140 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9141 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009142 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009143 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9144 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009145 case EXIT_REASON_MSR_READ:
9146 case EXIT_REASON_MSR_WRITE:
9147 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9148 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009149 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009150 case EXIT_REASON_MWAIT_INSTRUCTION:
9151 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009152 case EXIT_REASON_MONITOR_TRAP_FLAG:
9153 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009154 case EXIT_REASON_MONITOR_INSTRUCTION:
9155 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9156 case EXIT_REASON_PAUSE_INSTRUCTION:
9157 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9158 nested_cpu_has2(vmcs12,
9159 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9160 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009161 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009162 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009163 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009164 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009165 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009166 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009167 /*
9168 * The controls for "virtualize APIC accesses," "APIC-
9169 * register virtualization," and "virtual-interrupt
9170 * delivery" only come from vmcs12.
9171 */
Joe Perches1d804d02015-03-30 16:46:09 -07009172 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009173 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009174 /*
9175 * L0 always deals with the EPT violation. If nested EPT is
9176 * used, and the nested mmu code discovers that the address is
9177 * missing in the guest EPT table (EPT12), the EPT violation
9178 * will be injected with nested_ept_inject_page_fault()
9179 */
Joe Perches1d804d02015-03-30 16:46:09 -07009180 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009181 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009182 /*
9183 * L2 never uses directly L1's EPT, but rather L0's own EPT
9184 * table (shadow on EPT) or a merged EPT table that L0 built
9185 * (EPT on EPT). So any problems with the structure of the
9186 * table is L0's fault.
9187 */
Joe Perches1d804d02015-03-30 16:46:09 -07009188 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009189 case EXIT_REASON_INVPCID:
9190 return
9191 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9192 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 case EXIT_REASON_WBINVD:
9194 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9195 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009196 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009197 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9198 /*
9199 * This should never happen, since it is not possible to
9200 * set XSS to a non-zero value---neither in L1 nor in L2.
9201 * If if it were, XSS would have to be checked against
9202 * the XSS exit bitmap in vmcs12.
9203 */
9204 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009205 case EXIT_REASON_PREEMPTION_TIMER:
9206 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009207 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009208 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009209 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009210 case EXIT_REASON_VMFUNC:
9211 /* VM functions are emulated through L2->L0 vmexits. */
9212 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009213 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009214 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009215 }
9216}
9217
Paolo Bonzini7313c692017-07-27 10:31:25 +02009218static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9219{
9220 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9221
9222 /*
9223 * At this point, the exit interruption info in exit_intr_info
9224 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9225 * we need to query the in-kernel LAPIC.
9226 */
9227 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9228 if ((exit_intr_info &
9229 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9230 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9231 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9232 vmcs12->vm_exit_intr_error_code =
9233 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9234 }
9235
9236 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9237 vmcs_readl(EXIT_QUALIFICATION));
9238 return 1;
9239}
9240
Avi Kivity586f9602010-11-18 13:09:54 +02009241static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9242{
9243 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9244 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9245}
9246
Kai Huanga3eaa862015-11-04 13:46:05 +08009247static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009248{
Kai Huanga3eaa862015-11-04 13:46:05 +08009249 if (vmx->pml_pg) {
9250 __free_page(vmx->pml_pg);
9251 vmx->pml_pg = NULL;
9252 }
Kai Huang843e4332015-01-28 10:54:28 +08009253}
9254
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009255static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009256{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009258 u64 *pml_buf;
9259 u16 pml_idx;
9260
9261 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9262
9263 /* Do nothing if PML buffer is empty */
9264 if (pml_idx == (PML_ENTITY_NUM - 1))
9265 return;
9266
9267 /* PML index always points to next available PML buffer entity */
9268 if (pml_idx >= PML_ENTITY_NUM)
9269 pml_idx = 0;
9270 else
9271 pml_idx++;
9272
9273 pml_buf = page_address(vmx->pml_pg);
9274 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9275 u64 gpa;
9276
9277 gpa = pml_buf[pml_idx];
9278 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009279 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009280 }
9281
9282 /* reset PML index */
9283 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9284}
9285
9286/*
9287 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9288 * Called before reporting dirty_bitmap to userspace.
9289 */
9290static void kvm_flush_pml_buffers(struct kvm *kvm)
9291{
9292 int i;
9293 struct kvm_vcpu *vcpu;
9294 /*
9295 * We only need to kick vcpu out of guest mode here, as PML buffer
9296 * is flushed at beginning of all VMEXITs, and it's obvious that only
9297 * vcpus running in guest are possible to have unflushed GPAs in PML
9298 * buffer.
9299 */
9300 kvm_for_each_vcpu(i, vcpu, kvm)
9301 kvm_vcpu_kick(vcpu);
9302}
9303
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009304static void vmx_dump_sel(char *name, uint32_t sel)
9305{
9306 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009307 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009308 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9309 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9310 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9311}
9312
9313static void vmx_dump_dtsel(char *name, uint32_t limit)
9314{
9315 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9316 name, vmcs_read32(limit),
9317 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9318}
9319
9320static void dump_vmcs(void)
9321{
9322 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9323 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9324 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9325 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9326 u32 secondary_exec_control = 0;
9327 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009328 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009329 int i, n;
9330
9331 if (cpu_has_secondary_exec_ctrls())
9332 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9333
9334 pr_err("*** Guest State ***\n");
9335 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9336 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9337 vmcs_readl(CR0_GUEST_HOST_MASK));
9338 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9339 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9340 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9341 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9342 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9343 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009344 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9345 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9346 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9347 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009348 }
9349 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9350 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9351 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9352 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9353 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9354 vmcs_readl(GUEST_SYSENTER_ESP),
9355 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9356 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9357 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9358 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9359 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9360 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9361 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9362 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9363 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9364 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9365 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9366 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9367 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009368 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9369 efer, vmcs_read64(GUEST_IA32_PAT));
9370 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9371 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009372 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009373 if (cpu_has_load_perf_global_ctrl &&
9374 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009375 pr_err("PerfGlobCtl = 0x%016llx\n",
9376 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009377 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009378 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009379 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9380 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9381 vmcs_read32(GUEST_ACTIVITY_STATE));
9382 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9383 pr_err("InterruptStatus = %04x\n",
9384 vmcs_read16(GUEST_INTR_STATUS));
9385
9386 pr_err("*** Host State ***\n");
9387 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9388 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9389 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9390 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9391 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9392 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9393 vmcs_read16(HOST_TR_SELECTOR));
9394 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9395 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9396 vmcs_readl(HOST_TR_BASE));
9397 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9398 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9399 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9400 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9401 vmcs_readl(HOST_CR4));
9402 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9403 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9404 vmcs_read32(HOST_IA32_SYSENTER_CS),
9405 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9406 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009407 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9408 vmcs_read64(HOST_IA32_EFER),
9409 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009410 if (cpu_has_load_perf_global_ctrl &&
9411 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009412 pr_err("PerfGlobCtl = 0x%016llx\n",
9413 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009414
9415 pr_err("*** Control State ***\n");
9416 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9417 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9418 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9419 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9420 vmcs_read32(EXCEPTION_BITMAP),
9421 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9422 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9423 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9424 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9425 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9426 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9427 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9428 vmcs_read32(VM_EXIT_INTR_INFO),
9429 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9430 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9431 pr_err(" reason=%08x qualification=%016lx\n",
9432 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9433 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9434 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9435 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009436 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009437 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009438 pr_err("TSC Multiplier = 0x%016llx\n",
9439 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009440 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9441 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9442 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9443 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9444 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009445 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009446 n = vmcs_read32(CR3_TARGET_COUNT);
9447 for (i = 0; i + 1 < n; i += 4)
9448 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9449 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9450 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9451 if (i < n)
9452 pr_err("CR3 target%u=%016lx\n",
9453 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9454 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9455 pr_err("PLE Gap=%08x Window=%08x\n",
9456 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9457 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9458 pr_err("Virtual processor ID = 0x%04x\n",
9459 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9460}
9461
Avi Kivity6aa8b732006-12-10 02:21:36 -08009462/*
9463 * The guest has exited. See if we can fix it or if we need userspace
9464 * assistance.
9465 */
Avi Kivity851ba692009-08-24 11:10:17 +03009466static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009467{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009468 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009469 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009470 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009471
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009472 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9473
Kai Huang843e4332015-01-28 10:54:28 +08009474 /*
9475 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9476 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9477 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9478 * mode as if vcpus is in root mode, the PML buffer must has been
9479 * flushed already.
9480 */
9481 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009482 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009483
Mohammed Gamal80ced182009-09-01 12:48:18 +02009484 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009485 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009486 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009487
Paolo Bonzini7313c692017-07-27 10:31:25 +02009488 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9489 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009490
Mohammed Gamal51207022010-05-31 22:40:54 +03009491 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009492 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009493 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9494 vcpu->run->fail_entry.hardware_entry_failure_reason
9495 = exit_reason;
9496 return 0;
9497 }
9498
Avi Kivity29bd8a72007-09-10 17:27:03 +03009499 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009500 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9501 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009502 = vmcs_read32(VM_INSTRUCTION_ERROR);
9503 return 0;
9504 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009505
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009506 /*
9507 * Note:
9508 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9509 * delivery event since it indicates guest is accessing MMIO.
9510 * The vm-exit can be triggered again after return to guest that
9511 * will cause infinite loop.
9512 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009513 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009514 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009515 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009516 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009517 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9518 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9519 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009520 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009521 vcpu->run->internal.data[0] = vectoring_info;
9522 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009523 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9524 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9525 vcpu->run->internal.ndata++;
9526 vcpu->run->internal.data[3] =
9527 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9528 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009529 return 0;
9530 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009531
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009532 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009533 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9534 if (vmx_interrupt_allowed(vcpu)) {
9535 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9536 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9537 vcpu->arch.nmi_pending) {
9538 /*
9539 * This CPU don't support us in finding the end of an
9540 * NMI-blocked window if the guest runs with IRQs
9541 * disabled. So we pull the trigger after 1 s of
9542 * futile waiting, but inform the user about this.
9543 */
9544 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9545 "state on VCPU %d after 1 s timeout\n",
9546 __func__, vcpu->vcpu_id);
9547 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9548 }
9549 }
9550
Avi Kivity6aa8b732006-12-10 02:21:36 -08009551 if (exit_reason < kvm_vmx_max_exit_handlers
9552 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009553 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009554 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009555 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9556 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009557 kvm_queue_exception(vcpu, UD_VECTOR);
9558 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009559 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009560}
9561
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009562static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009563{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009564 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9565
9566 if (is_guest_mode(vcpu) &&
9567 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9568 return;
9569
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009570 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009571 vmcs_write32(TPR_THRESHOLD, 0);
9572 return;
9573 }
9574
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009575 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009576}
9577
Jim Mattson8d860bb2018-05-09 16:56:05 -04009578static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009579{
9580 u32 sec_exec_control;
9581
Jim Mattson8d860bb2018-05-09 16:56:05 -04009582 if (!lapic_in_kernel(vcpu))
9583 return;
9584
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009585 /* Postpone execution until vmcs01 is the current VMCS. */
9586 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009587 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009588 return;
9589 }
9590
Paolo Bonzini35754c92015-07-29 12:05:37 +02009591 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009592 return;
9593
9594 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009595 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9596 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009597
Jim Mattson8d860bb2018-05-09 16:56:05 -04009598 switch (kvm_get_apic_mode(vcpu)) {
9599 case LAPIC_MODE_INVALID:
9600 WARN_ONCE(true, "Invalid local APIC state");
9601 case LAPIC_MODE_DISABLED:
9602 break;
9603 case LAPIC_MODE_XAPIC:
9604 if (flexpriority_enabled) {
9605 sec_exec_control |=
9606 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9607 vmx_flush_tlb(vcpu, true);
9608 }
9609 break;
9610 case LAPIC_MODE_X2APIC:
9611 if (cpu_has_vmx_virtualize_x2apic_mode())
9612 sec_exec_control |=
9613 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9614 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009615 }
9616 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9617
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009618 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009619}
9620
Tang Chen38b99172014-09-24 15:57:54 +08009621static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9622{
Jim Mattsonab5df312018-05-09 17:02:03 -04009623 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009624 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009625 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009626 }
Tang Chen38b99172014-09-24 15:57:54 +08009627}
9628
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009629static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009630{
9631 u16 status;
9632 u8 old;
9633
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009634 if (max_isr == -1)
9635 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009636
9637 status = vmcs_read16(GUEST_INTR_STATUS);
9638 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009639 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009640 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009641 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009642 vmcs_write16(GUEST_INTR_STATUS, status);
9643 }
9644}
9645
9646static void vmx_set_rvi(int vector)
9647{
9648 u16 status;
9649 u8 old;
9650
Wei Wang4114c272014-11-05 10:53:43 +08009651 if (vector == -1)
9652 vector = 0;
9653
Yang Zhangc7c9c562013-01-25 10:18:51 +08009654 status = vmcs_read16(GUEST_INTR_STATUS);
9655 old = (u8)status & 0xff;
9656 if ((u8)vector != old) {
9657 status &= ~0xff;
9658 status |= (u8)vector;
9659 vmcs_write16(GUEST_INTR_STATUS, status);
9660 }
9661}
9662
9663static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9664{
Liran Alon851c1a182017-12-24 18:12:56 +02009665 /*
9666 * When running L2, updating RVI is only relevant when
9667 * vmcs12 virtual-interrupt-delivery enabled.
9668 * However, it can be enabled only when L1 also
9669 * intercepts external-interrupts and in that case
9670 * we should not update vmcs02 RVI but instead intercept
9671 * interrupt. Therefore, do nothing when running L2.
9672 */
9673 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009674 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009675}
9676
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009677static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009678{
9679 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009680 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009681 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009682
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009683 WARN_ON(!vcpu->arch.apicv_active);
9684 if (pi_test_on(&vmx->pi_desc)) {
9685 pi_clear_on(&vmx->pi_desc);
9686 /*
9687 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9688 * But on x86 this is just a compiler barrier anyway.
9689 */
9690 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009691 max_irr_updated =
9692 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9693
9694 /*
9695 * If we are running L2 and L1 has a new pending interrupt
9696 * which can be injected, we should re-evaluate
9697 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009698 * If L1 intercepts external-interrupts, we should
9699 * exit from L2 to L1. Otherwise, interrupt should be
9700 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009701 */
Liran Alon851c1a182017-12-24 18:12:56 +02009702 if (is_guest_mode(vcpu) && max_irr_updated) {
9703 if (nested_exit_on_intr(vcpu))
9704 kvm_vcpu_exiting_guest_mode(vcpu);
9705 else
9706 kvm_make_request(KVM_REQ_EVENT, vcpu);
9707 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009708 } else {
9709 max_irr = kvm_lapic_find_highest_irr(vcpu);
9710 }
9711 vmx_hwapic_irr_update(vcpu, max_irr);
9712 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009713}
9714
Andrey Smetanin63086302015-11-10 15:36:32 +03009715static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009716{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009717 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009718 return;
9719
Yang Zhangc7c9c562013-01-25 10:18:51 +08009720 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9721 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9722 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9723 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9724}
9725
Paolo Bonzini967235d2016-12-19 14:03:45 +01009726static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9727{
9728 struct vcpu_vmx *vmx = to_vmx(vcpu);
9729
9730 pi_clear_on(&vmx->pi_desc);
9731 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9732}
9733
Avi Kivity51aa01d2010-07-20 14:31:20 +03009734static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009735{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009736 u32 exit_intr_info = 0;
9737 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009738
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009739 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9740 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009741 return;
9742
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009743 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9744 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9745 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009746
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009747 /* if exit due to PF check for async PF */
9748 if (is_page_fault(exit_intr_info))
9749 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9750
Andi Kleena0861c02009-06-08 17:37:09 +08009751 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009752 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9753 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009754 kvm_machine_check();
9755
Gleb Natapov20f65982009-05-11 13:35:55 +03009756 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009757 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009758 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009759 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009760 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009761 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009762}
Gleb Natapov20f65982009-05-11 13:35:55 +03009763
Yang Zhanga547c6d2013-04-11 19:25:10 +08009764static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9765{
9766 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9767
Yang Zhanga547c6d2013-04-11 19:25:10 +08009768 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9769 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9770 unsigned int vector;
9771 unsigned long entry;
9772 gate_desc *desc;
9773 struct vcpu_vmx *vmx = to_vmx(vcpu);
9774#ifdef CONFIG_X86_64
9775 unsigned long tmp;
9776#endif
9777
9778 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9779 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009780 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009781 asm volatile(
9782#ifdef CONFIG_X86_64
9783 "mov %%" _ASM_SP ", %[sp]\n\t"
9784 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9785 "push $%c[ss]\n\t"
9786 "push %[sp]\n\t"
9787#endif
9788 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009789 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009790 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009791 :
9792#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009793 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009794#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009795 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009796 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009797 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009798 [ss]"i"(__KERNEL_DS),
9799 [cs]"i"(__KERNEL_CS)
9800 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009801 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009802}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009803STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009804
Tom Lendackybc226f02018-05-10 22:06:39 +02009805static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009806{
Tom Lendackybc226f02018-05-10 22:06:39 +02009807 switch (index) {
9808 case MSR_IA32_SMBASE:
9809 /*
9810 * We cannot do SMM unless we can run the guest in big
9811 * real mode.
9812 */
9813 return enable_unrestricted_guest || emulate_invalid_guest_state;
9814 case MSR_AMD64_VIRT_SPEC_CTRL:
9815 /* This is AMD only. */
9816 return false;
9817 default:
9818 return true;
9819 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009820}
9821
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009822static bool vmx_mpx_supported(void)
9823{
9824 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9825 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9826}
9827
Wanpeng Li55412b22014-12-02 19:21:30 +08009828static bool vmx_xsaves_supported(void)
9829{
9830 return vmcs_config.cpu_based_2nd_exec_ctrl &
9831 SECONDARY_EXEC_XSAVES;
9832}
9833
Avi Kivity51aa01d2010-07-20 14:31:20 +03009834static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9835{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009836 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009837 bool unblock_nmi;
9838 u8 vector;
9839 bool idtv_info_valid;
9840
9841 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009842
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009843 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009844 if (vmx->loaded_vmcs->nmi_known_unmasked)
9845 return;
9846 /*
9847 * Can't use vmx->exit_intr_info since we're not sure what
9848 * the exit reason is.
9849 */
9850 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9851 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9852 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9853 /*
9854 * SDM 3: 27.7.1.2 (September 2008)
9855 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9856 * a guest IRET fault.
9857 * SDM 3: 23.2.2 (September 2008)
9858 * Bit 12 is undefined in any of the following cases:
9859 * If the VM exit sets the valid bit in the IDT-vectoring
9860 * information field.
9861 * If the VM exit is due to a double fault.
9862 */
9863 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9864 vector != DF_VECTOR && !idtv_info_valid)
9865 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9866 GUEST_INTR_STATE_NMI);
9867 else
9868 vmx->loaded_vmcs->nmi_known_unmasked =
9869 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9870 & GUEST_INTR_STATE_NMI);
9871 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9872 vmx->loaded_vmcs->vnmi_blocked_time +=
9873 ktime_to_ns(ktime_sub(ktime_get(),
9874 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009875}
9876
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009877static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009878 u32 idt_vectoring_info,
9879 int instr_len_field,
9880 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009881{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009882 u8 vector;
9883 int type;
9884 bool idtv_info_valid;
9885
9886 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009887
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009888 vcpu->arch.nmi_injected = false;
9889 kvm_clear_exception_queue(vcpu);
9890 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009891
9892 if (!idtv_info_valid)
9893 return;
9894
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009895 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009896
Avi Kivity668f6122008-07-02 09:28:55 +03009897 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9898 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009899
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009900 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009901 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009902 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009903 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009904 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009905 * Clear bit "block by NMI" before VM entry if a NMI
9906 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009907 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009908 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009909 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009910 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009911 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009912 /* fall through */
9913 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009914 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009915 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009916 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009917 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009918 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009919 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009920 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009921 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009922 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009923 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009924 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009925 break;
9926 default:
9927 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009928 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009929}
9930
Avi Kivity83422e12010-07-20 14:43:23 +03009931static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9932{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009933 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009934 VM_EXIT_INSTRUCTION_LEN,
9935 IDT_VECTORING_ERROR_CODE);
9936}
9937
Avi Kivityb463a6f2010-07-20 15:06:17 +03009938static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9939{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009940 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009941 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9942 VM_ENTRY_INSTRUCTION_LEN,
9943 VM_ENTRY_EXCEPTION_ERROR_CODE);
9944
9945 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9946}
9947
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009948static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9949{
9950 int i, nr_msrs;
9951 struct perf_guest_switch_msr *msrs;
9952
9953 msrs = perf_guest_get_msrs(&nr_msrs);
9954
9955 if (!msrs)
9956 return;
9957
9958 for (i = 0; i < nr_msrs; i++)
9959 if (msrs[i].host == msrs[i].guest)
9960 clear_atomic_switch_msr(vmx, msrs[i].msr);
9961 else
9962 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9963 msrs[i].host);
9964}
9965
Jiang Biao33365e72016-11-03 15:03:37 +08009966static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009967{
9968 struct vcpu_vmx *vmx = to_vmx(vcpu);
9969 u64 tscl;
9970 u32 delta_tsc;
9971
9972 if (vmx->hv_deadline_tsc == -1)
9973 return;
9974
9975 tscl = rdtsc();
9976 if (vmx->hv_deadline_tsc > tscl)
9977 /* sure to be 32 bit only because checked on set_hv_timer */
9978 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9979 cpu_preemption_timer_multi);
9980 else
9981 delta_tsc = 0;
9982
9983 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9984}
9985
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009986static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009987{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009988 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009989 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009990
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009991 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009992 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009993 vmx->loaded_vmcs->soft_vnmi_blocked))
9994 vmx->loaded_vmcs->entry_time = ktime_get();
9995
Avi Kivity104f2262010-11-18 13:12:52 +02009996 /* Don't enter VMX if guest state is invalid, let the exit handler
9997 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009998 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009999 return;
10000
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010001 if (vmx->ple_window_dirty) {
10002 vmx->ple_window_dirty = false;
10003 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10004 }
10005
Abel Gordon012f83c2013-04-18 14:39:25 +030010006 if (vmx->nested.sync_shadow_vmcs) {
10007 copy_vmcs12_to_shadow(vmx);
10008 vmx->nested.sync_shadow_vmcs = false;
10009 }
10010
Avi Kivity104f2262010-11-18 13:12:52 +020010011 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10012 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10013 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10014 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10015
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010016 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010017 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010018 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010019 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010020 }
10021
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010022 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010023 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010024 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010025 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010026 }
10027
Avi Kivity104f2262010-11-18 13:12:52 +020010028 /* When single-stepping over STI and MOV SS, we must clear the
10029 * corresponding interruptibility bits in the guest state. Otherwise
10030 * vmentry fails as it then expects bit 14 (BS) in pending debug
10031 * exceptions being set, but that's not correct for the guest debugging
10032 * case. */
10033 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10034 vmx_set_interrupt_shadow(vcpu, 0);
10035
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010036 if (static_cpu_has(X86_FEATURE_PKU) &&
10037 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10038 vcpu->arch.pkru != vmx->host_pkru)
10039 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010040
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010041 atomic_switch_perf_msrs(vmx);
10042
Yunhong Jiang64672c92016-06-13 14:19:59 -070010043 vmx_arm_hv_timer(vcpu);
10044
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010045 /*
10046 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10047 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10048 * is no need to worry about the conditional branch over the wrmsr
10049 * being speculatively taken.
10050 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010051 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010052
Nadav Har'Eld462b812011-05-24 15:26:10 +030010053 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010054
10055 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10056 (unsigned long)&current_evmcs->host_rsp : 0;
10057
Avi Kivity104f2262010-11-18 13:12:52 +020010058 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010059 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010060 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10061 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10062 "push %%" _ASM_CX " \n\t"
10063 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010064 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010065 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010066 /* Avoid VMWRITE when Enlightened VMCS is in use */
10067 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10068 "jz 2f \n\t"
10069 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10070 "jmp 1f \n\t"
10071 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010072 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010073 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010074 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010075 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10076 "mov %%cr2, %%" _ASM_DX " \n\t"
10077 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010078 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010079 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010080 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010081 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010082 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010083 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010084 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10085 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10086 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10087 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10088 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10089 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010090#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010091 "mov %c[r8](%0), %%r8 \n\t"
10092 "mov %c[r9](%0), %%r9 \n\t"
10093 "mov %c[r10](%0), %%r10 \n\t"
10094 "mov %c[r11](%0), %%r11 \n\t"
10095 "mov %c[r12](%0), %%r12 \n\t"
10096 "mov %c[r13](%0), %%r13 \n\t"
10097 "mov %c[r14](%0), %%r14 \n\t"
10098 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010099#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010100 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010101
Avi Kivity6aa8b732006-12-10 02:21:36 -080010102 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010103 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010104 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010105 "jmp 2f \n\t"
10106 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10107 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010108 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010109 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010110 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010111 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010112 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10113 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10114 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10115 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10116 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10117 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10118 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010119#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010120 "mov %%r8, %c[r8](%0) \n\t"
10121 "mov %%r9, %c[r9](%0) \n\t"
10122 "mov %%r10, %c[r10](%0) \n\t"
10123 "mov %%r11, %c[r11](%0) \n\t"
10124 "mov %%r12, %c[r12](%0) \n\t"
10125 "mov %%r13, %c[r13](%0) \n\t"
10126 "mov %%r14, %c[r14](%0) \n\t"
10127 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010128 "xor %%r8d, %%r8d \n\t"
10129 "xor %%r9d, %%r9d \n\t"
10130 "xor %%r10d, %%r10d \n\t"
10131 "xor %%r11d, %%r11d \n\t"
10132 "xor %%r12d, %%r12d \n\t"
10133 "xor %%r13d, %%r13d \n\t"
10134 "xor %%r14d, %%r14d \n\t"
10135 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010136#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010137 "mov %%cr2, %%" _ASM_AX " \n\t"
10138 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010139
Jim Mattson0cb5b302018-01-03 14:31:38 -080010140 "xor %%eax, %%eax \n\t"
10141 "xor %%ebx, %%ebx \n\t"
10142 "xor %%esi, %%esi \n\t"
10143 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010144 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010145 ".pushsection .rodata \n\t"
10146 ".global vmx_return \n\t"
10147 "vmx_return: " _ASM_PTR " 2b \n\t"
10148 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010149 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010150 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010151 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010152 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010153 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10154 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10155 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10156 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10157 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10158 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10159 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010160#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010161 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10162 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10163 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10164 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10165 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10166 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10167 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10168 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010169#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010170 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10171 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010172 : "cc", "memory"
10173#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010174 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010175 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010176#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010177 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010178#endif
10179 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010180
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010181 /*
10182 * We do not use IBRS in the kernel. If this vCPU has used the
10183 * SPEC_CTRL MSR it may have left it on; save the value and
10184 * turn it off. This is much more efficient than blindly adding
10185 * it to the atomic save/restore list. Especially as the former
10186 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10187 *
10188 * For non-nested case:
10189 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10190 * save it.
10191 *
10192 * For nested case:
10193 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10194 * save it.
10195 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010196 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010197 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010198
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010199 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010200
David Woodhouse117cc7a2018-01-12 11:11:27 +000010201 /* Eliminate branch target predictions from guest mode */
10202 vmexit_fill_RSB();
10203
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010204 /* All fields are clean at this point */
10205 if (static_branch_unlikely(&enable_evmcs))
10206 current_evmcs->hv_clean_fields |=
10207 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10208
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010209 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010210 if (vmx->host_debugctlmsr)
10211 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010212
Avi Kivityaa67f602012-08-01 16:48:03 +030010213#ifndef CONFIG_X86_64
10214 /*
10215 * The sysexit path does not restore ds/es, so we must set them to
10216 * a reasonable value ourselves.
10217 *
10218 * We can't defer this to vmx_load_host_state() since that function
10219 * may be executed in interrupt context, which saves and restore segments
10220 * around it, nullifying its effect.
10221 */
10222 loadsegment(ds, __USER_DS);
10223 loadsegment(es, __USER_DS);
10224#endif
10225
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010226 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010227 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010228 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010229 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010230 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010231 vcpu->arch.regs_dirty = 0;
10232
Gleb Natapove0b890d2013-09-25 12:51:33 +030010233 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010234 * eager fpu is enabled if PKEY is supported and CR4 is switched
10235 * back on host, so it is safe to read guest PKRU from current
10236 * XSAVE.
10237 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010238 if (static_cpu_has(X86_FEATURE_PKU) &&
10239 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10240 vcpu->arch.pkru = __read_pkru();
10241 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010242 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010243 }
10244
Gleb Natapove0b890d2013-09-25 12:51:33 +030010245 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010246 vmx->idt_vectoring_info = 0;
10247
10248 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10249 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10250 return;
10251
10252 vmx->loaded_vmcs->launched = 1;
10253 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010254
Avi Kivity51aa01d2010-07-20 14:31:20 +030010255 vmx_complete_atomic_exit(vmx);
10256 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010257 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010258}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010259STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010260
Sean Christopherson434a1e92018-03-20 12:17:18 -070010261static struct kvm *vmx_vm_alloc(void)
10262{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010263 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010264 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010265}
10266
10267static void vmx_vm_free(struct kvm *kvm)
10268{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010269 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010270}
10271
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010272static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010273{
10274 struct vcpu_vmx *vmx = to_vmx(vcpu);
10275 int cpu;
10276
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010277 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010278 return;
10279
10280 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010281 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010282 vmx_vcpu_put(vcpu);
10283 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010284 put_cpu();
10285}
10286
Jim Mattson2f1fe812016-07-08 15:36:06 -070010287/*
10288 * Ensure that the current vmcs of the logical processor is the
10289 * vmcs01 of the vcpu before calling free_nested().
10290 */
10291static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10292{
10293 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010294
Christoffer Dallec7660c2017-12-04 21:35:23 +010010295 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010296 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010297 free_nested(vmx);
10298 vcpu_put(vcpu);
10299}
10300
Avi Kivity6aa8b732006-12-10 02:21:36 -080010301static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10302{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010303 struct vcpu_vmx *vmx = to_vmx(vcpu);
10304
Kai Huang843e4332015-01-28 10:54:28 +080010305 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010306 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010307 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010308 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010309 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010310 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010311 kfree(vmx->guest_msrs);
10312 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010313 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010314}
10315
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010316static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010317{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010318 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010319 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010320 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010321 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010322
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010323 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010324 return ERR_PTR(-ENOMEM);
10325
Wanpeng Li991e7a02015-09-16 17:30:05 +080010326 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010327
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010328 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10329 if (err)
10330 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010331
Peter Feiner4e595162016-07-07 14:49:58 -070010332 err = -ENOMEM;
10333
10334 /*
10335 * If PML is turned on, failure on enabling PML just results in failure
10336 * of creating the vcpu, therefore we can simplify PML logic (by
10337 * avoiding dealing with cases, such as enabling PML partially on vcpus
10338 * for the guest, etc.
10339 */
10340 if (enable_pml) {
10341 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10342 if (!vmx->pml_pg)
10343 goto uninit_vcpu;
10344 }
10345
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010346 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010347 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10348 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010349
Peter Feiner4e595162016-07-07 14:49:58 -070010350 if (!vmx->guest_msrs)
10351 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010352
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010353 err = alloc_loaded_vmcs(&vmx->vmcs01);
10354 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010355 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010356
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010357 msr_bitmap = vmx->vmcs01.msr_bitmap;
10358 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10359 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10360 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10361 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10362 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10363 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10364 vmx->msr_bitmap_mode = 0;
10365
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010366 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010367 cpu = get_cpu();
10368 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010369 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010370 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010371 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010372 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010373 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010374 err = alloc_apic_access_page(kvm);
10375 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010376 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010377 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010378
Sean Christophersone90008d2018-03-05 12:04:37 -080010379 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010380 err = init_rmode_identity_map(kvm);
10381 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010382 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010383 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010384
Wanpeng Li5c614b32015-10-13 09:18:36 -070010385 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010386 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10387 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010388 vmx->nested.vpid02 = allocate_vpid();
10389 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010390
Wincy Van705699a2015-02-03 23:58:17 +080010391 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010392 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010393
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010394 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10395
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010396 /*
10397 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10398 * or POSTED_INTR_WAKEUP_VECTOR.
10399 */
10400 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10401 vmx->pi_desc.sn = 1;
10402
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010403 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010404
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010405free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010406 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010407 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010408free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010409 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010410free_pml:
10411 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010412uninit_vcpu:
10413 kvm_vcpu_uninit(&vmx->vcpu);
10414free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010415 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010416 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010417 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010418}
10419
Wanpeng Lib31c1142018-03-12 04:53:04 -070010420static int vmx_vm_init(struct kvm *kvm)
10421{
10422 if (!ple_gap)
10423 kvm->arch.pause_in_guest = true;
10424 return 0;
10425}
10426
Yang, Sheng002c7f72007-07-31 14:23:01 +030010427static void __init vmx_check_processor_compat(void *rtn)
10428{
10429 struct vmcs_config vmcs_conf;
10430
10431 *(int *)rtn = 0;
10432 if (setup_vmcs_config(&vmcs_conf) < 0)
10433 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010434 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010435 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10436 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10437 smp_processor_id());
10438 *(int *)rtn = -EIO;
10439 }
10440}
10441
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010442static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010443{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010444 u8 cache;
10445 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010446
Sheng Yang522c68c2009-04-27 20:35:43 +080010447 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010448 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010449 * 2. EPT with VT-d:
10450 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010451 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010452 * b. VT-d with snooping control feature: snooping control feature of
10453 * VT-d engine can guarantee the cache correctness. Just set it
10454 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010455 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010456 * consistent with host MTRR
10457 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010458 if (is_mmio) {
10459 cache = MTRR_TYPE_UNCACHABLE;
10460 goto exit;
10461 }
10462
10463 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010464 ipat = VMX_EPT_IPAT_BIT;
10465 cache = MTRR_TYPE_WRBACK;
10466 goto exit;
10467 }
10468
10469 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10470 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010471 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010472 cache = MTRR_TYPE_WRBACK;
10473 else
10474 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010475 goto exit;
10476 }
10477
Xiao Guangrongff536042015-06-15 16:55:22 +080010478 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010479
10480exit:
10481 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010482}
10483
Sheng Yang17cc3932010-01-05 19:02:27 +080010484static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010485{
Sheng Yang878403b2010-01-05 19:02:29 +080010486 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10487 return PT_DIRECTORY_LEVEL;
10488 else
10489 /* For shadow and EPT supported 1GB page */
10490 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010491}
10492
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010493static void vmcs_set_secondary_exec_control(u32 new_ctl)
10494{
10495 /*
10496 * These bits in the secondary execution controls field
10497 * are dynamic, the others are mostly based on the hypervisor
10498 * architecture and the guest's CPUID. Do not touch the
10499 * dynamic bits.
10500 */
10501 u32 mask =
10502 SECONDARY_EXEC_SHADOW_VMCS |
10503 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010504 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10505 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010506
10507 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10508
10509 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10510 (new_ctl & ~mask) | (cur_ctl & mask));
10511}
10512
David Matlack8322ebb2016-11-29 18:14:09 -080010513/*
10514 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10515 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10516 */
10517static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10518{
10519 struct vcpu_vmx *vmx = to_vmx(vcpu);
10520 struct kvm_cpuid_entry2 *entry;
10521
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010522 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10523 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010524
10525#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10526 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010527 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010528} while (0)
10529
10530 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10531 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10532 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10533 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10534 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10535 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10536 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10537 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10538 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10539 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10540 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10541 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10542 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10543 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10544 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10545
10546 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10547 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10548 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10549 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10550 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010551 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010552
10553#undef cr4_fixed1_update
10554}
10555
Sheng Yang0e851882009-12-18 16:48:46 +080010556static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10557{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010558 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010559
Paolo Bonzini80154d72017-08-24 13:55:35 +020010560 if (cpu_has_secondary_exec_ctrls()) {
10561 vmx_compute_secondary_exec_control(vmx);
10562 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010563 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010564
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010565 if (nested_vmx_allowed(vcpu))
10566 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10567 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10568 else
10569 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10570 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010571
10572 if (nested_vmx_allowed(vcpu))
10573 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010574}
10575
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010576static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10577{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010578 if (func == 1 && nested)
10579 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010580}
10581
Yang Zhang25d92082013-08-06 12:00:32 +030010582static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10583 struct x86_exception *fault)
10584{
Jan Kiszka533558b2014-01-04 18:47:20 +010010585 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010586 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010587 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010588 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010589
Bandan Dasc5f983f2017-05-05 15:25:14 -040010590 if (vmx->nested.pml_full) {
10591 exit_reason = EXIT_REASON_PML_FULL;
10592 vmx->nested.pml_full = false;
10593 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10594 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010595 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010596 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010597 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010598
10599 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010600 vmcs12->guest_physical_address = fault->address;
10601}
10602
Peter Feiner995f00a2017-06-30 17:26:32 -070010603static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10604{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010605 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010606}
10607
Nadav Har'El155a97a2013-08-05 11:07:16 +030010608/* Callbacks for nested_ept_init_mmu_context: */
10609
10610static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10611{
10612 /* return the page table to be shadowed - in our case, EPT12 */
10613 return get_vmcs12(vcpu)->ept_pointer;
10614}
10615
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010616static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010617{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010618 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010619 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010620 return 1;
10621
10622 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010623 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010624 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010625 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010626 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010627 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10628 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10629 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10630
10631 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010632 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010633}
10634
10635static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10636{
10637 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10638}
10639
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010640static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10641 u16 error_code)
10642{
10643 bool inequality, bit;
10644
10645 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10646 inequality =
10647 (error_code & vmcs12->page_fault_error_code_mask) !=
10648 vmcs12->page_fault_error_code_match;
10649 return inequality ^ bit;
10650}
10651
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010652static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10653 struct x86_exception *fault)
10654{
10655 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10656
10657 WARN_ON(!is_guest_mode(vcpu));
10658
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010659 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10660 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010661 vmcs12->vm_exit_intr_error_code = fault->error_code;
10662 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10663 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10664 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10665 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010666 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010667 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010668 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010669}
10670
Paolo Bonzinic9923842017-12-13 14:16:30 +010010671static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10672 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010673
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010674static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010675{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010676 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010677 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010678 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010679 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010680
10681 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010682 /*
10683 * Translate L1 physical address to host physical
10684 * address for vmcs02. Keep the page pinned, so this
10685 * physical address remains valid. We keep a reference
10686 * to it so we can release it later.
10687 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010688 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010689 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010690 vmx->nested.apic_access_page = NULL;
10691 }
10692 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010693 /*
10694 * If translation failed, no matter: This feature asks
10695 * to exit when accessing the given address, and if it
10696 * can never be accessed, this feature won't do
10697 * anything anyway.
10698 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010699 if (!is_error_page(page)) {
10700 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010701 hpa = page_to_phys(vmx->nested.apic_access_page);
10702 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10703 } else {
10704 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10705 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10706 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010707 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010708
10709 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010710 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010711 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010712 vmx->nested.virtual_apic_page = NULL;
10713 }
10714 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010715
10716 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010717 * If translation failed, VM entry will fail because
10718 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10719 * Failing the vm entry is _not_ what the processor
10720 * does but it's basically the only possibility we
10721 * have. We could still enter the guest if CR8 load
10722 * exits are enabled, CR8 store exits are enabled, and
10723 * virtualize APIC access is disabled; in this case
10724 * the processor would never use the TPR shadow and we
10725 * could simply clear the bit from the execution
10726 * control. But such a configuration is useless, so
10727 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010728 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010729 if (!is_error_page(page)) {
10730 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010731 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10732 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10733 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010734 }
10735
Wincy Van705699a2015-02-03 23:58:17 +080010736 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010737 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10738 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010739 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010740 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010741 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010742 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10743 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010744 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010745 vmx->nested.pi_desc_page = page;
10746 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010747 vmx->nested.pi_desc =
10748 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10749 (unsigned long)(vmcs12->posted_intr_desc_addr &
10750 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010751 vmcs_write64(POSTED_INTR_DESC_ADDR,
10752 page_to_phys(vmx->nested.pi_desc_page) +
10753 (unsigned long)(vmcs12->posted_intr_desc_addr &
10754 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010755 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010756 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010757 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10758 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010759 else
10760 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10761 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010762}
10763
Jan Kiszkaf4124502014-03-07 20:03:13 +010010764static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10765{
10766 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10767 struct vcpu_vmx *vmx = to_vmx(vcpu);
10768
10769 if (vcpu->arch.virtual_tsc_khz == 0)
10770 return;
10771
10772 /* Make sure short timeouts reliably trigger an immediate vmexit.
10773 * hrtimer_start does not guarantee this. */
10774 if (preemption_timeout <= 1) {
10775 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10776 return;
10777 }
10778
10779 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10780 preemption_timeout *= 1000000;
10781 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10782 hrtimer_start(&vmx->nested.preemption_timer,
10783 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10784}
10785
Jim Mattson56a20512017-07-06 16:33:06 -070010786static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10787 struct vmcs12 *vmcs12)
10788{
10789 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10790 return 0;
10791
10792 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10793 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10794 return -EINVAL;
10795
10796 return 0;
10797}
10798
Wincy Van3af18d92015-02-03 23:49:31 +080010799static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10800 struct vmcs12 *vmcs12)
10801{
Wincy Van3af18d92015-02-03 23:49:31 +080010802 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10803 return 0;
10804
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010805 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010806 return -EINVAL;
10807
10808 return 0;
10809}
10810
Jim Mattson712b12d2017-08-24 13:24:47 -070010811static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10812 struct vmcs12 *vmcs12)
10813{
10814 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10815 return 0;
10816
10817 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10818 return -EINVAL;
10819
10820 return 0;
10821}
10822
Wincy Van3af18d92015-02-03 23:49:31 +080010823/*
10824 * Merge L0's and L1's MSR bitmap, return false to indicate that
10825 * we do not use the hardware.
10826 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010827static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10828 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010829{
Wincy Van82f0dd42015-02-03 23:57:18 +080010830 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010831 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010832 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010833 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010834 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010835 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010836 *
10837 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10838 * ensures that we do not accidentally generate an L02 MSR bitmap
10839 * from the L12 MSR bitmap that is too permissive.
10840 * 2. That L1 or L2s have actually used the MSR. This avoids
10841 * unnecessarily merging of the bitmap if the MSR is unused. This
10842 * works properly because we only update the L01 MSR bitmap lazily.
10843 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10844 * updated to reflect this when L1 (or its L2s) actually write to
10845 * the MSR.
10846 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010847 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10848 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010849
Paolo Bonzinic9923842017-12-13 14:16:30 +010010850 /* Nothing to do if the MSR bitmap is not in use. */
10851 if (!cpu_has_vmx_msr_bitmap() ||
10852 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10853 return false;
10854
Ashok Raj15d45072018-02-01 22:59:43 +010010855 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010856 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010857 return false;
10858
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010859 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10860 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010861 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010862
Radim Krčmářd048c092016-08-08 20:16:22 +020010863 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010864 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10865 /*
10866 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10867 * just lets the processor take the value from the virtual-APIC page;
10868 * take those 256 bits directly from the L1 bitmap.
10869 */
10870 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10871 unsigned word = msr / BITS_PER_LONG;
10872 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10873 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010874 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010875 } else {
10876 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10877 unsigned word = msr / BITS_PER_LONG;
10878 msr_bitmap_l0[word] = ~0;
10879 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10880 }
10881 }
10882
10883 nested_vmx_disable_intercept_for_msr(
10884 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010885 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010886 MSR_TYPE_W);
10887
10888 if (nested_cpu_has_vid(vmcs12)) {
10889 nested_vmx_disable_intercept_for_msr(
10890 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010891 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010892 MSR_TYPE_W);
10893 nested_vmx_disable_intercept_for_msr(
10894 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010895 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010896 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010897 }
Ashok Raj15d45072018-02-01 22:59:43 +010010898
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010899 if (spec_ctrl)
10900 nested_vmx_disable_intercept_for_msr(
10901 msr_bitmap_l1, msr_bitmap_l0,
10902 MSR_IA32_SPEC_CTRL,
10903 MSR_TYPE_R | MSR_TYPE_W);
10904
Ashok Raj15d45072018-02-01 22:59:43 +010010905 if (pred_cmd)
10906 nested_vmx_disable_intercept_for_msr(
10907 msr_bitmap_l1, msr_bitmap_l0,
10908 MSR_IA32_PRED_CMD,
10909 MSR_TYPE_W);
10910
Wincy Vanf2b93282015-02-03 23:56:03 +080010911 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010912 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010913
10914 return true;
10915}
10916
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010917static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10918 struct vmcs12 *vmcs12)
10919{
10920 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10921 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10922 return -EINVAL;
10923 else
10924 return 0;
10925}
10926
Wincy Vanf2b93282015-02-03 23:56:03 +080010927static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10928 struct vmcs12 *vmcs12)
10929{
Wincy Van82f0dd42015-02-03 23:57:18 +080010930 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010931 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010932 !nested_cpu_has_vid(vmcs12) &&
10933 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010934 return 0;
10935
10936 /*
10937 * If virtualize x2apic mode is enabled,
10938 * virtualize apic access must be disabled.
10939 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010940 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10941 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010942 return -EINVAL;
10943
Wincy Van608406e2015-02-03 23:57:51 +080010944 /*
10945 * If virtual interrupt delivery is enabled,
10946 * we must exit on external interrupts.
10947 */
10948 if (nested_cpu_has_vid(vmcs12) &&
10949 !nested_exit_on_intr(vcpu))
10950 return -EINVAL;
10951
Wincy Van705699a2015-02-03 23:58:17 +080010952 /*
10953 * bits 15:8 should be zero in posted_intr_nv,
10954 * the descriptor address has been already checked
10955 * in nested_get_vmcs12_pages.
10956 */
10957 if (nested_cpu_has_posted_intr(vmcs12) &&
10958 (!nested_cpu_has_vid(vmcs12) ||
10959 !nested_exit_intr_ack_set(vcpu) ||
10960 vmcs12->posted_intr_nv & 0xff00))
10961 return -EINVAL;
10962
Wincy Vanf2b93282015-02-03 23:56:03 +080010963 /* tpr shadow is needed by all apicv features. */
10964 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10965 return -EINVAL;
10966
10967 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010968}
10969
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010970static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10971 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010972 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010973{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010974 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010975 u64 count, addr;
10976
10977 if (vmcs12_read_any(vcpu, count_field, &count) ||
10978 vmcs12_read_any(vcpu, addr_field, &addr)) {
10979 WARN_ON(1);
10980 return -EINVAL;
10981 }
10982 if (count == 0)
10983 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010984 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010985 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10986 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010987 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010988 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10989 addr_field, maxphyaddr, count, addr);
10990 return -EINVAL;
10991 }
10992 return 0;
10993}
10994
10995static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10996 struct vmcs12 *vmcs12)
10997{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010998 if (vmcs12->vm_exit_msr_load_count == 0 &&
10999 vmcs12->vm_exit_msr_store_count == 0 &&
11000 vmcs12->vm_entry_msr_load_count == 0)
11001 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011002 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011003 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011004 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011005 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011006 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011007 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011008 return -EINVAL;
11009 return 0;
11010}
11011
Bandan Dasc5f983f2017-05-05 15:25:14 -040011012static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11013 struct vmcs12 *vmcs12)
11014{
11015 u64 address = vmcs12->pml_address;
11016 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11017
11018 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11019 if (!nested_cpu_has_ept(vmcs12) ||
11020 !IS_ALIGNED(address, 4096) ||
11021 address >> maxphyaddr)
11022 return -EINVAL;
11023 }
11024
11025 return 0;
11026}
11027
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011028static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11029 struct vmx_msr_entry *e)
11030{
11031 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011032 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011033 return -EINVAL;
11034 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11035 e->index == MSR_IA32_UCODE_REV)
11036 return -EINVAL;
11037 if (e->reserved != 0)
11038 return -EINVAL;
11039 return 0;
11040}
11041
11042static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11043 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011044{
11045 if (e->index == MSR_FS_BASE ||
11046 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011047 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11048 nested_vmx_msr_check_common(vcpu, e))
11049 return -EINVAL;
11050 return 0;
11051}
11052
11053static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11054 struct vmx_msr_entry *e)
11055{
11056 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11057 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011058 return -EINVAL;
11059 return 0;
11060}
11061
11062/*
11063 * Load guest's/host's msr at nested entry/exit.
11064 * return 0 for success, entry index for failure.
11065 */
11066static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11067{
11068 u32 i;
11069 struct vmx_msr_entry e;
11070 struct msr_data msr;
11071
11072 msr.host_initiated = false;
11073 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011074 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11075 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011076 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011077 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11078 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011079 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011080 }
11081 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011082 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011083 "%s check failed (%u, 0x%x, 0x%x)\n",
11084 __func__, i, e.index, e.reserved);
11085 goto fail;
11086 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011087 msr.index = e.index;
11088 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011089 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011090 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011091 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11092 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011093 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011094 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011095 }
11096 return 0;
11097fail:
11098 return i + 1;
11099}
11100
11101static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11102{
11103 u32 i;
11104 struct vmx_msr_entry e;
11105
11106 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011107 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011108 if (kvm_vcpu_read_guest(vcpu,
11109 gpa + i * sizeof(e),
11110 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011111 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011112 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11113 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011114 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011115 }
11116 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011117 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011118 "%s check failed (%u, 0x%x, 0x%x)\n",
11119 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011120 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011121 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011122 msr_info.host_initiated = false;
11123 msr_info.index = e.index;
11124 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011125 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011126 "%s cannot read MSR (%u, 0x%x)\n",
11127 __func__, i, e.index);
11128 return -EINVAL;
11129 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011130 if (kvm_vcpu_write_guest(vcpu,
11131 gpa + i * sizeof(e) +
11132 offsetof(struct vmx_msr_entry, value),
11133 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011134 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011135 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011136 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011137 return -EINVAL;
11138 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011139 }
11140 return 0;
11141}
11142
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011143static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11144{
11145 unsigned long invalid_mask;
11146
11147 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11148 return (val & invalid_mask) == 0;
11149}
11150
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011151/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011152 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11153 * emulating VM entry into a guest with EPT enabled.
11154 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11155 * is assigned to entry_failure_code on failure.
11156 */
11157static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011158 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011159{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011160 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011161 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011162 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11163 return 1;
11164 }
11165
11166 /*
11167 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11168 * must not be dereferenced.
11169 */
11170 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11171 !nested_ept) {
11172 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11173 *entry_failure_code = ENTRY_FAIL_PDPTE;
11174 return 1;
11175 }
11176 }
11177
11178 vcpu->arch.cr3 = cr3;
11179 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11180 }
11181
11182 kvm_mmu_reset_context(vcpu);
11183 return 0;
11184}
11185
Jim Mattson6514dc32018-04-26 16:09:12 -070011186static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011187{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011188 struct vcpu_vmx *vmx = to_vmx(vcpu);
11189
11190 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11191 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11192 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11193 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11194 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11195 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11196 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11197 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11198 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11199 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11200 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11201 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11202 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11203 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11204 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11205 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11206 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11207 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11208 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11209 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11210 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11211 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11212 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11213 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11214 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11215 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11216 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11217 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11218 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11219 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11220 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011221
11222 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11223 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11224 vmcs12->guest_pending_dbg_exceptions);
11225 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11226 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11227
11228 if (nested_cpu_has_xsaves(vmcs12))
11229 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11230 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11231
11232 if (cpu_has_vmx_posted_intr())
11233 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11234
11235 /*
11236 * Whether page-faults are trapped is determined by a combination of
11237 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11238 * If enable_ept, L0 doesn't care about page faults and we should
11239 * set all of these to L1's desires. However, if !enable_ept, L0 does
11240 * care about (at least some) page faults, and because it is not easy
11241 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11242 * to exit on each and every L2 page fault. This is done by setting
11243 * MASK=MATCH=0 and (see below) EB.PF=1.
11244 * Note that below we don't need special code to set EB.PF beyond the
11245 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11246 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11247 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11248 */
11249 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11250 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11251 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11252 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11253
11254 /* All VMFUNCs are currently emulated through L0 vmexits. */
11255 if (cpu_has_vmx_vmfunc())
11256 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11257
11258 if (cpu_has_vmx_apicv()) {
11259 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11260 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11261 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11262 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11263 }
11264
11265 /*
11266 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11267 * Some constant fields are set here by vmx_set_constant_host_state().
11268 * Other fields are different per CPU, and will be set later when
11269 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11270 */
11271 vmx_set_constant_host_state(vmx);
11272
11273 /*
11274 * Set the MSR load/store lists to match L0's settings.
11275 */
11276 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11277 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11278 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11279 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11280 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11281
11282 set_cr4_guest_host_mask(vmx);
11283
11284 if (vmx_mpx_supported())
11285 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11286
11287 if (enable_vpid) {
11288 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11289 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11290 else
11291 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11292 }
11293
11294 /*
11295 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11296 */
11297 if (enable_ept) {
11298 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11299 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11300 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11301 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11302 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011303
11304 if (cpu_has_vmx_msr_bitmap())
11305 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011306}
11307
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011308/*
11309 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11310 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011311 * 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 +030011312 * guest in a way that will both be appropriate to L1's requests, and our
11313 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11314 * function also has additional necessary side-effects, like setting various
11315 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011316 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11317 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011318 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011319static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011320 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011321{
11322 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011323 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011324
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011325 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011326 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011327 vmx->nested.dirty_vmcs12 = false;
11328 }
11329
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011330 /*
11331 * First, the fields that are shadowed. This must be kept in sync
11332 * with vmx_shadow_fields.h.
11333 */
11334
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011335 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011336 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011337 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011338 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11339 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011340
11341 /*
11342 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11343 * HOST_FS_BASE, HOST_GS_BASE.
11344 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011345
Jim Mattson6514dc32018-04-26 16:09:12 -070011346 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011347 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011348 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11349 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11350 } else {
11351 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11352 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11353 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011354 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011355 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11356 vmcs12->vm_entry_intr_info_field);
11357 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11358 vmcs12->vm_entry_exception_error_code);
11359 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11360 vmcs12->vm_entry_instruction_len);
11361 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11362 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011363 vmx->loaded_vmcs->nmi_known_unmasked =
11364 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011365 } else {
11366 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11367 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011368 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011369
Jan Kiszkaf4124502014-03-07 20:03:13 +010011370 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011371
Paolo Bonzini93140062016-07-06 13:23:51 +020011372 /* Preemption timer setting is only taken from vmcs01. */
11373 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11374 exec_control |= vmcs_config.pin_based_exec_ctrl;
11375 if (vmx->hv_deadline_tsc == -1)
11376 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11377
11378 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011379 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011380 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11381 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011382 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011383 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011384 }
Wincy Van705699a2015-02-03 23:58:17 +080011385
Jan Kiszkaf4124502014-03-07 20:03:13 +010011386 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011387
Jan Kiszkaf4124502014-03-07 20:03:13 +010011388 vmx->nested.preemption_timer_expired = false;
11389 if (nested_cpu_has_preemption_timer(vmcs12))
11390 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011391
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011392 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011393 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011394
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011395 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011396 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011397 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011398 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011399 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011400 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011401 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11402 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011403 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011404 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11405 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11406 ~SECONDARY_EXEC_ENABLE_PML;
11407 exec_control |= vmcs12_exec_ctrl;
11408 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011409
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011410 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011411 vmcs_write16(GUEST_INTR_STATUS,
11412 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011413
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011414 /*
11415 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11416 * nested_get_vmcs12_pages will either fix it up or
11417 * remove the VM execution control.
11418 */
11419 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11420 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11421
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011422 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11423 }
11424
Jim Mattson83bafef2016-10-04 10:48:38 -070011425 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011426 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11427 * entry, but only if the current (host) sp changed from the value
11428 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11429 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11430 * here we just force the write to happen on entry.
11431 */
11432 vmx->host_rsp = 0;
11433
11434 exec_control = vmx_exec_control(vmx); /* L0's desires */
11435 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11436 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11437 exec_control &= ~CPU_BASED_TPR_SHADOW;
11438 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011439
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011440 /*
11441 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11442 * nested_get_vmcs12_pages can't fix it up, the illegal value
11443 * will result in a VM entry failure.
11444 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011445 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011446 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011447 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011448 } else {
11449#ifdef CONFIG_X86_64
11450 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11451 CPU_BASED_CR8_STORE_EXITING;
11452#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011453 }
11454
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011455 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011456 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11457 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011458 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011459 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11460 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11461
11462 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11463
11464 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11465 * bitwise-or of what L1 wants to trap for L2, and what we want to
11466 * trap. Note that CR0.TS also needs updating - we do this later.
11467 */
11468 update_exception_bitmap(vcpu);
11469 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11470 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11471
Nadav Har'El8049d652013-08-05 11:07:06 +030011472 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11473 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11474 * bits are further modified by vmx_set_efer() below.
11475 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011476 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011477
11478 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11479 * emulated by vmx_set_efer(), below.
11480 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011481 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011482 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11483 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011484 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11485
Jim Mattson6514dc32018-04-26 16:09:12 -070011486 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011487 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011488 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011489 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011490 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011491 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011492 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011493
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011494 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11495
Peter Feinerc95ba922016-08-17 09:36:47 -070011496 if (kvm_has_tsc_control)
11497 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011498
11499 if (enable_vpid) {
11500 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011501 * There is no direct mapping between vpid02 and vpid12, the
11502 * vpid02 is per-vCPU for L0 and reused while the value of
11503 * vpid12 is changed w/ one invvpid during nested vmentry.
11504 * The vpid12 is allocated by L1 for L2, so it will not
11505 * influence global bitmap(for vpid01 and vpid02 allocation)
11506 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011507 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011508 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011509 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11510 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011511 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011512 }
11513 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011514 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011515 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011516 }
11517
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011518 if (enable_pml) {
11519 /*
11520 * Conceptually we want to copy the PML address and index from
11521 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11522 * since we always flush the log on each vmexit, this happens
11523 * to be equivalent to simply resetting the fields in vmcs02.
11524 */
11525 ASSERT(vmx->pml_pg);
11526 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11527 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11528 }
11529
Nadav Har'El155a97a2013-08-05 11:07:16 +030011530 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011531 if (nested_ept_init_mmu_context(vcpu)) {
11532 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11533 return 1;
11534 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011535 } else if (nested_cpu_has2(vmcs12,
11536 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011537 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011538 }
11539
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011540 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011541 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11542 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011543 * The CR0_READ_SHADOW is what L2 should have expected to read given
11544 * the specifications by L1; It's not enough to take
11545 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11546 * have more bits than L1 expected.
11547 */
11548 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11549 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11550
11551 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11552 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11553
Jim Mattson6514dc32018-04-26 16:09:12 -070011554 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011555 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011556 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11557 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11558 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11559 else
11560 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11561 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11562 vmx_set_efer(vcpu, vcpu->arch.efer);
11563
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011564 /*
11565 * Guest state is invalid and unrestricted guest is disabled,
11566 * which means L1 attempted VMEntry to L2 with invalid state.
11567 * Fail the VMEntry.
11568 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011569 if (vmx->emulation_required) {
11570 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011571 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011572 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011573
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011574 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011575 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011576 entry_failure_code))
11577 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011578
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011579 if (!enable_ept)
11580 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11581
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011582 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11583 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011584 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011585}
11586
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011587static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11588{
11589 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11590 nested_cpu_has_virtual_nmis(vmcs12))
11591 return -EINVAL;
11592
11593 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11594 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11595 return -EINVAL;
11596
11597 return 0;
11598}
11599
Jim Mattsonca0bde22016-11-30 12:03:46 -080011600static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11601{
11602 struct vcpu_vmx *vmx = to_vmx(vcpu);
11603
11604 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11605 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11606 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11607
Jim Mattson56a20512017-07-06 16:33:06 -070011608 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11609 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11610
Jim Mattsonca0bde22016-11-30 12:03:46 -080011611 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11612 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11613
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011614 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11615 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11616
Jim Mattson712b12d2017-08-24 13:24:47 -070011617 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11618 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11619
Jim Mattsonca0bde22016-11-30 12:03:46 -080011620 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11621 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11622
11623 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11624 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11625
Bandan Dasc5f983f2017-05-05 15:25:14 -040011626 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11627 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11628
Jim Mattsonca0bde22016-11-30 12:03:46 -080011629 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011630 vmx->nested.msrs.procbased_ctls_low,
11631 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011632 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11633 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011634 vmx->nested.msrs.secondary_ctls_low,
11635 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011636 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011637 vmx->nested.msrs.pinbased_ctls_low,
11638 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011639 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011640 vmx->nested.msrs.exit_ctls_low,
11641 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011642 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011643 vmx->nested.msrs.entry_ctls_low,
11644 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011645 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11646
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011647 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011648 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11649
Bandan Das41ab9372017-08-03 15:54:43 -040011650 if (nested_cpu_has_vmfunc(vmcs12)) {
11651 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011652 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011653 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11654
11655 if (nested_cpu_has_eptp_switching(vmcs12)) {
11656 if (!nested_cpu_has_ept(vmcs12) ||
11657 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11658 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11659 }
11660 }
Bandan Das27c42a12017-08-03 15:54:42 -040011661
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011662 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11663 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11664
Jim Mattsonca0bde22016-11-30 12:03:46 -080011665 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11666 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11667 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11668 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11669
Marc Orr04473782018-06-20 17:21:29 -070011670 /*
11671 * From the Intel SDM, volume 3:
11672 * Fields relevant to VM-entry event injection must be set properly.
11673 * These fields are the VM-entry interruption-information field, the
11674 * VM-entry exception error code, and the VM-entry instruction length.
11675 */
11676 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11677 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11678 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11679 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11680 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11681 bool should_have_error_code;
11682 bool urg = nested_cpu_has2(vmcs12,
11683 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11684 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11685
11686 /* VM-entry interruption-info field: interruption type */
11687 if (intr_type == INTR_TYPE_RESERVED ||
11688 (intr_type == INTR_TYPE_OTHER_EVENT &&
11689 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11690 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11691
11692 /* VM-entry interruption-info field: vector */
11693 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11694 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11695 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11696 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11697
11698 /* VM-entry interruption-info field: deliver error code */
11699 should_have_error_code =
11700 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11701 x86_exception_has_error_code(vector);
11702 if (has_error_code != should_have_error_code)
11703 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11704
11705 /* VM-entry exception error code */
11706 if (has_error_code &&
11707 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11708 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11709
11710 /* VM-entry interruption-info field: reserved bits */
11711 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11712 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11713
11714 /* VM-entry instruction length */
11715 switch (intr_type) {
11716 case INTR_TYPE_SOFT_EXCEPTION:
11717 case INTR_TYPE_SOFT_INTR:
11718 case INTR_TYPE_PRIV_SW_EXCEPTION:
11719 if ((vmcs12->vm_entry_instruction_len > 15) ||
11720 (vmcs12->vm_entry_instruction_len == 0 &&
11721 !nested_cpu_has_zero_length_injection(vcpu)))
11722 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11723 }
11724 }
11725
Jim Mattsonca0bde22016-11-30 12:03:46 -080011726 return 0;
11727}
11728
11729static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11730 u32 *exit_qual)
11731{
11732 bool ia32e;
11733
11734 *exit_qual = ENTRY_FAIL_DEFAULT;
11735
11736 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11737 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11738 return 1;
11739
11740 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11741 vmcs12->vmcs_link_pointer != -1ull) {
11742 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11743 return 1;
11744 }
11745
11746 /*
11747 * If the load IA32_EFER VM-entry control is 1, the following checks
11748 * are performed on the field for the IA32_EFER MSR:
11749 * - Bits reserved in the IA32_EFER MSR must be 0.
11750 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11751 * the IA-32e mode guest VM-exit control. It must also be identical
11752 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11753 * CR0.PG) is 1.
11754 */
11755 if (to_vmx(vcpu)->nested.nested_run_pending &&
11756 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11757 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11758 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11759 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11760 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11761 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11762 return 1;
11763 }
11764
11765 /*
11766 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11767 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11768 * the values of the LMA and LME bits in the field must each be that of
11769 * the host address-space size VM-exit control.
11770 */
11771 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11772 ia32e = (vmcs12->vm_exit_controls &
11773 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11774 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11775 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11776 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11777 return 1;
11778 }
11779
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011780 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11781 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11782 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11783 return 1;
11784
Jim Mattsonca0bde22016-11-30 12:03:46 -080011785 return 0;
11786}
11787
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011788/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011789 * If exit_qual is NULL, this is being called from state restore (either RSM
11790 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011791 */
11792static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080011793{
11794 struct vcpu_vmx *vmx = to_vmx(vcpu);
11795 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011796 bool from_vmentry = !!exit_qual;
11797 u32 dummy_exit_qual;
11798 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011799
Jim Mattson858e25c2016-11-30 12:03:47 -080011800 enter_guest_mode(vcpu);
11801
11802 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11803 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11804
Jim Mattsonde3a0022017-11-27 17:22:25 -060011805 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011806 vmx_segment_cache_clear(vmx);
11807
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011808 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11809 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11810
11811 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011812 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011813 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011814
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011815 if (from_vmentry) {
11816 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011817
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011818 r = EXIT_REASON_MSR_LOAD_FAIL;
11819 *exit_qual = nested_vmx_load_msr(vcpu,
11820 vmcs12->vm_entry_msr_load_addr,
11821 vmcs12->vm_entry_msr_load_count);
11822 if (*exit_qual)
11823 goto fail;
11824 } else {
11825 /*
11826 * The MMU is not initialized to point at the right entities yet and
11827 * "get pages" would need to read data from the guest (i.e. we will
11828 * need to perform gpa to hpa translation). Request a call
11829 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11830 * have already been set at vmentry time and should not be reset.
11831 */
11832 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11833 }
Jim Mattson858e25c2016-11-30 12:03:47 -080011834
Jim Mattson858e25c2016-11-30 12:03:47 -080011835 /*
11836 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11837 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11838 * returned as far as L1 is concerned. It will only return (and set
11839 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11840 */
11841 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011842
11843fail:
11844 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11845 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11846 leave_guest_mode(vcpu);
11847 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011848 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011849}
11850
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011851/*
11852 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11853 * for running an L2 nested guest.
11854 */
11855static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11856{
11857 struct vmcs12 *vmcs12;
11858 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011859 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011860 u32 exit_qual;
11861 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011862
Kyle Hueyeb277562016-11-29 12:40:39 -080011863 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011864 return 1;
11865
Kyle Hueyeb277562016-11-29 12:40:39 -080011866 if (!nested_vmx_check_vmcs12(vcpu))
11867 goto out;
11868
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011869 vmcs12 = get_vmcs12(vcpu);
11870
Abel Gordon012f83c2013-04-18 14:39:25 +030011871 if (enable_shadow_vmcs)
11872 copy_shadow_to_vmcs12(vmx);
11873
Nadav Har'El7c177932011-05-25 23:12:04 +030011874 /*
11875 * The nested entry process starts with enforcing various prerequisites
11876 * on vmcs12 as required by the Intel SDM, and act appropriately when
11877 * they fail: As the SDM explains, some conditions should cause the
11878 * instruction to fail, while others will cause the instruction to seem
11879 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11880 * To speed up the normal (success) code path, we should avoid checking
11881 * for misconfigurations which will anyway be caught by the processor
11882 * when using the merged vmcs02.
11883 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011884 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11885 nested_vmx_failValid(vcpu,
11886 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11887 goto out;
11888 }
11889
Nadav Har'El7c177932011-05-25 23:12:04 +030011890 if (vmcs12->launch_state == launch) {
11891 nested_vmx_failValid(vcpu,
11892 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11893 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011894 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011895 }
11896
Jim Mattsonca0bde22016-11-30 12:03:46 -080011897 ret = check_vmentry_prereqs(vcpu, vmcs12);
11898 if (ret) {
11899 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011900 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011901 }
11902
Nadav Har'El7c177932011-05-25 23:12:04 +030011903 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011904 * After this point, the trap flag no longer triggers a singlestep trap
11905 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11906 * This is not 100% correct; for performance reasons, we delegate most
11907 * of the checks on host state to the processor. If those fail,
11908 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011909 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011910 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011911
Jim Mattsonca0bde22016-11-30 12:03:46 -080011912 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11913 if (ret) {
11914 nested_vmx_entry_failure(vcpu, vmcs12,
11915 EXIT_REASON_INVALID_STATE, exit_qual);
11916 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011917 }
11918
11919 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011920 * We're finally done with prerequisite checking, and can start with
11921 * the nested entry.
11922 */
11923
Jim Mattson6514dc32018-04-26 16:09:12 -070011924 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011925 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070011926 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011927 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070011928 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011929 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070011930 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011931
Chao Gao135a06c2018-02-11 10:06:30 +080011932 /*
11933 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11934 * by event injection, halt vcpu.
11935 */
11936 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011937 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11938 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011939 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011940 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011941 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011942
11943out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011944 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011945}
11946
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011947/*
11948 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11949 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11950 * This function returns the new value we should put in vmcs12.guest_cr0.
11951 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11952 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11953 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11954 * didn't trap the bit, because if L1 did, so would L0).
11955 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11956 * been modified by L2, and L1 knows it. So just leave the old value of
11957 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11958 * isn't relevant, because if L0 traps this bit it can set it to anything.
11959 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11960 * changed these bits, and therefore they need to be updated, but L0
11961 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11962 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11963 */
11964static inline unsigned long
11965vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11966{
11967 return
11968 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11969 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11970 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11971 vcpu->arch.cr0_guest_owned_bits));
11972}
11973
11974static inline unsigned long
11975vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11976{
11977 return
11978 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11979 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11980 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11981 vcpu->arch.cr4_guest_owned_bits));
11982}
11983
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011984static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11985 struct vmcs12 *vmcs12)
11986{
11987 u32 idt_vectoring;
11988 unsigned int nr;
11989
Wanpeng Li664f8e22017-08-24 03:35:09 -070011990 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011991 nr = vcpu->arch.exception.nr;
11992 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11993
11994 if (kvm_exception_is_soft(nr)) {
11995 vmcs12->vm_exit_instruction_len =
11996 vcpu->arch.event_exit_inst_len;
11997 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11998 } else
11999 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12000
12001 if (vcpu->arch.exception.has_error_code) {
12002 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12003 vmcs12->idt_vectoring_error_code =
12004 vcpu->arch.exception.error_code;
12005 }
12006
12007 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012008 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012009 vmcs12->idt_vectoring_info_field =
12010 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012011 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012012 nr = vcpu->arch.interrupt.nr;
12013 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12014
12015 if (vcpu->arch.interrupt.soft) {
12016 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12017 vmcs12->vm_entry_instruction_len =
12018 vcpu->arch.event_exit_inst_len;
12019 } else
12020 idt_vectoring |= INTR_TYPE_EXT_INTR;
12021
12022 vmcs12->idt_vectoring_info_field = idt_vectoring;
12023 }
12024}
12025
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012026static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12027{
12028 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012029 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012030 bool block_nested_events =
12031 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012032
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012033 if (vcpu->arch.exception.pending &&
12034 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012035 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012036 return -EBUSY;
12037 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012038 return 0;
12039 }
12040
Jan Kiszkaf4124502014-03-07 20:03:13 +010012041 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12042 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012043 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012044 return -EBUSY;
12045 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12046 return 0;
12047 }
12048
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012049 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012050 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012051 return -EBUSY;
12052 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12053 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12054 INTR_INFO_VALID_MASK, 0);
12055 /*
12056 * The NMI-triggered VM exit counts as injection:
12057 * clear this one and block further NMIs.
12058 */
12059 vcpu->arch.nmi_pending = 0;
12060 vmx_set_nmi_mask(vcpu, true);
12061 return 0;
12062 }
12063
12064 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12065 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012066 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012067 return -EBUSY;
12068 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012069 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012070 }
12071
David Hildenbrand6342c502017-01-25 11:58:58 +010012072 vmx_complete_nested_posted_interrupt(vcpu);
12073 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012074}
12075
Jan Kiszkaf4124502014-03-07 20:03:13 +010012076static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12077{
12078 ktime_t remaining =
12079 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12080 u64 value;
12081
12082 if (ktime_to_ns(remaining) <= 0)
12083 return 0;
12084
12085 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12086 do_div(value, 1000000);
12087 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12088}
12089
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012090/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012091 * Update the guest state fields of vmcs12 to reflect changes that
12092 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12093 * VM-entry controls is also updated, since this is really a guest
12094 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012095 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012096static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012097{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012098 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12099 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12100
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012101 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12102 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12103 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12104
12105 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12106 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12107 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12108 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12109 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12110 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12111 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12112 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12113 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12114 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12115 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12116 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12117 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12118 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12119 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12120 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12121 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12122 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12123 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12124 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12125 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12126 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12127 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12128 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12129 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12130 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12131 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12132 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12133 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12134 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12135 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12136 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12137 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12138 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12139 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12140 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12141
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012142 vmcs12->guest_interruptibility_info =
12143 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12144 vmcs12->guest_pending_dbg_exceptions =
12145 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012146 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12147 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12148 else
12149 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012150
Jan Kiszkaf4124502014-03-07 20:03:13 +010012151 if (nested_cpu_has_preemption_timer(vmcs12)) {
12152 if (vmcs12->vm_exit_controls &
12153 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12154 vmcs12->vmx_preemption_timer_value =
12155 vmx_get_preemption_timer_value(vcpu);
12156 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12157 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012158
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012159 /*
12160 * In some cases (usually, nested EPT), L2 is allowed to change its
12161 * own CR3 without exiting. If it has changed it, we must keep it.
12162 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12163 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12164 *
12165 * Additionally, restore L2's PDPTR to vmcs12.
12166 */
12167 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012168 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012169 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12170 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12171 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12172 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12173 }
12174
Jim Mattsond281e132017-06-01 12:44:46 -070012175 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012176
Wincy Van608406e2015-02-03 23:57:51 +080012177 if (nested_cpu_has_vid(vmcs12))
12178 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12179
Jan Kiszkac18911a2013-03-13 16:06:41 +010012180 vmcs12->vm_entry_controls =
12181 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012182 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012183
Jan Kiszka2996fca2014-06-16 13:59:43 +020012184 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12185 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12186 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12187 }
12188
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012189 /* TODO: These cannot have changed unless we have MSR bitmaps and
12190 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012191 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012192 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012193 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12194 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012195 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12196 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12197 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012198 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012199 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012200}
12201
12202/*
12203 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12204 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12205 * and this function updates it to reflect the changes to the guest state while
12206 * L2 was running (and perhaps made some exits which were handled directly by L0
12207 * without going back to L1), and to reflect the exit reason.
12208 * Note that we do not have to copy here all VMCS fields, just those that
12209 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12210 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12211 * which already writes to vmcs12 directly.
12212 */
12213static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12214 u32 exit_reason, u32 exit_intr_info,
12215 unsigned long exit_qualification)
12216{
12217 /* update guest state fields: */
12218 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012219
12220 /* update exit information fields: */
12221
Jan Kiszka533558b2014-01-04 18:47:20 +010012222 vmcs12->vm_exit_reason = exit_reason;
12223 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012224 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012225
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012226 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012227 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12228 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12229
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012230 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012231 vmcs12->launch_state = 1;
12232
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012233 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12234 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012235 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012236
12237 /*
12238 * Transfer the event that L0 or L1 may wanted to inject into
12239 * L2 to IDT_VECTORING_INFO_FIELD.
12240 */
12241 vmcs12_save_pending_event(vcpu, vmcs12);
12242 }
12243
12244 /*
12245 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12246 * preserved above and would only end up incorrectly in L1.
12247 */
12248 vcpu->arch.nmi_injected = false;
12249 kvm_clear_exception_queue(vcpu);
12250 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012251}
12252
Wanpeng Li5af41572017-11-05 16:54:49 -080012253static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12254 struct vmcs12 *vmcs12)
12255{
12256 u32 entry_failure_code;
12257
12258 nested_ept_uninit_mmu_context(vcpu);
12259
12260 /*
12261 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12262 * couldn't have changed.
12263 */
12264 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12265 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12266
12267 if (!enable_ept)
12268 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12269}
12270
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012271/*
12272 * A part of what we need to when the nested L2 guest exits and we want to
12273 * run its L1 parent, is to reset L1's guest state to the host state specified
12274 * in vmcs12.
12275 * This function is to be called not only on normal nested exit, but also on
12276 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12277 * Failures During or After Loading Guest State").
12278 * This function should be called when the active VMCS is L1's (vmcs01).
12279 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012280static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12281 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012282{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012283 struct kvm_segment seg;
12284
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012285 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12286 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012287 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012288 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12289 else
12290 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12291 vmx_set_efer(vcpu, vcpu->arch.efer);
12292
12293 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12294 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012295 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012296 /*
12297 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012298 * actually changed, because vmx_set_cr0 refers to efer set above.
12299 *
12300 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12301 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012302 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012303 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012304 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012305
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012306 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012307 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012308 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012309
Wanpeng Li5af41572017-11-05 16:54:49 -080012310 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012311
Liran Alon6f1e03b2018-05-22 17:16:14 +030012312 /*
12313 * If vmcs01 don't use VPID, CPU flushes TLB on every
12314 * VMEntry/VMExit. Thus, no need to flush TLB.
12315 *
12316 * If vmcs12 uses VPID, TLB entries populated by L2 are
12317 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12318 * with vmx->vpid. Thus, no need to flush TLB.
12319 *
12320 * Therefore, flush TLB only in case vmcs01 uses VPID and
12321 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12322 * are both tagged with vmx->vpid.
12323 */
12324 if (enable_vpid &&
12325 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012326 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012327 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012328
12329 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12330 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12331 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12332 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12333 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012334 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12335 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012336
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012337 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12338 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12339 vmcs_write64(GUEST_BNDCFGS, 0);
12340
Jan Kiszka44811c02013-08-04 17:17:27 +020012341 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012342 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012343 vcpu->arch.pat = vmcs12->host_ia32_pat;
12344 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012345 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12346 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12347 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012348
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012349 /* Set L1 segment info according to Intel SDM
12350 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12351 seg = (struct kvm_segment) {
12352 .base = 0,
12353 .limit = 0xFFFFFFFF,
12354 .selector = vmcs12->host_cs_selector,
12355 .type = 11,
12356 .present = 1,
12357 .s = 1,
12358 .g = 1
12359 };
12360 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12361 seg.l = 1;
12362 else
12363 seg.db = 1;
12364 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12365 seg = (struct kvm_segment) {
12366 .base = 0,
12367 .limit = 0xFFFFFFFF,
12368 .type = 3,
12369 .present = 1,
12370 .s = 1,
12371 .db = 1,
12372 .g = 1
12373 };
12374 seg.selector = vmcs12->host_ds_selector;
12375 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12376 seg.selector = vmcs12->host_es_selector;
12377 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12378 seg.selector = vmcs12->host_ss_selector;
12379 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12380 seg.selector = vmcs12->host_fs_selector;
12381 seg.base = vmcs12->host_fs_base;
12382 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12383 seg.selector = vmcs12->host_gs_selector;
12384 seg.base = vmcs12->host_gs_base;
12385 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12386 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012387 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012388 .limit = 0x67,
12389 .selector = vmcs12->host_tr_selector,
12390 .type = 11,
12391 .present = 1
12392 };
12393 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12394
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012395 kvm_set_dr(vcpu, 7, 0x400);
12396 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012397
Wincy Van3af18d92015-02-03 23:49:31 +080012398 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012399 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012400
Wincy Vanff651cb2014-12-11 08:52:58 +030012401 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12402 vmcs12->vm_exit_msr_load_count))
12403 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012404}
12405
12406/*
12407 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12408 * and modify vmcs12 to make it see what it would expect to see there if
12409 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12410 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012411static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12412 u32 exit_intr_info,
12413 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012414{
12415 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012416 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12417
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012418 /* trying to cancel vmlaunch/vmresume is a bug */
12419 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12420
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012421 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012422 * The only expected VM-instruction error is "VM entry with
12423 * invalid control field(s)." Anything else indicates a
12424 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012425 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012426 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12427 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12428
12429 leave_guest_mode(vcpu);
12430
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012431 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12432 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12433
Jim Mattson4f350c62017-09-14 16:31:44 -070012434 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012435 if (exit_reason == -1)
12436 sync_vmcs12(vcpu, vmcs12);
12437 else
12438 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12439 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012440
12441 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12442 vmcs12->vm_exit_msr_store_count))
12443 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012444 }
12445
Jim Mattson4f350c62017-09-14 16:31:44 -070012446 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012447 vm_entry_controls_reset_shadow(vmx);
12448 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012449 vmx_segment_cache_clear(vmx);
12450
Paolo Bonzini93140062016-07-06 13:23:51 +020012451 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012452 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12453 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012454 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012455 if (vmx->hv_deadline_tsc == -1)
12456 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12457 PIN_BASED_VMX_PREEMPTION_TIMER);
12458 else
12459 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12460 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012461 if (kvm_has_tsc_control)
12462 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012463
Jim Mattson8d860bb2018-05-09 16:56:05 -040012464 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12465 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12466 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012467 } else if (!nested_cpu_has_ept(vmcs12) &&
12468 nested_cpu_has2(vmcs12,
12469 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012470 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012471 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012472
12473 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12474 vmx->host_rsp = 0;
12475
12476 /* Unpin physical memory we referred to in vmcs02 */
12477 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012478 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012479 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012480 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012481 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012482 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012483 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012484 }
Wincy Van705699a2015-02-03 23:58:17 +080012485 if (vmx->nested.pi_desc_page) {
12486 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012487 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012488 vmx->nested.pi_desc_page = NULL;
12489 vmx->nested.pi_desc = NULL;
12490 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012491
12492 /*
Tang Chen38b99172014-09-24 15:57:54 +080012493 * We are now running in L2, mmu_notifier will force to reload the
12494 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12495 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012496 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012497
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012498 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012499 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012500
12501 /* in case we halted in L2 */
12502 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012503
12504 if (likely(!vmx->fail)) {
12505 /*
12506 * TODO: SDM says that with acknowledge interrupt on
12507 * exit, bit 31 of the VM-exit interrupt information
12508 * (valid interrupt) is always set to 1 on
12509 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12510 * need kvm_cpu_has_interrupt(). See the commit
12511 * message for details.
12512 */
12513 if (nested_exit_intr_ack_set(vcpu) &&
12514 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12515 kvm_cpu_has_interrupt(vcpu)) {
12516 int irq = kvm_cpu_get_interrupt(vcpu);
12517 WARN_ON(irq < 0);
12518 vmcs12->vm_exit_intr_info = irq |
12519 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12520 }
12521
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012522 if (exit_reason != -1)
12523 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12524 vmcs12->exit_qualification,
12525 vmcs12->idt_vectoring_info_field,
12526 vmcs12->vm_exit_intr_info,
12527 vmcs12->vm_exit_intr_error_code,
12528 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012529
12530 load_vmcs12_host_state(vcpu, vmcs12);
12531
12532 return;
12533 }
12534
12535 /*
12536 * After an early L2 VM-entry failure, we're now back
12537 * in L1 which thinks it just finished a VMLAUNCH or
12538 * VMRESUME instruction, so we need to set the failure
12539 * flag and the VM-instruction error field of the VMCS
12540 * accordingly.
12541 */
12542 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012543
12544 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12545
Jim Mattson4f350c62017-09-14 16:31:44 -070012546 /*
12547 * The emulated instruction was already skipped in
12548 * nested_vmx_run, but the updated RIP was never
12549 * written back to the vmcs01.
12550 */
12551 skip_emulated_instruction(vcpu);
12552 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012553}
12554
Nadav Har'El7c177932011-05-25 23:12:04 +030012555/*
Jan Kiszka42124922014-01-04 18:47:19 +010012556 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12557 */
12558static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12559{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012560 if (is_guest_mode(vcpu)) {
12561 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012562 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012563 }
Jan Kiszka42124922014-01-04 18:47:19 +010012564 free_nested(to_vmx(vcpu));
12565}
12566
12567/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012568 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12569 * 23.7 "VM-entry failures during or after loading guest state" (this also
12570 * lists the acceptable exit-reason and exit-qualification parameters).
12571 * It should only be called before L2 actually succeeded to run, and when
12572 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12573 */
12574static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12575 struct vmcs12 *vmcs12,
12576 u32 reason, unsigned long qualification)
12577{
12578 load_vmcs12_host_state(vcpu, vmcs12);
12579 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12580 vmcs12->exit_qualification = qualification;
12581 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012582 if (enable_shadow_vmcs)
12583 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012584}
12585
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012586static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12587 struct x86_instruction_info *info,
12588 enum x86_intercept_stage stage)
12589{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012590 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12591 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12592
12593 /*
12594 * RDPID causes #UD if disabled through secondary execution controls.
12595 * Because it is marked as EmulateOnUD, we need to intercept it here.
12596 */
12597 if (info->intercept == x86_intercept_rdtscp &&
12598 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12599 ctxt->exception.vector = UD_VECTOR;
12600 ctxt->exception.error_code_valid = false;
12601 return X86EMUL_PROPAGATE_FAULT;
12602 }
12603
12604 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012605 return X86EMUL_CONTINUE;
12606}
12607
Yunhong Jiang64672c92016-06-13 14:19:59 -070012608#ifdef CONFIG_X86_64
12609/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12610static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12611 u64 divisor, u64 *result)
12612{
12613 u64 low = a << shift, high = a >> (64 - shift);
12614
12615 /* To avoid the overflow on divq */
12616 if (high >= divisor)
12617 return 1;
12618
12619 /* Low hold the result, high hold rem which is discarded */
12620 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12621 "rm" (divisor), "0" (low), "1" (high));
12622 *result = low;
12623
12624 return 0;
12625}
12626
12627static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12628{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012629 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012630 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012631
12632 if (kvm_mwait_in_guest(vcpu->kvm))
12633 return -EOPNOTSUPP;
12634
12635 vmx = to_vmx(vcpu);
12636 tscl = rdtsc();
12637 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12638 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012639 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12640
12641 if (delta_tsc > lapic_timer_advance_cycles)
12642 delta_tsc -= lapic_timer_advance_cycles;
12643 else
12644 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012645
12646 /* Convert to host delta tsc if tsc scaling is enabled */
12647 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12648 u64_shl_div_u64(delta_tsc,
12649 kvm_tsc_scaling_ratio_frac_bits,
12650 vcpu->arch.tsc_scaling_ratio,
12651 &delta_tsc))
12652 return -ERANGE;
12653
12654 /*
12655 * If the delta tsc can't fit in the 32 bit after the multi shift,
12656 * we can't use the preemption timer.
12657 * It's possible that it fits on later vmentries, but checking
12658 * on every vmentry is costly so we just use an hrtimer.
12659 */
12660 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12661 return -ERANGE;
12662
12663 vmx->hv_deadline_tsc = tscl + delta_tsc;
12664 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12665 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012666
12667 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012668}
12669
12670static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12671{
12672 struct vcpu_vmx *vmx = to_vmx(vcpu);
12673 vmx->hv_deadline_tsc = -1;
12674 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12675 PIN_BASED_VMX_PREEMPTION_TIMER);
12676}
12677#endif
12678
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012679static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012680{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012681 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012682 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012683}
12684
Kai Huang843e4332015-01-28 10:54:28 +080012685static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12686 struct kvm_memory_slot *slot)
12687{
12688 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12689 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12690}
12691
12692static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12693 struct kvm_memory_slot *slot)
12694{
12695 kvm_mmu_slot_set_dirty(kvm, slot);
12696}
12697
12698static void vmx_flush_log_dirty(struct kvm *kvm)
12699{
12700 kvm_flush_pml_buffers(kvm);
12701}
12702
Bandan Dasc5f983f2017-05-05 15:25:14 -040012703static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12704{
12705 struct vmcs12 *vmcs12;
12706 struct vcpu_vmx *vmx = to_vmx(vcpu);
12707 gpa_t gpa;
12708 struct page *page = NULL;
12709 u64 *pml_address;
12710
12711 if (is_guest_mode(vcpu)) {
12712 WARN_ON_ONCE(vmx->nested.pml_full);
12713
12714 /*
12715 * Check if PML is enabled for the nested guest.
12716 * Whether eptp bit 6 is set is already checked
12717 * as part of A/D emulation.
12718 */
12719 vmcs12 = get_vmcs12(vcpu);
12720 if (!nested_cpu_has_pml(vmcs12))
12721 return 0;
12722
Dan Carpenter47698862017-05-10 22:43:17 +030012723 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012724 vmx->nested.pml_full = true;
12725 return 1;
12726 }
12727
12728 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12729
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012730 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12731 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012732 return 0;
12733
12734 pml_address = kmap(page);
12735 pml_address[vmcs12->guest_pml_index--] = gpa;
12736 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012737 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012738 }
12739
12740 return 0;
12741}
12742
Kai Huang843e4332015-01-28 10:54:28 +080012743static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12744 struct kvm_memory_slot *memslot,
12745 gfn_t offset, unsigned long mask)
12746{
12747 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12748}
12749
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012750static void __pi_post_block(struct kvm_vcpu *vcpu)
12751{
12752 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12753 struct pi_desc old, new;
12754 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012755
12756 do {
12757 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012758 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12759 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012760
12761 dest = cpu_physical_id(vcpu->cpu);
12762
12763 if (x2apic_enabled())
12764 new.ndst = dest;
12765 else
12766 new.ndst = (dest << 8) & 0xFF00;
12767
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012768 /* set 'NV' to 'notification vector' */
12769 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012770 } while (cmpxchg64(&pi_desc->control, old.control,
12771 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012772
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012773 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12774 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012775 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012776 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012777 vcpu->pre_pcpu = -1;
12778 }
12779}
12780
Feng Wuefc64402015-09-18 22:29:51 +080012781/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012782 * This routine does the following things for vCPU which is going
12783 * to be blocked if VT-d PI is enabled.
12784 * - Store the vCPU to the wakeup list, so when interrupts happen
12785 * we can find the right vCPU to wake up.
12786 * - Change the Posted-interrupt descriptor as below:
12787 * 'NDST' <-- vcpu->pre_pcpu
12788 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12789 * - If 'ON' is set during this process, which means at least one
12790 * interrupt is posted for this vCPU, we cannot block it, in
12791 * this case, return 1, otherwise, return 0.
12792 *
12793 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012794static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012795{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012796 unsigned int dest;
12797 struct pi_desc old, new;
12798 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12799
12800 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012801 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12802 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012803 return 0;
12804
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012805 WARN_ON(irqs_disabled());
12806 local_irq_disable();
12807 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12808 vcpu->pre_pcpu = vcpu->cpu;
12809 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12810 list_add_tail(&vcpu->blocked_vcpu_list,
12811 &per_cpu(blocked_vcpu_on_cpu,
12812 vcpu->pre_pcpu));
12813 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12814 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012815
12816 do {
12817 old.control = new.control = pi_desc->control;
12818
Feng Wubf9f6ac2015-09-18 22:29:55 +080012819 WARN((pi_desc->sn == 1),
12820 "Warning: SN field of posted-interrupts "
12821 "is set before blocking\n");
12822
12823 /*
12824 * Since vCPU can be preempted during this process,
12825 * vcpu->cpu could be different with pre_pcpu, we
12826 * need to set pre_pcpu as the destination of wakeup
12827 * notification event, then we can find the right vCPU
12828 * to wakeup in wakeup handler if interrupts happen
12829 * when the vCPU is in blocked state.
12830 */
12831 dest = cpu_physical_id(vcpu->pre_pcpu);
12832
12833 if (x2apic_enabled())
12834 new.ndst = dest;
12835 else
12836 new.ndst = (dest << 8) & 0xFF00;
12837
12838 /* set 'NV' to 'wakeup vector' */
12839 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012840 } while (cmpxchg64(&pi_desc->control, old.control,
12841 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012842
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012843 /* We should not block the vCPU if an interrupt is posted for it. */
12844 if (pi_test_on(pi_desc) == 1)
12845 __pi_post_block(vcpu);
12846
12847 local_irq_enable();
12848 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012849}
12850
Yunhong Jiangbc225122016-06-13 14:19:58 -070012851static int vmx_pre_block(struct kvm_vcpu *vcpu)
12852{
12853 if (pi_pre_block(vcpu))
12854 return 1;
12855
Yunhong Jiang64672c92016-06-13 14:19:59 -070012856 if (kvm_lapic_hv_timer_in_use(vcpu))
12857 kvm_lapic_switch_to_sw_timer(vcpu);
12858
Yunhong Jiangbc225122016-06-13 14:19:58 -070012859 return 0;
12860}
12861
12862static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012863{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012864 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012865 return;
12866
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012867 WARN_ON(irqs_disabled());
12868 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012869 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012870 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012871}
12872
Yunhong Jiangbc225122016-06-13 14:19:58 -070012873static void vmx_post_block(struct kvm_vcpu *vcpu)
12874{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012875 if (kvm_x86_ops->set_hv_timer)
12876 kvm_lapic_switch_to_hv_timer(vcpu);
12877
Yunhong Jiangbc225122016-06-13 14:19:58 -070012878 pi_post_block(vcpu);
12879}
12880
Feng Wubf9f6ac2015-09-18 22:29:55 +080012881/*
Feng Wuefc64402015-09-18 22:29:51 +080012882 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12883 *
12884 * @kvm: kvm
12885 * @host_irq: host irq of the interrupt
12886 * @guest_irq: gsi of the interrupt
12887 * @set: set or unset PI
12888 * returns 0 on success, < 0 on failure
12889 */
12890static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12891 uint32_t guest_irq, bool set)
12892{
12893 struct kvm_kernel_irq_routing_entry *e;
12894 struct kvm_irq_routing_table *irq_rt;
12895 struct kvm_lapic_irq irq;
12896 struct kvm_vcpu *vcpu;
12897 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012898 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012899
12900 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012901 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12902 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012903 return 0;
12904
12905 idx = srcu_read_lock(&kvm->irq_srcu);
12906 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012907 if (guest_irq >= irq_rt->nr_rt_entries ||
12908 hlist_empty(&irq_rt->map[guest_irq])) {
12909 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12910 guest_irq, irq_rt->nr_rt_entries);
12911 goto out;
12912 }
Feng Wuefc64402015-09-18 22:29:51 +080012913
12914 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12915 if (e->type != KVM_IRQ_ROUTING_MSI)
12916 continue;
12917 /*
12918 * VT-d PI cannot support posting multicast/broadcast
12919 * interrupts to a vCPU, we still use interrupt remapping
12920 * for these kind of interrupts.
12921 *
12922 * For lowest-priority interrupts, we only support
12923 * those with single CPU as the destination, e.g. user
12924 * configures the interrupts via /proc/irq or uses
12925 * irqbalance to make the interrupts single-CPU.
12926 *
12927 * We will support full lowest-priority interrupt later.
12928 */
12929
Radim Krčmář371313132016-07-12 22:09:27 +020012930 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012931 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12932 /*
12933 * Make sure the IRTE is in remapped mode if
12934 * we don't handle it in posted mode.
12935 */
12936 ret = irq_set_vcpu_affinity(host_irq, NULL);
12937 if (ret < 0) {
12938 printk(KERN_INFO
12939 "failed to back to remapped mode, irq: %u\n",
12940 host_irq);
12941 goto out;
12942 }
12943
Feng Wuefc64402015-09-18 22:29:51 +080012944 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012945 }
Feng Wuefc64402015-09-18 22:29:51 +080012946
12947 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12948 vcpu_info.vector = irq.vector;
12949
hu huajun2698d822018-04-11 15:16:40 +080012950 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012951 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12952
12953 if (set)
12954 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012955 else
Feng Wuefc64402015-09-18 22:29:51 +080012956 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012957
12958 if (ret < 0) {
12959 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12960 __func__);
12961 goto out;
12962 }
12963 }
12964
12965 ret = 0;
12966out:
12967 srcu_read_unlock(&kvm->irq_srcu, idx);
12968 return ret;
12969}
12970
Ashok Rajc45dcc72016-06-22 14:59:56 +080012971static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12972{
12973 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12974 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12975 FEATURE_CONTROL_LMCE;
12976 else
12977 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12978 ~FEATURE_CONTROL_LMCE;
12979}
12980
Ladi Prosek72d7b372017-10-11 16:54:41 +020012981static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12982{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012983 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12984 if (to_vmx(vcpu)->nested.nested_run_pending)
12985 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012986 return 1;
12987}
12988
Ladi Prosek0234bf82017-10-11 16:54:40 +020012989static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12990{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012991 struct vcpu_vmx *vmx = to_vmx(vcpu);
12992
12993 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12994 if (vmx->nested.smm.guest_mode)
12995 nested_vmx_vmexit(vcpu, -1, 0, 0);
12996
12997 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12998 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012999 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013000 return 0;
13001}
13002
13003static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13004{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013005 struct vcpu_vmx *vmx = to_vmx(vcpu);
13006 int ret;
13007
13008 if (vmx->nested.smm.vmxon) {
13009 vmx->nested.vmxon = true;
13010 vmx->nested.smm.vmxon = false;
13011 }
13012
13013 if (vmx->nested.smm.guest_mode) {
13014 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013015 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013016 vcpu->arch.hflags |= HF_SMM_MASK;
13017 if (ret)
13018 return ret;
13019
13020 vmx->nested.smm.guest_mode = false;
13021 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013022 return 0;
13023}
13024
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013025static int enable_smi_window(struct kvm_vcpu *vcpu)
13026{
13027 return 0;
13028}
13029
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013030static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13031 struct kvm_nested_state __user *user_kvm_nested_state,
13032 u32 user_data_size)
13033{
13034 struct vcpu_vmx *vmx;
13035 struct vmcs12 *vmcs12;
13036 struct kvm_nested_state kvm_state = {
13037 .flags = 0,
13038 .format = 0,
13039 .size = sizeof(kvm_state),
13040 .vmx.vmxon_pa = -1ull,
13041 .vmx.vmcs_pa = -1ull,
13042 };
13043
13044 if (!vcpu)
13045 return kvm_state.size + 2 * VMCS12_SIZE;
13046
13047 vmx = to_vmx(vcpu);
13048 vmcs12 = get_vmcs12(vcpu);
13049 if (nested_vmx_allowed(vcpu) &&
13050 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13051 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13052 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13053
13054 if (vmx->nested.current_vmptr != -1ull)
13055 kvm_state.size += VMCS12_SIZE;
13056
13057 if (vmx->nested.smm.vmxon)
13058 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13059
13060 if (vmx->nested.smm.guest_mode)
13061 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13062
13063 if (is_guest_mode(vcpu)) {
13064 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13065
13066 if (vmx->nested.nested_run_pending)
13067 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13068 }
13069 }
13070
13071 if (user_data_size < kvm_state.size)
13072 goto out;
13073
13074 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13075 return -EFAULT;
13076
13077 if (vmx->nested.current_vmptr == -1ull)
13078 goto out;
13079
13080 /*
13081 * When running L2, the authoritative vmcs12 state is in the
13082 * vmcs02. When running L1, the authoritative vmcs12 state is
13083 * in the shadow vmcs linked to vmcs01, unless
13084 * sync_shadow_vmcs is set, in which case, the authoritative
13085 * vmcs12 state is in the vmcs12 already.
13086 */
13087 if (is_guest_mode(vcpu))
13088 sync_vmcs12(vcpu, vmcs12);
13089 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13090 copy_shadow_to_vmcs12(vmx);
13091
13092 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13093 return -EFAULT;
13094
13095out:
13096 return kvm_state.size;
13097}
13098
13099static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13100 struct kvm_nested_state __user *user_kvm_nested_state,
13101 struct kvm_nested_state *kvm_state)
13102{
13103 struct vcpu_vmx *vmx = to_vmx(vcpu);
13104 struct vmcs12 *vmcs12;
13105 u32 exit_qual;
13106 int ret;
13107
13108 if (kvm_state->format != 0)
13109 return -EINVAL;
13110
13111 if (!nested_vmx_allowed(vcpu))
13112 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13113
13114 if (kvm_state->vmx.vmxon_pa == -1ull) {
13115 if (kvm_state->vmx.smm.flags)
13116 return -EINVAL;
13117
13118 if (kvm_state->vmx.vmcs_pa != -1ull)
13119 return -EINVAL;
13120
13121 vmx_leave_nested(vcpu);
13122 return 0;
13123 }
13124
13125 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13126 return -EINVAL;
13127
13128 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13129 return -EINVAL;
13130
13131 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13132 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13133 return -EINVAL;
13134
13135 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13136 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13137 return -EINVAL;
13138
13139 if (kvm_state->vmx.smm.flags &
13140 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13141 return -EINVAL;
13142
13143 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13144 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13145 return -EINVAL;
13146
13147 vmx_leave_nested(vcpu);
13148 if (kvm_state->vmx.vmxon_pa == -1ull)
13149 return 0;
13150
13151 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13152 ret = enter_vmx_operation(vcpu);
13153 if (ret)
13154 return ret;
13155
13156 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13157
13158 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13159 vmx->nested.smm.vmxon = true;
13160 vmx->nested.vmxon = false;
13161
13162 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13163 vmx->nested.smm.guest_mode = true;
13164 }
13165
13166 vmcs12 = get_vmcs12(vcpu);
13167 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13168 return -EFAULT;
13169
Liran Alon392b2f22018-06-23 02:35:01 +030013170 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013171 return -EINVAL;
13172
13173 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13174 return 0;
13175
13176 vmx->nested.nested_run_pending =
13177 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13178
13179 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13180 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13181 return -EINVAL;
13182
13183 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13184 vmx->nested.nested_run_pending = 1;
13185
13186 vmx->nested.dirty_vmcs12 = true;
13187 ret = enter_vmx_non_root_mode(vcpu, NULL);
13188 if (ret)
13189 return -EINVAL;
13190
13191 return 0;
13192}
13193
Kees Cook404f6aa2016-08-08 16:29:06 -070013194static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013195 .cpu_has_kvm_support = cpu_has_kvm_support,
13196 .disabled_by_bios = vmx_disabled_by_bios,
13197 .hardware_setup = hardware_setup,
13198 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013199 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013200 .hardware_enable = hardware_enable,
13201 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013202 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013203 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013204
Wanpeng Lib31c1142018-03-12 04:53:04 -070013205 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013206 .vm_alloc = vmx_vm_alloc,
13207 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013208
Avi Kivity6aa8b732006-12-10 02:21:36 -080013209 .vcpu_create = vmx_create_vcpu,
13210 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013211 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013212
Avi Kivity04d2cc72007-09-10 18:10:54 +030013213 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013214 .vcpu_load = vmx_vcpu_load,
13215 .vcpu_put = vmx_vcpu_put,
13216
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013217 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013218 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013219 .get_msr = vmx_get_msr,
13220 .set_msr = vmx_set_msr,
13221 .get_segment_base = vmx_get_segment_base,
13222 .get_segment = vmx_get_segment,
13223 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013224 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013225 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013226 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013227 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013228 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013229 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013230 .set_cr3 = vmx_set_cr3,
13231 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013232 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013233 .get_idt = vmx_get_idt,
13234 .set_idt = vmx_set_idt,
13235 .get_gdt = vmx_get_gdt,
13236 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013237 .get_dr6 = vmx_get_dr6,
13238 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013239 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013240 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013241 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013242 .get_rflags = vmx_get_rflags,
13243 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013244
Avi Kivity6aa8b732006-12-10 02:21:36 -080013245 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013246
Avi Kivity6aa8b732006-12-10 02:21:36 -080013247 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013248 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013249 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013250 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13251 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013252 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013253 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013254 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013255 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013256 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013257 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013258 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013259 .get_nmi_mask = vmx_get_nmi_mask,
13260 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013261 .enable_nmi_window = enable_nmi_window,
13262 .enable_irq_window = enable_irq_window,
13263 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013264 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013265 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013266 .get_enable_apicv = vmx_get_enable_apicv,
13267 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013268 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013269 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013270 .hwapic_irr_update = vmx_hwapic_irr_update,
13271 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013272 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13273 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013274
Izik Eiduscbc94022007-10-25 00:29:55 +020013275 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013276 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013277 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013278 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013279
Avi Kivity586f9602010-11-18 13:09:54 +020013280 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013281
Sheng Yang17cc3932010-01-05 19:02:27 +080013282 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013283
13284 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013285
13286 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013287 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013288
13289 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013290
13291 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013292
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013293 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013294 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013295
13296 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013297
13298 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013299 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013300 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013301 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013302 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013303
13304 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013305
13306 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013307
13308 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13309 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13310 .flush_log_dirty = vmx_flush_log_dirty,
13311 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013312 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013313
Feng Wubf9f6ac2015-09-18 22:29:55 +080013314 .pre_block = vmx_pre_block,
13315 .post_block = vmx_post_block,
13316
Wei Huang25462f72015-06-19 15:45:05 +020013317 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013318
13319 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013320
13321#ifdef CONFIG_X86_64
13322 .set_hv_timer = vmx_set_hv_timer,
13323 .cancel_hv_timer = vmx_cancel_hv_timer,
13324#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013325
13326 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013327
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013328 .get_nested_state = vmx_get_nested_state,
13329 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013330 .get_vmcs12_pages = nested_get_vmcs12_pages,
13331
Ladi Prosek72d7b372017-10-11 16:54:41 +020013332 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013333 .pre_enter_smm = vmx_pre_enter_smm,
13334 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013335 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013336};
13337
13338static int __init vmx_init(void)
13339{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013340 int r;
13341
13342#if IS_ENABLED(CONFIG_HYPERV)
13343 /*
13344 * Enlightened VMCS usage should be recommended and the host needs
13345 * to support eVMCS v1 or above. We can also disable eVMCS support
13346 * with module parameter.
13347 */
13348 if (enlightened_vmcs &&
13349 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13350 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13351 KVM_EVMCS_VERSION) {
13352 int cpu;
13353
13354 /* Check that we have assist pages on all online CPUs */
13355 for_each_online_cpu(cpu) {
13356 if (!hv_get_vp_assist_page(cpu)) {
13357 enlightened_vmcs = false;
13358 break;
13359 }
13360 }
13361
13362 if (enlightened_vmcs) {
13363 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13364 static_branch_enable(&enable_evmcs);
13365 }
13366 } else {
13367 enlightened_vmcs = false;
13368 }
13369#endif
13370
13371 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013372 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013373 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013374 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013375
Dave Young2965faa2015-09-09 15:38:55 -070013376#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013377 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13378 crash_vmclear_local_loaded_vmcss);
13379#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013380 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013381
He, Qingfdef3ad2007-04-30 09:45:24 +030013382 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013383}
13384
13385static void __exit vmx_exit(void)
13386{
Dave Young2965faa2015-09-09 15:38:55 -070013387#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013388 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013389 synchronize_rcu();
13390#endif
13391
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013392 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013393
13394#if IS_ENABLED(CONFIG_HYPERV)
13395 if (static_branch_unlikely(&enable_evmcs)) {
13396 int cpu;
13397 struct hv_vp_assist_page *vp_ap;
13398 /*
13399 * Reset everything to support using non-enlightened VMCS
13400 * access later (e.g. when we reload the module with
13401 * enlightened_vmcs=0)
13402 */
13403 for_each_online_cpu(cpu) {
13404 vp_ap = hv_get_vp_assist_page(cpu);
13405
13406 if (!vp_ap)
13407 continue;
13408
13409 vp_ap->current_nested_vmcs = 0;
13410 vp_ap->enlighten_vmentry = 0;
13411 }
13412
13413 static_branch_disable(&enable_evmcs);
13414 }
13415#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013416}
13417
13418module_init(vmx_init)
13419module_exit(vmx_exit)