blob: f8ad42bcfc2dc10b06b39c8e1a1b7674088c5576 [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
Liran Alonfa97d7d2018-07-18 14:07:59 +02001723static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1724{
1725 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1726 SECONDARY_EXEC_SHADOW_VMCS;
1727}
1728
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001729static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1730{
1731 return vmcs12->cpu_based_vm_exec_control & bit;
1732}
1733
1734static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1735{
1736 return (vmcs12->cpu_based_vm_exec_control &
1737 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1738 (vmcs12->secondary_vm_exec_control & bit);
1739}
1740
Jan Kiszkaf4124502014-03-07 20:03:13 +01001741static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1742{
1743 return vmcs12->pin_based_vm_exec_control &
1744 PIN_BASED_VMX_PREEMPTION_TIMER;
1745}
1746
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001747static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1748{
1749 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1750}
1751
1752static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1753{
1754 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1755}
1756
Nadav Har'El155a97a2013-08-05 11:07:16 +03001757static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1758{
1759 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1760}
1761
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001762static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1763{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001764 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001765}
1766
Bandan Dasc5f983f2017-05-05 15:25:14 -04001767static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1768{
1769 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1770}
1771
Wincy Vanf2b93282015-02-03 23:56:03 +08001772static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1773{
1774 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1775}
1776
Wanpeng Li5c614b32015-10-13 09:18:36 -07001777static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1778{
1779 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1780}
1781
Wincy Van82f0dd42015-02-03 23:57:18 +08001782static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1783{
1784 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1785}
1786
Wincy Van608406e2015-02-03 23:57:51 +08001787static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1788{
1789 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1790}
1791
Wincy Van705699a2015-02-03 23:58:17 +08001792static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1793{
1794 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1795}
1796
Bandan Das27c42a12017-08-03 15:54:42 -04001797static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1798{
1799 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1800}
1801
Bandan Das41ab9372017-08-03 15:54:43 -04001802static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1803{
1804 return nested_cpu_has_vmfunc(vmcs12) &&
1805 (vmcs12->vm_function_control &
1806 VMX_VMFUNC_EPTP_SWITCHING);
1807}
1808
Liran Alonf792d272018-06-23 02:35:05 +03001809static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1810{
1811 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1812}
1813
Jim Mattsonef85b672016-12-12 11:01:37 -08001814static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001815{
1816 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001817 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001818}
1819
Jan Kiszka533558b2014-01-04 18:47:20 +01001820static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1821 u32 exit_intr_info,
1822 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001823static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1824 struct vmcs12 *vmcs12,
1825 u32 reason, unsigned long qualification);
1826
Rusty Russell8b9cf982007-07-30 16:31:43 +10001827static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001828{
1829 int i;
1830
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001831 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001832 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001833 return i;
1834 return -1;
1835}
1836
Sheng Yang2384d2b2008-01-17 15:14:33 +08001837static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1838{
1839 struct {
1840 u64 vpid : 16;
1841 u64 rsvd : 48;
1842 u64 gva;
1843 } operand = { vpid, 0, gva };
1844
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001845 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001846 /* CF==1 or ZF==1 --> rc = -1 */
1847 "; ja 1f ; ud2 ; 1:"
1848 : : "a"(&operand), "c"(ext) : "cc", "memory");
1849}
1850
Sheng Yang14394422008-04-28 12:24:45 +08001851static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1852{
1853 struct {
1854 u64 eptp, gpa;
1855 } operand = {eptp, gpa};
1856
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001857 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001858 /* CF==1 or ZF==1 --> rc = -1 */
1859 "; ja 1f ; ud2 ; 1:\n"
1860 : : "a" (&operand), "c" (ext) : "cc", "memory");
1861}
1862
Avi Kivity26bb0982009-09-07 11:14:12 +03001863static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001864{
1865 int i;
1866
Rusty Russell8b9cf982007-07-30 16:31:43 +10001867 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001868 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001869 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001870 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001871}
1872
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873static void vmcs_clear(struct vmcs *vmcs)
1874{
1875 u64 phys_addr = __pa(vmcs);
1876 u8 error;
1877
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001878 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001879 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 : "cc", "memory");
1881 if (error)
1882 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1883 vmcs, phys_addr);
1884}
1885
Nadav Har'Eld462b812011-05-24 15:26:10 +03001886static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1887{
1888 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001889 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1890 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001891 loaded_vmcs->cpu = -1;
1892 loaded_vmcs->launched = 0;
1893}
1894
Dongxiao Xu7725b892010-05-11 18:29:38 +08001895static void vmcs_load(struct vmcs *vmcs)
1896{
1897 u64 phys_addr = __pa(vmcs);
1898 u8 error;
1899
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001900 if (static_branch_unlikely(&enable_evmcs))
1901 return evmcs_load(phys_addr);
1902
Dongxiao Xu7725b892010-05-11 18:29:38 +08001903 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001904 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001905 : "cc", "memory");
1906 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001907 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001908 vmcs, phys_addr);
1909}
1910
Dave Young2965faa2015-09-09 15:38:55 -07001911#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001912/*
1913 * This bitmap is used to indicate whether the vmclear
1914 * operation is enabled on all cpus. All disabled by
1915 * default.
1916 */
1917static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1918
1919static inline void crash_enable_local_vmclear(int cpu)
1920{
1921 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1922}
1923
1924static inline void crash_disable_local_vmclear(int cpu)
1925{
1926 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1927}
1928
1929static inline int crash_local_vmclear_enabled(int cpu)
1930{
1931 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1932}
1933
1934static void crash_vmclear_local_loaded_vmcss(void)
1935{
1936 int cpu = raw_smp_processor_id();
1937 struct loaded_vmcs *v;
1938
1939 if (!crash_local_vmclear_enabled(cpu))
1940 return;
1941
1942 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1943 loaded_vmcss_on_cpu_link)
1944 vmcs_clear(v->vmcs);
1945}
1946#else
1947static inline void crash_enable_local_vmclear(int cpu) { }
1948static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001949#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001950
Nadav Har'Eld462b812011-05-24 15:26:10 +03001951static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001953 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001954 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955
Nadav Har'Eld462b812011-05-24 15:26:10 +03001956 if (loaded_vmcs->cpu != cpu)
1957 return; /* vcpu migration can race with cpu offline */
1958 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001960 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001961 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001962
1963 /*
1964 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1965 * is before setting loaded_vmcs->vcpu to -1 which is done in
1966 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1967 * then adds the vmcs into percpu list before it is deleted.
1968 */
1969 smp_wmb();
1970
Nadav Har'Eld462b812011-05-24 15:26:10 +03001971 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001972 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973}
1974
Nadav Har'Eld462b812011-05-24 15:26:10 +03001975static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001976{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001977 int cpu = loaded_vmcs->cpu;
1978
1979 if (cpu != -1)
1980 smp_call_function_single(cpu,
1981 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001982}
1983
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001984static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001985{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001986 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001987 return;
1988
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001989 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001990 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001991}
1992
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001993static inline void vpid_sync_vcpu_global(void)
1994{
1995 if (cpu_has_vmx_invvpid_global())
1996 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1997}
1998
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001999static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002000{
2001 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002002 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002003 else
2004 vpid_sync_vcpu_global();
2005}
2006
Sheng Yang14394422008-04-28 12:24:45 +08002007static inline void ept_sync_global(void)
2008{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002009 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002010}
2011
2012static inline void ept_sync_context(u64 eptp)
2013{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002014 if (cpu_has_vmx_invept_context())
2015 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2016 else
2017 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002018}
2019
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002020static __always_inline void vmcs_check16(unsigned long field)
2021{
2022 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2023 "16-bit accessor invalid for 64-bit field");
2024 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2025 "16-bit accessor invalid for 64-bit high field");
2026 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2027 "16-bit accessor invalid for 32-bit high field");
2028 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2029 "16-bit accessor invalid for natural width field");
2030}
2031
2032static __always_inline void vmcs_check32(unsigned long field)
2033{
2034 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2035 "32-bit accessor invalid for 16-bit field");
2036 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2037 "32-bit accessor invalid for natural width field");
2038}
2039
2040static __always_inline void vmcs_check64(unsigned long field)
2041{
2042 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2043 "64-bit accessor invalid for 16-bit field");
2044 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2045 "64-bit accessor invalid for 64-bit high field");
2046 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2047 "64-bit accessor invalid for 32-bit field");
2048 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2049 "64-bit accessor invalid for natural width field");
2050}
2051
2052static __always_inline void vmcs_checkl(unsigned long field)
2053{
2054 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2055 "Natural width accessor invalid for 16-bit field");
2056 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2057 "Natural width accessor invalid for 64-bit field");
2058 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2059 "Natural width accessor invalid for 64-bit high field");
2060 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2061 "Natural width accessor invalid for 32-bit field");
2062}
2063
2064static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065{
Avi Kivity5e520e62011-05-15 10:13:12 -04002066 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067
Avi Kivity5e520e62011-05-15 10:13:12 -04002068 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2069 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070 return value;
2071}
2072
Avi Kivity96304212011-05-15 10:13:13 -04002073static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002075 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002076 if (static_branch_unlikely(&enable_evmcs))
2077 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002078 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079}
2080
Avi Kivity96304212011-05-15 10:13:13 -04002081static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002083 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002084 if (static_branch_unlikely(&enable_evmcs))
2085 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002086 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087}
2088
Avi Kivity96304212011-05-15 10:13:13 -04002089static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002091 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002092 if (static_branch_unlikely(&enable_evmcs))
2093 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002094#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002095 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002097 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098#endif
2099}
2100
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002101static __always_inline unsigned long vmcs_readl(unsigned long field)
2102{
2103 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002104 if (static_branch_unlikely(&enable_evmcs))
2105 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002106 return __vmcs_readl(field);
2107}
2108
Avi Kivitye52de1b2007-01-05 16:36:56 -08002109static noinline void vmwrite_error(unsigned long field, unsigned long value)
2110{
2111 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2112 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2113 dump_stack();
2114}
2115
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002116static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117{
2118 u8 error;
2119
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002120 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002121 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002122 if (unlikely(error))
2123 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124}
2125
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002126static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002128 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002129 if (static_branch_unlikely(&enable_evmcs))
2130 return evmcs_write16(field, value);
2131
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002132 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133}
2134
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002135static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002137 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002138 if (static_branch_unlikely(&enable_evmcs))
2139 return evmcs_write32(field, value);
2140
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002141 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142}
2143
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002144static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002146 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002147 if (static_branch_unlikely(&enable_evmcs))
2148 return evmcs_write64(field, value);
2149
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002150 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002151#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002153 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154#endif
2155}
2156
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002158{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002159 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002160 if (static_branch_unlikely(&enable_evmcs))
2161 return evmcs_write64(field, value);
2162
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002163 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002164}
2165
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002166static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002167{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002168 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2169 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002170 if (static_branch_unlikely(&enable_evmcs))
2171 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2172
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002173 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2174}
2175
2176static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2177{
2178 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2179 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002180 if (static_branch_unlikely(&enable_evmcs))
2181 return evmcs_write32(field, evmcs_read32(field) | mask);
2182
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002183 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002184}
2185
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002186static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2187{
2188 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2189}
2190
Gleb Natapov2961e8762013-11-25 15:37:13 +02002191static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2192{
2193 vmcs_write32(VM_ENTRY_CONTROLS, val);
2194 vmx->vm_entry_controls_shadow = val;
2195}
2196
2197static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2198{
2199 if (vmx->vm_entry_controls_shadow != val)
2200 vm_entry_controls_init(vmx, val);
2201}
2202
2203static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2204{
2205 return vmx->vm_entry_controls_shadow;
2206}
2207
2208
2209static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2210{
2211 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2212}
2213
2214static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2215{
2216 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2217}
2218
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002219static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2220{
2221 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2222}
2223
Gleb Natapov2961e8762013-11-25 15:37:13 +02002224static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2225{
2226 vmcs_write32(VM_EXIT_CONTROLS, val);
2227 vmx->vm_exit_controls_shadow = val;
2228}
2229
2230static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2231{
2232 if (vmx->vm_exit_controls_shadow != val)
2233 vm_exit_controls_init(vmx, val);
2234}
2235
2236static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2237{
2238 return vmx->vm_exit_controls_shadow;
2239}
2240
2241
2242static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2243{
2244 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2245}
2246
2247static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2248{
2249 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2250}
2251
Avi Kivity2fb92db2011-04-27 19:42:18 +03002252static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2253{
2254 vmx->segment_cache.bitmask = 0;
2255}
2256
2257static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2258 unsigned field)
2259{
2260 bool ret;
2261 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2262
2263 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2264 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2265 vmx->segment_cache.bitmask = 0;
2266 }
2267 ret = vmx->segment_cache.bitmask & mask;
2268 vmx->segment_cache.bitmask |= mask;
2269 return ret;
2270}
2271
2272static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2273{
2274 u16 *p = &vmx->segment_cache.seg[seg].selector;
2275
2276 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2277 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2278 return *p;
2279}
2280
2281static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2282{
2283 ulong *p = &vmx->segment_cache.seg[seg].base;
2284
2285 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2286 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2287 return *p;
2288}
2289
2290static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2291{
2292 u32 *p = &vmx->segment_cache.seg[seg].limit;
2293
2294 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2295 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2296 return *p;
2297}
2298
2299static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2300{
2301 u32 *p = &vmx->segment_cache.seg[seg].ar;
2302
2303 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2304 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2305 return *p;
2306}
2307
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002308static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2309{
2310 u32 eb;
2311
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002312 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002313 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002314 /*
2315 * Guest access to VMware backdoor ports could legitimately
2316 * trigger #GP because of TSS I/O permission bitmap.
2317 * We intercept those #GP and allow access to them anyway
2318 * as VMware does.
2319 */
2320 if (enable_vmware_backdoor)
2321 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002322 if ((vcpu->guest_debug &
2323 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2324 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2325 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002326 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002327 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002328 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002329 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002330
2331 /* When we are running a nested L2 guest and L1 specified for it a
2332 * certain exception bitmap, we must trap the same exceptions and pass
2333 * them to L1. When running L2, we will only handle the exceptions
2334 * specified above if L1 did not want them.
2335 */
2336 if (is_guest_mode(vcpu))
2337 eb |= get_vmcs12(vcpu)->exception_bitmap;
2338
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002339 vmcs_write32(EXCEPTION_BITMAP, eb);
2340}
2341
Ashok Raj15d45072018-02-01 22:59:43 +01002342/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002343 * Check if MSR is intercepted for currently loaded MSR bitmap.
2344 */
2345static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2346{
2347 unsigned long *msr_bitmap;
2348 int f = sizeof(unsigned long);
2349
2350 if (!cpu_has_vmx_msr_bitmap())
2351 return true;
2352
2353 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2354
2355 if (msr <= 0x1fff) {
2356 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2357 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2358 msr &= 0x1fff;
2359 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2360 }
2361
2362 return true;
2363}
2364
2365/*
Ashok Raj15d45072018-02-01 22:59:43 +01002366 * Check if MSR is intercepted for L01 MSR bitmap.
2367 */
2368static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2369{
2370 unsigned long *msr_bitmap;
2371 int f = sizeof(unsigned long);
2372
2373 if (!cpu_has_vmx_msr_bitmap())
2374 return true;
2375
2376 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2377
2378 if (msr <= 0x1fff) {
2379 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2380 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2381 msr &= 0x1fff;
2382 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2383 }
2384
2385 return true;
2386}
2387
Gleb Natapov2961e8762013-11-25 15:37:13 +02002388static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2389 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002390{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002391 vm_entry_controls_clearbit(vmx, entry);
2392 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002393}
2394
Avi Kivity61d2ef22010-04-28 16:40:38 +03002395static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2396{
2397 unsigned i;
2398 struct msr_autoload *m = &vmx->msr_autoload;
2399
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002400 switch (msr) {
2401 case MSR_EFER:
2402 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002403 clear_atomic_switch_msr_special(vmx,
2404 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002405 VM_EXIT_LOAD_IA32_EFER);
2406 return;
2407 }
2408 break;
2409 case MSR_CORE_PERF_GLOBAL_CTRL:
2410 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002411 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002412 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2413 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2414 return;
2415 }
2416 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002417 }
2418
Avi Kivity61d2ef22010-04-28 16:40:38 +03002419 for (i = 0; i < m->nr; ++i)
2420 if (m->guest[i].index == msr)
2421 break;
2422
2423 if (i == m->nr)
2424 return;
2425 --m->nr;
2426 m->guest[i] = m->guest[m->nr];
2427 m->host[i] = m->host[m->nr];
2428 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2429 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2430}
2431
Gleb Natapov2961e8762013-11-25 15:37:13 +02002432static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2433 unsigned long entry, unsigned long exit,
2434 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2435 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002436{
2437 vmcs_write64(guest_val_vmcs, guest_val);
2438 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002439 vm_entry_controls_setbit(vmx, entry);
2440 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002441}
2442
Avi Kivity61d2ef22010-04-28 16:40:38 +03002443static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2444 u64 guest_val, u64 host_val)
2445{
2446 unsigned i;
2447 struct msr_autoload *m = &vmx->msr_autoload;
2448
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002449 switch (msr) {
2450 case MSR_EFER:
2451 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002452 add_atomic_switch_msr_special(vmx,
2453 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002454 VM_EXIT_LOAD_IA32_EFER,
2455 GUEST_IA32_EFER,
2456 HOST_IA32_EFER,
2457 guest_val, host_val);
2458 return;
2459 }
2460 break;
2461 case MSR_CORE_PERF_GLOBAL_CTRL:
2462 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002463 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002464 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2465 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2466 GUEST_IA32_PERF_GLOBAL_CTRL,
2467 HOST_IA32_PERF_GLOBAL_CTRL,
2468 guest_val, host_val);
2469 return;
2470 }
2471 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002472 case MSR_IA32_PEBS_ENABLE:
2473 /* PEBS needs a quiescent period after being disabled (to write
2474 * a record). Disabling PEBS through VMX MSR swapping doesn't
2475 * provide that period, so a CPU could write host's record into
2476 * guest's memory.
2477 */
2478 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002479 }
2480
Avi Kivity61d2ef22010-04-28 16:40:38 +03002481 for (i = 0; i < m->nr; ++i)
2482 if (m->guest[i].index == msr)
2483 break;
2484
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002485 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002486 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002487 "Can't add msr %x\n", msr);
2488 return;
2489 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002490 ++m->nr;
2491 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2492 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2493 }
2494
2495 m->guest[i].index = msr;
2496 m->guest[i].value = guest_val;
2497 m->host[i].index = msr;
2498 m->host[i].value = host_val;
2499}
2500
Avi Kivity92c0d902009-10-29 11:00:16 +02002501static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002502{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002503 u64 guest_efer = vmx->vcpu.arch.efer;
2504 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002505
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002506 if (!enable_ept) {
2507 /*
2508 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2509 * host CPUID is more efficient than testing guest CPUID
2510 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2511 */
2512 if (boot_cpu_has(X86_FEATURE_SMEP))
2513 guest_efer |= EFER_NX;
2514 else if (!(guest_efer & EFER_NX))
2515 ignore_bits |= EFER_NX;
2516 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002517
Avi Kivity51c6cf62007-08-29 03:48:05 +03002518 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002519 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002520 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002521 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002522#ifdef CONFIG_X86_64
2523 ignore_bits |= EFER_LMA | EFER_LME;
2524 /* SCE is meaningful only in long mode on Intel */
2525 if (guest_efer & EFER_LMA)
2526 ignore_bits &= ~(u64)EFER_SCE;
2527#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002528
2529 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002530
2531 /*
2532 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2533 * On CPUs that support "load IA32_EFER", always switch EFER
2534 * atomically, since it's faster than switching it manually.
2535 */
2536 if (cpu_has_load_ia32_efer ||
2537 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002538 if (!(guest_efer & EFER_LMA))
2539 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002540 if (guest_efer != host_efer)
2541 add_atomic_switch_msr(vmx, MSR_EFER,
2542 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002543 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002544 } else {
2545 guest_efer &= ~ignore_bits;
2546 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002547
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002548 vmx->guest_msrs[efer_offset].data = guest_efer;
2549 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2550
2551 return true;
2552 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002553}
2554
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002555#ifdef CONFIG_X86_32
2556/*
2557 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2558 * VMCS rather than the segment table. KVM uses this helper to figure
2559 * out the current bases to poke them into the VMCS before entry.
2560 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002561static unsigned long segment_base(u16 selector)
2562{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002563 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002564 unsigned long v;
2565
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002566 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002567 return 0;
2568
Thomas Garnier45fc8752017-03-14 10:05:08 -07002569 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002570
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002571 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002572 u16 ldt_selector = kvm_read_ldt();
2573
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002574 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002575 return 0;
2576
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002577 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002578 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002579 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002580 return v;
2581}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002582#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002583
Avi Kivity04d2cc72007-09-10 18:10:54 +03002584static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002585{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002586 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002587#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002588 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002589 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002590#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002591 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002592
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002593 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002594 return;
2595
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002596 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002597 /*
2598 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2599 * allow segment selectors with cpl > 0 or ti == 1.
2600 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002601 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002602 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002603
2604#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002605 if (likely(is_64bit_mm(current->mm))) {
2606 save_fsgs_for_kvm();
2607 vmx->host_state.fs_sel = current->thread.fsindex;
2608 vmx->host_state.gs_sel = current->thread.gsindex;
2609 fs_base = current->thread.fsbase;
2610 kernel_gs_base = current->thread.gsbase;
2611 } else {
2612#endif
2613 savesegment(fs, vmx->host_state.fs_sel);
2614 savesegment(gs, vmx->host_state.gs_sel);
2615#ifdef CONFIG_X86_64
2616 fs_base = read_msr(MSR_FS_BASE);
2617 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2618 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002619#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002620 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002621 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002622 vmx->host_state.fs_reload_needed = 0;
2623 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002624 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002625 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002626 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002627 if (!(vmx->host_state.gs_sel & 7))
2628 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002629 else {
2630 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002631 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002632 }
2633
2634#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002635 savesegment(ds, vmx->host_state.ds_sel);
2636 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002637
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002638 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002639 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002640
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002641 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002642 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002643 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002644#else
2645 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2646 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2647#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002648 for (i = 0; i < vmx->save_nmsrs; ++i)
2649 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002650 vmx->guest_msrs[i].data,
2651 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002652}
2653
Avi Kivitya9b21b62008-06-24 11:48:49 +03002654static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002655{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002656 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002657 return;
2658
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002659 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002660 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002661#ifdef CONFIG_X86_64
2662 if (is_long_mode(&vmx->vcpu))
2663 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2664#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002665 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002666 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002667#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002668 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002669#else
2670 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002671#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002672 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002673 if (vmx->host_state.fs_reload_needed)
2674 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002675#ifdef CONFIG_X86_64
2676 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2677 loadsegment(ds, vmx->host_state.ds_sel);
2678 loadsegment(es, vmx->host_state.es_sel);
2679 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002680#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002681 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002682#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002683 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002684#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002685 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002686}
2687
Avi Kivitya9b21b62008-06-24 11:48:49 +03002688static void vmx_load_host_state(struct vcpu_vmx *vmx)
2689{
2690 preempt_disable();
2691 __vmx_load_host_state(vmx);
2692 preempt_enable();
2693}
2694
Feng Wu28b835d2015-09-18 22:29:54 +08002695static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2696{
2697 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2698 struct pi_desc old, new;
2699 unsigned int dest;
2700
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002701 /*
2702 * In case of hot-plug or hot-unplug, we may have to undo
2703 * vmx_vcpu_pi_put even if there is no assigned device. And we
2704 * always keep PI.NDST up to date for simplicity: it makes the
2705 * code easier, and CPU migration is not a fast path.
2706 */
2707 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002708 return;
2709
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002710 /*
2711 * First handle the simple case where no cmpxchg is necessary; just
2712 * allow posting non-urgent interrupts.
2713 *
2714 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2715 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2716 * expects the VCPU to be on the blocked_vcpu_list that matches
2717 * PI.NDST.
2718 */
2719 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2720 vcpu->cpu == cpu) {
2721 pi_clear_sn(pi_desc);
2722 return;
2723 }
2724
2725 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002726 do {
2727 old.control = new.control = pi_desc->control;
2728
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002729 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002730
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002731 if (x2apic_enabled())
2732 new.ndst = dest;
2733 else
2734 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002735
Feng Wu28b835d2015-09-18 22:29:54 +08002736 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002737 } while (cmpxchg64(&pi_desc->control, old.control,
2738 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002739}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002740
Peter Feinerc95ba922016-08-17 09:36:47 -07002741static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2742{
2743 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2744 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2745}
2746
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747/*
2748 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2749 * vcpu mutex is already taken.
2750 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002751static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002753 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002754 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002756 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002757 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002758 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002759 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002760
2761 /*
2762 * Read loaded_vmcs->cpu should be before fetching
2763 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2764 * See the comments in __loaded_vmcs_clear().
2765 */
2766 smp_rmb();
2767
Nadav Har'Eld462b812011-05-24 15:26:10 +03002768 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2769 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002770 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002771 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002772 }
2773
2774 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2775 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2776 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002777 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002778 }
2779
2780 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002781 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002782 unsigned long sysenter_esp;
2783
2784 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002785
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 /*
2787 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002788 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002790 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002791 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002792 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002794 /*
2795 * VM exits change the host TR limit to 0x67 after a VM
2796 * exit. This is okay, since 0x67 covers everything except
2797 * the IO bitmap and have have code to handle the IO bitmap
2798 * being lost after a VM exit.
2799 */
2800 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2801
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2803 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002804
Nadav Har'Eld462b812011-05-24 15:26:10 +03002805 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 }
Feng Wu28b835d2015-09-18 22:29:54 +08002807
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002808 /* Setup TSC multiplier */
2809 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002810 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2811 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002812
Feng Wu28b835d2015-09-18 22:29:54 +08002813 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002814 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002815 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002816}
2817
2818static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2819{
2820 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2821
2822 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002823 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2824 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002825 return;
2826
2827 /* Set SN when the vCPU is preempted */
2828 if (vcpu->preempted)
2829 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830}
2831
2832static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2833{
Feng Wu28b835d2015-09-18 22:29:54 +08002834 vmx_vcpu_pi_put(vcpu);
2835
Avi Kivitya9b21b62008-06-24 11:48:49 +03002836 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837}
2838
Wanpeng Lif244dee2017-07-20 01:11:54 -07002839static bool emulation_required(struct kvm_vcpu *vcpu)
2840{
2841 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2842}
2843
Avi Kivityedcafe32009-12-30 18:07:40 +02002844static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2845
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002846/*
2847 * Return the cr0 value that a nested guest would read. This is a combination
2848 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2849 * its hypervisor (cr0_read_shadow).
2850 */
2851static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2852{
2853 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2854 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2855}
2856static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2857{
2858 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2859 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2860}
2861
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2863{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002864 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002865
Avi Kivity6de12732011-03-07 12:51:22 +02002866 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2867 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2868 rflags = vmcs_readl(GUEST_RFLAGS);
2869 if (to_vmx(vcpu)->rmode.vm86_active) {
2870 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2871 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2872 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2873 }
2874 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002875 }
Avi Kivity6de12732011-03-07 12:51:22 +02002876 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877}
2878
2879static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2880{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002881 unsigned long old_rflags = vmx_get_rflags(vcpu);
2882
Avi Kivity6de12732011-03-07 12:51:22 +02002883 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2884 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002885 if (to_vmx(vcpu)->rmode.vm86_active) {
2886 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002887 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002888 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002890
2891 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2892 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893}
2894
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002895static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002896{
2897 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2898 int ret = 0;
2899
2900 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002901 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002902 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002903 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002904
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002905 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002906}
2907
2908static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2909{
2910 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2911 u32 interruptibility = interruptibility_old;
2912
2913 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2914
Jan Kiszka48005f62010-02-19 19:38:07 +01002915 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002916 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002917 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002918 interruptibility |= GUEST_INTR_STATE_STI;
2919
2920 if ((interruptibility != interruptibility_old))
2921 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2922}
2923
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2925{
2926 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002928 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002930 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931
Glauber Costa2809f5d2009-05-12 16:21:05 -04002932 /* skipping an emulated instruction also counts */
2933 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934}
2935
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002936static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2937 unsigned long exit_qual)
2938{
2939 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2940 unsigned int nr = vcpu->arch.exception.nr;
2941 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2942
2943 if (vcpu->arch.exception.has_error_code) {
2944 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2945 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2946 }
2947
2948 if (kvm_exception_is_soft(nr))
2949 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2950 else
2951 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2952
2953 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2954 vmx_get_nmi_mask(vcpu))
2955 intr_info |= INTR_INFO_UNBLOCK_NMI;
2956
2957 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2958}
2959
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002960/*
2961 * KVM wants to inject page-faults which it got to the guest. This function
2962 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002963 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002964static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002965{
2966 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002967 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002968
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002969 if (nr == PF_VECTOR) {
2970 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002971 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002972 return 1;
2973 }
2974 /*
2975 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2976 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2977 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2978 * can be written only when inject_pending_event runs. This should be
2979 * conditional on a new capability---if the capability is disabled,
2980 * kvm_multiple_exception would write the ancillary information to
2981 * CR2 or DR6, for backwards ABI-compatibility.
2982 */
2983 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2984 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002985 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002986 return 1;
2987 }
2988 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002989 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002990 if (nr == DB_VECTOR)
2991 *exit_qual = vcpu->arch.dr6;
2992 else
2993 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002994 return 1;
2995 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002996 }
2997
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002998 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002999}
3000
Wanpeng Licaa057a2018-03-12 04:53:03 -07003001static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3002{
3003 /*
3004 * Ensure that we clear the HLT state in the VMCS. We don't need to
3005 * explicitly skip the instruction because if the HLT state is set,
3006 * then the instruction is already executing and RIP has already been
3007 * advanced.
3008 */
3009 if (kvm_hlt_in_guest(vcpu->kvm) &&
3010 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3011 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3012}
3013
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003014static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003015{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003017 unsigned nr = vcpu->arch.exception.nr;
3018 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003019 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003020 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003021
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003022 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003023 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003024 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3025 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003026
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003027 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003028 int inc_eip = 0;
3029 if (kvm_exception_is_soft(nr))
3030 inc_eip = vcpu->arch.event_exit_inst_len;
3031 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003032 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003033 return;
3034 }
3035
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003036 WARN_ON_ONCE(vmx->emulation_required);
3037
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003038 if (kvm_exception_is_soft(nr)) {
3039 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3040 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003041 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3042 } else
3043 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3044
3045 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003046
3047 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003048}
3049
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003050static bool vmx_rdtscp_supported(void)
3051{
3052 return cpu_has_vmx_rdtscp();
3053}
3054
Mao, Junjiead756a12012-07-02 01:18:48 +00003055static bool vmx_invpcid_supported(void)
3056{
3057 return cpu_has_vmx_invpcid() && enable_ept;
3058}
3059
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060/*
Eddie Donga75beee2007-05-17 18:55:15 +03003061 * Swap MSR entry in host/guest MSR entry array.
3062 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003063static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003064{
Avi Kivity26bb0982009-09-07 11:14:12 +03003065 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003066
3067 tmp = vmx->guest_msrs[to];
3068 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3069 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003070}
3071
3072/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003073 * Set up the vmcs to automatically save and restore system
3074 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3075 * mode, as fiddling with msrs is very expensive.
3076 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003077static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003078{
Avi Kivity26bb0982009-09-07 11:14:12 +03003079 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003080
Eddie Donga75beee2007-05-17 18:55:15 +03003081 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003082#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003083 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003084 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003085 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003086 move_msr_up(vmx, index, save_nmsrs++);
3087 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003088 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003089 move_msr_up(vmx, index, save_nmsrs++);
3090 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003091 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003092 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003093 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003094 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003095 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003096 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003097 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003098 * if efer.sce is enabled.
3099 */
Brian Gerst8c065852010-07-17 09:03:26 -04003100 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003101 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003102 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003103 }
Eddie Donga75beee2007-05-17 18:55:15 +03003104#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003105 index = __find_msr_index(vmx, MSR_EFER);
3106 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003107 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003108
Avi Kivity26bb0982009-09-07 11:14:12 +03003109 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003110
Yang Zhang8d146952013-01-25 10:18:50 +08003111 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003112 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003113}
3114
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003115static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003117 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003119 if (is_guest_mode(vcpu) &&
3120 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3121 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3122
3123 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124}
3125
3126/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003127 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003129static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003131 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003132 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003133 * We're here if L1 chose not to trap WRMSR to TSC. According
3134 * to the spec, this should set L1's TSC; The offset that L1
3135 * set for L2 remains unchanged, and still needs to be added
3136 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003137 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003138 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003139 /* recalculate vmcs02.TSC_OFFSET: */
3140 vmcs12 = get_vmcs12(vcpu);
3141 vmcs_write64(TSC_OFFSET, offset +
3142 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3143 vmcs12->tsc_offset : 0));
3144 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003145 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3146 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003147 vmcs_write64(TSC_OFFSET, offset);
3148 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149}
3150
Nadav Har'El801d3422011-05-25 23:02:23 +03003151/*
3152 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3153 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3154 * all guests if the "nested" module option is off, and can also be disabled
3155 * for a single guest by disabling its VMX cpuid bit.
3156 */
3157static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3158{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003159 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003160}
3161
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003163 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3164 * returned for the various VMX controls MSRs when nested VMX is enabled.
3165 * The same values should also be used to verify that vmcs12 control fields are
3166 * valid during nested entry from L1 to L2.
3167 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3168 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3169 * bit in the high half is on if the corresponding bit in the control field
3170 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003171 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003172static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003173{
Paolo Bonzini13893092018-02-26 13:40:09 +01003174 if (!nested) {
3175 memset(msrs, 0, sizeof(*msrs));
3176 return;
3177 }
3178
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003179 /*
3180 * Note that as a general rule, the high half of the MSRs (bits in
3181 * the control fields which may be 1) should be initialized by the
3182 * intersection of the underlying hardware's MSR (i.e., features which
3183 * can be supported) and the list of features we want to expose -
3184 * because they are known to be properly supported in our code.
3185 * Also, usually, the low half of the MSRs (bits which must be 1) can
3186 * be set to 0, meaning that L1 may turn off any of these bits. The
3187 * reason is that if one of these bits is necessary, it will appear
3188 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3189 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003190 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003191 * These rules have exceptions below.
3192 */
3193
3194 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003195 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003196 msrs->pinbased_ctls_low,
3197 msrs->pinbased_ctls_high);
3198 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003199 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003200 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003201 PIN_BASED_EXT_INTR_MASK |
3202 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003203 PIN_BASED_VIRTUAL_NMIS |
3204 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003205 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003206 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003207 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003208
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003209 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003210 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003211 msrs->exit_ctls_low,
3212 msrs->exit_ctls_high);
3213 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003214 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003215
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003217#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003218 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003219#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003220 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003221 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003222 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003223 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003224 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3225
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003226 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003227 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003228
Jan Kiszka2996fca2014-06-16 13:59:43 +02003229 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003230 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003231
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003232 /* entry controls */
3233 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003234 msrs->entry_ctls_low,
3235 msrs->entry_ctls_high);
3236 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003237 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003239#ifdef CONFIG_X86_64
3240 VM_ENTRY_IA32E_MODE |
3241#endif
3242 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003243 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003244 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003245 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003246 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003247
Jan Kiszka2996fca2014-06-16 13:59:43 +02003248 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003249 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003250
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003251 /* cpu-based controls */
3252 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003253 msrs->procbased_ctls_low,
3254 msrs->procbased_ctls_high);
3255 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003256 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003257 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003258 CPU_BASED_VIRTUAL_INTR_PENDING |
3259 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003260 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3261 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3262 CPU_BASED_CR3_STORE_EXITING |
3263#ifdef CONFIG_X86_64
3264 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3265#endif
3266 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003267 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3268 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3269 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3270 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003271 /*
3272 * We can allow some features even when not supported by the
3273 * hardware. For example, L1 can specify an MSR bitmap - and we
3274 * can use it to avoid exits to L1 - even when L0 runs L2
3275 * without MSR bitmaps.
3276 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003277 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003278 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003279 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003280
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003281 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003282 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003283 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3284
Paolo Bonzini80154d72017-08-24 13:55:35 +02003285 /*
3286 * secondary cpu-based controls. Do not include those that
3287 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3288 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003289 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003290 msrs->secondary_ctls_low,
3291 msrs->secondary_ctls_high);
3292 msrs->secondary_ctls_low = 0;
3293 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003294 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003295 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003296 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003297 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003298 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003299 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003300
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003301 if (enable_ept) {
3302 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003304 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003305 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003306 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003307 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003308 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003309 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 msrs->ept_caps &= vmx_capability.ept;
3311 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003312 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3313 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003314 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003315 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003316 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003317 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003318 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003319 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003320
Bandan Das27c42a12017-08-03 15:54:42 -04003321 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003323 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003324 /*
3325 * Advertise EPTP switching unconditionally
3326 * since we emulate it
3327 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003328 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003329 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003330 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003331 }
3332
Paolo Bonzinief697a72016-03-18 16:58:38 +01003333 /*
3334 * Old versions of KVM use the single-context version without
3335 * checking for support, so declare that it is supported even
3336 * though it is treated as global context. The alternative is
3337 * not failing the single-context invvpid, and it is worse.
3338 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003339 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003341 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003342 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003343 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003344 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003345
Radim Krčmář0790ec12015-03-17 14:02:32 +01003346 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003347 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003348 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3349
Jan Kiszkac18911a2013-03-13 16:06:41 +01003350 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003351 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003352 msrs->misc_low,
3353 msrs->misc_high);
3354 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3355 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003356 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003357 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003358 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003359 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003360
3361 /*
3362 * This MSR reports some information about VMX support. We
3363 * should return information about the VMX we emulate for the
3364 * guest, and the VMCS structure we give it - not about the
3365 * VMX support of the underlying hardware.
3366 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003367 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003368 VMCS12_REVISION |
3369 VMX_BASIC_TRUE_CTLS |
3370 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3371 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3372
3373 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003374 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003375
3376 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003377 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003378 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3379 * We picked the standard core2 setting.
3380 */
3381#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3382#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003383 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3384 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003385
3386 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003387 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3388 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003389
3390 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003391 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003392}
3393
David Matlack38991522016-11-29 18:14:08 -08003394/*
3395 * if fixed0[i] == 1: val[i] must be 1
3396 * if fixed1[i] == 0: val[i] must be 0
3397 */
3398static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3399{
3400 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003401}
3402
3403static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3404{
David Matlack38991522016-11-29 18:14:08 -08003405 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003406}
3407
3408static inline u64 vmx_control_msr(u32 low, u32 high)
3409{
3410 return low | ((u64)high << 32);
3411}
3412
David Matlack62cc6b9d2016-11-29 18:14:07 -08003413static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3414{
3415 superset &= mask;
3416 subset &= mask;
3417
3418 return (superset | subset) == superset;
3419}
3420
3421static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3422{
3423 const u64 feature_and_reserved =
3424 /* feature (except bit 48; see below) */
3425 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3426 /* reserved */
3427 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003428 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003429
3430 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3431 return -EINVAL;
3432
3433 /*
3434 * KVM does not emulate a version of VMX that constrains physical
3435 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3436 */
3437 if (data & BIT_ULL(48))
3438 return -EINVAL;
3439
3440 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3441 vmx_basic_vmcs_revision_id(data))
3442 return -EINVAL;
3443
3444 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3445 return -EINVAL;
3446
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003447 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003448 return 0;
3449}
3450
3451static int
3452vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3453{
3454 u64 supported;
3455 u32 *lowp, *highp;
3456
3457 switch (msr_index) {
3458 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3460 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003461 break;
3462 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003463 lowp = &vmx->nested.msrs.procbased_ctls_low;
3464 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003465 break;
3466 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467 lowp = &vmx->nested.msrs.exit_ctls_low;
3468 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003469 break;
3470 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003471 lowp = &vmx->nested.msrs.entry_ctls_low;
3472 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003473 break;
3474 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475 lowp = &vmx->nested.msrs.secondary_ctls_low;
3476 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003477 break;
3478 default:
3479 BUG();
3480 }
3481
3482 supported = vmx_control_msr(*lowp, *highp);
3483
3484 /* Check must-be-1 bits are still 1. */
3485 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3486 return -EINVAL;
3487
3488 /* Check must-be-0 bits are still 0. */
3489 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3490 return -EINVAL;
3491
3492 *lowp = data;
3493 *highp = data >> 32;
3494 return 0;
3495}
3496
3497static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3498{
3499 const u64 feature_and_reserved_bits =
3500 /* feature */
3501 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3502 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3503 /* reserved */
3504 GENMASK_ULL(13, 9) | BIT_ULL(31);
3505 u64 vmx_misc;
3506
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003507 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3508 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003509
3510 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3511 return -EINVAL;
3512
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003513 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003514 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3515 vmx_misc_preemption_timer_rate(data) !=
3516 vmx_misc_preemption_timer_rate(vmx_misc))
3517 return -EINVAL;
3518
3519 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3520 return -EINVAL;
3521
3522 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3523 return -EINVAL;
3524
3525 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3526 return -EINVAL;
3527
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003528 vmx->nested.msrs.misc_low = data;
3529 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003530
3531 /*
3532 * If L1 has read-only VM-exit information fields, use the
3533 * less permissive vmx_vmwrite_bitmap to specify write
3534 * permissions for the shadow VMCS.
3535 */
3536 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3537 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3538
David Matlack62cc6b9d2016-11-29 18:14:07 -08003539 return 0;
3540}
3541
3542static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3543{
3544 u64 vmx_ept_vpid_cap;
3545
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3547 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003548
3549 /* Every bit is either reserved or a feature bit. */
3550 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3551 return -EINVAL;
3552
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003553 vmx->nested.msrs.ept_caps = data;
3554 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003555 return 0;
3556}
3557
3558static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3559{
3560 u64 *msr;
3561
3562 switch (msr_index) {
3563 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003565 break;
3566 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003567 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003568 break;
3569 default:
3570 BUG();
3571 }
3572
3573 /*
3574 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3575 * must be 1 in the restored value.
3576 */
3577 if (!is_bitwise_subset(data, *msr, -1ULL))
3578 return -EINVAL;
3579
3580 *msr = data;
3581 return 0;
3582}
3583
3584/*
3585 * Called when userspace is restoring VMX MSRs.
3586 *
3587 * Returns 0 on success, non-0 otherwise.
3588 */
3589static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3590{
3591 struct vcpu_vmx *vmx = to_vmx(vcpu);
3592
Jim Mattsona943ac52018-05-29 09:11:32 -07003593 /*
3594 * Don't allow changes to the VMX capability MSRs while the vCPU
3595 * is in VMX operation.
3596 */
3597 if (vmx->nested.vmxon)
3598 return -EBUSY;
3599
David Matlack62cc6b9d2016-11-29 18:14:07 -08003600 switch (msr_index) {
3601 case MSR_IA32_VMX_BASIC:
3602 return vmx_restore_vmx_basic(vmx, data);
3603 case MSR_IA32_VMX_PINBASED_CTLS:
3604 case MSR_IA32_VMX_PROCBASED_CTLS:
3605 case MSR_IA32_VMX_EXIT_CTLS:
3606 case MSR_IA32_VMX_ENTRY_CTLS:
3607 /*
3608 * The "non-true" VMX capability MSRs are generated from the
3609 * "true" MSRs, so we do not support restoring them directly.
3610 *
3611 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3612 * should restore the "true" MSRs with the must-be-1 bits
3613 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3614 * DEFAULT SETTINGS".
3615 */
3616 return -EINVAL;
3617 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3618 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3619 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3620 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3621 case MSR_IA32_VMX_PROCBASED_CTLS2:
3622 return vmx_restore_control_msr(vmx, msr_index, data);
3623 case MSR_IA32_VMX_MISC:
3624 return vmx_restore_vmx_misc(vmx, data);
3625 case MSR_IA32_VMX_CR0_FIXED0:
3626 case MSR_IA32_VMX_CR4_FIXED0:
3627 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3628 case MSR_IA32_VMX_CR0_FIXED1:
3629 case MSR_IA32_VMX_CR4_FIXED1:
3630 /*
3631 * These MSRs are generated based on the vCPU's CPUID, so we
3632 * do not support restoring them directly.
3633 */
3634 return -EINVAL;
3635 case MSR_IA32_VMX_EPT_VPID_CAP:
3636 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3637 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003639 return 0;
3640 default:
3641 /*
3642 * The rest of the VMX capability MSRs do not support restore.
3643 */
3644 return -EINVAL;
3645 }
3646}
3647
Jan Kiszkacae50132014-01-04 18:47:22 +01003648/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003650{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003651 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003652 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003653 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003654 break;
3655 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3656 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003657 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->pinbased_ctls_low,
3659 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003660 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3661 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003662 break;
3663 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3664 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003665 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 msrs->procbased_ctls_low,
3667 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003668 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3669 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003670 break;
3671 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3672 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003673 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003674 msrs->exit_ctls_low,
3675 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003676 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3677 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003678 break;
3679 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3680 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003681 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 msrs->entry_ctls_low,
3683 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003684 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3685 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003686 break;
3687 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003688 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003689 msrs->misc_low,
3690 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003691 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003692 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003694 break;
3695 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003696 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003697 break;
3698 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003699 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700 break;
3701 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703 break;
3704 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003705 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003706 break;
3707 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003708 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003709 msrs->secondary_ctls_low,
3710 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003711 break;
3712 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003713 *pdata = msrs->ept_caps |
3714 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003715 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003716 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003717 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003718 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003719 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003720 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003721 }
3722
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003723 return 0;
3724}
3725
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003726static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3727 uint64_t val)
3728{
3729 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3730
3731 return !(val & ~valid_bits);
3732}
3733
Tom Lendacky801e4592018-02-21 13:39:51 -06003734static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3735{
Paolo Bonzini13893092018-02-26 13:40:09 +01003736 switch (msr->index) {
3737 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3738 if (!nested)
3739 return 1;
3740 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3741 default:
3742 return 1;
3743 }
3744
3745 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003746}
3747
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003748/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749 * Reads an msr value (of 'msr_index') into 'pdata'.
3750 * Returns 0 on success, non-0 otherwise.
3751 * Assumes vcpu_load() was already called.
3752 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003753static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003755 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003756 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003758 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003759#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003761 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003762 break;
3763 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003764 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003765 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003766 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003767 vmx_load_host_state(vmx);
3768 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003769 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003770#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003772 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003773 case MSR_IA32_SPEC_CTRL:
3774 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003775 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3776 return 1;
3777
3778 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3779 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003780 case MSR_IA32_ARCH_CAPABILITIES:
3781 if (!msr_info->host_initiated &&
3782 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3783 return 1;
3784 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3785 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003787 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 break;
3789 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003790 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791 break;
3792 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003793 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003795 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003796 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003797 (!msr_info->host_initiated &&
3798 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003799 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003800 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003801 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003802 case MSR_IA32_MCG_EXT_CTL:
3803 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003804 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003805 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003806 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003807 msr_info->data = vcpu->arch.mcg_ext_ctl;
3808 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003809 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003810 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003811 break;
3812 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3813 if (!nested_vmx_allowed(vcpu))
3814 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003815 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3816 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003817 case MSR_IA32_XSS:
3818 if (!vmx_xsaves_supported())
3819 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003820 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003821 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003822 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003823 if (!msr_info->host_initiated &&
3824 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003825 return 1;
3826 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003828 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003829 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003830 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003831 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003832 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003833 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834 }
3835
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836 return 0;
3837}
3838
Jan Kiszkacae50132014-01-04 18:47:22 +01003839static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3840
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841/*
3842 * Writes msr value into into the appropriate "register".
3843 * Returns 0 on success, non-0 otherwise.
3844 * Assumes vcpu_load() was already called.
3845 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003846static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003848 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003849 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003850 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003851 u32 msr_index = msr_info->index;
3852 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003853
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003855 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003856 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003857 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003858#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003860 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 vmcs_writel(GUEST_FS_BASE, data);
3862 break;
3863 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003864 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865 vmcs_writel(GUEST_GS_BASE, data);
3866 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003867 case MSR_KERNEL_GS_BASE:
3868 vmx_load_host_state(vmx);
3869 vmx->msr_guest_kernel_gs_base = data;
3870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871#endif
3872 case MSR_IA32_SYSENTER_CS:
3873 vmcs_write32(GUEST_SYSENTER_CS, data);
3874 break;
3875 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003876 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 break;
3878 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003879 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003881 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003882 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003883 (!msr_info->host_initiated &&
3884 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003885 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003886 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003887 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003889 vmcs_write64(GUEST_BNDCFGS, data);
3890 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003891 case MSR_IA32_SPEC_CTRL:
3892 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003893 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3894 return 1;
3895
3896 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003897 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003898 return 1;
3899
3900 vmx->spec_ctrl = data;
3901
3902 if (!data)
3903 break;
3904
3905 /*
3906 * For non-nested:
3907 * When it's written (to non-zero) for the first time, pass
3908 * it through.
3909 *
3910 * For nested:
3911 * The handling of the MSR bitmap for L2 guests is done in
3912 * nested_vmx_merge_msr_bitmap. We should not touch the
3913 * vmcs02.msr_bitmap here since it gets completely overwritten
3914 * in the merging. We update the vmcs01 here for L1 as well
3915 * since it will end up touching the MSR anyway now.
3916 */
3917 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3918 MSR_IA32_SPEC_CTRL,
3919 MSR_TYPE_RW);
3920 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003921 case MSR_IA32_PRED_CMD:
3922 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003923 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3924 return 1;
3925
3926 if (data & ~PRED_CMD_IBPB)
3927 return 1;
3928
3929 if (!data)
3930 break;
3931
3932 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3933
3934 /*
3935 * For non-nested:
3936 * When it's written (to non-zero) for the first time, pass
3937 * it through.
3938 *
3939 * For nested:
3940 * The handling of the MSR bitmap for L2 guests is done in
3941 * nested_vmx_merge_msr_bitmap. We should not touch the
3942 * vmcs02.msr_bitmap here since it gets completely overwritten
3943 * in the merging.
3944 */
3945 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3946 MSR_TYPE_W);
3947 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003948 case MSR_IA32_ARCH_CAPABILITIES:
3949 if (!msr_info->host_initiated)
3950 return 1;
3951 vmx->arch_capabilities = data;
3952 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003953 case MSR_IA32_CR_PAT:
3954 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003955 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3956 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003957 vmcs_write64(GUEST_IA32_PAT, data);
3958 vcpu->arch.pat = data;
3959 break;
3960 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003961 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003962 break;
Will Auldba904632012-11-29 12:42:50 -08003963 case MSR_IA32_TSC_ADJUST:
3964 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003965 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003966 case MSR_IA32_MCG_EXT_CTL:
3967 if ((!msr_info->host_initiated &&
3968 !(to_vmx(vcpu)->msr_ia32_feature_control &
3969 FEATURE_CONTROL_LMCE)) ||
3970 (data & ~MCG_EXT_CTL_LMCE_EN))
3971 return 1;
3972 vcpu->arch.mcg_ext_ctl = data;
3973 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003974 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003975 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003976 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003977 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3978 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003979 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003980 if (msr_info->host_initiated && data == 0)
3981 vmx_leave_nested(vcpu);
3982 break;
3983 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003984 if (!msr_info->host_initiated)
3985 return 1; /* they are read-only */
3986 if (!nested_vmx_allowed(vcpu))
3987 return 1;
3988 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003989 case MSR_IA32_XSS:
3990 if (!vmx_xsaves_supported())
3991 return 1;
3992 /*
3993 * The only supported bit as of Skylake is bit 8, but
3994 * it is not supported on KVM.
3995 */
3996 if (data != 0)
3997 return 1;
3998 vcpu->arch.ia32_xss = data;
3999 if (vcpu->arch.ia32_xss != host_xss)
4000 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4001 vcpu->arch.ia32_xss, host_xss);
4002 else
4003 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4004 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004005 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004006 if (!msr_info->host_initiated &&
4007 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004008 return 1;
4009 /* Check reserved bit, higher 32 bits should be zero */
4010 if ((data >> 32) != 0)
4011 return 1;
4012 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004014 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004015 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004016 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004017 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004018 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4019 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004020 ret = kvm_set_shared_msr(msr->index, msr->data,
4021 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004022 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004023 if (ret)
4024 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004025 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004026 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004027 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004028 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004029 }
4030
Eddie Dong2cc51562007-05-21 07:28:09 +03004031 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004032}
4033
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004034static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004036 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4037 switch (reg) {
4038 case VCPU_REGS_RSP:
4039 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4040 break;
4041 case VCPU_REGS_RIP:
4042 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4043 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004044 case VCPU_EXREG_PDPTR:
4045 if (enable_ept)
4046 ept_save_pdptrs(vcpu);
4047 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004048 default:
4049 break;
4050 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004051}
4052
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053static __init int cpu_has_kvm_support(void)
4054{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004055 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056}
4057
4058static __init int vmx_disabled_by_bios(void)
4059{
4060 u64 msr;
4061
4062 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004063 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004064 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004065 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4066 && tboot_enabled())
4067 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004068 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004069 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004070 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004071 && !tboot_enabled()) {
4072 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004073 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004074 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004075 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004076 /* launched w/o TXT and VMX disabled */
4077 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4078 && !tboot_enabled())
4079 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004080 }
4081
4082 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083}
4084
Dongxiao Xu7725b892010-05-11 18:29:38 +08004085static void kvm_cpu_vmxon(u64 addr)
4086{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004087 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004088 intel_pt_handle_vmx(1);
4089
Dongxiao Xu7725b892010-05-11 18:29:38 +08004090 asm volatile (ASM_VMX_VMXON_RAX
4091 : : "a"(&addr), "m"(addr)
4092 : "memory", "cc");
4093}
4094
Radim Krčmář13a34e02014-08-28 15:13:03 +02004095static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096{
4097 int cpu = raw_smp_processor_id();
4098 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004099 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004101 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004102 return -EBUSY;
4103
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004104 /*
4105 * This can happen if we hot-added a CPU but failed to allocate
4106 * VP assist page for it.
4107 */
4108 if (static_branch_unlikely(&enable_evmcs) &&
4109 !hv_get_vp_assist_page(cpu))
4110 return -EFAULT;
4111
Nadav Har'Eld462b812011-05-24 15:26:10 +03004112 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004113 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4114 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004115
4116 /*
4117 * Now we can enable the vmclear operation in kdump
4118 * since the loaded_vmcss_on_cpu list on this cpu
4119 * has been initialized.
4120 *
4121 * Though the cpu is not in VMX operation now, there
4122 * is no problem to enable the vmclear operation
4123 * for the loaded_vmcss_on_cpu list is empty!
4124 */
4125 crash_enable_local_vmclear(cpu);
4126
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004128
4129 test_bits = FEATURE_CONTROL_LOCKED;
4130 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4131 if (tboot_enabled())
4132 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4133
4134 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004136 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4137 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004138 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004139 if (enable_ept)
4140 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004141
4142 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143}
4144
Nadav Har'Eld462b812011-05-24 15:26:10 +03004145static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004146{
4147 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004148 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004149
Nadav Har'Eld462b812011-05-24 15:26:10 +03004150 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4151 loaded_vmcss_on_cpu_link)
4152 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004153}
4154
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004155
4156/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4157 * tricks.
4158 */
4159static void kvm_cpu_vmxoff(void)
4160{
4161 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004162
4163 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004164 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004165}
4166
Radim Krčmář13a34e02014-08-28 15:13:03 +02004167static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004169 vmclear_local_loaded_vmcss();
4170 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171}
4172
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004173static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004174 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175{
4176 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004177 u32 ctl = ctl_min | ctl_opt;
4178
4179 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4180
4181 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4182 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4183
4184 /* Ensure minimum (required) set of control bits are supported. */
4185 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004186 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004187
4188 *result = ctl;
4189 return 0;
4190}
4191
Avi Kivity110312c2010-12-21 12:54:20 +02004192static __init bool allow_1_setting(u32 msr, u32 ctl)
4193{
4194 u32 vmx_msr_low, vmx_msr_high;
4195
4196 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4197 return vmx_msr_high & ctl;
4198}
4199
Yang, Sheng002c7f72007-07-31 14:23:01 +03004200static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004201{
4202 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004203 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004204 u32 _pin_based_exec_control = 0;
4205 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004206 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004207 u32 _vmexit_control = 0;
4208 u32 _vmentry_control = 0;
4209
Paolo Bonzini13893092018-02-26 13:40:09 +01004210 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304211 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004212#ifdef CONFIG_X86_64
4213 CPU_BASED_CR8_LOAD_EXITING |
4214 CPU_BASED_CR8_STORE_EXITING |
4215#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004216 CPU_BASED_CR3_LOAD_EXITING |
4217 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004218 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004219 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004220 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004221 CPU_BASED_MWAIT_EXITING |
4222 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004223 CPU_BASED_INVLPG_EXITING |
4224 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004225
Sheng Yangf78e0e22007-10-29 09:40:42 +08004226 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004227 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004228 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004229 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4230 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004231 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004232#ifdef CONFIG_X86_64
4233 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4234 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4235 ~CPU_BASED_CR8_STORE_EXITING;
4236#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004237 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004238 min2 = 0;
4239 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004240 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004241 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004242 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004243 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004244 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004245 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004246 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004247 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004248 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004249 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004250 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004251 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004252 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004253 SECONDARY_EXEC_RDSEED_EXITING |
4254 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004255 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004256 SECONDARY_EXEC_TSC_SCALING |
4257 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004258 if (adjust_vmx_controls(min2, opt2,
4259 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004260 &_cpu_based_2nd_exec_control) < 0)
4261 return -EIO;
4262 }
4263#ifndef CONFIG_X86_64
4264 if (!(_cpu_based_2nd_exec_control &
4265 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4266 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4267#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004268
4269 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4270 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004271 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004272 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4273 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004274
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004275 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4276 &vmx_capability.ept, &vmx_capability.vpid);
4277
Sheng Yangd56f5462008-04-25 10:13:16 +08004278 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004279 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4280 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004281 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4282 CPU_BASED_CR3_STORE_EXITING |
4283 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004284 } else if (vmx_capability.ept) {
4285 vmx_capability.ept = 0;
4286 pr_warn_once("EPT CAP should not exist if not support "
4287 "1-setting enable EPT VM-execution control\n");
4288 }
4289 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4290 vmx_capability.vpid) {
4291 vmx_capability.vpid = 0;
4292 pr_warn_once("VPID CAP should not exist if not support "
4293 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004294 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004295
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004296 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004297#ifdef CONFIG_X86_64
4298 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4299#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004300 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004301 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004302 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4303 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004304 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004305
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004306 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4307 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4308 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004309 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4310 &_pin_based_exec_control) < 0)
4311 return -EIO;
4312
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004313 if (cpu_has_broken_vmx_preemption_timer())
4314 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004315 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004316 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004317 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4318
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004319 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004320 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004321 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4322 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004323 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004325 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004326
4327 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4328 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004329 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004330
4331#ifdef CONFIG_X86_64
4332 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4333 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004335#endif
4336
4337 /* Require Write-Back (WB) memory type for VMCS accesses. */
4338 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004339 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004340
Yang, Sheng002c7f72007-07-31 14:23:01 +03004341 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004342 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004343 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004344
Liran Alon2307af12018-06-29 22:59:04 +03004345 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004346
Yang, Sheng002c7f72007-07-31 14:23:01 +03004347 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4348 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004349 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004350 vmcs_conf->vmexit_ctrl = _vmexit_control;
4351 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004352
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004353 if (static_branch_unlikely(&enable_evmcs))
4354 evmcs_sanitize_exec_ctrls(vmcs_conf);
4355
Avi Kivity110312c2010-12-21 12:54:20 +02004356 cpu_has_load_ia32_efer =
4357 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4358 VM_ENTRY_LOAD_IA32_EFER)
4359 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4360 VM_EXIT_LOAD_IA32_EFER);
4361
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004362 cpu_has_load_perf_global_ctrl =
4363 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4364 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4365 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4366 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4367
4368 /*
4369 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004370 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004371 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4372 *
4373 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4374 *
4375 * AAK155 (model 26)
4376 * AAP115 (model 30)
4377 * AAT100 (model 37)
4378 * BC86,AAY89,BD102 (model 44)
4379 * BA97 (model 46)
4380 *
4381 */
4382 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4383 switch (boot_cpu_data.x86_model) {
4384 case 26:
4385 case 30:
4386 case 37:
4387 case 44:
4388 case 46:
4389 cpu_has_load_perf_global_ctrl = false;
4390 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4391 "does not work properly. Using workaround\n");
4392 break;
4393 default:
4394 break;
4395 }
4396 }
4397
Borislav Petkov782511b2016-04-04 22:25:03 +02004398 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004399 rdmsrl(MSR_IA32_XSS, host_xss);
4400
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004401 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004402}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403
4404static struct vmcs *alloc_vmcs_cpu(int cpu)
4405{
4406 int node = cpu_to_node(cpu);
4407 struct page *pages;
4408 struct vmcs *vmcs;
4409
Vlastimil Babka96db8002015-09-08 15:03:50 -07004410 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004411 if (!pages)
4412 return NULL;
4413 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004414 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004415
4416 /* KVM supports Enlightened VMCS v1 only */
4417 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004418 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004419 else
Liran Alon392b2f22018-06-23 02:35:01 +03004420 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004421
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422 return vmcs;
4423}
4424
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425static void free_vmcs(struct vmcs *vmcs)
4426{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004427 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004428}
4429
Nadav Har'Eld462b812011-05-24 15:26:10 +03004430/*
4431 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4432 */
4433static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4434{
4435 if (!loaded_vmcs->vmcs)
4436 return;
4437 loaded_vmcs_clear(loaded_vmcs);
4438 free_vmcs(loaded_vmcs->vmcs);
4439 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004440 if (loaded_vmcs->msr_bitmap)
4441 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004442 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004443}
4444
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004445static struct vmcs *alloc_vmcs(void)
4446{
4447 return alloc_vmcs_cpu(raw_smp_processor_id());
4448}
4449
4450static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4451{
4452 loaded_vmcs->vmcs = alloc_vmcs();
4453 if (!loaded_vmcs->vmcs)
4454 return -ENOMEM;
4455
4456 loaded_vmcs->shadow_vmcs = NULL;
4457 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004458
4459 if (cpu_has_vmx_msr_bitmap()) {
4460 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4461 if (!loaded_vmcs->msr_bitmap)
4462 goto out_vmcs;
4463 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004464
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004465 if (IS_ENABLED(CONFIG_HYPERV) &&
4466 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004467 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4468 struct hv_enlightened_vmcs *evmcs =
4469 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4470
4471 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4472 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004473 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004474 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004475
4476out_vmcs:
4477 free_loaded_vmcs(loaded_vmcs);
4478 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004479}
4480
Sam Ravnborg39959582007-06-01 00:47:13 -07004481static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482{
4483 int cpu;
4484
Zachary Amsden3230bb42009-09-29 11:38:37 -10004485 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004486 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004487 per_cpu(vmxarea, cpu) = NULL;
4488 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489}
4490
Jim Mattsond37f4262017-12-22 12:12:16 -08004491enum vmcs_field_width {
4492 VMCS_FIELD_WIDTH_U16 = 0,
4493 VMCS_FIELD_WIDTH_U64 = 1,
4494 VMCS_FIELD_WIDTH_U32 = 2,
4495 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004496};
4497
Jim Mattsond37f4262017-12-22 12:12:16 -08004498static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004499{
4500 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004501 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004502 return (field >> 13) & 0x3 ;
4503}
4504
4505static inline int vmcs_field_readonly(unsigned long field)
4506{
4507 return (((field >> 10) & 0x3) == 1);
4508}
4509
Bandan Dasfe2b2012014-04-21 15:20:14 -04004510static void init_vmcs_shadow_fields(void)
4511{
4512 int i, j;
4513
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004514 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4515 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004516 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004517 (i + 1 == max_shadow_read_only_fields ||
4518 shadow_read_only_fields[i + 1] != field + 1))
4519 pr_err("Missing field from shadow_read_only_field %x\n",
4520 field + 1);
4521
4522 clear_bit(field, vmx_vmread_bitmap);
4523#ifdef CONFIG_X86_64
4524 if (field & 1)
4525 continue;
4526#endif
4527 if (j < i)
4528 shadow_read_only_fields[j] = field;
4529 j++;
4530 }
4531 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004532
4533 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004534 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004535 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004536 (i + 1 == max_shadow_read_write_fields ||
4537 shadow_read_write_fields[i + 1] != field + 1))
4538 pr_err("Missing field from shadow_read_write_field %x\n",
4539 field + 1);
4540
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004541 /*
4542 * PML and the preemption timer can be emulated, but the
4543 * processor cannot vmwrite to fields that don't exist
4544 * on bare metal.
4545 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004546 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004547 case GUEST_PML_INDEX:
4548 if (!cpu_has_vmx_pml())
4549 continue;
4550 break;
4551 case VMX_PREEMPTION_TIMER_VALUE:
4552 if (!cpu_has_vmx_preemption_timer())
4553 continue;
4554 break;
4555 case GUEST_INTR_STATUS:
4556 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004557 continue;
4558 break;
4559 default:
4560 break;
4561 }
4562
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004563 clear_bit(field, vmx_vmwrite_bitmap);
4564 clear_bit(field, vmx_vmread_bitmap);
4565#ifdef CONFIG_X86_64
4566 if (field & 1)
4567 continue;
4568#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004569 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004570 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004571 j++;
4572 }
4573 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004574}
4575
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576static __init int alloc_kvm_area(void)
4577{
4578 int cpu;
4579
Zachary Amsden3230bb42009-09-29 11:38:37 -10004580 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581 struct vmcs *vmcs;
4582
4583 vmcs = alloc_vmcs_cpu(cpu);
4584 if (!vmcs) {
4585 free_kvm_area();
4586 return -ENOMEM;
4587 }
4588
Liran Alon2307af12018-06-29 22:59:04 +03004589 /*
4590 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4591 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4592 * revision_id reported by MSR_IA32_VMX_BASIC.
4593 *
4594 * However, even though not explictly documented by
4595 * TLFS, VMXArea passed as VMXON argument should
4596 * still be marked with revision_id reported by
4597 * physical CPU.
4598 */
4599 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004600 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004601
Avi Kivity6aa8b732006-12-10 02:21:36 -08004602 per_cpu(vmxarea, cpu) = vmcs;
4603 }
4604 return 0;
4605}
4606
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004607static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004608 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004610 if (!emulate_invalid_guest_state) {
4611 /*
4612 * CS and SS RPL should be equal during guest entry according
4613 * to VMX spec, but in reality it is not always so. Since vcpu
4614 * is in the middle of the transition from real mode to
4615 * protected mode it is safe to assume that RPL 0 is a good
4616 * default value.
4617 */
4618 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004619 save->selector &= ~SEGMENT_RPL_MASK;
4620 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004621 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004622 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004623 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004624}
4625
4626static void enter_pmode(struct kvm_vcpu *vcpu)
4627{
4628 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004629 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630
Gleb Natapovd99e4152012-12-20 16:57:45 +02004631 /*
4632 * Update real mode segment cache. It may be not up-to-date if sement
4633 * register was written while vcpu was in a guest mode.
4634 */
4635 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4636 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4637 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4638 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4639 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4640 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4641
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004642 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004643
Avi Kivity2fb92db2011-04-27 19:42:18 +03004644 vmx_segment_cache_clear(vmx);
4645
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004646 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004647
4648 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004649 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4650 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004651 vmcs_writel(GUEST_RFLAGS, flags);
4652
Rusty Russell66aee912007-07-17 23:34:16 +10004653 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4654 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004655
4656 update_exception_bitmap(vcpu);
4657
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004658 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4659 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4660 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4661 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4662 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4663 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004664}
4665
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004666static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004667{
Mathias Krause772e0312012-08-30 01:30:19 +02004668 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004669 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004670
Gleb Natapovd99e4152012-12-20 16:57:45 +02004671 var.dpl = 0x3;
4672 if (seg == VCPU_SREG_CS)
4673 var.type = 0x3;
4674
4675 if (!emulate_invalid_guest_state) {
4676 var.selector = var.base >> 4;
4677 var.base = var.base & 0xffff0;
4678 var.limit = 0xffff;
4679 var.g = 0;
4680 var.db = 0;
4681 var.present = 1;
4682 var.s = 1;
4683 var.l = 0;
4684 var.unusable = 0;
4685 var.type = 0x3;
4686 var.avl = 0;
4687 if (save->base & 0xf)
4688 printk_once(KERN_WARNING "kvm: segment base is not "
4689 "paragraph aligned when entering "
4690 "protected mode (seg=%d)", seg);
4691 }
4692
4693 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004694 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004695 vmcs_write32(sf->limit, var.limit);
4696 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697}
4698
4699static void enter_rmode(struct kvm_vcpu *vcpu)
4700{
4701 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004702 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004703 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004705 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4706 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4707 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4708 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4709 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004710 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4711 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004712
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004713 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714
Gleb Natapov776e58e2011-03-13 12:34:27 +02004715 /*
4716 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004717 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004718 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004719 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004720 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4721 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004722
Avi Kivity2fb92db2011-04-27 19:42:18 +03004723 vmx_segment_cache_clear(vmx);
4724
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004725 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4728
4729 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004730 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004732 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733
4734 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004735 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004736 update_exception_bitmap(vcpu);
4737
Gleb Natapovd99e4152012-12-20 16:57:45 +02004738 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4739 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4740 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4741 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4742 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4743 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004744
Eddie Dong8668a3c2007-10-10 14:26:45 +08004745 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746}
4747
Amit Shah401d10d2009-02-20 22:53:37 +05304748static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4749{
4750 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004751 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4752
4753 if (!msr)
4754 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304755
Avi Kivity44ea2b12009-09-06 15:55:37 +03004756 /*
4757 * Force kernel_gs_base reloading before EFER changes, as control
4758 * of this msr depends on is_long_mode().
4759 */
4760 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004761 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304762 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004763 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304764 msr->data = efer;
4765 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004766 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304767
4768 msr->data = efer & ~EFER_LME;
4769 }
4770 setup_msrs(vmx);
4771}
4772
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004773#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774
4775static void enter_lmode(struct kvm_vcpu *vcpu)
4776{
4777 u32 guest_tr_ar;
4778
Avi Kivity2fb92db2011-04-27 19:42:18 +03004779 vmx_segment_cache_clear(to_vmx(vcpu));
4780
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004782 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004783 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4784 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004786 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4787 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 }
Avi Kivityda38f432010-07-06 11:30:49 +03004789 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790}
4791
4792static void exit_lmode(struct kvm_vcpu *vcpu)
4793{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004794 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004795 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796}
4797
4798#endif
4799
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004800static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4801 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004802{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004803 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004804 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4805 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004806 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004807 } else {
4808 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004809 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004810}
4811
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004812static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004813{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004814 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004815}
4816
Avi Kivitye8467fd2009-12-29 18:43:06 +02004817static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4818{
4819 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4820
4821 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4822 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4823}
4824
Avi Kivityaff48ba2010-12-05 18:56:11 +02004825static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4826{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004827 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004828 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4829 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4830}
4831
Anthony Liguori25c4c272007-04-27 09:29:21 +03004832static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004833{
Avi Kivityfc78f512009-12-07 12:16:48 +02004834 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4835
4836 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4837 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004838}
4839
Sheng Yang14394422008-04-28 12:24:45 +08004840static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4841{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004842 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4843
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004844 if (!test_bit(VCPU_EXREG_PDPTR,
4845 (unsigned long *)&vcpu->arch.regs_dirty))
4846 return;
4847
Sheng Yang14394422008-04-28 12:24:45 +08004848 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004849 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4850 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4851 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4852 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004853 }
4854}
4855
Avi Kivity8f5d5492009-05-31 18:41:29 +03004856static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4857{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004858 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4859
Avi Kivity8f5d5492009-05-31 18:41:29 +03004860 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004861 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4862 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4863 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4864 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004865 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004866
4867 __set_bit(VCPU_EXREG_PDPTR,
4868 (unsigned long *)&vcpu->arch.regs_avail);
4869 __set_bit(VCPU_EXREG_PDPTR,
4870 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004871}
4872
David Matlack38991522016-11-29 18:14:08 -08004873static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4874{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004875 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4876 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004877 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4878
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004879 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004880 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4881 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4882 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4883
4884 return fixed_bits_valid(val, fixed0, fixed1);
4885}
4886
4887static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4888{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004889 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4890 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004891
4892 return fixed_bits_valid(val, fixed0, fixed1);
4893}
4894
4895static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4896{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004897 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4898 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004899
4900 return fixed_bits_valid(val, fixed0, fixed1);
4901}
4902
4903/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4904#define nested_guest_cr4_valid nested_cr4_valid
4905#define nested_host_cr4_valid nested_cr4_valid
4906
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004907static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004908
4909static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4910 unsigned long cr0,
4911 struct kvm_vcpu *vcpu)
4912{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004913 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4914 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004915 if (!(cr0 & X86_CR0_PG)) {
4916 /* From paging/starting to nonpaging */
4917 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004918 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004919 (CPU_BASED_CR3_LOAD_EXITING |
4920 CPU_BASED_CR3_STORE_EXITING));
4921 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004922 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004923 } else if (!is_paging(vcpu)) {
4924 /* From nonpaging to paging */
4925 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004926 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004927 ~(CPU_BASED_CR3_LOAD_EXITING |
4928 CPU_BASED_CR3_STORE_EXITING));
4929 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004930 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004931 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004932
4933 if (!(cr0 & X86_CR0_WP))
4934 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004935}
4936
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4938{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004939 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004940 unsigned long hw_cr0;
4941
Gleb Natapov50378782013-02-04 16:00:28 +02004942 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004943 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004944 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004945 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004946 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004947
Gleb Natapov218e7632013-01-21 15:36:45 +02004948 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4949 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004950
Gleb Natapov218e7632013-01-21 15:36:45 +02004951 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4952 enter_rmode(vcpu);
4953 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004955#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004956 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004957 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004959 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960 exit_lmode(vcpu);
4961 }
4962#endif
4963
Sean Christophersonb4d18512018-03-05 12:04:40 -08004964 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004965 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4966
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004968 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004969 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004970
4971 /* depends on vcpu->arch.cr0 to be set to a new value */
4972 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973}
4974
Yu Zhang855feb62017-08-24 20:27:55 +08004975static int get_ept_level(struct kvm_vcpu *vcpu)
4976{
4977 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4978 return 5;
4979 return 4;
4980}
4981
Peter Feiner995f00a2017-06-30 17:26:32 -07004982static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004983{
Yu Zhang855feb62017-08-24 20:27:55 +08004984 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004985
Yu Zhang855feb62017-08-24 20:27:55 +08004986 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004987
Peter Feiner995f00a2017-06-30 17:26:32 -07004988 if (enable_ept_ad_bits &&
4989 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004990 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004991 eptp |= (root_hpa & PAGE_MASK);
4992
4993 return eptp;
4994}
4995
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4997{
Sheng Yang14394422008-04-28 12:24:45 +08004998 unsigned long guest_cr3;
4999 u64 eptp;
5000
5001 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005002 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005003 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005004 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005005 if (enable_unrestricted_guest || is_paging(vcpu) ||
5006 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005007 guest_cr3 = kvm_read_cr3(vcpu);
5008 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005009 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005010 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005011 }
5012
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005013 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005014 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015}
5016
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005017static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005019 /*
5020 * Pass through host's Machine Check Enable value to hw_cr4, which
5021 * is in force while we are in guest mode. Do not let guests control
5022 * this bit, even if host CR4.MCE == 0.
5023 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005024 unsigned long hw_cr4;
5025
5026 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5027 if (enable_unrestricted_guest)
5028 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5029 else if (to_vmx(vcpu)->rmode.vm86_active)
5030 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5031 else
5032 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005033
Sean Christopherson64f7a112018-04-30 10:01:06 -07005034 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5035 if (cr4 & X86_CR4_UMIP) {
5036 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005037 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005038 hw_cr4 &= ~X86_CR4_UMIP;
5039 } else if (!is_guest_mode(vcpu) ||
5040 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5041 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5042 SECONDARY_EXEC_DESC);
5043 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005044
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005045 if (cr4 & X86_CR4_VMXE) {
5046 /*
5047 * To use VMXON (and later other VMX instructions), a guest
5048 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5049 * So basically the check on whether to allow nested VMX
5050 * is here.
5051 */
5052 if (!nested_vmx_allowed(vcpu))
5053 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005054 }
David Matlack38991522016-11-29 18:14:08 -08005055
5056 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005057 return 1;
5058
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005059 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005060
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005061 if (!enable_unrestricted_guest) {
5062 if (enable_ept) {
5063 if (!is_paging(vcpu)) {
5064 hw_cr4 &= ~X86_CR4_PAE;
5065 hw_cr4 |= X86_CR4_PSE;
5066 } else if (!(cr4 & X86_CR4_PAE)) {
5067 hw_cr4 &= ~X86_CR4_PAE;
5068 }
5069 }
5070
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005071 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005072 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5073 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5074 * to be manually disabled when guest switches to non-paging
5075 * mode.
5076 *
5077 * If !enable_unrestricted_guest, the CPU is always running
5078 * with CR0.PG=1 and CR4 needs to be modified.
5079 * If enable_unrestricted_guest, the CPU automatically
5080 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005081 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005082 if (!is_paging(vcpu))
5083 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5084 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005085
Sheng Yang14394422008-04-28 12:24:45 +08005086 vmcs_writel(CR4_READ_SHADOW, cr4);
5087 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005088 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089}
5090
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091static void vmx_get_segment(struct kvm_vcpu *vcpu,
5092 struct kvm_segment *var, int seg)
5093{
Avi Kivitya9179492011-01-03 14:28:52 +02005094 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 u32 ar;
5096
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005097 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005098 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005099 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005100 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005101 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005102 var->base = vmx_read_guest_seg_base(vmx, seg);
5103 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5104 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005105 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005106 var->base = vmx_read_guest_seg_base(vmx, seg);
5107 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5108 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5109 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005110 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005111 var->type = ar & 15;
5112 var->s = (ar >> 4) & 1;
5113 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005114 /*
5115 * Some userspaces do not preserve unusable property. Since usable
5116 * segment has to be present according to VMX spec we can use present
5117 * property to amend userspace bug by making unusable segment always
5118 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5119 * segment as unusable.
5120 */
5121 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122 var->avl = (ar >> 12) & 1;
5123 var->l = (ar >> 13) & 1;
5124 var->db = (ar >> 14) & 1;
5125 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005126}
5127
Avi Kivitya9179492011-01-03 14:28:52 +02005128static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5129{
Avi Kivitya9179492011-01-03 14:28:52 +02005130 struct kvm_segment s;
5131
5132 if (to_vmx(vcpu)->rmode.vm86_active) {
5133 vmx_get_segment(vcpu, &s, seg);
5134 return s.base;
5135 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005136 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005137}
5138
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005139static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005140{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005141 struct vcpu_vmx *vmx = to_vmx(vcpu);
5142
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005143 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005144 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005145 else {
5146 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005147 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005148 }
Avi Kivity69c73022011-03-07 15:26:44 +02005149}
5150
Avi Kivity653e3102007-05-07 10:55:37 +03005151static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005152{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005153 u32 ar;
5154
Avi Kivityf0495f92012-06-07 17:06:10 +03005155 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005156 ar = 1 << 16;
5157 else {
5158 ar = var->type & 15;
5159 ar |= (var->s & 1) << 4;
5160 ar |= (var->dpl & 3) << 5;
5161 ar |= (var->present & 1) << 7;
5162 ar |= (var->avl & 1) << 12;
5163 ar |= (var->l & 1) << 13;
5164 ar |= (var->db & 1) << 14;
5165 ar |= (var->g & 1) << 15;
5166 }
Avi Kivity653e3102007-05-07 10:55:37 +03005167
5168 return ar;
5169}
5170
5171static void vmx_set_segment(struct kvm_vcpu *vcpu,
5172 struct kvm_segment *var, int seg)
5173{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005174 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005175 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005176
Avi Kivity2fb92db2011-04-27 19:42:18 +03005177 vmx_segment_cache_clear(vmx);
5178
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005179 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5180 vmx->rmode.segs[seg] = *var;
5181 if (seg == VCPU_SREG_TR)
5182 vmcs_write16(sf->selector, var->selector);
5183 else if (var->s)
5184 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005185 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005186 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005187
Avi Kivity653e3102007-05-07 10:55:37 +03005188 vmcs_writel(sf->base, var->base);
5189 vmcs_write32(sf->limit, var->limit);
5190 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005191
5192 /*
5193 * Fix the "Accessed" bit in AR field of segment registers for older
5194 * qemu binaries.
5195 * IA32 arch specifies that at the time of processor reset the
5196 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005197 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005198 * state vmexit when "unrestricted guest" mode is turned on.
5199 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5200 * tree. Newer qemu binaries with that qemu fix would not need this
5201 * kvm hack.
5202 */
5203 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005204 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005205
Gleb Natapovf924d662012-12-12 19:10:55 +02005206 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005207
5208out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005209 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005210}
5211
Avi Kivity6aa8b732006-12-10 02:21:36 -08005212static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5213{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005214 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215
5216 *db = (ar >> 14) & 1;
5217 *l = (ar >> 13) & 1;
5218}
5219
Gleb Natapov89a27f42010-02-16 10:51:48 +02005220static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005221{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005222 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5223 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005224}
5225
Gleb Natapov89a27f42010-02-16 10:51:48 +02005226static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005227{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005228 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5229 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005230}
5231
Gleb Natapov89a27f42010-02-16 10:51:48 +02005232static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005234 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5235 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005236}
5237
Gleb Natapov89a27f42010-02-16 10:51:48 +02005238static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005240 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5241 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005242}
5243
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005244static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5245{
5246 struct kvm_segment var;
5247 u32 ar;
5248
5249 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005250 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005251 if (seg == VCPU_SREG_CS)
5252 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005253 ar = vmx_segment_access_rights(&var);
5254
5255 if (var.base != (var.selector << 4))
5256 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005257 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005258 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005259 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005260 return false;
5261
5262 return true;
5263}
5264
5265static bool code_segment_valid(struct kvm_vcpu *vcpu)
5266{
5267 struct kvm_segment cs;
5268 unsigned int cs_rpl;
5269
5270 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005271 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005272
Avi Kivity1872a3f2009-01-04 23:26:52 +02005273 if (cs.unusable)
5274 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005275 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005276 return false;
5277 if (!cs.s)
5278 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005279 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005280 if (cs.dpl > cs_rpl)
5281 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005282 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005283 if (cs.dpl != cs_rpl)
5284 return false;
5285 }
5286 if (!cs.present)
5287 return false;
5288
5289 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5290 return true;
5291}
5292
5293static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5294{
5295 struct kvm_segment ss;
5296 unsigned int ss_rpl;
5297
5298 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005299 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005300
Avi Kivity1872a3f2009-01-04 23:26:52 +02005301 if (ss.unusable)
5302 return true;
5303 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005304 return false;
5305 if (!ss.s)
5306 return false;
5307 if (ss.dpl != ss_rpl) /* DPL != RPL */
5308 return false;
5309 if (!ss.present)
5310 return false;
5311
5312 return true;
5313}
5314
5315static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5316{
5317 struct kvm_segment var;
5318 unsigned int rpl;
5319
5320 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005321 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005322
Avi Kivity1872a3f2009-01-04 23:26:52 +02005323 if (var.unusable)
5324 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005325 if (!var.s)
5326 return false;
5327 if (!var.present)
5328 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005329 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005330 if (var.dpl < rpl) /* DPL < RPL */
5331 return false;
5332 }
5333
5334 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5335 * rights flags
5336 */
5337 return true;
5338}
5339
5340static bool tr_valid(struct kvm_vcpu *vcpu)
5341{
5342 struct kvm_segment tr;
5343
5344 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5345
Avi Kivity1872a3f2009-01-04 23:26:52 +02005346 if (tr.unusable)
5347 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005348 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005349 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005350 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005351 return false;
5352 if (!tr.present)
5353 return false;
5354
5355 return true;
5356}
5357
5358static bool ldtr_valid(struct kvm_vcpu *vcpu)
5359{
5360 struct kvm_segment ldtr;
5361
5362 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5363
Avi Kivity1872a3f2009-01-04 23:26:52 +02005364 if (ldtr.unusable)
5365 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005366 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005367 return false;
5368 if (ldtr.type != 2)
5369 return false;
5370 if (!ldtr.present)
5371 return false;
5372
5373 return true;
5374}
5375
5376static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5377{
5378 struct kvm_segment cs, ss;
5379
5380 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5381 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5382
Nadav Amitb32a9912015-03-29 16:33:04 +03005383 return ((cs.selector & SEGMENT_RPL_MASK) ==
5384 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005385}
5386
5387/*
5388 * Check if guest state is valid. Returns true if valid, false if
5389 * not.
5390 * We assume that registers are always usable
5391 */
5392static bool guest_state_valid(struct kvm_vcpu *vcpu)
5393{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005394 if (enable_unrestricted_guest)
5395 return true;
5396
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005397 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005398 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005399 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5400 return false;
5401 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5402 return false;
5403 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5404 return false;
5405 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5406 return false;
5407 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5408 return false;
5409 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5410 return false;
5411 } else {
5412 /* protected mode guest state checks */
5413 if (!cs_ss_rpl_check(vcpu))
5414 return false;
5415 if (!code_segment_valid(vcpu))
5416 return false;
5417 if (!stack_segment_valid(vcpu))
5418 return false;
5419 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5420 return false;
5421 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5422 return false;
5423 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5424 return false;
5425 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5426 return false;
5427 if (!tr_valid(vcpu))
5428 return false;
5429 if (!ldtr_valid(vcpu))
5430 return false;
5431 }
5432 /* TODO:
5433 * - Add checks on RIP
5434 * - Add checks on RFLAGS
5435 */
5436
5437 return true;
5438}
5439
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005440static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5441{
5442 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5443}
5444
Mike Dayd77c26f2007-10-08 09:02:08 -04005445static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005447 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005448 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005449 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005451 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005452 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
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 = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005457 r = kvm_write_guest_page(kvm, fn++, &data,
5458 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005459 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005460 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005461 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5462 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005463 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005464 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5465 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005466 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005467 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005468 r = kvm_write_guest_page(kvm, fn, &data,
5469 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5470 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005471out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005472 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005473 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474}
5475
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005476static int init_rmode_identity_map(struct kvm *kvm)
5477{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005478 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005479 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005480 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005481 u32 tmp;
5482
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005483 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005484 mutex_lock(&kvm->slots_lock);
5485
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005486 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005487 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005488
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005489 if (!kvm_vmx->ept_identity_map_addr)
5490 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5491 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005492
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005493 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005494 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005495 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005496 goto out2;
5497
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005498 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005499 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5500 if (r < 0)
5501 goto out;
5502 /* Set up identity-mapping pagetable for EPT in real mode */
5503 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5504 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5505 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5506 r = kvm_write_guest_page(kvm, identity_map_pfn,
5507 &tmp, i * sizeof(tmp), sizeof(tmp));
5508 if (r < 0)
5509 goto out;
5510 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005511 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005512
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005513out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005514 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005515
5516out2:
5517 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005518 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005519}
5520
Avi Kivity6aa8b732006-12-10 02:21:36 -08005521static void seg_setup(int seg)
5522{
Mathias Krause772e0312012-08-30 01:30:19 +02005523 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005524 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005525
5526 vmcs_write16(sf->selector, 0);
5527 vmcs_writel(sf->base, 0);
5528 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005529 ar = 0x93;
5530 if (seg == VCPU_SREG_CS)
5531 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005532
5533 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005534}
5535
Sheng Yangf78e0e22007-10-29 09:40:42 +08005536static int alloc_apic_access_page(struct kvm *kvm)
5537{
Xiao Guangrong44841412012-09-07 14:14:20 +08005538 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005539 int r = 0;
5540
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005541 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005542 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005543 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005544 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5545 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005546 if (r)
5547 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005548
Tang Chen73a6d942014-09-11 13:38:00 +08005549 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005550 if (is_error_page(page)) {
5551 r = -EFAULT;
5552 goto out;
5553 }
5554
Tang Chenc24ae0d2014-09-24 15:57:58 +08005555 /*
5556 * Do not pin the page in memory, so that memory hot-unplug
5557 * is able to migrate it.
5558 */
5559 put_page(page);
5560 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005561out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005562 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005563 return r;
5564}
5565
Wanpeng Li991e7a02015-09-16 17:30:05 +08005566static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005567{
5568 int vpid;
5569
Avi Kivity919818a2009-03-23 18:01:29 +02005570 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005571 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005572 spin_lock(&vmx_vpid_lock);
5573 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005574 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005575 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005576 else
5577 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005578 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005579 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005580}
5581
Wanpeng Li991e7a02015-09-16 17:30:05 +08005582static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005583{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005584 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005585 return;
5586 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005587 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005588 spin_unlock(&vmx_vpid_lock);
5589}
5590
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005591static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5592 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005593{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005594 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005595
5596 if (!cpu_has_vmx_msr_bitmap())
5597 return;
5598
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005599 if (static_branch_unlikely(&enable_evmcs))
5600 evmcs_touch_msr_bitmap();
5601
Sheng Yang25c5f222008-03-28 13:18:56 +08005602 /*
5603 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5604 * have the write-low and read-high bitmap offsets the wrong way round.
5605 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5606 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005607 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005608 if (type & MSR_TYPE_R)
5609 /* read-low */
5610 __clear_bit(msr, msr_bitmap + 0x000 / f);
5611
5612 if (type & MSR_TYPE_W)
5613 /* write-low */
5614 __clear_bit(msr, msr_bitmap + 0x800 / f);
5615
Sheng Yang25c5f222008-03-28 13:18:56 +08005616 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5617 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005618 if (type & MSR_TYPE_R)
5619 /* read-high */
5620 __clear_bit(msr, msr_bitmap + 0x400 / f);
5621
5622 if (type & MSR_TYPE_W)
5623 /* write-high */
5624 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5625
5626 }
5627}
5628
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005629static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5630 u32 msr, int type)
5631{
5632 int f = sizeof(unsigned long);
5633
5634 if (!cpu_has_vmx_msr_bitmap())
5635 return;
5636
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005637 if (static_branch_unlikely(&enable_evmcs))
5638 evmcs_touch_msr_bitmap();
5639
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005640 /*
5641 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5642 * have the write-low and read-high bitmap offsets the wrong way round.
5643 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5644 */
5645 if (msr <= 0x1fff) {
5646 if (type & MSR_TYPE_R)
5647 /* read-low */
5648 __set_bit(msr, msr_bitmap + 0x000 / f);
5649
5650 if (type & MSR_TYPE_W)
5651 /* write-low */
5652 __set_bit(msr, msr_bitmap + 0x800 / f);
5653
5654 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5655 msr &= 0x1fff;
5656 if (type & MSR_TYPE_R)
5657 /* read-high */
5658 __set_bit(msr, msr_bitmap + 0x400 / f);
5659
5660 if (type & MSR_TYPE_W)
5661 /* write-high */
5662 __set_bit(msr, msr_bitmap + 0xc00 / f);
5663
5664 }
5665}
5666
5667static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5668 u32 msr, int type, bool value)
5669{
5670 if (value)
5671 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5672 else
5673 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5674}
5675
Wincy Vanf2b93282015-02-03 23:56:03 +08005676/*
5677 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5678 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5679 */
5680static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5681 unsigned long *msr_bitmap_nested,
5682 u32 msr, int type)
5683{
5684 int f = sizeof(unsigned long);
5685
Wincy Vanf2b93282015-02-03 23:56:03 +08005686 /*
5687 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5688 * have the write-low and read-high bitmap offsets the wrong way round.
5689 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5690 */
5691 if (msr <= 0x1fff) {
5692 if (type & MSR_TYPE_R &&
5693 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5694 /* read-low */
5695 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5696
5697 if (type & MSR_TYPE_W &&
5698 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5699 /* write-low */
5700 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5701
5702 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5703 msr &= 0x1fff;
5704 if (type & MSR_TYPE_R &&
5705 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5706 /* read-high */
5707 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5708
5709 if (type & MSR_TYPE_W &&
5710 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5711 /* write-high */
5712 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5713
5714 }
5715}
5716
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005717static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005718{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005719 u8 mode = 0;
5720
5721 if (cpu_has_secondary_exec_ctrls() &&
5722 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5723 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5724 mode |= MSR_BITMAP_MODE_X2APIC;
5725 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5726 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5727 }
5728
5729 if (is_long_mode(vcpu))
5730 mode |= MSR_BITMAP_MODE_LM;
5731
5732 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005733}
5734
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005735#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5736
5737static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5738 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005739{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005740 int msr;
5741
5742 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5743 unsigned word = msr / BITS_PER_LONG;
5744 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5745 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005746 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005747
5748 if (mode & MSR_BITMAP_MODE_X2APIC) {
5749 /*
5750 * TPR reads and writes can be virtualized even if virtual interrupt
5751 * delivery is not in use.
5752 */
5753 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5754 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5755 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5756 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5757 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5758 }
5759 }
5760}
5761
5762static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5763{
5764 struct vcpu_vmx *vmx = to_vmx(vcpu);
5765 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5766 u8 mode = vmx_msr_bitmap_mode(vcpu);
5767 u8 changed = mode ^ vmx->msr_bitmap_mode;
5768
5769 if (!changed)
5770 return;
5771
5772 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5773 !(mode & MSR_BITMAP_MODE_LM));
5774
5775 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5776 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5777
5778 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005779}
5780
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005781static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005782{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005783 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005784}
5785
David Matlackc9f04402017-08-01 14:00:40 -07005786static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5787{
5788 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5789 gfn_t gfn;
5790
5791 /*
5792 * Don't need to mark the APIC access page dirty; it is never
5793 * written to by the CPU during APIC virtualization.
5794 */
5795
5796 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5797 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5798 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5799 }
5800
5801 if (nested_cpu_has_posted_intr(vmcs12)) {
5802 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5803 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5804 }
5805}
5806
5807
David Hildenbrand6342c502017-01-25 11:58:58 +01005808static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005809{
5810 struct vcpu_vmx *vmx = to_vmx(vcpu);
5811 int max_irr;
5812 void *vapic_page;
5813 u16 status;
5814
David Matlackc9f04402017-08-01 14:00:40 -07005815 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5816 return;
Wincy Van705699a2015-02-03 23:58:17 +08005817
David Matlackc9f04402017-08-01 14:00:40 -07005818 vmx->nested.pi_pending = false;
5819 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5820 return;
Wincy Van705699a2015-02-03 23:58:17 +08005821
David Matlackc9f04402017-08-01 14:00:40 -07005822 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5823 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005824 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005825 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5826 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005827 kunmap(vmx->nested.virtual_apic_page);
5828
5829 status = vmcs_read16(GUEST_INTR_STATUS);
5830 if ((u8)max_irr > ((u8)status & 0xff)) {
5831 status &= ~0xff;
5832 status |= (u8)max_irr;
5833 vmcs_write16(GUEST_INTR_STATUS, status);
5834 }
5835 }
David Matlackc9f04402017-08-01 14:00:40 -07005836
5837 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005838}
5839
Wincy Van06a55242017-04-28 13:13:59 +08005840static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5841 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005842{
5843#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005844 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5845
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005846 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005847 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005848 * The vector of interrupt to be delivered to vcpu had
5849 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005850 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005851 * Following cases will be reached in this block, and
5852 * we always send a notification event in all cases as
5853 * explained below.
5854 *
5855 * Case 1: vcpu keeps in non-root mode. Sending a
5856 * notification event posts the interrupt to vcpu.
5857 *
5858 * Case 2: vcpu exits to root mode and is still
5859 * runnable. PIR will be synced to vIRR before the
5860 * next vcpu entry. Sending a notification event in
5861 * this case has no effect, as vcpu is not in root
5862 * mode.
5863 *
5864 * Case 3: vcpu exits to root mode and is blocked.
5865 * vcpu_block() has already synced PIR to vIRR and
5866 * never blocks vcpu if vIRR is not cleared. Therefore,
5867 * a blocked vcpu here does not wait for any requested
5868 * interrupts in PIR, and sending a notification event
5869 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005870 */
Feng Wu28b835d2015-09-18 22:29:54 +08005871
Wincy Van06a55242017-04-28 13:13:59 +08005872 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005873 return true;
5874 }
5875#endif
5876 return false;
5877}
5878
Wincy Van705699a2015-02-03 23:58:17 +08005879static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5880 int vector)
5881{
5882 struct vcpu_vmx *vmx = to_vmx(vcpu);
5883
5884 if (is_guest_mode(vcpu) &&
5885 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005886 /*
5887 * If a posted intr is not recognized by hardware,
5888 * we will accomplish it in the next vmentry.
5889 */
5890 vmx->nested.pi_pending = true;
5891 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005892 /* the PIR and ON have been set by L1. */
5893 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5894 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005895 return 0;
5896 }
5897 return -1;
5898}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005899/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005900 * Send interrupt to vcpu via posted interrupt way.
5901 * 1. If target vcpu is running(non-root mode), send posted interrupt
5902 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5903 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5904 * interrupt from PIR in next vmentry.
5905 */
5906static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5907{
5908 struct vcpu_vmx *vmx = to_vmx(vcpu);
5909 int r;
5910
Wincy Van705699a2015-02-03 23:58:17 +08005911 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5912 if (!r)
5913 return;
5914
Yang Zhanga20ed542013-04-11 19:25:15 +08005915 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5916 return;
5917
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005918 /* If a previous notification has sent the IPI, nothing to do. */
5919 if (pi_test_and_set_on(&vmx->pi_desc))
5920 return;
5921
Wincy Van06a55242017-04-28 13:13:59 +08005922 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005923 kvm_vcpu_kick(vcpu);
5924}
5925
Avi Kivity6aa8b732006-12-10 02:21:36 -08005926/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005927 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5928 * will not change in the lifetime of the guest.
5929 * Note that host-state that does change is set elsewhere. E.g., host-state
5930 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5931 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005932static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005933{
5934 u32 low32, high32;
5935 unsigned long tmpl;
5936 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005937 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005938
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005939 cr0 = read_cr0();
5940 WARN_ON(cr0 & X86_CR0_TS);
5941 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005942
5943 /*
5944 * Save the most likely value for this task's CR3 in the VMCS.
5945 * We can't use __get_current_cr3_fast() because we're not atomic.
5946 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005947 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005948 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005949 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005950
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005951 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005952 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005953 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005954 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005955
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005956 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005957#ifdef CONFIG_X86_64
5958 /*
5959 * Load null selectors, so we can avoid reloading them in
5960 * __vmx_load_host_state(), in case userspace uses the null selectors
5961 * too (the expected case).
5962 */
5963 vmcs_write16(HOST_DS_SELECTOR, 0);
5964 vmcs_write16(HOST_ES_SELECTOR, 0);
5965#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005966 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5967 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005968#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005969 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5970 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5971
Juergen Gross87930012017-09-04 12:25:27 +02005972 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005973 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005974 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005975
Avi Kivity83287ea422012-09-16 15:10:57 +03005976 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005977
5978 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5979 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5980 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5981 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5982
5983 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5984 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5985 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5986 }
5987}
5988
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005989static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5990{
5991 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5992 if (enable_ept)
5993 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005994 if (is_guest_mode(&vmx->vcpu))
5995 vmx->vcpu.arch.cr4_guest_owned_bits &=
5996 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005997 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5998}
5999
Yang Zhang01e439b2013-04-11 19:25:12 +08006000static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6001{
6002 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6003
Andrey Smetanind62caab2015-11-10 15:36:33 +03006004 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006005 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006006
6007 if (!enable_vnmi)
6008 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6009
Yunhong Jiang64672c92016-06-13 14:19:59 -07006010 /* Enable the preemption timer dynamically */
6011 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006012 return pin_based_exec_ctrl;
6013}
6014
Andrey Smetanind62caab2015-11-10 15:36:33 +03006015static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6016{
6017 struct vcpu_vmx *vmx = to_vmx(vcpu);
6018
6019 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006020 if (cpu_has_secondary_exec_ctrls()) {
6021 if (kvm_vcpu_apicv_active(vcpu))
6022 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6023 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6024 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6025 else
6026 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6027 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6028 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6029 }
6030
6031 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006032 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006033}
6034
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006035static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6036{
6037 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006038
6039 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6040 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6041
Paolo Bonzini35754c92015-07-29 12:05:37 +02006042 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006043 exec_control &= ~CPU_BASED_TPR_SHADOW;
6044#ifdef CONFIG_X86_64
6045 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6046 CPU_BASED_CR8_LOAD_EXITING;
6047#endif
6048 }
6049 if (!enable_ept)
6050 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6051 CPU_BASED_CR3_LOAD_EXITING |
6052 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006053 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6054 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6055 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006056 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6057 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006058 return exec_control;
6059}
6060
Jim Mattson45ec3682017-08-23 16:32:04 -07006061static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006062{
Jim Mattson45ec3682017-08-23 16:32:04 -07006063 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006064 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006065}
6066
Jim Mattson75f4fc82017-08-23 16:32:03 -07006067static bool vmx_rdseed_supported(void)
6068{
6069 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006070 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006071}
6072
Paolo Bonzini80154d72017-08-24 13:55:35 +02006073static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006074{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006075 struct kvm_vcpu *vcpu = &vmx->vcpu;
6076
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006077 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006078
Paolo Bonzini80154d72017-08-24 13:55:35 +02006079 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006080 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6081 if (vmx->vpid == 0)
6082 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6083 if (!enable_ept) {
6084 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6085 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006086 /* Enable INVPCID for non-ept guests may cause performance regression. */
6087 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006088 }
6089 if (!enable_unrestricted_guest)
6090 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006091 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006092 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006093 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006094 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6095 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006096 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006097
6098 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6099 * in vmx_set_cr4. */
6100 exec_control &= ~SECONDARY_EXEC_DESC;
6101
Abel Gordonabc4fc52013-04-18 14:35:25 +03006102 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6103 (handle_vmptrld).
6104 We can NOT enable shadow_vmcs here because we don't have yet
6105 a current VMCS12
6106 */
6107 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006108
6109 if (!enable_pml)
6110 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006111
Paolo Bonzini3db13482017-08-24 14:48:03 +02006112 if (vmx_xsaves_supported()) {
6113 /* Exposing XSAVES only when XSAVE is exposed */
6114 bool xsaves_enabled =
6115 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6116 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6117
6118 if (!xsaves_enabled)
6119 exec_control &= ~SECONDARY_EXEC_XSAVES;
6120
6121 if (nested) {
6122 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006123 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006124 SECONDARY_EXEC_XSAVES;
6125 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006126 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006127 ~SECONDARY_EXEC_XSAVES;
6128 }
6129 }
6130
Paolo Bonzini80154d72017-08-24 13:55:35 +02006131 if (vmx_rdtscp_supported()) {
6132 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6133 if (!rdtscp_enabled)
6134 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6135
6136 if (nested) {
6137 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006138 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006139 SECONDARY_EXEC_RDTSCP;
6140 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006141 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006142 ~SECONDARY_EXEC_RDTSCP;
6143 }
6144 }
6145
6146 if (vmx_invpcid_supported()) {
6147 /* Exposing INVPCID only when PCID is exposed */
6148 bool invpcid_enabled =
6149 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6150 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6151
6152 if (!invpcid_enabled) {
6153 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6154 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6155 }
6156
6157 if (nested) {
6158 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006159 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006160 SECONDARY_EXEC_ENABLE_INVPCID;
6161 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006162 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006163 ~SECONDARY_EXEC_ENABLE_INVPCID;
6164 }
6165 }
6166
Jim Mattson45ec3682017-08-23 16:32:04 -07006167 if (vmx_rdrand_supported()) {
6168 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6169 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006170 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006171
6172 if (nested) {
6173 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006174 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006175 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006176 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006177 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006178 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006179 }
6180 }
6181
Jim Mattson75f4fc82017-08-23 16:32:03 -07006182 if (vmx_rdseed_supported()) {
6183 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6184 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006185 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006186
6187 if (nested) {
6188 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006189 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006190 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006191 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006192 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006193 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006194 }
6195 }
6196
Paolo Bonzini80154d72017-08-24 13:55:35 +02006197 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006198}
6199
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006200static void ept_set_mmio_spte_mask(void)
6201{
6202 /*
6203 * EPT Misconfigurations can be generated if the value of bits 2:0
6204 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006205 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006206 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6207 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006208}
6209
Wanpeng Lif53cd632014-12-02 19:14:58 +08006210#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006211/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006212 * Sets up the vmcs for emulated real mode.
6213 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006214static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006215{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006216#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006217 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006218#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006219 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006220
Abel Gordon4607c2d2013-04-18 14:35:55 +03006221 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006222 /*
6223 * At vCPU creation, "VMWRITE to any supported field
6224 * in the VMCS" is supported, so use the more
6225 * permissive vmx_vmread_bitmap to specify both read
6226 * and write permissions for the shadow VMCS.
6227 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006228 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006229 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006230 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006231 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006232 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006233
Avi Kivity6aa8b732006-12-10 02:21:36 -08006234 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6235
Avi Kivity6aa8b732006-12-10 02:21:36 -08006236 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006237 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006238 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006239
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006240 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006241
Dan Williamsdfa169b2016-06-02 11:17:24 -07006242 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006243 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006244 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006245 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006246 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006247
Andrey Smetanind62caab2015-11-10 15:36:33 +03006248 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006249 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6250 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6251 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6252 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6253
6254 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006255
Li RongQing0bcf2612015-12-03 13:29:34 +08006256 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006257 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006258 }
6259
Wanpeng Lib31c1142018-03-12 04:53:04 -07006260 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006261 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006262 vmx->ple_window = ple_window;
6263 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006264 }
6265
Xiao Guangrongc3707952011-07-12 03:28:04 +08006266 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6267 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006268 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6269
Avi Kivity9581d442010-10-19 16:46:55 +02006270 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6271 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006272 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006273#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006274 rdmsrl(MSR_FS_BASE, a);
6275 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6276 rdmsrl(MSR_GS_BASE, a);
6277 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6278#else
6279 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6280 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6281#endif
6282
Bandan Das2a499e42017-08-03 15:54:41 -04006283 if (cpu_has_vmx_vmfunc())
6284 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6285
Eddie Dong2cc51562007-05-21 07:28:09 +03006286 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6287 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006288 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006289 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006290 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006291
Radim Krčmář74545702015-04-27 15:11:25 +02006292 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6293 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006294
Paolo Bonzini03916db2014-07-24 14:21:57 +02006295 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006296 u32 index = vmx_msr_index[i];
6297 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006298 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006299
6300 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6301 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006302 if (wrmsr_safe(index, data_low, data_high) < 0)
6303 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006304 vmx->guest_msrs[j].index = i;
6305 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006306 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006307 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006308 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006309
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006310 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6311 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006312
6313 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314
6315 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006316 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006317
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006318 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6319 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6320
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006321 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006322
Wanpeng Lif53cd632014-12-02 19:14:58 +08006323 if (vmx_xsaves_supported())
6324 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6325
Peter Feiner4e595162016-07-07 14:49:58 -07006326 if (enable_pml) {
6327 ASSERT(vmx->pml_pg);
6328 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6329 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6330 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006331}
6332
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006333static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006334{
6335 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006336 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006337 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006338
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006339 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006340 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006341
Wanpeng Li518e7b92018-02-28 14:03:31 +08006342 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006343 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006344 kvm_set_cr8(vcpu, 0);
6345
6346 if (!init_event) {
6347 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6348 MSR_IA32_APICBASE_ENABLE;
6349 if (kvm_vcpu_is_reset_bsp(vcpu))
6350 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6351 apic_base_msr.host_initiated = true;
6352 kvm_set_apic_base(vcpu, &apic_base_msr);
6353 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006354
Avi Kivity2fb92db2011-04-27 19:42:18 +03006355 vmx_segment_cache_clear(vmx);
6356
Avi Kivity5706be02008-08-20 15:07:31 +03006357 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006358 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006359 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006360
6361 seg_setup(VCPU_SREG_DS);
6362 seg_setup(VCPU_SREG_ES);
6363 seg_setup(VCPU_SREG_FS);
6364 seg_setup(VCPU_SREG_GS);
6365 seg_setup(VCPU_SREG_SS);
6366
6367 vmcs_write16(GUEST_TR_SELECTOR, 0);
6368 vmcs_writel(GUEST_TR_BASE, 0);
6369 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6370 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6371
6372 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6373 vmcs_writel(GUEST_LDTR_BASE, 0);
6374 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6375 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6376
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006377 if (!init_event) {
6378 vmcs_write32(GUEST_SYSENTER_CS, 0);
6379 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6380 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6381 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6382 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006383
Wanpeng Lic37c2872017-11-20 14:52:21 -08006384 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006385 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006386
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006387 vmcs_writel(GUEST_GDTR_BASE, 0);
6388 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6389
6390 vmcs_writel(GUEST_IDTR_BASE, 0);
6391 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6392
Anthony Liguori443381a2010-12-06 10:53:38 -06006393 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006394 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006395 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006396 if (kvm_mpx_supported())
6397 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006398
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006399 setup_msrs(vmx);
6400
Avi Kivity6aa8b732006-12-10 02:21:36 -08006401 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6402
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006403 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006404 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006405 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006406 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006407 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006408 vmcs_write32(TPR_THRESHOLD, 0);
6409 }
6410
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006411 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412
Sheng Yang2384d2b2008-01-17 15:14:33 +08006413 if (vmx->vpid != 0)
6414 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6415
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006416 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006417 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006418 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006419 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006420 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006421
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006422 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006423
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006424 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006425 if (init_event)
6426 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006427}
6428
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006429/*
6430 * In nested virtualization, check if L1 asked to exit on external interrupts.
6431 * For most existing hypervisors, this will always return true.
6432 */
6433static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6434{
6435 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6436 PIN_BASED_EXT_INTR_MASK;
6437}
6438
Bandan Das77b0f5d2014-04-19 18:17:45 -04006439/*
6440 * In nested virtualization, check if L1 has set
6441 * VM_EXIT_ACK_INTR_ON_EXIT
6442 */
6443static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6444{
6445 return get_vmcs12(vcpu)->vm_exit_controls &
6446 VM_EXIT_ACK_INTR_ON_EXIT;
6447}
6448
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006449static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6450{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006451 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006452}
6453
Jan Kiszkac9a79532014-03-07 20:03:15 +01006454static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006455{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006456 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6457 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006458}
6459
Jan Kiszkac9a79532014-03-07 20:03:15 +01006460static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006461{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006462 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006463 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006464 enable_irq_window(vcpu);
6465 return;
6466 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006467
Paolo Bonzini47c01522016-12-19 11:44:07 +01006468 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6469 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006470}
6471
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006472static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006473{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006474 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006475 uint32_t intr;
6476 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006477
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006478 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006479
Avi Kivityfa89a812008-09-01 15:57:51 +03006480 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006481 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006482 int inc_eip = 0;
6483 if (vcpu->arch.interrupt.soft)
6484 inc_eip = vcpu->arch.event_exit_inst_len;
6485 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006486 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006487 return;
6488 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006489 intr = irq | INTR_INFO_VALID_MASK;
6490 if (vcpu->arch.interrupt.soft) {
6491 intr |= INTR_TYPE_SOFT_INTR;
6492 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6493 vmx->vcpu.arch.event_exit_inst_len);
6494 } else
6495 intr |= INTR_TYPE_EXT_INTR;
6496 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006497
6498 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006499}
6500
Sheng Yangf08864b2008-05-15 18:23:25 +08006501static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6502{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006503 struct vcpu_vmx *vmx = to_vmx(vcpu);
6504
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006505 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006506 /*
6507 * Tracking the NMI-blocked state in software is built upon
6508 * finding the next open IRQ window. This, in turn, depends on
6509 * well-behaving guests: They have to keep IRQs disabled at
6510 * least as long as the NMI handler runs. Otherwise we may
6511 * cause NMI nesting, maybe breaking the guest. But as this is
6512 * highly unlikely, we can live with the residual risk.
6513 */
6514 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6515 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6516 }
6517
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006518 ++vcpu->stat.nmi_injections;
6519 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006520
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006521 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006522 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006523 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006524 return;
6525 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006526
Sheng Yangf08864b2008-05-15 18:23:25 +08006527 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6528 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006529
6530 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006531}
6532
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006533static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6534{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006535 struct vcpu_vmx *vmx = to_vmx(vcpu);
6536 bool masked;
6537
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006538 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006539 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006540 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006541 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006542 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6543 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6544 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006545}
6546
6547static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6548{
6549 struct vcpu_vmx *vmx = to_vmx(vcpu);
6550
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006551 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006552 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6553 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6554 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6555 }
6556 } else {
6557 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6558 if (masked)
6559 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6560 GUEST_INTR_STATE_NMI);
6561 else
6562 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6563 GUEST_INTR_STATE_NMI);
6564 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006565}
6566
Jan Kiszka2505dc92013-04-14 12:12:47 +02006567static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6568{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006569 if (to_vmx(vcpu)->nested.nested_run_pending)
6570 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006571
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006572 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006573 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6574 return 0;
6575
Jan Kiszka2505dc92013-04-14 12:12:47 +02006576 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6577 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6578 | GUEST_INTR_STATE_NMI));
6579}
6580
Gleb Natapov78646122009-03-23 12:12:11 +02006581static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6582{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006583 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6584 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006585 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6586 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006587}
6588
Izik Eiduscbc94022007-10-25 00:29:55 +02006589static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6590{
6591 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006592
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006593 if (enable_unrestricted_guest)
6594 return 0;
6595
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006596 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6597 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006598 if (ret)
6599 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006600 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006601 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006602}
6603
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006604static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6605{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006606 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006607 return 0;
6608}
6609
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006610static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006611{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006612 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006613 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006614 /*
6615 * Update instruction length as we may reinject the exception
6616 * from user space while in guest debugging mode.
6617 */
6618 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6619 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006620 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006621 return false;
6622 /* fall through */
6623 case DB_VECTOR:
6624 if (vcpu->guest_debug &
6625 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6626 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006627 /* fall through */
6628 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006629 case OF_VECTOR:
6630 case BR_VECTOR:
6631 case UD_VECTOR:
6632 case DF_VECTOR:
6633 case SS_VECTOR:
6634 case GP_VECTOR:
6635 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006636 return true;
6637 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006638 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006639 return false;
6640}
6641
6642static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6643 int vec, u32 err_code)
6644{
6645 /*
6646 * Instruction with address size override prefix opcode 0x67
6647 * Cause the #SS fault with 0 error code in VM86 mode.
6648 */
6649 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6650 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6651 if (vcpu->arch.halt_request) {
6652 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006653 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006654 }
6655 return 1;
6656 }
6657 return 0;
6658 }
6659
6660 /*
6661 * Forward all other exceptions that are valid in real mode.
6662 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6663 * the required debugging infrastructure rework.
6664 */
6665 kvm_queue_exception(vcpu, vec);
6666 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006667}
6668
Andi Kleena0861c02009-06-08 17:37:09 +08006669/*
6670 * Trigger machine check on the host. We assume all the MSRs are already set up
6671 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6672 * We pass a fake environment to the machine check handler because we want
6673 * the guest to be always treated like user space, no matter what context
6674 * it used internally.
6675 */
6676static void kvm_machine_check(void)
6677{
6678#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6679 struct pt_regs regs = {
6680 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6681 .flags = X86_EFLAGS_IF,
6682 };
6683
6684 do_machine_check(&regs, 0);
6685#endif
6686}
6687
Avi Kivity851ba692009-08-24 11:10:17 +03006688static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006689{
6690 /* already handled by vcpu_run */
6691 return 1;
6692}
6693
Avi Kivity851ba692009-08-24 11:10:17 +03006694static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006695{
Avi Kivity1155f762007-11-22 11:30:47 +02006696 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006697 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006698 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006699 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006700 u32 vect_info;
6701 enum emulation_result er;
6702
Avi Kivity1155f762007-11-22 11:30:47 +02006703 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006704 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006705
Andi Kleena0861c02009-06-08 17:37:09 +08006706 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006707 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006708
Jim Mattsonef85b672016-12-12 11:01:37 -08006709 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006710 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006711
Wanpeng Li082d06e2018-04-03 16:28:48 -07006712 if (is_invalid_opcode(intr_info))
6713 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006714
Avi Kivity6aa8b732006-12-10 02:21:36 -08006715 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006716 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006717 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006718
Liran Alon9e869482018-03-12 13:12:51 +02006719 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6720 WARN_ON_ONCE(!enable_vmware_backdoor);
6721 er = emulate_instruction(vcpu,
6722 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6723 if (er == EMULATE_USER_EXIT)
6724 return 0;
6725 else if (er != EMULATE_DONE)
6726 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6727 return 1;
6728 }
6729
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006730 /*
6731 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6732 * MMIO, it is better to report an internal error.
6733 * See the comments in vmx_handle_exit.
6734 */
6735 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6736 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6737 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6738 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006739 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006740 vcpu->run->internal.data[0] = vect_info;
6741 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006742 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006743 return 0;
6744 }
6745
Avi Kivity6aa8b732006-12-10 02:21:36 -08006746 if (is_page_fault(intr_info)) {
6747 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006748 /* EPT won't cause page fault directly */
6749 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006750 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006751 }
6752
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006753 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006754
6755 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6756 return handle_rmode_exception(vcpu, ex_no, error_code);
6757
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006758 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006759 case AC_VECTOR:
6760 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6761 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006762 case DB_VECTOR:
6763 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6764 if (!(vcpu->guest_debug &
6765 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006766 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006767 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006768 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006769 skip_emulated_instruction(vcpu);
6770
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006771 kvm_queue_exception(vcpu, DB_VECTOR);
6772 return 1;
6773 }
6774 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6775 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6776 /* fall through */
6777 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006778 /*
6779 * Update instruction length as we may reinject #BP from
6780 * user space while in guest debugging mode. Reading it for
6781 * #DB as well causes no harm, it is not used in that case.
6782 */
6783 vmx->vcpu.arch.event_exit_inst_len =
6784 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006785 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006786 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006787 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6788 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006789 break;
6790 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006791 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6792 kvm_run->ex.exception = ex_no;
6793 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006794 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006795 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006796 return 0;
6797}
6798
Avi Kivity851ba692009-08-24 11:10:17 +03006799static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006801 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006802 return 1;
6803}
6804
Avi Kivity851ba692009-08-24 11:10:17 +03006805static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006806{
Avi Kivity851ba692009-08-24 11:10:17 +03006807 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006808 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006809 return 0;
6810}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811
Avi Kivity851ba692009-08-24 11:10:17 +03006812static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006813{
He, Qingbfdaab02007-09-12 14:18:28 +08006814 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006815 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006816 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006817
He, Qingbfdaab02007-09-12 14:18:28 +08006818 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006819 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006820
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006821 ++vcpu->stat.io_exits;
6822
Sean Christopherson432baf62018-03-08 08:57:26 -08006823 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006824 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006825
6826 port = exit_qualification >> 16;
6827 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006828 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006829
Sean Christophersondca7f122018-03-08 08:57:27 -08006830 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006831}
6832
Ingo Molnar102d8322007-02-19 14:37:47 +02006833static void
6834vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6835{
6836 /*
6837 * Patch in the VMCALL instruction:
6838 */
6839 hypercall[0] = 0x0f;
6840 hypercall[1] = 0x01;
6841 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006842}
6843
Guo Chao0fa06072012-06-28 15:16:19 +08006844/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006845static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6846{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006847 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006848 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6849 unsigned long orig_val = val;
6850
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006851 /*
6852 * We get here when L2 changed cr0 in a way that did not change
6853 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006854 * but did change L0 shadowed bits. So we first calculate the
6855 * effective cr0 value that L1 would like to write into the
6856 * hardware. It consists of the L2-owned bits from the new
6857 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006858 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006859 val = (val & ~vmcs12->cr0_guest_host_mask) |
6860 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6861
David Matlack38991522016-11-29 18:14:08 -08006862 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006863 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006864
6865 if (kvm_set_cr0(vcpu, val))
6866 return 1;
6867 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006868 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006869 } else {
6870 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006871 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006872 return 1;
David Matlack38991522016-11-29 18:14:08 -08006873
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006874 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006875 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006876}
6877
6878static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6879{
6880 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006881 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6882 unsigned long orig_val = val;
6883
6884 /* analogously to handle_set_cr0 */
6885 val = (val & ~vmcs12->cr4_guest_host_mask) |
6886 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6887 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006888 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006889 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006890 return 0;
6891 } else
6892 return kvm_set_cr4(vcpu, val);
6893}
6894
Paolo Bonzini0367f202016-07-12 10:44:55 +02006895static int handle_desc(struct kvm_vcpu *vcpu)
6896{
6897 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6898 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6899}
6900
Avi Kivity851ba692009-08-24 11:10:17 +03006901static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006902{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006903 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 int cr;
6905 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006906 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006907 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006908
He, Qingbfdaab02007-09-12 14:18:28 +08006909 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006910 cr = exit_qualification & 15;
6911 reg = (exit_qualification >> 8) & 15;
6912 switch ((exit_qualification >> 4) & 3) {
6913 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006914 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006915 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006916 switch (cr) {
6917 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006918 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006919 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006920 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006921 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006922 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006923 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006924 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006925 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006926 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006927 case 8: {
6928 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006929 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006930 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006931 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006932 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006933 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006934 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006935 return ret;
6936 /*
6937 * TODO: we might be squashing a
6938 * KVM_GUESTDBG_SINGLESTEP-triggered
6939 * KVM_EXIT_DEBUG here.
6940 */
Avi Kivity851ba692009-08-24 11:10:17 +03006941 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006942 return 0;
6943 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006944 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006945 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006946 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006947 WARN_ONCE(1, "Guest should always own CR0.TS");
6948 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006949 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006950 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006951 case 1: /*mov from cr*/
6952 switch (cr) {
6953 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006954 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006955 val = kvm_read_cr3(vcpu);
6956 kvm_register_write(vcpu, reg, val);
6957 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006958 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006959 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006960 val = kvm_get_cr8(vcpu);
6961 kvm_register_write(vcpu, reg, val);
6962 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006963 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006964 }
6965 break;
6966 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006967 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006968 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006969 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006970
Kyle Huey6affcbe2016-11-29 12:40:40 -08006971 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006972 default:
6973 break;
6974 }
Avi Kivity851ba692009-08-24 11:10:17 +03006975 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006976 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006977 (int)(exit_qualification >> 4) & 3, cr);
6978 return 0;
6979}
6980
Avi Kivity851ba692009-08-24 11:10:17 +03006981static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006982{
He, Qingbfdaab02007-09-12 14:18:28 +08006983 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006984 int dr, dr7, reg;
6985
6986 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6987 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6988
6989 /* First, if DR does not exist, trigger UD */
6990 if (!kvm_require_dr(vcpu, dr))
6991 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006992
Jan Kiszkaf2483412010-01-20 18:20:20 +01006993 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006994 if (!kvm_require_cpl(vcpu, 0))
6995 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006996 dr7 = vmcs_readl(GUEST_DR7);
6997 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006998 /*
6999 * As the vm-exit takes precedence over the debug trap, we
7000 * need to emulate the latter, either for the host or the
7001 * guest debugging itself.
7002 */
7003 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007004 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007005 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007006 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007007 vcpu->run->debug.arch.exception = DB_VECTOR;
7008 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007009 return 0;
7010 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007011 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007012 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007013 kvm_queue_exception(vcpu, DB_VECTOR);
7014 return 1;
7015 }
7016 }
7017
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007018 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007019 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7020 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007021
7022 /*
7023 * No more DR vmexits; force a reload of the debug registers
7024 * and reenter on this instruction. The next vmexit will
7025 * retrieve the full state of the debug registers.
7026 */
7027 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7028 return 1;
7029 }
7030
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007031 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7032 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007033 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007034
7035 if (kvm_get_dr(vcpu, dr, &val))
7036 return 1;
7037 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007038 } else
Nadav Amit57773922014-06-18 17:19:23 +03007039 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007040 return 1;
7041
Kyle Huey6affcbe2016-11-29 12:40:40 -08007042 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007043}
7044
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007045static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7046{
7047 return vcpu->arch.dr6;
7048}
7049
7050static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7051{
7052}
7053
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007054static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7055{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007056 get_debugreg(vcpu->arch.db[0], 0);
7057 get_debugreg(vcpu->arch.db[1], 1);
7058 get_debugreg(vcpu->arch.db[2], 2);
7059 get_debugreg(vcpu->arch.db[3], 3);
7060 get_debugreg(vcpu->arch.dr6, 6);
7061 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7062
7063 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007064 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007065}
7066
Gleb Natapov020df072010-04-13 10:05:23 +03007067static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7068{
7069 vmcs_writel(GUEST_DR7, val);
7070}
7071
Avi Kivity851ba692009-08-24 11:10:17 +03007072static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007073{
Kyle Huey6a908b62016-11-29 12:40:37 -08007074 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007075}
7076
Avi Kivity851ba692009-08-24 11:10:17 +03007077static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007079 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007080 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007081
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007082 msr_info.index = ecx;
7083 msr_info.host_initiated = false;
7084 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007085 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007086 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007087 return 1;
7088 }
7089
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007090 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007091
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007093 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7094 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007095 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007096}
7097
Avi Kivity851ba692009-08-24 11:10:17 +03007098static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007099{
Will Auld8fe8ab42012-11-29 12:42:12 -08007100 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007101 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7102 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7103 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007104
Will Auld8fe8ab42012-11-29 12:42:12 -08007105 msr.data = data;
7106 msr.index = ecx;
7107 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007108 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007109 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007110 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007111 return 1;
7112 }
7113
Avi Kivity59200272010-01-25 19:47:02 +02007114 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007115 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116}
7117
Avi Kivity851ba692009-08-24 11:10:17 +03007118static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007119{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007120 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007121 return 1;
7122}
7123
Avi Kivity851ba692009-08-24 11:10:17 +03007124static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007125{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007126 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7127 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007128
Avi Kivity3842d132010-07-27 12:30:24 +03007129 kvm_make_request(KVM_REQ_EVENT, vcpu);
7130
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007131 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007132 return 1;
7133}
7134
Avi Kivity851ba692009-08-24 11:10:17 +03007135static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007136{
Avi Kivityd3bef152007-06-05 15:53:05 +03007137 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007138}
7139
Avi Kivity851ba692009-08-24 11:10:17 +03007140static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007141{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007142 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007143}
7144
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007145static int handle_invd(struct kvm_vcpu *vcpu)
7146{
Andre Przywara51d8b662010-12-21 11:12:02 +01007147 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007148}
7149
Avi Kivity851ba692009-08-24 11:10:17 +03007150static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007151{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007152 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007153
7154 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007155 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007156}
7157
Avi Kivityfee84b02011-11-10 14:57:25 +02007158static int handle_rdpmc(struct kvm_vcpu *vcpu)
7159{
7160 int err;
7161
7162 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007163 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007164}
7165
Avi Kivity851ba692009-08-24 11:10:17 +03007166static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007167{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007168 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007169}
7170
Dexuan Cui2acf9232010-06-10 11:27:12 +08007171static int handle_xsetbv(struct kvm_vcpu *vcpu)
7172{
7173 u64 new_bv = kvm_read_edx_eax(vcpu);
7174 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7175
7176 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007177 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007178 return 1;
7179}
7180
Wanpeng Lif53cd632014-12-02 19:14:58 +08007181static int handle_xsaves(struct kvm_vcpu *vcpu)
7182{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007183 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007184 WARN(1, "this should never happen\n");
7185 return 1;
7186}
7187
7188static int handle_xrstors(struct kvm_vcpu *vcpu)
7189{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007190 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007191 WARN(1, "this should never happen\n");
7192 return 1;
7193}
7194
Avi Kivity851ba692009-08-24 11:10:17 +03007195static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007196{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007197 if (likely(fasteoi)) {
7198 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7199 int access_type, offset;
7200
7201 access_type = exit_qualification & APIC_ACCESS_TYPE;
7202 offset = exit_qualification & APIC_ACCESS_OFFSET;
7203 /*
7204 * Sane guest uses MOV to write EOI, with written value
7205 * not cared. So make a short-circuit here by avoiding
7206 * heavy instruction emulation.
7207 */
7208 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7209 (offset == APIC_EOI)) {
7210 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007211 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007212 }
7213 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007214 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007215}
7216
Yang Zhangc7c9c562013-01-25 10:18:51 +08007217static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7218{
7219 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7220 int vector = exit_qualification & 0xff;
7221
7222 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7223 kvm_apic_set_eoi_accelerated(vcpu, vector);
7224 return 1;
7225}
7226
Yang Zhang83d4c282013-01-25 10:18:49 +08007227static int handle_apic_write(struct kvm_vcpu *vcpu)
7228{
7229 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7230 u32 offset = exit_qualification & 0xfff;
7231
7232 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7233 kvm_apic_write_nodecode(vcpu, offset);
7234 return 1;
7235}
7236
Avi Kivity851ba692009-08-24 11:10:17 +03007237static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007238{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007239 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007240 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007241 bool has_error_code = false;
7242 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007243 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007244 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007245
7246 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007247 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007248 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007249
7250 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7251
7252 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007253 if (reason == TASK_SWITCH_GATE && idt_v) {
7254 switch (type) {
7255 case INTR_TYPE_NMI_INTR:
7256 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007257 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007258 break;
7259 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007260 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007261 kvm_clear_interrupt_queue(vcpu);
7262 break;
7263 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007264 if (vmx->idt_vectoring_info &
7265 VECTORING_INFO_DELIVER_CODE_MASK) {
7266 has_error_code = true;
7267 error_code =
7268 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7269 }
7270 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007271 case INTR_TYPE_SOFT_EXCEPTION:
7272 kvm_clear_exception_queue(vcpu);
7273 break;
7274 default:
7275 break;
7276 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007277 }
Izik Eidus37817f22008-03-24 23:14:53 +02007278 tss_selector = exit_qualification;
7279
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007280 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7281 type != INTR_TYPE_EXT_INTR &&
7282 type != INTR_TYPE_NMI_INTR))
7283 skip_emulated_instruction(vcpu);
7284
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007285 if (kvm_task_switch(vcpu, tss_selector,
7286 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7287 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007288 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7289 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7290 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007291 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007292 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007293
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007294 /*
7295 * TODO: What about debug traps on tss switch?
7296 * Are we supposed to inject them and update dr6?
7297 */
7298
7299 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007300}
7301
Avi Kivity851ba692009-08-24 11:10:17 +03007302static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007303{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007304 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007305 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007306 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007307
Sheng Yangf9c617f2009-03-25 10:08:52 +08007308 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007309
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007310 /*
7311 * EPT violation happened while executing iret from NMI,
7312 * "blocked by NMI" bit has to be set before next VM entry.
7313 * There are errata that may cause this bit to not be set:
7314 * AAK134, BY25.
7315 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007316 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007317 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007318 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007319 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7320
Sheng Yang14394422008-04-28 12:24:45 +08007321 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007322 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007323
Junaid Shahid27959a42016-12-06 16:46:10 -08007324 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007325 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007326 ? PFERR_USER_MASK : 0;
7327 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007328 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007329 ? PFERR_WRITE_MASK : 0;
7330 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007331 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007332 ? PFERR_FETCH_MASK : 0;
7333 /* ept page table entry is present? */
7334 error_code |= (exit_qualification &
7335 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7336 EPT_VIOLATION_EXECUTABLE))
7337 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007338
Paolo Bonzinieebed242016-11-28 14:39:58 +01007339 error_code |= (exit_qualification & 0x100) != 0 ?
7340 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007341
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007342 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007343 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007344}
7345
Avi Kivity851ba692009-08-24 11:10:17 +03007346static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007347{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007348 gpa_t gpa;
7349
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007350 /*
7351 * A nested guest cannot optimize MMIO vmexits, because we have an
7352 * nGPA here instead of the required GPA.
7353 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007354 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007355 if (!is_guest_mode(vcpu) &&
7356 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007357 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007358 /*
7359 * Doing kvm_skip_emulated_instruction() depends on undefined
7360 * behavior: Intel's manual doesn't mandate
7361 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7362 * occurs and while on real hardware it was observed to be set,
7363 * other hypervisors (namely Hyper-V) don't set it, we end up
7364 * advancing IP with some random value. Disable fast mmio when
7365 * running nested and keep it for real hardware in hope that
7366 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7367 */
7368 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7369 return kvm_skip_emulated_instruction(vcpu);
7370 else
7371 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7372 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007373 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007374
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007375 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007376}
7377
Avi Kivity851ba692009-08-24 11:10:17 +03007378static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007379{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007380 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007381 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7382 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007383 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007384 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007385
7386 return 1;
7387}
7388
Mohammed Gamal80ced182009-09-01 12:48:18 +02007389static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007390{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007391 struct vcpu_vmx *vmx = to_vmx(vcpu);
7392 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007393 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007394 u32 cpu_exec_ctrl;
7395 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007396 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007397
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007398 /*
7399 * We should never reach the point where we are emulating L2
7400 * due to invalid guest state as that means we incorrectly
7401 * allowed a nested VMEntry with an invalid vmcs12.
7402 */
7403 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7404
Avi Kivity49e9d552010-09-19 14:34:08 +02007405 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7406 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007407
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007408 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007409 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007410 return handle_interrupt_window(&vmx->vcpu);
7411
Radim Krčmář72875d82017-04-26 22:32:19 +02007412 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007413 return 1;
7414
Liran Alon9b8ae632017-11-05 16:56:34 +02007415 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007416
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007417 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007418 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007419 ret = 0;
7420 goto out;
7421 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007422
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007423 if (err != EMULATE_DONE)
7424 goto emulation_error;
7425
7426 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7427 vcpu->arch.exception.pending)
7428 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007429
Gleb Natapov8d76c492013-05-08 18:38:44 +03007430 if (vcpu->arch.halt_request) {
7431 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007432 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007433 goto out;
7434 }
7435
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007436 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007437 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007438 if (need_resched())
7439 schedule();
7440 }
7441
Mohammed Gamal80ced182009-09-01 12:48:18 +02007442out:
7443 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007444
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007445emulation_error:
7446 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7447 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7448 vcpu->run->internal.ndata = 0;
7449 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007450}
7451
7452static void grow_ple_window(struct kvm_vcpu *vcpu)
7453{
7454 struct vcpu_vmx *vmx = to_vmx(vcpu);
7455 int old = vmx->ple_window;
7456
Babu Mogerc8e88712018-03-16 16:37:24 -04007457 vmx->ple_window = __grow_ple_window(old, ple_window,
7458 ple_window_grow,
7459 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007460
7461 if (vmx->ple_window != old)
7462 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007463
7464 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007465}
7466
7467static void shrink_ple_window(struct kvm_vcpu *vcpu)
7468{
7469 struct vcpu_vmx *vmx = to_vmx(vcpu);
7470 int old = vmx->ple_window;
7471
Babu Mogerc8e88712018-03-16 16:37:24 -04007472 vmx->ple_window = __shrink_ple_window(old, ple_window,
7473 ple_window_shrink,
7474 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007475
7476 if (vmx->ple_window != old)
7477 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007478
7479 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007480}
7481
7482/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007483 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7484 */
7485static void wakeup_handler(void)
7486{
7487 struct kvm_vcpu *vcpu;
7488 int cpu = smp_processor_id();
7489
7490 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7491 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7492 blocked_vcpu_list) {
7493 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7494
7495 if (pi_test_on(pi_desc) == 1)
7496 kvm_vcpu_kick(vcpu);
7497 }
7498 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7499}
7500
Peng Haoe01bca22018-04-07 05:47:32 +08007501static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007502{
7503 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7504 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7505 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7506 0ull, VMX_EPT_EXECUTABLE_MASK,
7507 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007508 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007509
7510 ept_set_mmio_spte_mask();
7511 kvm_enable_tdp();
7512}
7513
Tiejun Chenf2c76482014-10-28 10:14:47 +08007514static __init int hardware_setup(void)
7515{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007516 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007517 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007518
7519 rdmsrl_safe(MSR_EFER, &host_efer);
7520
7521 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7522 kvm_define_shared_msr(i, vmx_msr_index[i]);
7523
Radim Krčmář23611332016-09-29 22:41:33 +02007524 for (i = 0; i < VMX_BITMAP_NR; i++) {
7525 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7526 if (!vmx_bitmap[i])
7527 goto out;
7528 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007529
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007530 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7531 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7532
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007533 if (setup_vmcs_config(&vmcs_config) < 0) {
7534 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007535 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007536 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007537
7538 if (boot_cpu_has(X86_FEATURE_NX))
7539 kvm_enable_efer_bits(EFER_NX);
7540
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007541 if (boot_cpu_has(X86_FEATURE_MPX)) {
7542 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7543 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7544 }
7545
Wanpeng Li08d839c2017-03-23 05:30:08 -07007546 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7547 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007548 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007549
Tiejun Chenf2c76482014-10-28 10:14:47 +08007550 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007551 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007552 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007553 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007554 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007555
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007556 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007557 enable_ept_ad_bits = 0;
7558
Wanpeng Li8ad81822017-10-09 15:51:53 -07007559 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007560 enable_unrestricted_guest = 0;
7561
Paolo Bonziniad15a292015-01-30 16:18:49 +01007562 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007563 flexpriority_enabled = 0;
7564
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007565 if (!cpu_has_virtual_nmis())
7566 enable_vnmi = 0;
7567
Paolo Bonziniad15a292015-01-30 16:18:49 +01007568 /*
7569 * set_apic_access_page_addr() is used to reload apic access
7570 * page upon invalidation. No need to do anything if not
7571 * using the APIC_ACCESS_ADDR VMCS field.
7572 */
7573 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007574 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007575
7576 if (!cpu_has_vmx_tpr_shadow())
7577 kvm_x86_ops->update_cr8_intercept = NULL;
7578
7579 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7580 kvm_disable_largepages();
7581
Wanpeng Li0f107682017-09-28 18:06:24 -07007582 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007583 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007584 ple_window = 0;
7585 ple_window_grow = 0;
7586 ple_window_max = 0;
7587 ple_window_shrink = 0;
7588 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007589
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007590 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007591 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007592 kvm_x86_ops->sync_pir_to_irr = NULL;
7593 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007594
Haozhong Zhang64903d62015-10-20 15:39:09 +08007595 if (cpu_has_vmx_tsc_scaling()) {
7596 kvm_has_tsc_control = true;
7597 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7598 kvm_tsc_scaling_ratio_frac_bits = 48;
7599 }
7600
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007601 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7602
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007603 if (enable_ept)
7604 vmx_enable_tdp();
7605 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007606 kvm_disable_tdp();
7607
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007608 if (!nested) {
7609 kvm_x86_ops->get_nested_state = NULL;
7610 kvm_x86_ops->set_nested_state = NULL;
7611 }
7612
Kai Huang843e4332015-01-28 10:54:28 +08007613 /*
7614 * Only enable PML when hardware supports PML feature, and both EPT
7615 * and EPT A/D bit features are enabled -- PML depends on them to work.
7616 */
7617 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7618 enable_pml = 0;
7619
7620 if (!enable_pml) {
7621 kvm_x86_ops->slot_enable_log_dirty = NULL;
7622 kvm_x86_ops->slot_disable_log_dirty = NULL;
7623 kvm_x86_ops->flush_log_dirty = NULL;
7624 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7625 }
7626
Yunhong Jiang64672c92016-06-13 14:19:59 -07007627 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7628 u64 vmx_msr;
7629
7630 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7631 cpu_preemption_timer_multi =
7632 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7633 } else {
7634 kvm_x86_ops->set_hv_timer = NULL;
7635 kvm_x86_ops->cancel_hv_timer = NULL;
7636 }
7637
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007638 if (!cpu_has_vmx_shadow_vmcs())
7639 enable_shadow_vmcs = 0;
7640 if (enable_shadow_vmcs)
7641 init_vmcs_shadow_fields();
7642
Feng Wubf9f6ac2015-09-18 22:29:55 +08007643 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007644 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007645
Ashok Rajc45dcc72016-06-22 14:59:56 +08007646 kvm_mce_cap_supported |= MCG_LMCE_P;
7647
Tiejun Chenf2c76482014-10-28 10:14:47 +08007648 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007649
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007650out:
Radim Krčmář23611332016-09-29 22:41:33 +02007651 for (i = 0; i < VMX_BITMAP_NR; i++)
7652 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007653
7654 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007655}
7656
7657static __exit void hardware_unsetup(void)
7658{
Radim Krčmář23611332016-09-29 22:41:33 +02007659 int i;
7660
7661 for (i = 0; i < VMX_BITMAP_NR; i++)
7662 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007663
Tiejun Chenf2c76482014-10-28 10:14:47 +08007664 free_kvm_area();
7665}
7666
Avi Kivity6aa8b732006-12-10 02:21:36 -08007667/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007668 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7669 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7670 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007671static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007672{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007673 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007674 grow_ple_window(vcpu);
7675
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007676 /*
7677 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7678 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7679 * never set PAUSE_EXITING and just set PLE if supported,
7680 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7681 */
7682 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007683 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007684}
7685
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007686static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007687{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007688 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007689}
7690
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007691static int handle_mwait(struct kvm_vcpu *vcpu)
7692{
7693 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7694 return handle_nop(vcpu);
7695}
7696
Jim Mattson45ec3682017-08-23 16:32:04 -07007697static int handle_invalid_op(struct kvm_vcpu *vcpu)
7698{
7699 kvm_queue_exception(vcpu, UD_VECTOR);
7700 return 1;
7701}
7702
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007703static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7704{
7705 return 1;
7706}
7707
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007708static int handle_monitor(struct kvm_vcpu *vcpu)
7709{
7710 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7711 return handle_nop(vcpu);
7712}
7713
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007714/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007715 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7716 * set the success or error code of an emulated VMX instruction, as specified
7717 * by Vol 2B, VMX Instruction Reference, "Conventions".
7718 */
7719static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7720{
7721 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7722 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7723 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7724}
7725
7726static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7727{
7728 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7729 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7730 X86_EFLAGS_SF | X86_EFLAGS_OF))
7731 | X86_EFLAGS_CF);
7732}
7733
Abel Gordon145c28d2013-04-18 14:36:55 +03007734static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007735 u32 vm_instruction_error)
7736{
7737 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7738 /*
7739 * failValid writes the error number to the current VMCS, which
7740 * can't be done there isn't a current VMCS.
7741 */
7742 nested_vmx_failInvalid(vcpu);
7743 return;
7744 }
7745 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7746 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7747 X86_EFLAGS_SF | X86_EFLAGS_OF))
7748 | X86_EFLAGS_ZF);
7749 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7750 /*
7751 * We don't need to force a shadow sync because
7752 * VM_INSTRUCTION_ERROR is not shadowed
7753 */
7754}
Abel Gordon145c28d2013-04-18 14:36:55 +03007755
Wincy Vanff651cb2014-12-11 08:52:58 +03007756static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7757{
7758 /* TODO: not to reset guest simply here. */
7759 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007760 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007761}
7762
Jan Kiszkaf4124502014-03-07 20:03:13 +01007763static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7764{
7765 struct vcpu_vmx *vmx =
7766 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7767
7768 vmx->nested.preemption_timer_expired = true;
7769 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7770 kvm_vcpu_kick(&vmx->vcpu);
7771
7772 return HRTIMER_NORESTART;
7773}
7774
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007775/*
Bandan Das19677e32014-05-06 02:19:15 -04007776 * Decode the memory-address operand of a vmx instruction, as recorded on an
7777 * exit caused by such an instruction (run by a guest hypervisor).
7778 * On success, returns 0. When the operand is invalid, returns 1 and throws
7779 * #UD or #GP.
7780 */
7781static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7782 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007783 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007784{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007785 gva_t off;
7786 bool exn;
7787 struct kvm_segment s;
7788
Bandan Das19677e32014-05-06 02:19:15 -04007789 /*
7790 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7791 * Execution", on an exit, vmx_instruction_info holds most of the
7792 * addressing components of the operand. Only the displacement part
7793 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7794 * For how an actual address is calculated from all these components,
7795 * refer to Vol. 1, "Operand Addressing".
7796 */
7797 int scaling = vmx_instruction_info & 3;
7798 int addr_size = (vmx_instruction_info >> 7) & 7;
7799 bool is_reg = vmx_instruction_info & (1u << 10);
7800 int seg_reg = (vmx_instruction_info >> 15) & 7;
7801 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7802 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7803 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7804 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7805
7806 if (is_reg) {
7807 kvm_queue_exception(vcpu, UD_VECTOR);
7808 return 1;
7809 }
7810
7811 /* Addr = segment_base + offset */
7812 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007813 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007814 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007815 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007816 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007817 off += kvm_register_read(vcpu, index_reg)<<scaling;
7818 vmx_get_segment(vcpu, &s, seg_reg);
7819 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007820
7821 if (addr_size == 1) /* 32 bit */
7822 *ret &= 0xffffffff;
7823
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007824 /* Checks for #GP/#SS exceptions. */
7825 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007826 if (is_long_mode(vcpu)) {
7827 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7828 * non-canonical form. This is the only check on the memory
7829 * destination for long mode!
7830 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007831 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007832 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007833 /* Protected mode: apply checks for segment validity in the
7834 * following order:
7835 * - segment type check (#GP(0) may be thrown)
7836 * - usability check (#GP(0)/#SS(0))
7837 * - limit check (#GP(0)/#SS(0))
7838 */
7839 if (wr)
7840 /* #GP(0) if the destination operand is located in a
7841 * read-only data segment or any code segment.
7842 */
7843 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7844 else
7845 /* #GP(0) if the source operand is located in an
7846 * execute-only code segment
7847 */
7848 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007849 if (exn) {
7850 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7851 return 1;
7852 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007853 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7854 */
7855 exn = (s.unusable != 0);
7856 /* Protected mode: #GP(0)/#SS(0) if the memory
7857 * operand is outside the segment limit.
7858 */
7859 exn = exn || (off + sizeof(u64) > s.limit);
7860 }
7861 if (exn) {
7862 kvm_queue_exception_e(vcpu,
7863 seg_reg == VCPU_SREG_SS ?
7864 SS_VECTOR : GP_VECTOR,
7865 0);
7866 return 1;
7867 }
7868
Bandan Das19677e32014-05-06 02:19:15 -04007869 return 0;
7870}
7871
Radim Krčmářcbf71272017-05-19 15:48:51 +02007872static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007873{
7874 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007875 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007876
7877 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007878 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007879 return 1;
7880
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007881 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007882 kvm_inject_page_fault(vcpu, &e);
7883 return 1;
7884 }
7885
Bandan Das3573e222014-05-06 02:19:16 -04007886 return 0;
7887}
7888
Jim Mattsone29acc52016-11-30 12:03:43 -08007889static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7890{
7891 struct vcpu_vmx *vmx = to_vmx(vcpu);
7892 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007893 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007894
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007895 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7896 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007897 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007898
7899 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7900 if (!vmx->nested.cached_vmcs12)
7901 goto out_cached_vmcs12;
7902
7903 if (enable_shadow_vmcs) {
7904 shadow_vmcs = alloc_vmcs();
7905 if (!shadow_vmcs)
7906 goto out_shadow_vmcs;
7907 /* mark vmcs as shadow */
Liran Alon392b2f22018-06-23 02:35:01 +03007908 shadow_vmcs->hdr.shadow_vmcs = 1;
Jim Mattsone29acc52016-11-30 12:03:43 -08007909 /* init shadow vmcs */
7910 vmcs_clear(shadow_vmcs);
7911 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7912 }
7913
Jim Mattsone29acc52016-11-30 12:03:43 -08007914 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7915 HRTIMER_MODE_REL_PINNED);
7916 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7917
7918 vmx->nested.vmxon = true;
7919 return 0;
7920
7921out_shadow_vmcs:
7922 kfree(vmx->nested.cached_vmcs12);
7923
7924out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007925 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007926
Jim Mattsonde3a0022017-11-27 17:22:25 -06007927out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007928 return -ENOMEM;
7929}
7930
Bandan Das3573e222014-05-06 02:19:16 -04007931/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007932 * Emulate the VMXON instruction.
7933 * Currently, we just remember that VMX is active, and do not save or even
7934 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7935 * do not currently need to store anything in that guest-allocated memory
7936 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7937 * argument is different from the VMXON pointer (which the spec says they do).
7938 */
7939static int handle_vmon(struct kvm_vcpu *vcpu)
7940{
Jim Mattsone29acc52016-11-30 12:03:43 -08007941 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007942 gpa_t vmptr;
7943 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007945 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7946 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007947
Jim Mattson70f3aac2017-04-26 08:53:46 -07007948 /*
7949 * The Intel VMX Instruction Reference lists a bunch of bits that are
7950 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7951 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7952 * Otherwise, we should fail with #UD. But most faulting conditions
7953 * have already been checked by hardware, prior to the VM-exit for
7954 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7955 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007956 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007957 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007958 kvm_queue_exception(vcpu, UD_VECTOR);
7959 return 1;
7960 }
7961
Felix Wilhelm727ba742018-06-11 09:43:44 +02007962 /* CPL=0 must be checked manually. */
7963 if (vmx_get_cpl(vcpu)) {
7964 kvm_queue_exception(vcpu, UD_VECTOR);
7965 return 1;
7966 }
7967
Abel Gordon145c28d2013-04-18 14:36:55 +03007968 if (vmx->nested.vmxon) {
7969 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007970 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007971 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007972
Haozhong Zhang3b840802016-06-22 14:59:54 +08007973 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007974 != VMXON_NEEDED_FEATURES) {
7975 kvm_inject_gp(vcpu, 0);
7976 return 1;
7977 }
7978
Radim Krčmářcbf71272017-05-19 15:48:51 +02007979 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007980 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007981
7982 /*
7983 * SDM 3: 24.11.5
7984 * The first 4 bytes of VMXON region contain the supported
7985 * VMCS revision identifier
7986 *
7987 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7988 * which replaces physical address width with 32
7989 */
7990 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7991 nested_vmx_failInvalid(vcpu);
7992 return kvm_skip_emulated_instruction(vcpu);
7993 }
7994
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007995 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7996 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007997 nested_vmx_failInvalid(vcpu);
7998 return kvm_skip_emulated_instruction(vcpu);
7999 }
8000 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8001 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008002 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008003 nested_vmx_failInvalid(vcpu);
8004 return kvm_skip_emulated_instruction(vcpu);
8005 }
8006 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008007 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008008
8009 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008010 ret = enter_vmx_operation(vcpu);
8011 if (ret)
8012 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008013
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008014 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008015 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008016}
8017
8018/*
8019 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8020 * for running VMX instructions (except VMXON, whose prerequisites are
8021 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008022 * Note that many of these exceptions have priority over VM exits, so they
8023 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008024 */
8025static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8026{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008027 if (vmx_get_cpl(vcpu)) {
8028 kvm_queue_exception(vcpu, UD_VECTOR);
8029 return 0;
8030 }
8031
Jim Mattson70f3aac2017-04-26 08:53:46 -07008032 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008033 kvm_queue_exception(vcpu, UD_VECTOR);
8034 return 0;
8035 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008036 return 1;
8037}
8038
David Matlack8ca44e82017-08-01 14:00:39 -07008039static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8040{
8041 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8042 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8043}
8044
Abel Gordone7953d72013-04-18 14:37:55 +03008045static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8046{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008047 if (vmx->nested.current_vmptr == -1ull)
8048 return;
8049
Abel Gordon012f83c2013-04-18 14:39:25 +03008050 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008051 /* copy to memory all shadowed fields in case
8052 they were modified */
8053 copy_shadow_to_vmcs12(vmx);
8054 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008055 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008056 }
Wincy Van705699a2015-02-03 23:58:17 +08008057 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008058
8059 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008060 kvm_vcpu_write_guest_page(&vmx->vcpu,
8061 vmx->nested.current_vmptr >> PAGE_SHIFT,
8062 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008063
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008064 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008065}
8066
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008067/*
8068 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8069 * just stops using VMX.
8070 */
8071static void free_nested(struct vcpu_vmx *vmx)
8072{
Wanpeng Lib7455822017-11-22 14:04:00 -08008073 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008074 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008075
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008076 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008077 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008078 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008079 vmx->nested.posted_intr_nv = -1;
8080 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008081 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008082 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008083 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8084 free_vmcs(vmx->vmcs01.shadow_vmcs);
8085 vmx->vmcs01.shadow_vmcs = NULL;
8086 }
David Matlack4f2777b2016-07-13 17:16:37 -07008087 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008088 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008089 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008090 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008091 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008092 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008093 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008094 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008095 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008096 }
Wincy Van705699a2015-02-03 23:58:17 +08008097 if (vmx->nested.pi_desc_page) {
8098 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008099 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008100 vmx->nested.pi_desc_page = NULL;
8101 vmx->nested.pi_desc = NULL;
8102 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008103
Jim Mattsonde3a0022017-11-27 17:22:25 -06008104 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008105}
8106
8107/* Emulate the VMXOFF instruction */
8108static int handle_vmoff(struct kvm_vcpu *vcpu)
8109{
8110 if (!nested_vmx_check_permission(vcpu))
8111 return 1;
8112 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008113 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008114 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008115}
8116
Nadav Har'El27d6c862011-05-25 23:06:59 +03008117/* Emulate the VMCLEAR instruction */
8118static int handle_vmclear(struct kvm_vcpu *vcpu)
8119{
8120 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008121 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008122 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008123
8124 if (!nested_vmx_check_permission(vcpu))
8125 return 1;
8126
Radim Krčmářcbf71272017-05-19 15:48:51 +02008127 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008128 return 1;
8129
Radim Krčmářcbf71272017-05-19 15:48:51 +02008130 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8131 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8132 return kvm_skip_emulated_instruction(vcpu);
8133 }
8134
8135 if (vmptr == vmx->nested.vmxon_ptr) {
8136 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8137 return kvm_skip_emulated_instruction(vcpu);
8138 }
8139
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008140 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008141 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008142
Jim Mattson587d7e722017-03-02 12:41:48 -08008143 kvm_vcpu_write_guest(vcpu,
8144 vmptr + offsetof(struct vmcs12, launch_state),
8145 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008146
Nadav Har'El27d6c862011-05-25 23:06:59 +03008147 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008148 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008149}
8150
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008151static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8152
8153/* Emulate the VMLAUNCH instruction */
8154static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8155{
8156 return nested_vmx_run(vcpu, true);
8157}
8158
8159/* Emulate the VMRESUME instruction */
8160static int handle_vmresume(struct kvm_vcpu *vcpu)
8161{
8162
8163 return nested_vmx_run(vcpu, false);
8164}
8165
Nadav Har'El49f705c2011-05-25 23:08:30 +03008166/*
8167 * Read a vmcs12 field. Since these can have varying lengths and we return
8168 * one type, we chose the biggest type (u64) and zero-extend the return value
8169 * to that size. Note that the caller, handle_vmread, might need to use only
8170 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8171 * 64-bit fields are to be returned).
8172 */
Liran Alone2536742018-06-23 02:35:02 +03008173static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008174 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008175{
8176 short offset = vmcs_field_to_offset(field);
8177 char *p;
8178
8179 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008180 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008181
Liran Alone2536742018-06-23 02:35:02 +03008182 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008183
Jim Mattsond37f4262017-12-22 12:12:16 -08008184 switch (vmcs_field_width(field)) {
8185 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008186 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008187 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008188 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008189 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008190 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008191 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008192 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008193 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008194 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008195 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008196 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008197 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008198 WARN_ON(1);
8199 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008200 }
8201}
8202
Abel Gordon20b97fe2013-04-18 14:36:25 +03008203
Liran Alone2536742018-06-23 02:35:02 +03008204static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008205 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008206 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008207 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008208 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008209 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008210
Jim Mattsond37f4262017-12-22 12:12:16 -08008211 switch (vmcs_field_width(field)) {
8212 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008213 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008214 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008215 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008216 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008217 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008218 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008219 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008220 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008221 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008222 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008223 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008224 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008225 WARN_ON(1);
8226 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008227 }
8228
8229}
8230
Jim Mattsonf4160e42018-05-29 09:11:33 -07008231/*
8232 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8233 * they have been modified by the L1 guest. Note that the "read-only"
8234 * VM-exit information fields are actually writable if the vCPU is
8235 * configured to support "VMWRITE to any supported field in the VMCS."
8236 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008237static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8238{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008239 const u16 *fields[] = {
8240 shadow_read_write_fields,
8241 shadow_read_only_fields
8242 };
8243 const int max_fields[] = {
8244 max_shadow_read_write_fields,
8245 max_shadow_read_only_fields
8246 };
8247 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008248 unsigned long field;
8249 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008250 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008251
Jan Kiszka282da872014-10-08 18:05:39 +02008252 preempt_disable();
8253
Abel Gordon16f5b902013-04-18 14:38:25 +03008254 vmcs_load(shadow_vmcs);
8255
Jim Mattsonf4160e42018-05-29 09:11:33 -07008256 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8257 for (i = 0; i < max_fields[q]; i++) {
8258 field = fields[q][i];
8259 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008260 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008261 }
8262 /*
8263 * Skip the VM-exit information fields if they are read-only.
8264 */
8265 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8266 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008267 }
8268
8269 vmcs_clear(shadow_vmcs);
8270 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008271
8272 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008273}
8274
Abel Gordonc3114422013-04-18 14:38:55 +03008275static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8276{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008277 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008278 shadow_read_write_fields,
8279 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008280 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008281 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008282 max_shadow_read_write_fields,
8283 max_shadow_read_only_fields
8284 };
8285 int i, q;
8286 unsigned long field;
8287 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008288 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008289
8290 vmcs_load(shadow_vmcs);
8291
Mathias Krausec2bae892013-06-26 20:36:21 +02008292 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008293 for (i = 0; i < max_fields[q]; i++) {
8294 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008295 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008296 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008297 }
8298 }
8299
8300 vmcs_clear(shadow_vmcs);
8301 vmcs_load(vmx->loaded_vmcs->vmcs);
8302}
8303
Nadav Har'El49f705c2011-05-25 23:08:30 +03008304/*
8305 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8306 * used before) all generate the same failure when it is missing.
8307 */
8308static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8309{
8310 struct vcpu_vmx *vmx = to_vmx(vcpu);
8311 if (vmx->nested.current_vmptr == -1ull) {
8312 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008313 return 0;
8314 }
8315 return 1;
8316}
8317
8318static int handle_vmread(struct kvm_vcpu *vcpu)
8319{
8320 unsigned long field;
8321 u64 field_value;
8322 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8323 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8324 gva_t gva = 0;
8325
Kyle Hueyeb277562016-11-29 12:40:39 -08008326 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008327 return 1;
8328
Kyle Huey6affcbe2016-11-29 12:40:40 -08008329 if (!nested_vmx_check_vmcs12(vcpu))
8330 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008331
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008333 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008334 /* Read the field, zero-extended to a u64 field_value */
Liran Alone2536742018-06-23 02:35:02 +03008335 if (vmcs12_read_any(get_vmcs12(vcpu), field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008336 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008337 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008338 }
8339 /*
8340 * Now copy part of this value to register or memory, as requested.
8341 * Note that the number of bits actually copied is 32 or 64 depending
8342 * on the guest's mode (32 or 64 bit), not on the given field's length.
8343 */
8344 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008345 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008346 field_value);
8347 } else {
8348 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008349 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008350 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008351 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008352 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8353 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008354 }
8355
8356 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008357 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008358}
8359
8360
8361static int handle_vmwrite(struct kvm_vcpu *vcpu)
8362{
8363 unsigned long field;
8364 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008365 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008366 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8367 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008368
Nadav Har'El49f705c2011-05-25 23:08:30 +03008369 /* The value to write might be 32 or 64 bits, depending on L1's long
8370 * mode, and eventually we need to write that into a field of several
8371 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008372 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008373 * bits into the vmcs12 field.
8374 */
8375 u64 field_value = 0;
8376 struct x86_exception e;
8377
Kyle Hueyeb277562016-11-29 12:40:39 -08008378 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008379 return 1;
8380
Kyle Huey6affcbe2016-11-29 12:40:40 -08008381 if (!nested_vmx_check_vmcs12(vcpu))
8382 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008383
Nadav Har'El49f705c2011-05-25 23:08:30 +03008384 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008385 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008386 (((vmx_instruction_info) >> 3) & 0xf));
8387 else {
8388 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008389 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008390 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008391 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8392 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008393 kvm_inject_page_fault(vcpu, &e);
8394 return 1;
8395 }
8396 }
8397
8398
Nadav Amit27e6fb52014-06-18 17:19:26 +03008399 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008400 /*
8401 * If the vCPU supports "VMWRITE to any supported field in the
8402 * VMCS," then the "read-only" fields are actually read/write.
8403 */
8404 if (vmcs_field_readonly(field) &&
8405 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008406 nested_vmx_failValid(vcpu,
8407 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008408 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008409 }
8410
Liran Alone2536742018-06-23 02:35:02 +03008411 if (vmcs12_write_any(get_vmcs12(vcpu), field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008412 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008413 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008414 }
8415
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008416 switch (field) {
8417#define SHADOW_FIELD_RW(x) case x:
8418#include "vmx_shadow_fields.h"
8419 /*
8420 * The fields that can be updated by L1 without a vmexit are
8421 * always updated in the vmcs02, the others go down the slow
8422 * path of prepare_vmcs02.
8423 */
8424 break;
8425 default:
8426 vmx->nested.dirty_vmcs12 = true;
8427 break;
8428 }
8429
Nadav Har'El49f705c2011-05-25 23:08:30 +03008430 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008431 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008432}
8433
Jim Mattsona8bc2842016-11-30 12:03:44 -08008434static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8435{
8436 vmx->nested.current_vmptr = vmptr;
8437 if (enable_shadow_vmcs) {
8438 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8439 SECONDARY_EXEC_SHADOW_VMCS);
8440 vmcs_write64(VMCS_LINK_POINTER,
8441 __pa(vmx->vmcs01.shadow_vmcs));
8442 vmx->nested.sync_shadow_vmcs = true;
8443 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008444 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008445}
8446
Nadav Har'El63846662011-05-25 23:07:29 +03008447/* Emulate the VMPTRLD instruction */
8448static int handle_vmptrld(struct kvm_vcpu *vcpu)
8449{
8450 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008451 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008452
8453 if (!nested_vmx_check_permission(vcpu))
8454 return 1;
8455
Radim Krčmářcbf71272017-05-19 15:48:51 +02008456 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008457 return 1;
8458
Radim Krčmářcbf71272017-05-19 15:48:51 +02008459 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8460 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8461 return kvm_skip_emulated_instruction(vcpu);
8462 }
8463
8464 if (vmptr == vmx->nested.vmxon_ptr) {
8465 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8466 return kvm_skip_emulated_instruction(vcpu);
8467 }
8468
Nadav Har'El63846662011-05-25 23:07:29 +03008469 if (vmx->nested.current_vmptr != vmptr) {
8470 struct vmcs12 *new_vmcs12;
8471 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008472 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8473 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008474 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008475 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008476 }
8477 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008478 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008479 (new_vmcs12->hdr.shadow_vmcs &&
8480 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008481 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008482 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008483 nested_vmx_failValid(vcpu,
8484 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008485 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008486 }
Nadav Har'El63846662011-05-25 23:07:29 +03008487
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008488 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008489 /*
8490 * Load VMCS12 from guest memory since it is not already
8491 * cached.
8492 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008493 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8494 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008495 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008496
Jim Mattsona8bc2842016-11-30 12:03:44 -08008497 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008498 }
8499
8500 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008501 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008502}
8503
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008504/* Emulate the VMPTRST instruction */
8505static int handle_vmptrst(struct kvm_vcpu *vcpu)
8506{
8507 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8508 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8509 gva_t vmcs_gva;
8510 struct x86_exception e;
8511
8512 if (!nested_vmx_check_permission(vcpu))
8513 return 1;
8514
8515 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008516 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008517 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008518 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008519 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8520 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8521 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008522 kvm_inject_page_fault(vcpu, &e);
8523 return 1;
8524 }
8525 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008526 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008527}
8528
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008529/* Emulate the INVEPT instruction */
8530static int handle_invept(struct kvm_vcpu *vcpu)
8531{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008532 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008533 u32 vmx_instruction_info, types;
8534 unsigned long type;
8535 gva_t gva;
8536 struct x86_exception e;
8537 struct {
8538 u64 eptp, gpa;
8539 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008540
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008541 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008542 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008543 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008544 kvm_queue_exception(vcpu, UD_VECTOR);
8545 return 1;
8546 }
8547
8548 if (!nested_vmx_check_permission(vcpu))
8549 return 1;
8550
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008551 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008552 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008553
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008554 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008555
Jim Mattson85c856b2016-10-26 08:38:38 -07008556 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008557 nested_vmx_failValid(vcpu,
8558 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008559 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008560 }
8561
8562 /* According to the Intel VMX instruction reference, the memory
8563 * operand is read even if it isn't needed (e.g., for type==global)
8564 */
8565 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008566 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008567 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008568 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008569 kvm_inject_page_fault(vcpu, &e);
8570 return 1;
8571 }
8572
8573 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008574 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008575 /*
8576 * TODO: track mappings and invalidate
8577 * single context requests appropriately
8578 */
8579 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008580 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008581 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008582 nested_vmx_succeed(vcpu);
8583 break;
8584 default:
8585 BUG_ON(1);
8586 break;
8587 }
8588
Kyle Huey6affcbe2016-11-29 12:40:40 -08008589 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008590}
8591
Petr Matouseka642fc32014-09-23 20:22:30 +02008592static int handle_invvpid(struct kvm_vcpu *vcpu)
8593{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008594 struct vcpu_vmx *vmx = to_vmx(vcpu);
8595 u32 vmx_instruction_info;
8596 unsigned long type, types;
8597 gva_t gva;
8598 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008599 struct {
8600 u64 vpid;
8601 u64 gla;
8602 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008603
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008604 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008605 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008606 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008607 kvm_queue_exception(vcpu, UD_VECTOR);
8608 return 1;
8609 }
8610
8611 if (!nested_vmx_check_permission(vcpu))
8612 return 1;
8613
8614 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8615 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8616
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008617 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008618 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008619
Jim Mattson85c856b2016-10-26 08:38:38 -07008620 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008621 nested_vmx_failValid(vcpu,
8622 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008623 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008624 }
8625
8626 /* according to the intel vmx instruction reference, the memory
8627 * operand is read even if it isn't needed (e.g., for type==global)
8628 */
8629 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8630 vmx_instruction_info, false, &gva))
8631 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008632 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008633 kvm_inject_page_fault(vcpu, &e);
8634 return 1;
8635 }
Jim Mattson40352602017-06-28 09:37:37 -07008636 if (operand.vpid >> 16) {
8637 nested_vmx_failValid(vcpu,
8638 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8639 return kvm_skip_emulated_instruction(vcpu);
8640 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008641
8642 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008643 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008644 if (!operand.vpid ||
8645 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008646 nested_vmx_failValid(vcpu,
8647 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8648 return kvm_skip_emulated_instruction(vcpu);
8649 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008650 if (cpu_has_vmx_invvpid_individual_addr() &&
8651 vmx->nested.vpid02) {
8652 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8653 vmx->nested.vpid02, operand.gla);
8654 } else
8655 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8656 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008657 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008658 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008659 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008660 nested_vmx_failValid(vcpu,
8661 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008662 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008663 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008664 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008665 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008666 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008667 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008668 break;
8669 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008670 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008671 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008672 }
8673
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008674 nested_vmx_succeed(vcpu);
8675
Kyle Huey6affcbe2016-11-29 12:40:40 -08008676 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008677}
8678
Kai Huang843e4332015-01-28 10:54:28 +08008679static int handle_pml_full(struct kvm_vcpu *vcpu)
8680{
8681 unsigned long exit_qualification;
8682
8683 trace_kvm_pml_full(vcpu->vcpu_id);
8684
8685 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8686
8687 /*
8688 * PML buffer FULL happened while executing iret from NMI,
8689 * "blocked by NMI" bit has to be set before next VM entry.
8690 */
8691 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008692 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008693 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8694 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8695 GUEST_INTR_STATE_NMI);
8696
8697 /*
8698 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8699 * here.., and there's no userspace involvement needed for PML.
8700 */
8701 return 1;
8702}
8703
Yunhong Jiang64672c92016-06-13 14:19:59 -07008704static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8705{
8706 kvm_lapic_expired_hv_timer(vcpu);
8707 return 1;
8708}
8709
Bandan Das41ab9372017-08-03 15:54:43 -04008710static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8711{
8712 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008713 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8714
8715 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008716 switch (address & VMX_EPTP_MT_MASK) {
8717 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008718 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008719 return false;
8720 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008721 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008722 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008723 return false;
8724 break;
8725 default:
8726 return false;
8727 }
8728
David Hildenbrandbb97a012017-08-10 23:15:28 +02008729 /* only 4 levels page-walk length are valid */
8730 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008731 return false;
8732
8733 /* Reserved bits should not be set */
8734 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8735 return false;
8736
8737 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008738 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008739 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008740 return false;
8741 }
8742
8743 return true;
8744}
8745
8746static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8747 struct vmcs12 *vmcs12)
8748{
8749 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8750 u64 address;
8751 bool accessed_dirty;
8752 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8753
8754 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8755 !nested_cpu_has_ept(vmcs12))
8756 return 1;
8757
8758 if (index >= VMFUNC_EPTP_ENTRIES)
8759 return 1;
8760
8761
8762 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8763 &address, index * 8, 8))
8764 return 1;
8765
David Hildenbrandbb97a012017-08-10 23:15:28 +02008766 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008767
8768 /*
8769 * If the (L2) guest does a vmfunc to the currently
8770 * active ept pointer, we don't have to do anything else
8771 */
8772 if (vmcs12->ept_pointer != address) {
8773 if (!valid_ept_address(vcpu, address))
8774 return 1;
8775
8776 kvm_mmu_unload(vcpu);
8777 mmu->ept_ad = accessed_dirty;
8778 mmu->base_role.ad_disabled = !accessed_dirty;
8779 vmcs12->ept_pointer = address;
8780 /*
8781 * TODO: Check what's the correct approach in case
8782 * mmu reload fails. Currently, we just let the next
8783 * reload potentially fail
8784 */
8785 kvm_mmu_reload(vcpu);
8786 }
8787
8788 return 0;
8789}
8790
Bandan Das2a499e42017-08-03 15:54:41 -04008791static int handle_vmfunc(struct kvm_vcpu *vcpu)
8792{
Bandan Das27c42a12017-08-03 15:54:42 -04008793 struct vcpu_vmx *vmx = to_vmx(vcpu);
8794 struct vmcs12 *vmcs12;
8795 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8796
8797 /*
8798 * VMFUNC is only supported for nested guests, but we always enable the
8799 * secondary control for simplicity; for non-nested mode, fake that we
8800 * didn't by injecting #UD.
8801 */
8802 if (!is_guest_mode(vcpu)) {
8803 kvm_queue_exception(vcpu, UD_VECTOR);
8804 return 1;
8805 }
8806
8807 vmcs12 = get_vmcs12(vcpu);
8808 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8809 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008810
8811 switch (function) {
8812 case 0:
8813 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8814 goto fail;
8815 break;
8816 default:
8817 goto fail;
8818 }
8819 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008820
8821fail:
8822 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8823 vmcs_read32(VM_EXIT_INTR_INFO),
8824 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008825 return 1;
8826}
8827
Nadav Har'El0140cae2011-05-25 23:06:28 +03008828/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008829 * The exit handlers return 1 if the exit was handled fully and guest execution
8830 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8831 * to be done to userspace and return 0.
8832 */
Mathias Krause772e0312012-08-30 01:30:19 +02008833static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008834 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8835 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008836 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008837 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008838 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008839 [EXIT_REASON_CR_ACCESS] = handle_cr,
8840 [EXIT_REASON_DR_ACCESS] = handle_dr,
8841 [EXIT_REASON_CPUID] = handle_cpuid,
8842 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8843 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8844 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8845 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008846 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008847 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008848 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008849 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008850 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008851 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008852 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008853 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008854 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008855 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008856 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008857 [EXIT_REASON_VMOFF] = handle_vmoff,
8858 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008859 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8860 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008861 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008862 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008863 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008864 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008865 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008866 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008867 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8868 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008869 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8870 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008871 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008872 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008873 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008874 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008875 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008876 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008877 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008878 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008879 [EXIT_REASON_XSAVES] = handle_xsaves,
8880 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008881 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008882 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008883 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008884};
8885
8886static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008887 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008888
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008889static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8890 struct vmcs12 *vmcs12)
8891{
8892 unsigned long exit_qualification;
8893 gpa_t bitmap, last_bitmap;
8894 unsigned int port;
8895 int size;
8896 u8 b;
8897
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008898 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008899 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008900
8901 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8902
8903 port = exit_qualification >> 16;
8904 size = (exit_qualification & 7) + 1;
8905
8906 last_bitmap = (gpa_t)-1;
8907 b = -1;
8908
8909 while (size > 0) {
8910 if (port < 0x8000)
8911 bitmap = vmcs12->io_bitmap_a;
8912 else if (port < 0x10000)
8913 bitmap = vmcs12->io_bitmap_b;
8914 else
Joe Perches1d804d02015-03-30 16:46:09 -07008915 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008916 bitmap += (port & 0x7fff) / 8;
8917
8918 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008919 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008920 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008921 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008922 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008923
8924 port++;
8925 size--;
8926 last_bitmap = bitmap;
8927 }
8928
Joe Perches1d804d02015-03-30 16:46:09 -07008929 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008930}
8931
Nadav Har'El644d7112011-05-25 23:12:35 +03008932/*
8933 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8934 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8935 * disinterest in the current event (read or write a specific MSR) by using an
8936 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8937 */
8938static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8939 struct vmcs12 *vmcs12, u32 exit_reason)
8940{
8941 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8942 gpa_t bitmap;
8943
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008944 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008945 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008946
8947 /*
8948 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8949 * for the four combinations of read/write and low/high MSR numbers.
8950 * First we need to figure out which of the four to use:
8951 */
8952 bitmap = vmcs12->msr_bitmap;
8953 if (exit_reason == EXIT_REASON_MSR_WRITE)
8954 bitmap += 2048;
8955 if (msr_index >= 0xc0000000) {
8956 msr_index -= 0xc0000000;
8957 bitmap += 1024;
8958 }
8959
8960 /* Then read the msr_index'th bit from this bitmap: */
8961 if (msr_index < 1024*8) {
8962 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008963 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008964 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008965 return 1 & (b >> (msr_index & 7));
8966 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008967 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008968}
8969
8970/*
8971 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8972 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8973 * intercept (via guest_host_mask etc.) the current event.
8974 */
8975static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8976 struct vmcs12 *vmcs12)
8977{
8978 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8979 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008980 int reg;
8981 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008982
8983 switch ((exit_qualification >> 4) & 3) {
8984 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008985 reg = (exit_qualification >> 8) & 15;
8986 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008987 switch (cr) {
8988 case 0:
8989 if (vmcs12->cr0_guest_host_mask &
8990 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008991 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008992 break;
8993 case 3:
8994 if ((vmcs12->cr3_target_count >= 1 &&
8995 vmcs12->cr3_target_value0 == val) ||
8996 (vmcs12->cr3_target_count >= 2 &&
8997 vmcs12->cr3_target_value1 == val) ||
8998 (vmcs12->cr3_target_count >= 3 &&
8999 vmcs12->cr3_target_value2 == val) ||
9000 (vmcs12->cr3_target_count >= 4 &&
9001 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009002 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009003 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009004 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009005 break;
9006 case 4:
9007 if (vmcs12->cr4_guest_host_mask &
9008 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009009 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009010 break;
9011 case 8:
9012 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009013 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009014 break;
9015 }
9016 break;
9017 case 2: /* clts */
9018 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9019 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009020 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009021 break;
9022 case 1: /* mov from cr */
9023 switch (cr) {
9024 case 3:
9025 if (vmcs12->cpu_based_vm_exec_control &
9026 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009027 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009028 break;
9029 case 8:
9030 if (vmcs12->cpu_based_vm_exec_control &
9031 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009032 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009033 break;
9034 }
9035 break;
9036 case 3: /* lmsw */
9037 /*
9038 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9039 * cr0. Other attempted changes are ignored, with no exit.
9040 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009041 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009042 if (vmcs12->cr0_guest_host_mask & 0xe &
9043 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009044 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009045 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9046 !(vmcs12->cr0_read_shadow & 0x1) &&
9047 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009048 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009049 break;
9050 }
Joe Perches1d804d02015-03-30 16:46:09 -07009051 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009052}
9053
9054/*
9055 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9056 * should handle it ourselves in L0 (and then continue L2). Only call this
9057 * when in is_guest_mode (L2).
9058 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009059static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009060{
Nadav Har'El644d7112011-05-25 23:12:35 +03009061 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9062 struct vcpu_vmx *vmx = to_vmx(vcpu);
9063 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9064
Jim Mattson4f350c62017-09-14 16:31:44 -07009065 if (vmx->nested.nested_run_pending)
9066 return false;
9067
9068 if (unlikely(vmx->fail)) {
9069 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9070 vmcs_read32(VM_INSTRUCTION_ERROR));
9071 return true;
9072 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009073
David Matlackc9f04402017-08-01 14:00:40 -07009074 /*
9075 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009076 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9077 * Page). The CPU may write to these pages via their host
9078 * physical address while L2 is running, bypassing any
9079 * address-translation-based dirty tracking (e.g. EPT write
9080 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009081 *
9082 * Mark them dirty on every exit from L2 to prevent them from
9083 * getting out of sync with dirty tracking.
9084 */
9085 nested_mark_vmcs12_pages_dirty(vcpu);
9086
Jim Mattson4f350c62017-09-14 16:31:44 -07009087 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9088 vmcs_readl(EXIT_QUALIFICATION),
9089 vmx->idt_vectoring_info,
9090 intr_info,
9091 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9092 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009093
9094 switch (exit_reason) {
9095 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009096 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009097 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009098 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009099 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009100 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009101 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009102 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009103 else if (is_debug(intr_info) &&
9104 vcpu->guest_debug &
9105 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9106 return false;
9107 else if (is_breakpoint(intr_info) &&
9108 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9109 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009110 return vmcs12->exception_bitmap &
9111 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9112 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009113 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009114 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009115 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009116 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009117 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009118 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009119 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009120 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009121 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009122 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009123 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009124 case EXIT_REASON_HLT:
9125 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9126 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009127 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009128 case EXIT_REASON_INVLPG:
9129 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9130 case EXIT_REASON_RDPMC:
9131 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009132 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009133 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009134 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009135 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009136 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009137 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9138 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9139 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9140 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9141 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9142 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009143 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009144 /*
9145 * VMX instructions trap unconditionally. This allows L1 to
9146 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9147 */
Joe Perches1d804d02015-03-30 16:46:09 -07009148 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009149 case EXIT_REASON_CR_ACCESS:
9150 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9151 case EXIT_REASON_DR_ACCESS:
9152 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9153 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009154 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009155 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9156 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009157 case EXIT_REASON_MSR_READ:
9158 case EXIT_REASON_MSR_WRITE:
9159 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9160 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009161 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009162 case EXIT_REASON_MWAIT_INSTRUCTION:
9163 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009164 case EXIT_REASON_MONITOR_TRAP_FLAG:
9165 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 case EXIT_REASON_MONITOR_INSTRUCTION:
9167 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9168 case EXIT_REASON_PAUSE_INSTRUCTION:
9169 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9170 nested_cpu_has2(vmcs12,
9171 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9172 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009173 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009174 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009175 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009176 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009177 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009178 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009179 /*
9180 * The controls for "virtualize APIC accesses," "APIC-
9181 * register virtualization," and "virtual-interrupt
9182 * delivery" only come from vmcs12.
9183 */
Joe Perches1d804d02015-03-30 16:46:09 -07009184 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009185 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009186 /*
9187 * L0 always deals with the EPT violation. If nested EPT is
9188 * used, and the nested mmu code discovers that the address is
9189 * missing in the guest EPT table (EPT12), the EPT violation
9190 * will be injected with nested_ept_inject_page_fault()
9191 */
Joe Perches1d804d02015-03-30 16:46:09 -07009192 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009194 /*
9195 * L2 never uses directly L1's EPT, but rather L0's own EPT
9196 * table (shadow on EPT) or a merged EPT table that L0 built
9197 * (EPT on EPT). So any problems with the structure of the
9198 * table is L0's fault.
9199 */
Joe Perches1d804d02015-03-30 16:46:09 -07009200 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009201 case EXIT_REASON_INVPCID:
9202 return
9203 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9204 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009205 case EXIT_REASON_WBINVD:
9206 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9207 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009208 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009209 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9210 /*
9211 * This should never happen, since it is not possible to
9212 * set XSS to a non-zero value---neither in L1 nor in L2.
9213 * If if it were, XSS would have to be checked against
9214 * the XSS exit bitmap in vmcs12.
9215 */
9216 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009217 case EXIT_REASON_PREEMPTION_TIMER:
9218 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009219 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009220 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009221 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009222 case EXIT_REASON_VMFUNC:
9223 /* VM functions are emulated through L2->L0 vmexits. */
9224 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009225 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009226 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009227 }
9228}
9229
Paolo Bonzini7313c692017-07-27 10:31:25 +02009230static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9231{
9232 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9233
9234 /*
9235 * At this point, the exit interruption info in exit_intr_info
9236 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9237 * we need to query the in-kernel LAPIC.
9238 */
9239 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9240 if ((exit_intr_info &
9241 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9242 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9243 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9244 vmcs12->vm_exit_intr_error_code =
9245 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9246 }
9247
9248 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9249 vmcs_readl(EXIT_QUALIFICATION));
9250 return 1;
9251}
9252
Avi Kivity586f9602010-11-18 13:09:54 +02009253static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9254{
9255 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9256 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9257}
9258
Kai Huanga3eaa862015-11-04 13:46:05 +08009259static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009260{
Kai Huanga3eaa862015-11-04 13:46:05 +08009261 if (vmx->pml_pg) {
9262 __free_page(vmx->pml_pg);
9263 vmx->pml_pg = NULL;
9264 }
Kai Huang843e4332015-01-28 10:54:28 +08009265}
9266
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009267static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009268{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009269 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009270 u64 *pml_buf;
9271 u16 pml_idx;
9272
9273 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9274
9275 /* Do nothing if PML buffer is empty */
9276 if (pml_idx == (PML_ENTITY_NUM - 1))
9277 return;
9278
9279 /* PML index always points to next available PML buffer entity */
9280 if (pml_idx >= PML_ENTITY_NUM)
9281 pml_idx = 0;
9282 else
9283 pml_idx++;
9284
9285 pml_buf = page_address(vmx->pml_pg);
9286 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9287 u64 gpa;
9288
9289 gpa = pml_buf[pml_idx];
9290 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009291 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009292 }
9293
9294 /* reset PML index */
9295 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9296}
9297
9298/*
9299 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9300 * Called before reporting dirty_bitmap to userspace.
9301 */
9302static void kvm_flush_pml_buffers(struct kvm *kvm)
9303{
9304 int i;
9305 struct kvm_vcpu *vcpu;
9306 /*
9307 * We only need to kick vcpu out of guest mode here, as PML buffer
9308 * is flushed at beginning of all VMEXITs, and it's obvious that only
9309 * vcpus running in guest are possible to have unflushed GPAs in PML
9310 * buffer.
9311 */
9312 kvm_for_each_vcpu(i, vcpu, kvm)
9313 kvm_vcpu_kick(vcpu);
9314}
9315
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009316static void vmx_dump_sel(char *name, uint32_t sel)
9317{
9318 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009319 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009320 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9321 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9322 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9323}
9324
9325static void vmx_dump_dtsel(char *name, uint32_t limit)
9326{
9327 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9328 name, vmcs_read32(limit),
9329 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9330}
9331
9332static void dump_vmcs(void)
9333{
9334 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9335 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9336 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9337 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9338 u32 secondary_exec_control = 0;
9339 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009340 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009341 int i, n;
9342
9343 if (cpu_has_secondary_exec_ctrls())
9344 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9345
9346 pr_err("*** Guest State ***\n");
9347 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9348 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9349 vmcs_readl(CR0_GUEST_HOST_MASK));
9350 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9351 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9352 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9353 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9354 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9355 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009356 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9357 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9358 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9359 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009360 }
9361 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9362 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9363 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9364 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9365 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9366 vmcs_readl(GUEST_SYSENTER_ESP),
9367 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9368 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9369 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9370 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9371 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9372 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9373 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9374 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9375 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9376 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9377 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9378 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9379 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009380 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9381 efer, vmcs_read64(GUEST_IA32_PAT));
9382 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9383 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009384 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009385 if (cpu_has_load_perf_global_ctrl &&
9386 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009387 pr_err("PerfGlobCtl = 0x%016llx\n",
9388 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009389 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009390 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009391 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9392 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9393 vmcs_read32(GUEST_ACTIVITY_STATE));
9394 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9395 pr_err("InterruptStatus = %04x\n",
9396 vmcs_read16(GUEST_INTR_STATUS));
9397
9398 pr_err("*** Host State ***\n");
9399 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9400 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9401 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9402 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9403 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9404 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9405 vmcs_read16(HOST_TR_SELECTOR));
9406 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9407 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9408 vmcs_readl(HOST_TR_BASE));
9409 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9410 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9411 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9412 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9413 vmcs_readl(HOST_CR4));
9414 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9415 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9416 vmcs_read32(HOST_IA32_SYSENTER_CS),
9417 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9418 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009419 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9420 vmcs_read64(HOST_IA32_EFER),
9421 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009422 if (cpu_has_load_perf_global_ctrl &&
9423 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009424 pr_err("PerfGlobCtl = 0x%016llx\n",
9425 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009426
9427 pr_err("*** Control State ***\n");
9428 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9429 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9430 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9431 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9432 vmcs_read32(EXCEPTION_BITMAP),
9433 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9434 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9435 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9436 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9437 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9438 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9439 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9440 vmcs_read32(VM_EXIT_INTR_INFO),
9441 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9442 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9443 pr_err(" reason=%08x qualification=%016lx\n",
9444 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9445 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9446 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9447 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009448 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009449 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009450 pr_err("TSC Multiplier = 0x%016llx\n",
9451 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009452 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9453 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9454 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9455 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9456 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009457 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009458 n = vmcs_read32(CR3_TARGET_COUNT);
9459 for (i = 0; i + 1 < n; i += 4)
9460 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9461 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9462 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9463 if (i < n)
9464 pr_err("CR3 target%u=%016lx\n",
9465 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9466 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9467 pr_err("PLE Gap=%08x Window=%08x\n",
9468 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9469 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9470 pr_err("Virtual processor ID = 0x%04x\n",
9471 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9472}
9473
Avi Kivity6aa8b732006-12-10 02:21:36 -08009474/*
9475 * The guest has exited. See if we can fix it or if we need userspace
9476 * assistance.
9477 */
Avi Kivity851ba692009-08-24 11:10:17 +03009478static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009479{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009480 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009481 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009482 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009483
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009484 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9485
Kai Huang843e4332015-01-28 10:54:28 +08009486 /*
9487 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9488 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9489 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9490 * mode as if vcpus is in root mode, the PML buffer must has been
9491 * flushed already.
9492 */
9493 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009494 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009495
Mohammed Gamal80ced182009-09-01 12:48:18 +02009496 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009497 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009498 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009499
Paolo Bonzini7313c692017-07-27 10:31:25 +02009500 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9501 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009502
Mohammed Gamal51207022010-05-31 22:40:54 +03009503 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009504 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009505 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9506 vcpu->run->fail_entry.hardware_entry_failure_reason
9507 = exit_reason;
9508 return 0;
9509 }
9510
Avi Kivity29bd8a72007-09-10 17:27:03 +03009511 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009512 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9513 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009514 = vmcs_read32(VM_INSTRUCTION_ERROR);
9515 return 0;
9516 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009517
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009518 /*
9519 * Note:
9520 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9521 * delivery event since it indicates guest is accessing MMIO.
9522 * The vm-exit can be triggered again after return to guest that
9523 * will cause infinite loop.
9524 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009525 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009526 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009527 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009528 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009529 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9530 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9531 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009532 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009533 vcpu->run->internal.data[0] = vectoring_info;
9534 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009535 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9536 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9537 vcpu->run->internal.ndata++;
9538 vcpu->run->internal.data[3] =
9539 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9540 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009541 return 0;
9542 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009543
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009544 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009545 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9546 if (vmx_interrupt_allowed(vcpu)) {
9547 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9548 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9549 vcpu->arch.nmi_pending) {
9550 /*
9551 * This CPU don't support us in finding the end of an
9552 * NMI-blocked window if the guest runs with IRQs
9553 * disabled. So we pull the trigger after 1 s of
9554 * futile waiting, but inform the user about this.
9555 */
9556 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9557 "state on VCPU %d after 1 s timeout\n",
9558 __func__, vcpu->vcpu_id);
9559 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9560 }
9561 }
9562
Avi Kivity6aa8b732006-12-10 02:21:36 -08009563 if (exit_reason < kvm_vmx_max_exit_handlers
9564 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009565 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009566 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009567 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9568 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009569 kvm_queue_exception(vcpu, UD_VECTOR);
9570 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009571 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009572}
9573
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009574static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009575{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009576 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9577
9578 if (is_guest_mode(vcpu) &&
9579 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9580 return;
9581
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009582 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009583 vmcs_write32(TPR_THRESHOLD, 0);
9584 return;
9585 }
9586
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009587 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009588}
9589
Jim Mattson8d860bb2018-05-09 16:56:05 -04009590static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009591{
9592 u32 sec_exec_control;
9593
Jim Mattson8d860bb2018-05-09 16:56:05 -04009594 if (!lapic_in_kernel(vcpu))
9595 return;
9596
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009597 /* Postpone execution until vmcs01 is the current VMCS. */
9598 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009599 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009600 return;
9601 }
9602
Paolo Bonzini35754c92015-07-29 12:05:37 +02009603 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009604 return;
9605
9606 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009607 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9608 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009609
Jim Mattson8d860bb2018-05-09 16:56:05 -04009610 switch (kvm_get_apic_mode(vcpu)) {
9611 case LAPIC_MODE_INVALID:
9612 WARN_ONCE(true, "Invalid local APIC state");
9613 case LAPIC_MODE_DISABLED:
9614 break;
9615 case LAPIC_MODE_XAPIC:
9616 if (flexpriority_enabled) {
9617 sec_exec_control |=
9618 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9619 vmx_flush_tlb(vcpu, true);
9620 }
9621 break;
9622 case LAPIC_MODE_X2APIC:
9623 if (cpu_has_vmx_virtualize_x2apic_mode())
9624 sec_exec_control |=
9625 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9626 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009627 }
9628 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9629
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009630 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009631}
9632
Tang Chen38b99172014-09-24 15:57:54 +08009633static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9634{
Jim Mattsonab5df312018-05-09 17:02:03 -04009635 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009636 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009637 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009638 }
Tang Chen38b99172014-09-24 15:57:54 +08009639}
9640
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009641static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009642{
9643 u16 status;
9644 u8 old;
9645
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009646 if (max_isr == -1)
9647 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009648
9649 status = vmcs_read16(GUEST_INTR_STATUS);
9650 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009651 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009652 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009653 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009654 vmcs_write16(GUEST_INTR_STATUS, status);
9655 }
9656}
9657
9658static void vmx_set_rvi(int vector)
9659{
9660 u16 status;
9661 u8 old;
9662
Wei Wang4114c272014-11-05 10:53:43 +08009663 if (vector == -1)
9664 vector = 0;
9665
Yang Zhangc7c9c562013-01-25 10:18:51 +08009666 status = vmcs_read16(GUEST_INTR_STATUS);
9667 old = (u8)status & 0xff;
9668 if ((u8)vector != old) {
9669 status &= ~0xff;
9670 status |= (u8)vector;
9671 vmcs_write16(GUEST_INTR_STATUS, status);
9672 }
9673}
9674
9675static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9676{
Liran Alon851c1a182017-12-24 18:12:56 +02009677 /*
9678 * When running L2, updating RVI is only relevant when
9679 * vmcs12 virtual-interrupt-delivery enabled.
9680 * However, it can be enabled only when L1 also
9681 * intercepts external-interrupts and in that case
9682 * we should not update vmcs02 RVI but instead intercept
9683 * interrupt. Therefore, do nothing when running L2.
9684 */
9685 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009686 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009687}
9688
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009689static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009690{
9691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009692 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009693 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009694
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009695 WARN_ON(!vcpu->arch.apicv_active);
9696 if (pi_test_on(&vmx->pi_desc)) {
9697 pi_clear_on(&vmx->pi_desc);
9698 /*
9699 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9700 * But on x86 this is just a compiler barrier anyway.
9701 */
9702 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009703 max_irr_updated =
9704 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9705
9706 /*
9707 * If we are running L2 and L1 has a new pending interrupt
9708 * which can be injected, we should re-evaluate
9709 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009710 * If L1 intercepts external-interrupts, we should
9711 * exit from L2 to L1. Otherwise, interrupt should be
9712 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009713 */
Liran Alon851c1a182017-12-24 18:12:56 +02009714 if (is_guest_mode(vcpu) && max_irr_updated) {
9715 if (nested_exit_on_intr(vcpu))
9716 kvm_vcpu_exiting_guest_mode(vcpu);
9717 else
9718 kvm_make_request(KVM_REQ_EVENT, vcpu);
9719 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009720 } else {
9721 max_irr = kvm_lapic_find_highest_irr(vcpu);
9722 }
9723 vmx_hwapic_irr_update(vcpu, max_irr);
9724 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009725}
9726
Andrey Smetanin63086302015-11-10 15:36:32 +03009727static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009728{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009729 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009730 return;
9731
Yang Zhangc7c9c562013-01-25 10:18:51 +08009732 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9733 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9734 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9735 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9736}
9737
Paolo Bonzini967235d2016-12-19 14:03:45 +01009738static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9739{
9740 struct vcpu_vmx *vmx = to_vmx(vcpu);
9741
9742 pi_clear_on(&vmx->pi_desc);
9743 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9744}
9745
Avi Kivity51aa01d2010-07-20 14:31:20 +03009746static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009747{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009748 u32 exit_intr_info = 0;
9749 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009750
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009751 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9752 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009753 return;
9754
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009755 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9756 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9757 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009758
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009759 /* if exit due to PF check for async PF */
9760 if (is_page_fault(exit_intr_info))
9761 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9762
Andi Kleena0861c02009-06-08 17:37:09 +08009763 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009764 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9765 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009766 kvm_machine_check();
9767
Gleb Natapov20f65982009-05-11 13:35:55 +03009768 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009769 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009770 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009771 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009772 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009773 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009774}
Gleb Natapov20f65982009-05-11 13:35:55 +03009775
Yang Zhanga547c6d2013-04-11 19:25:10 +08009776static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9777{
9778 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9779
Yang Zhanga547c6d2013-04-11 19:25:10 +08009780 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9781 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9782 unsigned int vector;
9783 unsigned long entry;
9784 gate_desc *desc;
9785 struct vcpu_vmx *vmx = to_vmx(vcpu);
9786#ifdef CONFIG_X86_64
9787 unsigned long tmp;
9788#endif
9789
9790 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9791 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009792 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009793 asm volatile(
9794#ifdef CONFIG_X86_64
9795 "mov %%" _ASM_SP ", %[sp]\n\t"
9796 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9797 "push $%c[ss]\n\t"
9798 "push %[sp]\n\t"
9799#endif
9800 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009801 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009802 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009803 :
9804#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009805 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009806#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009807 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009808 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009809 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009810 [ss]"i"(__KERNEL_DS),
9811 [cs]"i"(__KERNEL_CS)
9812 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009813 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009814}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009815STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009816
Tom Lendackybc226f02018-05-10 22:06:39 +02009817static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009818{
Tom Lendackybc226f02018-05-10 22:06:39 +02009819 switch (index) {
9820 case MSR_IA32_SMBASE:
9821 /*
9822 * We cannot do SMM unless we can run the guest in big
9823 * real mode.
9824 */
9825 return enable_unrestricted_guest || emulate_invalid_guest_state;
9826 case MSR_AMD64_VIRT_SPEC_CTRL:
9827 /* This is AMD only. */
9828 return false;
9829 default:
9830 return true;
9831 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009832}
9833
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009834static bool vmx_mpx_supported(void)
9835{
9836 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9837 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9838}
9839
Wanpeng Li55412b22014-12-02 19:21:30 +08009840static bool vmx_xsaves_supported(void)
9841{
9842 return vmcs_config.cpu_based_2nd_exec_ctrl &
9843 SECONDARY_EXEC_XSAVES;
9844}
9845
Avi Kivity51aa01d2010-07-20 14:31:20 +03009846static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9847{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009848 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009849 bool unblock_nmi;
9850 u8 vector;
9851 bool idtv_info_valid;
9852
9853 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009854
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009855 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009856 if (vmx->loaded_vmcs->nmi_known_unmasked)
9857 return;
9858 /*
9859 * Can't use vmx->exit_intr_info since we're not sure what
9860 * the exit reason is.
9861 */
9862 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9863 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9864 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9865 /*
9866 * SDM 3: 27.7.1.2 (September 2008)
9867 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9868 * a guest IRET fault.
9869 * SDM 3: 23.2.2 (September 2008)
9870 * Bit 12 is undefined in any of the following cases:
9871 * If the VM exit sets the valid bit in the IDT-vectoring
9872 * information field.
9873 * If the VM exit is due to a double fault.
9874 */
9875 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9876 vector != DF_VECTOR && !idtv_info_valid)
9877 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9878 GUEST_INTR_STATE_NMI);
9879 else
9880 vmx->loaded_vmcs->nmi_known_unmasked =
9881 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9882 & GUEST_INTR_STATE_NMI);
9883 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9884 vmx->loaded_vmcs->vnmi_blocked_time +=
9885 ktime_to_ns(ktime_sub(ktime_get(),
9886 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009887}
9888
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009889static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009890 u32 idt_vectoring_info,
9891 int instr_len_field,
9892 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009893{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009894 u8 vector;
9895 int type;
9896 bool idtv_info_valid;
9897
9898 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009899
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009900 vcpu->arch.nmi_injected = false;
9901 kvm_clear_exception_queue(vcpu);
9902 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009903
9904 if (!idtv_info_valid)
9905 return;
9906
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009907 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009908
Avi Kivity668f6122008-07-02 09:28:55 +03009909 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9910 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009911
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009912 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009913 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009914 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009915 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009916 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009917 * Clear bit "block by NMI" before VM entry if a NMI
9918 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009919 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009920 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009921 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009922 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009923 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009924 /* fall through */
9925 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009926 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009927 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009928 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009929 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009930 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009931 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009932 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009933 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009934 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009935 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009936 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009937 break;
9938 default:
9939 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009940 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009941}
9942
Avi Kivity83422e12010-07-20 14:43:23 +03009943static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9944{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009945 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009946 VM_EXIT_INSTRUCTION_LEN,
9947 IDT_VECTORING_ERROR_CODE);
9948}
9949
Avi Kivityb463a6f2010-07-20 15:06:17 +03009950static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9951{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009952 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009953 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9954 VM_ENTRY_INSTRUCTION_LEN,
9955 VM_ENTRY_EXCEPTION_ERROR_CODE);
9956
9957 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9958}
9959
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009960static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9961{
9962 int i, nr_msrs;
9963 struct perf_guest_switch_msr *msrs;
9964
9965 msrs = perf_guest_get_msrs(&nr_msrs);
9966
9967 if (!msrs)
9968 return;
9969
9970 for (i = 0; i < nr_msrs; i++)
9971 if (msrs[i].host == msrs[i].guest)
9972 clear_atomic_switch_msr(vmx, msrs[i].msr);
9973 else
9974 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9975 msrs[i].host);
9976}
9977
Jiang Biao33365e72016-11-03 15:03:37 +08009978static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009979{
9980 struct vcpu_vmx *vmx = to_vmx(vcpu);
9981 u64 tscl;
9982 u32 delta_tsc;
9983
9984 if (vmx->hv_deadline_tsc == -1)
9985 return;
9986
9987 tscl = rdtsc();
9988 if (vmx->hv_deadline_tsc > tscl)
9989 /* sure to be 32 bit only because checked on set_hv_timer */
9990 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9991 cpu_preemption_timer_multi);
9992 else
9993 delta_tsc = 0;
9994
9995 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9996}
9997
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009998static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009999{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010000 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010001 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010002
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010003 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010004 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010005 vmx->loaded_vmcs->soft_vnmi_blocked))
10006 vmx->loaded_vmcs->entry_time = ktime_get();
10007
Avi Kivity104f2262010-11-18 13:12:52 +020010008 /* Don't enter VMX if guest state is invalid, let the exit handler
10009 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010010 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010011 return;
10012
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010013 if (vmx->ple_window_dirty) {
10014 vmx->ple_window_dirty = false;
10015 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10016 }
10017
Abel Gordon012f83c2013-04-18 14:39:25 +030010018 if (vmx->nested.sync_shadow_vmcs) {
10019 copy_vmcs12_to_shadow(vmx);
10020 vmx->nested.sync_shadow_vmcs = false;
10021 }
10022
Avi Kivity104f2262010-11-18 13:12:52 +020010023 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10024 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10025 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10026 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10027
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010028 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010029 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010030 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010031 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010032 }
10033
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010034 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010035 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010036 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010037 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010038 }
10039
Avi Kivity104f2262010-11-18 13:12:52 +020010040 /* When single-stepping over STI and MOV SS, we must clear the
10041 * corresponding interruptibility bits in the guest state. Otherwise
10042 * vmentry fails as it then expects bit 14 (BS) in pending debug
10043 * exceptions being set, but that's not correct for the guest debugging
10044 * case. */
10045 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10046 vmx_set_interrupt_shadow(vcpu, 0);
10047
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010048 if (static_cpu_has(X86_FEATURE_PKU) &&
10049 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10050 vcpu->arch.pkru != vmx->host_pkru)
10051 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010052
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010053 atomic_switch_perf_msrs(vmx);
10054
Yunhong Jiang64672c92016-06-13 14:19:59 -070010055 vmx_arm_hv_timer(vcpu);
10056
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010057 /*
10058 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10059 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10060 * is no need to worry about the conditional branch over the wrmsr
10061 * being speculatively taken.
10062 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010063 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010064
Nadav Har'Eld462b812011-05-24 15:26:10 +030010065 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010066
10067 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10068 (unsigned long)&current_evmcs->host_rsp : 0;
10069
Avi Kivity104f2262010-11-18 13:12:52 +020010070 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010071 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010072 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10073 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10074 "push %%" _ASM_CX " \n\t"
10075 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010076 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010077 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010078 /* Avoid VMWRITE when Enlightened VMCS is in use */
10079 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10080 "jz 2f \n\t"
10081 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10082 "jmp 1f \n\t"
10083 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010084 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010085 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010086 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010087 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10088 "mov %%cr2, %%" _ASM_DX " \n\t"
10089 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010090 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010091 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010092 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010093 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010094 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010095 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010096 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10097 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10098 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10099 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10100 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10101 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010102#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010103 "mov %c[r8](%0), %%r8 \n\t"
10104 "mov %c[r9](%0), %%r9 \n\t"
10105 "mov %c[r10](%0), %%r10 \n\t"
10106 "mov %c[r11](%0), %%r11 \n\t"
10107 "mov %c[r12](%0), %%r12 \n\t"
10108 "mov %c[r13](%0), %%r13 \n\t"
10109 "mov %c[r14](%0), %%r14 \n\t"
10110 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010111#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010112 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010113
Avi Kivity6aa8b732006-12-10 02:21:36 -080010114 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010115 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010116 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010117 "jmp 2f \n\t"
10118 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10119 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010120 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010121 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010122 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010123 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010124 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10125 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10126 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10127 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10128 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10129 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10130 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010131#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010132 "mov %%r8, %c[r8](%0) \n\t"
10133 "mov %%r9, %c[r9](%0) \n\t"
10134 "mov %%r10, %c[r10](%0) \n\t"
10135 "mov %%r11, %c[r11](%0) \n\t"
10136 "mov %%r12, %c[r12](%0) \n\t"
10137 "mov %%r13, %c[r13](%0) \n\t"
10138 "mov %%r14, %c[r14](%0) \n\t"
10139 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010140 "xor %%r8d, %%r8d \n\t"
10141 "xor %%r9d, %%r9d \n\t"
10142 "xor %%r10d, %%r10d \n\t"
10143 "xor %%r11d, %%r11d \n\t"
10144 "xor %%r12d, %%r12d \n\t"
10145 "xor %%r13d, %%r13d \n\t"
10146 "xor %%r14d, %%r14d \n\t"
10147 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010148#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010149 "mov %%cr2, %%" _ASM_AX " \n\t"
10150 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010151
Jim Mattson0cb5b302018-01-03 14:31:38 -080010152 "xor %%eax, %%eax \n\t"
10153 "xor %%ebx, %%ebx \n\t"
10154 "xor %%esi, %%esi \n\t"
10155 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010156 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010157 ".pushsection .rodata \n\t"
10158 ".global vmx_return \n\t"
10159 "vmx_return: " _ASM_PTR " 2b \n\t"
10160 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010161 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010162 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010163 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010164 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010165 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10166 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10167 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10168 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10169 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10170 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10171 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010172#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010173 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10174 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10175 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10176 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10177 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10178 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10179 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10180 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010181#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010182 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10183 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010184 : "cc", "memory"
10185#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010186 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010187 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010188#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010189 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010190#endif
10191 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010192
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010193 /*
10194 * We do not use IBRS in the kernel. If this vCPU has used the
10195 * SPEC_CTRL MSR it may have left it on; save the value and
10196 * turn it off. This is much more efficient than blindly adding
10197 * it to the atomic save/restore list. Especially as the former
10198 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10199 *
10200 * For non-nested case:
10201 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10202 * save it.
10203 *
10204 * For nested case:
10205 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10206 * save it.
10207 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010208 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010209 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010210
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010211 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010212
David Woodhouse117cc7a2018-01-12 11:11:27 +000010213 /* Eliminate branch target predictions from guest mode */
10214 vmexit_fill_RSB();
10215
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010216 /* All fields are clean at this point */
10217 if (static_branch_unlikely(&enable_evmcs))
10218 current_evmcs->hv_clean_fields |=
10219 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10220
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010221 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010222 if (vmx->host_debugctlmsr)
10223 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010224
Avi Kivityaa67f602012-08-01 16:48:03 +030010225#ifndef CONFIG_X86_64
10226 /*
10227 * The sysexit path does not restore ds/es, so we must set them to
10228 * a reasonable value ourselves.
10229 *
10230 * We can't defer this to vmx_load_host_state() since that function
10231 * may be executed in interrupt context, which saves and restore segments
10232 * around it, nullifying its effect.
10233 */
10234 loadsegment(ds, __USER_DS);
10235 loadsegment(es, __USER_DS);
10236#endif
10237
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010238 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010239 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010240 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010241 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010242 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010243 vcpu->arch.regs_dirty = 0;
10244
Gleb Natapove0b890d2013-09-25 12:51:33 +030010245 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010246 * eager fpu is enabled if PKEY is supported and CR4 is switched
10247 * back on host, so it is safe to read guest PKRU from current
10248 * XSAVE.
10249 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010250 if (static_cpu_has(X86_FEATURE_PKU) &&
10251 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10252 vcpu->arch.pkru = __read_pkru();
10253 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010254 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010255 }
10256
Gleb Natapove0b890d2013-09-25 12:51:33 +030010257 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010258 vmx->idt_vectoring_info = 0;
10259
10260 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10261 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10262 return;
10263
10264 vmx->loaded_vmcs->launched = 1;
10265 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010266
Avi Kivity51aa01d2010-07-20 14:31:20 +030010267 vmx_complete_atomic_exit(vmx);
10268 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010269 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010270}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010271STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010272
Sean Christopherson434a1e92018-03-20 12:17:18 -070010273static struct kvm *vmx_vm_alloc(void)
10274{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010275 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010276 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010277}
10278
10279static void vmx_vm_free(struct kvm *kvm)
10280{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010281 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010282}
10283
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010284static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010285{
10286 struct vcpu_vmx *vmx = to_vmx(vcpu);
10287 int cpu;
10288
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010289 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010290 return;
10291
10292 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010293 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010294 vmx_vcpu_put(vcpu);
10295 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010296 put_cpu();
10297}
10298
Jim Mattson2f1fe812016-07-08 15:36:06 -070010299/*
10300 * Ensure that the current vmcs of the logical processor is the
10301 * vmcs01 of the vcpu before calling free_nested().
10302 */
10303static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10304{
10305 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010306
Christoffer Dallec7660c2017-12-04 21:35:23 +010010307 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010308 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010309 free_nested(vmx);
10310 vcpu_put(vcpu);
10311}
10312
Avi Kivity6aa8b732006-12-10 02:21:36 -080010313static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10314{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010315 struct vcpu_vmx *vmx = to_vmx(vcpu);
10316
Kai Huang843e4332015-01-28 10:54:28 +080010317 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010318 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010319 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010320 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010321 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010322 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010323 kfree(vmx->guest_msrs);
10324 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010325 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010326}
10327
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010328static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010329{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010330 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010331 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010332 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010333 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010334
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010335 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010336 return ERR_PTR(-ENOMEM);
10337
Wanpeng Li991e7a02015-09-16 17:30:05 +080010338 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010339
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010340 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10341 if (err)
10342 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010343
Peter Feiner4e595162016-07-07 14:49:58 -070010344 err = -ENOMEM;
10345
10346 /*
10347 * If PML is turned on, failure on enabling PML just results in failure
10348 * of creating the vcpu, therefore we can simplify PML logic (by
10349 * avoiding dealing with cases, such as enabling PML partially on vcpus
10350 * for the guest, etc.
10351 */
10352 if (enable_pml) {
10353 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10354 if (!vmx->pml_pg)
10355 goto uninit_vcpu;
10356 }
10357
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010358 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010359 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10360 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010361
Peter Feiner4e595162016-07-07 14:49:58 -070010362 if (!vmx->guest_msrs)
10363 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010364
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010365 err = alloc_loaded_vmcs(&vmx->vmcs01);
10366 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010367 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010368
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010369 msr_bitmap = vmx->vmcs01.msr_bitmap;
10370 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10371 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10372 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10373 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10374 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10375 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10376 vmx->msr_bitmap_mode = 0;
10377
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010378 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010379 cpu = get_cpu();
10380 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010381 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010382 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010383 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010384 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010385 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010386 err = alloc_apic_access_page(kvm);
10387 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010388 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010389 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010390
Sean Christophersone90008d2018-03-05 12:04:37 -080010391 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010392 err = init_rmode_identity_map(kvm);
10393 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010394 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010395 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010396
Wanpeng Li5c614b32015-10-13 09:18:36 -070010397 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010398 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10399 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010400 vmx->nested.vpid02 = allocate_vpid();
10401 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010402
Wincy Van705699a2015-02-03 23:58:17 +080010403 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010404 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010405
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010406 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10407
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010408 /*
10409 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10410 * or POSTED_INTR_WAKEUP_VECTOR.
10411 */
10412 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10413 vmx->pi_desc.sn = 1;
10414
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010415 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010416
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010417free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010418 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010419 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010420free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010421 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010422free_pml:
10423 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010424uninit_vcpu:
10425 kvm_vcpu_uninit(&vmx->vcpu);
10426free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010427 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010428 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010429 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010430}
10431
Wanpeng Lib31c1142018-03-12 04:53:04 -070010432static int vmx_vm_init(struct kvm *kvm)
10433{
10434 if (!ple_gap)
10435 kvm->arch.pause_in_guest = true;
10436 return 0;
10437}
10438
Yang, Sheng002c7f72007-07-31 14:23:01 +030010439static void __init vmx_check_processor_compat(void *rtn)
10440{
10441 struct vmcs_config vmcs_conf;
10442
10443 *(int *)rtn = 0;
10444 if (setup_vmcs_config(&vmcs_conf) < 0)
10445 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010446 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010447 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10448 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10449 smp_processor_id());
10450 *(int *)rtn = -EIO;
10451 }
10452}
10453
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010454static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010455{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010456 u8 cache;
10457 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010458
Sheng Yang522c68c2009-04-27 20:35:43 +080010459 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010460 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010461 * 2. EPT with VT-d:
10462 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010463 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010464 * b. VT-d with snooping control feature: snooping control feature of
10465 * VT-d engine can guarantee the cache correctness. Just set it
10466 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010467 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010468 * consistent with host MTRR
10469 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010470 if (is_mmio) {
10471 cache = MTRR_TYPE_UNCACHABLE;
10472 goto exit;
10473 }
10474
10475 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010476 ipat = VMX_EPT_IPAT_BIT;
10477 cache = MTRR_TYPE_WRBACK;
10478 goto exit;
10479 }
10480
10481 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10482 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010483 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010484 cache = MTRR_TYPE_WRBACK;
10485 else
10486 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010487 goto exit;
10488 }
10489
Xiao Guangrongff536042015-06-15 16:55:22 +080010490 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010491
10492exit:
10493 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010494}
10495
Sheng Yang17cc3932010-01-05 19:02:27 +080010496static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010497{
Sheng Yang878403b2010-01-05 19:02:29 +080010498 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10499 return PT_DIRECTORY_LEVEL;
10500 else
10501 /* For shadow and EPT supported 1GB page */
10502 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010503}
10504
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010505static void vmcs_set_secondary_exec_control(u32 new_ctl)
10506{
10507 /*
10508 * These bits in the secondary execution controls field
10509 * are dynamic, the others are mostly based on the hypervisor
10510 * architecture and the guest's CPUID. Do not touch the
10511 * dynamic bits.
10512 */
10513 u32 mask =
10514 SECONDARY_EXEC_SHADOW_VMCS |
10515 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010516 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10517 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010518
10519 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10520
10521 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10522 (new_ctl & ~mask) | (cur_ctl & mask));
10523}
10524
David Matlack8322ebb2016-11-29 18:14:09 -080010525/*
10526 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10527 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10528 */
10529static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10530{
10531 struct vcpu_vmx *vmx = to_vmx(vcpu);
10532 struct kvm_cpuid_entry2 *entry;
10533
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010534 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10535 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010536
10537#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10538 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010539 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010540} while (0)
10541
10542 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10543 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10544 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10545 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10546 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10547 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10548 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10549 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10550 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10551 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10552 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10553 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10554 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10555 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10556 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10557
10558 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10559 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10560 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10561 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10562 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010563 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010564
10565#undef cr4_fixed1_update
10566}
10567
Sheng Yang0e851882009-12-18 16:48:46 +080010568static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10569{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010570 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010571
Paolo Bonzini80154d72017-08-24 13:55:35 +020010572 if (cpu_has_secondary_exec_ctrls()) {
10573 vmx_compute_secondary_exec_control(vmx);
10574 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010575 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010576
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010577 if (nested_vmx_allowed(vcpu))
10578 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10579 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10580 else
10581 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10582 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010583
10584 if (nested_vmx_allowed(vcpu))
10585 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010586}
10587
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010588static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10589{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010590 if (func == 1 && nested)
10591 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010592}
10593
Yang Zhang25d92082013-08-06 12:00:32 +030010594static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10595 struct x86_exception *fault)
10596{
Jan Kiszka533558b2014-01-04 18:47:20 +010010597 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010598 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010599 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010600 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010601
Bandan Dasc5f983f2017-05-05 15:25:14 -040010602 if (vmx->nested.pml_full) {
10603 exit_reason = EXIT_REASON_PML_FULL;
10604 vmx->nested.pml_full = false;
10605 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10606 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010607 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010608 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010609 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010610
10611 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010612 vmcs12->guest_physical_address = fault->address;
10613}
10614
Peter Feiner995f00a2017-06-30 17:26:32 -070010615static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10616{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010617 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010618}
10619
Nadav Har'El155a97a2013-08-05 11:07:16 +030010620/* Callbacks for nested_ept_init_mmu_context: */
10621
10622static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10623{
10624 /* return the page table to be shadowed - in our case, EPT12 */
10625 return get_vmcs12(vcpu)->ept_pointer;
10626}
10627
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010628static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010629{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010630 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010631 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010632 return 1;
10633
10634 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010635 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010636 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010637 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010638 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010639 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10640 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10641 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10642
10643 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010644 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010645}
10646
10647static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10648{
10649 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10650}
10651
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010652static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10653 u16 error_code)
10654{
10655 bool inequality, bit;
10656
10657 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10658 inequality =
10659 (error_code & vmcs12->page_fault_error_code_mask) !=
10660 vmcs12->page_fault_error_code_match;
10661 return inequality ^ bit;
10662}
10663
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010664static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10665 struct x86_exception *fault)
10666{
10667 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10668
10669 WARN_ON(!is_guest_mode(vcpu));
10670
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010671 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10672 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010673 vmcs12->vm_exit_intr_error_code = fault->error_code;
10674 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10675 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10676 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10677 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010678 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010679 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010680 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010681}
10682
Paolo Bonzinic9923842017-12-13 14:16:30 +010010683static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10684 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010685
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010686static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010687{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010688 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010689 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010690 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010691 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010692
10693 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010694 /*
10695 * Translate L1 physical address to host physical
10696 * address for vmcs02. Keep the page pinned, so this
10697 * physical address remains valid. We keep a reference
10698 * to it so we can release it later.
10699 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010700 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010701 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010702 vmx->nested.apic_access_page = NULL;
10703 }
10704 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010705 /*
10706 * If translation failed, no matter: This feature asks
10707 * to exit when accessing the given address, and if it
10708 * can never be accessed, this feature won't do
10709 * anything anyway.
10710 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010711 if (!is_error_page(page)) {
10712 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010713 hpa = page_to_phys(vmx->nested.apic_access_page);
10714 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10715 } else {
10716 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10717 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10718 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010719 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010720
10721 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010722 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010723 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010724 vmx->nested.virtual_apic_page = NULL;
10725 }
10726 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010727
10728 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010729 * If translation failed, VM entry will fail because
10730 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10731 * Failing the vm entry is _not_ what the processor
10732 * does but it's basically the only possibility we
10733 * have. We could still enter the guest if CR8 load
10734 * exits are enabled, CR8 store exits are enabled, and
10735 * virtualize APIC access is disabled; in this case
10736 * the processor would never use the TPR shadow and we
10737 * could simply clear the bit from the execution
10738 * control. But such a configuration is useless, so
10739 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010740 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010741 if (!is_error_page(page)) {
10742 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010743 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10744 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10745 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010746 }
10747
Wincy Van705699a2015-02-03 23:58:17 +080010748 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010749 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10750 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010751 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010752 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010753 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010754 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10755 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010756 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010757 vmx->nested.pi_desc_page = page;
10758 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010759 vmx->nested.pi_desc =
10760 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10761 (unsigned long)(vmcs12->posted_intr_desc_addr &
10762 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010763 vmcs_write64(POSTED_INTR_DESC_ADDR,
10764 page_to_phys(vmx->nested.pi_desc_page) +
10765 (unsigned long)(vmcs12->posted_intr_desc_addr &
10766 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010767 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010768 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010769 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10770 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010771 else
10772 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10773 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010774}
10775
Jan Kiszkaf4124502014-03-07 20:03:13 +010010776static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10777{
10778 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10779 struct vcpu_vmx *vmx = to_vmx(vcpu);
10780
10781 if (vcpu->arch.virtual_tsc_khz == 0)
10782 return;
10783
10784 /* Make sure short timeouts reliably trigger an immediate vmexit.
10785 * hrtimer_start does not guarantee this. */
10786 if (preemption_timeout <= 1) {
10787 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10788 return;
10789 }
10790
10791 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10792 preemption_timeout *= 1000000;
10793 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10794 hrtimer_start(&vmx->nested.preemption_timer,
10795 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10796}
10797
Jim Mattson56a20512017-07-06 16:33:06 -070010798static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10799 struct vmcs12 *vmcs12)
10800{
10801 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10802 return 0;
10803
10804 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10805 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10806 return -EINVAL;
10807
10808 return 0;
10809}
10810
Wincy Van3af18d92015-02-03 23:49:31 +080010811static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10812 struct vmcs12 *vmcs12)
10813{
Wincy Van3af18d92015-02-03 23:49:31 +080010814 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10815 return 0;
10816
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010817 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010818 return -EINVAL;
10819
10820 return 0;
10821}
10822
Jim Mattson712b12d2017-08-24 13:24:47 -070010823static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10824 struct vmcs12 *vmcs12)
10825{
10826 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10827 return 0;
10828
10829 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10830 return -EINVAL;
10831
10832 return 0;
10833}
10834
Wincy Van3af18d92015-02-03 23:49:31 +080010835/*
10836 * Merge L0's and L1's MSR bitmap, return false to indicate that
10837 * we do not use the hardware.
10838 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010839static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10840 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010841{
Wincy Van82f0dd42015-02-03 23:57:18 +080010842 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010843 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010844 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010845 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010846 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010847 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010848 *
10849 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10850 * ensures that we do not accidentally generate an L02 MSR bitmap
10851 * from the L12 MSR bitmap that is too permissive.
10852 * 2. That L1 or L2s have actually used the MSR. This avoids
10853 * unnecessarily merging of the bitmap if the MSR is unused. This
10854 * works properly because we only update the L01 MSR bitmap lazily.
10855 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10856 * updated to reflect this when L1 (or its L2s) actually write to
10857 * the MSR.
10858 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010859 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10860 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010861
Paolo Bonzinic9923842017-12-13 14:16:30 +010010862 /* Nothing to do if the MSR bitmap is not in use. */
10863 if (!cpu_has_vmx_msr_bitmap() ||
10864 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10865 return false;
10866
Ashok Raj15d45072018-02-01 22:59:43 +010010867 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010868 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010869 return false;
10870
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010871 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10872 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010873 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010874
Radim Krčmářd048c092016-08-08 20:16:22 +020010875 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010876 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10877 /*
10878 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10879 * just lets the processor take the value from the virtual-APIC page;
10880 * take those 256 bits directly from the L1 bitmap.
10881 */
10882 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10883 unsigned word = msr / BITS_PER_LONG;
10884 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10885 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010886 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010887 } else {
10888 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10889 unsigned word = msr / BITS_PER_LONG;
10890 msr_bitmap_l0[word] = ~0;
10891 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10892 }
10893 }
10894
10895 nested_vmx_disable_intercept_for_msr(
10896 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010897 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010898 MSR_TYPE_W);
10899
10900 if (nested_cpu_has_vid(vmcs12)) {
10901 nested_vmx_disable_intercept_for_msr(
10902 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010903 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010904 MSR_TYPE_W);
10905 nested_vmx_disable_intercept_for_msr(
10906 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010907 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010908 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010909 }
Ashok Raj15d45072018-02-01 22:59:43 +010010910
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010911 if (spec_ctrl)
10912 nested_vmx_disable_intercept_for_msr(
10913 msr_bitmap_l1, msr_bitmap_l0,
10914 MSR_IA32_SPEC_CTRL,
10915 MSR_TYPE_R | MSR_TYPE_W);
10916
Ashok Raj15d45072018-02-01 22:59:43 +010010917 if (pred_cmd)
10918 nested_vmx_disable_intercept_for_msr(
10919 msr_bitmap_l1, msr_bitmap_l0,
10920 MSR_IA32_PRED_CMD,
10921 MSR_TYPE_W);
10922
Wincy Vanf2b93282015-02-03 23:56:03 +080010923 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010924 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010925
10926 return true;
10927}
10928
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010929static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10930 struct vmcs12 *vmcs12)
10931{
10932 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10933 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10934 return -EINVAL;
10935 else
10936 return 0;
10937}
10938
Wincy Vanf2b93282015-02-03 23:56:03 +080010939static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10940 struct vmcs12 *vmcs12)
10941{
Wincy Van82f0dd42015-02-03 23:57:18 +080010942 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010943 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010944 !nested_cpu_has_vid(vmcs12) &&
10945 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010946 return 0;
10947
10948 /*
10949 * If virtualize x2apic mode is enabled,
10950 * virtualize apic access must be disabled.
10951 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010952 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10953 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010954 return -EINVAL;
10955
Wincy Van608406e2015-02-03 23:57:51 +080010956 /*
10957 * If virtual interrupt delivery is enabled,
10958 * we must exit on external interrupts.
10959 */
10960 if (nested_cpu_has_vid(vmcs12) &&
10961 !nested_exit_on_intr(vcpu))
10962 return -EINVAL;
10963
Wincy Van705699a2015-02-03 23:58:17 +080010964 /*
10965 * bits 15:8 should be zero in posted_intr_nv,
10966 * the descriptor address has been already checked
10967 * in nested_get_vmcs12_pages.
10968 */
10969 if (nested_cpu_has_posted_intr(vmcs12) &&
10970 (!nested_cpu_has_vid(vmcs12) ||
10971 !nested_exit_intr_ack_set(vcpu) ||
10972 vmcs12->posted_intr_nv & 0xff00))
10973 return -EINVAL;
10974
Wincy Vanf2b93282015-02-03 23:56:03 +080010975 /* tpr shadow is needed by all apicv features. */
10976 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10977 return -EINVAL;
10978
10979 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010980}
10981
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010982static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10983 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010984 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010985{
Liran Alone2536742018-06-23 02:35:02 +030010986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010987 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010988 u64 count, addr;
10989
Liran Alone2536742018-06-23 02:35:02 +030010990 if (vmcs12_read_any(vmcs12, count_field, &count) ||
10991 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010992 WARN_ON(1);
10993 return -EINVAL;
10994 }
10995 if (count == 0)
10996 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010997 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010998 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10999 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011000 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011001 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11002 addr_field, maxphyaddr, count, addr);
11003 return -EINVAL;
11004 }
11005 return 0;
11006}
11007
11008static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11009 struct vmcs12 *vmcs12)
11010{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011011 if (vmcs12->vm_exit_msr_load_count == 0 &&
11012 vmcs12->vm_exit_msr_store_count == 0 &&
11013 vmcs12->vm_entry_msr_load_count == 0)
11014 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011015 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011016 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011017 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011018 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011019 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011020 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011021 return -EINVAL;
11022 return 0;
11023}
11024
Bandan Dasc5f983f2017-05-05 15:25:14 -040011025static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11026 struct vmcs12 *vmcs12)
11027{
11028 u64 address = vmcs12->pml_address;
11029 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11030
11031 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11032 if (!nested_cpu_has_ept(vmcs12) ||
11033 !IS_ALIGNED(address, 4096) ||
11034 address >> maxphyaddr)
11035 return -EINVAL;
11036 }
11037
11038 return 0;
11039}
11040
Liran Alona8a7c022018-06-23 02:35:06 +030011041static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11042 struct vmcs12 *vmcs12)
11043{
11044 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11045 return 0;
11046
11047 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11048 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11049 return -EINVAL;
11050
11051 return 0;
11052}
11053
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011054static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11055 struct vmx_msr_entry *e)
11056{
11057 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011058 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011059 return -EINVAL;
11060 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11061 e->index == MSR_IA32_UCODE_REV)
11062 return -EINVAL;
11063 if (e->reserved != 0)
11064 return -EINVAL;
11065 return 0;
11066}
11067
11068static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11069 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011070{
11071 if (e->index == MSR_FS_BASE ||
11072 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011073 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11074 nested_vmx_msr_check_common(vcpu, e))
11075 return -EINVAL;
11076 return 0;
11077}
11078
11079static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11080 struct vmx_msr_entry *e)
11081{
11082 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11083 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011084 return -EINVAL;
11085 return 0;
11086}
11087
11088/*
11089 * Load guest's/host's msr at nested entry/exit.
11090 * return 0 for success, entry index for failure.
11091 */
11092static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11093{
11094 u32 i;
11095 struct vmx_msr_entry e;
11096 struct msr_data msr;
11097
11098 msr.host_initiated = false;
11099 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011100 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11101 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011102 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011103 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11104 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011105 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011106 }
11107 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011108 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011109 "%s check failed (%u, 0x%x, 0x%x)\n",
11110 __func__, i, e.index, e.reserved);
11111 goto fail;
11112 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011113 msr.index = e.index;
11114 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011115 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011116 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011117 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11118 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011119 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011120 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011121 }
11122 return 0;
11123fail:
11124 return i + 1;
11125}
11126
11127static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11128{
11129 u32 i;
11130 struct vmx_msr_entry e;
11131
11132 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011133 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011134 if (kvm_vcpu_read_guest(vcpu,
11135 gpa + i * sizeof(e),
11136 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011137 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011138 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11139 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011140 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011141 }
11142 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011143 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011144 "%s check failed (%u, 0x%x, 0x%x)\n",
11145 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011146 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011147 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011148 msr_info.host_initiated = false;
11149 msr_info.index = e.index;
11150 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011151 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011152 "%s cannot read MSR (%u, 0x%x)\n",
11153 __func__, i, e.index);
11154 return -EINVAL;
11155 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011156 if (kvm_vcpu_write_guest(vcpu,
11157 gpa + i * sizeof(e) +
11158 offsetof(struct vmx_msr_entry, value),
11159 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011160 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011161 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011162 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011163 return -EINVAL;
11164 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011165 }
11166 return 0;
11167}
11168
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011169static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11170{
11171 unsigned long invalid_mask;
11172
11173 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11174 return (val & invalid_mask) == 0;
11175}
11176
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011177/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011178 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11179 * emulating VM entry into a guest with EPT enabled.
11180 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11181 * is assigned to entry_failure_code on failure.
11182 */
11183static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011184 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011185{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011186 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011187 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011188 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11189 return 1;
11190 }
11191
11192 /*
11193 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11194 * must not be dereferenced.
11195 */
11196 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11197 !nested_ept) {
11198 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11199 *entry_failure_code = ENTRY_FAIL_PDPTE;
11200 return 1;
11201 }
11202 }
11203
11204 vcpu->arch.cr3 = cr3;
11205 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11206 }
11207
11208 kvm_mmu_reset_context(vcpu);
11209 return 0;
11210}
11211
Jim Mattson6514dc32018-04-26 16:09:12 -070011212static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011213{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011214 struct vcpu_vmx *vmx = to_vmx(vcpu);
11215
11216 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11217 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11218 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11219 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11220 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11221 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11222 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11223 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11224 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11225 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11226 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11227 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11228 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11229 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11230 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11231 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11232 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11233 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11234 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11235 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11236 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11237 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11238 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11239 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11240 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11241 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11242 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11243 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11244 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11245 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11246 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011247
11248 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11249 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11250 vmcs12->guest_pending_dbg_exceptions);
11251 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11252 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11253
11254 if (nested_cpu_has_xsaves(vmcs12))
11255 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11256 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11257
11258 if (cpu_has_vmx_posted_intr())
11259 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11260
11261 /*
11262 * Whether page-faults are trapped is determined by a combination of
11263 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11264 * If enable_ept, L0 doesn't care about page faults and we should
11265 * set all of these to L1's desires. However, if !enable_ept, L0 does
11266 * care about (at least some) page faults, and because it is not easy
11267 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11268 * to exit on each and every L2 page fault. This is done by setting
11269 * MASK=MATCH=0 and (see below) EB.PF=1.
11270 * Note that below we don't need special code to set EB.PF beyond the
11271 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11272 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11273 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11274 */
11275 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11276 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11277 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11278 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11279
11280 /* All VMFUNCs are currently emulated through L0 vmexits. */
11281 if (cpu_has_vmx_vmfunc())
11282 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11283
11284 if (cpu_has_vmx_apicv()) {
11285 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11286 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11287 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11288 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11289 }
11290
11291 /*
11292 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11293 * Some constant fields are set here by vmx_set_constant_host_state().
11294 * Other fields are different per CPU, and will be set later when
11295 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11296 */
11297 vmx_set_constant_host_state(vmx);
11298
11299 /*
11300 * Set the MSR load/store lists to match L0's settings.
11301 */
11302 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11303 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11304 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11305 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11306 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11307
11308 set_cr4_guest_host_mask(vmx);
11309
11310 if (vmx_mpx_supported())
11311 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11312
11313 if (enable_vpid) {
11314 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11315 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11316 else
11317 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11318 }
11319
11320 /*
11321 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11322 */
11323 if (enable_ept) {
11324 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11325 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11326 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11327 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11328 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011329
11330 if (cpu_has_vmx_msr_bitmap())
11331 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011332}
11333
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011334/*
11335 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11336 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011337 * 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 +030011338 * guest in a way that will both be appropriate to L1's requests, and our
11339 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11340 * function also has additional necessary side-effects, like setting various
11341 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011342 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11343 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011344 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011345static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011346 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011347{
11348 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011349 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011350
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011351 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011352 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011353 vmx->nested.dirty_vmcs12 = false;
11354 }
11355
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011356 /*
11357 * First, the fields that are shadowed. This must be kept in sync
11358 * with vmx_shadow_fields.h.
11359 */
11360
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011361 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011362 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011363 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011364 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11365 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011366
11367 /*
11368 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11369 * HOST_FS_BASE, HOST_GS_BASE.
11370 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011371
Jim Mattson6514dc32018-04-26 16:09:12 -070011372 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011373 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011374 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11375 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11376 } else {
11377 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11378 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11379 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011380 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011381 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11382 vmcs12->vm_entry_intr_info_field);
11383 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11384 vmcs12->vm_entry_exception_error_code);
11385 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11386 vmcs12->vm_entry_instruction_len);
11387 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11388 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011389 vmx->loaded_vmcs->nmi_known_unmasked =
11390 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011391 } else {
11392 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11393 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011394 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011395
Jan Kiszkaf4124502014-03-07 20:03:13 +010011396 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011397
Paolo Bonzini93140062016-07-06 13:23:51 +020011398 /* Preemption timer setting is only taken from vmcs01. */
11399 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11400 exec_control |= vmcs_config.pin_based_exec_ctrl;
11401 if (vmx->hv_deadline_tsc == -1)
11402 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11403
11404 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011405 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011406 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11407 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011408 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011409 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011410 }
Wincy Van705699a2015-02-03 23:58:17 +080011411
Jan Kiszkaf4124502014-03-07 20:03:13 +010011412 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011413
Jan Kiszkaf4124502014-03-07 20:03:13 +010011414 vmx->nested.preemption_timer_expired = false;
11415 if (nested_cpu_has_preemption_timer(vmcs12))
11416 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011417
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011418 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011419 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011420
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011421 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011422 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011423 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011424 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011425 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011426 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011427 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11428 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011429 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011430 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11431 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11432 ~SECONDARY_EXEC_ENABLE_PML;
11433 exec_control |= vmcs12_exec_ctrl;
11434 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011435
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011436 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011437 vmcs_write16(GUEST_INTR_STATUS,
11438 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011439
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011440 /*
11441 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11442 * nested_get_vmcs12_pages will either fix it up or
11443 * remove the VM execution control.
11444 */
11445 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11446 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11447
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011448 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11449 }
11450
Jim Mattson83bafef2016-10-04 10:48:38 -070011451 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011452 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11453 * entry, but only if the current (host) sp changed from the value
11454 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11455 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11456 * here we just force the write to happen on entry.
11457 */
11458 vmx->host_rsp = 0;
11459
11460 exec_control = vmx_exec_control(vmx); /* L0's desires */
11461 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11462 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11463 exec_control &= ~CPU_BASED_TPR_SHADOW;
11464 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011465
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011466 /*
11467 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11468 * nested_get_vmcs12_pages can't fix it up, the illegal value
11469 * will result in a VM entry failure.
11470 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011471 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011472 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011473 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011474 } else {
11475#ifdef CONFIG_X86_64
11476 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11477 CPU_BASED_CR8_STORE_EXITING;
11478#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011479 }
11480
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011481 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011482 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11483 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011484 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011485 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11486 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11487
11488 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11489
11490 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11491 * bitwise-or of what L1 wants to trap for L2, and what we want to
11492 * trap. Note that CR0.TS also needs updating - we do this later.
11493 */
11494 update_exception_bitmap(vcpu);
11495 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11496 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11497
Nadav Har'El8049d652013-08-05 11:07:06 +030011498 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11499 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11500 * bits are further modified by vmx_set_efer() below.
11501 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011502 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011503
11504 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11505 * emulated by vmx_set_efer(), below.
11506 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011507 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011508 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11509 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011510 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11511
Jim Mattson6514dc32018-04-26 16:09:12 -070011512 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011513 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011514 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011515 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011516 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011517 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011518 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011519
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011520 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11521
Peter Feinerc95ba922016-08-17 09:36:47 -070011522 if (kvm_has_tsc_control)
11523 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011524
11525 if (enable_vpid) {
11526 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011527 * There is no direct mapping between vpid02 and vpid12, the
11528 * vpid02 is per-vCPU for L0 and reused while the value of
11529 * vpid12 is changed w/ one invvpid during nested vmentry.
11530 * The vpid12 is allocated by L1 for L2, so it will not
11531 * influence global bitmap(for vpid01 and vpid02 allocation)
11532 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011533 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011534 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011535 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11536 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011537 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011538 }
11539 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011540 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011541 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011542 }
11543
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011544 if (enable_pml) {
11545 /*
11546 * Conceptually we want to copy the PML address and index from
11547 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11548 * since we always flush the log on each vmexit, this happens
11549 * to be equivalent to simply resetting the fields in vmcs02.
11550 */
11551 ASSERT(vmx->pml_pg);
11552 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11553 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11554 }
11555
Nadav Har'El155a97a2013-08-05 11:07:16 +030011556 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011557 if (nested_ept_init_mmu_context(vcpu)) {
11558 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11559 return 1;
11560 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011561 } else if (nested_cpu_has2(vmcs12,
11562 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011563 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011564 }
11565
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011566 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011567 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11568 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011569 * The CR0_READ_SHADOW is what L2 should have expected to read given
11570 * the specifications by L1; It's not enough to take
11571 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11572 * have more bits than L1 expected.
11573 */
11574 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11575 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11576
11577 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11578 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11579
Jim Mattson6514dc32018-04-26 16:09:12 -070011580 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011581 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011582 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11583 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11584 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11585 else
11586 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11587 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11588 vmx_set_efer(vcpu, vcpu->arch.efer);
11589
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011590 /*
11591 * Guest state is invalid and unrestricted guest is disabled,
11592 * which means L1 attempted VMEntry to L2 with invalid state.
11593 * Fail the VMEntry.
11594 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011595 if (vmx->emulation_required) {
11596 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011597 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011598 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011599
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011600 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011601 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011602 entry_failure_code))
11603 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011604
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011605 if (!enable_ept)
11606 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11607
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011608 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11609 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011610 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011611}
11612
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011613static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11614{
11615 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11616 nested_cpu_has_virtual_nmis(vmcs12))
11617 return -EINVAL;
11618
11619 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11620 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11621 return -EINVAL;
11622
11623 return 0;
11624}
11625
Jim Mattsonca0bde22016-11-30 12:03:46 -080011626static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11627{
11628 struct vcpu_vmx *vmx = to_vmx(vcpu);
11629
11630 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11631 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11632 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11633
Jim Mattson56a20512017-07-06 16:33:06 -070011634 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11635 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11636
Jim Mattsonca0bde22016-11-30 12:03:46 -080011637 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11638 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11639
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011640 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11641 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11642
Jim Mattson712b12d2017-08-24 13:24:47 -070011643 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11644 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11645
Jim Mattsonca0bde22016-11-30 12:03:46 -080011646 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11647 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11648
11649 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11650 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11651
Bandan Dasc5f983f2017-05-05 15:25:14 -040011652 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11653 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11654
Liran Alona8a7c022018-06-23 02:35:06 +030011655 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11656 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11657
Jim Mattsonca0bde22016-11-30 12:03:46 -080011658 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011659 vmx->nested.msrs.procbased_ctls_low,
11660 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011661 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11662 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011663 vmx->nested.msrs.secondary_ctls_low,
11664 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011665 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011666 vmx->nested.msrs.pinbased_ctls_low,
11667 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011668 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011669 vmx->nested.msrs.exit_ctls_low,
11670 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011671 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011672 vmx->nested.msrs.entry_ctls_low,
11673 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011674 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11675
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011676 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011677 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11678
Bandan Das41ab9372017-08-03 15:54:43 -040011679 if (nested_cpu_has_vmfunc(vmcs12)) {
11680 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011681 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011682 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11683
11684 if (nested_cpu_has_eptp_switching(vmcs12)) {
11685 if (!nested_cpu_has_ept(vmcs12) ||
11686 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11687 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11688 }
11689 }
Bandan Das27c42a12017-08-03 15:54:42 -040011690
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011691 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11692 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11693
Jim Mattsonca0bde22016-11-30 12:03:46 -080011694 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11695 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11696 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11697 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11698
Marc Orr04473782018-06-20 17:21:29 -070011699 /*
11700 * From the Intel SDM, volume 3:
11701 * Fields relevant to VM-entry event injection must be set properly.
11702 * These fields are the VM-entry interruption-information field, the
11703 * VM-entry exception error code, and the VM-entry instruction length.
11704 */
11705 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11706 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11707 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11708 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11709 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11710 bool should_have_error_code;
11711 bool urg = nested_cpu_has2(vmcs12,
11712 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11713 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11714
11715 /* VM-entry interruption-info field: interruption type */
11716 if (intr_type == INTR_TYPE_RESERVED ||
11717 (intr_type == INTR_TYPE_OTHER_EVENT &&
11718 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11719 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11720
11721 /* VM-entry interruption-info field: vector */
11722 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11723 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11724 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11725 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11726
11727 /* VM-entry interruption-info field: deliver error code */
11728 should_have_error_code =
11729 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11730 x86_exception_has_error_code(vector);
11731 if (has_error_code != should_have_error_code)
11732 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11733
11734 /* VM-entry exception error code */
11735 if (has_error_code &&
11736 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11737 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11738
11739 /* VM-entry interruption-info field: reserved bits */
11740 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11741 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11742
11743 /* VM-entry instruction length */
11744 switch (intr_type) {
11745 case INTR_TYPE_SOFT_EXCEPTION:
11746 case INTR_TYPE_SOFT_INTR:
11747 case INTR_TYPE_PRIV_SW_EXCEPTION:
11748 if ((vmcs12->vm_entry_instruction_len > 15) ||
11749 (vmcs12->vm_entry_instruction_len == 0 &&
11750 !nested_cpu_has_zero_length_injection(vcpu)))
11751 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11752 }
11753 }
11754
Jim Mattsonca0bde22016-11-30 12:03:46 -080011755 return 0;
11756}
11757
Liran Alonf145d902018-06-23 02:35:07 +030011758static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
11759 struct vmcs12 *vmcs12)
11760{
11761 int r;
11762 struct page *page;
11763 struct vmcs12 *shadow;
11764
11765 if (vmcs12->vmcs_link_pointer == -1ull)
11766 return 0;
11767
11768 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
11769 return -EINVAL;
11770
11771 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11772 if (is_error_page(page))
11773 return -EINVAL;
11774
11775 r = 0;
11776 shadow = kmap(page);
11777 if (shadow->hdr.revision_id != VMCS12_REVISION ||
11778 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
11779 r = -EINVAL;
11780 kunmap(page);
11781 kvm_release_page_clean(page);
11782 return r;
11783}
11784
Jim Mattsonca0bde22016-11-30 12:03:46 -080011785static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11786 u32 *exit_qual)
11787{
11788 bool ia32e;
11789
11790 *exit_qual = ENTRY_FAIL_DEFAULT;
11791
11792 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11793 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11794 return 1;
11795
Liran Alonf145d902018-06-23 02:35:07 +030011796 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080011797 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11798 return 1;
11799 }
11800
11801 /*
11802 * If the load IA32_EFER VM-entry control is 1, the following checks
11803 * are performed on the field for the IA32_EFER MSR:
11804 * - Bits reserved in the IA32_EFER MSR must be 0.
11805 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11806 * the IA-32e mode guest VM-exit control. It must also be identical
11807 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11808 * CR0.PG) is 1.
11809 */
11810 if (to_vmx(vcpu)->nested.nested_run_pending &&
11811 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11812 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11813 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11814 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11815 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11816 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11817 return 1;
11818 }
11819
11820 /*
11821 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11822 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11823 * the values of the LMA and LME bits in the field must each be that of
11824 * the host address-space size VM-exit control.
11825 */
11826 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11827 ia32e = (vmcs12->vm_exit_controls &
11828 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11829 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11830 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11831 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11832 return 1;
11833 }
11834
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011835 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11836 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11837 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11838 return 1;
11839
Jim Mattsonca0bde22016-11-30 12:03:46 -080011840 return 0;
11841}
11842
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011843/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011844 * If exit_qual is NULL, this is being called from state restore (either RSM
11845 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011846 */
11847static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080011848{
11849 struct vcpu_vmx *vmx = to_vmx(vcpu);
11850 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011851 bool from_vmentry = !!exit_qual;
11852 u32 dummy_exit_qual;
11853 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011854
Jim Mattson858e25c2016-11-30 12:03:47 -080011855 enter_guest_mode(vcpu);
11856
11857 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11858 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11859
Jim Mattsonde3a0022017-11-27 17:22:25 -060011860 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011861 vmx_segment_cache_clear(vmx);
11862
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011863 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11864 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11865
11866 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011867 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011868 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011869
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011870 if (from_vmentry) {
11871 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011872
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011873 r = EXIT_REASON_MSR_LOAD_FAIL;
11874 *exit_qual = nested_vmx_load_msr(vcpu,
11875 vmcs12->vm_entry_msr_load_addr,
11876 vmcs12->vm_entry_msr_load_count);
11877 if (*exit_qual)
11878 goto fail;
11879 } else {
11880 /*
11881 * The MMU is not initialized to point at the right entities yet and
11882 * "get pages" would need to read data from the guest (i.e. we will
11883 * need to perform gpa to hpa translation). Request a call
11884 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11885 * have already been set at vmentry time and should not be reset.
11886 */
11887 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11888 }
Jim Mattson858e25c2016-11-30 12:03:47 -080011889
Jim Mattson858e25c2016-11-30 12:03:47 -080011890 /*
11891 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11892 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11893 * returned as far as L1 is concerned. It will only return (and set
11894 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11895 */
11896 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011897
11898fail:
11899 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11900 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11901 leave_guest_mode(vcpu);
11902 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011903 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011904}
11905
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011906/*
11907 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11908 * for running an L2 nested guest.
11909 */
11910static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11911{
11912 struct vmcs12 *vmcs12;
11913 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011914 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011915 u32 exit_qual;
11916 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011917
Kyle Hueyeb277562016-11-29 12:40:39 -080011918 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011919 return 1;
11920
Kyle Hueyeb277562016-11-29 12:40:39 -080011921 if (!nested_vmx_check_vmcs12(vcpu))
11922 goto out;
11923
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011924 vmcs12 = get_vmcs12(vcpu);
11925
Liran Alona6192d42018-06-23 02:35:04 +030011926 /*
11927 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
11928 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
11929 * rather than RFLAGS.ZF, and no error number is stored to the
11930 * VM-instruction error field.
11931 */
11932 if (vmcs12->hdr.shadow_vmcs) {
11933 nested_vmx_failInvalid(vcpu);
11934 goto out;
11935 }
11936
Abel Gordon012f83c2013-04-18 14:39:25 +030011937 if (enable_shadow_vmcs)
11938 copy_shadow_to_vmcs12(vmx);
11939
Nadav Har'El7c177932011-05-25 23:12:04 +030011940 /*
11941 * The nested entry process starts with enforcing various prerequisites
11942 * on vmcs12 as required by the Intel SDM, and act appropriately when
11943 * they fail: As the SDM explains, some conditions should cause the
11944 * instruction to fail, while others will cause the instruction to seem
11945 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11946 * To speed up the normal (success) code path, we should avoid checking
11947 * for misconfigurations which will anyway be caught by the processor
11948 * when using the merged vmcs02.
11949 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011950 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11951 nested_vmx_failValid(vcpu,
11952 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11953 goto out;
11954 }
11955
Nadav Har'El7c177932011-05-25 23:12:04 +030011956 if (vmcs12->launch_state == launch) {
11957 nested_vmx_failValid(vcpu,
11958 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11959 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011960 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011961 }
11962
Jim Mattsonca0bde22016-11-30 12:03:46 -080011963 ret = check_vmentry_prereqs(vcpu, vmcs12);
11964 if (ret) {
11965 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011966 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011967 }
11968
Nadav Har'El7c177932011-05-25 23:12:04 +030011969 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011970 * After this point, the trap flag no longer triggers a singlestep trap
11971 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11972 * This is not 100% correct; for performance reasons, we delegate most
11973 * of the checks on host state to the processor. If those fail,
11974 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011975 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011976 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011977
Jim Mattsonca0bde22016-11-30 12:03:46 -080011978 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11979 if (ret) {
11980 nested_vmx_entry_failure(vcpu, vmcs12,
11981 EXIT_REASON_INVALID_STATE, exit_qual);
11982 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011983 }
11984
11985 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011986 * We're finally done with prerequisite checking, and can start with
11987 * the nested entry.
11988 */
11989
Jim Mattson6514dc32018-04-26 16:09:12 -070011990 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011991 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070011992 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011993 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070011994 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011995 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070011996 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011997
Chao Gao135a06c2018-02-11 10:06:30 +080011998 /*
11999 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12000 * by event injection, halt vcpu.
12001 */
12002 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012003 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12004 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012005 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012006 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012007 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012008
12009out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012010 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012011}
12012
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012013/*
12014 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12015 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12016 * This function returns the new value we should put in vmcs12.guest_cr0.
12017 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12018 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12019 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12020 * didn't trap the bit, because if L1 did, so would L0).
12021 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12022 * been modified by L2, and L1 knows it. So just leave the old value of
12023 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12024 * isn't relevant, because if L0 traps this bit it can set it to anything.
12025 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12026 * changed these bits, and therefore they need to be updated, but L0
12027 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12028 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12029 */
12030static inline unsigned long
12031vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12032{
12033 return
12034 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12035 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12036 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12037 vcpu->arch.cr0_guest_owned_bits));
12038}
12039
12040static inline unsigned long
12041vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12042{
12043 return
12044 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12045 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12046 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12047 vcpu->arch.cr4_guest_owned_bits));
12048}
12049
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012050static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12051 struct vmcs12 *vmcs12)
12052{
12053 u32 idt_vectoring;
12054 unsigned int nr;
12055
Wanpeng Li664f8e22017-08-24 03:35:09 -070012056 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012057 nr = vcpu->arch.exception.nr;
12058 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12059
12060 if (kvm_exception_is_soft(nr)) {
12061 vmcs12->vm_exit_instruction_len =
12062 vcpu->arch.event_exit_inst_len;
12063 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12064 } else
12065 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12066
12067 if (vcpu->arch.exception.has_error_code) {
12068 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12069 vmcs12->idt_vectoring_error_code =
12070 vcpu->arch.exception.error_code;
12071 }
12072
12073 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012074 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012075 vmcs12->idt_vectoring_info_field =
12076 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012077 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012078 nr = vcpu->arch.interrupt.nr;
12079 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12080
12081 if (vcpu->arch.interrupt.soft) {
12082 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12083 vmcs12->vm_entry_instruction_len =
12084 vcpu->arch.event_exit_inst_len;
12085 } else
12086 idt_vectoring |= INTR_TYPE_EXT_INTR;
12087
12088 vmcs12->idt_vectoring_info_field = idt_vectoring;
12089 }
12090}
12091
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012092static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12093{
12094 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012095 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012096 bool block_nested_events =
12097 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012098
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012099 if (vcpu->arch.exception.pending &&
12100 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012101 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012102 return -EBUSY;
12103 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012104 return 0;
12105 }
12106
Jan Kiszkaf4124502014-03-07 20:03:13 +010012107 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12108 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012109 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012110 return -EBUSY;
12111 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12112 return 0;
12113 }
12114
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012115 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012116 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012117 return -EBUSY;
12118 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12119 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12120 INTR_INFO_VALID_MASK, 0);
12121 /*
12122 * The NMI-triggered VM exit counts as injection:
12123 * clear this one and block further NMIs.
12124 */
12125 vcpu->arch.nmi_pending = 0;
12126 vmx_set_nmi_mask(vcpu, true);
12127 return 0;
12128 }
12129
12130 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12131 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012132 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012133 return -EBUSY;
12134 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012135 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012136 }
12137
David Hildenbrand6342c502017-01-25 11:58:58 +010012138 vmx_complete_nested_posted_interrupt(vcpu);
12139 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012140}
12141
Jan Kiszkaf4124502014-03-07 20:03:13 +010012142static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12143{
12144 ktime_t remaining =
12145 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12146 u64 value;
12147
12148 if (ktime_to_ns(remaining) <= 0)
12149 return 0;
12150
12151 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12152 do_div(value, 1000000);
12153 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12154}
12155
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012156/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012157 * Update the guest state fields of vmcs12 to reflect changes that
12158 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12159 * VM-entry controls is also updated, since this is really a guest
12160 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012161 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012162static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012163{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012164 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12165 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12166
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012167 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12168 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12169 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12170
12171 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12172 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12173 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12174 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12175 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12176 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12177 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12178 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12179 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12180 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12181 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12182 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12183 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12184 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12185 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12186 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12187 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12188 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12189 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12190 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12191 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12192 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12193 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12194 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12195 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12196 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12197 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12198 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12199 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12200 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12201 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12202 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12203 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12204 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12205 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12206 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12207
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012208 vmcs12->guest_interruptibility_info =
12209 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12210 vmcs12->guest_pending_dbg_exceptions =
12211 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012212 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12213 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12214 else
12215 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012216
Jan Kiszkaf4124502014-03-07 20:03:13 +010012217 if (nested_cpu_has_preemption_timer(vmcs12)) {
12218 if (vmcs12->vm_exit_controls &
12219 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12220 vmcs12->vmx_preemption_timer_value =
12221 vmx_get_preemption_timer_value(vcpu);
12222 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12223 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012224
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012225 /*
12226 * In some cases (usually, nested EPT), L2 is allowed to change its
12227 * own CR3 without exiting. If it has changed it, we must keep it.
12228 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12229 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12230 *
12231 * Additionally, restore L2's PDPTR to vmcs12.
12232 */
12233 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012234 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012235 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12236 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12237 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12238 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12239 }
12240
Jim Mattsond281e132017-06-01 12:44:46 -070012241 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012242
Wincy Van608406e2015-02-03 23:57:51 +080012243 if (nested_cpu_has_vid(vmcs12))
12244 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12245
Jan Kiszkac18911a2013-03-13 16:06:41 +010012246 vmcs12->vm_entry_controls =
12247 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012248 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012249
Jan Kiszka2996fca2014-06-16 13:59:43 +020012250 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12251 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12252 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12253 }
12254
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012255 /* TODO: These cannot have changed unless we have MSR bitmaps and
12256 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012257 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012258 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012259 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12260 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012261 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12262 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12263 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012264 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012265 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012266}
12267
12268/*
12269 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12270 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12271 * and this function updates it to reflect the changes to the guest state while
12272 * L2 was running (and perhaps made some exits which were handled directly by L0
12273 * without going back to L1), and to reflect the exit reason.
12274 * Note that we do not have to copy here all VMCS fields, just those that
12275 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12276 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12277 * which already writes to vmcs12 directly.
12278 */
12279static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12280 u32 exit_reason, u32 exit_intr_info,
12281 unsigned long exit_qualification)
12282{
12283 /* update guest state fields: */
12284 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012285
12286 /* update exit information fields: */
12287
Jan Kiszka533558b2014-01-04 18:47:20 +010012288 vmcs12->vm_exit_reason = exit_reason;
12289 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012290 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012291
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012292 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012293 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12294 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12295
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012296 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012297 vmcs12->launch_state = 1;
12298
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012299 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12300 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012301 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012302
12303 /*
12304 * Transfer the event that L0 or L1 may wanted to inject into
12305 * L2 to IDT_VECTORING_INFO_FIELD.
12306 */
12307 vmcs12_save_pending_event(vcpu, vmcs12);
12308 }
12309
12310 /*
12311 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12312 * preserved above and would only end up incorrectly in L1.
12313 */
12314 vcpu->arch.nmi_injected = false;
12315 kvm_clear_exception_queue(vcpu);
12316 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012317}
12318
Wanpeng Li5af41572017-11-05 16:54:49 -080012319static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12320 struct vmcs12 *vmcs12)
12321{
12322 u32 entry_failure_code;
12323
12324 nested_ept_uninit_mmu_context(vcpu);
12325
12326 /*
12327 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12328 * couldn't have changed.
12329 */
12330 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12331 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12332
12333 if (!enable_ept)
12334 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12335}
12336
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012337/*
12338 * A part of what we need to when the nested L2 guest exits and we want to
12339 * run its L1 parent, is to reset L1's guest state to the host state specified
12340 * in vmcs12.
12341 * This function is to be called not only on normal nested exit, but also on
12342 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12343 * Failures During or After Loading Guest State").
12344 * This function should be called when the active VMCS is L1's (vmcs01).
12345 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012346static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12347 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012348{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012349 struct kvm_segment seg;
12350
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012351 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12352 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012353 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012354 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12355 else
12356 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12357 vmx_set_efer(vcpu, vcpu->arch.efer);
12358
12359 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12360 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012361 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012362 /*
12363 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012364 * actually changed, because vmx_set_cr0 refers to efer set above.
12365 *
12366 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12367 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012368 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012369 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012370 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012371
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012372 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012373 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012374 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012375
Wanpeng Li5af41572017-11-05 16:54:49 -080012376 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012377
Liran Alon6f1e03b2018-05-22 17:16:14 +030012378 /*
12379 * If vmcs01 don't use VPID, CPU flushes TLB on every
12380 * VMEntry/VMExit. Thus, no need to flush TLB.
12381 *
12382 * If vmcs12 uses VPID, TLB entries populated by L2 are
12383 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12384 * with vmx->vpid. Thus, no need to flush TLB.
12385 *
12386 * Therefore, flush TLB only in case vmcs01 uses VPID and
12387 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12388 * are both tagged with vmx->vpid.
12389 */
12390 if (enable_vpid &&
12391 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012392 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012393 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012394
12395 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12396 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12397 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12398 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12399 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012400 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12401 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012402
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012403 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12404 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12405 vmcs_write64(GUEST_BNDCFGS, 0);
12406
Jan Kiszka44811c02013-08-04 17:17:27 +020012407 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012408 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012409 vcpu->arch.pat = vmcs12->host_ia32_pat;
12410 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012411 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12412 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12413 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012414
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012415 /* Set L1 segment info according to Intel SDM
12416 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12417 seg = (struct kvm_segment) {
12418 .base = 0,
12419 .limit = 0xFFFFFFFF,
12420 .selector = vmcs12->host_cs_selector,
12421 .type = 11,
12422 .present = 1,
12423 .s = 1,
12424 .g = 1
12425 };
12426 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12427 seg.l = 1;
12428 else
12429 seg.db = 1;
12430 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12431 seg = (struct kvm_segment) {
12432 .base = 0,
12433 .limit = 0xFFFFFFFF,
12434 .type = 3,
12435 .present = 1,
12436 .s = 1,
12437 .db = 1,
12438 .g = 1
12439 };
12440 seg.selector = vmcs12->host_ds_selector;
12441 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12442 seg.selector = vmcs12->host_es_selector;
12443 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12444 seg.selector = vmcs12->host_ss_selector;
12445 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12446 seg.selector = vmcs12->host_fs_selector;
12447 seg.base = vmcs12->host_fs_base;
12448 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12449 seg.selector = vmcs12->host_gs_selector;
12450 seg.base = vmcs12->host_gs_base;
12451 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12452 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012453 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012454 .limit = 0x67,
12455 .selector = vmcs12->host_tr_selector,
12456 .type = 11,
12457 .present = 1
12458 };
12459 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12460
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012461 kvm_set_dr(vcpu, 7, 0x400);
12462 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012463
Wincy Van3af18d92015-02-03 23:49:31 +080012464 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012465 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012466
Wincy Vanff651cb2014-12-11 08:52:58 +030012467 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12468 vmcs12->vm_exit_msr_load_count))
12469 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012470}
12471
12472/*
12473 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12474 * and modify vmcs12 to make it see what it would expect to see there if
12475 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12476 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012477static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12478 u32 exit_intr_info,
12479 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012480{
12481 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012482 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12483
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012484 /* trying to cancel vmlaunch/vmresume is a bug */
12485 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12486
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012487 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012488 * The only expected VM-instruction error is "VM entry with
12489 * invalid control field(s)." Anything else indicates a
12490 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012491 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012492 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12493 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12494
12495 leave_guest_mode(vcpu);
12496
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012497 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12498 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12499
Jim Mattson4f350c62017-09-14 16:31:44 -070012500 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012501 if (exit_reason == -1)
12502 sync_vmcs12(vcpu, vmcs12);
12503 else
12504 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12505 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012506
12507 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12508 vmcs12->vm_exit_msr_store_count))
12509 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012510 }
12511
Jim Mattson4f350c62017-09-14 16:31:44 -070012512 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012513 vm_entry_controls_reset_shadow(vmx);
12514 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012515 vmx_segment_cache_clear(vmx);
12516
Paolo Bonzini93140062016-07-06 13:23:51 +020012517 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012518 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12519 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012520 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012521 if (vmx->hv_deadline_tsc == -1)
12522 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12523 PIN_BASED_VMX_PREEMPTION_TIMER);
12524 else
12525 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12526 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012527 if (kvm_has_tsc_control)
12528 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012529
Jim Mattson8d860bb2018-05-09 16:56:05 -040012530 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12531 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12532 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012533 } else if (!nested_cpu_has_ept(vmcs12) &&
12534 nested_cpu_has2(vmcs12,
12535 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012536 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012537 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012538
12539 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12540 vmx->host_rsp = 0;
12541
12542 /* Unpin physical memory we referred to in vmcs02 */
12543 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012544 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012545 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012546 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012547 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012548 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012549 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012550 }
Wincy Van705699a2015-02-03 23:58:17 +080012551 if (vmx->nested.pi_desc_page) {
12552 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012553 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012554 vmx->nested.pi_desc_page = NULL;
12555 vmx->nested.pi_desc = NULL;
12556 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012557
12558 /*
Tang Chen38b99172014-09-24 15:57:54 +080012559 * We are now running in L2, mmu_notifier will force to reload the
12560 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12561 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012562 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012563
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012564 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012565 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012566
12567 /* in case we halted in L2 */
12568 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012569
12570 if (likely(!vmx->fail)) {
12571 /*
12572 * TODO: SDM says that with acknowledge interrupt on
12573 * exit, bit 31 of the VM-exit interrupt information
12574 * (valid interrupt) is always set to 1 on
12575 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12576 * need kvm_cpu_has_interrupt(). See the commit
12577 * message for details.
12578 */
12579 if (nested_exit_intr_ack_set(vcpu) &&
12580 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12581 kvm_cpu_has_interrupt(vcpu)) {
12582 int irq = kvm_cpu_get_interrupt(vcpu);
12583 WARN_ON(irq < 0);
12584 vmcs12->vm_exit_intr_info = irq |
12585 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12586 }
12587
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012588 if (exit_reason != -1)
12589 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12590 vmcs12->exit_qualification,
12591 vmcs12->idt_vectoring_info_field,
12592 vmcs12->vm_exit_intr_info,
12593 vmcs12->vm_exit_intr_error_code,
12594 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012595
12596 load_vmcs12_host_state(vcpu, vmcs12);
12597
12598 return;
12599 }
12600
12601 /*
12602 * After an early L2 VM-entry failure, we're now back
12603 * in L1 which thinks it just finished a VMLAUNCH or
12604 * VMRESUME instruction, so we need to set the failure
12605 * flag and the VM-instruction error field of the VMCS
12606 * accordingly.
12607 */
12608 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012609
12610 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12611
Jim Mattson4f350c62017-09-14 16:31:44 -070012612 /*
12613 * The emulated instruction was already skipped in
12614 * nested_vmx_run, but the updated RIP was never
12615 * written back to the vmcs01.
12616 */
12617 skip_emulated_instruction(vcpu);
12618 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012619}
12620
Nadav Har'El7c177932011-05-25 23:12:04 +030012621/*
Jan Kiszka42124922014-01-04 18:47:19 +010012622 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12623 */
12624static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12625{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012626 if (is_guest_mode(vcpu)) {
12627 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012628 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012629 }
Jan Kiszka42124922014-01-04 18:47:19 +010012630 free_nested(to_vmx(vcpu));
12631}
12632
12633/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012634 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12635 * 23.7 "VM-entry failures during or after loading guest state" (this also
12636 * lists the acceptable exit-reason and exit-qualification parameters).
12637 * It should only be called before L2 actually succeeded to run, and when
12638 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12639 */
12640static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12641 struct vmcs12 *vmcs12,
12642 u32 reason, unsigned long qualification)
12643{
12644 load_vmcs12_host_state(vcpu, vmcs12);
12645 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12646 vmcs12->exit_qualification = qualification;
12647 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012648 if (enable_shadow_vmcs)
12649 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012650}
12651
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012652static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12653 struct x86_instruction_info *info,
12654 enum x86_intercept_stage stage)
12655{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012656 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12657 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12658
12659 /*
12660 * RDPID causes #UD if disabled through secondary execution controls.
12661 * Because it is marked as EmulateOnUD, we need to intercept it here.
12662 */
12663 if (info->intercept == x86_intercept_rdtscp &&
12664 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12665 ctxt->exception.vector = UD_VECTOR;
12666 ctxt->exception.error_code_valid = false;
12667 return X86EMUL_PROPAGATE_FAULT;
12668 }
12669
12670 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012671 return X86EMUL_CONTINUE;
12672}
12673
Yunhong Jiang64672c92016-06-13 14:19:59 -070012674#ifdef CONFIG_X86_64
12675/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12676static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12677 u64 divisor, u64 *result)
12678{
12679 u64 low = a << shift, high = a >> (64 - shift);
12680
12681 /* To avoid the overflow on divq */
12682 if (high >= divisor)
12683 return 1;
12684
12685 /* Low hold the result, high hold rem which is discarded */
12686 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12687 "rm" (divisor), "0" (low), "1" (high));
12688 *result = low;
12689
12690 return 0;
12691}
12692
12693static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12694{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012695 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012696 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012697
12698 if (kvm_mwait_in_guest(vcpu->kvm))
12699 return -EOPNOTSUPP;
12700
12701 vmx = to_vmx(vcpu);
12702 tscl = rdtsc();
12703 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12704 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012705 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12706
12707 if (delta_tsc > lapic_timer_advance_cycles)
12708 delta_tsc -= lapic_timer_advance_cycles;
12709 else
12710 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012711
12712 /* Convert to host delta tsc if tsc scaling is enabled */
12713 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12714 u64_shl_div_u64(delta_tsc,
12715 kvm_tsc_scaling_ratio_frac_bits,
12716 vcpu->arch.tsc_scaling_ratio,
12717 &delta_tsc))
12718 return -ERANGE;
12719
12720 /*
12721 * If the delta tsc can't fit in the 32 bit after the multi shift,
12722 * we can't use the preemption timer.
12723 * It's possible that it fits on later vmentries, but checking
12724 * on every vmentry is costly so we just use an hrtimer.
12725 */
12726 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12727 return -ERANGE;
12728
12729 vmx->hv_deadline_tsc = tscl + delta_tsc;
12730 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12731 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012732
12733 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012734}
12735
12736static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12737{
12738 struct vcpu_vmx *vmx = to_vmx(vcpu);
12739 vmx->hv_deadline_tsc = -1;
12740 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12741 PIN_BASED_VMX_PREEMPTION_TIMER);
12742}
12743#endif
12744
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012745static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012746{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012747 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012748 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012749}
12750
Kai Huang843e4332015-01-28 10:54:28 +080012751static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12752 struct kvm_memory_slot *slot)
12753{
12754 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12755 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12756}
12757
12758static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12759 struct kvm_memory_slot *slot)
12760{
12761 kvm_mmu_slot_set_dirty(kvm, slot);
12762}
12763
12764static void vmx_flush_log_dirty(struct kvm *kvm)
12765{
12766 kvm_flush_pml_buffers(kvm);
12767}
12768
Bandan Dasc5f983f2017-05-05 15:25:14 -040012769static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12770{
12771 struct vmcs12 *vmcs12;
12772 struct vcpu_vmx *vmx = to_vmx(vcpu);
12773 gpa_t gpa;
12774 struct page *page = NULL;
12775 u64 *pml_address;
12776
12777 if (is_guest_mode(vcpu)) {
12778 WARN_ON_ONCE(vmx->nested.pml_full);
12779
12780 /*
12781 * Check if PML is enabled for the nested guest.
12782 * Whether eptp bit 6 is set is already checked
12783 * as part of A/D emulation.
12784 */
12785 vmcs12 = get_vmcs12(vcpu);
12786 if (!nested_cpu_has_pml(vmcs12))
12787 return 0;
12788
Dan Carpenter47698862017-05-10 22:43:17 +030012789 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012790 vmx->nested.pml_full = true;
12791 return 1;
12792 }
12793
12794 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12795
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012796 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12797 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012798 return 0;
12799
12800 pml_address = kmap(page);
12801 pml_address[vmcs12->guest_pml_index--] = gpa;
12802 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012803 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012804 }
12805
12806 return 0;
12807}
12808
Kai Huang843e4332015-01-28 10:54:28 +080012809static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12810 struct kvm_memory_slot *memslot,
12811 gfn_t offset, unsigned long mask)
12812{
12813 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12814}
12815
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012816static void __pi_post_block(struct kvm_vcpu *vcpu)
12817{
12818 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12819 struct pi_desc old, new;
12820 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012821
12822 do {
12823 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012824 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12825 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012826
12827 dest = cpu_physical_id(vcpu->cpu);
12828
12829 if (x2apic_enabled())
12830 new.ndst = dest;
12831 else
12832 new.ndst = (dest << 8) & 0xFF00;
12833
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012834 /* set 'NV' to 'notification vector' */
12835 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012836 } while (cmpxchg64(&pi_desc->control, old.control,
12837 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012838
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012839 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12840 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012841 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012842 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012843 vcpu->pre_pcpu = -1;
12844 }
12845}
12846
Feng Wuefc64402015-09-18 22:29:51 +080012847/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012848 * This routine does the following things for vCPU which is going
12849 * to be blocked if VT-d PI is enabled.
12850 * - Store the vCPU to the wakeup list, so when interrupts happen
12851 * we can find the right vCPU to wake up.
12852 * - Change the Posted-interrupt descriptor as below:
12853 * 'NDST' <-- vcpu->pre_pcpu
12854 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12855 * - If 'ON' is set during this process, which means at least one
12856 * interrupt is posted for this vCPU, we cannot block it, in
12857 * this case, return 1, otherwise, return 0.
12858 *
12859 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012860static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012861{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012862 unsigned int dest;
12863 struct pi_desc old, new;
12864 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12865
12866 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012867 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12868 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012869 return 0;
12870
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012871 WARN_ON(irqs_disabled());
12872 local_irq_disable();
12873 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12874 vcpu->pre_pcpu = vcpu->cpu;
12875 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12876 list_add_tail(&vcpu->blocked_vcpu_list,
12877 &per_cpu(blocked_vcpu_on_cpu,
12878 vcpu->pre_pcpu));
12879 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12880 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012881
12882 do {
12883 old.control = new.control = pi_desc->control;
12884
Feng Wubf9f6ac2015-09-18 22:29:55 +080012885 WARN((pi_desc->sn == 1),
12886 "Warning: SN field of posted-interrupts "
12887 "is set before blocking\n");
12888
12889 /*
12890 * Since vCPU can be preempted during this process,
12891 * vcpu->cpu could be different with pre_pcpu, we
12892 * need to set pre_pcpu as the destination of wakeup
12893 * notification event, then we can find the right vCPU
12894 * to wakeup in wakeup handler if interrupts happen
12895 * when the vCPU is in blocked state.
12896 */
12897 dest = cpu_physical_id(vcpu->pre_pcpu);
12898
12899 if (x2apic_enabled())
12900 new.ndst = dest;
12901 else
12902 new.ndst = (dest << 8) & 0xFF00;
12903
12904 /* set 'NV' to 'wakeup vector' */
12905 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012906 } while (cmpxchg64(&pi_desc->control, old.control,
12907 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012908
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012909 /* We should not block the vCPU if an interrupt is posted for it. */
12910 if (pi_test_on(pi_desc) == 1)
12911 __pi_post_block(vcpu);
12912
12913 local_irq_enable();
12914 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012915}
12916
Yunhong Jiangbc225122016-06-13 14:19:58 -070012917static int vmx_pre_block(struct kvm_vcpu *vcpu)
12918{
12919 if (pi_pre_block(vcpu))
12920 return 1;
12921
Yunhong Jiang64672c92016-06-13 14:19:59 -070012922 if (kvm_lapic_hv_timer_in_use(vcpu))
12923 kvm_lapic_switch_to_sw_timer(vcpu);
12924
Yunhong Jiangbc225122016-06-13 14:19:58 -070012925 return 0;
12926}
12927
12928static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012929{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012930 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012931 return;
12932
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012933 WARN_ON(irqs_disabled());
12934 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012935 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012936 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012937}
12938
Yunhong Jiangbc225122016-06-13 14:19:58 -070012939static void vmx_post_block(struct kvm_vcpu *vcpu)
12940{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012941 if (kvm_x86_ops->set_hv_timer)
12942 kvm_lapic_switch_to_hv_timer(vcpu);
12943
Yunhong Jiangbc225122016-06-13 14:19:58 -070012944 pi_post_block(vcpu);
12945}
12946
Feng Wubf9f6ac2015-09-18 22:29:55 +080012947/*
Feng Wuefc64402015-09-18 22:29:51 +080012948 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12949 *
12950 * @kvm: kvm
12951 * @host_irq: host irq of the interrupt
12952 * @guest_irq: gsi of the interrupt
12953 * @set: set or unset PI
12954 * returns 0 on success, < 0 on failure
12955 */
12956static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12957 uint32_t guest_irq, bool set)
12958{
12959 struct kvm_kernel_irq_routing_entry *e;
12960 struct kvm_irq_routing_table *irq_rt;
12961 struct kvm_lapic_irq irq;
12962 struct kvm_vcpu *vcpu;
12963 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012964 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012965
12966 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012967 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12968 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012969 return 0;
12970
12971 idx = srcu_read_lock(&kvm->irq_srcu);
12972 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012973 if (guest_irq >= irq_rt->nr_rt_entries ||
12974 hlist_empty(&irq_rt->map[guest_irq])) {
12975 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12976 guest_irq, irq_rt->nr_rt_entries);
12977 goto out;
12978 }
Feng Wuefc64402015-09-18 22:29:51 +080012979
12980 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12981 if (e->type != KVM_IRQ_ROUTING_MSI)
12982 continue;
12983 /*
12984 * VT-d PI cannot support posting multicast/broadcast
12985 * interrupts to a vCPU, we still use interrupt remapping
12986 * for these kind of interrupts.
12987 *
12988 * For lowest-priority interrupts, we only support
12989 * those with single CPU as the destination, e.g. user
12990 * configures the interrupts via /proc/irq or uses
12991 * irqbalance to make the interrupts single-CPU.
12992 *
12993 * We will support full lowest-priority interrupt later.
12994 */
12995
Radim Krčmář371313132016-07-12 22:09:27 +020012996 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012997 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12998 /*
12999 * Make sure the IRTE is in remapped mode if
13000 * we don't handle it in posted mode.
13001 */
13002 ret = irq_set_vcpu_affinity(host_irq, NULL);
13003 if (ret < 0) {
13004 printk(KERN_INFO
13005 "failed to back to remapped mode, irq: %u\n",
13006 host_irq);
13007 goto out;
13008 }
13009
Feng Wuefc64402015-09-18 22:29:51 +080013010 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013011 }
Feng Wuefc64402015-09-18 22:29:51 +080013012
13013 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13014 vcpu_info.vector = irq.vector;
13015
hu huajun2698d822018-04-11 15:16:40 +080013016 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013017 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13018
13019 if (set)
13020 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013021 else
Feng Wuefc64402015-09-18 22:29:51 +080013022 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013023
13024 if (ret < 0) {
13025 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13026 __func__);
13027 goto out;
13028 }
13029 }
13030
13031 ret = 0;
13032out:
13033 srcu_read_unlock(&kvm->irq_srcu, idx);
13034 return ret;
13035}
13036
Ashok Rajc45dcc72016-06-22 14:59:56 +080013037static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13038{
13039 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13040 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13041 FEATURE_CONTROL_LMCE;
13042 else
13043 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13044 ~FEATURE_CONTROL_LMCE;
13045}
13046
Ladi Prosek72d7b372017-10-11 16:54:41 +020013047static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13048{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013049 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13050 if (to_vmx(vcpu)->nested.nested_run_pending)
13051 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013052 return 1;
13053}
13054
Ladi Prosek0234bf82017-10-11 16:54:40 +020013055static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13056{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013057 struct vcpu_vmx *vmx = to_vmx(vcpu);
13058
13059 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13060 if (vmx->nested.smm.guest_mode)
13061 nested_vmx_vmexit(vcpu, -1, 0, 0);
13062
13063 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13064 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013065 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013066 return 0;
13067}
13068
13069static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13070{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013071 struct vcpu_vmx *vmx = to_vmx(vcpu);
13072 int ret;
13073
13074 if (vmx->nested.smm.vmxon) {
13075 vmx->nested.vmxon = true;
13076 vmx->nested.smm.vmxon = false;
13077 }
13078
13079 if (vmx->nested.smm.guest_mode) {
13080 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013081 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013082 vcpu->arch.hflags |= HF_SMM_MASK;
13083 if (ret)
13084 return ret;
13085
13086 vmx->nested.smm.guest_mode = false;
13087 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013088 return 0;
13089}
13090
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013091static int enable_smi_window(struct kvm_vcpu *vcpu)
13092{
13093 return 0;
13094}
13095
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013096static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13097 struct kvm_nested_state __user *user_kvm_nested_state,
13098 u32 user_data_size)
13099{
13100 struct vcpu_vmx *vmx;
13101 struct vmcs12 *vmcs12;
13102 struct kvm_nested_state kvm_state = {
13103 .flags = 0,
13104 .format = 0,
13105 .size = sizeof(kvm_state),
13106 .vmx.vmxon_pa = -1ull,
13107 .vmx.vmcs_pa = -1ull,
13108 };
13109
13110 if (!vcpu)
13111 return kvm_state.size + 2 * VMCS12_SIZE;
13112
13113 vmx = to_vmx(vcpu);
13114 vmcs12 = get_vmcs12(vcpu);
13115 if (nested_vmx_allowed(vcpu) &&
13116 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13117 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13118 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13119
13120 if (vmx->nested.current_vmptr != -1ull)
13121 kvm_state.size += VMCS12_SIZE;
13122
13123 if (vmx->nested.smm.vmxon)
13124 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13125
13126 if (vmx->nested.smm.guest_mode)
13127 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13128
13129 if (is_guest_mode(vcpu)) {
13130 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13131
13132 if (vmx->nested.nested_run_pending)
13133 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13134 }
13135 }
13136
13137 if (user_data_size < kvm_state.size)
13138 goto out;
13139
13140 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13141 return -EFAULT;
13142
13143 if (vmx->nested.current_vmptr == -1ull)
13144 goto out;
13145
13146 /*
13147 * When running L2, the authoritative vmcs12 state is in the
13148 * vmcs02. When running L1, the authoritative vmcs12 state is
13149 * in the shadow vmcs linked to vmcs01, unless
13150 * sync_shadow_vmcs is set, in which case, the authoritative
13151 * vmcs12 state is in the vmcs12 already.
13152 */
13153 if (is_guest_mode(vcpu))
13154 sync_vmcs12(vcpu, vmcs12);
13155 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13156 copy_shadow_to_vmcs12(vmx);
13157
13158 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13159 return -EFAULT;
13160
13161out:
13162 return kvm_state.size;
13163}
13164
13165static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13166 struct kvm_nested_state __user *user_kvm_nested_state,
13167 struct kvm_nested_state *kvm_state)
13168{
13169 struct vcpu_vmx *vmx = to_vmx(vcpu);
13170 struct vmcs12 *vmcs12;
13171 u32 exit_qual;
13172 int ret;
13173
13174 if (kvm_state->format != 0)
13175 return -EINVAL;
13176
13177 if (!nested_vmx_allowed(vcpu))
13178 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13179
13180 if (kvm_state->vmx.vmxon_pa == -1ull) {
13181 if (kvm_state->vmx.smm.flags)
13182 return -EINVAL;
13183
13184 if (kvm_state->vmx.vmcs_pa != -1ull)
13185 return -EINVAL;
13186
13187 vmx_leave_nested(vcpu);
13188 return 0;
13189 }
13190
13191 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13192 return -EINVAL;
13193
13194 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13195 return -EINVAL;
13196
13197 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13198 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13199 return -EINVAL;
13200
13201 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13202 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13203 return -EINVAL;
13204
13205 if (kvm_state->vmx.smm.flags &
13206 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13207 return -EINVAL;
13208
13209 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13210 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13211 return -EINVAL;
13212
13213 vmx_leave_nested(vcpu);
13214 if (kvm_state->vmx.vmxon_pa == -1ull)
13215 return 0;
13216
13217 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13218 ret = enter_vmx_operation(vcpu);
13219 if (ret)
13220 return ret;
13221
13222 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13223
13224 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13225 vmx->nested.smm.vmxon = true;
13226 vmx->nested.vmxon = false;
13227
13228 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13229 vmx->nested.smm.guest_mode = true;
13230 }
13231
13232 vmcs12 = get_vmcs12(vcpu);
13233 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13234 return -EFAULT;
13235
Liran Alon392b2f22018-06-23 02:35:01 +030013236 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013237 return -EINVAL;
13238
13239 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13240 return 0;
13241
13242 vmx->nested.nested_run_pending =
13243 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13244
13245 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13246 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13247 return -EINVAL;
13248
13249 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13250 vmx->nested.nested_run_pending = 1;
13251
13252 vmx->nested.dirty_vmcs12 = true;
13253 ret = enter_vmx_non_root_mode(vcpu, NULL);
13254 if (ret)
13255 return -EINVAL;
13256
13257 return 0;
13258}
13259
Kees Cook404f6aa2016-08-08 16:29:06 -070013260static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013261 .cpu_has_kvm_support = cpu_has_kvm_support,
13262 .disabled_by_bios = vmx_disabled_by_bios,
13263 .hardware_setup = hardware_setup,
13264 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013265 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013266 .hardware_enable = hardware_enable,
13267 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013268 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013269 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013270
Wanpeng Lib31c1142018-03-12 04:53:04 -070013271 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013272 .vm_alloc = vmx_vm_alloc,
13273 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013274
Avi Kivity6aa8b732006-12-10 02:21:36 -080013275 .vcpu_create = vmx_create_vcpu,
13276 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013277 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013278
Avi Kivity04d2cc72007-09-10 18:10:54 +030013279 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013280 .vcpu_load = vmx_vcpu_load,
13281 .vcpu_put = vmx_vcpu_put,
13282
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013283 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013284 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013285 .get_msr = vmx_get_msr,
13286 .set_msr = vmx_set_msr,
13287 .get_segment_base = vmx_get_segment_base,
13288 .get_segment = vmx_get_segment,
13289 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013290 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013291 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013292 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013293 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013294 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013295 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013296 .set_cr3 = vmx_set_cr3,
13297 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013298 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013299 .get_idt = vmx_get_idt,
13300 .set_idt = vmx_set_idt,
13301 .get_gdt = vmx_get_gdt,
13302 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013303 .get_dr6 = vmx_get_dr6,
13304 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013305 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013306 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013307 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013308 .get_rflags = vmx_get_rflags,
13309 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013310
Avi Kivity6aa8b732006-12-10 02:21:36 -080013311 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013312
Avi Kivity6aa8b732006-12-10 02:21:36 -080013313 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013314 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013315 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013316 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13317 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013318 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013319 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013320 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013321 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013322 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013323 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013324 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013325 .get_nmi_mask = vmx_get_nmi_mask,
13326 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013327 .enable_nmi_window = enable_nmi_window,
13328 .enable_irq_window = enable_irq_window,
13329 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013330 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013331 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013332 .get_enable_apicv = vmx_get_enable_apicv,
13333 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013334 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013335 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013336 .hwapic_irr_update = vmx_hwapic_irr_update,
13337 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013338 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13339 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013340
Izik Eiduscbc94022007-10-25 00:29:55 +020013341 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013342 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013343 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013344 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013345
Avi Kivity586f9602010-11-18 13:09:54 +020013346 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013347
Sheng Yang17cc3932010-01-05 19:02:27 +080013348 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013349
13350 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013351
13352 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013353 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013354
13355 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013356
13357 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013358
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013359 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013360 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013361
13362 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013363
13364 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013365 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013366 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013367 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013368 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013369
13370 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013371
13372 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013373
13374 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13375 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13376 .flush_log_dirty = vmx_flush_log_dirty,
13377 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013378 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013379
Feng Wubf9f6ac2015-09-18 22:29:55 +080013380 .pre_block = vmx_pre_block,
13381 .post_block = vmx_post_block,
13382
Wei Huang25462f72015-06-19 15:45:05 +020013383 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013384
13385 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013386
13387#ifdef CONFIG_X86_64
13388 .set_hv_timer = vmx_set_hv_timer,
13389 .cancel_hv_timer = vmx_cancel_hv_timer,
13390#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013391
13392 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013393
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013394 .get_nested_state = vmx_get_nested_state,
13395 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013396 .get_vmcs12_pages = nested_get_vmcs12_pages,
13397
Ladi Prosek72d7b372017-10-11 16:54:41 +020013398 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013399 .pre_enter_smm = vmx_pre_enter_smm,
13400 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013401 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013402};
13403
13404static int __init vmx_init(void)
13405{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013406 int r;
13407
13408#if IS_ENABLED(CONFIG_HYPERV)
13409 /*
13410 * Enlightened VMCS usage should be recommended and the host needs
13411 * to support eVMCS v1 or above. We can also disable eVMCS support
13412 * with module parameter.
13413 */
13414 if (enlightened_vmcs &&
13415 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13416 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13417 KVM_EVMCS_VERSION) {
13418 int cpu;
13419
13420 /* Check that we have assist pages on all online CPUs */
13421 for_each_online_cpu(cpu) {
13422 if (!hv_get_vp_assist_page(cpu)) {
13423 enlightened_vmcs = false;
13424 break;
13425 }
13426 }
13427
13428 if (enlightened_vmcs) {
13429 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13430 static_branch_enable(&enable_evmcs);
13431 }
13432 } else {
13433 enlightened_vmcs = false;
13434 }
13435#endif
13436
13437 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013438 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013439 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013440 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013441
Dave Young2965faa2015-09-09 15:38:55 -070013442#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013443 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13444 crash_vmclear_local_loaded_vmcss);
13445#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013446 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013447
He, Qingfdef3ad2007-04-30 09:45:24 +030013448 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013449}
13450
13451static void __exit vmx_exit(void)
13452{
Dave Young2965faa2015-09-09 15:38:55 -070013453#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013454 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013455 synchronize_rcu();
13456#endif
13457
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013458 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013459
13460#if IS_ENABLED(CONFIG_HYPERV)
13461 if (static_branch_unlikely(&enable_evmcs)) {
13462 int cpu;
13463 struct hv_vp_assist_page *vp_ap;
13464 /*
13465 * Reset everything to support using non-enlightened VMCS
13466 * access later (e.g. when we reload the module with
13467 * enlightened_vmcs=0)
13468 */
13469 for_each_online_cpu(cpu) {
13470 vp_ap = hv_get_vp_assist_page(cpu);
13471
13472 if (!vp_ap)
13473 continue;
13474
13475 vp_ap->current_nested_vmcs = 0;
13476 vp_ap->enlighten_vmentry = 0;
13477 }
13478
13479 static_branch_disable(&enable_evmcs);
13480 }
13481#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013482}
13483
13484module_init(vmx_init)
13485module_exit(vmx_exit)