blob: 2a005f519032ad1607556ea10fbfcfed83220029 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700191struct kvm_vmx {
192 struct kvm kvm;
193
194 unsigned int tss_addr;
195 bool ept_identity_pagetable_done;
196 gpa_t ept_identity_map_addr;
197};
198
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200199#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300200
Liran Alon392b2f22018-06-23 02:35:01 +0300201struct vmcs_hdr {
202 u32 revision_id:31;
203 u32 shadow_vmcs:1;
204};
205
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400206struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300207 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400208 u32 abort;
209 char data[0];
210};
211
Nadav Har'Eld462b812011-05-24 15:26:10 +0300212/*
213 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
214 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
215 * loaded on this CPU (so we can clear them if the CPU goes down).
216 */
217struct loaded_vmcs {
218 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700219 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300220 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200221 bool launched;
222 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200223 unsigned long vmcs_host_cr3; /* May not match real cr3 */
224 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100225 /* Support for vnmi-less CPUs */
226 int soft_vnmi_blocked;
227 ktime_t entry_time;
228 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100229 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300230 struct list_head loaded_vmcss_on_cpu_link;
231};
232
Avi Kivity26bb0982009-09-07 11:14:12 +0300233struct shared_msr_entry {
234 unsigned index;
235 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200236 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300237};
238
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300239/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300240 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
241 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
242 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
243 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
244 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
245 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600246 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300247 * underlying hardware which will be used to run L2.
248 * This structure is packed to ensure that its layout is identical across
249 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700250 *
251 * IMPORTANT: Changing the layout of existing fields in this structure
252 * will break save/restore compatibility with older kvm releases. When
253 * adding new fields, either use space in the reserved padding* arrays
254 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300255 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300256typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300257struct __packed vmcs12 {
258 /* According to the Intel spec, a VMCS region must start with the
259 * following two fields. Then follow implementation-specific data.
260 */
Liran Alon392b2f22018-06-23 02:35:01 +0300261 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300262 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300263
Nadav Har'El27d6c862011-05-25 23:06:59 +0300264 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
265 u32 padding[7]; /* room for future expansion */
266
Nadav Har'El22bd0352011-05-25 23:05:57 +0300267 u64 io_bitmap_a;
268 u64 io_bitmap_b;
269 u64 msr_bitmap;
270 u64 vm_exit_msr_store_addr;
271 u64 vm_exit_msr_load_addr;
272 u64 vm_entry_msr_load_addr;
273 u64 tsc_offset;
274 u64 virtual_apic_page_addr;
275 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800276 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300277 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800278 u64 eoi_exit_bitmap0;
279 u64 eoi_exit_bitmap1;
280 u64 eoi_exit_bitmap2;
281 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800282 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300283 u64 guest_physical_address;
284 u64 vmcs_link_pointer;
285 u64 guest_ia32_debugctl;
286 u64 guest_ia32_pat;
287 u64 guest_ia32_efer;
288 u64 guest_ia32_perf_global_ctrl;
289 u64 guest_pdptr0;
290 u64 guest_pdptr1;
291 u64 guest_pdptr2;
292 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100293 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300294 u64 host_ia32_pat;
295 u64 host_ia32_efer;
296 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700297 u64 vmread_bitmap;
298 u64 vmwrite_bitmap;
299 u64 vm_function_control;
300 u64 eptp_list_address;
301 u64 pml_address;
302 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 /*
304 * To allow migration of L1 (complete with its L2 guests) between
305 * machines of different natural widths (32 or 64 bit), we cannot have
306 * unsigned long fields with no explict size. We use u64 (aliased
307 * natural_width) instead. Luckily, x86 is little-endian.
308 */
309 natural_width cr0_guest_host_mask;
310 natural_width cr4_guest_host_mask;
311 natural_width cr0_read_shadow;
312 natural_width cr4_read_shadow;
313 natural_width cr3_target_value0;
314 natural_width cr3_target_value1;
315 natural_width cr3_target_value2;
316 natural_width cr3_target_value3;
317 natural_width exit_qualification;
318 natural_width guest_linear_address;
319 natural_width guest_cr0;
320 natural_width guest_cr3;
321 natural_width guest_cr4;
322 natural_width guest_es_base;
323 natural_width guest_cs_base;
324 natural_width guest_ss_base;
325 natural_width guest_ds_base;
326 natural_width guest_fs_base;
327 natural_width guest_gs_base;
328 natural_width guest_ldtr_base;
329 natural_width guest_tr_base;
330 natural_width guest_gdtr_base;
331 natural_width guest_idtr_base;
332 natural_width guest_dr7;
333 natural_width guest_rsp;
334 natural_width guest_rip;
335 natural_width guest_rflags;
336 natural_width guest_pending_dbg_exceptions;
337 natural_width guest_sysenter_esp;
338 natural_width guest_sysenter_eip;
339 natural_width host_cr0;
340 natural_width host_cr3;
341 natural_width host_cr4;
342 natural_width host_fs_base;
343 natural_width host_gs_base;
344 natural_width host_tr_base;
345 natural_width host_gdtr_base;
346 natural_width host_idtr_base;
347 natural_width host_ia32_sysenter_esp;
348 natural_width host_ia32_sysenter_eip;
349 natural_width host_rsp;
350 natural_width host_rip;
351 natural_width paddingl[8]; /* room for future expansion */
352 u32 pin_based_vm_exec_control;
353 u32 cpu_based_vm_exec_control;
354 u32 exception_bitmap;
355 u32 page_fault_error_code_mask;
356 u32 page_fault_error_code_match;
357 u32 cr3_target_count;
358 u32 vm_exit_controls;
359 u32 vm_exit_msr_store_count;
360 u32 vm_exit_msr_load_count;
361 u32 vm_entry_controls;
362 u32 vm_entry_msr_load_count;
363 u32 vm_entry_intr_info_field;
364 u32 vm_entry_exception_error_code;
365 u32 vm_entry_instruction_len;
366 u32 tpr_threshold;
367 u32 secondary_vm_exec_control;
368 u32 vm_instruction_error;
369 u32 vm_exit_reason;
370 u32 vm_exit_intr_info;
371 u32 vm_exit_intr_error_code;
372 u32 idt_vectoring_info_field;
373 u32 idt_vectoring_error_code;
374 u32 vm_exit_instruction_len;
375 u32 vmx_instruction_info;
376 u32 guest_es_limit;
377 u32 guest_cs_limit;
378 u32 guest_ss_limit;
379 u32 guest_ds_limit;
380 u32 guest_fs_limit;
381 u32 guest_gs_limit;
382 u32 guest_ldtr_limit;
383 u32 guest_tr_limit;
384 u32 guest_gdtr_limit;
385 u32 guest_idtr_limit;
386 u32 guest_es_ar_bytes;
387 u32 guest_cs_ar_bytes;
388 u32 guest_ss_ar_bytes;
389 u32 guest_ds_ar_bytes;
390 u32 guest_fs_ar_bytes;
391 u32 guest_gs_ar_bytes;
392 u32 guest_ldtr_ar_bytes;
393 u32 guest_tr_ar_bytes;
394 u32 guest_interruptibility_info;
395 u32 guest_activity_state;
396 u32 guest_sysenter_cs;
397 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100398 u32 vmx_preemption_timer_value;
399 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300400 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800401 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300402 u16 guest_es_selector;
403 u16 guest_cs_selector;
404 u16 guest_ss_selector;
405 u16 guest_ds_selector;
406 u16 guest_fs_selector;
407 u16 guest_gs_selector;
408 u16 guest_ldtr_selector;
409 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800410 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300411 u16 host_es_selector;
412 u16 host_cs_selector;
413 u16 host_ss_selector;
414 u16 host_ds_selector;
415 u16 host_fs_selector;
416 u16 host_gs_selector;
417 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700418 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300419};
420
421/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700422 * For save/restore compatibility, the vmcs12 field offsets must not change.
423 */
424#define CHECK_OFFSET(field, loc) \
425 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
426 "Offset of " #field " in struct vmcs12 has changed.")
427
428static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300429 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700430 CHECK_OFFSET(abort, 4);
431 CHECK_OFFSET(launch_state, 8);
432 CHECK_OFFSET(io_bitmap_a, 40);
433 CHECK_OFFSET(io_bitmap_b, 48);
434 CHECK_OFFSET(msr_bitmap, 56);
435 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
436 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
437 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
438 CHECK_OFFSET(tsc_offset, 88);
439 CHECK_OFFSET(virtual_apic_page_addr, 96);
440 CHECK_OFFSET(apic_access_addr, 104);
441 CHECK_OFFSET(posted_intr_desc_addr, 112);
442 CHECK_OFFSET(ept_pointer, 120);
443 CHECK_OFFSET(eoi_exit_bitmap0, 128);
444 CHECK_OFFSET(eoi_exit_bitmap1, 136);
445 CHECK_OFFSET(eoi_exit_bitmap2, 144);
446 CHECK_OFFSET(eoi_exit_bitmap3, 152);
447 CHECK_OFFSET(xss_exit_bitmap, 160);
448 CHECK_OFFSET(guest_physical_address, 168);
449 CHECK_OFFSET(vmcs_link_pointer, 176);
450 CHECK_OFFSET(guest_ia32_debugctl, 184);
451 CHECK_OFFSET(guest_ia32_pat, 192);
452 CHECK_OFFSET(guest_ia32_efer, 200);
453 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
454 CHECK_OFFSET(guest_pdptr0, 216);
455 CHECK_OFFSET(guest_pdptr1, 224);
456 CHECK_OFFSET(guest_pdptr2, 232);
457 CHECK_OFFSET(guest_pdptr3, 240);
458 CHECK_OFFSET(guest_bndcfgs, 248);
459 CHECK_OFFSET(host_ia32_pat, 256);
460 CHECK_OFFSET(host_ia32_efer, 264);
461 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
462 CHECK_OFFSET(vmread_bitmap, 280);
463 CHECK_OFFSET(vmwrite_bitmap, 288);
464 CHECK_OFFSET(vm_function_control, 296);
465 CHECK_OFFSET(eptp_list_address, 304);
466 CHECK_OFFSET(pml_address, 312);
467 CHECK_OFFSET(cr0_guest_host_mask, 344);
468 CHECK_OFFSET(cr4_guest_host_mask, 352);
469 CHECK_OFFSET(cr0_read_shadow, 360);
470 CHECK_OFFSET(cr4_read_shadow, 368);
471 CHECK_OFFSET(cr3_target_value0, 376);
472 CHECK_OFFSET(cr3_target_value1, 384);
473 CHECK_OFFSET(cr3_target_value2, 392);
474 CHECK_OFFSET(cr3_target_value3, 400);
475 CHECK_OFFSET(exit_qualification, 408);
476 CHECK_OFFSET(guest_linear_address, 416);
477 CHECK_OFFSET(guest_cr0, 424);
478 CHECK_OFFSET(guest_cr3, 432);
479 CHECK_OFFSET(guest_cr4, 440);
480 CHECK_OFFSET(guest_es_base, 448);
481 CHECK_OFFSET(guest_cs_base, 456);
482 CHECK_OFFSET(guest_ss_base, 464);
483 CHECK_OFFSET(guest_ds_base, 472);
484 CHECK_OFFSET(guest_fs_base, 480);
485 CHECK_OFFSET(guest_gs_base, 488);
486 CHECK_OFFSET(guest_ldtr_base, 496);
487 CHECK_OFFSET(guest_tr_base, 504);
488 CHECK_OFFSET(guest_gdtr_base, 512);
489 CHECK_OFFSET(guest_idtr_base, 520);
490 CHECK_OFFSET(guest_dr7, 528);
491 CHECK_OFFSET(guest_rsp, 536);
492 CHECK_OFFSET(guest_rip, 544);
493 CHECK_OFFSET(guest_rflags, 552);
494 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
495 CHECK_OFFSET(guest_sysenter_esp, 568);
496 CHECK_OFFSET(guest_sysenter_eip, 576);
497 CHECK_OFFSET(host_cr0, 584);
498 CHECK_OFFSET(host_cr3, 592);
499 CHECK_OFFSET(host_cr4, 600);
500 CHECK_OFFSET(host_fs_base, 608);
501 CHECK_OFFSET(host_gs_base, 616);
502 CHECK_OFFSET(host_tr_base, 624);
503 CHECK_OFFSET(host_gdtr_base, 632);
504 CHECK_OFFSET(host_idtr_base, 640);
505 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
506 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
507 CHECK_OFFSET(host_rsp, 664);
508 CHECK_OFFSET(host_rip, 672);
509 CHECK_OFFSET(pin_based_vm_exec_control, 744);
510 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
511 CHECK_OFFSET(exception_bitmap, 752);
512 CHECK_OFFSET(page_fault_error_code_mask, 756);
513 CHECK_OFFSET(page_fault_error_code_match, 760);
514 CHECK_OFFSET(cr3_target_count, 764);
515 CHECK_OFFSET(vm_exit_controls, 768);
516 CHECK_OFFSET(vm_exit_msr_store_count, 772);
517 CHECK_OFFSET(vm_exit_msr_load_count, 776);
518 CHECK_OFFSET(vm_entry_controls, 780);
519 CHECK_OFFSET(vm_entry_msr_load_count, 784);
520 CHECK_OFFSET(vm_entry_intr_info_field, 788);
521 CHECK_OFFSET(vm_entry_exception_error_code, 792);
522 CHECK_OFFSET(vm_entry_instruction_len, 796);
523 CHECK_OFFSET(tpr_threshold, 800);
524 CHECK_OFFSET(secondary_vm_exec_control, 804);
525 CHECK_OFFSET(vm_instruction_error, 808);
526 CHECK_OFFSET(vm_exit_reason, 812);
527 CHECK_OFFSET(vm_exit_intr_info, 816);
528 CHECK_OFFSET(vm_exit_intr_error_code, 820);
529 CHECK_OFFSET(idt_vectoring_info_field, 824);
530 CHECK_OFFSET(idt_vectoring_error_code, 828);
531 CHECK_OFFSET(vm_exit_instruction_len, 832);
532 CHECK_OFFSET(vmx_instruction_info, 836);
533 CHECK_OFFSET(guest_es_limit, 840);
534 CHECK_OFFSET(guest_cs_limit, 844);
535 CHECK_OFFSET(guest_ss_limit, 848);
536 CHECK_OFFSET(guest_ds_limit, 852);
537 CHECK_OFFSET(guest_fs_limit, 856);
538 CHECK_OFFSET(guest_gs_limit, 860);
539 CHECK_OFFSET(guest_ldtr_limit, 864);
540 CHECK_OFFSET(guest_tr_limit, 868);
541 CHECK_OFFSET(guest_gdtr_limit, 872);
542 CHECK_OFFSET(guest_idtr_limit, 876);
543 CHECK_OFFSET(guest_es_ar_bytes, 880);
544 CHECK_OFFSET(guest_cs_ar_bytes, 884);
545 CHECK_OFFSET(guest_ss_ar_bytes, 888);
546 CHECK_OFFSET(guest_ds_ar_bytes, 892);
547 CHECK_OFFSET(guest_fs_ar_bytes, 896);
548 CHECK_OFFSET(guest_gs_ar_bytes, 900);
549 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
550 CHECK_OFFSET(guest_tr_ar_bytes, 908);
551 CHECK_OFFSET(guest_interruptibility_info, 912);
552 CHECK_OFFSET(guest_activity_state, 916);
553 CHECK_OFFSET(guest_sysenter_cs, 920);
554 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
555 CHECK_OFFSET(vmx_preemption_timer_value, 928);
556 CHECK_OFFSET(virtual_processor_id, 960);
557 CHECK_OFFSET(posted_intr_nv, 962);
558 CHECK_OFFSET(guest_es_selector, 964);
559 CHECK_OFFSET(guest_cs_selector, 966);
560 CHECK_OFFSET(guest_ss_selector, 968);
561 CHECK_OFFSET(guest_ds_selector, 970);
562 CHECK_OFFSET(guest_fs_selector, 972);
563 CHECK_OFFSET(guest_gs_selector, 974);
564 CHECK_OFFSET(guest_ldtr_selector, 976);
565 CHECK_OFFSET(guest_tr_selector, 978);
566 CHECK_OFFSET(guest_intr_status, 980);
567 CHECK_OFFSET(host_es_selector, 982);
568 CHECK_OFFSET(host_cs_selector, 984);
569 CHECK_OFFSET(host_ss_selector, 986);
570 CHECK_OFFSET(host_ds_selector, 988);
571 CHECK_OFFSET(host_fs_selector, 990);
572 CHECK_OFFSET(host_gs_selector, 992);
573 CHECK_OFFSET(host_tr_selector, 994);
574 CHECK_OFFSET(guest_pml_index, 996);
575}
576
577/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300578 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
579 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
580 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700581 *
582 * IMPORTANT: Changing this value will break save/restore compatibility with
583 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300584 */
585#define VMCS12_REVISION 0x11e57ed0
586
587/*
588 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
589 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
590 * current implementation, 4K are reserved to avoid future complications.
591 */
592#define VMCS12_SIZE 0x1000
593
594/*
Jim Mattson5b157062017-12-22 12:11:12 -0800595 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
596 * supported VMCS12 field encoding.
597 */
598#define VMCS12_MAX_FIELD_INDEX 0x17
599
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100600struct nested_vmx_msrs {
601 /*
602 * We only store the "true" versions of the VMX capability MSRs. We
603 * generate the "non-true" versions by setting the must-be-1 bits
604 * according to the SDM.
605 */
606 u32 procbased_ctls_low;
607 u32 procbased_ctls_high;
608 u32 secondary_ctls_low;
609 u32 secondary_ctls_high;
610 u32 pinbased_ctls_low;
611 u32 pinbased_ctls_high;
612 u32 exit_ctls_low;
613 u32 exit_ctls_high;
614 u32 entry_ctls_low;
615 u32 entry_ctls_high;
616 u32 misc_low;
617 u32 misc_high;
618 u32 ept_caps;
619 u32 vpid_caps;
620 u64 basic;
621 u64 cr0_fixed0;
622 u64 cr0_fixed1;
623 u64 cr4_fixed0;
624 u64 cr4_fixed1;
625 u64 vmcs_enum;
626 u64 vmfunc_controls;
627};
628
Jim Mattson5b157062017-12-22 12:11:12 -0800629/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300630 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
631 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
632 */
633struct nested_vmx {
634 /* Has the level1 guest done vmxon? */
635 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400636 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400637 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300638
639 /* The guest-physical address of the current VMCS L1 keeps for L2 */
640 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700641 /*
642 * Cache of the guest's VMCS, existing outside of guest memory.
643 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700644 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700645 */
646 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300647 /*
Liran Alon61ada742018-06-23 02:35:08 +0300648 * Cache of the guest's shadow VMCS, existing outside of guest
649 * memory. Loaded from guest memory during VM entry. Flushed
650 * to guest memory during VM exit.
651 */
652 struct vmcs12 *cached_shadow_vmcs12;
653 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300654 * Indicates if the shadow vmcs must be updated with the
655 * data hold by vmcs12
656 */
657 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100658 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300659
Jim Mattson8d860bb2018-05-09 16:56:05 -0400660 bool change_vmcs01_virtual_apic_mode;
661
Nadav Har'El644d7112011-05-25 23:12:35 +0300662 /* L2 must run next, and mustn't decide to exit to L1. */
663 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600664
665 struct loaded_vmcs vmcs02;
666
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300667 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600668 * Guest pages referred to in the vmcs02 with host-physical
669 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300670 */
671 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800672 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800673 struct page *pi_desc_page;
674 struct pi_desc *pi_desc;
675 bool pi_pending;
676 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100677
678 struct hrtimer preemption_timer;
679 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200680
681 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
682 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800683
Wanpeng Li5c614b32015-10-13 09:18:36 -0700684 u16 vpid02;
685 u16 last_vpid;
686
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100687 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200688
689 /* SMM related state */
690 struct {
691 /* in VMX operation on SMM entry? */
692 bool vmxon;
693 /* in guest mode on SMM entry? */
694 bool guest_mode;
695 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300696};
697
Yang Zhang01e439b2013-04-11 19:25:12 +0800698#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800699#define POSTED_INTR_SN 1
700
Yang Zhang01e439b2013-04-11 19:25:12 +0800701/* Posted-Interrupt Descriptor */
702struct pi_desc {
703 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800704 union {
705 struct {
706 /* bit 256 - Outstanding Notification */
707 u16 on : 1,
708 /* bit 257 - Suppress Notification */
709 sn : 1,
710 /* bit 271:258 - Reserved */
711 rsvd_1 : 14;
712 /* bit 279:272 - Notification Vector */
713 u8 nv;
714 /* bit 287:280 - Reserved */
715 u8 rsvd_2;
716 /* bit 319:288 - Notification Destination */
717 u32 ndst;
718 };
719 u64 control;
720 };
721 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800722} __aligned(64);
723
Yang Zhanga20ed542013-04-11 19:25:15 +0800724static bool pi_test_and_set_on(struct pi_desc *pi_desc)
725{
726 return test_and_set_bit(POSTED_INTR_ON,
727 (unsigned long *)&pi_desc->control);
728}
729
730static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
731{
732 return test_and_clear_bit(POSTED_INTR_ON,
733 (unsigned long *)&pi_desc->control);
734}
735
736static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
737{
738 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
739}
740
Feng Wuebbfc762015-09-18 22:29:46 +0800741static inline void pi_clear_sn(struct pi_desc *pi_desc)
742{
743 return clear_bit(POSTED_INTR_SN,
744 (unsigned long *)&pi_desc->control);
745}
746
747static inline void pi_set_sn(struct pi_desc *pi_desc)
748{
749 return set_bit(POSTED_INTR_SN,
750 (unsigned long *)&pi_desc->control);
751}
752
Paolo Bonziniad361092016-09-20 16:15:05 +0200753static inline void pi_clear_on(struct pi_desc *pi_desc)
754{
755 clear_bit(POSTED_INTR_ON,
756 (unsigned long *)&pi_desc->control);
757}
758
Feng Wuebbfc762015-09-18 22:29:46 +0800759static inline int pi_test_on(struct pi_desc *pi_desc)
760{
761 return test_bit(POSTED_INTR_ON,
762 (unsigned long *)&pi_desc->control);
763}
764
765static inline int pi_test_sn(struct pi_desc *pi_desc)
766{
767 return test_bit(POSTED_INTR_SN,
768 (unsigned long *)&pi_desc->control);
769}
770
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000772 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300773 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300774 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100775 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300776 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200777 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200778 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300779 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780 int nmsrs;
781 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800782 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400783#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300784 u64 msr_host_kernel_gs_base;
785 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400786#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100787
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100788 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100789 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100790
Gleb Natapov2961e8762013-11-25 15:37:13 +0200791 u32 vm_entry_controls_shadow;
792 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200793 u32 secondary_exec_control;
794
Nadav Har'Eld462b812011-05-24 15:26:10 +0300795 /*
796 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
797 * non-nested (L1) guest, it always points to vmcs01. For a nested
798 * guest (L2), it points to a different VMCS.
799 */
800 struct loaded_vmcs vmcs01;
801 struct loaded_vmcs *loaded_vmcs;
802 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300803 struct msr_autoload {
804 unsigned nr;
805 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
806 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
807 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808 struct {
809 int loaded;
810 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300811#ifdef CONFIG_X86_64
812 u16 ds_sel, es_sel;
813#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200814 int gs_ldt_reload_needed;
815 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400816 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200817 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300818 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300819 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300820 struct kvm_segment segs[8];
821 } rmode;
822 struct {
823 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300824 struct kvm_save_segment {
825 u16 selector;
826 unsigned long base;
827 u32 limit;
828 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300829 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300830 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800831 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300832 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200833
Andi Kleena0861c02009-06-08 17:37:09 +0800834 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800835
Yang Zhang01e439b2013-04-11 19:25:12 +0800836 /* Posted interrupt descriptor */
837 struct pi_desc pi_desc;
838
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300839 /* Support for a guest hypervisor (nested VMX) */
840 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200841
842 /* Dynamic PLE window. */
843 int ple_window;
844 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800845
846 /* Support for PML */
847#define PML_ENTITY_NUM 512
848 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800849
Yunhong Jiang64672c92016-06-13 14:19:59 -0700850 /* apic deadline value in host tsc */
851 u64 hv_deadline_tsc;
852
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800853 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800854
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800855 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800856
Wanpeng Li74c55932017-11-29 01:31:20 -0800857 unsigned long host_debugctlmsr;
858
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800859 /*
860 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
861 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
862 * in msr_ia32_feature_control_valid_bits.
863 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800864 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800865 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400866};
867
Avi Kivity2fb92db2011-04-27 19:42:18 +0300868enum segment_cache_field {
869 SEG_FIELD_SEL = 0,
870 SEG_FIELD_BASE = 1,
871 SEG_FIELD_LIMIT = 2,
872 SEG_FIELD_AR = 3,
873
874 SEG_FIELD_NR = 4
875};
876
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700877static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
878{
879 return container_of(kvm, struct kvm_vmx, kvm);
880}
881
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400882static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
883{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000884 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400885}
886
Feng Wuefc64402015-09-18 22:29:51 +0800887static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
888{
889 return &(to_vmx(vcpu)->pi_desc);
890}
891
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800892#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300893#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800894#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
895#define FIELD64(number, name) \
896 FIELD(number, name), \
897 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300898
Abel Gordon4607c2d2013-04-18 14:35:55 +0300899
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100900static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100901#define SHADOW_FIELD_RO(x) x,
902#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300903};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400904static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300905 ARRAY_SIZE(shadow_read_only_fields);
906
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100907static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100908#define SHADOW_FIELD_RW(x) x,
909#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300910};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400911static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300912 ARRAY_SIZE(shadow_read_write_fields);
913
Mathias Krause772e0312012-08-30 01:30:19 +0200914static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300915 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800916 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300917 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
918 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
919 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
920 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
921 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
922 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
923 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
924 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800925 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400926 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300927 FIELD(HOST_ES_SELECTOR, host_es_selector),
928 FIELD(HOST_CS_SELECTOR, host_cs_selector),
929 FIELD(HOST_SS_SELECTOR, host_ss_selector),
930 FIELD(HOST_DS_SELECTOR, host_ds_selector),
931 FIELD(HOST_FS_SELECTOR, host_fs_selector),
932 FIELD(HOST_GS_SELECTOR, host_gs_selector),
933 FIELD(HOST_TR_SELECTOR, host_tr_selector),
934 FIELD64(IO_BITMAP_A, io_bitmap_a),
935 FIELD64(IO_BITMAP_B, io_bitmap_b),
936 FIELD64(MSR_BITMAP, msr_bitmap),
937 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
938 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
939 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700940 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300941 FIELD64(TSC_OFFSET, tsc_offset),
942 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
943 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800944 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400945 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300946 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800947 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
948 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
949 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
950 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400951 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700952 FIELD64(VMREAD_BITMAP, vmread_bitmap),
953 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800954 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300955 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
956 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
957 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
958 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
959 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
960 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
961 FIELD64(GUEST_PDPTR0, guest_pdptr0),
962 FIELD64(GUEST_PDPTR1, guest_pdptr1),
963 FIELD64(GUEST_PDPTR2, guest_pdptr2),
964 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100965 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300966 FIELD64(HOST_IA32_PAT, host_ia32_pat),
967 FIELD64(HOST_IA32_EFER, host_ia32_efer),
968 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
969 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
970 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
971 FIELD(EXCEPTION_BITMAP, exception_bitmap),
972 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
973 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
974 FIELD(CR3_TARGET_COUNT, cr3_target_count),
975 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
976 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
977 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
978 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
979 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
980 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
981 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
982 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
983 FIELD(TPR_THRESHOLD, tpr_threshold),
984 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
985 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
986 FIELD(VM_EXIT_REASON, vm_exit_reason),
987 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
988 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
989 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
990 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
991 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
992 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
993 FIELD(GUEST_ES_LIMIT, guest_es_limit),
994 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
995 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
996 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
997 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
998 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
999 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1000 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1001 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1002 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1003 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1004 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1005 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1006 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1007 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1008 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1009 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1010 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1011 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1012 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1013 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1014 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001015 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001016 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1017 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1018 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1019 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1020 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1021 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1022 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1023 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1024 FIELD(EXIT_QUALIFICATION, exit_qualification),
1025 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1026 FIELD(GUEST_CR0, guest_cr0),
1027 FIELD(GUEST_CR3, guest_cr3),
1028 FIELD(GUEST_CR4, guest_cr4),
1029 FIELD(GUEST_ES_BASE, guest_es_base),
1030 FIELD(GUEST_CS_BASE, guest_cs_base),
1031 FIELD(GUEST_SS_BASE, guest_ss_base),
1032 FIELD(GUEST_DS_BASE, guest_ds_base),
1033 FIELD(GUEST_FS_BASE, guest_fs_base),
1034 FIELD(GUEST_GS_BASE, guest_gs_base),
1035 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1036 FIELD(GUEST_TR_BASE, guest_tr_base),
1037 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1038 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1039 FIELD(GUEST_DR7, guest_dr7),
1040 FIELD(GUEST_RSP, guest_rsp),
1041 FIELD(GUEST_RIP, guest_rip),
1042 FIELD(GUEST_RFLAGS, guest_rflags),
1043 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1044 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1045 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1046 FIELD(HOST_CR0, host_cr0),
1047 FIELD(HOST_CR3, host_cr3),
1048 FIELD(HOST_CR4, host_cr4),
1049 FIELD(HOST_FS_BASE, host_fs_base),
1050 FIELD(HOST_GS_BASE, host_gs_base),
1051 FIELD(HOST_TR_BASE, host_tr_base),
1052 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1053 FIELD(HOST_IDTR_BASE, host_idtr_base),
1054 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1055 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1056 FIELD(HOST_RSP, host_rsp),
1057 FIELD(HOST_RIP, host_rip),
1058};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001059
1060static inline short vmcs_field_to_offset(unsigned long field)
1061{
Dan Williams085331d2018-01-31 17:47:03 -08001062 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1063 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001064 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001065
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001066 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001067 return -ENOENT;
1068
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001069 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001070 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001071 return -ENOENT;
1072
Linus Torvalds15303ba2018-02-10 13:16:35 -08001073 index = array_index_nospec(index, size);
1074 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001075 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001076 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001077 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078}
1079
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001080static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1081{
David Matlack4f2777b2016-07-13 17:16:37 -07001082 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001083}
1084
Liran Alon61ada742018-06-23 02:35:08 +03001085static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1086{
1087 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1088}
1089
Peter Feiner995f00a2017-06-30 17:26:32 -07001090static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001091static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001092static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001093static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001094static void vmx_set_segment(struct kvm_vcpu *vcpu,
1095 struct kvm_segment *var, int seg);
1096static void vmx_get_segment(struct kvm_vcpu *vcpu,
1097 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001098static bool guest_state_valid(struct kvm_vcpu *vcpu);
1099static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001100static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001101static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1102static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1103static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1104 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001105static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001106static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1107 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1110static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001111/*
1112 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1113 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1114 */
1115static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116
Feng Wubf9f6ac2015-09-18 22:29:55 +08001117/*
1118 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1119 * can find which vCPU should be waken up.
1120 */
1121static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1122static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1123
Radim Krčmář23611332016-09-29 22:41:33 +02001124enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001125 VMX_VMREAD_BITMAP,
1126 VMX_VMWRITE_BITMAP,
1127 VMX_BITMAP_NR
1128};
1129
1130static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1131
Radim Krčmář23611332016-09-29 22:41:33 +02001132#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1133#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001134
Avi Kivity110312c2010-12-21 12:54:20 +02001135static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001136static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001137
Sheng Yang2384d2b2008-01-17 15:14:33 +08001138static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1139static DEFINE_SPINLOCK(vmx_vpid_lock);
1140
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001141static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 int size;
1143 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001144 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001146 u32 pin_based_exec_ctrl;
1147 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001148 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001149 u32 vmexit_ctrl;
1150 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001151 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001152} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153
Hannes Ederefff9e52008-11-28 17:02:06 +01001154static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001155 u32 ept;
1156 u32 vpid;
1157} vmx_capability;
1158
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159#define VMX_SEGMENT_FIELD(seg) \
1160 [VCPU_SREG_##seg] = { \
1161 .selector = GUEST_##seg##_SELECTOR, \
1162 .base = GUEST_##seg##_BASE, \
1163 .limit = GUEST_##seg##_LIMIT, \
1164 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1165 }
1166
Mathias Krause772e0312012-08-30 01:30:19 +02001167static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 unsigned selector;
1169 unsigned base;
1170 unsigned limit;
1171 unsigned ar_bytes;
1172} kvm_vmx_segment_fields[] = {
1173 VMX_SEGMENT_FIELD(CS),
1174 VMX_SEGMENT_FIELD(DS),
1175 VMX_SEGMENT_FIELD(ES),
1176 VMX_SEGMENT_FIELD(FS),
1177 VMX_SEGMENT_FIELD(GS),
1178 VMX_SEGMENT_FIELD(SS),
1179 VMX_SEGMENT_FIELD(TR),
1180 VMX_SEGMENT_FIELD(LDTR),
1181};
1182
Avi Kivity26bb0982009-09-07 11:14:12 +03001183static u64 host_efer;
1184
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001185static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1186
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001187/*
Brian Gerst8c065852010-07-17 09:03:26 -04001188 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001189 * away by decrementing the array size.
1190 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001192#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001193 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001195 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001198DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1199
1200#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1201
1202#define KVM_EVMCS_VERSION 1
1203
1204#if IS_ENABLED(CONFIG_HYPERV)
1205static bool __read_mostly enlightened_vmcs = true;
1206module_param(enlightened_vmcs, bool, 0444);
1207
1208static inline void evmcs_write64(unsigned long field, u64 value)
1209{
1210 u16 clean_field;
1211 int offset = get_evmcs_offset(field, &clean_field);
1212
1213 if (offset < 0)
1214 return;
1215
1216 *(u64 *)((char *)current_evmcs + offset) = value;
1217
1218 current_evmcs->hv_clean_fields &= ~clean_field;
1219}
1220
1221static inline void evmcs_write32(unsigned long field, u32 value)
1222{
1223 u16 clean_field;
1224 int offset = get_evmcs_offset(field, &clean_field);
1225
1226 if (offset < 0)
1227 return;
1228
1229 *(u32 *)((char *)current_evmcs + offset) = value;
1230 current_evmcs->hv_clean_fields &= ~clean_field;
1231}
1232
1233static inline void evmcs_write16(unsigned long field, u16 value)
1234{
1235 u16 clean_field;
1236 int offset = get_evmcs_offset(field, &clean_field);
1237
1238 if (offset < 0)
1239 return;
1240
1241 *(u16 *)((char *)current_evmcs + offset) = value;
1242 current_evmcs->hv_clean_fields &= ~clean_field;
1243}
1244
1245static inline u64 evmcs_read64(unsigned long field)
1246{
1247 int offset = get_evmcs_offset(field, NULL);
1248
1249 if (offset < 0)
1250 return 0;
1251
1252 return *(u64 *)((char *)current_evmcs + offset);
1253}
1254
1255static inline u32 evmcs_read32(unsigned long field)
1256{
1257 int offset = get_evmcs_offset(field, NULL);
1258
1259 if (offset < 0)
1260 return 0;
1261
1262 return *(u32 *)((char *)current_evmcs + offset);
1263}
1264
1265static inline u16 evmcs_read16(unsigned long field)
1266{
1267 int offset = get_evmcs_offset(field, NULL);
1268
1269 if (offset < 0)
1270 return 0;
1271
1272 return *(u16 *)((char *)current_evmcs + offset);
1273}
1274
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001275static inline void evmcs_touch_msr_bitmap(void)
1276{
1277 if (unlikely(!current_evmcs))
1278 return;
1279
1280 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1281 current_evmcs->hv_clean_fields &=
1282 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1283}
1284
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001285static void evmcs_load(u64 phys_addr)
1286{
1287 struct hv_vp_assist_page *vp_ap =
1288 hv_get_vp_assist_page(smp_processor_id());
1289
1290 vp_ap->current_nested_vmcs = phys_addr;
1291 vp_ap->enlighten_vmentry = 1;
1292}
1293
1294static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1295{
1296 /*
1297 * Enlightened VMCSv1 doesn't support these:
1298 *
1299 * POSTED_INTR_NV = 0x00000002,
1300 * GUEST_INTR_STATUS = 0x00000810,
1301 * APIC_ACCESS_ADDR = 0x00002014,
1302 * POSTED_INTR_DESC_ADDR = 0x00002016,
1303 * EOI_EXIT_BITMAP0 = 0x0000201c,
1304 * EOI_EXIT_BITMAP1 = 0x0000201e,
1305 * EOI_EXIT_BITMAP2 = 0x00002020,
1306 * EOI_EXIT_BITMAP3 = 0x00002022,
1307 */
1308 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1309 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1310 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1311 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1312 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1313 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1314 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1315
1316 /*
1317 * GUEST_PML_INDEX = 0x00000812,
1318 * PML_ADDRESS = 0x0000200e,
1319 */
1320 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1321
1322 /* VM_FUNCTION_CONTROL = 0x00002018, */
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1324
1325 /*
1326 * EPTP_LIST_ADDRESS = 0x00002024,
1327 * VMREAD_BITMAP = 0x00002026,
1328 * VMWRITE_BITMAP = 0x00002028,
1329 */
1330 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1331
1332 /*
1333 * TSC_MULTIPLIER = 0x00002032,
1334 */
1335 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1336
1337 /*
1338 * PLE_GAP = 0x00004020,
1339 * PLE_WINDOW = 0x00004022,
1340 */
1341 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1342
1343 /*
1344 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1345 */
1346 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1347
1348 /*
1349 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1350 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1351 */
1352 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1353 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1354
1355 /*
1356 * Currently unsupported in KVM:
1357 * GUEST_IA32_RTIT_CTL = 0x00002814,
1358 */
1359}
1360#else /* !IS_ENABLED(CONFIG_HYPERV) */
1361static inline void evmcs_write64(unsigned long field, u64 value) {}
1362static inline void evmcs_write32(unsigned long field, u32 value) {}
1363static inline void evmcs_write16(unsigned long field, u16 value) {}
1364static inline u64 evmcs_read64(unsigned long field) { return 0; }
1365static inline u32 evmcs_read32(unsigned long field) { return 0; }
1366static inline u16 evmcs_read16(unsigned long field) { return 0; }
1367static inline void evmcs_load(u64 phys_addr) {}
1368static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001369static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001370#endif /* IS_ENABLED(CONFIG_HYPERV) */
1371
Jan Kiszka5bb16012016-02-09 20:14:21 +01001372static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373{
1374 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1375 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001376 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1377}
1378
Jan Kiszka6f054852016-02-09 20:15:18 +01001379static inline bool is_debug(u32 intr_info)
1380{
1381 return is_exception_n(intr_info, DB_VECTOR);
1382}
1383
1384static inline bool is_breakpoint(u32 intr_info)
1385{
1386 return is_exception_n(intr_info, BP_VECTOR);
1387}
1388
Jan Kiszka5bb16012016-02-09 20:14:21 +01001389static inline bool is_page_fault(u32 intr_info)
1390{
1391 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392}
1393
Gui Jianfeng31299942010-03-15 17:29:09 +08001394static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001395{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001396 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001397}
1398
Gui Jianfeng31299942010-03-15 17:29:09 +08001399static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001400{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001401 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001402}
1403
Liran Alon9e869482018-03-12 13:12:51 +02001404static inline bool is_gp_fault(u32 intr_info)
1405{
1406 return is_exception_n(intr_info, GP_VECTOR);
1407}
1408
Gui Jianfeng31299942010-03-15 17:29:09 +08001409static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
1411 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1412 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1413}
1414
Gui Jianfeng31299942010-03-15 17:29:09 +08001415static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001416{
1417 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1418 INTR_INFO_VALID_MASK)) ==
1419 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1420}
1421
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001422/* Undocumented: icebp/int1 */
1423static inline bool is_icebp(u32 intr_info)
1424{
1425 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1426 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001430{
Sheng Yang04547152009-04-01 15:52:31 +08001431 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001432}
1433
Gui Jianfeng31299942010-03-15 17:29:09 +08001434static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001435{
Sheng Yang04547152009-04-01 15:52:31 +08001436 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001437}
1438
Paolo Bonzini35754c92015-07-29 12:05:37 +02001439static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001440{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001441 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001442}
1443
Gui Jianfeng31299942010-03-15 17:29:09 +08001444static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001445{
Sheng Yang04547152009-04-01 15:52:31 +08001446 return vmcs_config.cpu_based_exec_ctrl &
1447 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001448}
1449
Avi Kivity774ead32007-12-26 13:57:04 +02001450static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001451{
Sheng Yang04547152009-04-01 15:52:31 +08001452 return vmcs_config.cpu_based_2nd_exec_ctrl &
1453 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1454}
1455
Yang Zhang8d146952013-01-25 10:18:50 +08001456static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1457{
1458 return vmcs_config.cpu_based_2nd_exec_ctrl &
1459 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1460}
1461
Yang Zhang83d4c282013-01-25 10:18:49 +08001462static inline bool cpu_has_vmx_apic_register_virt(void)
1463{
1464 return vmcs_config.cpu_based_2nd_exec_ctrl &
1465 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1466}
1467
Yang Zhangc7c9c562013-01-25 10:18:51 +08001468static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1469{
1470 return vmcs_config.cpu_based_2nd_exec_ctrl &
1471 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1472}
1473
Yunhong Jiang64672c92016-06-13 14:19:59 -07001474/*
1475 * Comment's format: document - errata name - stepping - processor name.
1476 * Refer from
1477 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1478 */
1479static u32 vmx_preemption_cpu_tfms[] = {
1480/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
14810x000206E6,
1482/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1483/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1484/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
14850x00020652,
1486/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
14870x00020655,
1488/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1489/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1490/*
1491 * 320767.pdf - AAP86 - B1 -
1492 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1493 */
14940x000106E5,
1495/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
14960x000106A0,
1497/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
14980x000106A1,
1499/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15000x000106A4,
1501 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1502 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1503 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15040x000106A5,
1505};
1506
1507static inline bool cpu_has_broken_vmx_preemption_timer(void)
1508{
1509 u32 eax = cpuid_eax(0x00000001), i;
1510
1511 /* Clear the reserved bits */
1512 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001513 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001514 if (eax == vmx_preemption_cpu_tfms[i])
1515 return true;
1516
1517 return false;
1518}
1519
1520static inline bool cpu_has_vmx_preemption_timer(void)
1521{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001522 return vmcs_config.pin_based_exec_ctrl &
1523 PIN_BASED_VMX_PREEMPTION_TIMER;
1524}
1525
Yang Zhang01e439b2013-04-11 19:25:12 +08001526static inline bool cpu_has_vmx_posted_intr(void)
1527{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001528 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1529 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001530}
1531
1532static inline bool cpu_has_vmx_apicv(void)
1533{
1534 return cpu_has_vmx_apic_register_virt() &&
1535 cpu_has_vmx_virtual_intr_delivery() &&
1536 cpu_has_vmx_posted_intr();
1537}
1538
Sheng Yang04547152009-04-01 15:52:31 +08001539static inline bool cpu_has_vmx_flexpriority(void)
1540{
1541 return cpu_has_vmx_tpr_shadow() &&
1542 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001543}
1544
Marcelo Tosattie7997942009-06-11 12:07:40 -03001545static inline bool cpu_has_vmx_ept_execute_only(void)
1546{
Gui Jianfeng31299942010-03-15 17:29:09 +08001547 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001548}
1549
Marcelo Tosattie7997942009-06-11 12:07:40 -03001550static inline bool cpu_has_vmx_ept_2m_page(void)
1551{
Gui Jianfeng31299942010-03-15 17:29:09 +08001552 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001553}
1554
Sheng Yang878403b2010-01-05 19:02:29 +08001555static inline bool cpu_has_vmx_ept_1g_page(void)
1556{
Gui Jianfeng31299942010-03-15 17:29:09 +08001557 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001558}
1559
Sheng Yang4bc9b982010-06-02 14:05:24 +08001560static inline bool cpu_has_vmx_ept_4levels(void)
1561{
1562 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1563}
1564
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001565static inline bool cpu_has_vmx_ept_mt_wb(void)
1566{
1567 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1568}
1569
Yu Zhang855feb62017-08-24 20:27:55 +08001570static inline bool cpu_has_vmx_ept_5levels(void)
1571{
1572 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1573}
1574
Xudong Hao83c3a332012-05-28 19:33:35 +08001575static inline bool cpu_has_vmx_ept_ad_bits(void)
1576{
1577 return vmx_capability.ept & VMX_EPT_AD_BIT;
1578}
1579
Gui Jianfeng31299942010-03-15 17:29:09 +08001580static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001581{
Gui Jianfeng31299942010-03-15 17:29:09 +08001582 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001583}
1584
Gui Jianfeng31299942010-03-15 17:29:09 +08001585static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001586{
Gui Jianfeng31299942010-03-15 17:29:09 +08001587 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001588}
1589
Liran Aloncd9a4912018-05-22 17:16:15 +03001590static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1591{
1592 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1593}
1594
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001595static inline bool cpu_has_vmx_invvpid_single(void)
1596{
1597 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1598}
1599
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001600static inline bool cpu_has_vmx_invvpid_global(void)
1601{
1602 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1603}
1604
Wanpeng Li08d839c2017-03-23 05:30:08 -07001605static inline bool cpu_has_vmx_invvpid(void)
1606{
1607 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1608}
1609
Gui Jianfeng31299942010-03-15 17:29:09 +08001610static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001611{
Sheng Yang04547152009-04-01 15:52:31 +08001612 return vmcs_config.cpu_based_2nd_exec_ctrl &
1613 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001617{
1618 return vmcs_config.cpu_based_2nd_exec_ctrl &
1619 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1620}
1621
Gui Jianfeng31299942010-03-15 17:29:09 +08001622static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001623{
1624 return vmcs_config.cpu_based_2nd_exec_ctrl &
1625 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1626}
1627
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001628static inline bool cpu_has_vmx_basic_inout(void)
1629{
1630 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1631}
1632
Paolo Bonzini35754c92015-07-29 12:05:37 +02001633static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001634{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001635 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001636}
1637
Gui Jianfeng31299942010-03-15 17:29:09 +08001638static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001639{
Sheng Yang04547152009-04-01 15:52:31 +08001640 return vmcs_config.cpu_based_2nd_exec_ctrl &
1641 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001642}
1643
Gui Jianfeng31299942010-03-15 17:29:09 +08001644static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001645{
1646 return vmcs_config.cpu_based_2nd_exec_ctrl &
1647 SECONDARY_EXEC_RDTSCP;
1648}
1649
Mao, Junjiead756a12012-07-02 01:18:48 +00001650static inline bool cpu_has_vmx_invpcid(void)
1651{
1652 return vmcs_config.cpu_based_2nd_exec_ctrl &
1653 SECONDARY_EXEC_ENABLE_INVPCID;
1654}
1655
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001656static inline bool cpu_has_virtual_nmis(void)
1657{
1658 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1659}
1660
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001661static inline bool cpu_has_vmx_wbinvd_exit(void)
1662{
1663 return vmcs_config.cpu_based_2nd_exec_ctrl &
1664 SECONDARY_EXEC_WBINVD_EXITING;
1665}
1666
Abel Gordonabc4fc52013-04-18 14:35:25 +03001667static inline bool cpu_has_vmx_shadow_vmcs(void)
1668{
1669 u64 vmx_msr;
1670 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1671 /* check if the cpu supports writing r/o exit information fields */
1672 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1673 return false;
1674
1675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_SHADOW_VMCS;
1677}
1678
Kai Huang843e4332015-01-28 10:54:28 +08001679static inline bool cpu_has_vmx_pml(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1682}
1683
Haozhong Zhang64903d62015-10-20 15:39:09 +08001684static inline bool cpu_has_vmx_tsc_scaling(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_TSC_SCALING;
1688}
1689
Bandan Das2a499e42017-08-03 15:54:41 -04001690static inline bool cpu_has_vmx_vmfunc(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_ENABLE_VMFUNC;
1694}
1695
Sean Christopherson64f7a112018-04-30 10:01:06 -07001696static bool vmx_umip_emulated(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_DESC;
1700}
1701
Sheng Yang04547152009-04-01 15:52:31 +08001702static inline bool report_flexpriority(void)
1703{
1704 return flexpriority_enabled;
1705}
1706
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001707static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1708{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001709 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001710}
1711
Jim Mattsonf4160e42018-05-29 09:11:33 -07001712/*
1713 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1714 * to modify any valid field of the VMCS, or are the VM-exit
1715 * information fields read-only?
1716 */
1717static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1718{
1719 return to_vmx(vcpu)->nested.msrs.misc_low &
1720 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1721}
1722
Marc Orr04473782018-06-20 17:21:29 -07001723static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1724{
1725 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1726}
1727
1728static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1729{
1730 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1731 CPU_BASED_MONITOR_TRAP_FLAG;
1732}
1733
Liran Alonfa97d7d2018-07-18 14:07:59 +02001734static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1735{
1736 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1737 SECONDARY_EXEC_SHADOW_VMCS;
1738}
1739
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001740static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1741{
1742 return vmcs12->cpu_based_vm_exec_control & bit;
1743}
1744
1745static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1746{
1747 return (vmcs12->cpu_based_vm_exec_control &
1748 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1749 (vmcs12->secondary_vm_exec_control & bit);
1750}
1751
Jan Kiszkaf4124502014-03-07 20:03:13 +01001752static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1753{
1754 return vmcs12->pin_based_vm_exec_control &
1755 PIN_BASED_VMX_PREEMPTION_TIMER;
1756}
1757
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001758static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1759{
1760 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1761}
1762
1763static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1764{
1765 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1766}
1767
Nadav Har'El155a97a2013-08-05 11:07:16 +03001768static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1769{
1770 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1771}
1772
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001773static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1774{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001775 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001776}
1777
Bandan Dasc5f983f2017-05-05 15:25:14 -04001778static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1779{
1780 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1781}
1782
Wincy Vanf2b93282015-02-03 23:56:03 +08001783static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1784{
1785 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1786}
1787
Wanpeng Li5c614b32015-10-13 09:18:36 -07001788static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1789{
1790 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1791}
1792
Wincy Van82f0dd42015-02-03 23:57:18 +08001793static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1794{
1795 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1796}
1797
Wincy Van608406e2015-02-03 23:57:51 +08001798static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1799{
1800 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1801}
1802
Wincy Van705699a2015-02-03 23:58:17 +08001803static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1804{
1805 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1806}
1807
Bandan Das27c42a12017-08-03 15:54:42 -04001808static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1809{
1810 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1811}
1812
Bandan Das41ab9372017-08-03 15:54:43 -04001813static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1814{
1815 return nested_cpu_has_vmfunc(vmcs12) &&
1816 (vmcs12->vm_function_control &
1817 VMX_VMFUNC_EPTP_SWITCHING);
1818}
1819
Liran Alonf792d272018-06-23 02:35:05 +03001820static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1821{
1822 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1823}
1824
Jim Mattsonef85b672016-12-12 11:01:37 -08001825static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001826{
1827 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001828 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001829}
1830
Jan Kiszka533558b2014-01-04 18:47:20 +01001831static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1832 u32 exit_intr_info,
1833 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001834static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1835 struct vmcs12 *vmcs12,
1836 u32 reason, unsigned long qualification);
1837
Rusty Russell8b9cf982007-07-30 16:31:43 +10001838static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001839{
1840 int i;
1841
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001842 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001843 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001844 return i;
1845 return -1;
1846}
1847
Sheng Yang2384d2b2008-01-17 15:14:33 +08001848static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1849{
1850 struct {
1851 u64 vpid : 16;
1852 u64 rsvd : 48;
1853 u64 gva;
1854 } operand = { vpid, 0, gva };
1855
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001856 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001857 /* CF==1 or ZF==1 --> rc = -1 */
1858 "; ja 1f ; ud2 ; 1:"
1859 : : "a"(&operand), "c"(ext) : "cc", "memory");
1860}
1861
Sheng Yang14394422008-04-28 12:24:45 +08001862static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1863{
1864 struct {
1865 u64 eptp, gpa;
1866 } operand = {eptp, gpa};
1867
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001868 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001869 /* CF==1 or ZF==1 --> rc = -1 */
1870 "; ja 1f ; ud2 ; 1:\n"
1871 : : "a" (&operand), "c" (ext) : "cc", "memory");
1872}
1873
Avi Kivity26bb0982009-09-07 11:14:12 +03001874static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001875{
1876 int i;
1877
Rusty Russell8b9cf982007-07-30 16:31:43 +10001878 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001879 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001880 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001881 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001882}
1883
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884static void vmcs_clear(struct vmcs *vmcs)
1885{
1886 u64 phys_addr = __pa(vmcs);
1887 u8 error;
1888
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001889 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001890 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 : "cc", "memory");
1892 if (error)
1893 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1894 vmcs, phys_addr);
1895}
1896
Nadav Har'Eld462b812011-05-24 15:26:10 +03001897static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1898{
1899 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001900 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1901 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001902 loaded_vmcs->cpu = -1;
1903 loaded_vmcs->launched = 0;
1904}
1905
Dongxiao Xu7725b892010-05-11 18:29:38 +08001906static void vmcs_load(struct vmcs *vmcs)
1907{
1908 u64 phys_addr = __pa(vmcs);
1909 u8 error;
1910
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001911 if (static_branch_unlikely(&enable_evmcs))
1912 return evmcs_load(phys_addr);
1913
Dongxiao Xu7725b892010-05-11 18:29:38 +08001914 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001915 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001916 : "cc", "memory");
1917 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001918 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001919 vmcs, phys_addr);
1920}
1921
Dave Young2965faa2015-09-09 15:38:55 -07001922#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001923/*
1924 * This bitmap is used to indicate whether the vmclear
1925 * operation is enabled on all cpus. All disabled by
1926 * default.
1927 */
1928static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1929
1930static inline void crash_enable_local_vmclear(int cpu)
1931{
1932 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1933}
1934
1935static inline void crash_disable_local_vmclear(int cpu)
1936{
1937 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1938}
1939
1940static inline int crash_local_vmclear_enabled(int cpu)
1941{
1942 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1943}
1944
1945static void crash_vmclear_local_loaded_vmcss(void)
1946{
1947 int cpu = raw_smp_processor_id();
1948 struct loaded_vmcs *v;
1949
1950 if (!crash_local_vmclear_enabled(cpu))
1951 return;
1952
1953 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1954 loaded_vmcss_on_cpu_link)
1955 vmcs_clear(v->vmcs);
1956}
1957#else
1958static inline void crash_enable_local_vmclear(int cpu) { }
1959static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001960#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001961
Nadav Har'Eld462b812011-05-24 15:26:10 +03001962static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001964 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001965 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966
Nadav Har'Eld462b812011-05-24 15:26:10 +03001967 if (loaded_vmcs->cpu != cpu)
1968 return; /* vcpu migration can race with cpu offline */
1969 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001971 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001972 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001973
1974 /*
1975 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1976 * is before setting loaded_vmcs->vcpu to -1 which is done in
1977 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1978 * then adds the vmcs into percpu list before it is deleted.
1979 */
1980 smp_wmb();
1981
Nadav Har'Eld462b812011-05-24 15:26:10 +03001982 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001983 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984}
1985
Nadav Har'Eld462b812011-05-24 15:26:10 +03001986static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001987{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001988 int cpu = loaded_vmcs->cpu;
1989
1990 if (cpu != -1)
1991 smp_call_function_single(cpu,
1992 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001993}
1994
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001995static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001996{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001997 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001998 return;
1999
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002000 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002001 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002002}
2003
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002004static inline void vpid_sync_vcpu_global(void)
2005{
2006 if (cpu_has_vmx_invvpid_global())
2007 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2008}
2009
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002010static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002011{
2012 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002013 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002014 else
2015 vpid_sync_vcpu_global();
2016}
2017
Sheng Yang14394422008-04-28 12:24:45 +08002018static inline void ept_sync_global(void)
2019{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002020 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002021}
2022
2023static inline void ept_sync_context(u64 eptp)
2024{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002025 if (cpu_has_vmx_invept_context())
2026 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2027 else
2028 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002029}
2030
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002031static __always_inline void vmcs_check16(unsigned long field)
2032{
2033 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2034 "16-bit accessor invalid for 64-bit field");
2035 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2036 "16-bit accessor invalid for 64-bit high field");
2037 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2038 "16-bit accessor invalid for 32-bit high field");
2039 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2040 "16-bit accessor invalid for natural width field");
2041}
2042
2043static __always_inline void vmcs_check32(unsigned long field)
2044{
2045 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2046 "32-bit accessor invalid for 16-bit field");
2047 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2048 "32-bit accessor invalid for natural width field");
2049}
2050
2051static __always_inline void vmcs_check64(unsigned long field)
2052{
2053 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2054 "64-bit accessor invalid for 16-bit field");
2055 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2056 "64-bit accessor invalid for 64-bit high field");
2057 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2058 "64-bit accessor invalid for 32-bit field");
2059 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2060 "64-bit accessor invalid for natural width field");
2061}
2062
2063static __always_inline void vmcs_checkl(unsigned long field)
2064{
2065 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2066 "Natural width accessor invalid for 16-bit field");
2067 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2068 "Natural width accessor invalid for 64-bit field");
2069 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2070 "Natural width accessor invalid for 64-bit high field");
2071 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2072 "Natural width accessor invalid for 32-bit field");
2073}
2074
2075static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076{
Avi Kivity5e520e62011-05-15 10:13:12 -04002077 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
Avi Kivity5e520e62011-05-15 10:13:12 -04002079 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2080 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 return value;
2082}
2083
Avi Kivity96304212011-05-15 10:13:13 -04002084static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002086 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002087 if (static_branch_unlikely(&enable_evmcs))
2088 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002089 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090}
2091
Avi Kivity96304212011-05-15 10:13:13 -04002092static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002094 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002095 if (static_branch_unlikely(&enable_evmcs))
2096 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002097 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098}
2099
Avi Kivity96304212011-05-15 10:13:13 -04002100static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002102 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002103 if (static_branch_unlikely(&enable_evmcs))
2104 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002105#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002106 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002108 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109#endif
2110}
2111
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002112static __always_inline unsigned long vmcs_readl(unsigned long field)
2113{
2114 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002115 if (static_branch_unlikely(&enable_evmcs))
2116 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002117 return __vmcs_readl(field);
2118}
2119
Avi Kivitye52de1b2007-01-05 16:36:56 -08002120static noinline void vmwrite_error(unsigned long field, unsigned long value)
2121{
2122 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2123 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2124 dump_stack();
2125}
2126
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002127static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128{
2129 u8 error;
2130
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002131 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002132 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002133 if (unlikely(error))
2134 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002137static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002139 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002140 if (static_branch_unlikely(&enable_evmcs))
2141 return evmcs_write16(field, value);
2142
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002143 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144}
2145
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002146static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002148 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002149 if (static_branch_unlikely(&enable_evmcs))
2150 return evmcs_write32(field, value);
2151
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002152 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002155static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002158 if (static_branch_unlikely(&enable_evmcs))
2159 return evmcs_write64(field, value);
2160
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002161 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002162#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002164 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165#endif
2166}
2167
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002168static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002169{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002170 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002171 if (static_branch_unlikely(&enable_evmcs))
2172 return evmcs_write64(field, value);
2173
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002174 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002175}
2176
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002178{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002179 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2180 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002181 if (static_branch_unlikely(&enable_evmcs))
2182 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2183
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002184 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2185}
2186
2187static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2188{
2189 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2190 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002191 if (static_branch_unlikely(&enable_evmcs))
2192 return evmcs_write32(field, evmcs_read32(field) | mask);
2193
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002194 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002195}
2196
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002197static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2198{
2199 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2200}
2201
Gleb Natapov2961e8762013-11-25 15:37:13 +02002202static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2203{
2204 vmcs_write32(VM_ENTRY_CONTROLS, val);
2205 vmx->vm_entry_controls_shadow = val;
2206}
2207
2208static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2209{
2210 if (vmx->vm_entry_controls_shadow != val)
2211 vm_entry_controls_init(vmx, val);
2212}
2213
2214static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2215{
2216 return vmx->vm_entry_controls_shadow;
2217}
2218
2219
2220static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2221{
2222 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2223}
2224
2225static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2226{
2227 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2228}
2229
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002230static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2231{
2232 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2233}
2234
Gleb Natapov2961e8762013-11-25 15:37:13 +02002235static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2236{
2237 vmcs_write32(VM_EXIT_CONTROLS, val);
2238 vmx->vm_exit_controls_shadow = val;
2239}
2240
2241static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2242{
2243 if (vmx->vm_exit_controls_shadow != val)
2244 vm_exit_controls_init(vmx, val);
2245}
2246
2247static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2248{
2249 return vmx->vm_exit_controls_shadow;
2250}
2251
2252
2253static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2254{
2255 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2256}
2257
2258static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2259{
2260 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2261}
2262
Avi Kivity2fb92db2011-04-27 19:42:18 +03002263static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2264{
2265 vmx->segment_cache.bitmask = 0;
2266}
2267
2268static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2269 unsigned field)
2270{
2271 bool ret;
2272 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2273
2274 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2275 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2276 vmx->segment_cache.bitmask = 0;
2277 }
2278 ret = vmx->segment_cache.bitmask & mask;
2279 vmx->segment_cache.bitmask |= mask;
2280 return ret;
2281}
2282
2283static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2284{
2285 u16 *p = &vmx->segment_cache.seg[seg].selector;
2286
2287 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2288 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2289 return *p;
2290}
2291
2292static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2293{
2294 ulong *p = &vmx->segment_cache.seg[seg].base;
2295
2296 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2297 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2298 return *p;
2299}
2300
2301static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2302{
2303 u32 *p = &vmx->segment_cache.seg[seg].limit;
2304
2305 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2306 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2307 return *p;
2308}
2309
2310static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2311{
2312 u32 *p = &vmx->segment_cache.seg[seg].ar;
2313
2314 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2315 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2316 return *p;
2317}
2318
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002319static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2320{
2321 u32 eb;
2322
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002323 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002324 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002325 /*
2326 * Guest access to VMware backdoor ports could legitimately
2327 * trigger #GP because of TSS I/O permission bitmap.
2328 * We intercept those #GP and allow access to them anyway
2329 * as VMware does.
2330 */
2331 if (enable_vmware_backdoor)
2332 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002333 if ((vcpu->guest_debug &
2334 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2335 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2336 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002337 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002338 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002339 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002340 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002341
2342 /* When we are running a nested L2 guest and L1 specified for it a
2343 * certain exception bitmap, we must trap the same exceptions and pass
2344 * them to L1. When running L2, we will only handle the exceptions
2345 * specified above if L1 did not want them.
2346 */
2347 if (is_guest_mode(vcpu))
2348 eb |= get_vmcs12(vcpu)->exception_bitmap;
2349
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002350 vmcs_write32(EXCEPTION_BITMAP, eb);
2351}
2352
Ashok Raj15d45072018-02-01 22:59:43 +01002353/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002354 * Check if MSR is intercepted for currently loaded MSR bitmap.
2355 */
2356static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2357{
2358 unsigned long *msr_bitmap;
2359 int f = sizeof(unsigned long);
2360
2361 if (!cpu_has_vmx_msr_bitmap())
2362 return true;
2363
2364 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2365
2366 if (msr <= 0x1fff) {
2367 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2368 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2369 msr &= 0x1fff;
2370 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2371 }
2372
2373 return true;
2374}
2375
2376/*
Ashok Raj15d45072018-02-01 22:59:43 +01002377 * Check if MSR is intercepted for L01 MSR bitmap.
2378 */
2379static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2380{
2381 unsigned long *msr_bitmap;
2382 int f = sizeof(unsigned long);
2383
2384 if (!cpu_has_vmx_msr_bitmap())
2385 return true;
2386
2387 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2388
2389 if (msr <= 0x1fff) {
2390 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2391 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2392 msr &= 0x1fff;
2393 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2394 }
2395
2396 return true;
2397}
2398
Gleb Natapov2961e8762013-11-25 15:37:13 +02002399static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2400 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002401{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002402 vm_entry_controls_clearbit(vmx, entry);
2403 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002404}
2405
Avi Kivity61d2ef22010-04-28 16:40:38 +03002406static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2407{
2408 unsigned i;
2409 struct msr_autoload *m = &vmx->msr_autoload;
2410
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002411 switch (msr) {
2412 case MSR_EFER:
2413 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002414 clear_atomic_switch_msr_special(vmx,
2415 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002416 VM_EXIT_LOAD_IA32_EFER);
2417 return;
2418 }
2419 break;
2420 case MSR_CORE_PERF_GLOBAL_CTRL:
2421 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002422 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002423 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2424 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2425 return;
2426 }
2427 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002428 }
2429
Avi Kivity61d2ef22010-04-28 16:40:38 +03002430 for (i = 0; i < m->nr; ++i)
2431 if (m->guest[i].index == msr)
2432 break;
2433
2434 if (i == m->nr)
2435 return;
2436 --m->nr;
2437 m->guest[i] = m->guest[m->nr];
2438 m->host[i] = m->host[m->nr];
2439 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2440 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2441}
2442
Gleb Natapov2961e8762013-11-25 15:37:13 +02002443static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2444 unsigned long entry, unsigned long exit,
2445 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2446 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002447{
2448 vmcs_write64(guest_val_vmcs, guest_val);
2449 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002450 vm_entry_controls_setbit(vmx, entry);
2451 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002452}
2453
Avi Kivity61d2ef22010-04-28 16:40:38 +03002454static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2455 u64 guest_val, u64 host_val)
2456{
2457 unsigned i;
2458 struct msr_autoload *m = &vmx->msr_autoload;
2459
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002460 switch (msr) {
2461 case MSR_EFER:
2462 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002463 add_atomic_switch_msr_special(vmx,
2464 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002465 VM_EXIT_LOAD_IA32_EFER,
2466 GUEST_IA32_EFER,
2467 HOST_IA32_EFER,
2468 guest_val, host_val);
2469 return;
2470 }
2471 break;
2472 case MSR_CORE_PERF_GLOBAL_CTRL:
2473 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002474 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002475 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2476 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2477 GUEST_IA32_PERF_GLOBAL_CTRL,
2478 HOST_IA32_PERF_GLOBAL_CTRL,
2479 guest_val, host_val);
2480 return;
2481 }
2482 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002483 case MSR_IA32_PEBS_ENABLE:
2484 /* PEBS needs a quiescent period after being disabled (to write
2485 * a record). Disabling PEBS through VMX MSR swapping doesn't
2486 * provide that period, so a CPU could write host's record into
2487 * guest's memory.
2488 */
2489 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002490 }
2491
Avi Kivity61d2ef22010-04-28 16:40:38 +03002492 for (i = 0; i < m->nr; ++i)
2493 if (m->guest[i].index == msr)
2494 break;
2495
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002496 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002497 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002498 "Can't add msr %x\n", msr);
2499 return;
2500 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002501 ++m->nr;
2502 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2503 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2504 }
2505
2506 m->guest[i].index = msr;
2507 m->guest[i].value = guest_val;
2508 m->host[i].index = msr;
2509 m->host[i].value = host_val;
2510}
2511
Avi Kivity92c0d902009-10-29 11:00:16 +02002512static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002513{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002514 u64 guest_efer = vmx->vcpu.arch.efer;
2515 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002516
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002517 if (!enable_ept) {
2518 /*
2519 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2520 * host CPUID is more efficient than testing guest CPUID
2521 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2522 */
2523 if (boot_cpu_has(X86_FEATURE_SMEP))
2524 guest_efer |= EFER_NX;
2525 else if (!(guest_efer & EFER_NX))
2526 ignore_bits |= EFER_NX;
2527 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002528
Avi Kivity51c6cf62007-08-29 03:48:05 +03002529 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002530 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002531 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002532 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002533#ifdef CONFIG_X86_64
2534 ignore_bits |= EFER_LMA | EFER_LME;
2535 /* SCE is meaningful only in long mode on Intel */
2536 if (guest_efer & EFER_LMA)
2537 ignore_bits &= ~(u64)EFER_SCE;
2538#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002539
2540 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002541
2542 /*
2543 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2544 * On CPUs that support "load IA32_EFER", always switch EFER
2545 * atomically, since it's faster than switching it manually.
2546 */
2547 if (cpu_has_load_ia32_efer ||
2548 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002549 if (!(guest_efer & EFER_LMA))
2550 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002551 if (guest_efer != host_efer)
2552 add_atomic_switch_msr(vmx, MSR_EFER,
2553 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002554 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002555 } else {
2556 guest_efer &= ~ignore_bits;
2557 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002558
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002559 vmx->guest_msrs[efer_offset].data = guest_efer;
2560 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2561
2562 return true;
2563 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002564}
2565
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002566#ifdef CONFIG_X86_32
2567/*
2568 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2569 * VMCS rather than the segment table. KVM uses this helper to figure
2570 * out the current bases to poke them into the VMCS before entry.
2571 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002572static unsigned long segment_base(u16 selector)
2573{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002574 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002575 unsigned long v;
2576
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002577 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002578 return 0;
2579
Thomas Garnier45fc8752017-03-14 10:05:08 -07002580 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002581
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002582 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002583 u16 ldt_selector = kvm_read_ldt();
2584
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002585 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002586 return 0;
2587
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002588 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002589 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002590 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002591 return v;
2592}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002593#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002594
Avi Kivity04d2cc72007-09-10 18:10:54 +03002595static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002596{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002597 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002598#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002599 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002600 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002601#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002602 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002603
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002604 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002605 return;
2606
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002607 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002608 /*
2609 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2610 * allow segment selectors with cpl > 0 or ti == 1.
2611 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002612 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002613 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002614
2615#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002616 if (likely(is_64bit_mm(current->mm))) {
2617 save_fsgs_for_kvm();
2618 vmx->host_state.fs_sel = current->thread.fsindex;
2619 vmx->host_state.gs_sel = current->thread.gsindex;
2620 fs_base = current->thread.fsbase;
2621 kernel_gs_base = current->thread.gsbase;
2622 } else {
2623#endif
2624 savesegment(fs, vmx->host_state.fs_sel);
2625 savesegment(gs, vmx->host_state.gs_sel);
2626#ifdef CONFIG_X86_64
2627 fs_base = read_msr(MSR_FS_BASE);
2628 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2629 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002630#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002631 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002632 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002633 vmx->host_state.fs_reload_needed = 0;
2634 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002635 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002636 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002637 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002638 if (!(vmx->host_state.gs_sel & 7))
2639 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002640 else {
2641 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002642 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002643 }
2644
2645#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002646 savesegment(ds, vmx->host_state.ds_sel);
2647 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002648
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002649 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002650 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002651
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002652 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002653 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002654 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002655#else
2656 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2657 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2658#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002659 for (i = 0; i < vmx->save_nmsrs; ++i)
2660 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002661 vmx->guest_msrs[i].data,
2662 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002663}
2664
Avi Kivitya9b21b62008-06-24 11:48:49 +03002665static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002666{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002667 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002668 return;
2669
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002670 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002671 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002672#ifdef CONFIG_X86_64
2673 if (is_long_mode(&vmx->vcpu))
2674 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2675#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002676 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002677 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002678#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002679 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002680#else
2681 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002682#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002683 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002684 if (vmx->host_state.fs_reload_needed)
2685 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002686#ifdef CONFIG_X86_64
2687 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2688 loadsegment(ds, vmx->host_state.ds_sel);
2689 loadsegment(es, vmx->host_state.es_sel);
2690 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002691#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002692 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002693#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002694 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002695#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002696 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002697}
2698
Avi Kivitya9b21b62008-06-24 11:48:49 +03002699static void vmx_load_host_state(struct vcpu_vmx *vmx)
2700{
2701 preempt_disable();
2702 __vmx_load_host_state(vmx);
2703 preempt_enable();
2704}
2705
Feng Wu28b835d2015-09-18 22:29:54 +08002706static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2707{
2708 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2709 struct pi_desc old, new;
2710 unsigned int dest;
2711
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002712 /*
2713 * In case of hot-plug or hot-unplug, we may have to undo
2714 * vmx_vcpu_pi_put even if there is no assigned device. And we
2715 * always keep PI.NDST up to date for simplicity: it makes the
2716 * code easier, and CPU migration is not a fast path.
2717 */
2718 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002719 return;
2720
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002721 /*
2722 * First handle the simple case where no cmpxchg is necessary; just
2723 * allow posting non-urgent interrupts.
2724 *
2725 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2726 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2727 * expects the VCPU to be on the blocked_vcpu_list that matches
2728 * PI.NDST.
2729 */
2730 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2731 vcpu->cpu == cpu) {
2732 pi_clear_sn(pi_desc);
2733 return;
2734 }
2735
2736 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002737 do {
2738 old.control = new.control = pi_desc->control;
2739
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002740 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002741
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002742 if (x2apic_enabled())
2743 new.ndst = dest;
2744 else
2745 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002746
Feng Wu28b835d2015-09-18 22:29:54 +08002747 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002748 } while (cmpxchg64(&pi_desc->control, old.control,
2749 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002750}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002751
Peter Feinerc95ba922016-08-17 09:36:47 -07002752static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2753{
2754 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2755 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2756}
2757
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758/*
2759 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2760 * vcpu mutex is already taken.
2761 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002762static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002764 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002765 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002767 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002768 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002769 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002770 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002771
2772 /*
2773 * Read loaded_vmcs->cpu should be before fetching
2774 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2775 * See the comments in __loaded_vmcs_clear().
2776 */
2777 smp_rmb();
2778
Nadav Har'Eld462b812011-05-24 15:26:10 +03002779 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2780 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002781 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002782 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002783 }
2784
2785 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2786 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2787 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002788 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002789 }
2790
2791 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002792 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002793 unsigned long sysenter_esp;
2794
2795 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002796
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 /*
2798 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002799 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002801 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002802 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002803 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002805 /*
2806 * VM exits change the host TR limit to 0x67 after a VM
2807 * exit. This is okay, since 0x67 covers everything except
2808 * the IO bitmap and have have code to handle the IO bitmap
2809 * being lost after a VM exit.
2810 */
2811 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2812
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2814 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002815
Nadav Har'Eld462b812011-05-24 15:26:10 +03002816 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 }
Feng Wu28b835d2015-09-18 22:29:54 +08002818
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002819 /* Setup TSC multiplier */
2820 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002821 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2822 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002823
Feng Wu28b835d2015-09-18 22:29:54 +08002824 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002825 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002826 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002827}
2828
2829static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2830{
2831 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2832
2833 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002834 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2835 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002836 return;
2837
2838 /* Set SN when the vCPU is preempted */
2839 if (vcpu->preempted)
2840 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841}
2842
2843static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2844{
Feng Wu28b835d2015-09-18 22:29:54 +08002845 vmx_vcpu_pi_put(vcpu);
2846
Avi Kivitya9b21b62008-06-24 11:48:49 +03002847 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848}
2849
Wanpeng Lif244dee2017-07-20 01:11:54 -07002850static bool emulation_required(struct kvm_vcpu *vcpu)
2851{
2852 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2853}
2854
Avi Kivityedcafe32009-12-30 18:07:40 +02002855static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2856
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002857/*
2858 * Return the cr0 value that a nested guest would read. This is a combination
2859 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2860 * its hypervisor (cr0_read_shadow).
2861 */
2862static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2863{
2864 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2865 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2866}
2867static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2868{
2869 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2870 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2871}
2872
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2874{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002875 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002876
Avi Kivity6de12732011-03-07 12:51:22 +02002877 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2878 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2879 rflags = vmcs_readl(GUEST_RFLAGS);
2880 if (to_vmx(vcpu)->rmode.vm86_active) {
2881 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2882 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2883 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2884 }
2885 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002886 }
Avi Kivity6de12732011-03-07 12:51:22 +02002887 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888}
2889
2890static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2891{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002892 unsigned long old_rflags = vmx_get_rflags(vcpu);
2893
Avi Kivity6de12732011-03-07 12:51:22 +02002894 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2895 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002896 if (to_vmx(vcpu)->rmode.vm86_active) {
2897 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002898 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002899 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002901
2902 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2903 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904}
2905
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002906static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002907{
2908 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2909 int ret = 0;
2910
2911 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002912 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002913 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002914 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002915
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002916 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002917}
2918
2919static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2920{
2921 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2922 u32 interruptibility = interruptibility_old;
2923
2924 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2925
Jan Kiszka48005f62010-02-19 19:38:07 +01002926 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002927 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002928 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002929 interruptibility |= GUEST_INTR_STATE_STI;
2930
2931 if ((interruptibility != interruptibility_old))
2932 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2933}
2934
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2936{
2937 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002939 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002941 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942
Glauber Costa2809f5d2009-05-12 16:21:05 -04002943 /* skipping an emulated instruction also counts */
2944 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945}
2946
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002947static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2948 unsigned long exit_qual)
2949{
2950 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2951 unsigned int nr = vcpu->arch.exception.nr;
2952 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2953
2954 if (vcpu->arch.exception.has_error_code) {
2955 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2956 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2957 }
2958
2959 if (kvm_exception_is_soft(nr))
2960 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2961 else
2962 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2963
2964 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2965 vmx_get_nmi_mask(vcpu))
2966 intr_info |= INTR_INFO_UNBLOCK_NMI;
2967
2968 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2969}
2970
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002971/*
2972 * KVM wants to inject page-faults which it got to the guest. This function
2973 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002974 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002975static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002976{
2977 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002978 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002979
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002980 if (nr == PF_VECTOR) {
2981 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002982 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002983 return 1;
2984 }
2985 /*
2986 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2987 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2988 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2989 * can be written only when inject_pending_event runs. This should be
2990 * conditional on a new capability---if the capability is disabled,
2991 * kvm_multiple_exception would write the ancillary information to
2992 * CR2 or DR6, for backwards ABI-compatibility.
2993 */
2994 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2995 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002996 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002997 return 1;
2998 }
2999 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003000 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003001 if (nr == DB_VECTOR)
3002 *exit_qual = vcpu->arch.dr6;
3003 else
3004 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003005 return 1;
3006 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003007 }
3008
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003009 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003010}
3011
Wanpeng Licaa057a2018-03-12 04:53:03 -07003012static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3013{
3014 /*
3015 * Ensure that we clear the HLT state in the VMCS. We don't need to
3016 * explicitly skip the instruction because if the HLT state is set,
3017 * then the instruction is already executing and RIP has already been
3018 * advanced.
3019 */
3020 if (kvm_hlt_in_guest(vcpu->kvm) &&
3021 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3022 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3023}
3024
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003025static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003026{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003027 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003028 unsigned nr = vcpu->arch.exception.nr;
3029 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003030 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003031 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003032
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003033 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003034 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003035 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3036 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003037
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003038 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003039 int inc_eip = 0;
3040 if (kvm_exception_is_soft(nr))
3041 inc_eip = vcpu->arch.event_exit_inst_len;
3042 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003043 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003044 return;
3045 }
3046
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003047 WARN_ON_ONCE(vmx->emulation_required);
3048
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003049 if (kvm_exception_is_soft(nr)) {
3050 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3051 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003052 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3053 } else
3054 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3055
3056 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003057
3058 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003059}
3060
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003061static bool vmx_rdtscp_supported(void)
3062{
3063 return cpu_has_vmx_rdtscp();
3064}
3065
Mao, Junjiead756a12012-07-02 01:18:48 +00003066static bool vmx_invpcid_supported(void)
3067{
3068 return cpu_has_vmx_invpcid() && enable_ept;
3069}
3070
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071/*
Eddie Donga75beee2007-05-17 18:55:15 +03003072 * Swap MSR entry in host/guest MSR entry array.
3073 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003074static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003075{
Avi Kivity26bb0982009-09-07 11:14:12 +03003076 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003077
3078 tmp = vmx->guest_msrs[to];
3079 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3080 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003081}
3082
3083/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003084 * Set up the vmcs to automatically save and restore system
3085 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3086 * mode, as fiddling with msrs is very expensive.
3087 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003088static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003089{
Avi Kivity26bb0982009-09-07 11:14:12 +03003090 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003091
Eddie Donga75beee2007-05-17 18:55:15 +03003092 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003093#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003094 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003095 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003096 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003097 move_msr_up(vmx, index, save_nmsrs++);
3098 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003099 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003100 move_msr_up(vmx, index, save_nmsrs++);
3101 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003102 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003103 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003104 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003105 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003106 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003107 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003108 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003109 * if efer.sce is enabled.
3110 */
Brian Gerst8c065852010-07-17 09:03:26 -04003111 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003112 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003113 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003114 }
Eddie Donga75beee2007-05-17 18:55:15 +03003115#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003116 index = __find_msr_index(vmx, MSR_EFER);
3117 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003118 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003119
Avi Kivity26bb0982009-09-07 11:14:12 +03003120 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003121
Yang Zhang8d146952013-01-25 10:18:50 +08003122 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003123 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003124}
3125
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003126static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003128 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003130 if (is_guest_mode(vcpu) &&
3131 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3132 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3133
3134 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135}
3136
3137/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003138 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003140static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003142 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003143 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003144 * We're here if L1 chose not to trap WRMSR to TSC. According
3145 * to the spec, this should set L1's TSC; The offset that L1
3146 * set for L2 remains unchanged, and still needs to be added
3147 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003148 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003149 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003150 /* recalculate vmcs02.TSC_OFFSET: */
3151 vmcs12 = get_vmcs12(vcpu);
3152 vmcs_write64(TSC_OFFSET, offset +
3153 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3154 vmcs12->tsc_offset : 0));
3155 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003156 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3157 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003158 vmcs_write64(TSC_OFFSET, offset);
3159 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160}
3161
Nadav Har'El801d3422011-05-25 23:02:23 +03003162/*
3163 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3164 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3165 * all guests if the "nested" module option is off, and can also be disabled
3166 * for a single guest by disabling its VMX cpuid bit.
3167 */
3168static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3169{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003170 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003171}
3172
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003174 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3175 * returned for the various VMX controls MSRs when nested VMX is enabled.
3176 * The same values should also be used to verify that vmcs12 control fields are
3177 * valid during nested entry from L1 to L2.
3178 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3179 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3180 * bit in the high half is on if the corresponding bit in the control field
3181 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003182 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003183static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003184{
Paolo Bonzini13893092018-02-26 13:40:09 +01003185 if (!nested) {
3186 memset(msrs, 0, sizeof(*msrs));
3187 return;
3188 }
3189
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003190 /*
3191 * Note that as a general rule, the high half of the MSRs (bits in
3192 * the control fields which may be 1) should be initialized by the
3193 * intersection of the underlying hardware's MSR (i.e., features which
3194 * can be supported) and the list of features we want to expose -
3195 * because they are known to be properly supported in our code.
3196 * Also, usually, the low half of the MSRs (bits which must be 1) can
3197 * be set to 0, meaning that L1 may turn off any of these bits. The
3198 * reason is that if one of these bits is necessary, it will appear
3199 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3200 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003201 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202 * These rules have exceptions below.
3203 */
3204
3205 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003206 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003207 msrs->pinbased_ctls_low,
3208 msrs->pinbased_ctls_high);
3209 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003210 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003211 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003212 PIN_BASED_EXT_INTR_MASK |
3213 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003214 PIN_BASED_VIRTUAL_NMIS |
3215 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003217 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003218 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003219
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003220 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003221 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003222 msrs->exit_ctls_low,
3223 msrs->exit_ctls_high);
3224 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003225 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003226
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003227 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003228#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003229 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003230#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003231 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003232 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003233 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003234 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003235 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3236
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003237 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239
Jan Kiszka2996fca2014-06-16 13:59:43 +02003240 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003242
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003243 /* entry controls */
3244 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003245 msrs->entry_ctls_low,
3246 msrs->entry_ctls_high);
3247 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003248 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003249 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003250#ifdef CONFIG_X86_64
3251 VM_ENTRY_IA32E_MODE |
3252#endif
3253 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003254 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003255 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003256 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003257 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003258
Jan Kiszka2996fca2014-06-16 13:59:43 +02003259 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003260 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003261
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003262 /* cpu-based controls */
3263 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003264 msrs->procbased_ctls_low,
3265 msrs->procbased_ctls_high);
3266 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003267 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003268 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003269 CPU_BASED_VIRTUAL_INTR_PENDING |
3270 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003271 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3272 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3273 CPU_BASED_CR3_STORE_EXITING |
3274#ifdef CONFIG_X86_64
3275 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3276#endif
3277 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003278 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3279 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3280 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3281 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003282 /*
3283 * We can allow some features even when not supported by the
3284 * hardware. For example, L1 can specify an MSR bitmap - and we
3285 * can use it to avoid exits to L1 - even when L0 runs L2
3286 * without MSR bitmaps.
3287 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003288 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003289 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003290 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003291
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003292 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003293 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003294 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3295
Paolo Bonzini80154d72017-08-24 13:55:35 +02003296 /*
3297 * secondary cpu-based controls. Do not include those that
3298 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3299 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003300 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003301 msrs->secondary_ctls_low,
3302 msrs->secondary_ctls_high);
3303 msrs->secondary_ctls_low = 0;
3304 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003305 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003306 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003307 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003308 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003309 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003310 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003311
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003312 if (enable_ept) {
3313 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003315 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003316 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003317 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003318 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003320 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003321 msrs->ept_caps &= vmx_capability.ept;
3322 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003323 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3324 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003325 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003327 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003329 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003330 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003331
Bandan Das27c42a12017-08-03 15:54:42 -04003332 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003334 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003335 /*
3336 * Advertise EPTP switching unconditionally
3337 * since we emulate it
3338 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003339 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003341 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003342 }
3343
Paolo Bonzinief697a72016-03-18 16:58:38 +01003344 /*
3345 * Old versions of KVM use the single-context version without
3346 * checking for support, so declare that it is supported even
3347 * though it is treated as global context. The alternative is
3348 * not failing the single-context invvpid, and it is worse.
3349 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003350 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003351 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003352 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003353 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003354 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003355 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003356
Radim Krčmář0790ec12015-03-17 14:02:32 +01003357 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003359 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3360
Jan Kiszkac18911a2013-03-13 16:06:41 +01003361 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003362 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003363 msrs->misc_low,
3364 msrs->misc_high);
3365 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3366 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003367 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003368 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003369 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003370 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003371
3372 /*
3373 * This MSR reports some information about VMX support. We
3374 * should return information about the VMX we emulate for the
3375 * guest, and the VMCS structure we give it - not about the
3376 * VMX support of the underlying hardware.
3377 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003378 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003379 VMCS12_REVISION |
3380 VMX_BASIC_TRUE_CTLS |
3381 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3382 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3383
3384 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003385 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003386
3387 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003388 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003389 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3390 * We picked the standard core2 setting.
3391 */
3392#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3393#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003394 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3395 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003396
3397 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003398 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3399 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003400
3401 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003402 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003403}
3404
David Matlack38991522016-11-29 18:14:08 -08003405/*
3406 * if fixed0[i] == 1: val[i] must be 1
3407 * if fixed1[i] == 0: val[i] must be 0
3408 */
3409static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3410{
3411 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003412}
3413
3414static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3415{
David Matlack38991522016-11-29 18:14:08 -08003416 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003417}
3418
3419static inline u64 vmx_control_msr(u32 low, u32 high)
3420{
3421 return low | ((u64)high << 32);
3422}
3423
David Matlack62cc6b9d2016-11-29 18:14:07 -08003424static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3425{
3426 superset &= mask;
3427 subset &= mask;
3428
3429 return (superset | subset) == superset;
3430}
3431
3432static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3433{
3434 const u64 feature_and_reserved =
3435 /* feature (except bit 48; see below) */
3436 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3437 /* reserved */
3438 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003439 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003440
3441 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3442 return -EINVAL;
3443
3444 /*
3445 * KVM does not emulate a version of VMX that constrains physical
3446 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3447 */
3448 if (data & BIT_ULL(48))
3449 return -EINVAL;
3450
3451 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3452 vmx_basic_vmcs_revision_id(data))
3453 return -EINVAL;
3454
3455 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3456 return -EINVAL;
3457
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003458 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003459 return 0;
3460}
3461
3462static int
3463vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3464{
3465 u64 supported;
3466 u32 *lowp, *highp;
3467
3468 switch (msr_index) {
3469 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003470 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3471 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003472 break;
3473 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003474 lowp = &vmx->nested.msrs.procbased_ctls_low;
3475 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003476 break;
3477 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003478 lowp = &vmx->nested.msrs.exit_ctls_low;
3479 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003480 break;
3481 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003482 lowp = &vmx->nested.msrs.entry_ctls_low;
3483 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003484 break;
3485 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003486 lowp = &vmx->nested.msrs.secondary_ctls_low;
3487 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003488 break;
3489 default:
3490 BUG();
3491 }
3492
3493 supported = vmx_control_msr(*lowp, *highp);
3494
3495 /* Check must-be-1 bits are still 1. */
3496 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3497 return -EINVAL;
3498
3499 /* Check must-be-0 bits are still 0. */
3500 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3501 return -EINVAL;
3502
3503 *lowp = data;
3504 *highp = data >> 32;
3505 return 0;
3506}
3507
3508static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3509{
3510 const u64 feature_and_reserved_bits =
3511 /* feature */
3512 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3513 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3514 /* reserved */
3515 GENMASK_ULL(13, 9) | BIT_ULL(31);
3516 u64 vmx_misc;
3517
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003518 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3519 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003520
3521 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3522 return -EINVAL;
3523
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003525 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3526 vmx_misc_preemption_timer_rate(data) !=
3527 vmx_misc_preemption_timer_rate(vmx_misc))
3528 return -EINVAL;
3529
3530 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3531 return -EINVAL;
3532
3533 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3534 return -EINVAL;
3535
3536 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3537 return -EINVAL;
3538
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 vmx->nested.msrs.misc_low = data;
3540 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003541
3542 /*
3543 * If L1 has read-only VM-exit information fields, use the
3544 * less permissive vmx_vmwrite_bitmap to specify write
3545 * permissions for the shadow VMCS.
3546 */
3547 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3548 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3549
David Matlack62cc6b9d2016-11-29 18:14:07 -08003550 return 0;
3551}
3552
3553static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3554{
3555 u64 vmx_ept_vpid_cap;
3556
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003557 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3558 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003559
3560 /* Every bit is either reserved or a feature bit. */
3561 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3562 return -EINVAL;
3563
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 vmx->nested.msrs.ept_caps = data;
3565 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003566 return 0;
3567}
3568
3569static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3570{
3571 u64 *msr;
3572
3573 switch (msr_index) {
3574 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003575 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003576 break;
3577 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003579 break;
3580 default:
3581 BUG();
3582 }
3583
3584 /*
3585 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3586 * must be 1 in the restored value.
3587 */
3588 if (!is_bitwise_subset(data, *msr, -1ULL))
3589 return -EINVAL;
3590
3591 *msr = data;
3592 return 0;
3593}
3594
3595/*
3596 * Called when userspace is restoring VMX MSRs.
3597 *
3598 * Returns 0 on success, non-0 otherwise.
3599 */
3600static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3601{
3602 struct vcpu_vmx *vmx = to_vmx(vcpu);
3603
Jim Mattsona943ac52018-05-29 09:11:32 -07003604 /*
3605 * Don't allow changes to the VMX capability MSRs while the vCPU
3606 * is in VMX operation.
3607 */
3608 if (vmx->nested.vmxon)
3609 return -EBUSY;
3610
David Matlack62cc6b9d2016-11-29 18:14:07 -08003611 switch (msr_index) {
3612 case MSR_IA32_VMX_BASIC:
3613 return vmx_restore_vmx_basic(vmx, data);
3614 case MSR_IA32_VMX_PINBASED_CTLS:
3615 case MSR_IA32_VMX_PROCBASED_CTLS:
3616 case MSR_IA32_VMX_EXIT_CTLS:
3617 case MSR_IA32_VMX_ENTRY_CTLS:
3618 /*
3619 * The "non-true" VMX capability MSRs are generated from the
3620 * "true" MSRs, so we do not support restoring them directly.
3621 *
3622 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3623 * should restore the "true" MSRs with the must-be-1 bits
3624 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3625 * DEFAULT SETTINGS".
3626 */
3627 return -EINVAL;
3628 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3629 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3630 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3631 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3632 case MSR_IA32_VMX_PROCBASED_CTLS2:
3633 return vmx_restore_control_msr(vmx, msr_index, data);
3634 case MSR_IA32_VMX_MISC:
3635 return vmx_restore_vmx_misc(vmx, data);
3636 case MSR_IA32_VMX_CR0_FIXED0:
3637 case MSR_IA32_VMX_CR4_FIXED0:
3638 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3639 case MSR_IA32_VMX_CR0_FIXED1:
3640 case MSR_IA32_VMX_CR4_FIXED1:
3641 /*
3642 * These MSRs are generated based on the vCPU's CPUID, so we
3643 * do not support restoring them directly.
3644 */
3645 return -EINVAL;
3646 case MSR_IA32_VMX_EPT_VPID_CAP:
3647 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3648 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003650 return 0;
3651 default:
3652 /*
3653 * The rest of the VMX capability MSRs do not support restore.
3654 */
3655 return -EINVAL;
3656 }
3657}
3658
Jan Kiszkacae50132014-01-04 18:47:22 +01003659/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003661{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003662 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003663 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003664 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003665 break;
3666 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3667 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003668 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003669 msrs->pinbased_ctls_low,
3670 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003671 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3672 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003673 break;
3674 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3675 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003676 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003677 msrs->procbased_ctls_low,
3678 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003679 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3680 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003681 break;
3682 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3683 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003684 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->exit_ctls_low,
3686 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003687 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3688 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003689 break;
3690 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3691 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003692 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 msrs->entry_ctls_low,
3694 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003695 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3696 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003697 break;
3698 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003699 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003700 msrs->misc_low,
3701 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003702 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003704 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003705 break;
3706 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003707 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708 break;
3709 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003710 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003711 break;
3712 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003713 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003714 break;
3715 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003716 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717 break;
3718 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003719 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003720 msrs->secondary_ctls_low,
3721 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003722 break;
3723 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003724 *pdata = msrs->ept_caps |
3725 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003726 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003727 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003728 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003729 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003730 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003731 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003732 }
3733
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003734 return 0;
3735}
3736
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003737static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3738 uint64_t val)
3739{
3740 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3741
3742 return !(val & ~valid_bits);
3743}
3744
Tom Lendacky801e4592018-02-21 13:39:51 -06003745static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3746{
Paolo Bonzini13893092018-02-26 13:40:09 +01003747 switch (msr->index) {
3748 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3749 if (!nested)
3750 return 1;
3751 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3752 default:
3753 return 1;
3754 }
3755
3756 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003757}
3758
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003759/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760 * Reads an msr value (of 'msr_index') into 'pdata'.
3761 * Returns 0 on success, non-0 otherwise.
3762 * Assumes vcpu_load() was already called.
3763 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003764static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003765{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003766 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003767 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003769 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003770#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003772 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 break;
3774 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003775 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003777 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003778 vmx_load_host_state(vmx);
3779 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003780 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003781#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003783 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003784 case MSR_IA32_SPEC_CTRL:
3785 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003786 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3787 return 1;
3788
3789 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3790 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003791 case MSR_IA32_ARCH_CAPABILITIES:
3792 if (!msr_info->host_initiated &&
3793 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3794 return 1;
3795 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3796 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003798 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 break;
3800 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003801 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 break;
3803 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003804 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003806 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003807 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003808 (!msr_info->host_initiated &&
3809 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003810 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003811 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003812 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003813 case MSR_IA32_MCG_EXT_CTL:
3814 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003815 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003816 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003817 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003818 msr_info->data = vcpu->arch.mcg_ext_ctl;
3819 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003820 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003821 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003822 break;
3823 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3824 if (!nested_vmx_allowed(vcpu))
3825 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003826 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3827 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003828 case MSR_IA32_XSS:
3829 if (!vmx_xsaves_supported())
3830 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003831 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003832 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003833 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003834 if (!msr_info->host_initiated &&
3835 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003836 return 1;
3837 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003839 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003840 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003841 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003842 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003844 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 }
3846
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 return 0;
3848}
3849
Jan Kiszkacae50132014-01-04 18:47:22 +01003850static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3851
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852/*
3853 * Writes msr value into into the appropriate "register".
3854 * Returns 0 on success, non-0 otherwise.
3855 * Assumes vcpu_load() was already called.
3856 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003857static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003859 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003860 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003861 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003862 u32 msr_index = msr_info->index;
3863 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003864
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003866 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003867 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003868 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003869#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003870 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003871 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 vmcs_writel(GUEST_FS_BASE, data);
3873 break;
3874 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003875 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 vmcs_writel(GUEST_GS_BASE, data);
3877 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003878 case MSR_KERNEL_GS_BASE:
3879 vmx_load_host_state(vmx);
3880 vmx->msr_guest_kernel_gs_base = data;
3881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882#endif
3883 case MSR_IA32_SYSENTER_CS:
3884 vmcs_write32(GUEST_SYSENTER_CS, data);
3885 break;
3886 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003887 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 break;
3889 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003890 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003892 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003893 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003894 (!msr_info->host_initiated &&
3895 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003896 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003897 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003898 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003900 vmcs_write64(GUEST_BNDCFGS, data);
3901 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003902 case MSR_IA32_SPEC_CTRL:
3903 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003904 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3905 return 1;
3906
3907 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003908 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003909 return 1;
3910
3911 vmx->spec_ctrl = data;
3912
3913 if (!data)
3914 break;
3915
3916 /*
3917 * For non-nested:
3918 * When it's written (to non-zero) for the first time, pass
3919 * it through.
3920 *
3921 * For nested:
3922 * The handling of the MSR bitmap for L2 guests is done in
3923 * nested_vmx_merge_msr_bitmap. We should not touch the
3924 * vmcs02.msr_bitmap here since it gets completely overwritten
3925 * in the merging. We update the vmcs01 here for L1 as well
3926 * since it will end up touching the MSR anyway now.
3927 */
3928 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3929 MSR_IA32_SPEC_CTRL,
3930 MSR_TYPE_RW);
3931 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003932 case MSR_IA32_PRED_CMD:
3933 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003934 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3935 return 1;
3936
3937 if (data & ~PRED_CMD_IBPB)
3938 return 1;
3939
3940 if (!data)
3941 break;
3942
3943 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3944
3945 /*
3946 * For non-nested:
3947 * When it's written (to non-zero) for the first time, pass
3948 * it through.
3949 *
3950 * For nested:
3951 * The handling of the MSR bitmap for L2 guests is done in
3952 * nested_vmx_merge_msr_bitmap. We should not touch the
3953 * vmcs02.msr_bitmap here since it gets completely overwritten
3954 * in the merging.
3955 */
3956 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3957 MSR_TYPE_W);
3958 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003959 case MSR_IA32_ARCH_CAPABILITIES:
3960 if (!msr_info->host_initiated)
3961 return 1;
3962 vmx->arch_capabilities = data;
3963 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003964 case MSR_IA32_CR_PAT:
3965 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003966 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3967 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003968 vmcs_write64(GUEST_IA32_PAT, data);
3969 vcpu->arch.pat = data;
3970 break;
3971 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003972 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003973 break;
Will Auldba904632012-11-29 12:42:50 -08003974 case MSR_IA32_TSC_ADJUST:
3975 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003976 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003977 case MSR_IA32_MCG_EXT_CTL:
3978 if ((!msr_info->host_initiated &&
3979 !(to_vmx(vcpu)->msr_ia32_feature_control &
3980 FEATURE_CONTROL_LMCE)) ||
3981 (data & ~MCG_EXT_CTL_LMCE_EN))
3982 return 1;
3983 vcpu->arch.mcg_ext_ctl = data;
3984 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003985 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003986 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003987 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003988 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3989 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003990 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003991 if (msr_info->host_initiated && data == 0)
3992 vmx_leave_nested(vcpu);
3993 break;
3994 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003995 if (!msr_info->host_initiated)
3996 return 1; /* they are read-only */
3997 if (!nested_vmx_allowed(vcpu))
3998 return 1;
3999 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004000 case MSR_IA32_XSS:
4001 if (!vmx_xsaves_supported())
4002 return 1;
4003 /*
4004 * The only supported bit as of Skylake is bit 8, but
4005 * it is not supported on KVM.
4006 */
4007 if (data != 0)
4008 return 1;
4009 vcpu->arch.ia32_xss = data;
4010 if (vcpu->arch.ia32_xss != host_xss)
4011 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4012 vcpu->arch.ia32_xss, host_xss);
4013 else
4014 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4015 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004016 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004017 if (!msr_info->host_initiated &&
4018 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004019 return 1;
4020 /* Check reserved bit, higher 32 bits should be zero */
4021 if ((data >> 32) != 0)
4022 return 1;
4023 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004025 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004026 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004027 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004028 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004029 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4030 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004031 ret = kvm_set_shared_msr(msr->index, msr->data,
4032 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004033 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004034 if (ret)
4035 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004036 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004037 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004039 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040 }
4041
Eddie Dong2cc51562007-05-21 07:28:09 +03004042 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043}
4044
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004045static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004047 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4048 switch (reg) {
4049 case VCPU_REGS_RSP:
4050 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4051 break;
4052 case VCPU_REGS_RIP:
4053 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4054 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004055 case VCPU_EXREG_PDPTR:
4056 if (enable_ept)
4057 ept_save_pdptrs(vcpu);
4058 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004059 default:
4060 break;
4061 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062}
4063
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064static __init int cpu_has_kvm_support(void)
4065{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004066 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067}
4068
4069static __init int vmx_disabled_by_bios(void)
4070{
4071 u64 msr;
4072
4073 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004074 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004075 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004076 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4077 && tboot_enabled())
4078 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004079 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004080 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004081 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004082 && !tboot_enabled()) {
4083 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004084 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004085 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004086 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004087 /* launched w/o TXT and VMX disabled */
4088 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4089 && !tboot_enabled())
4090 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004091 }
4092
4093 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094}
4095
Dongxiao Xu7725b892010-05-11 18:29:38 +08004096static void kvm_cpu_vmxon(u64 addr)
4097{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004098 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004099 intel_pt_handle_vmx(1);
4100
Dongxiao Xu7725b892010-05-11 18:29:38 +08004101 asm volatile (ASM_VMX_VMXON_RAX
4102 : : "a"(&addr), "m"(addr)
4103 : "memory", "cc");
4104}
4105
Radim Krčmář13a34e02014-08-28 15:13:03 +02004106static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107{
4108 int cpu = raw_smp_processor_id();
4109 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004110 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004112 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004113 return -EBUSY;
4114
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004115 /*
4116 * This can happen if we hot-added a CPU but failed to allocate
4117 * VP assist page for it.
4118 */
4119 if (static_branch_unlikely(&enable_evmcs) &&
4120 !hv_get_vp_assist_page(cpu))
4121 return -EFAULT;
4122
Nadav Har'Eld462b812011-05-24 15:26:10 +03004123 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004124 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4125 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004126
4127 /*
4128 * Now we can enable the vmclear operation in kdump
4129 * since the loaded_vmcss_on_cpu list on this cpu
4130 * has been initialized.
4131 *
4132 * Though the cpu is not in VMX operation now, there
4133 * is no problem to enable the vmclear operation
4134 * for the loaded_vmcss_on_cpu list is empty!
4135 */
4136 crash_enable_local_vmclear(cpu);
4137
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004139
4140 test_bits = FEATURE_CONTROL_LOCKED;
4141 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4142 if (tboot_enabled())
4143 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4144
4145 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004147 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4148 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004149 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004150 if (enable_ept)
4151 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004152
4153 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154}
4155
Nadav Har'Eld462b812011-05-24 15:26:10 +03004156static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004157{
4158 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004159 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004160
Nadav Har'Eld462b812011-05-24 15:26:10 +03004161 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4162 loaded_vmcss_on_cpu_link)
4163 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004164}
4165
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004166
4167/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4168 * tricks.
4169 */
4170static void kvm_cpu_vmxoff(void)
4171{
4172 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004173
4174 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004175 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004176}
4177
Radim Krčmář13a34e02014-08-28 15:13:03 +02004178static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004180 vmclear_local_loaded_vmcss();
4181 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182}
4183
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004184static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004185 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186{
4187 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004188 u32 ctl = ctl_min | ctl_opt;
4189
4190 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4191
4192 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4193 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4194
4195 /* Ensure minimum (required) set of control bits are supported. */
4196 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004197 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004198
4199 *result = ctl;
4200 return 0;
4201}
4202
Avi Kivity110312c2010-12-21 12:54:20 +02004203static __init bool allow_1_setting(u32 msr, u32 ctl)
4204{
4205 u32 vmx_msr_low, vmx_msr_high;
4206
4207 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4208 return vmx_msr_high & ctl;
4209}
4210
Yang, Sheng002c7f72007-07-31 14:23:01 +03004211static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004212{
4213 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004214 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004215 u32 _pin_based_exec_control = 0;
4216 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004217 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004218 u32 _vmexit_control = 0;
4219 u32 _vmentry_control = 0;
4220
Paolo Bonzini13893092018-02-26 13:40:09 +01004221 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304222 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004223#ifdef CONFIG_X86_64
4224 CPU_BASED_CR8_LOAD_EXITING |
4225 CPU_BASED_CR8_STORE_EXITING |
4226#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004227 CPU_BASED_CR3_LOAD_EXITING |
4228 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004229 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004230 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004231 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004232 CPU_BASED_MWAIT_EXITING |
4233 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004234 CPU_BASED_INVLPG_EXITING |
4235 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004236
Sheng Yangf78e0e22007-10-29 09:40:42 +08004237 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004238 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004239 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004240 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4241 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004242 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004243#ifdef CONFIG_X86_64
4244 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4245 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4246 ~CPU_BASED_CR8_STORE_EXITING;
4247#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004248 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004249 min2 = 0;
4250 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004251 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004252 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004253 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004254 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004255 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004256 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004257 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004258 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004259 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004260 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004261 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004262 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004263 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004264 SECONDARY_EXEC_RDSEED_EXITING |
4265 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004266 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004267 SECONDARY_EXEC_TSC_SCALING |
4268 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004269 if (adjust_vmx_controls(min2, opt2,
4270 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004271 &_cpu_based_2nd_exec_control) < 0)
4272 return -EIO;
4273 }
4274#ifndef CONFIG_X86_64
4275 if (!(_cpu_based_2nd_exec_control &
4276 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4277 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4278#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004279
4280 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4281 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004282 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004283 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4284 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004285
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004286 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4287 &vmx_capability.ept, &vmx_capability.vpid);
4288
Sheng Yangd56f5462008-04-25 10:13:16 +08004289 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004290 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4291 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004292 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4293 CPU_BASED_CR3_STORE_EXITING |
4294 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004295 } else if (vmx_capability.ept) {
4296 vmx_capability.ept = 0;
4297 pr_warn_once("EPT CAP should not exist if not support "
4298 "1-setting enable EPT VM-execution control\n");
4299 }
4300 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4301 vmx_capability.vpid) {
4302 vmx_capability.vpid = 0;
4303 pr_warn_once("VPID CAP should not exist if not support "
4304 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004305 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004306
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004307 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004308#ifdef CONFIG_X86_64
4309 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4310#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004311 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004312 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004313 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4314 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004315 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004316
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004317 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4318 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4319 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004320 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4321 &_pin_based_exec_control) < 0)
4322 return -EIO;
4323
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004324 if (cpu_has_broken_vmx_preemption_timer())
4325 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004326 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004327 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004328 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4329
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004330 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004331 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004332 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4333 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004336 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004337
4338 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4339 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004340 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004341
4342#ifdef CONFIG_X86_64
4343 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4344 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004345 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004346#endif
4347
4348 /* Require Write-Back (WB) memory type for VMCS accesses. */
4349 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004350 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004351
Yang, Sheng002c7f72007-07-31 14:23:01 +03004352 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004353 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004354 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004355
Liran Alon2307af12018-06-29 22:59:04 +03004356 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004357
Yang, Sheng002c7f72007-07-31 14:23:01 +03004358 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4359 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004360 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004361 vmcs_conf->vmexit_ctrl = _vmexit_control;
4362 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004363
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004364 if (static_branch_unlikely(&enable_evmcs))
4365 evmcs_sanitize_exec_ctrls(vmcs_conf);
4366
Avi Kivity110312c2010-12-21 12:54:20 +02004367 cpu_has_load_ia32_efer =
4368 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4369 VM_ENTRY_LOAD_IA32_EFER)
4370 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4371 VM_EXIT_LOAD_IA32_EFER);
4372
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004373 cpu_has_load_perf_global_ctrl =
4374 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4375 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4376 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4377 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4378
4379 /*
4380 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004381 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004382 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4383 *
4384 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4385 *
4386 * AAK155 (model 26)
4387 * AAP115 (model 30)
4388 * AAT100 (model 37)
4389 * BC86,AAY89,BD102 (model 44)
4390 * BA97 (model 46)
4391 *
4392 */
4393 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4394 switch (boot_cpu_data.x86_model) {
4395 case 26:
4396 case 30:
4397 case 37:
4398 case 44:
4399 case 46:
4400 cpu_has_load_perf_global_ctrl = false;
4401 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4402 "does not work properly. Using workaround\n");
4403 break;
4404 default:
4405 break;
4406 }
4407 }
4408
Borislav Petkov782511b2016-04-04 22:25:03 +02004409 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004410 rdmsrl(MSR_IA32_XSS, host_xss);
4411
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004412 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004413}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414
4415static struct vmcs *alloc_vmcs_cpu(int cpu)
4416{
4417 int node = cpu_to_node(cpu);
4418 struct page *pages;
4419 struct vmcs *vmcs;
4420
Vlastimil Babka96db8002015-09-08 15:03:50 -07004421 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422 if (!pages)
4423 return NULL;
4424 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004425 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004426
4427 /* KVM supports Enlightened VMCS v1 only */
4428 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004429 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004430 else
Liran Alon392b2f22018-06-23 02:35:01 +03004431 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004432
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 return vmcs;
4434}
4435
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436static void free_vmcs(struct vmcs *vmcs)
4437{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004438 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439}
4440
Nadav Har'Eld462b812011-05-24 15:26:10 +03004441/*
4442 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4443 */
4444static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4445{
4446 if (!loaded_vmcs->vmcs)
4447 return;
4448 loaded_vmcs_clear(loaded_vmcs);
4449 free_vmcs(loaded_vmcs->vmcs);
4450 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004451 if (loaded_vmcs->msr_bitmap)
4452 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004453 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454}
4455
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004456static struct vmcs *alloc_vmcs(void)
4457{
4458 return alloc_vmcs_cpu(raw_smp_processor_id());
4459}
4460
4461static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4462{
4463 loaded_vmcs->vmcs = alloc_vmcs();
4464 if (!loaded_vmcs->vmcs)
4465 return -ENOMEM;
4466
4467 loaded_vmcs->shadow_vmcs = NULL;
4468 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004469
4470 if (cpu_has_vmx_msr_bitmap()) {
4471 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4472 if (!loaded_vmcs->msr_bitmap)
4473 goto out_vmcs;
4474 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004475
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004476 if (IS_ENABLED(CONFIG_HYPERV) &&
4477 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004478 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4479 struct hv_enlightened_vmcs *evmcs =
4480 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4481
4482 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4483 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004484 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004485 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004486
4487out_vmcs:
4488 free_loaded_vmcs(loaded_vmcs);
4489 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004490}
4491
Sam Ravnborg39959582007-06-01 00:47:13 -07004492static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493{
4494 int cpu;
4495
Zachary Amsden3230bb42009-09-29 11:38:37 -10004496 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004498 per_cpu(vmxarea, cpu) = NULL;
4499 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500}
4501
Jim Mattsond37f4262017-12-22 12:12:16 -08004502enum vmcs_field_width {
4503 VMCS_FIELD_WIDTH_U16 = 0,
4504 VMCS_FIELD_WIDTH_U64 = 1,
4505 VMCS_FIELD_WIDTH_U32 = 2,
4506 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004507};
4508
Jim Mattsond37f4262017-12-22 12:12:16 -08004509static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004510{
4511 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004512 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004513 return (field >> 13) & 0x3 ;
4514}
4515
4516static inline int vmcs_field_readonly(unsigned long field)
4517{
4518 return (((field >> 10) & 0x3) == 1);
4519}
4520
Bandan Dasfe2b2012014-04-21 15:20:14 -04004521static void init_vmcs_shadow_fields(void)
4522{
4523 int i, j;
4524
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004525 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4526 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004527 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004528 (i + 1 == max_shadow_read_only_fields ||
4529 shadow_read_only_fields[i + 1] != field + 1))
4530 pr_err("Missing field from shadow_read_only_field %x\n",
4531 field + 1);
4532
4533 clear_bit(field, vmx_vmread_bitmap);
4534#ifdef CONFIG_X86_64
4535 if (field & 1)
4536 continue;
4537#endif
4538 if (j < i)
4539 shadow_read_only_fields[j] = field;
4540 j++;
4541 }
4542 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004543
4544 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004545 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004546 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004547 (i + 1 == max_shadow_read_write_fields ||
4548 shadow_read_write_fields[i + 1] != field + 1))
4549 pr_err("Missing field from shadow_read_write_field %x\n",
4550 field + 1);
4551
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004552 /*
4553 * PML and the preemption timer can be emulated, but the
4554 * processor cannot vmwrite to fields that don't exist
4555 * on bare metal.
4556 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004557 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004558 case GUEST_PML_INDEX:
4559 if (!cpu_has_vmx_pml())
4560 continue;
4561 break;
4562 case VMX_PREEMPTION_TIMER_VALUE:
4563 if (!cpu_has_vmx_preemption_timer())
4564 continue;
4565 break;
4566 case GUEST_INTR_STATUS:
4567 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004568 continue;
4569 break;
4570 default:
4571 break;
4572 }
4573
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004574 clear_bit(field, vmx_vmwrite_bitmap);
4575 clear_bit(field, vmx_vmread_bitmap);
4576#ifdef CONFIG_X86_64
4577 if (field & 1)
4578 continue;
4579#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004580 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004581 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004582 j++;
4583 }
4584 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004585}
4586
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587static __init int alloc_kvm_area(void)
4588{
4589 int cpu;
4590
Zachary Amsden3230bb42009-09-29 11:38:37 -10004591 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004592 struct vmcs *vmcs;
4593
4594 vmcs = alloc_vmcs_cpu(cpu);
4595 if (!vmcs) {
4596 free_kvm_area();
4597 return -ENOMEM;
4598 }
4599
Liran Alon2307af12018-06-29 22:59:04 +03004600 /*
4601 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4602 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4603 * revision_id reported by MSR_IA32_VMX_BASIC.
4604 *
4605 * However, even though not explictly documented by
4606 * TLFS, VMXArea passed as VMXON argument should
4607 * still be marked with revision_id reported by
4608 * physical CPU.
4609 */
4610 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004611 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004612
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613 per_cpu(vmxarea, cpu) = vmcs;
4614 }
4615 return 0;
4616}
4617
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004618static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004619 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004621 if (!emulate_invalid_guest_state) {
4622 /*
4623 * CS and SS RPL should be equal during guest entry according
4624 * to VMX spec, but in reality it is not always so. Since vcpu
4625 * is in the middle of the transition from real mode to
4626 * protected mode it is safe to assume that RPL 0 is a good
4627 * default value.
4628 */
4629 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004630 save->selector &= ~SEGMENT_RPL_MASK;
4631 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004632 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004634 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635}
4636
4637static void enter_pmode(struct kvm_vcpu *vcpu)
4638{
4639 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641
Gleb Natapovd99e4152012-12-20 16:57:45 +02004642 /*
4643 * Update real mode segment cache. It may be not up-to-date if sement
4644 * register was written while vcpu was in a guest mode.
4645 */
4646 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4647 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4648 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4649 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4650 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4651 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4652
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004653 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654
Avi Kivity2fb92db2011-04-27 19:42:18 +03004655 vmx_segment_cache_clear(vmx);
4656
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004657 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004658
4659 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004660 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4661 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662 vmcs_writel(GUEST_RFLAGS, flags);
4663
Rusty Russell66aee912007-07-17 23:34:16 +10004664 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4665 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666
4667 update_exception_bitmap(vcpu);
4668
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004669 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4670 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4671 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4672 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4673 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4674 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675}
4676
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004677static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678{
Mathias Krause772e0312012-08-30 01:30:19 +02004679 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004680 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681
Gleb Natapovd99e4152012-12-20 16:57:45 +02004682 var.dpl = 0x3;
4683 if (seg == VCPU_SREG_CS)
4684 var.type = 0x3;
4685
4686 if (!emulate_invalid_guest_state) {
4687 var.selector = var.base >> 4;
4688 var.base = var.base & 0xffff0;
4689 var.limit = 0xffff;
4690 var.g = 0;
4691 var.db = 0;
4692 var.present = 1;
4693 var.s = 1;
4694 var.l = 0;
4695 var.unusable = 0;
4696 var.type = 0x3;
4697 var.avl = 0;
4698 if (save->base & 0xf)
4699 printk_once(KERN_WARNING "kvm: segment base is not "
4700 "paragraph aligned when entering "
4701 "protected mode (seg=%d)", seg);
4702 }
4703
4704 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004705 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004706 vmcs_write32(sf->limit, var.limit);
4707 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708}
4709
4710static void enter_rmode(struct kvm_vcpu *vcpu)
4711{
4712 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004714 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004716 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4717 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4718 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004723
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004724 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725
Gleb Natapov776e58e2011-03-13 12:34:27 +02004726 /*
4727 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004728 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004729 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004730 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004731 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4732 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004733
Avi Kivity2fb92db2011-04-27 19:42:18 +03004734 vmx_segment_cache_clear(vmx);
4735
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004736 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4739
4740 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004741 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004743 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744
4745 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004746 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747 update_exception_bitmap(vcpu);
4748
Gleb Natapovd99e4152012-12-20 16:57:45 +02004749 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4750 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4751 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4752 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4753 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4754 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004755
Eddie Dong8668a3c2007-10-10 14:26:45 +08004756 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757}
4758
Amit Shah401d10d2009-02-20 22:53:37 +05304759static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4760{
4761 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004762 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4763
4764 if (!msr)
4765 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304766
Avi Kivity44ea2b12009-09-06 15:55:37 +03004767 /*
4768 * Force kernel_gs_base reloading before EFER changes, as control
4769 * of this msr depends on is_long_mode().
4770 */
4771 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004772 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304773 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004774 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304775 msr->data = efer;
4776 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004777 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304778
4779 msr->data = efer & ~EFER_LME;
4780 }
4781 setup_msrs(vmx);
4782}
4783
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004784#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785
4786static void enter_lmode(struct kvm_vcpu *vcpu)
4787{
4788 u32 guest_tr_ar;
4789
Avi Kivity2fb92db2011-04-27 19:42:18 +03004790 vmx_segment_cache_clear(to_vmx(vcpu));
4791
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004793 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004794 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4795 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004797 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4798 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 }
Avi Kivityda38f432010-07-06 11:30:49 +03004800 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801}
4802
4803static void exit_lmode(struct kvm_vcpu *vcpu)
4804{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004805 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004806 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004807}
4808
4809#endif
4810
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004811static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4812 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004813{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004814 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004815 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4816 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004817 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004818 } else {
4819 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004820 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004821}
4822
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004823static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004824{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004825 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004826}
4827
Avi Kivitye8467fd2009-12-29 18:43:06 +02004828static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4829{
4830 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4831
4832 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4833 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4834}
4835
Avi Kivityaff48ba2010-12-05 18:56:11 +02004836static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4837{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004838 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004839 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4840 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4841}
4842
Anthony Liguori25c4c272007-04-27 09:29:21 +03004843static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004844{
Avi Kivityfc78f512009-12-07 12:16:48 +02004845 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4846
4847 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4848 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004849}
4850
Sheng Yang14394422008-04-28 12:24:45 +08004851static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4852{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004853 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4854
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004855 if (!test_bit(VCPU_EXREG_PDPTR,
4856 (unsigned long *)&vcpu->arch.regs_dirty))
4857 return;
4858
Sheng Yang14394422008-04-28 12:24:45 +08004859 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004860 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4861 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4862 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4863 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004864 }
4865}
4866
Avi Kivity8f5d5492009-05-31 18:41:29 +03004867static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4868{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004869 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4870
Avi Kivity8f5d5492009-05-31 18:41:29 +03004871 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004872 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4873 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4874 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4875 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004876 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004877
4878 __set_bit(VCPU_EXREG_PDPTR,
4879 (unsigned long *)&vcpu->arch.regs_avail);
4880 __set_bit(VCPU_EXREG_PDPTR,
4881 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004882}
4883
David Matlack38991522016-11-29 18:14:08 -08004884static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4885{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004886 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4887 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004888 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4889
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004890 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004891 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4892 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4893 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4894
4895 return fixed_bits_valid(val, fixed0, fixed1);
4896}
4897
4898static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4899{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004900 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4901 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004902
4903 return fixed_bits_valid(val, fixed0, fixed1);
4904}
4905
4906static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4907{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004908 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4909 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004910
4911 return fixed_bits_valid(val, fixed0, fixed1);
4912}
4913
4914/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4915#define nested_guest_cr4_valid nested_cr4_valid
4916#define nested_host_cr4_valid nested_cr4_valid
4917
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004918static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004919
4920static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4921 unsigned long cr0,
4922 struct kvm_vcpu *vcpu)
4923{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004924 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4925 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004926 if (!(cr0 & X86_CR0_PG)) {
4927 /* From paging/starting to nonpaging */
4928 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004929 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004930 (CPU_BASED_CR3_LOAD_EXITING |
4931 CPU_BASED_CR3_STORE_EXITING));
4932 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004933 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004934 } else if (!is_paging(vcpu)) {
4935 /* From nonpaging to paging */
4936 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004937 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004938 ~(CPU_BASED_CR3_LOAD_EXITING |
4939 CPU_BASED_CR3_STORE_EXITING));
4940 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004941 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004942 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004943
4944 if (!(cr0 & X86_CR0_WP))
4945 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004946}
4947
Avi Kivity6aa8b732006-12-10 02:21:36 -08004948static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4949{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004950 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004951 unsigned long hw_cr0;
4952
Gleb Natapov50378782013-02-04 16:00:28 +02004953 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004954 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004955 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004956 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004957 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004958
Gleb Natapov218e7632013-01-21 15:36:45 +02004959 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4960 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961
Gleb Natapov218e7632013-01-21 15:36:45 +02004962 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4963 enter_rmode(vcpu);
4964 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004966#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004967 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004968 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004970 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 exit_lmode(vcpu);
4972 }
4973#endif
4974
Sean Christophersonb4d18512018-03-05 12:04:40 -08004975 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004976 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4977
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004979 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004980 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004981
4982 /* depends on vcpu->arch.cr0 to be set to a new value */
4983 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984}
4985
Yu Zhang855feb62017-08-24 20:27:55 +08004986static int get_ept_level(struct kvm_vcpu *vcpu)
4987{
4988 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4989 return 5;
4990 return 4;
4991}
4992
Peter Feiner995f00a2017-06-30 17:26:32 -07004993static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004994{
Yu Zhang855feb62017-08-24 20:27:55 +08004995 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004996
Yu Zhang855feb62017-08-24 20:27:55 +08004997 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004998
Peter Feiner995f00a2017-06-30 17:26:32 -07004999 if (enable_ept_ad_bits &&
5000 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005001 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005002 eptp |= (root_hpa & PAGE_MASK);
5003
5004 return eptp;
5005}
5006
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5008{
Sheng Yang14394422008-04-28 12:24:45 +08005009 unsigned long guest_cr3;
5010 u64 eptp;
5011
5012 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005013 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005014 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005015 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005016 if (enable_unrestricted_guest || is_paging(vcpu) ||
5017 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005018 guest_cr3 = kvm_read_cr3(vcpu);
5019 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005020 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005021 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005022 }
5023
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005024 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005025 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026}
5027
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005028static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005030 /*
5031 * Pass through host's Machine Check Enable value to hw_cr4, which
5032 * is in force while we are in guest mode. Do not let guests control
5033 * this bit, even if host CR4.MCE == 0.
5034 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005035 unsigned long hw_cr4;
5036
5037 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5038 if (enable_unrestricted_guest)
5039 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5040 else if (to_vmx(vcpu)->rmode.vm86_active)
5041 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5042 else
5043 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005044
Sean Christopherson64f7a112018-04-30 10:01:06 -07005045 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5046 if (cr4 & X86_CR4_UMIP) {
5047 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005048 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005049 hw_cr4 &= ~X86_CR4_UMIP;
5050 } else if (!is_guest_mode(vcpu) ||
5051 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5052 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5053 SECONDARY_EXEC_DESC);
5054 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005055
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005056 if (cr4 & X86_CR4_VMXE) {
5057 /*
5058 * To use VMXON (and later other VMX instructions), a guest
5059 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5060 * So basically the check on whether to allow nested VMX
5061 * is here.
5062 */
5063 if (!nested_vmx_allowed(vcpu))
5064 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005065 }
David Matlack38991522016-11-29 18:14:08 -08005066
5067 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005068 return 1;
5069
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005070 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005071
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005072 if (!enable_unrestricted_guest) {
5073 if (enable_ept) {
5074 if (!is_paging(vcpu)) {
5075 hw_cr4 &= ~X86_CR4_PAE;
5076 hw_cr4 |= X86_CR4_PSE;
5077 } else if (!(cr4 & X86_CR4_PAE)) {
5078 hw_cr4 &= ~X86_CR4_PAE;
5079 }
5080 }
5081
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005082 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005083 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5084 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5085 * to be manually disabled when guest switches to non-paging
5086 * mode.
5087 *
5088 * If !enable_unrestricted_guest, the CPU is always running
5089 * with CR0.PG=1 and CR4 needs to be modified.
5090 * If enable_unrestricted_guest, the CPU automatically
5091 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005092 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005093 if (!is_paging(vcpu))
5094 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5095 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005096
Sheng Yang14394422008-04-28 12:24:45 +08005097 vmcs_writel(CR4_READ_SHADOW, cr4);
5098 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005099 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100}
5101
Avi Kivity6aa8b732006-12-10 02:21:36 -08005102static void vmx_get_segment(struct kvm_vcpu *vcpu,
5103 struct kvm_segment *var, int seg)
5104{
Avi Kivitya9179492011-01-03 14:28:52 +02005105 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106 u32 ar;
5107
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005108 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005109 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005110 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005111 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005112 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005113 var->base = vmx_read_guest_seg_base(vmx, seg);
5114 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5115 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005116 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005117 var->base = vmx_read_guest_seg_base(vmx, seg);
5118 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5119 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5120 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005121 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122 var->type = ar & 15;
5123 var->s = (ar >> 4) & 1;
5124 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005125 /*
5126 * Some userspaces do not preserve unusable property. Since usable
5127 * segment has to be present according to VMX spec we can use present
5128 * property to amend userspace bug by making unusable segment always
5129 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5130 * segment as unusable.
5131 */
5132 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133 var->avl = (ar >> 12) & 1;
5134 var->l = (ar >> 13) & 1;
5135 var->db = (ar >> 14) & 1;
5136 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137}
5138
Avi Kivitya9179492011-01-03 14:28:52 +02005139static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5140{
Avi Kivitya9179492011-01-03 14:28:52 +02005141 struct kvm_segment s;
5142
5143 if (to_vmx(vcpu)->rmode.vm86_active) {
5144 vmx_get_segment(vcpu, &s, seg);
5145 return s.base;
5146 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005147 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005148}
5149
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005150static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005151{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005152 struct vcpu_vmx *vmx = to_vmx(vcpu);
5153
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005154 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005155 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005156 else {
5157 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005158 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005159 }
Avi Kivity69c73022011-03-07 15:26:44 +02005160}
5161
Avi Kivity653e3102007-05-07 10:55:37 +03005162static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164 u32 ar;
5165
Avi Kivityf0495f92012-06-07 17:06:10 +03005166 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005167 ar = 1 << 16;
5168 else {
5169 ar = var->type & 15;
5170 ar |= (var->s & 1) << 4;
5171 ar |= (var->dpl & 3) << 5;
5172 ar |= (var->present & 1) << 7;
5173 ar |= (var->avl & 1) << 12;
5174 ar |= (var->l & 1) << 13;
5175 ar |= (var->db & 1) << 14;
5176 ar |= (var->g & 1) << 15;
5177 }
Avi Kivity653e3102007-05-07 10:55:37 +03005178
5179 return ar;
5180}
5181
5182static void vmx_set_segment(struct kvm_vcpu *vcpu,
5183 struct kvm_segment *var, int seg)
5184{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005185 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005186 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005187
Avi Kivity2fb92db2011-04-27 19:42:18 +03005188 vmx_segment_cache_clear(vmx);
5189
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005190 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5191 vmx->rmode.segs[seg] = *var;
5192 if (seg == VCPU_SREG_TR)
5193 vmcs_write16(sf->selector, var->selector);
5194 else if (var->s)
5195 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005196 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005197 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005198
Avi Kivity653e3102007-05-07 10:55:37 +03005199 vmcs_writel(sf->base, var->base);
5200 vmcs_write32(sf->limit, var->limit);
5201 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005202
5203 /*
5204 * Fix the "Accessed" bit in AR field of segment registers for older
5205 * qemu binaries.
5206 * IA32 arch specifies that at the time of processor reset the
5207 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005208 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005209 * state vmexit when "unrestricted guest" mode is turned on.
5210 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5211 * tree. Newer qemu binaries with that qemu fix would not need this
5212 * kvm hack.
5213 */
5214 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005215 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005216
Gleb Natapovf924d662012-12-12 19:10:55 +02005217 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005218
5219out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005220 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005221}
5222
Avi Kivity6aa8b732006-12-10 02:21:36 -08005223static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5224{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005225 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005226
5227 *db = (ar >> 14) & 1;
5228 *l = (ar >> 13) & 1;
5229}
5230
Gleb Natapov89a27f42010-02-16 10:51:48 +02005231static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005232{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005233 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5234 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235}
5236
Gleb Natapov89a27f42010-02-16 10:51:48 +02005237static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005239 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5240 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005241}
5242
Gleb Natapov89a27f42010-02-16 10:51:48 +02005243static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005244{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005245 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5246 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005247}
5248
Gleb Natapov89a27f42010-02-16 10:51:48 +02005249static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005251 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5252 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005253}
5254
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005255static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5256{
5257 struct kvm_segment var;
5258 u32 ar;
5259
5260 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005261 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005262 if (seg == VCPU_SREG_CS)
5263 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005264 ar = vmx_segment_access_rights(&var);
5265
5266 if (var.base != (var.selector << 4))
5267 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005268 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005269 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005270 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005271 return false;
5272
5273 return true;
5274}
5275
5276static bool code_segment_valid(struct kvm_vcpu *vcpu)
5277{
5278 struct kvm_segment cs;
5279 unsigned int cs_rpl;
5280
5281 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005282 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005283
Avi Kivity1872a3f2009-01-04 23:26:52 +02005284 if (cs.unusable)
5285 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005286 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005287 return false;
5288 if (!cs.s)
5289 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005290 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005291 if (cs.dpl > cs_rpl)
5292 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005293 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005294 if (cs.dpl != cs_rpl)
5295 return false;
5296 }
5297 if (!cs.present)
5298 return false;
5299
5300 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5301 return true;
5302}
5303
5304static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5305{
5306 struct kvm_segment ss;
5307 unsigned int ss_rpl;
5308
5309 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005310 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005311
Avi Kivity1872a3f2009-01-04 23:26:52 +02005312 if (ss.unusable)
5313 return true;
5314 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005315 return false;
5316 if (!ss.s)
5317 return false;
5318 if (ss.dpl != ss_rpl) /* DPL != RPL */
5319 return false;
5320 if (!ss.present)
5321 return false;
5322
5323 return true;
5324}
5325
5326static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5327{
5328 struct kvm_segment var;
5329 unsigned int rpl;
5330
5331 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005332 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005333
Avi Kivity1872a3f2009-01-04 23:26:52 +02005334 if (var.unusable)
5335 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005336 if (!var.s)
5337 return false;
5338 if (!var.present)
5339 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005340 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005341 if (var.dpl < rpl) /* DPL < RPL */
5342 return false;
5343 }
5344
5345 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5346 * rights flags
5347 */
5348 return true;
5349}
5350
5351static bool tr_valid(struct kvm_vcpu *vcpu)
5352{
5353 struct kvm_segment tr;
5354
5355 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5356
Avi Kivity1872a3f2009-01-04 23:26:52 +02005357 if (tr.unusable)
5358 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005359 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005361 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005362 return false;
5363 if (!tr.present)
5364 return false;
5365
5366 return true;
5367}
5368
5369static bool ldtr_valid(struct kvm_vcpu *vcpu)
5370{
5371 struct kvm_segment ldtr;
5372
5373 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5374
Avi Kivity1872a3f2009-01-04 23:26:52 +02005375 if (ldtr.unusable)
5376 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005377 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005378 return false;
5379 if (ldtr.type != 2)
5380 return false;
5381 if (!ldtr.present)
5382 return false;
5383
5384 return true;
5385}
5386
5387static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5388{
5389 struct kvm_segment cs, ss;
5390
5391 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5392 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5393
Nadav Amitb32a9912015-03-29 16:33:04 +03005394 return ((cs.selector & SEGMENT_RPL_MASK) ==
5395 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005396}
5397
5398/*
5399 * Check if guest state is valid. Returns true if valid, false if
5400 * not.
5401 * We assume that registers are always usable
5402 */
5403static bool guest_state_valid(struct kvm_vcpu *vcpu)
5404{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005405 if (enable_unrestricted_guest)
5406 return true;
5407
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005408 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005409 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005410 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5411 return false;
5412 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5413 return false;
5414 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5415 return false;
5416 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5417 return false;
5418 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5419 return false;
5420 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5421 return false;
5422 } else {
5423 /* protected mode guest state checks */
5424 if (!cs_ss_rpl_check(vcpu))
5425 return false;
5426 if (!code_segment_valid(vcpu))
5427 return false;
5428 if (!stack_segment_valid(vcpu))
5429 return false;
5430 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5431 return false;
5432 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5433 return false;
5434 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5435 return false;
5436 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5437 return false;
5438 if (!tr_valid(vcpu))
5439 return false;
5440 if (!ldtr_valid(vcpu))
5441 return false;
5442 }
5443 /* TODO:
5444 * - Add checks on RIP
5445 * - Add checks on RFLAGS
5446 */
5447
5448 return true;
5449}
5450
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005451static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5452{
5453 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5454}
5455
Mike Dayd77c26f2007-10-08 09:02:08 -04005456static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005458 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005459 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005460 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005461
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005462 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005463 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
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 = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005468 r = kvm_write_guest_page(kvm, fn++, &data,
5469 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005470 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005471 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005472 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5473 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005474 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005475 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5476 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005477 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005478 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005479 r = kvm_write_guest_page(kvm, fn, &data,
5480 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5481 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005482out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005483 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005484 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005485}
5486
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005487static int init_rmode_identity_map(struct kvm *kvm)
5488{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005489 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005490 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005491 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005492 u32 tmp;
5493
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005494 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005495 mutex_lock(&kvm->slots_lock);
5496
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005497 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005498 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005499
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005500 if (!kvm_vmx->ept_identity_map_addr)
5501 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5502 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005503
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005504 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005505 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005506 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005507 goto out2;
5508
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005509 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005510 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5511 if (r < 0)
5512 goto out;
5513 /* Set up identity-mapping pagetable for EPT in real mode */
5514 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5515 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5516 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5517 r = kvm_write_guest_page(kvm, identity_map_pfn,
5518 &tmp, i * sizeof(tmp), sizeof(tmp));
5519 if (r < 0)
5520 goto out;
5521 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005522 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005523
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005524out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005525 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005526
5527out2:
5528 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005529 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005530}
5531
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532static void seg_setup(int seg)
5533{
Mathias Krause772e0312012-08-30 01:30:19 +02005534 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005535 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536
5537 vmcs_write16(sf->selector, 0);
5538 vmcs_writel(sf->base, 0);
5539 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005540 ar = 0x93;
5541 if (seg == VCPU_SREG_CS)
5542 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005543
5544 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545}
5546
Sheng Yangf78e0e22007-10-29 09:40:42 +08005547static int alloc_apic_access_page(struct kvm *kvm)
5548{
Xiao Guangrong44841412012-09-07 14:14:20 +08005549 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005550 int r = 0;
5551
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005552 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005553 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005554 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005555 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5556 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005557 if (r)
5558 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005559
Tang Chen73a6d942014-09-11 13:38:00 +08005560 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005561 if (is_error_page(page)) {
5562 r = -EFAULT;
5563 goto out;
5564 }
5565
Tang Chenc24ae0d2014-09-24 15:57:58 +08005566 /*
5567 * Do not pin the page in memory, so that memory hot-unplug
5568 * is able to migrate it.
5569 */
5570 put_page(page);
5571 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005572out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005573 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005574 return r;
5575}
5576
Wanpeng Li991e7a02015-09-16 17:30:05 +08005577static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005578{
5579 int vpid;
5580
Avi Kivity919818a2009-03-23 18:01:29 +02005581 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005582 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005583 spin_lock(&vmx_vpid_lock);
5584 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005585 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005586 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005587 else
5588 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005589 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005590 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005591}
5592
Wanpeng Li991e7a02015-09-16 17:30:05 +08005593static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005594{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005595 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005596 return;
5597 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005598 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005599 spin_unlock(&vmx_vpid_lock);
5600}
5601
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005602static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5603 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005604{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005605 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005606
5607 if (!cpu_has_vmx_msr_bitmap())
5608 return;
5609
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005610 if (static_branch_unlikely(&enable_evmcs))
5611 evmcs_touch_msr_bitmap();
5612
Sheng Yang25c5f222008-03-28 13:18:56 +08005613 /*
5614 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5615 * have the write-low and read-high bitmap offsets the wrong way round.
5616 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5617 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005618 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005619 if (type & MSR_TYPE_R)
5620 /* read-low */
5621 __clear_bit(msr, msr_bitmap + 0x000 / f);
5622
5623 if (type & MSR_TYPE_W)
5624 /* write-low */
5625 __clear_bit(msr, msr_bitmap + 0x800 / f);
5626
Sheng Yang25c5f222008-03-28 13:18:56 +08005627 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5628 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005629 if (type & MSR_TYPE_R)
5630 /* read-high */
5631 __clear_bit(msr, msr_bitmap + 0x400 / f);
5632
5633 if (type & MSR_TYPE_W)
5634 /* write-high */
5635 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5636
5637 }
5638}
5639
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005640static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5641 u32 msr, int type)
5642{
5643 int f = sizeof(unsigned long);
5644
5645 if (!cpu_has_vmx_msr_bitmap())
5646 return;
5647
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005648 if (static_branch_unlikely(&enable_evmcs))
5649 evmcs_touch_msr_bitmap();
5650
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005651 /*
5652 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5653 * have the write-low and read-high bitmap offsets the wrong way round.
5654 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5655 */
5656 if (msr <= 0x1fff) {
5657 if (type & MSR_TYPE_R)
5658 /* read-low */
5659 __set_bit(msr, msr_bitmap + 0x000 / f);
5660
5661 if (type & MSR_TYPE_W)
5662 /* write-low */
5663 __set_bit(msr, msr_bitmap + 0x800 / f);
5664
5665 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5666 msr &= 0x1fff;
5667 if (type & MSR_TYPE_R)
5668 /* read-high */
5669 __set_bit(msr, msr_bitmap + 0x400 / f);
5670
5671 if (type & MSR_TYPE_W)
5672 /* write-high */
5673 __set_bit(msr, msr_bitmap + 0xc00 / f);
5674
5675 }
5676}
5677
5678static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5679 u32 msr, int type, bool value)
5680{
5681 if (value)
5682 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5683 else
5684 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5685}
5686
Wincy Vanf2b93282015-02-03 23:56:03 +08005687/*
5688 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5689 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5690 */
5691static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5692 unsigned long *msr_bitmap_nested,
5693 u32 msr, int type)
5694{
5695 int f = sizeof(unsigned long);
5696
Wincy Vanf2b93282015-02-03 23:56:03 +08005697 /*
5698 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5699 * have the write-low and read-high bitmap offsets the wrong way round.
5700 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5701 */
5702 if (msr <= 0x1fff) {
5703 if (type & MSR_TYPE_R &&
5704 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5705 /* read-low */
5706 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5707
5708 if (type & MSR_TYPE_W &&
5709 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5710 /* write-low */
5711 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5712
5713 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5714 msr &= 0x1fff;
5715 if (type & MSR_TYPE_R &&
5716 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5717 /* read-high */
5718 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5719
5720 if (type & MSR_TYPE_W &&
5721 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5722 /* write-high */
5723 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5724
5725 }
5726}
5727
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005728static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005729{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005730 u8 mode = 0;
5731
5732 if (cpu_has_secondary_exec_ctrls() &&
5733 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5734 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5735 mode |= MSR_BITMAP_MODE_X2APIC;
5736 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5737 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5738 }
5739
5740 if (is_long_mode(vcpu))
5741 mode |= MSR_BITMAP_MODE_LM;
5742
5743 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005744}
5745
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005746#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5747
5748static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5749 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005750{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005751 int msr;
5752
5753 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5754 unsigned word = msr / BITS_PER_LONG;
5755 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5756 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005757 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005758
5759 if (mode & MSR_BITMAP_MODE_X2APIC) {
5760 /*
5761 * TPR reads and writes can be virtualized even if virtual interrupt
5762 * delivery is not in use.
5763 */
5764 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5765 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5766 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5767 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5768 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5769 }
5770 }
5771}
5772
5773static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5774{
5775 struct vcpu_vmx *vmx = to_vmx(vcpu);
5776 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5777 u8 mode = vmx_msr_bitmap_mode(vcpu);
5778 u8 changed = mode ^ vmx->msr_bitmap_mode;
5779
5780 if (!changed)
5781 return;
5782
5783 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5784 !(mode & MSR_BITMAP_MODE_LM));
5785
5786 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5787 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5788
5789 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005790}
5791
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005792static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005793{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005794 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005795}
5796
David Matlackc9f04402017-08-01 14:00:40 -07005797static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5798{
5799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5800 gfn_t gfn;
5801
5802 /*
5803 * Don't need to mark the APIC access page dirty; it is never
5804 * written to by the CPU during APIC virtualization.
5805 */
5806
5807 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5808 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5809 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5810 }
5811
5812 if (nested_cpu_has_posted_intr(vmcs12)) {
5813 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5814 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5815 }
5816}
5817
5818
David Hildenbrand6342c502017-01-25 11:58:58 +01005819static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005820{
5821 struct vcpu_vmx *vmx = to_vmx(vcpu);
5822 int max_irr;
5823 void *vapic_page;
5824 u16 status;
5825
David Matlackc9f04402017-08-01 14:00:40 -07005826 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5827 return;
Wincy Van705699a2015-02-03 23:58:17 +08005828
David Matlackc9f04402017-08-01 14:00:40 -07005829 vmx->nested.pi_pending = false;
5830 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5831 return;
Wincy Van705699a2015-02-03 23:58:17 +08005832
David Matlackc9f04402017-08-01 14:00:40 -07005833 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5834 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005835 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005836 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5837 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005838 kunmap(vmx->nested.virtual_apic_page);
5839
5840 status = vmcs_read16(GUEST_INTR_STATUS);
5841 if ((u8)max_irr > ((u8)status & 0xff)) {
5842 status &= ~0xff;
5843 status |= (u8)max_irr;
5844 vmcs_write16(GUEST_INTR_STATUS, status);
5845 }
5846 }
David Matlackc9f04402017-08-01 14:00:40 -07005847
5848 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005849}
5850
Wincy Van06a55242017-04-28 13:13:59 +08005851static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5852 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005853{
5854#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005855 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5856
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005857 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005858 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005859 * The vector of interrupt to be delivered to vcpu had
5860 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005861 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005862 * Following cases will be reached in this block, and
5863 * we always send a notification event in all cases as
5864 * explained below.
5865 *
5866 * Case 1: vcpu keeps in non-root mode. Sending a
5867 * notification event posts the interrupt to vcpu.
5868 *
5869 * Case 2: vcpu exits to root mode and is still
5870 * runnable. PIR will be synced to vIRR before the
5871 * next vcpu entry. Sending a notification event in
5872 * this case has no effect, as vcpu is not in root
5873 * mode.
5874 *
5875 * Case 3: vcpu exits to root mode and is blocked.
5876 * vcpu_block() has already synced PIR to vIRR and
5877 * never blocks vcpu if vIRR is not cleared. Therefore,
5878 * a blocked vcpu here does not wait for any requested
5879 * interrupts in PIR, and sending a notification event
5880 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005881 */
Feng Wu28b835d2015-09-18 22:29:54 +08005882
Wincy Van06a55242017-04-28 13:13:59 +08005883 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005884 return true;
5885 }
5886#endif
5887 return false;
5888}
5889
Wincy Van705699a2015-02-03 23:58:17 +08005890static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5891 int vector)
5892{
5893 struct vcpu_vmx *vmx = to_vmx(vcpu);
5894
5895 if (is_guest_mode(vcpu) &&
5896 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005897 /*
5898 * If a posted intr is not recognized by hardware,
5899 * we will accomplish it in the next vmentry.
5900 */
5901 vmx->nested.pi_pending = true;
5902 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005903 /* the PIR and ON have been set by L1. */
5904 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5905 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005906 return 0;
5907 }
5908 return -1;
5909}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005911 * Send interrupt to vcpu via posted interrupt way.
5912 * 1. If target vcpu is running(non-root mode), send posted interrupt
5913 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5914 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5915 * interrupt from PIR in next vmentry.
5916 */
5917static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5918{
5919 struct vcpu_vmx *vmx = to_vmx(vcpu);
5920 int r;
5921
Wincy Van705699a2015-02-03 23:58:17 +08005922 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5923 if (!r)
5924 return;
5925
Yang Zhanga20ed542013-04-11 19:25:15 +08005926 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5927 return;
5928
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005929 /* If a previous notification has sent the IPI, nothing to do. */
5930 if (pi_test_and_set_on(&vmx->pi_desc))
5931 return;
5932
Wincy Van06a55242017-04-28 13:13:59 +08005933 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005934 kvm_vcpu_kick(vcpu);
5935}
5936
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005938 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5939 * will not change in the lifetime of the guest.
5940 * Note that host-state that does change is set elsewhere. E.g., host-state
5941 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5942 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005943static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005944{
5945 u32 low32, high32;
5946 unsigned long tmpl;
5947 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005948 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005949
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005950 cr0 = read_cr0();
5951 WARN_ON(cr0 & X86_CR0_TS);
5952 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005953
5954 /*
5955 * Save the most likely value for this task's CR3 in the VMCS.
5956 * We can't use __get_current_cr3_fast() because we're not atomic.
5957 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005958 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005959 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005960 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005961
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005962 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005963 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005964 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005965 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005966
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005967 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005968#ifdef CONFIG_X86_64
5969 /*
5970 * Load null selectors, so we can avoid reloading them in
5971 * __vmx_load_host_state(), in case userspace uses the null selectors
5972 * too (the expected case).
5973 */
5974 vmcs_write16(HOST_DS_SELECTOR, 0);
5975 vmcs_write16(HOST_ES_SELECTOR, 0);
5976#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005977 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5978 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005979#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005980 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5981 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5982
Juergen Gross87930012017-09-04 12:25:27 +02005983 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005984 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005985 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005986
Avi Kivity83287ea422012-09-16 15:10:57 +03005987 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005988
5989 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5990 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5991 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5992 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5993
5994 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5995 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5996 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5997 }
5998}
5999
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006000static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6001{
6002 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6003 if (enable_ept)
6004 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006005 if (is_guest_mode(&vmx->vcpu))
6006 vmx->vcpu.arch.cr4_guest_owned_bits &=
6007 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006008 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6009}
6010
Yang Zhang01e439b2013-04-11 19:25:12 +08006011static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6012{
6013 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6014
Andrey Smetanind62caab2015-11-10 15:36:33 +03006015 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006016 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006017
6018 if (!enable_vnmi)
6019 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6020
Yunhong Jiang64672c92016-06-13 14:19:59 -07006021 /* Enable the preemption timer dynamically */
6022 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006023 return pin_based_exec_ctrl;
6024}
6025
Andrey Smetanind62caab2015-11-10 15:36:33 +03006026static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6027{
6028 struct vcpu_vmx *vmx = to_vmx(vcpu);
6029
6030 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006031 if (cpu_has_secondary_exec_ctrls()) {
6032 if (kvm_vcpu_apicv_active(vcpu))
6033 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6034 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6035 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6036 else
6037 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6038 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6039 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6040 }
6041
6042 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006043 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006044}
6045
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006046static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6047{
6048 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006049
6050 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6051 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6052
Paolo Bonzini35754c92015-07-29 12:05:37 +02006053 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006054 exec_control &= ~CPU_BASED_TPR_SHADOW;
6055#ifdef CONFIG_X86_64
6056 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6057 CPU_BASED_CR8_LOAD_EXITING;
6058#endif
6059 }
6060 if (!enable_ept)
6061 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6062 CPU_BASED_CR3_LOAD_EXITING |
6063 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006064 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6065 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6066 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006067 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6068 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006069 return exec_control;
6070}
6071
Jim Mattson45ec3682017-08-23 16:32:04 -07006072static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006073{
Jim Mattson45ec3682017-08-23 16:32:04 -07006074 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006075 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006076}
6077
Jim Mattson75f4fc82017-08-23 16:32:03 -07006078static bool vmx_rdseed_supported(void)
6079{
6080 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006081 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006082}
6083
Paolo Bonzini80154d72017-08-24 13:55:35 +02006084static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006085{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006086 struct kvm_vcpu *vcpu = &vmx->vcpu;
6087
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006088 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006089
Paolo Bonzini80154d72017-08-24 13:55:35 +02006090 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006091 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6092 if (vmx->vpid == 0)
6093 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6094 if (!enable_ept) {
6095 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6096 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006097 /* Enable INVPCID for non-ept guests may cause performance regression. */
6098 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006099 }
6100 if (!enable_unrestricted_guest)
6101 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006102 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006103 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006104 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006105 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6106 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006107 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006108
6109 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6110 * in vmx_set_cr4. */
6111 exec_control &= ~SECONDARY_EXEC_DESC;
6112
Abel Gordonabc4fc52013-04-18 14:35:25 +03006113 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6114 (handle_vmptrld).
6115 We can NOT enable shadow_vmcs here because we don't have yet
6116 a current VMCS12
6117 */
6118 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006119
6120 if (!enable_pml)
6121 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006122
Paolo Bonzini3db13482017-08-24 14:48:03 +02006123 if (vmx_xsaves_supported()) {
6124 /* Exposing XSAVES only when XSAVE is exposed */
6125 bool xsaves_enabled =
6126 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6127 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6128
6129 if (!xsaves_enabled)
6130 exec_control &= ~SECONDARY_EXEC_XSAVES;
6131
6132 if (nested) {
6133 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006134 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006135 SECONDARY_EXEC_XSAVES;
6136 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006137 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006138 ~SECONDARY_EXEC_XSAVES;
6139 }
6140 }
6141
Paolo Bonzini80154d72017-08-24 13:55:35 +02006142 if (vmx_rdtscp_supported()) {
6143 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6144 if (!rdtscp_enabled)
6145 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6146
6147 if (nested) {
6148 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006149 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006150 SECONDARY_EXEC_RDTSCP;
6151 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006152 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006153 ~SECONDARY_EXEC_RDTSCP;
6154 }
6155 }
6156
6157 if (vmx_invpcid_supported()) {
6158 /* Exposing INVPCID only when PCID is exposed */
6159 bool invpcid_enabled =
6160 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6161 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6162
6163 if (!invpcid_enabled) {
6164 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6165 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6166 }
6167
6168 if (nested) {
6169 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006170 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006171 SECONDARY_EXEC_ENABLE_INVPCID;
6172 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006173 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006174 ~SECONDARY_EXEC_ENABLE_INVPCID;
6175 }
6176 }
6177
Jim Mattson45ec3682017-08-23 16:32:04 -07006178 if (vmx_rdrand_supported()) {
6179 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6180 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006181 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006182
6183 if (nested) {
6184 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006185 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006186 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006187 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006188 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006189 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006190 }
6191 }
6192
Jim Mattson75f4fc82017-08-23 16:32:03 -07006193 if (vmx_rdseed_supported()) {
6194 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6195 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006196 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006197
6198 if (nested) {
6199 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006200 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006201 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006202 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006203 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006204 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006205 }
6206 }
6207
Paolo Bonzini80154d72017-08-24 13:55:35 +02006208 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006209}
6210
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006211static void ept_set_mmio_spte_mask(void)
6212{
6213 /*
6214 * EPT Misconfigurations can be generated if the value of bits 2:0
6215 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006216 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006217 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6218 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006219}
6220
Wanpeng Lif53cd632014-12-02 19:14:58 +08006221#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006222/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223 * Sets up the vmcs for emulated real mode.
6224 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006225static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006226{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006227#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006228 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006229#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006230 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231
Abel Gordon4607c2d2013-04-18 14:35:55 +03006232 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006233 /*
6234 * At vCPU creation, "VMWRITE to any supported field
6235 * in the VMCS" is supported, so use the more
6236 * permissive vmx_vmread_bitmap to specify both read
6237 * and write permissions for the shadow VMCS.
6238 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006239 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006240 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006241 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006242 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006243 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006244
Avi Kivity6aa8b732006-12-10 02:21:36 -08006245 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6246
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006248 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006249 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006250
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006251 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252
Dan Williamsdfa169b2016-06-02 11:17:24 -07006253 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006254 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006255 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006256 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006257 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006258
Andrey Smetanind62caab2015-11-10 15:36:33 +03006259 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006260 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6261 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6262 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6263 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6264
6265 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006266
Li RongQing0bcf2612015-12-03 13:29:34 +08006267 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006268 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006269 }
6270
Wanpeng Lib31c1142018-03-12 04:53:04 -07006271 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006272 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006273 vmx->ple_window = ple_window;
6274 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006275 }
6276
Xiao Guangrongc3707952011-07-12 03:28:04 +08006277 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6278 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006279 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6280
Avi Kivity9581d442010-10-19 16:46:55 +02006281 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6282 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006283 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006284#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006285 rdmsrl(MSR_FS_BASE, a);
6286 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6287 rdmsrl(MSR_GS_BASE, a);
6288 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6289#else
6290 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6291 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6292#endif
6293
Bandan Das2a499e42017-08-03 15:54:41 -04006294 if (cpu_has_vmx_vmfunc())
6295 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6296
Eddie Dong2cc51562007-05-21 07:28:09 +03006297 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6298 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006299 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006300 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006301 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006302
Radim Krčmář74545702015-04-27 15:11:25 +02006303 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6304 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006305
Paolo Bonzini03916db2014-07-24 14:21:57 +02006306 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006307 u32 index = vmx_msr_index[i];
6308 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006309 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310
6311 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6312 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006313 if (wrmsr_safe(index, data_low, data_high) < 0)
6314 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006315 vmx->guest_msrs[j].index = i;
6316 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006317 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006318 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006321 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6322 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006323
6324 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
6326 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006327 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006328
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006329 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6330 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6331
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006332 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006333
Wanpeng Lif53cd632014-12-02 19:14:58 +08006334 if (vmx_xsaves_supported())
6335 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6336
Peter Feiner4e595162016-07-07 14:49:58 -07006337 if (enable_pml) {
6338 ASSERT(vmx->pml_pg);
6339 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6340 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6341 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006342}
6343
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006344static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006345{
6346 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006347 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006348 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006349
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006350 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006351 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006352
Wanpeng Li518e7b92018-02-28 14:03:31 +08006353 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006354 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006355 kvm_set_cr8(vcpu, 0);
6356
6357 if (!init_event) {
6358 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6359 MSR_IA32_APICBASE_ENABLE;
6360 if (kvm_vcpu_is_reset_bsp(vcpu))
6361 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6362 apic_base_msr.host_initiated = true;
6363 kvm_set_apic_base(vcpu, &apic_base_msr);
6364 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006365
Avi Kivity2fb92db2011-04-27 19:42:18 +03006366 vmx_segment_cache_clear(vmx);
6367
Avi Kivity5706be02008-08-20 15:07:31 +03006368 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006369 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006370 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006371
6372 seg_setup(VCPU_SREG_DS);
6373 seg_setup(VCPU_SREG_ES);
6374 seg_setup(VCPU_SREG_FS);
6375 seg_setup(VCPU_SREG_GS);
6376 seg_setup(VCPU_SREG_SS);
6377
6378 vmcs_write16(GUEST_TR_SELECTOR, 0);
6379 vmcs_writel(GUEST_TR_BASE, 0);
6380 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6381 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6382
6383 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6384 vmcs_writel(GUEST_LDTR_BASE, 0);
6385 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6386 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6387
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006388 if (!init_event) {
6389 vmcs_write32(GUEST_SYSENTER_CS, 0);
6390 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6391 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6392 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6393 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006394
Wanpeng Lic37c2872017-11-20 14:52:21 -08006395 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006396 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006397
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006398 vmcs_writel(GUEST_GDTR_BASE, 0);
6399 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6400
6401 vmcs_writel(GUEST_IDTR_BASE, 0);
6402 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6403
Anthony Liguori443381a2010-12-06 10:53:38 -06006404 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006405 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006406 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006407 if (kvm_mpx_supported())
6408 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006409
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006410 setup_msrs(vmx);
6411
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6413
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006414 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006415 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006416 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006417 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006418 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006419 vmcs_write32(TPR_THRESHOLD, 0);
6420 }
6421
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006422 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006423
Sheng Yang2384d2b2008-01-17 15:14:33 +08006424 if (vmx->vpid != 0)
6425 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6426
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006427 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006428 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006429 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006430 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006431 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006432
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006433 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006434
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006435 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006436 if (init_event)
6437 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006438}
6439
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006440/*
6441 * In nested virtualization, check if L1 asked to exit on external interrupts.
6442 * For most existing hypervisors, this will always return true.
6443 */
6444static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6445{
6446 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6447 PIN_BASED_EXT_INTR_MASK;
6448}
6449
Bandan Das77b0f5d2014-04-19 18:17:45 -04006450/*
6451 * In nested virtualization, check if L1 has set
6452 * VM_EXIT_ACK_INTR_ON_EXIT
6453 */
6454static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6455{
6456 return get_vmcs12(vcpu)->vm_exit_controls &
6457 VM_EXIT_ACK_INTR_ON_EXIT;
6458}
6459
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006460static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6461{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006462 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006463}
6464
Jan Kiszkac9a79532014-03-07 20:03:15 +01006465static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006466{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006467 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6468 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006469}
6470
Jan Kiszkac9a79532014-03-07 20:03:15 +01006471static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006472{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006473 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006474 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006475 enable_irq_window(vcpu);
6476 return;
6477 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006478
Paolo Bonzini47c01522016-12-19 11:44:07 +01006479 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6480 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006481}
6482
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006483static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006484{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006485 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006486 uint32_t intr;
6487 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006488
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006489 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006490
Avi Kivityfa89a812008-09-01 15:57:51 +03006491 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006492 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006493 int inc_eip = 0;
6494 if (vcpu->arch.interrupt.soft)
6495 inc_eip = vcpu->arch.event_exit_inst_len;
6496 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006497 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006498 return;
6499 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006500 intr = irq | INTR_INFO_VALID_MASK;
6501 if (vcpu->arch.interrupt.soft) {
6502 intr |= INTR_TYPE_SOFT_INTR;
6503 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6504 vmx->vcpu.arch.event_exit_inst_len);
6505 } else
6506 intr |= INTR_TYPE_EXT_INTR;
6507 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006508
6509 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006510}
6511
Sheng Yangf08864b2008-05-15 18:23:25 +08006512static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6513{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006514 struct vcpu_vmx *vmx = to_vmx(vcpu);
6515
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006516 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006517 /*
6518 * Tracking the NMI-blocked state in software is built upon
6519 * finding the next open IRQ window. This, in turn, depends on
6520 * well-behaving guests: They have to keep IRQs disabled at
6521 * least as long as the NMI handler runs. Otherwise we may
6522 * cause NMI nesting, maybe breaking the guest. But as this is
6523 * highly unlikely, we can live with the residual risk.
6524 */
6525 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6526 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6527 }
6528
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006529 ++vcpu->stat.nmi_injections;
6530 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006531
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006532 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006533 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006534 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006535 return;
6536 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006537
Sheng Yangf08864b2008-05-15 18:23:25 +08006538 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6539 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006540
6541 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006542}
6543
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006544static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6545{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006546 struct vcpu_vmx *vmx = to_vmx(vcpu);
6547 bool masked;
6548
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006549 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006550 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006551 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006552 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006553 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6554 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6555 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006556}
6557
6558static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6559{
6560 struct vcpu_vmx *vmx = to_vmx(vcpu);
6561
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006562 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006563 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6564 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6565 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6566 }
6567 } else {
6568 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6569 if (masked)
6570 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6571 GUEST_INTR_STATE_NMI);
6572 else
6573 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6574 GUEST_INTR_STATE_NMI);
6575 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006576}
6577
Jan Kiszka2505dc92013-04-14 12:12:47 +02006578static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6579{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006580 if (to_vmx(vcpu)->nested.nested_run_pending)
6581 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006582
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006583 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006584 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6585 return 0;
6586
Jan Kiszka2505dc92013-04-14 12:12:47 +02006587 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6588 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6589 | GUEST_INTR_STATE_NMI));
6590}
6591
Gleb Natapov78646122009-03-23 12:12:11 +02006592static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6593{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006594 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6595 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006596 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6597 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006598}
6599
Izik Eiduscbc94022007-10-25 00:29:55 +02006600static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6601{
6602 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006603
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006604 if (enable_unrestricted_guest)
6605 return 0;
6606
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006607 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6608 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006609 if (ret)
6610 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006611 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006612 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006613}
6614
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006615static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6616{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006617 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006618 return 0;
6619}
6620
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006621static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006622{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006623 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006624 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006625 /*
6626 * Update instruction length as we may reinject the exception
6627 * from user space while in guest debugging mode.
6628 */
6629 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6630 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006631 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006632 return false;
6633 /* fall through */
6634 case DB_VECTOR:
6635 if (vcpu->guest_debug &
6636 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6637 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006638 /* fall through */
6639 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006640 case OF_VECTOR:
6641 case BR_VECTOR:
6642 case UD_VECTOR:
6643 case DF_VECTOR:
6644 case SS_VECTOR:
6645 case GP_VECTOR:
6646 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006647 return true;
6648 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006649 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006650 return false;
6651}
6652
6653static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6654 int vec, u32 err_code)
6655{
6656 /*
6657 * Instruction with address size override prefix opcode 0x67
6658 * Cause the #SS fault with 0 error code in VM86 mode.
6659 */
6660 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6661 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6662 if (vcpu->arch.halt_request) {
6663 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006664 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006665 }
6666 return 1;
6667 }
6668 return 0;
6669 }
6670
6671 /*
6672 * Forward all other exceptions that are valid in real mode.
6673 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6674 * the required debugging infrastructure rework.
6675 */
6676 kvm_queue_exception(vcpu, vec);
6677 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006678}
6679
Andi Kleena0861c02009-06-08 17:37:09 +08006680/*
6681 * Trigger machine check on the host. We assume all the MSRs are already set up
6682 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6683 * We pass a fake environment to the machine check handler because we want
6684 * the guest to be always treated like user space, no matter what context
6685 * it used internally.
6686 */
6687static void kvm_machine_check(void)
6688{
6689#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6690 struct pt_regs regs = {
6691 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6692 .flags = X86_EFLAGS_IF,
6693 };
6694
6695 do_machine_check(&regs, 0);
6696#endif
6697}
6698
Avi Kivity851ba692009-08-24 11:10:17 +03006699static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006700{
6701 /* already handled by vcpu_run */
6702 return 1;
6703}
6704
Avi Kivity851ba692009-08-24 11:10:17 +03006705static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006706{
Avi Kivity1155f762007-11-22 11:30:47 +02006707 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006708 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006709 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006710 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006711 u32 vect_info;
6712 enum emulation_result er;
6713
Avi Kivity1155f762007-11-22 11:30:47 +02006714 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006715 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006716
Andi Kleena0861c02009-06-08 17:37:09 +08006717 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006718 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006719
Jim Mattsonef85b672016-12-12 11:01:37 -08006720 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006721 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006722
Wanpeng Li082d06e2018-04-03 16:28:48 -07006723 if (is_invalid_opcode(intr_info))
6724 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006725
Avi Kivity6aa8b732006-12-10 02:21:36 -08006726 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006727 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006728 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006729
Liran Alon9e869482018-03-12 13:12:51 +02006730 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6731 WARN_ON_ONCE(!enable_vmware_backdoor);
6732 er = emulate_instruction(vcpu,
6733 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6734 if (er == EMULATE_USER_EXIT)
6735 return 0;
6736 else if (er != EMULATE_DONE)
6737 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6738 return 1;
6739 }
6740
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006741 /*
6742 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6743 * MMIO, it is better to report an internal error.
6744 * See the comments in vmx_handle_exit.
6745 */
6746 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6747 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6748 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6749 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006750 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006751 vcpu->run->internal.data[0] = vect_info;
6752 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006753 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006754 return 0;
6755 }
6756
Avi Kivity6aa8b732006-12-10 02:21:36 -08006757 if (is_page_fault(intr_info)) {
6758 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006759 /* EPT won't cause page fault directly */
6760 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006761 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006762 }
6763
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006764 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006765
6766 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6767 return handle_rmode_exception(vcpu, ex_no, error_code);
6768
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006769 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006770 case AC_VECTOR:
6771 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6772 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006773 case DB_VECTOR:
6774 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6775 if (!(vcpu->guest_debug &
6776 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006777 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006778 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006779 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006780 skip_emulated_instruction(vcpu);
6781
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006782 kvm_queue_exception(vcpu, DB_VECTOR);
6783 return 1;
6784 }
6785 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6786 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6787 /* fall through */
6788 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006789 /*
6790 * Update instruction length as we may reinject #BP from
6791 * user space while in guest debugging mode. Reading it for
6792 * #DB as well causes no harm, it is not used in that case.
6793 */
6794 vmx->vcpu.arch.event_exit_inst_len =
6795 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006796 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006797 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006798 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6799 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006800 break;
6801 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006802 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6803 kvm_run->ex.exception = ex_no;
6804 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006805 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006806 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006807 return 0;
6808}
6809
Avi Kivity851ba692009-08-24 11:10:17 +03006810static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006812 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006813 return 1;
6814}
6815
Avi Kivity851ba692009-08-24 11:10:17 +03006816static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006817{
Avi Kivity851ba692009-08-24 11:10:17 +03006818 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006819 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006820 return 0;
6821}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822
Avi Kivity851ba692009-08-24 11:10:17 +03006823static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006824{
He, Qingbfdaab02007-09-12 14:18:28 +08006825 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006826 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006827 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828
He, Qingbfdaab02007-09-12 14:18:28 +08006829 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006830 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006831
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006832 ++vcpu->stat.io_exits;
6833
Sean Christopherson432baf62018-03-08 08:57:26 -08006834 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006835 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006836
6837 port = exit_qualification >> 16;
6838 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006839 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006840
Sean Christophersondca7f122018-03-08 08:57:27 -08006841 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006842}
6843
Ingo Molnar102d8322007-02-19 14:37:47 +02006844static void
6845vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6846{
6847 /*
6848 * Patch in the VMCALL instruction:
6849 */
6850 hypercall[0] = 0x0f;
6851 hypercall[1] = 0x01;
6852 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006853}
6854
Guo Chao0fa06072012-06-28 15:16:19 +08006855/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006856static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6857{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006858 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006859 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6860 unsigned long orig_val = val;
6861
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006862 /*
6863 * We get here when L2 changed cr0 in a way that did not change
6864 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006865 * but did change L0 shadowed bits. So we first calculate the
6866 * effective cr0 value that L1 would like to write into the
6867 * hardware. It consists of the L2-owned bits from the new
6868 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006869 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006870 val = (val & ~vmcs12->cr0_guest_host_mask) |
6871 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6872
David Matlack38991522016-11-29 18:14:08 -08006873 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006874 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006875
6876 if (kvm_set_cr0(vcpu, val))
6877 return 1;
6878 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006879 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006880 } else {
6881 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006882 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006883 return 1;
David Matlack38991522016-11-29 18:14:08 -08006884
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006885 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006886 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006887}
6888
6889static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6890{
6891 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006892 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6893 unsigned long orig_val = val;
6894
6895 /* analogously to handle_set_cr0 */
6896 val = (val & ~vmcs12->cr4_guest_host_mask) |
6897 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6898 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006899 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006900 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006901 return 0;
6902 } else
6903 return kvm_set_cr4(vcpu, val);
6904}
6905
Paolo Bonzini0367f202016-07-12 10:44:55 +02006906static int handle_desc(struct kvm_vcpu *vcpu)
6907{
6908 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6909 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6910}
6911
Avi Kivity851ba692009-08-24 11:10:17 +03006912static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006914 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006915 int cr;
6916 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006917 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006918 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919
He, Qingbfdaab02007-09-12 14:18:28 +08006920 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006921 cr = exit_qualification & 15;
6922 reg = (exit_qualification >> 8) & 15;
6923 switch ((exit_qualification >> 4) & 3) {
6924 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006925 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006926 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006927 switch (cr) {
6928 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006929 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006930 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006931 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006932 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006933 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006934 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006936 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006937 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006938 case 8: {
6939 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006940 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006941 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006942 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006943 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006944 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006945 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006946 return ret;
6947 /*
6948 * TODO: we might be squashing a
6949 * KVM_GUESTDBG_SINGLESTEP-triggered
6950 * KVM_EXIT_DEBUG here.
6951 */
Avi Kivity851ba692009-08-24 11:10:17 +03006952 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006953 return 0;
6954 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006955 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006956 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006957 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006958 WARN_ONCE(1, "Guest should always own CR0.TS");
6959 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006960 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006961 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006962 case 1: /*mov from cr*/
6963 switch (cr) {
6964 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006965 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006966 val = kvm_read_cr3(vcpu);
6967 kvm_register_write(vcpu, reg, val);
6968 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006969 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006970 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006971 val = kvm_get_cr8(vcpu);
6972 kvm_register_write(vcpu, reg, val);
6973 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006974 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006975 }
6976 break;
6977 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006978 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006979 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006980 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006981
Kyle Huey6affcbe2016-11-29 12:40:40 -08006982 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006983 default:
6984 break;
6985 }
Avi Kivity851ba692009-08-24 11:10:17 +03006986 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006987 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988 (int)(exit_qualification >> 4) & 3, cr);
6989 return 0;
6990}
6991
Avi Kivity851ba692009-08-24 11:10:17 +03006992static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006993{
He, Qingbfdaab02007-09-12 14:18:28 +08006994 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006995 int dr, dr7, reg;
6996
6997 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6998 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6999
7000 /* First, if DR does not exist, trigger UD */
7001 if (!kvm_require_dr(vcpu, dr))
7002 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007003
Jan Kiszkaf2483412010-01-20 18:20:20 +01007004 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007005 if (!kvm_require_cpl(vcpu, 0))
7006 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007007 dr7 = vmcs_readl(GUEST_DR7);
7008 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007009 /*
7010 * As the vm-exit takes precedence over the debug trap, we
7011 * need to emulate the latter, either for the host or the
7012 * guest debugging itself.
7013 */
7014 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007015 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007016 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007017 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007018 vcpu->run->debug.arch.exception = DB_VECTOR;
7019 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007020 return 0;
7021 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007022 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007023 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007024 kvm_queue_exception(vcpu, DB_VECTOR);
7025 return 1;
7026 }
7027 }
7028
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007029 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007030 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7031 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007032
7033 /*
7034 * No more DR vmexits; force a reload of the debug registers
7035 * and reenter on this instruction. The next vmexit will
7036 * retrieve the full state of the debug registers.
7037 */
7038 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7039 return 1;
7040 }
7041
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007042 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7043 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007044 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007045
7046 if (kvm_get_dr(vcpu, dr, &val))
7047 return 1;
7048 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007049 } else
Nadav Amit57773922014-06-18 17:19:23 +03007050 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007051 return 1;
7052
Kyle Huey6affcbe2016-11-29 12:40:40 -08007053 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054}
7055
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007056static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7057{
7058 return vcpu->arch.dr6;
7059}
7060
7061static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7062{
7063}
7064
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007065static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7066{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007067 get_debugreg(vcpu->arch.db[0], 0);
7068 get_debugreg(vcpu->arch.db[1], 1);
7069 get_debugreg(vcpu->arch.db[2], 2);
7070 get_debugreg(vcpu->arch.db[3], 3);
7071 get_debugreg(vcpu->arch.dr6, 6);
7072 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7073
7074 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007075 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007076}
7077
Gleb Natapov020df072010-04-13 10:05:23 +03007078static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7079{
7080 vmcs_writel(GUEST_DR7, val);
7081}
7082
Avi Kivity851ba692009-08-24 11:10:17 +03007083static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084{
Kyle Huey6a908b62016-11-29 12:40:37 -08007085 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086}
7087
Avi Kivity851ba692009-08-24 11:10:17 +03007088static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007089{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007090 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007091 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007093 msr_info.index = ecx;
7094 msr_info.host_initiated = false;
7095 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007096 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007097 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007098 return 1;
7099 }
7100
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007101 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007102
Avi Kivity6aa8b732006-12-10 02:21:36 -08007103 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007104 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7105 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007106 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007107}
7108
Avi Kivity851ba692009-08-24 11:10:17 +03007109static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007110{
Will Auld8fe8ab42012-11-29 12:42:12 -08007111 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007112 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7113 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7114 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115
Will Auld8fe8ab42012-11-29 12:42:12 -08007116 msr.data = data;
7117 msr.index = ecx;
7118 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007119 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007120 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007121 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007122 return 1;
7123 }
7124
Avi Kivity59200272010-01-25 19:47:02 +02007125 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007126 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127}
7128
Avi Kivity851ba692009-08-24 11:10:17 +03007129static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007130{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007131 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007132 return 1;
7133}
7134
Avi Kivity851ba692009-08-24 11:10:17 +03007135static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007136{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007137 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7138 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007139
Avi Kivity3842d132010-07-27 12:30:24 +03007140 kvm_make_request(KVM_REQ_EVENT, vcpu);
7141
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007142 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007143 return 1;
7144}
7145
Avi Kivity851ba692009-08-24 11:10:17 +03007146static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147{
Avi Kivityd3bef152007-06-05 15:53:05 +03007148 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007149}
7150
Avi Kivity851ba692009-08-24 11:10:17 +03007151static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007152{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007153 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007154}
7155
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007156static int handle_invd(struct kvm_vcpu *vcpu)
7157{
Andre Przywara51d8b662010-12-21 11:12:02 +01007158 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007159}
7160
Avi Kivity851ba692009-08-24 11:10:17 +03007161static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007162{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007163 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007164
7165 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007166 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007167}
7168
Avi Kivityfee84b02011-11-10 14:57:25 +02007169static int handle_rdpmc(struct kvm_vcpu *vcpu)
7170{
7171 int err;
7172
7173 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007174 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007175}
7176
Avi Kivity851ba692009-08-24 11:10:17 +03007177static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007178{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007179 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007180}
7181
Dexuan Cui2acf9232010-06-10 11:27:12 +08007182static int handle_xsetbv(struct kvm_vcpu *vcpu)
7183{
7184 u64 new_bv = kvm_read_edx_eax(vcpu);
7185 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7186
7187 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007188 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007189 return 1;
7190}
7191
Wanpeng Lif53cd632014-12-02 19:14:58 +08007192static int handle_xsaves(struct kvm_vcpu *vcpu)
7193{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007194 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007195 WARN(1, "this should never happen\n");
7196 return 1;
7197}
7198
7199static int handle_xrstors(struct kvm_vcpu *vcpu)
7200{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007201 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007202 WARN(1, "this should never happen\n");
7203 return 1;
7204}
7205
Avi Kivity851ba692009-08-24 11:10:17 +03007206static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007207{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007208 if (likely(fasteoi)) {
7209 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7210 int access_type, offset;
7211
7212 access_type = exit_qualification & APIC_ACCESS_TYPE;
7213 offset = exit_qualification & APIC_ACCESS_OFFSET;
7214 /*
7215 * Sane guest uses MOV to write EOI, with written value
7216 * not cared. So make a short-circuit here by avoiding
7217 * heavy instruction emulation.
7218 */
7219 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7220 (offset == APIC_EOI)) {
7221 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007222 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007223 }
7224 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007225 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007226}
7227
Yang Zhangc7c9c562013-01-25 10:18:51 +08007228static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7229{
7230 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7231 int vector = exit_qualification & 0xff;
7232
7233 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7234 kvm_apic_set_eoi_accelerated(vcpu, vector);
7235 return 1;
7236}
7237
Yang Zhang83d4c282013-01-25 10:18:49 +08007238static int handle_apic_write(struct kvm_vcpu *vcpu)
7239{
7240 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7241 u32 offset = exit_qualification & 0xfff;
7242
7243 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7244 kvm_apic_write_nodecode(vcpu, offset);
7245 return 1;
7246}
7247
Avi Kivity851ba692009-08-24 11:10:17 +03007248static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007249{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007250 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007251 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007252 bool has_error_code = false;
7253 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007254 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007255 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007256
7257 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007258 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007259 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007260
7261 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7262
7263 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007264 if (reason == TASK_SWITCH_GATE && idt_v) {
7265 switch (type) {
7266 case INTR_TYPE_NMI_INTR:
7267 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007268 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007269 break;
7270 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007271 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007272 kvm_clear_interrupt_queue(vcpu);
7273 break;
7274 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007275 if (vmx->idt_vectoring_info &
7276 VECTORING_INFO_DELIVER_CODE_MASK) {
7277 has_error_code = true;
7278 error_code =
7279 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7280 }
7281 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007282 case INTR_TYPE_SOFT_EXCEPTION:
7283 kvm_clear_exception_queue(vcpu);
7284 break;
7285 default:
7286 break;
7287 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007288 }
Izik Eidus37817f22008-03-24 23:14:53 +02007289 tss_selector = exit_qualification;
7290
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007291 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7292 type != INTR_TYPE_EXT_INTR &&
7293 type != INTR_TYPE_NMI_INTR))
7294 skip_emulated_instruction(vcpu);
7295
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007296 if (kvm_task_switch(vcpu, tss_selector,
7297 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7298 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007299 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7300 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7301 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007302 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007303 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007304
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007305 /*
7306 * TODO: What about debug traps on tss switch?
7307 * Are we supposed to inject them and update dr6?
7308 */
7309
7310 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007311}
7312
Avi Kivity851ba692009-08-24 11:10:17 +03007313static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007314{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007315 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007316 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007317 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007318
Sheng Yangf9c617f2009-03-25 10:08:52 +08007319 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007320
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007321 /*
7322 * EPT violation happened while executing iret from NMI,
7323 * "blocked by NMI" bit has to be set before next VM entry.
7324 * There are errata that may cause this bit to not be set:
7325 * AAK134, BY25.
7326 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007327 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007328 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007329 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007330 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7331
Sheng Yang14394422008-04-28 12:24:45 +08007332 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007333 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007334
Junaid Shahid27959a42016-12-06 16:46:10 -08007335 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007336 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007337 ? PFERR_USER_MASK : 0;
7338 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007339 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007340 ? PFERR_WRITE_MASK : 0;
7341 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007342 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007343 ? PFERR_FETCH_MASK : 0;
7344 /* ept page table entry is present? */
7345 error_code |= (exit_qualification &
7346 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7347 EPT_VIOLATION_EXECUTABLE))
7348 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007349
Paolo Bonzinieebed242016-11-28 14:39:58 +01007350 error_code |= (exit_qualification & 0x100) != 0 ?
7351 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007352
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007353 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007354 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007355}
7356
Avi Kivity851ba692009-08-24 11:10:17 +03007357static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007358{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007359 gpa_t gpa;
7360
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007361 /*
7362 * A nested guest cannot optimize MMIO vmexits, because we have an
7363 * nGPA here instead of the required GPA.
7364 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007365 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007366 if (!is_guest_mode(vcpu) &&
7367 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007368 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007369 /*
7370 * Doing kvm_skip_emulated_instruction() depends on undefined
7371 * behavior: Intel's manual doesn't mandate
7372 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7373 * occurs and while on real hardware it was observed to be set,
7374 * other hypervisors (namely Hyper-V) don't set it, we end up
7375 * advancing IP with some random value. Disable fast mmio when
7376 * running nested and keep it for real hardware in hope that
7377 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7378 */
7379 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7380 return kvm_skip_emulated_instruction(vcpu);
7381 else
7382 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7383 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007384 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007385
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007386 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007387}
7388
Avi Kivity851ba692009-08-24 11:10:17 +03007389static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007390{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007391 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007392 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7393 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007394 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007395 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007396
7397 return 1;
7398}
7399
Mohammed Gamal80ced182009-09-01 12:48:18 +02007400static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007401{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007402 struct vcpu_vmx *vmx = to_vmx(vcpu);
7403 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007404 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007405 u32 cpu_exec_ctrl;
7406 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007407 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007408
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007409 /*
7410 * We should never reach the point where we are emulating L2
7411 * due to invalid guest state as that means we incorrectly
7412 * allowed a nested VMEntry with an invalid vmcs12.
7413 */
7414 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7415
Avi Kivity49e9d552010-09-19 14:34:08 +02007416 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7417 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007418
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007419 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007420 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007421 return handle_interrupt_window(&vmx->vcpu);
7422
Radim Krčmář72875d82017-04-26 22:32:19 +02007423 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007424 return 1;
7425
Liran Alon9b8ae632017-11-05 16:56:34 +02007426 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007427
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007428 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007429 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007430 ret = 0;
7431 goto out;
7432 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007433
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007434 if (err != EMULATE_DONE)
7435 goto emulation_error;
7436
7437 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7438 vcpu->arch.exception.pending)
7439 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007440
Gleb Natapov8d76c492013-05-08 18:38:44 +03007441 if (vcpu->arch.halt_request) {
7442 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007443 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007444 goto out;
7445 }
7446
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007447 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007448 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007449 if (need_resched())
7450 schedule();
7451 }
7452
Mohammed Gamal80ced182009-09-01 12:48:18 +02007453out:
7454 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007455
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007456emulation_error:
7457 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7458 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7459 vcpu->run->internal.ndata = 0;
7460 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007461}
7462
7463static void grow_ple_window(struct kvm_vcpu *vcpu)
7464{
7465 struct vcpu_vmx *vmx = to_vmx(vcpu);
7466 int old = vmx->ple_window;
7467
Babu Mogerc8e88712018-03-16 16:37:24 -04007468 vmx->ple_window = __grow_ple_window(old, ple_window,
7469 ple_window_grow,
7470 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007471
7472 if (vmx->ple_window != old)
7473 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007474
7475 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007476}
7477
7478static void shrink_ple_window(struct kvm_vcpu *vcpu)
7479{
7480 struct vcpu_vmx *vmx = to_vmx(vcpu);
7481 int old = vmx->ple_window;
7482
Babu Mogerc8e88712018-03-16 16:37:24 -04007483 vmx->ple_window = __shrink_ple_window(old, ple_window,
7484 ple_window_shrink,
7485 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007486
7487 if (vmx->ple_window != old)
7488 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007489
7490 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007491}
7492
7493/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007494 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7495 */
7496static void wakeup_handler(void)
7497{
7498 struct kvm_vcpu *vcpu;
7499 int cpu = smp_processor_id();
7500
7501 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7502 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7503 blocked_vcpu_list) {
7504 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7505
7506 if (pi_test_on(pi_desc) == 1)
7507 kvm_vcpu_kick(vcpu);
7508 }
7509 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7510}
7511
Peng Haoe01bca22018-04-07 05:47:32 +08007512static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007513{
7514 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7515 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7516 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7517 0ull, VMX_EPT_EXECUTABLE_MASK,
7518 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007519 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007520
7521 ept_set_mmio_spte_mask();
7522 kvm_enable_tdp();
7523}
7524
Tiejun Chenf2c76482014-10-28 10:14:47 +08007525static __init int hardware_setup(void)
7526{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007527 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007528 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007529
7530 rdmsrl_safe(MSR_EFER, &host_efer);
7531
7532 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7533 kvm_define_shared_msr(i, vmx_msr_index[i]);
7534
Radim Krčmář23611332016-09-29 22:41:33 +02007535 for (i = 0; i < VMX_BITMAP_NR; i++) {
7536 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7537 if (!vmx_bitmap[i])
7538 goto out;
7539 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007540
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007541 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7542 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7543
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007544 if (setup_vmcs_config(&vmcs_config) < 0) {
7545 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007546 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007547 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007548
7549 if (boot_cpu_has(X86_FEATURE_NX))
7550 kvm_enable_efer_bits(EFER_NX);
7551
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007552 if (boot_cpu_has(X86_FEATURE_MPX)) {
7553 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7554 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7555 }
7556
Wanpeng Li08d839c2017-03-23 05:30:08 -07007557 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7558 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007559 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007560
Tiejun Chenf2c76482014-10-28 10:14:47 +08007561 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007562 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007563 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007564 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007565 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007566
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007567 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007568 enable_ept_ad_bits = 0;
7569
Wanpeng Li8ad81822017-10-09 15:51:53 -07007570 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007571 enable_unrestricted_guest = 0;
7572
Paolo Bonziniad15a292015-01-30 16:18:49 +01007573 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007574 flexpriority_enabled = 0;
7575
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007576 if (!cpu_has_virtual_nmis())
7577 enable_vnmi = 0;
7578
Paolo Bonziniad15a292015-01-30 16:18:49 +01007579 /*
7580 * set_apic_access_page_addr() is used to reload apic access
7581 * page upon invalidation. No need to do anything if not
7582 * using the APIC_ACCESS_ADDR VMCS field.
7583 */
7584 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007585 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007586
7587 if (!cpu_has_vmx_tpr_shadow())
7588 kvm_x86_ops->update_cr8_intercept = NULL;
7589
7590 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7591 kvm_disable_largepages();
7592
Wanpeng Li0f107682017-09-28 18:06:24 -07007593 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007594 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007595 ple_window = 0;
7596 ple_window_grow = 0;
7597 ple_window_max = 0;
7598 ple_window_shrink = 0;
7599 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007601 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007602 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007603 kvm_x86_ops->sync_pir_to_irr = NULL;
7604 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007605
Haozhong Zhang64903d62015-10-20 15:39:09 +08007606 if (cpu_has_vmx_tsc_scaling()) {
7607 kvm_has_tsc_control = true;
7608 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7609 kvm_tsc_scaling_ratio_frac_bits = 48;
7610 }
7611
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007612 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7613
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007614 if (enable_ept)
7615 vmx_enable_tdp();
7616 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007617 kvm_disable_tdp();
7618
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007619 if (!nested) {
7620 kvm_x86_ops->get_nested_state = NULL;
7621 kvm_x86_ops->set_nested_state = NULL;
7622 }
7623
Kai Huang843e4332015-01-28 10:54:28 +08007624 /*
7625 * Only enable PML when hardware supports PML feature, and both EPT
7626 * and EPT A/D bit features are enabled -- PML depends on them to work.
7627 */
7628 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7629 enable_pml = 0;
7630
7631 if (!enable_pml) {
7632 kvm_x86_ops->slot_enable_log_dirty = NULL;
7633 kvm_x86_ops->slot_disable_log_dirty = NULL;
7634 kvm_x86_ops->flush_log_dirty = NULL;
7635 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7636 }
7637
Yunhong Jiang64672c92016-06-13 14:19:59 -07007638 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7639 u64 vmx_msr;
7640
7641 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7642 cpu_preemption_timer_multi =
7643 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7644 } else {
7645 kvm_x86_ops->set_hv_timer = NULL;
7646 kvm_x86_ops->cancel_hv_timer = NULL;
7647 }
7648
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007649 if (!cpu_has_vmx_shadow_vmcs())
7650 enable_shadow_vmcs = 0;
7651 if (enable_shadow_vmcs)
7652 init_vmcs_shadow_fields();
7653
Feng Wubf9f6ac2015-09-18 22:29:55 +08007654 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007655 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007656
Ashok Rajc45dcc72016-06-22 14:59:56 +08007657 kvm_mce_cap_supported |= MCG_LMCE_P;
7658
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007660
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007661out:
Radim Krčmář23611332016-09-29 22:41:33 +02007662 for (i = 0; i < VMX_BITMAP_NR; i++)
7663 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007664
7665 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007666}
7667
7668static __exit void hardware_unsetup(void)
7669{
Radim Krčmář23611332016-09-29 22:41:33 +02007670 int i;
7671
7672 for (i = 0; i < VMX_BITMAP_NR; i++)
7673 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007674
Tiejun Chenf2c76482014-10-28 10:14:47 +08007675 free_kvm_area();
7676}
7677
Avi Kivity6aa8b732006-12-10 02:21:36 -08007678/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007679 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7680 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7681 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007682static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007683{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007684 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007685 grow_ple_window(vcpu);
7686
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007687 /*
7688 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7689 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7690 * never set PAUSE_EXITING and just set PLE if supported,
7691 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7692 */
7693 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007694 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007695}
7696
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007697static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007698{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007699 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007700}
7701
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007702static int handle_mwait(struct kvm_vcpu *vcpu)
7703{
7704 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7705 return handle_nop(vcpu);
7706}
7707
Jim Mattson45ec3682017-08-23 16:32:04 -07007708static int handle_invalid_op(struct kvm_vcpu *vcpu)
7709{
7710 kvm_queue_exception(vcpu, UD_VECTOR);
7711 return 1;
7712}
7713
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007714static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7715{
7716 return 1;
7717}
7718
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007719static int handle_monitor(struct kvm_vcpu *vcpu)
7720{
7721 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7722 return handle_nop(vcpu);
7723}
7724
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007725/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007726 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7727 * set the success or error code of an emulated VMX instruction, as specified
7728 * by Vol 2B, VMX Instruction Reference, "Conventions".
7729 */
7730static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7731{
7732 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7733 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7734 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7735}
7736
7737static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7738{
7739 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7740 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7741 X86_EFLAGS_SF | X86_EFLAGS_OF))
7742 | X86_EFLAGS_CF);
7743}
7744
Abel Gordon145c28d2013-04-18 14:36:55 +03007745static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007746 u32 vm_instruction_error)
7747{
7748 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7749 /*
7750 * failValid writes the error number to the current VMCS, which
7751 * can't be done there isn't a current VMCS.
7752 */
7753 nested_vmx_failInvalid(vcpu);
7754 return;
7755 }
7756 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7757 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7758 X86_EFLAGS_SF | X86_EFLAGS_OF))
7759 | X86_EFLAGS_ZF);
7760 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7761 /*
7762 * We don't need to force a shadow sync because
7763 * VM_INSTRUCTION_ERROR is not shadowed
7764 */
7765}
Abel Gordon145c28d2013-04-18 14:36:55 +03007766
Wincy Vanff651cb2014-12-11 08:52:58 +03007767static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7768{
7769 /* TODO: not to reset guest simply here. */
7770 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007771 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007772}
7773
Jan Kiszkaf4124502014-03-07 20:03:13 +01007774static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7775{
7776 struct vcpu_vmx *vmx =
7777 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7778
7779 vmx->nested.preemption_timer_expired = true;
7780 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7781 kvm_vcpu_kick(&vmx->vcpu);
7782
7783 return HRTIMER_NORESTART;
7784}
7785
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007786/*
Bandan Das19677e32014-05-06 02:19:15 -04007787 * Decode the memory-address operand of a vmx instruction, as recorded on an
7788 * exit caused by such an instruction (run by a guest hypervisor).
7789 * On success, returns 0. When the operand is invalid, returns 1 and throws
7790 * #UD or #GP.
7791 */
7792static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7793 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007794 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007795{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007796 gva_t off;
7797 bool exn;
7798 struct kvm_segment s;
7799
Bandan Das19677e32014-05-06 02:19:15 -04007800 /*
7801 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7802 * Execution", on an exit, vmx_instruction_info holds most of the
7803 * addressing components of the operand. Only the displacement part
7804 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7805 * For how an actual address is calculated from all these components,
7806 * refer to Vol. 1, "Operand Addressing".
7807 */
7808 int scaling = vmx_instruction_info & 3;
7809 int addr_size = (vmx_instruction_info >> 7) & 7;
7810 bool is_reg = vmx_instruction_info & (1u << 10);
7811 int seg_reg = (vmx_instruction_info >> 15) & 7;
7812 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7813 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7814 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7815 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7816
7817 if (is_reg) {
7818 kvm_queue_exception(vcpu, UD_VECTOR);
7819 return 1;
7820 }
7821
7822 /* Addr = segment_base + offset */
7823 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007824 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007825 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007826 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007827 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007828 off += kvm_register_read(vcpu, index_reg)<<scaling;
7829 vmx_get_segment(vcpu, &s, seg_reg);
7830 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007831
7832 if (addr_size == 1) /* 32 bit */
7833 *ret &= 0xffffffff;
7834
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007835 /* Checks for #GP/#SS exceptions. */
7836 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007837 if (is_long_mode(vcpu)) {
7838 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7839 * non-canonical form. This is the only check on the memory
7840 * destination for long mode!
7841 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007842 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007843 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007844 /* Protected mode: apply checks for segment validity in the
7845 * following order:
7846 * - segment type check (#GP(0) may be thrown)
7847 * - usability check (#GP(0)/#SS(0))
7848 * - limit check (#GP(0)/#SS(0))
7849 */
7850 if (wr)
7851 /* #GP(0) if the destination operand is located in a
7852 * read-only data segment or any code segment.
7853 */
7854 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7855 else
7856 /* #GP(0) if the source operand is located in an
7857 * execute-only code segment
7858 */
7859 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007860 if (exn) {
7861 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7862 return 1;
7863 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007864 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7865 */
7866 exn = (s.unusable != 0);
7867 /* Protected mode: #GP(0)/#SS(0) if the memory
7868 * operand is outside the segment limit.
7869 */
7870 exn = exn || (off + sizeof(u64) > s.limit);
7871 }
7872 if (exn) {
7873 kvm_queue_exception_e(vcpu,
7874 seg_reg == VCPU_SREG_SS ?
7875 SS_VECTOR : GP_VECTOR,
7876 0);
7877 return 1;
7878 }
7879
Bandan Das19677e32014-05-06 02:19:15 -04007880 return 0;
7881}
7882
Radim Krčmářcbf71272017-05-19 15:48:51 +02007883static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007884{
7885 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007886 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007887
7888 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007889 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007890 return 1;
7891
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007892 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007893 kvm_inject_page_fault(vcpu, &e);
7894 return 1;
7895 }
7896
Bandan Das3573e222014-05-06 02:19:16 -04007897 return 0;
7898}
7899
Jim Mattsone29acc52016-11-30 12:03:43 -08007900static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7901{
7902 struct vcpu_vmx *vmx = to_vmx(vcpu);
7903 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007904 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007905
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007906 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7907 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007908 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007909
7910 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7911 if (!vmx->nested.cached_vmcs12)
7912 goto out_cached_vmcs12;
7913
Liran Alon61ada742018-06-23 02:35:08 +03007914 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7915 if (!vmx->nested.cached_shadow_vmcs12)
7916 goto out_cached_shadow_vmcs12;
7917
Jim Mattsone29acc52016-11-30 12:03:43 -08007918 if (enable_shadow_vmcs) {
7919 shadow_vmcs = alloc_vmcs();
7920 if (!shadow_vmcs)
7921 goto out_shadow_vmcs;
7922 /* mark vmcs as shadow */
Liran Alon392b2f22018-06-23 02:35:01 +03007923 shadow_vmcs->hdr.shadow_vmcs = 1;
Jim Mattsone29acc52016-11-30 12:03:43 -08007924 /* init shadow vmcs */
7925 vmcs_clear(shadow_vmcs);
7926 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7927 }
7928
Jim Mattsone29acc52016-11-30 12:03:43 -08007929 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7930 HRTIMER_MODE_REL_PINNED);
7931 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7932
7933 vmx->nested.vmxon = true;
7934 return 0;
7935
7936out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03007937 kfree(vmx->nested.cached_shadow_vmcs12);
7938
7939out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08007940 kfree(vmx->nested.cached_vmcs12);
7941
7942out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007943 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007944
Jim Mattsonde3a0022017-11-27 17:22:25 -06007945out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007946 return -ENOMEM;
7947}
7948
Bandan Das3573e222014-05-06 02:19:16 -04007949/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007950 * Emulate the VMXON instruction.
7951 * Currently, we just remember that VMX is active, and do not save or even
7952 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7953 * do not currently need to store anything in that guest-allocated memory
7954 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7955 * argument is different from the VMXON pointer (which the spec says they do).
7956 */
7957static int handle_vmon(struct kvm_vcpu *vcpu)
7958{
Jim Mattsone29acc52016-11-30 12:03:43 -08007959 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007960 gpa_t vmptr;
7961 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007962 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007963 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7964 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007965
Jim Mattson70f3aac2017-04-26 08:53:46 -07007966 /*
7967 * The Intel VMX Instruction Reference lists a bunch of bits that are
7968 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7969 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7970 * Otherwise, we should fail with #UD. But most faulting conditions
7971 * have already been checked by hardware, prior to the VM-exit for
7972 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7973 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007974 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007975 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007976 kvm_queue_exception(vcpu, UD_VECTOR);
7977 return 1;
7978 }
7979
Felix Wilhelm727ba742018-06-11 09:43:44 +02007980 /* CPL=0 must be checked manually. */
7981 if (vmx_get_cpl(vcpu)) {
7982 kvm_queue_exception(vcpu, UD_VECTOR);
7983 return 1;
7984 }
7985
Abel Gordon145c28d2013-04-18 14:36:55 +03007986 if (vmx->nested.vmxon) {
7987 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007988 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007989 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007990
Haozhong Zhang3b840802016-06-22 14:59:54 +08007991 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007992 != VMXON_NEEDED_FEATURES) {
7993 kvm_inject_gp(vcpu, 0);
7994 return 1;
7995 }
7996
Radim Krčmářcbf71272017-05-19 15:48:51 +02007997 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007998 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007999
8000 /*
8001 * SDM 3: 24.11.5
8002 * The first 4 bytes of VMXON region contain the supported
8003 * VMCS revision identifier
8004 *
8005 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8006 * which replaces physical address width with 32
8007 */
8008 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8009 nested_vmx_failInvalid(vcpu);
8010 return kvm_skip_emulated_instruction(vcpu);
8011 }
8012
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008013 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8014 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008015 nested_vmx_failInvalid(vcpu);
8016 return kvm_skip_emulated_instruction(vcpu);
8017 }
8018 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8019 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008020 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008021 nested_vmx_failInvalid(vcpu);
8022 return kvm_skip_emulated_instruction(vcpu);
8023 }
8024 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008025 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008026
8027 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008028 ret = enter_vmx_operation(vcpu);
8029 if (ret)
8030 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008031
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008032 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008033 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008034}
8035
8036/*
8037 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8038 * for running VMX instructions (except VMXON, whose prerequisites are
8039 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008040 * Note that many of these exceptions have priority over VM exits, so they
8041 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008042 */
8043static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8044{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008045 if (vmx_get_cpl(vcpu)) {
8046 kvm_queue_exception(vcpu, UD_VECTOR);
8047 return 0;
8048 }
8049
Jim Mattson70f3aac2017-04-26 08:53:46 -07008050 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008051 kvm_queue_exception(vcpu, UD_VECTOR);
8052 return 0;
8053 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008054 return 1;
8055}
8056
David Matlack8ca44e82017-08-01 14:00:39 -07008057static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8058{
8059 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8060 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8061}
8062
Abel Gordone7953d72013-04-18 14:37:55 +03008063static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8064{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008065 if (vmx->nested.current_vmptr == -1ull)
8066 return;
8067
Abel Gordon012f83c2013-04-18 14:39:25 +03008068 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008069 /* copy to memory all shadowed fields in case
8070 they were modified */
8071 copy_shadow_to_vmcs12(vmx);
8072 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008073 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008074 }
Wincy Van705699a2015-02-03 23:58:17 +08008075 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008076
8077 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008078 kvm_vcpu_write_guest_page(&vmx->vcpu,
8079 vmx->nested.current_vmptr >> PAGE_SHIFT,
8080 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008081
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008082 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008083}
8084
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008085/*
8086 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8087 * just stops using VMX.
8088 */
8089static void free_nested(struct vcpu_vmx *vmx)
8090{
Wanpeng Lib7455822017-11-22 14:04:00 -08008091 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008092 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008093
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008094 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008095 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008096 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008097 vmx->nested.posted_intr_nv = -1;
8098 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008099 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008100 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008101 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8102 free_vmcs(vmx->vmcs01.shadow_vmcs);
8103 vmx->vmcs01.shadow_vmcs = NULL;
8104 }
David Matlack4f2777b2016-07-13 17:16:37 -07008105 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008106 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008107 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008108 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008109 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008110 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008111 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008112 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008113 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008114 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008115 }
Wincy Van705699a2015-02-03 23:58:17 +08008116 if (vmx->nested.pi_desc_page) {
8117 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008118 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008119 vmx->nested.pi_desc_page = NULL;
8120 vmx->nested.pi_desc = NULL;
8121 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008122
Jim Mattsonde3a0022017-11-27 17:22:25 -06008123 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008124}
8125
8126/* Emulate the VMXOFF instruction */
8127static int handle_vmoff(struct kvm_vcpu *vcpu)
8128{
8129 if (!nested_vmx_check_permission(vcpu))
8130 return 1;
8131 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008132 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008133 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008134}
8135
Nadav Har'El27d6c862011-05-25 23:06:59 +03008136/* Emulate the VMCLEAR instruction */
8137static int handle_vmclear(struct kvm_vcpu *vcpu)
8138{
8139 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008140 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008141 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008142
8143 if (!nested_vmx_check_permission(vcpu))
8144 return 1;
8145
Radim Krčmářcbf71272017-05-19 15:48:51 +02008146 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008147 return 1;
8148
Radim Krčmářcbf71272017-05-19 15:48:51 +02008149 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8150 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8151 return kvm_skip_emulated_instruction(vcpu);
8152 }
8153
8154 if (vmptr == vmx->nested.vmxon_ptr) {
8155 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8156 return kvm_skip_emulated_instruction(vcpu);
8157 }
8158
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008159 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008160 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008161
Jim Mattson587d7e722017-03-02 12:41:48 -08008162 kvm_vcpu_write_guest(vcpu,
8163 vmptr + offsetof(struct vmcs12, launch_state),
8164 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008165
Nadav Har'El27d6c862011-05-25 23:06:59 +03008166 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008167 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008168}
8169
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008170static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8171
8172/* Emulate the VMLAUNCH instruction */
8173static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8174{
8175 return nested_vmx_run(vcpu, true);
8176}
8177
8178/* Emulate the VMRESUME instruction */
8179static int handle_vmresume(struct kvm_vcpu *vcpu)
8180{
8181
8182 return nested_vmx_run(vcpu, false);
8183}
8184
Nadav Har'El49f705c2011-05-25 23:08:30 +03008185/*
8186 * Read a vmcs12 field. Since these can have varying lengths and we return
8187 * one type, we chose the biggest type (u64) and zero-extend the return value
8188 * to that size. Note that the caller, handle_vmread, might need to use only
8189 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8190 * 64-bit fields are to be returned).
8191 */
Liran Alone2536742018-06-23 02:35:02 +03008192static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008193 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008194{
8195 short offset = vmcs_field_to_offset(field);
8196 char *p;
8197
8198 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008199 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008200
Liran Alone2536742018-06-23 02:35:02 +03008201 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008202
Jim Mattsond37f4262017-12-22 12:12:16 -08008203 switch (vmcs_field_width(field)) {
8204 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008205 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008206 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008207 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008208 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008209 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008210 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008211 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008212 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008213 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008214 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008215 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008216 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008217 WARN_ON(1);
8218 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008219 }
8220}
8221
Abel Gordon20b97fe2013-04-18 14:36:25 +03008222
Liran Alone2536742018-06-23 02:35:02 +03008223static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008224 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008225 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008226 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008227 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008228 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008229
Jim Mattsond37f4262017-12-22 12:12:16 -08008230 switch (vmcs_field_width(field)) {
8231 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008232 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008233 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008234 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008235 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008236 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008237 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008238 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008239 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008240 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008241 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008242 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008243 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008244 WARN_ON(1);
8245 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008246 }
8247
8248}
8249
Jim Mattsonf4160e42018-05-29 09:11:33 -07008250/*
8251 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8252 * they have been modified by the L1 guest. Note that the "read-only"
8253 * VM-exit information fields are actually writable if the vCPU is
8254 * configured to support "VMWRITE to any supported field in the VMCS."
8255 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008256static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8257{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008258 const u16 *fields[] = {
8259 shadow_read_write_fields,
8260 shadow_read_only_fields
8261 };
8262 const int max_fields[] = {
8263 max_shadow_read_write_fields,
8264 max_shadow_read_only_fields
8265 };
8266 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008267 unsigned long field;
8268 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008269 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008270
Jan Kiszka282da872014-10-08 18:05:39 +02008271 preempt_disable();
8272
Abel Gordon16f5b902013-04-18 14:38:25 +03008273 vmcs_load(shadow_vmcs);
8274
Jim Mattsonf4160e42018-05-29 09:11:33 -07008275 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8276 for (i = 0; i < max_fields[q]; i++) {
8277 field = fields[q][i];
8278 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008279 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008280 }
8281 /*
8282 * Skip the VM-exit information fields if they are read-only.
8283 */
8284 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8285 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008286 }
8287
8288 vmcs_clear(shadow_vmcs);
8289 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008290
8291 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008292}
8293
Abel Gordonc3114422013-04-18 14:38:55 +03008294static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8295{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008296 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008297 shadow_read_write_fields,
8298 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008299 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008300 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008301 max_shadow_read_write_fields,
8302 max_shadow_read_only_fields
8303 };
8304 int i, q;
8305 unsigned long field;
8306 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008307 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008308
8309 vmcs_load(shadow_vmcs);
8310
Mathias Krausec2bae892013-06-26 20:36:21 +02008311 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008312 for (i = 0; i < max_fields[q]; i++) {
8313 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008314 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008315 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008316 }
8317 }
8318
8319 vmcs_clear(shadow_vmcs);
8320 vmcs_load(vmx->loaded_vmcs->vmcs);
8321}
8322
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323/*
8324 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8325 * used before) all generate the same failure when it is missing.
8326 */
8327static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8328{
8329 struct vcpu_vmx *vmx = to_vmx(vcpu);
8330 if (vmx->nested.current_vmptr == -1ull) {
8331 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 return 0;
8333 }
8334 return 1;
8335}
8336
8337static int handle_vmread(struct kvm_vcpu *vcpu)
8338{
8339 unsigned long field;
8340 u64 field_value;
8341 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8342 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8343 gva_t gva = 0;
8344
Kyle Hueyeb277562016-11-29 12:40:39 -08008345 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008346 return 1;
8347
Kyle Huey6affcbe2016-11-29 12:40:40 -08008348 if (!nested_vmx_check_vmcs12(vcpu))
8349 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008350
Nadav Har'El49f705c2011-05-25 23:08:30 +03008351 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008352 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008353 /* Read the field, zero-extended to a u64 field_value */
Liran Alone2536742018-06-23 02:35:02 +03008354 if (vmcs12_read_any(get_vmcs12(vcpu), field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008355 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008356 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008357 }
8358 /*
8359 * Now copy part of this value to register or memory, as requested.
8360 * Note that the number of bits actually copied is 32 or 64 depending
8361 * on the guest's mode (32 or 64 bit), not on the given field's length.
8362 */
8363 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008364 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008365 field_value);
8366 } else {
8367 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008368 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008369 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008370 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008371 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8372 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008373 }
8374
8375 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008376 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008377}
8378
8379
8380static int handle_vmwrite(struct kvm_vcpu *vcpu)
8381{
8382 unsigned long field;
8383 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008384 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008385 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8386 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008387
Nadav Har'El49f705c2011-05-25 23:08:30 +03008388 /* The value to write might be 32 or 64 bits, depending on L1's long
8389 * mode, and eventually we need to write that into a field of several
8390 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008391 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008392 * bits into the vmcs12 field.
8393 */
8394 u64 field_value = 0;
8395 struct x86_exception e;
8396
Kyle Hueyeb277562016-11-29 12:40:39 -08008397 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008398 return 1;
8399
Kyle Huey6affcbe2016-11-29 12:40:40 -08008400 if (!nested_vmx_check_vmcs12(vcpu))
8401 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008402
Nadav Har'El49f705c2011-05-25 23:08:30 +03008403 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008404 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008405 (((vmx_instruction_info) >> 3) & 0xf));
8406 else {
8407 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008408 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008409 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008410 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8411 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008412 kvm_inject_page_fault(vcpu, &e);
8413 return 1;
8414 }
8415 }
8416
8417
Nadav Amit27e6fb52014-06-18 17:19:26 +03008418 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008419 /*
8420 * If the vCPU supports "VMWRITE to any supported field in the
8421 * VMCS," then the "read-only" fields are actually read/write.
8422 */
8423 if (vmcs_field_readonly(field) &&
8424 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 nested_vmx_failValid(vcpu,
8426 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008427 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008428 }
8429
Liran Alone2536742018-06-23 02:35:02 +03008430 if (vmcs12_write_any(get_vmcs12(vcpu), field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008431 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008432 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008433 }
8434
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008435 switch (field) {
8436#define SHADOW_FIELD_RW(x) case x:
8437#include "vmx_shadow_fields.h"
8438 /*
8439 * The fields that can be updated by L1 without a vmexit are
8440 * always updated in the vmcs02, the others go down the slow
8441 * path of prepare_vmcs02.
8442 */
8443 break;
8444 default:
8445 vmx->nested.dirty_vmcs12 = true;
8446 break;
8447 }
8448
Nadav Har'El49f705c2011-05-25 23:08:30 +03008449 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008450 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008451}
8452
Jim Mattsona8bc2842016-11-30 12:03:44 -08008453static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8454{
8455 vmx->nested.current_vmptr = vmptr;
8456 if (enable_shadow_vmcs) {
8457 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8458 SECONDARY_EXEC_SHADOW_VMCS);
8459 vmcs_write64(VMCS_LINK_POINTER,
8460 __pa(vmx->vmcs01.shadow_vmcs));
8461 vmx->nested.sync_shadow_vmcs = true;
8462 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008463 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008464}
8465
Nadav Har'El63846662011-05-25 23:07:29 +03008466/* Emulate the VMPTRLD instruction */
8467static int handle_vmptrld(struct kvm_vcpu *vcpu)
8468{
8469 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008470 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008471
8472 if (!nested_vmx_check_permission(vcpu))
8473 return 1;
8474
Radim Krčmářcbf71272017-05-19 15:48:51 +02008475 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008476 return 1;
8477
Radim Krčmářcbf71272017-05-19 15:48:51 +02008478 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8479 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8480 return kvm_skip_emulated_instruction(vcpu);
8481 }
8482
8483 if (vmptr == vmx->nested.vmxon_ptr) {
8484 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8485 return kvm_skip_emulated_instruction(vcpu);
8486 }
8487
Nadav Har'El63846662011-05-25 23:07:29 +03008488 if (vmx->nested.current_vmptr != vmptr) {
8489 struct vmcs12 *new_vmcs12;
8490 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008491 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8492 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008493 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008494 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008495 }
8496 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008497 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008498 (new_vmcs12->hdr.shadow_vmcs &&
8499 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008500 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008501 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008502 nested_vmx_failValid(vcpu,
8503 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008504 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008505 }
Nadav Har'El63846662011-05-25 23:07:29 +03008506
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008507 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008508 /*
8509 * Load VMCS12 from guest memory since it is not already
8510 * cached.
8511 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008512 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8513 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008514 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008515
Jim Mattsona8bc2842016-11-30 12:03:44 -08008516 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008517 }
8518
8519 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008520 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008521}
8522
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008523/* Emulate the VMPTRST instruction */
8524static int handle_vmptrst(struct kvm_vcpu *vcpu)
8525{
8526 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8527 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8528 gva_t vmcs_gva;
8529 struct x86_exception e;
8530
8531 if (!nested_vmx_check_permission(vcpu))
8532 return 1;
8533
8534 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008535 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008536 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008537 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008538 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8539 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8540 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008541 kvm_inject_page_fault(vcpu, &e);
8542 return 1;
8543 }
8544 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008545 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008546}
8547
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008548/* Emulate the INVEPT instruction */
8549static int handle_invept(struct kvm_vcpu *vcpu)
8550{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008551 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008552 u32 vmx_instruction_info, types;
8553 unsigned long type;
8554 gva_t gva;
8555 struct x86_exception e;
8556 struct {
8557 u64 eptp, gpa;
8558 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008559
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008560 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008561 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008562 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008563 kvm_queue_exception(vcpu, UD_VECTOR);
8564 return 1;
8565 }
8566
8567 if (!nested_vmx_check_permission(vcpu))
8568 return 1;
8569
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008570 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008571 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008572
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008573 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008574
Jim Mattson85c856b2016-10-26 08:38:38 -07008575 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008576 nested_vmx_failValid(vcpu,
8577 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008578 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008579 }
8580
8581 /* According to the Intel VMX instruction reference, the memory
8582 * operand is read even if it isn't needed (e.g., for type==global)
8583 */
8584 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008585 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008586 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008587 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008588 kvm_inject_page_fault(vcpu, &e);
8589 return 1;
8590 }
8591
8592 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008593 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008594 /*
8595 * TODO: track mappings and invalidate
8596 * single context requests appropriately
8597 */
8598 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008599 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008600 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008601 nested_vmx_succeed(vcpu);
8602 break;
8603 default:
8604 BUG_ON(1);
8605 break;
8606 }
8607
Kyle Huey6affcbe2016-11-29 12:40:40 -08008608 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008609}
8610
Petr Matouseka642fc32014-09-23 20:22:30 +02008611static int handle_invvpid(struct kvm_vcpu *vcpu)
8612{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008613 struct vcpu_vmx *vmx = to_vmx(vcpu);
8614 u32 vmx_instruction_info;
8615 unsigned long type, types;
8616 gva_t gva;
8617 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008618 struct {
8619 u64 vpid;
8620 u64 gla;
8621 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008622
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008623 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008624 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008625 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008626 kvm_queue_exception(vcpu, UD_VECTOR);
8627 return 1;
8628 }
8629
8630 if (!nested_vmx_check_permission(vcpu))
8631 return 1;
8632
8633 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8634 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8635
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008636 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008637 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008638
Jim Mattson85c856b2016-10-26 08:38:38 -07008639 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008640 nested_vmx_failValid(vcpu,
8641 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008642 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008643 }
8644
8645 /* according to the intel vmx instruction reference, the memory
8646 * operand is read even if it isn't needed (e.g., for type==global)
8647 */
8648 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8649 vmx_instruction_info, false, &gva))
8650 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008651 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008652 kvm_inject_page_fault(vcpu, &e);
8653 return 1;
8654 }
Jim Mattson40352602017-06-28 09:37:37 -07008655 if (operand.vpid >> 16) {
8656 nested_vmx_failValid(vcpu,
8657 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8658 return kvm_skip_emulated_instruction(vcpu);
8659 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008660
8661 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008662 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008663 if (!operand.vpid ||
8664 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008665 nested_vmx_failValid(vcpu,
8666 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8667 return kvm_skip_emulated_instruction(vcpu);
8668 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008669 if (cpu_has_vmx_invvpid_individual_addr() &&
8670 vmx->nested.vpid02) {
8671 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8672 vmx->nested.vpid02, operand.gla);
8673 } else
8674 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8675 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008676 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008677 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008678 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008679 nested_vmx_failValid(vcpu,
8680 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008681 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008682 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008683 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008684 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008685 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008686 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008687 break;
8688 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008689 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008690 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008691 }
8692
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008693 nested_vmx_succeed(vcpu);
8694
Kyle Huey6affcbe2016-11-29 12:40:40 -08008695 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008696}
8697
Kai Huang843e4332015-01-28 10:54:28 +08008698static int handle_pml_full(struct kvm_vcpu *vcpu)
8699{
8700 unsigned long exit_qualification;
8701
8702 trace_kvm_pml_full(vcpu->vcpu_id);
8703
8704 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8705
8706 /*
8707 * PML buffer FULL happened while executing iret from NMI,
8708 * "blocked by NMI" bit has to be set before next VM entry.
8709 */
8710 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008711 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008712 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8713 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8714 GUEST_INTR_STATE_NMI);
8715
8716 /*
8717 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8718 * here.., and there's no userspace involvement needed for PML.
8719 */
8720 return 1;
8721}
8722
Yunhong Jiang64672c92016-06-13 14:19:59 -07008723static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8724{
8725 kvm_lapic_expired_hv_timer(vcpu);
8726 return 1;
8727}
8728
Bandan Das41ab9372017-08-03 15:54:43 -04008729static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8730{
8731 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008732 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8733
8734 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008735 switch (address & VMX_EPTP_MT_MASK) {
8736 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008737 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008738 return false;
8739 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008740 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008741 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008742 return false;
8743 break;
8744 default:
8745 return false;
8746 }
8747
David Hildenbrandbb97a012017-08-10 23:15:28 +02008748 /* only 4 levels page-walk length are valid */
8749 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008750 return false;
8751
8752 /* Reserved bits should not be set */
8753 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8754 return false;
8755
8756 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008757 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008758 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008759 return false;
8760 }
8761
8762 return true;
8763}
8764
8765static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8766 struct vmcs12 *vmcs12)
8767{
8768 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8769 u64 address;
8770 bool accessed_dirty;
8771 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8772
8773 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8774 !nested_cpu_has_ept(vmcs12))
8775 return 1;
8776
8777 if (index >= VMFUNC_EPTP_ENTRIES)
8778 return 1;
8779
8780
8781 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8782 &address, index * 8, 8))
8783 return 1;
8784
David Hildenbrandbb97a012017-08-10 23:15:28 +02008785 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008786
8787 /*
8788 * If the (L2) guest does a vmfunc to the currently
8789 * active ept pointer, we don't have to do anything else
8790 */
8791 if (vmcs12->ept_pointer != address) {
8792 if (!valid_ept_address(vcpu, address))
8793 return 1;
8794
8795 kvm_mmu_unload(vcpu);
8796 mmu->ept_ad = accessed_dirty;
8797 mmu->base_role.ad_disabled = !accessed_dirty;
8798 vmcs12->ept_pointer = address;
8799 /*
8800 * TODO: Check what's the correct approach in case
8801 * mmu reload fails. Currently, we just let the next
8802 * reload potentially fail
8803 */
8804 kvm_mmu_reload(vcpu);
8805 }
8806
8807 return 0;
8808}
8809
Bandan Das2a499e42017-08-03 15:54:41 -04008810static int handle_vmfunc(struct kvm_vcpu *vcpu)
8811{
Bandan Das27c42a12017-08-03 15:54:42 -04008812 struct vcpu_vmx *vmx = to_vmx(vcpu);
8813 struct vmcs12 *vmcs12;
8814 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8815
8816 /*
8817 * VMFUNC is only supported for nested guests, but we always enable the
8818 * secondary control for simplicity; for non-nested mode, fake that we
8819 * didn't by injecting #UD.
8820 */
8821 if (!is_guest_mode(vcpu)) {
8822 kvm_queue_exception(vcpu, UD_VECTOR);
8823 return 1;
8824 }
8825
8826 vmcs12 = get_vmcs12(vcpu);
8827 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8828 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008829
8830 switch (function) {
8831 case 0:
8832 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8833 goto fail;
8834 break;
8835 default:
8836 goto fail;
8837 }
8838 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008839
8840fail:
8841 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8842 vmcs_read32(VM_EXIT_INTR_INFO),
8843 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008844 return 1;
8845}
8846
Nadav Har'El0140cae2011-05-25 23:06:28 +03008847/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008848 * The exit handlers return 1 if the exit was handled fully and guest execution
8849 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8850 * to be done to userspace and return 0.
8851 */
Mathias Krause772e0312012-08-30 01:30:19 +02008852static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008853 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8854 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008855 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008856 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008857 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008858 [EXIT_REASON_CR_ACCESS] = handle_cr,
8859 [EXIT_REASON_DR_ACCESS] = handle_dr,
8860 [EXIT_REASON_CPUID] = handle_cpuid,
8861 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8862 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8863 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8864 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008865 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008866 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008867 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008868 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008869 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008870 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008871 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008872 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008873 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008874 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008875 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008876 [EXIT_REASON_VMOFF] = handle_vmoff,
8877 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008878 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8879 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008880 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008881 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008882 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008883 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008884 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008885 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008886 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8887 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008888 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8889 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008890 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008891 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008892 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008893 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008894 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008895 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008896 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008897 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008898 [EXIT_REASON_XSAVES] = handle_xsaves,
8899 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008900 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008901 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008902 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008903};
8904
8905static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008906 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008907
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008908static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8909 struct vmcs12 *vmcs12)
8910{
8911 unsigned long exit_qualification;
8912 gpa_t bitmap, last_bitmap;
8913 unsigned int port;
8914 int size;
8915 u8 b;
8916
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008917 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008918 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008919
8920 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8921
8922 port = exit_qualification >> 16;
8923 size = (exit_qualification & 7) + 1;
8924
8925 last_bitmap = (gpa_t)-1;
8926 b = -1;
8927
8928 while (size > 0) {
8929 if (port < 0x8000)
8930 bitmap = vmcs12->io_bitmap_a;
8931 else if (port < 0x10000)
8932 bitmap = vmcs12->io_bitmap_b;
8933 else
Joe Perches1d804d02015-03-30 16:46:09 -07008934 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008935 bitmap += (port & 0x7fff) / 8;
8936
8937 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008938 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008939 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008940 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008941 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008942
8943 port++;
8944 size--;
8945 last_bitmap = bitmap;
8946 }
8947
Joe Perches1d804d02015-03-30 16:46:09 -07008948 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008949}
8950
Nadav Har'El644d7112011-05-25 23:12:35 +03008951/*
8952 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8953 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8954 * disinterest in the current event (read or write a specific MSR) by using an
8955 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8956 */
8957static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8958 struct vmcs12 *vmcs12, u32 exit_reason)
8959{
8960 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8961 gpa_t bitmap;
8962
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008963 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008964 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008965
8966 /*
8967 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8968 * for the four combinations of read/write and low/high MSR numbers.
8969 * First we need to figure out which of the four to use:
8970 */
8971 bitmap = vmcs12->msr_bitmap;
8972 if (exit_reason == EXIT_REASON_MSR_WRITE)
8973 bitmap += 2048;
8974 if (msr_index >= 0xc0000000) {
8975 msr_index -= 0xc0000000;
8976 bitmap += 1024;
8977 }
8978
8979 /* Then read the msr_index'th bit from this bitmap: */
8980 if (msr_index < 1024*8) {
8981 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008982 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008983 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008984 return 1 & (b >> (msr_index & 7));
8985 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008986 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008987}
8988
8989/*
8990 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8991 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8992 * intercept (via guest_host_mask etc.) the current event.
8993 */
8994static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8995 struct vmcs12 *vmcs12)
8996{
8997 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8998 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008999 int reg;
9000 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009001
9002 switch ((exit_qualification >> 4) & 3) {
9003 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009004 reg = (exit_qualification >> 8) & 15;
9005 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009006 switch (cr) {
9007 case 0:
9008 if (vmcs12->cr0_guest_host_mask &
9009 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009010 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009011 break;
9012 case 3:
9013 if ((vmcs12->cr3_target_count >= 1 &&
9014 vmcs12->cr3_target_value0 == val) ||
9015 (vmcs12->cr3_target_count >= 2 &&
9016 vmcs12->cr3_target_value1 == val) ||
9017 (vmcs12->cr3_target_count >= 3 &&
9018 vmcs12->cr3_target_value2 == val) ||
9019 (vmcs12->cr3_target_count >= 4 &&
9020 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009021 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009022 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009023 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009024 break;
9025 case 4:
9026 if (vmcs12->cr4_guest_host_mask &
9027 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009028 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009029 break;
9030 case 8:
9031 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_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 2: /* clts */
9037 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9038 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009039 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009040 break;
9041 case 1: /* mov from cr */
9042 switch (cr) {
9043 case 3:
9044 if (vmcs12->cpu_based_vm_exec_control &
9045 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009046 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009047 break;
9048 case 8:
9049 if (vmcs12->cpu_based_vm_exec_control &
9050 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009051 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009052 break;
9053 }
9054 break;
9055 case 3: /* lmsw */
9056 /*
9057 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9058 * cr0. Other attempted changes are ignored, with no exit.
9059 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009060 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009061 if (vmcs12->cr0_guest_host_mask & 0xe &
9062 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009063 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009064 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9065 !(vmcs12->cr0_read_shadow & 0x1) &&
9066 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009067 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009068 break;
9069 }
Joe Perches1d804d02015-03-30 16:46:09 -07009070 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009071}
9072
9073/*
9074 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9075 * should handle it ourselves in L0 (and then continue L2). Only call this
9076 * when in is_guest_mode (L2).
9077 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009078static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009079{
Nadav Har'El644d7112011-05-25 23:12:35 +03009080 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9081 struct vcpu_vmx *vmx = to_vmx(vcpu);
9082 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9083
Jim Mattson4f350c62017-09-14 16:31:44 -07009084 if (vmx->nested.nested_run_pending)
9085 return false;
9086
9087 if (unlikely(vmx->fail)) {
9088 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9089 vmcs_read32(VM_INSTRUCTION_ERROR));
9090 return true;
9091 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009092
David Matlackc9f04402017-08-01 14:00:40 -07009093 /*
9094 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009095 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9096 * Page). The CPU may write to these pages via their host
9097 * physical address while L2 is running, bypassing any
9098 * address-translation-based dirty tracking (e.g. EPT write
9099 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009100 *
9101 * Mark them dirty on every exit from L2 to prevent them from
9102 * getting out of sync with dirty tracking.
9103 */
9104 nested_mark_vmcs12_pages_dirty(vcpu);
9105
Jim Mattson4f350c62017-09-14 16:31:44 -07009106 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9107 vmcs_readl(EXIT_QUALIFICATION),
9108 vmx->idt_vectoring_info,
9109 intr_info,
9110 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9111 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009112
9113 switch (exit_reason) {
9114 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009115 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009116 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009117 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009118 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009119 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009120 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009121 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009122 else if (is_debug(intr_info) &&
9123 vcpu->guest_debug &
9124 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9125 return false;
9126 else if (is_breakpoint(intr_info) &&
9127 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9128 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009129 return vmcs12->exception_bitmap &
9130 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9131 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009132 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009133 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009134 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009135 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009136 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009137 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009138 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009139 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009140 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009141 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009142 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009143 case EXIT_REASON_HLT:
9144 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9145 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009146 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009147 case EXIT_REASON_INVLPG:
9148 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9149 case EXIT_REASON_RDPMC:
9150 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009151 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009152 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009153 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009154 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009155 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009156 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9157 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9158 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9159 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9160 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9161 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009162 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009163 /*
9164 * VMX instructions trap unconditionally. This allows L1 to
9165 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9166 */
Joe Perches1d804d02015-03-30 16:46:09 -07009167 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009168 case EXIT_REASON_CR_ACCESS:
9169 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9170 case EXIT_REASON_DR_ACCESS:
9171 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9172 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009173 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009174 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9175 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009176 case EXIT_REASON_MSR_READ:
9177 case EXIT_REASON_MSR_WRITE:
9178 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9179 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009180 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009181 case EXIT_REASON_MWAIT_INSTRUCTION:
9182 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009183 case EXIT_REASON_MONITOR_TRAP_FLAG:
9184 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009185 case EXIT_REASON_MONITOR_INSTRUCTION:
9186 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9187 case EXIT_REASON_PAUSE_INSTRUCTION:
9188 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9189 nested_cpu_has2(vmcs12,
9190 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9191 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009192 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009194 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009195 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009196 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009197 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009198 /*
9199 * The controls for "virtualize APIC accesses," "APIC-
9200 * register virtualization," and "virtual-interrupt
9201 * delivery" only come from vmcs12.
9202 */
Joe Perches1d804d02015-03-30 16:46:09 -07009203 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009204 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009205 /*
9206 * L0 always deals with the EPT violation. If nested EPT is
9207 * used, and the nested mmu code discovers that the address is
9208 * missing in the guest EPT table (EPT12), the EPT violation
9209 * will be injected with nested_ept_inject_page_fault()
9210 */
Joe Perches1d804d02015-03-30 16:46:09 -07009211 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009212 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009213 /*
9214 * L2 never uses directly L1's EPT, but rather L0's own EPT
9215 * table (shadow on EPT) or a merged EPT table that L0 built
9216 * (EPT on EPT). So any problems with the structure of the
9217 * table is L0's fault.
9218 */
Joe Perches1d804d02015-03-30 16:46:09 -07009219 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009220 case EXIT_REASON_INVPCID:
9221 return
9222 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9223 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009224 case EXIT_REASON_WBINVD:
9225 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9226 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009227 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009228 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9229 /*
9230 * This should never happen, since it is not possible to
9231 * set XSS to a non-zero value---neither in L1 nor in L2.
9232 * If if it were, XSS would have to be checked against
9233 * the XSS exit bitmap in vmcs12.
9234 */
9235 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009236 case EXIT_REASON_PREEMPTION_TIMER:
9237 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009238 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009239 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009240 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009241 case EXIT_REASON_VMFUNC:
9242 /* VM functions are emulated through L2->L0 vmexits. */
9243 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009244 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009245 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009246 }
9247}
9248
Paolo Bonzini7313c692017-07-27 10:31:25 +02009249static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9250{
9251 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9252
9253 /*
9254 * At this point, the exit interruption info in exit_intr_info
9255 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9256 * we need to query the in-kernel LAPIC.
9257 */
9258 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9259 if ((exit_intr_info &
9260 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9261 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9262 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9263 vmcs12->vm_exit_intr_error_code =
9264 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9265 }
9266
9267 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9268 vmcs_readl(EXIT_QUALIFICATION));
9269 return 1;
9270}
9271
Avi Kivity586f9602010-11-18 13:09:54 +02009272static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9273{
9274 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9275 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9276}
9277
Kai Huanga3eaa862015-11-04 13:46:05 +08009278static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009279{
Kai Huanga3eaa862015-11-04 13:46:05 +08009280 if (vmx->pml_pg) {
9281 __free_page(vmx->pml_pg);
9282 vmx->pml_pg = NULL;
9283 }
Kai Huang843e4332015-01-28 10:54:28 +08009284}
9285
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009286static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009287{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009288 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009289 u64 *pml_buf;
9290 u16 pml_idx;
9291
9292 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9293
9294 /* Do nothing if PML buffer is empty */
9295 if (pml_idx == (PML_ENTITY_NUM - 1))
9296 return;
9297
9298 /* PML index always points to next available PML buffer entity */
9299 if (pml_idx >= PML_ENTITY_NUM)
9300 pml_idx = 0;
9301 else
9302 pml_idx++;
9303
9304 pml_buf = page_address(vmx->pml_pg);
9305 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9306 u64 gpa;
9307
9308 gpa = pml_buf[pml_idx];
9309 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009310 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009311 }
9312
9313 /* reset PML index */
9314 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9315}
9316
9317/*
9318 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9319 * Called before reporting dirty_bitmap to userspace.
9320 */
9321static void kvm_flush_pml_buffers(struct kvm *kvm)
9322{
9323 int i;
9324 struct kvm_vcpu *vcpu;
9325 /*
9326 * We only need to kick vcpu out of guest mode here, as PML buffer
9327 * is flushed at beginning of all VMEXITs, and it's obvious that only
9328 * vcpus running in guest are possible to have unflushed GPAs in PML
9329 * buffer.
9330 */
9331 kvm_for_each_vcpu(i, vcpu, kvm)
9332 kvm_vcpu_kick(vcpu);
9333}
9334
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009335static void vmx_dump_sel(char *name, uint32_t sel)
9336{
9337 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009338 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009339 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9340 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9341 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9342}
9343
9344static void vmx_dump_dtsel(char *name, uint32_t limit)
9345{
9346 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9347 name, vmcs_read32(limit),
9348 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9349}
9350
9351static void dump_vmcs(void)
9352{
9353 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9354 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9355 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9356 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9357 u32 secondary_exec_control = 0;
9358 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009359 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009360 int i, n;
9361
9362 if (cpu_has_secondary_exec_ctrls())
9363 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9364
9365 pr_err("*** Guest State ***\n");
9366 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9367 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9368 vmcs_readl(CR0_GUEST_HOST_MASK));
9369 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9370 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9371 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9372 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9373 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9374 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009375 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9376 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9377 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9378 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009379 }
9380 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9381 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9382 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9383 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9384 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9385 vmcs_readl(GUEST_SYSENTER_ESP),
9386 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9387 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9388 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9389 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9390 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9391 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9392 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9393 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9394 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9395 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9396 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9397 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9398 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009399 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9400 efer, vmcs_read64(GUEST_IA32_PAT));
9401 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9402 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009403 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009404 if (cpu_has_load_perf_global_ctrl &&
9405 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009406 pr_err("PerfGlobCtl = 0x%016llx\n",
9407 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009408 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009409 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009410 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9411 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9412 vmcs_read32(GUEST_ACTIVITY_STATE));
9413 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9414 pr_err("InterruptStatus = %04x\n",
9415 vmcs_read16(GUEST_INTR_STATUS));
9416
9417 pr_err("*** Host State ***\n");
9418 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9419 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9420 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9421 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9422 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9423 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9424 vmcs_read16(HOST_TR_SELECTOR));
9425 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9426 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9427 vmcs_readl(HOST_TR_BASE));
9428 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9429 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9430 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9431 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9432 vmcs_readl(HOST_CR4));
9433 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9434 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9435 vmcs_read32(HOST_IA32_SYSENTER_CS),
9436 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9437 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009438 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9439 vmcs_read64(HOST_IA32_EFER),
9440 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009441 if (cpu_has_load_perf_global_ctrl &&
9442 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009443 pr_err("PerfGlobCtl = 0x%016llx\n",
9444 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009445
9446 pr_err("*** Control State ***\n");
9447 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9448 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9449 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9450 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9451 vmcs_read32(EXCEPTION_BITMAP),
9452 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9453 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9454 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9455 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9456 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9457 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9458 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9459 vmcs_read32(VM_EXIT_INTR_INFO),
9460 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9461 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9462 pr_err(" reason=%08x qualification=%016lx\n",
9463 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9464 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9465 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9466 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009467 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009468 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009469 pr_err("TSC Multiplier = 0x%016llx\n",
9470 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009471 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9472 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9473 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9474 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9475 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009476 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009477 n = vmcs_read32(CR3_TARGET_COUNT);
9478 for (i = 0; i + 1 < n; i += 4)
9479 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9480 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9481 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9482 if (i < n)
9483 pr_err("CR3 target%u=%016lx\n",
9484 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9485 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9486 pr_err("PLE Gap=%08x Window=%08x\n",
9487 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9488 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9489 pr_err("Virtual processor ID = 0x%04x\n",
9490 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9491}
9492
Avi Kivity6aa8b732006-12-10 02:21:36 -08009493/*
9494 * The guest has exited. See if we can fix it or if we need userspace
9495 * assistance.
9496 */
Avi Kivity851ba692009-08-24 11:10:17 +03009497static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009498{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009499 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009500 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009501 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009502
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009503 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9504
Kai Huang843e4332015-01-28 10:54:28 +08009505 /*
9506 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9507 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9508 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9509 * mode as if vcpus is in root mode, the PML buffer must has been
9510 * flushed already.
9511 */
9512 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009513 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009514
Mohammed Gamal80ced182009-09-01 12:48:18 +02009515 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009516 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009517 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009518
Paolo Bonzini7313c692017-07-27 10:31:25 +02009519 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9520 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009521
Mohammed Gamal51207022010-05-31 22:40:54 +03009522 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009523 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009524 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9525 vcpu->run->fail_entry.hardware_entry_failure_reason
9526 = exit_reason;
9527 return 0;
9528 }
9529
Avi Kivity29bd8a72007-09-10 17:27:03 +03009530 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009531 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9532 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009533 = vmcs_read32(VM_INSTRUCTION_ERROR);
9534 return 0;
9535 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009536
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009537 /*
9538 * Note:
9539 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9540 * delivery event since it indicates guest is accessing MMIO.
9541 * The vm-exit can be triggered again after return to guest that
9542 * will cause infinite loop.
9543 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009544 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009545 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009546 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009547 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009548 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9549 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9550 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009551 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009552 vcpu->run->internal.data[0] = vectoring_info;
9553 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009554 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9555 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9556 vcpu->run->internal.ndata++;
9557 vcpu->run->internal.data[3] =
9558 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9559 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009560 return 0;
9561 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009562
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009563 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009564 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9565 if (vmx_interrupt_allowed(vcpu)) {
9566 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9567 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9568 vcpu->arch.nmi_pending) {
9569 /*
9570 * This CPU don't support us in finding the end of an
9571 * NMI-blocked window if the guest runs with IRQs
9572 * disabled. So we pull the trigger after 1 s of
9573 * futile waiting, but inform the user about this.
9574 */
9575 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9576 "state on VCPU %d after 1 s timeout\n",
9577 __func__, vcpu->vcpu_id);
9578 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9579 }
9580 }
9581
Avi Kivity6aa8b732006-12-10 02:21:36 -08009582 if (exit_reason < kvm_vmx_max_exit_handlers
9583 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009584 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009585 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009586 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9587 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009588 kvm_queue_exception(vcpu, UD_VECTOR);
9589 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009590 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009591}
9592
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009593static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009594{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009595 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9596
9597 if (is_guest_mode(vcpu) &&
9598 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9599 return;
9600
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009601 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009602 vmcs_write32(TPR_THRESHOLD, 0);
9603 return;
9604 }
9605
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009606 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009607}
9608
Jim Mattson8d860bb2018-05-09 16:56:05 -04009609static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009610{
9611 u32 sec_exec_control;
9612
Jim Mattson8d860bb2018-05-09 16:56:05 -04009613 if (!lapic_in_kernel(vcpu))
9614 return;
9615
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009616 /* Postpone execution until vmcs01 is the current VMCS. */
9617 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009618 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009619 return;
9620 }
9621
Paolo Bonzini35754c92015-07-29 12:05:37 +02009622 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009623 return;
9624
9625 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009626 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9627 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009628
Jim Mattson8d860bb2018-05-09 16:56:05 -04009629 switch (kvm_get_apic_mode(vcpu)) {
9630 case LAPIC_MODE_INVALID:
9631 WARN_ONCE(true, "Invalid local APIC state");
9632 case LAPIC_MODE_DISABLED:
9633 break;
9634 case LAPIC_MODE_XAPIC:
9635 if (flexpriority_enabled) {
9636 sec_exec_control |=
9637 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9638 vmx_flush_tlb(vcpu, true);
9639 }
9640 break;
9641 case LAPIC_MODE_X2APIC:
9642 if (cpu_has_vmx_virtualize_x2apic_mode())
9643 sec_exec_control |=
9644 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9645 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009646 }
9647 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9648
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009649 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009650}
9651
Tang Chen38b99172014-09-24 15:57:54 +08009652static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9653{
Jim Mattsonab5df312018-05-09 17:02:03 -04009654 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009655 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009656 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009657 }
Tang Chen38b99172014-09-24 15:57:54 +08009658}
9659
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009660static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009661{
9662 u16 status;
9663 u8 old;
9664
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009665 if (max_isr == -1)
9666 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009667
9668 status = vmcs_read16(GUEST_INTR_STATUS);
9669 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009670 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009671 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009672 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009673 vmcs_write16(GUEST_INTR_STATUS, status);
9674 }
9675}
9676
9677static void vmx_set_rvi(int vector)
9678{
9679 u16 status;
9680 u8 old;
9681
Wei Wang4114c272014-11-05 10:53:43 +08009682 if (vector == -1)
9683 vector = 0;
9684
Yang Zhangc7c9c562013-01-25 10:18:51 +08009685 status = vmcs_read16(GUEST_INTR_STATUS);
9686 old = (u8)status & 0xff;
9687 if ((u8)vector != old) {
9688 status &= ~0xff;
9689 status |= (u8)vector;
9690 vmcs_write16(GUEST_INTR_STATUS, status);
9691 }
9692}
9693
9694static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9695{
Liran Alon851c1a182017-12-24 18:12:56 +02009696 /*
9697 * When running L2, updating RVI is only relevant when
9698 * vmcs12 virtual-interrupt-delivery enabled.
9699 * However, it can be enabled only when L1 also
9700 * intercepts external-interrupts and in that case
9701 * we should not update vmcs02 RVI but instead intercept
9702 * interrupt. Therefore, do nothing when running L2.
9703 */
9704 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009705 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009706}
9707
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009708static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009709{
9710 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009711 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009712 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009713
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009714 WARN_ON(!vcpu->arch.apicv_active);
9715 if (pi_test_on(&vmx->pi_desc)) {
9716 pi_clear_on(&vmx->pi_desc);
9717 /*
9718 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9719 * But on x86 this is just a compiler barrier anyway.
9720 */
9721 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009722 max_irr_updated =
9723 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9724
9725 /*
9726 * If we are running L2 and L1 has a new pending interrupt
9727 * which can be injected, we should re-evaluate
9728 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009729 * If L1 intercepts external-interrupts, we should
9730 * exit from L2 to L1. Otherwise, interrupt should be
9731 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009732 */
Liran Alon851c1a182017-12-24 18:12:56 +02009733 if (is_guest_mode(vcpu) && max_irr_updated) {
9734 if (nested_exit_on_intr(vcpu))
9735 kvm_vcpu_exiting_guest_mode(vcpu);
9736 else
9737 kvm_make_request(KVM_REQ_EVENT, vcpu);
9738 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009739 } else {
9740 max_irr = kvm_lapic_find_highest_irr(vcpu);
9741 }
9742 vmx_hwapic_irr_update(vcpu, max_irr);
9743 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009744}
9745
Andrey Smetanin63086302015-11-10 15:36:32 +03009746static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009747{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009748 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009749 return;
9750
Yang Zhangc7c9c562013-01-25 10:18:51 +08009751 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9752 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9753 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9754 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9755}
9756
Paolo Bonzini967235d2016-12-19 14:03:45 +01009757static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9758{
9759 struct vcpu_vmx *vmx = to_vmx(vcpu);
9760
9761 pi_clear_on(&vmx->pi_desc);
9762 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9763}
9764
Avi Kivity51aa01d2010-07-20 14:31:20 +03009765static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009766{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009767 u32 exit_intr_info = 0;
9768 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009769
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009770 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9771 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009772 return;
9773
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009774 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9775 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9776 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009777
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009778 /* if exit due to PF check for async PF */
9779 if (is_page_fault(exit_intr_info))
9780 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9781
Andi Kleena0861c02009-06-08 17:37:09 +08009782 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009783 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9784 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009785 kvm_machine_check();
9786
Gleb Natapov20f65982009-05-11 13:35:55 +03009787 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009788 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009789 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009790 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009791 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009792 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009793}
Gleb Natapov20f65982009-05-11 13:35:55 +03009794
Yang Zhanga547c6d2013-04-11 19:25:10 +08009795static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9796{
9797 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9798
Yang Zhanga547c6d2013-04-11 19:25:10 +08009799 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9800 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9801 unsigned int vector;
9802 unsigned long entry;
9803 gate_desc *desc;
9804 struct vcpu_vmx *vmx = to_vmx(vcpu);
9805#ifdef CONFIG_X86_64
9806 unsigned long tmp;
9807#endif
9808
9809 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9810 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009811 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009812 asm volatile(
9813#ifdef CONFIG_X86_64
9814 "mov %%" _ASM_SP ", %[sp]\n\t"
9815 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9816 "push $%c[ss]\n\t"
9817 "push %[sp]\n\t"
9818#endif
9819 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009820 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009821 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009822 :
9823#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009824 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009825#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009826 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009827 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009828 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009829 [ss]"i"(__KERNEL_DS),
9830 [cs]"i"(__KERNEL_CS)
9831 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009832 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009833}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009834STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009835
Tom Lendackybc226f02018-05-10 22:06:39 +02009836static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009837{
Tom Lendackybc226f02018-05-10 22:06:39 +02009838 switch (index) {
9839 case MSR_IA32_SMBASE:
9840 /*
9841 * We cannot do SMM unless we can run the guest in big
9842 * real mode.
9843 */
9844 return enable_unrestricted_guest || emulate_invalid_guest_state;
9845 case MSR_AMD64_VIRT_SPEC_CTRL:
9846 /* This is AMD only. */
9847 return false;
9848 default:
9849 return true;
9850 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009851}
9852
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009853static bool vmx_mpx_supported(void)
9854{
9855 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9856 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9857}
9858
Wanpeng Li55412b22014-12-02 19:21:30 +08009859static bool vmx_xsaves_supported(void)
9860{
9861 return vmcs_config.cpu_based_2nd_exec_ctrl &
9862 SECONDARY_EXEC_XSAVES;
9863}
9864
Avi Kivity51aa01d2010-07-20 14:31:20 +03009865static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9866{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009867 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009868 bool unblock_nmi;
9869 u8 vector;
9870 bool idtv_info_valid;
9871
9872 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009873
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009874 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009875 if (vmx->loaded_vmcs->nmi_known_unmasked)
9876 return;
9877 /*
9878 * Can't use vmx->exit_intr_info since we're not sure what
9879 * the exit reason is.
9880 */
9881 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9882 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9883 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9884 /*
9885 * SDM 3: 27.7.1.2 (September 2008)
9886 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9887 * a guest IRET fault.
9888 * SDM 3: 23.2.2 (September 2008)
9889 * Bit 12 is undefined in any of the following cases:
9890 * If the VM exit sets the valid bit in the IDT-vectoring
9891 * information field.
9892 * If the VM exit is due to a double fault.
9893 */
9894 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9895 vector != DF_VECTOR && !idtv_info_valid)
9896 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9897 GUEST_INTR_STATE_NMI);
9898 else
9899 vmx->loaded_vmcs->nmi_known_unmasked =
9900 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9901 & GUEST_INTR_STATE_NMI);
9902 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9903 vmx->loaded_vmcs->vnmi_blocked_time +=
9904 ktime_to_ns(ktime_sub(ktime_get(),
9905 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009906}
9907
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009908static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009909 u32 idt_vectoring_info,
9910 int instr_len_field,
9911 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009912{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009913 u8 vector;
9914 int type;
9915 bool idtv_info_valid;
9916
9917 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009918
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009919 vcpu->arch.nmi_injected = false;
9920 kvm_clear_exception_queue(vcpu);
9921 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009922
9923 if (!idtv_info_valid)
9924 return;
9925
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009926 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009927
Avi Kivity668f6122008-07-02 09:28:55 +03009928 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9929 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009930
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009931 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009932 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009933 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009934 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009935 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009936 * Clear bit "block by NMI" before VM entry if a NMI
9937 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009938 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009939 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009940 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009941 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009942 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009943 /* fall through */
9944 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009945 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009946 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009947 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009948 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009949 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009950 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009951 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009952 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009953 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009954 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009955 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009956 break;
9957 default:
9958 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009959 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009960}
9961
Avi Kivity83422e12010-07-20 14:43:23 +03009962static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9963{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009964 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009965 VM_EXIT_INSTRUCTION_LEN,
9966 IDT_VECTORING_ERROR_CODE);
9967}
9968
Avi Kivityb463a6f2010-07-20 15:06:17 +03009969static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9970{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009971 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009972 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9973 VM_ENTRY_INSTRUCTION_LEN,
9974 VM_ENTRY_EXCEPTION_ERROR_CODE);
9975
9976 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9977}
9978
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009979static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9980{
9981 int i, nr_msrs;
9982 struct perf_guest_switch_msr *msrs;
9983
9984 msrs = perf_guest_get_msrs(&nr_msrs);
9985
9986 if (!msrs)
9987 return;
9988
9989 for (i = 0; i < nr_msrs; i++)
9990 if (msrs[i].host == msrs[i].guest)
9991 clear_atomic_switch_msr(vmx, msrs[i].msr);
9992 else
9993 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9994 msrs[i].host);
9995}
9996
Jiang Biao33365e72016-11-03 15:03:37 +08009997static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009998{
9999 struct vcpu_vmx *vmx = to_vmx(vcpu);
10000 u64 tscl;
10001 u32 delta_tsc;
10002
10003 if (vmx->hv_deadline_tsc == -1)
10004 return;
10005
10006 tscl = rdtsc();
10007 if (vmx->hv_deadline_tsc > tscl)
10008 /* sure to be 32 bit only because checked on set_hv_timer */
10009 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10010 cpu_preemption_timer_multi);
10011 else
10012 delta_tsc = 0;
10013
10014 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10015}
10016
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010017static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010018{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010019 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010020 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010021
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010022 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010023 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010024 vmx->loaded_vmcs->soft_vnmi_blocked))
10025 vmx->loaded_vmcs->entry_time = ktime_get();
10026
Avi Kivity104f2262010-11-18 13:12:52 +020010027 /* Don't enter VMX if guest state is invalid, let the exit handler
10028 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010029 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010030 return;
10031
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010032 if (vmx->ple_window_dirty) {
10033 vmx->ple_window_dirty = false;
10034 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10035 }
10036
Abel Gordon012f83c2013-04-18 14:39:25 +030010037 if (vmx->nested.sync_shadow_vmcs) {
10038 copy_vmcs12_to_shadow(vmx);
10039 vmx->nested.sync_shadow_vmcs = false;
10040 }
10041
Avi Kivity104f2262010-11-18 13:12:52 +020010042 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10043 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10044 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10045 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10046
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010047 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010048 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010049 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010050 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010051 }
10052
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010053 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010054 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010055 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010056 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010057 }
10058
Avi Kivity104f2262010-11-18 13:12:52 +020010059 /* When single-stepping over STI and MOV SS, we must clear the
10060 * corresponding interruptibility bits in the guest state. Otherwise
10061 * vmentry fails as it then expects bit 14 (BS) in pending debug
10062 * exceptions being set, but that's not correct for the guest debugging
10063 * case. */
10064 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10065 vmx_set_interrupt_shadow(vcpu, 0);
10066
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010067 if (static_cpu_has(X86_FEATURE_PKU) &&
10068 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10069 vcpu->arch.pkru != vmx->host_pkru)
10070 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010071
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010072 atomic_switch_perf_msrs(vmx);
10073
Yunhong Jiang64672c92016-06-13 14:19:59 -070010074 vmx_arm_hv_timer(vcpu);
10075
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010076 /*
10077 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10078 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10079 * is no need to worry about the conditional branch over the wrmsr
10080 * being speculatively taken.
10081 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010082 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010083
Nadav Har'Eld462b812011-05-24 15:26:10 +030010084 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010085
10086 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10087 (unsigned long)&current_evmcs->host_rsp : 0;
10088
Avi Kivity104f2262010-11-18 13:12:52 +020010089 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010090 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010091 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10092 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10093 "push %%" _ASM_CX " \n\t"
10094 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010095 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010096 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010097 /* Avoid VMWRITE when Enlightened VMCS is in use */
10098 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10099 "jz 2f \n\t"
10100 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10101 "jmp 1f \n\t"
10102 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010103 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010104 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010105 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010106 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10107 "mov %%cr2, %%" _ASM_DX " \n\t"
10108 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010109 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010110 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010111 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010112 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010113 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010114 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010115 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10116 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10117 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10118 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10119 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10120 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010121#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010122 "mov %c[r8](%0), %%r8 \n\t"
10123 "mov %c[r9](%0), %%r9 \n\t"
10124 "mov %c[r10](%0), %%r10 \n\t"
10125 "mov %c[r11](%0), %%r11 \n\t"
10126 "mov %c[r12](%0), %%r12 \n\t"
10127 "mov %c[r13](%0), %%r13 \n\t"
10128 "mov %c[r14](%0), %%r14 \n\t"
10129 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010130#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010131 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010132
Avi Kivity6aa8b732006-12-10 02:21:36 -080010133 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010134 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010135 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010136 "jmp 2f \n\t"
10137 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10138 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010139 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010140 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010141 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010142 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010143 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10144 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10145 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10146 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10147 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10148 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10149 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010150#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010151 "mov %%r8, %c[r8](%0) \n\t"
10152 "mov %%r9, %c[r9](%0) \n\t"
10153 "mov %%r10, %c[r10](%0) \n\t"
10154 "mov %%r11, %c[r11](%0) \n\t"
10155 "mov %%r12, %c[r12](%0) \n\t"
10156 "mov %%r13, %c[r13](%0) \n\t"
10157 "mov %%r14, %c[r14](%0) \n\t"
10158 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010159 "xor %%r8d, %%r8d \n\t"
10160 "xor %%r9d, %%r9d \n\t"
10161 "xor %%r10d, %%r10d \n\t"
10162 "xor %%r11d, %%r11d \n\t"
10163 "xor %%r12d, %%r12d \n\t"
10164 "xor %%r13d, %%r13d \n\t"
10165 "xor %%r14d, %%r14d \n\t"
10166 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010167#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010168 "mov %%cr2, %%" _ASM_AX " \n\t"
10169 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010170
Jim Mattson0cb5b302018-01-03 14:31:38 -080010171 "xor %%eax, %%eax \n\t"
10172 "xor %%ebx, %%ebx \n\t"
10173 "xor %%esi, %%esi \n\t"
10174 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010175 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010176 ".pushsection .rodata \n\t"
10177 ".global vmx_return \n\t"
10178 "vmx_return: " _ASM_PTR " 2b \n\t"
10179 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010180 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010181 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010182 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010183 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010184 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10185 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10186 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10187 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10188 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10189 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10190 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010191#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010192 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10193 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10194 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10195 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10196 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10197 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10198 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10199 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010200#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010201 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10202 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010203 : "cc", "memory"
10204#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010205 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010206 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010207#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010208 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010209#endif
10210 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010211
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010212 /*
10213 * We do not use IBRS in the kernel. If this vCPU has used the
10214 * SPEC_CTRL MSR it may have left it on; save the value and
10215 * turn it off. This is much more efficient than blindly adding
10216 * it to the atomic save/restore list. Especially as the former
10217 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10218 *
10219 * For non-nested case:
10220 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10221 * save it.
10222 *
10223 * For nested case:
10224 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10225 * save it.
10226 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010227 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010228 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010229
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010230 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010231
David Woodhouse117cc7a2018-01-12 11:11:27 +000010232 /* Eliminate branch target predictions from guest mode */
10233 vmexit_fill_RSB();
10234
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010235 /* All fields are clean at this point */
10236 if (static_branch_unlikely(&enable_evmcs))
10237 current_evmcs->hv_clean_fields |=
10238 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10239
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010240 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010241 if (vmx->host_debugctlmsr)
10242 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010243
Avi Kivityaa67f602012-08-01 16:48:03 +030010244#ifndef CONFIG_X86_64
10245 /*
10246 * The sysexit path does not restore ds/es, so we must set them to
10247 * a reasonable value ourselves.
10248 *
10249 * We can't defer this to vmx_load_host_state() since that function
10250 * may be executed in interrupt context, which saves and restore segments
10251 * around it, nullifying its effect.
10252 */
10253 loadsegment(ds, __USER_DS);
10254 loadsegment(es, __USER_DS);
10255#endif
10256
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010257 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010258 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010259 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010260 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010261 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010262 vcpu->arch.regs_dirty = 0;
10263
Gleb Natapove0b890d2013-09-25 12:51:33 +030010264 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010265 * eager fpu is enabled if PKEY is supported and CR4 is switched
10266 * back on host, so it is safe to read guest PKRU from current
10267 * XSAVE.
10268 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010269 if (static_cpu_has(X86_FEATURE_PKU) &&
10270 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10271 vcpu->arch.pkru = __read_pkru();
10272 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010273 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010274 }
10275
Gleb Natapove0b890d2013-09-25 12:51:33 +030010276 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010277 vmx->idt_vectoring_info = 0;
10278
10279 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10280 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10281 return;
10282
10283 vmx->loaded_vmcs->launched = 1;
10284 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010285
Avi Kivity51aa01d2010-07-20 14:31:20 +030010286 vmx_complete_atomic_exit(vmx);
10287 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010288 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010289}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010290STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010291
Sean Christopherson434a1e92018-03-20 12:17:18 -070010292static struct kvm *vmx_vm_alloc(void)
10293{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010294 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010295 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010296}
10297
10298static void vmx_vm_free(struct kvm *kvm)
10299{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010300 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010301}
10302
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010303static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010304{
10305 struct vcpu_vmx *vmx = to_vmx(vcpu);
10306 int cpu;
10307
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010308 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010309 return;
10310
10311 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010312 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010313 vmx_vcpu_put(vcpu);
10314 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010315 put_cpu();
10316}
10317
Jim Mattson2f1fe812016-07-08 15:36:06 -070010318/*
10319 * Ensure that the current vmcs of the logical processor is the
10320 * vmcs01 of the vcpu before calling free_nested().
10321 */
10322static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10323{
10324 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010325
Christoffer Dallec7660c2017-12-04 21:35:23 +010010326 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010327 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010328 free_nested(vmx);
10329 vcpu_put(vcpu);
10330}
10331
Avi Kivity6aa8b732006-12-10 02:21:36 -080010332static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10333{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010334 struct vcpu_vmx *vmx = to_vmx(vcpu);
10335
Kai Huang843e4332015-01-28 10:54:28 +080010336 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010337 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010338 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010339 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010340 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010341 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010342 kfree(vmx->guest_msrs);
10343 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010344 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010345}
10346
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010347static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010348{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010349 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010350 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010351 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010352 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010353
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010354 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010355 return ERR_PTR(-ENOMEM);
10356
Wanpeng Li991e7a02015-09-16 17:30:05 +080010357 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010358
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010359 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10360 if (err)
10361 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010362
Peter Feiner4e595162016-07-07 14:49:58 -070010363 err = -ENOMEM;
10364
10365 /*
10366 * If PML is turned on, failure on enabling PML just results in failure
10367 * of creating the vcpu, therefore we can simplify PML logic (by
10368 * avoiding dealing with cases, such as enabling PML partially on vcpus
10369 * for the guest, etc.
10370 */
10371 if (enable_pml) {
10372 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10373 if (!vmx->pml_pg)
10374 goto uninit_vcpu;
10375 }
10376
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010377 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010378 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10379 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010380
Peter Feiner4e595162016-07-07 14:49:58 -070010381 if (!vmx->guest_msrs)
10382 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010383
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010384 err = alloc_loaded_vmcs(&vmx->vmcs01);
10385 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010386 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010387
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010388 msr_bitmap = vmx->vmcs01.msr_bitmap;
10389 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10390 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10391 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10392 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10393 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10394 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10395 vmx->msr_bitmap_mode = 0;
10396
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010397 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010398 cpu = get_cpu();
10399 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010400 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010401 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010402 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010403 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010404 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010405 err = alloc_apic_access_page(kvm);
10406 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010407 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010408 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010409
Sean Christophersone90008d2018-03-05 12:04:37 -080010410 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010411 err = init_rmode_identity_map(kvm);
10412 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010413 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010414 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010415
Wanpeng Li5c614b32015-10-13 09:18:36 -070010416 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010417 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10418 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010419 vmx->nested.vpid02 = allocate_vpid();
10420 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010421
Wincy Van705699a2015-02-03 23:58:17 +080010422 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010423 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010424
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010425 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10426
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010427 /*
10428 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10429 * or POSTED_INTR_WAKEUP_VECTOR.
10430 */
10431 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10432 vmx->pi_desc.sn = 1;
10433
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010434 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010435
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010436free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010437 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010438 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010439free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010440 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010441free_pml:
10442 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010443uninit_vcpu:
10444 kvm_vcpu_uninit(&vmx->vcpu);
10445free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010446 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010447 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010448 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010449}
10450
Wanpeng Lib31c1142018-03-12 04:53:04 -070010451static int vmx_vm_init(struct kvm *kvm)
10452{
10453 if (!ple_gap)
10454 kvm->arch.pause_in_guest = true;
10455 return 0;
10456}
10457
Yang, Sheng002c7f72007-07-31 14:23:01 +030010458static void __init vmx_check_processor_compat(void *rtn)
10459{
10460 struct vmcs_config vmcs_conf;
10461
10462 *(int *)rtn = 0;
10463 if (setup_vmcs_config(&vmcs_conf) < 0)
10464 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010465 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010466 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10467 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10468 smp_processor_id());
10469 *(int *)rtn = -EIO;
10470 }
10471}
10472
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010473static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010474{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010475 u8 cache;
10476 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010477
Sheng Yang522c68c2009-04-27 20:35:43 +080010478 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010479 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010480 * 2. EPT with VT-d:
10481 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010482 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010483 * b. VT-d with snooping control feature: snooping control feature of
10484 * VT-d engine can guarantee the cache correctness. Just set it
10485 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010486 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010487 * consistent with host MTRR
10488 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010489 if (is_mmio) {
10490 cache = MTRR_TYPE_UNCACHABLE;
10491 goto exit;
10492 }
10493
10494 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010495 ipat = VMX_EPT_IPAT_BIT;
10496 cache = MTRR_TYPE_WRBACK;
10497 goto exit;
10498 }
10499
10500 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10501 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010502 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010503 cache = MTRR_TYPE_WRBACK;
10504 else
10505 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010506 goto exit;
10507 }
10508
Xiao Guangrongff536042015-06-15 16:55:22 +080010509 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010510
10511exit:
10512 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010513}
10514
Sheng Yang17cc3932010-01-05 19:02:27 +080010515static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010516{
Sheng Yang878403b2010-01-05 19:02:29 +080010517 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10518 return PT_DIRECTORY_LEVEL;
10519 else
10520 /* For shadow and EPT supported 1GB page */
10521 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010522}
10523
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010524static void vmcs_set_secondary_exec_control(u32 new_ctl)
10525{
10526 /*
10527 * These bits in the secondary execution controls field
10528 * are dynamic, the others are mostly based on the hypervisor
10529 * architecture and the guest's CPUID. Do not touch the
10530 * dynamic bits.
10531 */
10532 u32 mask =
10533 SECONDARY_EXEC_SHADOW_VMCS |
10534 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010535 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10536 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010537
10538 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10539
10540 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10541 (new_ctl & ~mask) | (cur_ctl & mask));
10542}
10543
David Matlack8322ebb2016-11-29 18:14:09 -080010544/*
10545 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10546 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10547 */
10548static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10549{
10550 struct vcpu_vmx *vmx = to_vmx(vcpu);
10551 struct kvm_cpuid_entry2 *entry;
10552
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010553 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10554 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010555
10556#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10557 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010558 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010559} while (0)
10560
10561 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10562 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10563 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10564 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10565 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10566 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10567 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10568 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10569 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10570 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10571 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10572 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10573 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10574 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10575 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10576
10577 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10578 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10579 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10580 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10581 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010582 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010583
10584#undef cr4_fixed1_update
10585}
10586
Sheng Yang0e851882009-12-18 16:48:46 +080010587static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10588{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010589 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010590
Paolo Bonzini80154d72017-08-24 13:55:35 +020010591 if (cpu_has_secondary_exec_ctrls()) {
10592 vmx_compute_secondary_exec_control(vmx);
10593 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010594 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010595
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010596 if (nested_vmx_allowed(vcpu))
10597 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10598 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10599 else
10600 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10601 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010602
10603 if (nested_vmx_allowed(vcpu))
10604 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010605}
10606
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010607static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10608{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010609 if (func == 1 && nested)
10610 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010611}
10612
Yang Zhang25d92082013-08-06 12:00:32 +030010613static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10614 struct x86_exception *fault)
10615{
Jan Kiszka533558b2014-01-04 18:47:20 +010010616 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010617 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010618 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010619 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010620
Bandan Dasc5f983f2017-05-05 15:25:14 -040010621 if (vmx->nested.pml_full) {
10622 exit_reason = EXIT_REASON_PML_FULL;
10623 vmx->nested.pml_full = false;
10624 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10625 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010626 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010627 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010628 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010629
10630 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010631 vmcs12->guest_physical_address = fault->address;
10632}
10633
Peter Feiner995f00a2017-06-30 17:26:32 -070010634static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10635{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010636 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010637}
10638
Nadav Har'El155a97a2013-08-05 11:07:16 +030010639/* Callbacks for nested_ept_init_mmu_context: */
10640
10641static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10642{
10643 /* return the page table to be shadowed - in our case, EPT12 */
10644 return get_vmcs12(vcpu)->ept_pointer;
10645}
10646
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010647static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010648{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010649 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010650 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010651 return 1;
10652
10653 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010654 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010655 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010656 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010657 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010658 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10659 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10660 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10661
10662 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010663 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010664}
10665
10666static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10667{
10668 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10669}
10670
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010671static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10672 u16 error_code)
10673{
10674 bool inequality, bit;
10675
10676 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10677 inequality =
10678 (error_code & vmcs12->page_fault_error_code_mask) !=
10679 vmcs12->page_fault_error_code_match;
10680 return inequality ^ bit;
10681}
10682
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010683static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10684 struct x86_exception *fault)
10685{
10686 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10687
10688 WARN_ON(!is_guest_mode(vcpu));
10689
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010690 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10691 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010692 vmcs12->vm_exit_intr_error_code = fault->error_code;
10693 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10694 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10695 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10696 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010697 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010698 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010699 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010700}
10701
Paolo Bonzinic9923842017-12-13 14:16:30 +010010702static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10703 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010704
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010705static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010706{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010707 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010708 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010709 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010710 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010711
10712 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010713 /*
10714 * Translate L1 physical address to host physical
10715 * address for vmcs02. Keep the page pinned, so this
10716 * physical address remains valid. We keep a reference
10717 * to it so we can release it later.
10718 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010719 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010720 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010721 vmx->nested.apic_access_page = NULL;
10722 }
10723 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010724 /*
10725 * If translation failed, no matter: This feature asks
10726 * to exit when accessing the given address, and if it
10727 * can never be accessed, this feature won't do
10728 * anything anyway.
10729 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010730 if (!is_error_page(page)) {
10731 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010732 hpa = page_to_phys(vmx->nested.apic_access_page);
10733 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10734 } else {
10735 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10736 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10737 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010738 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010739
10740 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010741 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010742 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010743 vmx->nested.virtual_apic_page = NULL;
10744 }
10745 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010746
10747 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010748 * If translation failed, VM entry will fail because
10749 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10750 * Failing the vm entry is _not_ what the processor
10751 * does but it's basically the only possibility we
10752 * have. We could still enter the guest if CR8 load
10753 * exits are enabled, CR8 store exits are enabled, and
10754 * virtualize APIC access is disabled; in this case
10755 * the processor would never use the TPR shadow and we
10756 * could simply clear the bit from the execution
10757 * control. But such a configuration is useless, so
10758 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010759 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010760 if (!is_error_page(page)) {
10761 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010762 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10763 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10764 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010765 }
10766
Wincy Van705699a2015-02-03 23:58:17 +080010767 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010768 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10769 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010770 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010771 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010772 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010773 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10774 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010775 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010776 vmx->nested.pi_desc_page = page;
10777 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010778 vmx->nested.pi_desc =
10779 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10780 (unsigned long)(vmcs12->posted_intr_desc_addr &
10781 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010782 vmcs_write64(POSTED_INTR_DESC_ADDR,
10783 page_to_phys(vmx->nested.pi_desc_page) +
10784 (unsigned long)(vmcs12->posted_intr_desc_addr &
10785 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010786 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010787 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010788 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10789 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010790 else
10791 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10792 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010793}
10794
Jan Kiszkaf4124502014-03-07 20:03:13 +010010795static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10796{
10797 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10798 struct vcpu_vmx *vmx = to_vmx(vcpu);
10799
10800 if (vcpu->arch.virtual_tsc_khz == 0)
10801 return;
10802
10803 /* Make sure short timeouts reliably trigger an immediate vmexit.
10804 * hrtimer_start does not guarantee this. */
10805 if (preemption_timeout <= 1) {
10806 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10807 return;
10808 }
10809
10810 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10811 preemption_timeout *= 1000000;
10812 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10813 hrtimer_start(&vmx->nested.preemption_timer,
10814 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10815}
10816
Jim Mattson56a20512017-07-06 16:33:06 -070010817static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10818 struct vmcs12 *vmcs12)
10819{
10820 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10821 return 0;
10822
10823 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10824 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10825 return -EINVAL;
10826
10827 return 0;
10828}
10829
Wincy Van3af18d92015-02-03 23:49:31 +080010830static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10831 struct vmcs12 *vmcs12)
10832{
Wincy Van3af18d92015-02-03 23:49:31 +080010833 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10834 return 0;
10835
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010836 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010837 return -EINVAL;
10838
10839 return 0;
10840}
10841
Jim Mattson712b12d2017-08-24 13:24:47 -070010842static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10843 struct vmcs12 *vmcs12)
10844{
10845 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10846 return 0;
10847
10848 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10849 return -EINVAL;
10850
10851 return 0;
10852}
10853
Wincy Van3af18d92015-02-03 23:49:31 +080010854/*
10855 * Merge L0's and L1's MSR bitmap, return false to indicate that
10856 * we do not use the hardware.
10857 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010858static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10859 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010860{
Wincy Van82f0dd42015-02-03 23:57:18 +080010861 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010862 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010863 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010864 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010865 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010866 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010867 *
10868 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10869 * ensures that we do not accidentally generate an L02 MSR bitmap
10870 * from the L12 MSR bitmap that is too permissive.
10871 * 2. That L1 or L2s have actually used the MSR. This avoids
10872 * unnecessarily merging of the bitmap if the MSR is unused. This
10873 * works properly because we only update the L01 MSR bitmap lazily.
10874 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10875 * updated to reflect this when L1 (or its L2s) actually write to
10876 * the MSR.
10877 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010878 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10879 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010880
Paolo Bonzinic9923842017-12-13 14:16:30 +010010881 /* Nothing to do if the MSR bitmap is not in use. */
10882 if (!cpu_has_vmx_msr_bitmap() ||
10883 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10884 return false;
10885
Ashok Raj15d45072018-02-01 22:59:43 +010010886 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010887 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010888 return false;
10889
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010890 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10891 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010892 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010893
Radim Krčmářd048c092016-08-08 20:16:22 +020010894 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010895 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10896 /*
10897 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10898 * just lets the processor take the value from the virtual-APIC page;
10899 * take those 256 bits directly from the L1 bitmap.
10900 */
10901 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10902 unsigned word = msr / BITS_PER_LONG;
10903 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10904 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010905 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010906 } else {
10907 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10908 unsigned word = msr / BITS_PER_LONG;
10909 msr_bitmap_l0[word] = ~0;
10910 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10911 }
10912 }
10913
10914 nested_vmx_disable_intercept_for_msr(
10915 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010916 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010917 MSR_TYPE_W);
10918
10919 if (nested_cpu_has_vid(vmcs12)) {
10920 nested_vmx_disable_intercept_for_msr(
10921 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010922 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010923 MSR_TYPE_W);
10924 nested_vmx_disable_intercept_for_msr(
10925 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010926 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010927 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010928 }
Ashok Raj15d45072018-02-01 22:59:43 +010010929
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010930 if (spec_ctrl)
10931 nested_vmx_disable_intercept_for_msr(
10932 msr_bitmap_l1, msr_bitmap_l0,
10933 MSR_IA32_SPEC_CTRL,
10934 MSR_TYPE_R | MSR_TYPE_W);
10935
Ashok Raj15d45072018-02-01 22:59:43 +010010936 if (pred_cmd)
10937 nested_vmx_disable_intercept_for_msr(
10938 msr_bitmap_l1, msr_bitmap_l0,
10939 MSR_IA32_PRED_CMD,
10940 MSR_TYPE_W);
10941
Wincy Vanf2b93282015-02-03 23:56:03 +080010942 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010943 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010944
10945 return true;
10946}
10947
Liran Alon61ada742018-06-23 02:35:08 +030010948static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
10949 struct vmcs12 *vmcs12)
10950{
10951 struct vmcs12 *shadow;
10952 struct page *page;
10953
10954 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10955 vmcs12->vmcs_link_pointer == -1ull)
10956 return;
10957
10958 shadow = get_shadow_vmcs12(vcpu);
10959 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
10960
10961 memcpy(shadow, kmap(page), VMCS12_SIZE);
10962
10963 kunmap(page);
10964 kvm_release_page_clean(page);
10965}
10966
10967static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
10968 struct vmcs12 *vmcs12)
10969{
10970 struct vcpu_vmx *vmx = to_vmx(vcpu);
10971
10972 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10973 vmcs12->vmcs_link_pointer == -1ull)
10974 return;
10975
10976 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
10977 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
10978}
10979
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010980static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10981 struct vmcs12 *vmcs12)
10982{
10983 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10984 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10985 return -EINVAL;
10986 else
10987 return 0;
10988}
10989
Wincy Vanf2b93282015-02-03 23:56:03 +080010990static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10991 struct vmcs12 *vmcs12)
10992{
Wincy Van82f0dd42015-02-03 23:57:18 +080010993 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010994 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010995 !nested_cpu_has_vid(vmcs12) &&
10996 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010997 return 0;
10998
10999 /*
11000 * If virtualize x2apic mode is enabled,
11001 * virtualize apic access must be disabled.
11002 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011003 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11004 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011005 return -EINVAL;
11006
Wincy Van608406e2015-02-03 23:57:51 +080011007 /*
11008 * If virtual interrupt delivery is enabled,
11009 * we must exit on external interrupts.
11010 */
11011 if (nested_cpu_has_vid(vmcs12) &&
11012 !nested_exit_on_intr(vcpu))
11013 return -EINVAL;
11014
Wincy Van705699a2015-02-03 23:58:17 +080011015 /*
11016 * bits 15:8 should be zero in posted_intr_nv,
11017 * the descriptor address has been already checked
11018 * in nested_get_vmcs12_pages.
11019 */
11020 if (nested_cpu_has_posted_intr(vmcs12) &&
11021 (!nested_cpu_has_vid(vmcs12) ||
11022 !nested_exit_intr_ack_set(vcpu) ||
11023 vmcs12->posted_intr_nv & 0xff00))
11024 return -EINVAL;
11025
Wincy Vanf2b93282015-02-03 23:56:03 +080011026 /* tpr shadow is needed by all apicv features. */
11027 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11028 return -EINVAL;
11029
11030 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011031}
11032
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011033static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11034 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011035 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011036{
Liran Alone2536742018-06-23 02:35:02 +030011037 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011038 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011039 u64 count, addr;
11040
Liran Alone2536742018-06-23 02:35:02 +030011041 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11042 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011043 WARN_ON(1);
11044 return -EINVAL;
11045 }
11046 if (count == 0)
11047 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011048 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011049 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11050 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011051 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011052 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11053 addr_field, maxphyaddr, count, addr);
11054 return -EINVAL;
11055 }
11056 return 0;
11057}
11058
11059static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11060 struct vmcs12 *vmcs12)
11061{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011062 if (vmcs12->vm_exit_msr_load_count == 0 &&
11063 vmcs12->vm_exit_msr_store_count == 0 &&
11064 vmcs12->vm_entry_msr_load_count == 0)
11065 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011066 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011067 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011068 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011069 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011070 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011071 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011072 return -EINVAL;
11073 return 0;
11074}
11075
Bandan Dasc5f983f2017-05-05 15:25:14 -040011076static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11077 struct vmcs12 *vmcs12)
11078{
11079 u64 address = vmcs12->pml_address;
11080 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11081
11082 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11083 if (!nested_cpu_has_ept(vmcs12) ||
11084 !IS_ALIGNED(address, 4096) ||
11085 address >> maxphyaddr)
11086 return -EINVAL;
11087 }
11088
11089 return 0;
11090}
11091
Liran Alona8a7c022018-06-23 02:35:06 +030011092static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11093 struct vmcs12 *vmcs12)
11094{
11095 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11096 return 0;
11097
11098 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11099 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11100 return -EINVAL;
11101
11102 return 0;
11103}
11104
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011105static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11106 struct vmx_msr_entry *e)
11107{
11108 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011109 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011110 return -EINVAL;
11111 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11112 e->index == MSR_IA32_UCODE_REV)
11113 return -EINVAL;
11114 if (e->reserved != 0)
11115 return -EINVAL;
11116 return 0;
11117}
11118
11119static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11120 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011121{
11122 if (e->index == MSR_FS_BASE ||
11123 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011124 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11125 nested_vmx_msr_check_common(vcpu, e))
11126 return -EINVAL;
11127 return 0;
11128}
11129
11130static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11131 struct vmx_msr_entry *e)
11132{
11133 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11134 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011135 return -EINVAL;
11136 return 0;
11137}
11138
11139/*
11140 * Load guest's/host's msr at nested entry/exit.
11141 * return 0 for success, entry index for failure.
11142 */
11143static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11144{
11145 u32 i;
11146 struct vmx_msr_entry e;
11147 struct msr_data msr;
11148
11149 msr.host_initiated = false;
11150 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011151 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11152 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011153 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011154 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11155 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011156 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011157 }
11158 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011159 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011160 "%s check failed (%u, 0x%x, 0x%x)\n",
11161 __func__, i, e.index, e.reserved);
11162 goto fail;
11163 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011164 msr.index = e.index;
11165 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011166 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011167 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011168 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11169 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011170 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011171 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011172 }
11173 return 0;
11174fail:
11175 return i + 1;
11176}
11177
11178static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11179{
11180 u32 i;
11181 struct vmx_msr_entry e;
11182
11183 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011184 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011185 if (kvm_vcpu_read_guest(vcpu,
11186 gpa + i * sizeof(e),
11187 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011188 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011189 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11190 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011191 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011192 }
11193 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011194 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011195 "%s check failed (%u, 0x%x, 0x%x)\n",
11196 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011197 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011198 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011199 msr_info.host_initiated = false;
11200 msr_info.index = e.index;
11201 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011202 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011203 "%s cannot read MSR (%u, 0x%x)\n",
11204 __func__, i, e.index);
11205 return -EINVAL;
11206 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011207 if (kvm_vcpu_write_guest(vcpu,
11208 gpa + i * sizeof(e) +
11209 offsetof(struct vmx_msr_entry, value),
11210 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011211 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011212 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011213 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011214 return -EINVAL;
11215 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011216 }
11217 return 0;
11218}
11219
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011220static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11221{
11222 unsigned long invalid_mask;
11223
11224 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11225 return (val & invalid_mask) == 0;
11226}
11227
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011228/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011229 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11230 * emulating VM entry into a guest with EPT enabled.
11231 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11232 * is assigned to entry_failure_code on failure.
11233 */
11234static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011235 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011236{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011237 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011238 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011239 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11240 return 1;
11241 }
11242
11243 /*
11244 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11245 * must not be dereferenced.
11246 */
11247 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11248 !nested_ept) {
11249 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11250 *entry_failure_code = ENTRY_FAIL_PDPTE;
11251 return 1;
11252 }
11253 }
11254
11255 vcpu->arch.cr3 = cr3;
11256 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11257 }
11258
11259 kvm_mmu_reset_context(vcpu);
11260 return 0;
11261}
11262
Jim Mattson6514dc32018-04-26 16:09:12 -070011263static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011264{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011265 struct vcpu_vmx *vmx = to_vmx(vcpu);
11266
11267 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11268 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11269 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11270 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11271 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11272 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11273 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11274 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11275 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11276 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11277 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11278 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11279 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11280 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11281 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11282 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11283 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11284 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11285 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11286 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11287 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11288 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11289 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11290 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11291 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11292 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11293 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11294 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11295 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11296 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11297 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011298
11299 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11300 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11301 vmcs12->guest_pending_dbg_exceptions);
11302 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11303 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11304
11305 if (nested_cpu_has_xsaves(vmcs12))
11306 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11307 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11308
11309 if (cpu_has_vmx_posted_intr())
11310 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11311
11312 /*
11313 * Whether page-faults are trapped is determined by a combination of
11314 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11315 * If enable_ept, L0 doesn't care about page faults and we should
11316 * set all of these to L1's desires. However, if !enable_ept, L0 does
11317 * care about (at least some) page faults, and because it is not easy
11318 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11319 * to exit on each and every L2 page fault. This is done by setting
11320 * MASK=MATCH=0 and (see below) EB.PF=1.
11321 * Note that below we don't need special code to set EB.PF beyond the
11322 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11323 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11324 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11325 */
11326 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11327 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11328 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11329 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11330
11331 /* All VMFUNCs are currently emulated through L0 vmexits. */
11332 if (cpu_has_vmx_vmfunc())
11333 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11334
11335 if (cpu_has_vmx_apicv()) {
11336 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11337 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11338 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11339 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11340 }
11341
11342 /*
11343 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11344 * Some constant fields are set here by vmx_set_constant_host_state().
11345 * Other fields are different per CPU, and will be set later when
11346 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11347 */
11348 vmx_set_constant_host_state(vmx);
11349
11350 /*
11351 * Set the MSR load/store lists to match L0's settings.
11352 */
11353 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11354 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11355 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11356 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11357 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11358
11359 set_cr4_guest_host_mask(vmx);
11360
11361 if (vmx_mpx_supported())
11362 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11363
11364 if (enable_vpid) {
11365 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11366 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11367 else
11368 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11369 }
11370
11371 /*
11372 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11373 */
11374 if (enable_ept) {
11375 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11376 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11377 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11378 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11379 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011380
11381 if (cpu_has_vmx_msr_bitmap())
11382 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011383}
11384
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011385/*
11386 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11387 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011388 * 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 +030011389 * guest in a way that will both be appropriate to L1's requests, and our
11390 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11391 * function also has additional necessary side-effects, like setting various
11392 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011393 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11394 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011395 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011396static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011397 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011398{
11399 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011400 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011401
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011402 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011403 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011404 vmx->nested.dirty_vmcs12 = false;
11405 }
11406
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011407 /*
11408 * First, the fields that are shadowed. This must be kept in sync
11409 * with vmx_shadow_fields.h.
11410 */
11411
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011412 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011413 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011414 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011415 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11416 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011417
11418 /*
11419 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11420 * HOST_FS_BASE, HOST_GS_BASE.
11421 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011422
Jim Mattson6514dc32018-04-26 16:09:12 -070011423 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011424 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011425 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11426 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11427 } else {
11428 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11429 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11430 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011431 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011432 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11433 vmcs12->vm_entry_intr_info_field);
11434 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11435 vmcs12->vm_entry_exception_error_code);
11436 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11437 vmcs12->vm_entry_instruction_len);
11438 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11439 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011440 vmx->loaded_vmcs->nmi_known_unmasked =
11441 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011442 } else {
11443 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11444 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011445 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011446
Jan Kiszkaf4124502014-03-07 20:03:13 +010011447 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011448
Paolo Bonzini93140062016-07-06 13:23:51 +020011449 /* Preemption timer setting is only taken from vmcs01. */
11450 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11451 exec_control |= vmcs_config.pin_based_exec_ctrl;
11452 if (vmx->hv_deadline_tsc == -1)
11453 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11454
11455 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011456 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011457 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11458 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011459 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011460 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011461 }
Wincy Van705699a2015-02-03 23:58:17 +080011462
Jan Kiszkaf4124502014-03-07 20:03:13 +010011463 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011464
Jan Kiszkaf4124502014-03-07 20:03:13 +010011465 vmx->nested.preemption_timer_expired = false;
11466 if (nested_cpu_has_preemption_timer(vmcs12))
11467 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011468
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011469 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011470 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011471
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011472 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011473 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011474 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011475 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011476 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011477 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011478 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11479 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011480 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011481 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11482 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11483 ~SECONDARY_EXEC_ENABLE_PML;
11484 exec_control |= vmcs12_exec_ctrl;
11485 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011486
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011487 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011488 vmcs_write16(GUEST_INTR_STATUS,
11489 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011490
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011491 /*
11492 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11493 * nested_get_vmcs12_pages will either fix it up or
11494 * remove the VM execution control.
11495 */
11496 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11497 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11498
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011499 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11500 }
11501
Jim Mattson83bafef2016-10-04 10:48:38 -070011502 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011503 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11504 * entry, but only if the current (host) sp changed from the value
11505 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11506 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11507 * here we just force the write to happen on entry.
11508 */
11509 vmx->host_rsp = 0;
11510
11511 exec_control = vmx_exec_control(vmx); /* L0's desires */
11512 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11513 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11514 exec_control &= ~CPU_BASED_TPR_SHADOW;
11515 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011516
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011517 /*
11518 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11519 * nested_get_vmcs12_pages can't fix it up, the illegal value
11520 * will result in a VM entry failure.
11521 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011522 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011523 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011524 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011525 } else {
11526#ifdef CONFIG_X86_64
11527 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11528 CPU_BASED_CR8_STORE_EXITING;
11529#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011530 }
11531
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011532 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011533 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11534 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011535 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011536 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11537 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11538
11539 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11540
11541 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11542 * bitwise-or of what L1 wants to trap for L2, and what we want to
11543 * trap. Note that CR0.TS also needs updating - we do this later.
11544 */
11545 update_exception_bitmap(vcpu);
11546 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11547 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11548
Nadav Har'El8049d652013-08-05 11:07:06 +030011549 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11550 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11551 * bits are further modified by vmx_set_efer() below.
11552 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011553 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011554
11555 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11556 * emulated by vmx_set_efer(), below.
11557 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011558 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011559 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11560 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011561 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11562
Jim Mattson6514dc32018-04-26 16:09:12 -070011563 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011564 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011565 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011566 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011567 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011568 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011569 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011570
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011571 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11572
Peter Feinerc95ba922016-08-17 09:36:47 -070011573 if (kvm_has_tsc_control)
11574 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011575
11576 if (enable_vpid) {
11577 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011578 * There is no direct mapping between vpid02 and vpid12, the
11579 * vpid02 is per-vCPU for L0 and reused while the value of
11580 * vpid12 is changed w/ one invvpid during nested vmentry.
11581 * The vpid12 is allocated by L1 for L2, so it will not
11582 * influence global bitmap(for vpid01 and vpid02 allocation)
11583 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011584 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011585 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011586 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11587 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011588 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011589 }
11590 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011591 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011592 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011593 }
11594
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011595 if (enable_pml) {
11596 /*
11597 * Conceptually we want to copy the PML address and index from
11598 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11599 * since we always flush the log on each vmexit, this happens
11600 * to be equivalent to simply resetting the fields in vmcs02.
11601 */
11602 ASSERT(vmx->pml_pg);
11603 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11604 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11605 }
11606
Nadav Har'El155a97a2013-08-05 11:07:16 +030011607 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011608 if (nested_ept_init_mmu_context(vcpu)) {
11609 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11610 return 1;
11611 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011612 } else if (nested_cpu_has2(vmcs12,
11613 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011614 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011615 }
11616
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011617 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011618 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11619 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011620 * The CR0_READ_SHADOW is what L2 should have expected to read given
11621 * the specifications by L1; It's not enough to take
11622 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11623 * have more bits than L1 expected.
11624 */
11625 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11626 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11627
11628 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11629 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11630
Jim Mattson6514dc32018-04-26 16:09:12 -070011631 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011632 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011633 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11634 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11635 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11636 else
11637 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11638 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11639 vmx_set_efer(vcpu, vcpu->arch.efer);
11640
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011641 /*
11642 * Guest state is invalid and unrestricted guest is disabled,
11643 * which means L1 attempted VMEntry to L2 with invalid state.
11644 * Fail the VMEntry.
11645 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011646 if (vmx->emulation_required) {
11647 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011648 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011649 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011650
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011651 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011652 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011653 entry_failure_code))
11654 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011655
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011656 if (!enable_ept)
11657 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11658
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011659 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11660 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011661 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011662}
11663
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011664static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11665{
11666 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11667 nested_cpu_has_virtual_nmis(vmcs12))
11668 return -EINVAL;
11669
11670 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11671 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11672 return -EINVAL;
11673
11674 return 0;
11675}
11676
Jim Mattsonca0bde22016-11-30 12:03:46 -080011677static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11678{
11679 struct vcpu_vmx *vmx = to_vmx(vcpu);
11680
11681 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11682 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11683 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11684
Jim Mattson56a20512017-07-06 16:33:06 -070011685 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11686 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11687
Jim Mattsonca0bde22016-11-30 12:03:46 -080011688 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11689 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11690
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011691 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11692 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11693
Jim Mattson712b12d2017-08-24 13:24:47 -070011694 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11695 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11696
Jim Mattsonca0bde22016-11-30 12:03:46 -080011697 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11698 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11699
11700 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11701 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11702
Bandan Dasc5f983f2017-05-05 15:25:14 -040011703 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11704 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11705
Liran Alona8a7c022018-06-23 02:35:06 +030011706 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11707 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11708
Jim Mattsonca0bde22016-11-30 12:03:46 -080011709 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011710 vmx->nested.msrs.procbased_ctls_low,
11711 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011712 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11713 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011714 vmx->nested.msrs.secondary_ctls_low,
11715 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011716 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011717 vmx->nested.msrs.pinbased_ctls_low,
11718 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011719 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011720 vmx->nested.msrs.exit_ctls_low,
11721 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011722 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011723 vmx->nested.msrs.entry_ctls_low,
11724 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011725 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11726
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011727 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011728 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11729
Bandan Das41ab9372017-08-03 15:54:43 -040011730 if (nested_cpu_has_vmfunc(vmcs12)) {
11731 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011732 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011733 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11734
11735 if (nested_cpu_has_eptp_switching(vmcs12)) {
11736 if (!nested_cpu_has_ept(vmcs12) ||
11737 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11738 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11739 }
11740 }
Bandan Das27c42a12017-08-03 15:54:42 -040011741
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011742 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11743 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11744
Jim Mattsonca0bde22016-11-30 12:03:46 -080011745 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11746 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11747 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11748 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11749
Marc Orr04473782018-06-20 17:21:29 -070011750 /*
11751 * From the Intel SDM, volume 3:
11752 * Fields relevant to VM-entry event injection must be set properly.
11753 * These fields are the VM-entry interruption-information field, the
11754 * VM-entry exception error code, and the VM-entry instruction length.
11755 */
11756 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11757 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11758 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11759 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11760 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11761 bool should_have_error_code;
11762 bool urg = nested_cpu_has2(vmcs12,
11763 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11764 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11765
11766 /* VM-entry interruption-info field: interruption type */
11767 if (intr_type == INTR_TYPE_RESERVED ||
11768 (intr_type == INTR_TYPE_OTHER_EVENT &&
11769 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11770 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11771
11772 /* VM-entry interruption-info field: vector */
11773 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11774 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11775 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11776 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11777
11778 /* VM-entry interruption-info field: deliver error code */
11779 should_have_error_code =
11780 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11781 x86_exception_has_error_code(vector);
11782 if (has_error_code != should_have_error_code)
11783 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11784
11785 /* VM-entry exception error code */
11786 if (has_error_code &&
11787 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11788 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11789
11790 /* VM-entry interruption-info field: reserved bits */
11791 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11792 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11793
11794 /* VM-entry instruction length */
11795 switch (intr_type) {
11796 case INTR_TYPE_SOFT_EXCEPTION:
11797 case INTR_TYPE_SOFT_INTR:
11798 case INTR_TYPE_PRIV_SW_EXCEPTION:
11799 if ((vmcs12->vm_entry_instruction_len > 15) ||
11800 (vmcs12->vm_entry_instruction_len == 0 &&
11801 !nested_cpu_has_zero_length_injection(vcpu)))
11802 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11803 }
11804 }
11805
Jim Mattsonca0bde22016-11-30 12:03:46 -080011806 return 0;
11807}
11808
Liran Alonf145d902018-06-23 02:35:07 +030011809static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
11810 struct vmcs12 *vmcs12)
11811{
11812 int r;
11813 struct page *page;
11814 struct vmcs12 *shadow;
11815
11816 if (vmcs12->vmcs_link_pointer == -1ull)
11817 return 0;
11818
11819 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
11820 return -EINVAL;
11821
11822 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11823 if (is_error_page(page))
11824 return -EINVAL;
11825
11826 r = 0;
11827 shadow = kmap(page);
11828 if (shadow->hdr.revision_id != VMCS12_REVISION ||
11829 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
11830 r = -EINVAL;
11831 kunmap(page);
11832 kvm_release_page_clean(page);
11833 return r;
11834}
11835
Jim Mattsonca0bde22016-11-30 12:03:46 -080011836static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11837 u32 *exit_qual)
11838{
11839 bool ia32e;
11840
11841 *exit_qual = ENTRY_FAIL_DEFAULT;
11842
11843 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11844 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11845 return 1;
11846
Liran Alonf145d902018-06-23 02:35:07 +030011847 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080011848 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11849 return 1;
11850 }
11851
11852 /*
11853 * If the load IA32_EFER VM-entry control is 1, the following checks
11854 * are performed on the field for the IA32_EFER MSR:
11855 * - Bits reserved in the IA32_EFER MSR must be 0.
11856 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11857 * the IA-32e mode guest VM-exit control. It must also be identical
11858 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11859 * CR0.PG) is 1.
11860 */
11861 if (to_vmx(vcpu)->nested.nested_run_pending &&
11862 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11863 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11864 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11865 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11866 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11867 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11868 return 1;
11869 }
11870
11871 /*
11872 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11873 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11874 * the values of the LMA and LME bits in the field must each be that of
11875 * the host address-space size VM-exit control.
11876 */
11877 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11878 ia32e = (vmcs12->vm_exit_controls &
11879 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11880 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11881 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11882 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11883 return 1;
11884 }
11885
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011886 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11887 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11888 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11889 return 1;
11890
Jim Mattsonca0bde22016-11-30 12:03:46 -080011891 return 0;
11892}
11893
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011894/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011895 * If exit_qual is NULL, this is being called from state restore (either RSM
11896 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011897 */
11898static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080011899{
11900 struct vcpu_vmx *vmx = to_vmx(vcpu);
11901 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011902 bool from_vmentry = !!exit_qual;
11903 u32 dummy_exit_qual;
11904 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011905
Jim Mattson858e25c2016-11-30 12:03:47 -080011906 enter_guest_mode(vcpu);
11907
11908 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11909 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11910
Jim Mattsonde3a0022017-11-27 17:22:25 -060011911 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011912 vmx_segment_cache_clear(vmx);
11913
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011914 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11915 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11916
11917 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011918 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011919 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011920
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011921 if (from_vmentry) {
11922 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011923
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011924 r = EXIT_REASON_MSR_LOAD_FAIL;
11925 *exit_qual = nested_vmx_load_msr(vcpu,
11926 vmcs12->vm_entry_msr_load_addr,
11927 vmcs12->vm_entry_msr_load_count);
11928 if (*exit_qual)
11929 goto fail;
11930 } else {
11931 /*
11932 * The MMU is not initialized to point at the right entities yet and
11933 * "get pages" would need to read data from the guest (i.e. we will
11934 * need to perform gpa to hpa translation). Request a call
11935 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11936 * have already been set at vmentry time and should not be reset.
11937 */
11938 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11939 }
Jim Mattson858e25c2016-11-30 12:03:47 -080011940
Jim Mattson858e25c2016-11-30 12:03:47 -080011941 /*
11942 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11943 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11944 * returned as far as L1 is concerned. It will only return (and set
11945 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11946 */
11947 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011948
11949fail:
11950 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11951 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11952 leave_guest_mode(vcpu);
11953 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011954 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011955}
11956
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011957/*
11958 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11959 * for running an L2 nested guest.
11960 */
11961static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11962{
11963 struct vmcs12 *vmcs12;
11964 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011965 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011966 u32 exit_qual;
11967 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011968
Kyle Hueyeb277562016-11-29 12:40:39 -080011969 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011970 return 1;
11971
Kyle Hueyeb277562016-11-29 12:40:39 -080011972 if (!nested_vmx_check_vmcs12(vcpu))
11973 goto out;
11974
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011975 vmcs12 = get_vmcs12(vcpu);
11976
Liran Alona6192d42018-06-23 02:35:04 +030011977 /*
11978 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
11979 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
11980 * rather than RFLAGS.ZF, and no error number is stored to the
11981 * VM-instruction error field.
11982 */
11983 if (vmcs12->hdr.shadow_vmcs) {
11984 nested_vmx_failInvalid(vcpu);
11985 goto out;
11986 }
11987
Abel Gordon012f83c2013-04-18 14:39:25 +030011988 if (enable_shadow_vmcs)
11989 copy_shadow_to_vmcs12(vmx);
11990
Nadav Har'El7c177932011-05-25 23:12:04 +030011991 /*
11992 * The nested entry process starts with enforcing various prerequisites
11993 * on vmcs12 as required by the Intel SDM, and act appropriately when
11994 * they fail: As the SDM explains, some conditions should cause the
11995 * instruction to fail, while others will cause the instruction to seem
11996 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11997 * To speed up the normal (success) code path, we should avoid checking
11998 * for misconfigurations which will anyway be caught by the processor
11999 * when using the merged vmcs02.
12000 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012001 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12002 nested_vmx_failValid(vcpu,
12003 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12004 goto out;
12005 }
12006
Nadav Har'El7c177932011-05-25 23:12:04 +030012007 if (vmcs12->launch_state == launch) {
12008 nested_vmx_failValid(vcpu,
12009 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12010 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012011 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012012 }
12013
Jim Mattsonca0bde22016-11-30 12:03:46 -080012014 ret = check_vmentry_prereqs(vcpu, vmcs12);
12015 if (ret) {
12016 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012017 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012018 }
12019
Nadav Har'El7c177932011-05-25 23:12:04 +030012020 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012021 * After this point, the trap flag no longer triggers a singlestep trap
12022 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12023 * This is not 100% correct; for performance reasons, we delegate most
12024 * of the checks on host state to the processor. If those fail,
12025 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012026 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012027 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012028
Jim Mattsonca0bde22016-11-30 12:03:46 -080012029 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12030 if (ret) {
12031 nested_vmx_entry_failure(vcpu, vmcs12,
12032 EXIT_REASON_INVALID_STATE, exit_qual);
12033 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012034 }
12035
12036 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012037 * We're finally done with prerequisite checking, and can start with
12038 * the nested entry.
12039 */
12040
Jim Mattson6514dc32018-04-26 16:09:12 -070012041 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012042 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012043 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012044 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012045 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012046 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012047 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012048
Chao Gao135a06c2018-02-11 10:06:30 +080012049 /*
Liran Alon61ada742018-06-23 02:35:08 +030012050 * Must happen outside of enter_vmx_non_root_mode() as it will
12051 * also be used as part of restoring nVMX state for
12052 * snapshot restore (migration).
12053 *
12054 * In this flow, it is assumed that vmcs12 cache was
12055 * trasferred as part of captured nVMX state and should
12056 * therefore not be read from guest memory (which may not
12057 * exist on destination host yet).
12058 */
12059 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12060
12061 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012062 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12063 * by event injection, halt vcpu.
12064 */
12065 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012066 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12067 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012068 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012069 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012070 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012071
12072out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012073 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012074}
12075
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012076/*
12077 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12078 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12079 * This function returns the new value we should put in vmcs12.guest_cr0.
12080 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12081 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12082 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12083 * didn't trap the bit, because if L1 did, so would L0).
12084 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12085 * been modified by L2, and L1 knows it. So just leave the old value of
12086 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12087 * isn't relevant, because if L0 traps this bit it can set it to anything.
12088 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12089 * changed these bits, and therefore they need to be updated, but L0
12090 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12091 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12092 */
12093static inline unsigned long
12094vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12095{
12096 return
12097 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12098 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12099 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12100 vcpu->arch.cr0_guest_owned_bits));
12101}
12102
12103static inline unsigned long
12104vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12105{
12106 return
12107 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12108 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12109 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12110 vcpu->arch.cr4_guest_owned_bits));
12111}
12112
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012113static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12114 struct vmcs12 *vmcs12)
12115{
12116 u32 idt_vectoring;
12117 unsigned int nr;
12118
Wanpeng Li664f8e22017-08-24 03:35:09 -070012119 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012120 nr = vcpu->arch.exception.nr;
12121 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12122
12123 if (kvm_exception_is_soft(nr)) {
12124 vmcs12->vm_exit_instruction_len =
12125 vcpu->arch.event_exit_inst_len;
12126 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12127 } else
12128 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12129
12130 if (vcpu->arch.exception.has_error_code) {
12131 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12132 vmcs12->idt_vectoring_error_code =
12133 vcpu->arch.exception.error_code;
12134 }
12135
12136 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012137 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012138 vmcs12->idt_vectoring_info_field =
12139 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012140 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012141 nr = vcpu->arch.interrupt.nr;
12142 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12143
12144 if (vcpu->arch.interrupt.soft) {
12145 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12146 vmcs12->vm_entry_instruction_len =
12147 vcpu->arch.event_exit_inst_len;
12148 } else
12149 idt_vectoring |= INTR_TYPE_EXT_INTR;
12150
12151 vmcs12->idt_vectoring_info_field = idt_vectoring;
12152 }
12153}
12154
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012155static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12156{
12157 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012158 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012159 bool block_nested_events =
12160 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012161
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012162 if (vcpu->arch.exception.pending &&
12163 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012164 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012165 return -EBUSY;
12166 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012167 return 0;
12168 }
12169
Jan Kiszkaf4124502014-03-07 20:03:13 +010012170 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12171 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012172 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012173 return -EBUSY;
12174 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12175 return 0;
12176 }
12177
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012178 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012179 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012180 return -EBUSY;
12181 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12182 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12183 INTR_INFO_VALID_MASK, 0);
12184 /*
12185 * The NMI-triggered VM exit counts as injection:
12186 * clear this one and block further NMIs.
12187 */
12188 vcpu->arch.nmi_pending = 0;
12189 vmx_set_nmi_mask(vcpu, true);
12190 return 0;
12191 }
12192
12193 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12194 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012195 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012196 return -EBUSY;
12197 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012198 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012199 }
12200
David Hildenbrand6342c502017-01-25 11:58:58 +010012201 vmx_complete_nested_posted_interrupt(vcpu);
12202 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012203}
12204
Jan Kiszkaf4124502014-03-07 20:03:13 +010012205static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12206{
12207 ktime_t remaining =
12208 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12209 u64 value;
12210
12211 if (ktime_to_ns(remaining) <= 0)
12212 return 0;
12213
12214 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12215 do_div(value, 1000000);
12216 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12217}
12218
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012219/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012220 * Update the guest state fields of vmcs12 to reflect changes that
12221 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12222 * VM-entry controls is also updated, since this is really a guest
12223 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012224 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012225static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012226{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012227 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12228 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12229
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012230 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12231 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12232 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12233
12234 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12235 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12236 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12237 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12238 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12239 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12240 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12241 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12242 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12243 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12244 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12245 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12246 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12247 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12248 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12249 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12250 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12251 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12252 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12253 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12254 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12255 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12256 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12257 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12258 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12259 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12260 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12261 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12262 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12263 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12264 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12265 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12266 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12267 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12268 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12269 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12270
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012271 vmcs12->guest_interruptibility_info =
12272 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12273 vmcs12->guest_pending_dbg_exceptions =
12274 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012275 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12276 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12277 else
12278 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012279
Jan Kiszkaf4124502014-03-07 20:03:13 +010012280 if (nested_cpu_has_preemption_timer(vmcs12)) {
12281 if (vmcs12->vm_exit_controls &
12282 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12283 vmcs12->vmx_preemption_timer_value =
12284 vmx_get_preemption_timer_value(vcpu);
12285 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12286 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012287
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012288 /*
12289 * In some cases (usually, nested EPT), L2 is allowed to change its
12290 * own CR3 without exiting. If it has changed it, we must keep it.
12291 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12292 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12293 *
12294 * Additionally, restore L2's PDPTR to vmcs12.
12295 */
12296 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012297 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012298 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12299 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12300 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12301 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12302 }
12303
Jim Mattsond281e132017-06-01 12:44:46 -070012304 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012305
Wincy Van608406e2015-02-03 23:57:51 +080012306 if (nested_cpu_has_vid(vmcs12))
12307 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12308
Jan Kiszkac18911a2013-03-13 16:06:41 +010012309 vmcs12->vm_entry_controls =
12310 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012311 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012312
Jan Kiszka2996fca2014-06-16 13:59:43 +020012313 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12314 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12315 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12316 }
12317
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012318 /* TODO: These cannot have changed unless we have MSR bitmaps and
12319 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012320 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012321 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012322 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12323 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012324 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12325 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12326 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012327 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012328 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012329}
12330
12331/*
12332 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12333 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12334 * and this function updates it to reflect the changes to the guest state while
12335 * L2 was running (and perhaps made some exits which were handled directly by L0
12336 * without going back to L1), and to reflect the exit reason.
12337 * Note that we do not have to copy here all VMCS fields, just those that
12338 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12339 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12340 * which already writes to vmcs12 directly.
12341 */
12342static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12343 u32 exit_reason, u32 exit_intr_info,
12344 unsigned long exit_qualification)
12345{
12346 /* update guest state fields: */
12347 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012348
12349 /* update exit information fields: */
12350
Jan Kiszka533558b2014-01-04 18:47:20 +010012351 vmcs12->vm_exit_reason = exit_reason;
12352 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012353 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012354
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012355 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012356 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12357 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12358
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012359 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012360 vmcs12->launch_state = 1;
12361
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012362 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12363 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012364 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012365
12366 /*
12367 * Transfer the event that L0 or L1 may wanted to inject into
12368 * L2 to IDT_VECTORING_INFO_FIELD.
12369 */
12370 vmcs12_save_pending_event(vcpu, vmcs12);
12371 }
12372
12373 /*
12374 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12375 * preserved above and would only end up incorrectly in L1.
12376 */
12377 vcpu->arch.nmi_injected = false;
12378 kvm_clear_exception_queue(vcpu);
12379 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012380}
12381
Wanpeng Li5af41572017-11-05 16:54:49 -080012382static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12383 struct vmcs12 *vmcs12)
12384{
12385 u32 entry_failure_code;
12386
12387 nested_ept_uninit_mmu_context(vcpu);
12388
12389 /*
12390 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12391 * couldn't have changed.
12392 */
12393 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12394 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12395
12396 if (!enable_ept)
12397 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12398}
12399
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012400/*
12401 * A part of what we need to when the nested L2 guest exits and we want to
12402 * run its L1 parent, is to reset L1's guest state to the host state specified
12403 * in vmcs12.
12404 * This function is to be called not only on normal nested exit, but also on
12405 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12406 * Failures During or After Loading Guest State").
12407 * This function should be called when the active VMCS is L1's (vmcs01).
12408 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012409static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12410 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012411{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012412 struct kvm_segment seg;
12413
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012414 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12415 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012416 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012417 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12418 else
12419 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12420 vmx_set_efer(vcpu, vcpu->arch.efer);
12421
12422 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12423 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012424 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012425 /*
12426 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012427 * actually changed, because vmx_set_cr0 refers to efer set above.
12428 *
12429 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12430 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012431 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012432 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012433 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012434
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012435 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012436 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012437 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012438
Wanpeng Li5af41572017-11-05 16:54:49 -080012439 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012440
Liran Alon6f1e03b2018-05-22 17:16:14 +030012441 /*
12442 * If vmcs01 don't use VPID, CPU flushes TLB on every
12443 * VMEntry/VMExit. Thus, no need to flush TLB.
12444 *
12445 * If vmcs12 uses VPID, TLB entries populated by L2 are
12446 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12447 * with vmx->vpid. Thus, no need to flush TLB.
12448 *
12449 * Therefore, flush TLB only in case vmcs01 uses VPID and
12450 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12451 * are both tagged with vmx->vpid.
12452 */
12453 if (enable_vpid &&
12454 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012455 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012456 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012457
12458 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12459 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12460 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12461 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12462 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012463 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12464 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012465
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012466 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12467 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12468 vmcs_write64(GUEST_BNDCFGS, 0);
12469
Jan Kiszka44811c02013-08-04 17:17:27 +020012470 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012471 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012472 vcpu->arch.pat = vmcs12->host_ia32_pat;
12473 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012474 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12475 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12476 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012477
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012478 /* Set L1 segment info according to Intel SDM
12479 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12480 seg = (struct kvm_segment) {
12481 .base = 0,
12482 .limit = 0xFFFFFFFF,
12483 .selector = vmcs12->host_cs_selector,
12484 .type = 11,
12485 .present = 1,
12486 .s = 1,
12487 .g = 1
12488 };
12489 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12490 seg.l = 1;
12491 else
12492 seg.db = 1;
12493 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12494 seg = (struct kvm_segment) {
12495 .base = 0,
12496 .limit = 0xFFFFFFFF,
12497 .type = 3,
12498 .present = 1,
12499 .s = 1,
12500 .db = 1,
12501 .g = 1
12502 };
12503 seg.selector = vmcs12->host_ds_selector;
12504 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12505 seg.selector = vmcs12->host_es_selector;
12506 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12507 seg.selector = vmcs12->host_ss_selector;
12508 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12509 seg.selector = vmcs12->host_fs_selector;
12510 seg.base = vmcs12->host_fs_base;
12511 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12512 seg.selector = vmcs12->host_gs_selector;
12513 seg.base = vmcs12->host_gs_base;
12514 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12515 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012516 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012517 .limit = 0x67,
12518 .selector = vmcs12->host_tr_selector,
12519 .type = 11,
12520 .present = 1
12521 };
12522 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12523
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012524 kvm_set_dr(vcpu, 7, 0x400);
12525 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012526
Wincy Van3af18d92015-02-03 23:49:31 +080012527 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012528 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012529
Wincy Vanff651cb2014-12-11 08:52:58 +030012530 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12531 vmcs12->vm_exit_msr_load_count))
12532 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012533}
12534
12535/*
12536 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12537 * and modify vmcs12 to make it see what it would expect to see there if
12538 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12539 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012540static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12541 u32 exit_intr_info,
12542 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012543{
12544 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012545 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12546
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012547 /* trying to cancel vmlaunch/vmresume is a bug */
12548 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12549
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012550 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012551 * The only expected VM-instruction error is "VM entry with
12552 * invalid control field(s)." Anything else indicates a
12553 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012554 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012555 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12556 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12557
12558 leave_guest_mode(vcpu);
12559
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012560 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12561 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12562
Jim Mattson4f350c62017-09-14 16:31:44 -070012563 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012564 if (exit_reason == -1)
12565 sync_vmcs12(vcpu, vmcs12);
12566 else
12567 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12568 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012569
Liran Alon61ada742018-06-23 02:35:08 +030012570 /*
12571 * Must happen outside of sync_vmcs12() as it will
12572 * also be used to capture vmcs12 cache as part of
12573 * capturing nVMX state for snapshot (migration).
12574 *
12575 * Otherwise, this flush will dirty guest memory at a
12576 * point it is already assumed by user-space to be
12577 * immutable.
12578 */
12579 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12580
Jim Mattson4f350c62017-09-14 16:31:44 -070012581 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12582 vmcs12->vm_exit_msr_store_count))
12583 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012584 }
12585
Jim Mattson4f350c62017-09-14 16:31:44 -070012586 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012587 vm_entry_controls_reset_shadow(vmx);
12588 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012589 vmx_segment_cache_clear(vmx);
12590
Paolo Bonzini93140062016-07-06 13:23:51 +020012591 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012592 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12593 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012594 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012595 if (vmx->hv_deadline_tsc == -1)
12596 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12597 PIN_BASED_VMX_PREEMPTION_TIMER);
12598 else
12599 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12600 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012601 if (kvm_has_tsc_control)
12602 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012603
Jim Mattson8d860bb2018-05-09 16:56:05 -040012604 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12605 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12606 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012607 } else if (!nested_cpu_has_ept(vmcs12) &&
12608 nested_cpu_has2(vmcs12,
12609 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012610 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012611 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012612
12613 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12614 vmx->host_rsp = 0;
12615
12616 /* Unpin physical memory we referred to in vmcs02 */
12617 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012618 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012619 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012620 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012621 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012622 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012623 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012624 }
Wincy Van705699a2015-02-03 23:58:17 +080012625 if (vmx->nested.pi_desc_page) {
12626 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012627 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012628 vmx->nested.pi_desc_page = NULL;
12629 vmx->nested.pi_desc = NULL;
12630 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012631
12632 /*
Tang Chen38b99172014-09-24 15:57:54 +080012633 * We are now running in L2, mmu_notifier will force to reload the
12634 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12635 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012636 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012637
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012638 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012639 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012640
12641 /* in case we halted in L2 */
12642 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012643
12644 if (likely(!vmx->fail)) {
12645 /*
12646 * TODO: SDM says that with acknowledge interrupt on
12647 * exit, bit 31 of the VM-exit interrupt information
12648 * (valid interrupt) is always set to 1 on
12649 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12650 * need kvm_cpu_has_interrupt(). See the commit
12651 * message for details.
12652 */
12653 if (nested_exit_intr_ack_set(vcpu) &&
12654 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12655 kvm_cpu_has_interrupt(vcpu)) {
12656 int irq = kvm_cpu_get_interrupt(vcpu);
12657 WARN_ON(irq < 0);
12658 vmcs12->vm_exit_intr_info = irq |
12659 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12660 }
12661
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012662 if (exit_reason != -1)
12663 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12664 vmcs12->exit_qualification,
12665 vmcs12->idt_vectoring_info_field,
12666 vmcs12->vm_exit_intr_info,
12667 vmcs12->vm_exit_intr_error_code,
12668 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012669
12670 load_vmcs12_host_state(vcpu, vmcs12);
12671
12672 return;
12673 }
12674
12675 /*
12676 * After an early L2 VM-entry failure, we're now back
12677 * in L1 which thinks it just finished a VMLAUNCH or
12678 * VMRESUME instruction, so we need to set the failure
12679 * flag and the VM-instruction error field of the VMCS
12680 * accordingly.
12681 */
12682 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012683
12684 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12685
Jim Mattson4f350c62017-09-14 16:31:44 -070012686 /*
12687 * The emulated instruction was already skipped in
12688 * nested_vmx_run, but the updated RIP was never
12689 * written back to the vmcs01.
12690 */
12691 skip_emulated_instruction(vcpu);
12692 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012693}
12694
Nadav Har'El7c177932011-05-25 23:12:04 +030012695/*
Jan Kiszka42124922014-01-04 18:47:19 +010012696 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12697 */
12698static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12699{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012700 if (is_guest_mode(vcpu)) {
12701 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012702 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012703 }
Jan Kiszka42124922014-01-04 18:47:19 +010012704 free_nested(to_vmx(vcpu));
12705}
12706
12707/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012708 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12709 * 23.7 "VM-entry failures during or after loading guest state" (this also
12710 * lists the acceptable exit-reason and exit-qualification parameters).
12711 * It should only be called before L2 actually succeeded to run, and when
12712 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12713 */
12714static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12715 struct vmcs12 *vmcs12,
12716 u32 reason, unsigned long qualification)
12717{
12718 load_vmcs12_host_state(vcpu, vmcs12);
12719 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12720 vmcs12->exit_qualification = qualification;
12721 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012722 if (enable_shadow_vmcs)
12723 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012724}
12725
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012726static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12727 struct x86_instruction_info *info,
12728 enum x86_intercept_stage stage)
12729{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012730 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12731 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12732
12733 /*
12734 * RDPID causes #UD if disabled through secondary execution controls.
12735 * Because it is marked as EmulateOnUD, we need to intercept it here.
12736 */
12737 if (info->intercept == x86_intercept_rdtscp &&
12738 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12739 ctxt->exception.vector = UD_VECTOR;
12740 ctxt->exception.error_code_valid = false;
12741 return X86EMUL_PROPAGATE_FAULT;
12742 }
12743
12744 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012745 return X86EMUL_CONTINUE;
12746}
12747
Yunhong Jiang64672c92016-06-13 14:19:59 -070012748#ifdef CONFIG_X86_64
12749/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12750static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12751 u64 divisor, u64 *result)
12752{
12753 u64 low = a << shift, high = a >> (64 - shift);
12754
12755 /* To avoid the overflow on divq */
12756 if (high >= divisor)
12757 return 1;
12758
12759 /* Low hold the result, high hold rem which is discarded */
12760 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12761 "rm" (divisor), "0" (low), "1" (high));
12762 *result = low;
12763
12764 return 0;
12765}
12766
12767static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12768{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012769 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012770 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012771
12772 if (kvm_mwait_in_guest(vcpu->kvm))
12773 return -EOPNOTSUPP;
12774
12775 vmx = to_vmx(vcpu);
12776 tscl = rdtsc();
12777 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12778 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012779 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12780
12781 if (delta_tsc > lapic_timer_advance_cycles)
12782 delta_tsc -= lapic_timer_advance_cycles;
12783 else
12784 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012785
12786 /* Convert to host delta tsc if tsc scaling is enabled */
12787 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12788 u64_shl_div_u64(delta_tsc,
12789 kvm_tsc_scaling_ratio_frac_bits,
12790 vcpu->arch.tsc_scaling_ratio,
12791 &delta_tsc))
12792 return -ERANGE;
12793
12794 /*
12795 * If the delta tsc can't fit in the 32 bit after the multi shift,
12796 * we can't use the preemption timer.
12797 * It's possible that it fits on later vmentries, but checking
12798 * on every vmentry is costly so we just use an hrtimer.
12799 */
12800 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12801 return -ERANGE;
12802
12803 vmx->hv_deadline_tsc = tscl + delta_tsc;
12804 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12805 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012806
12807 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012808}
12809
12810static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12811{
12812 struct vcpu_vmx *vmx = to_vmx(vcpu);
12813 vmx->hv_deadline_tsc = -1;
12814 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12815 PIN_BASED_VMX_PREEMPTION_TIMER);
12816}
12817#endif
12818
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012819static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012820{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012821 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012822 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012823}
12824
Kai Huang843e4332015-01-28 10:54:28 +080012825static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12826 struct kvm_memory_slot *slot)
12827{
12828 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12829 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12830}
12831
12832static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12833 struct kvm_memory_slot *slot)
12834{
12835 kvm_mmu_slot_set_dirty(kvm, slot);
12836}
12837
12838static void vmx_flush_log_dirty(struct kvm *kvm)
12839{
12840 kvm_flush_pml_buffers(kvm);
12841}
12842
Bandan Dasc5f983f2017-05-05 15:25:14 -040012843static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12844{
12845 struct vmcs12 *vmcs12;
12846 struct vcpu_vmx *vmx = to_vmx(vcpu);
12847 gpa_t gpa;
12848 struct page *page = NULL;
12849 u64 *pml_address;
12850
12851 if (is_guest_mode(vcpu)) {
12852 WARN_ON_ONCE(vmx->nested.pml_full);
12853
12854 /*
12855 * Check if PML is enabled for the nested guest.
12856 * Whether eptp bit 6 is set is already checked
12857 * as part of A/D emulation.
12858 */
12859 vmcs12 = get_vmcs12(vcpu);
12860 if (!nested_cpu_has_pml(vmcs12))
12861 return 0;
12862
Dan Carpenter47698862017-05-10 22:43:17 +030012863 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012864 vmx->nested.pml_full = true;
12865 return 1;
12866 }
12867
12868 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12869
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012870 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12871 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012872 return 0;
12873
12874 pml_address = kmap(page);
12875 pml_address[vmcs12->guest_pml_index--] = gpa;
12876 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012877 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012878 }
12879
12880 return 0;
12881}
12882
Kai Huang843e4332015-01-28 10:54:28 +080012883static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12884 struct kvm_memory_slot *memslot,
12885 gfn_t offset, unsigned long mask)
12886{
12887 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12888}
12889
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012890static void __pi_post_block(struct kvm_vcpu *vcpu)
12891{
12892 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12893 struct pi_desc old, new;
12894 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012895
12896 do {
12897 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012898 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12899 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012900
12901 dest = cpu_physical_id(vcpu->cpu);
12902
12903 if (x2apic_enabled())
12904 new.ndst = dest;
12905 else
12906 new.ndst = (dest << 8) & 0xFF00;
12907
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012908 /* set 'NV' to 'notification vector' */
12909 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012910 } while (cmpxchg64(&pi_desc->control, old.control,
12911 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012912
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012913 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12914 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012915 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012916 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012917 vcpu->pre_pcpu = -1;
12918 }
12919}
12920
Feng Wuefc64402015-09-18 22:29:51 +080012921/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012922 * This routine does the following things for vCPU which is going
12923 * to be blocked if VT-d PI is enabled.
12924 * - Store the vCPU to the wakeup list, so when interrupts happen
12925 * we can find the right vCPU to wake up.
12926 * - Change the Posted-interrupt descriptor as below:
12927 * 'NDST' <-- vcpu->pre_pcpu
12928 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12929 * - If 'ON' is set during this process, which means at least one
12930 * interrupt is posted for this vCPU, we cannot block it, in
12931 * this case, return 1, otherwise, return 0.
12932 *
12933 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012934static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012935{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012936 unsigned int dest;
12937 struct pi_desc old, new;
12938 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12939
12940 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012941 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12942 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012943 return 0;
12944
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012945 WARN_ON(irqs_disabled());
12946 local_irq_disable();
12947 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12948 vcpu->pre_pcpu = vcpu->cpu;
12949 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12950 list_add_tail(&vcpu->blocked_vcpu_list,
12951 &per_cpu(blocked_vcpu_on_cpu,
12952 vcpu->pre_pcpu));
12953 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12954 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012955
12956 do {
12957 old.control = new.control = pi_desc->control;
12958
Feng Wubf9f6ac2015-09-18 22:29:55 +080012959 WARN((pi_desc->sn == 1),
12960 "Warning: SN field of posted-interrupts "
12961 "is set before blocking\n");
12962
12963 /*
12964 * Since vCPU can be preempted during this process,
12965 * vcpu->cpu could be different with pre_pcpu, we
12966 * need to set pre_pcpu as the destination of wakeup
12967 * notification event, then we can find the right vCPU
12968 * to wakeup in wakeup handler if interrupts happen
12969 * when the vCPU is in blocked state.
12970 */
12971 dest = cpu_physical_id(vcpu->pre_pcpu);
12972
12973 if (x2apic_enabled())
12974 new.ndst = dest;
12975 else
12976 new.ndst = (dest << 8) & 0xFF00;
12977
12978 /* set 'NV' to 'wakeup vector' */
12979 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012980 } while (cmpxchg64(&pi_desc->control, old.control,
12981 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012982
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012983 /* We should not block the vCPU if an interrupt is posted for it. */
12984 if (pi_test_on(pi_desc) == 1)
12985 __pi_post_block(vcpu);
12986
12987 local_irq_enable();
12988 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012989}
12990
Yunhong Jiangbc225122016-06-13 14:19:58 -070012991static int vmx_pre_block(struct kvm_vcpu *vcpu)
12992{
12993 if (pi_pre_block(vcpu))
12994 return 1;
12995
Yunhong Jiang64672c92016-06-13 14:19:59 -070012996 if (kvm_lapic_hv_timer_in_use(vcpu))
12997 kvm_lapic_switch_to_sw_timer(vcpu);
12998
Yunhong Jiangbc225122016-06-13 14:19:58 -070012999 return 0;
13000}
13001
13002static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013003{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013004 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013005 return;
13006
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013007 WARN_ON(irqs_disabled());
13008 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013009 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013010 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013011}
13012
Yunhong Jiangbc225122016-06-13 14:19:58 -070013013static void vmx_post_block(struct kvm_vcpu *vcpu)
13014{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013015 if (kvm_x86_ops->set_hv_timer)
13016 kvm_lapic_switch_to_hv_timer(vcpu);
13017
Yunhong Jiangbc225122016-06-13 14:19:58 -070013018 pi_post_block(vcpu);
13019}
13020
Feng Wubf9f6ac2015-09-18 22:29:55 +080013021/*
Feng Wuefc64402015-09-18 22:29:51 +080013022 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13023 *
13024 * @kvm: kvm
13025 * @host_irq: host irq of the interrupt
13026 * @guest_irq: gsi of the interrupt
13027 * @set: set or unset PI
13028 * returns 0 on success, < 0 on failure
13029 */
13030static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13031 uint32_t guest_irq, bool set)
13032{
13033 struct kvm_kernel_irq_routing_entry *e;
13034 struct kvm_irq_routing_table *irq_rt;
13035 struct kvm_lapic_irq irq;
13036 struct kvm_vcpu *vcpu;
13037 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013038 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013039
13040 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013041 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13042 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013043 return 0;
13044
13045 idx = srcu_read_lock(&kvm->irq_srcu);
13046 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013047 if (guest_irq >= irq_rt->nr_rt_entries ||
13048 hlist_empty(&irq_rt->map[guest_irq])) {
13049 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13050 guest_irq, irq_rt->nr_rt_entries);
13051 goto out;
13052 }
Feng Wuefc64402015-09-18 22:29:51 +080013053
13054 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13055 if (e->type != KVM_IRQ_ROUTING_MSI)
13056 continue;
13057 /*
13058 * VT-d PI cannot support posting multicast/broadcast
13059 * interrupts to a vCPU, we still use interrupt remapping
13060 * for these kind of interrupts.
13061 *
13062 * For lowest-priority interrupts, we only support
13063 * those with single CPU as the destination, e.g. user
13064 * configures the interrupts via /proc/irq or uses
13065 * irqbalance to make the interrupts single-CPU.
13066 *
13067 * We will support full lowest-priority interrupt later.
13068 */
13069
Radim Krčmář371313132016-07-12 22:09:27 +020013070 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013071 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13072 /*
13073 * Make sure the IRTE is in remapped mode if
13074 * we don't handle it in posted mode.
13075 */
13076 ret = irq_set_vcpu_affinity(host_irq, NULL);
13077 if (ret < 0) {
13078 printk(KERN_INFO
13079 "failed to back to remapped mode, irq: %u\n",
13080 host_irq);
13081 goto out;
13082 }
13083
Feng Wuefc64402015-09-18 22:29:51 +080013084 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013085 }
Feng Wuefc64402015-09-18 22:29:51 +080013086
13087 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13088 vcpu_info.vector = irq.vector;
13089
hu huajun2698d822018-04-11 15:16:40 +080013090 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013091 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13092
13093 if (set)
13094 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013095 else
Feng Wuefc64402015-09-18 22:29:51 +080013096 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013097
13098 if (ret < 0) {
13099 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13100 __func__);
13101 goto out;
13102 }
13103 }
13104
13105 ret = 0;
13106out:
13107 srcu_read_unlock(&kvm->irq_srcu, idx);
13108 return ret;
13109}
13110
Ashok Rajc45dcc72016-06-22 14:59:56 +080013111static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13112{
13113 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13114 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13115 FEATURE_CONTROL_LMCE;
13116 else
13117 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13118 ~FEATURE_CONTROL_LMCE;
13119}
13120
Ladi Prosek72d7b372017-10-11 16:54:41 +020013121static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13122{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013123 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13124 if (to_vmx(vcpu)->nested.nested_run_pending)
13125 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013126 return 1;
13127}
13128
Ladi Prosek0234bf82017-10-11 16:54:40 +020013129static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13130{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013131 struct vcpu_vmx *vmx = to_vmx(vcpu);
13132
13133 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13134 if (vmx->nested.smm.guest_mode)
13135 nested_vmx_vmexit(vcpu, -1, 0, 0);
13136
13137 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13138 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013139 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013140 return 0;
13141}
13142
13143static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13144{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013145 struct vcpu_vmx *vmx = to_vmx(vcpu);
13146 int ret;
13147
13148 if (vmx->nested.smm.vmxon) {
13149 vmx->nested.vmxon = true;
13150 vmx->nested.smm.vmxon = false;
13151 }
13152
13153 if (vmx->nested.smm.guest_mode) {
13154 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013155 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013156 vcpu->arch.hflags |= HF_SMM_MASK;
13157 if (ret)
13158 return ret;
13159
13160 vmx->nested.smm.guest_mode = false;
13161 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013162 return 0;
13163}
13164
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013165static int enable_smi_window(struct kvm_vcpu *vcpu)
13166{
13167 return 0;
13168}
13169
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013170static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13171 struct kvm_nested_state __user *user_kvm_nested_state,
13172 u32 user_data_size)
13173{
13174 struct vcpu_vmx *vmx;
13175 struct vmcs12 *vmcs12;
13176 struct kvm_nested_state kvm_state = {
13177 .flags = 0,
13178 .format = 0,
13179 .size = sizeof(kvm_state),
13180 .vmx.vmxon_pa = -1ull,
13181 .vmx.vmcs_pa = -1ull,
13182 };
13183
13184 if (!vcpu)
13185 return kvm_state.size + 2 * VMCS12_SIZE;
13186
13187 vmx = to_vmx(vcpu);
13188 vmcs12 = get_vmcs12(vcpu);
13189 if (nested_vmx_allowed(vcpu) &&
13190 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13191 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13192 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13193
13194 if (vmx->nested.current_vmptr != -1ull)
13195 kvm_state.size += VMCS12_SIZE;
13196
13197 if (vmx->nested.smm.vmxon)
13198 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13199
13200 if (vmx->nested.smm.guest_mode)
13201 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13202
13203 if (is_guest_mode(vcpu)) {
13204 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13205
13206 if (vmx->nested.nested_run_pending)
13207 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13208 }
13209 }
13210
13211 if (user_data_size < kvm_state.size)
13212 goto out;
13213
13214 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13215 return -EFAULT;
13216
13217 if (vmx->nested.current_vmptr == -1ull)
13218 goto out;
13219
13220 /*
13221 * When running L2, the authoritative vmcs12 state is in the
13222 * vmcs02. When running L1, the authoritative vmcs12 state is
13223 * in the shadow vmcs linked to vmcs01, unless
13224 * sync_shadow_vmcs is set, in which case, the authoritative
13225 * vmcs12 state is in the vmcs12 already.
13226 */
13227 if (is_guest_mode(vcpu))
13228 sync_vmcs12(vcpu, vmcs12);
13229 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13230 copy_shadow_to_vmcs12(vmx);
13231
13232 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13233 return -EFAULT;
13234
13235out:
13236 return kvm_state.size;
13237}
13238
13239static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13240 struct kvm_nested_state __user *user_kvm_nested_state,
13241 struct kvm_nested_state *kvm_state)
13242{
13243 struct vcpu_vmx *vmx = to_vmx(vcpu);
13244 struct vmcs12 *vmcs12;
13245 u32 exit_qual;
13246 int ret;
13247
13248 if (kvm_state->format != 0)
13249 return -EINVAL;
13250
13251 if (!nested_vmx_allowed(vcpu))
13252 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13253
13254 if (kvm_state->vmx.vmxon_pa == -1ull) {
13255 if (kvm_state->vmx.smm.flags)
13256 return -EINVAL;
13257
13258 if (kvm_state->vmx.vmcs_pa != -1ull)
13259 return -EINVAL;
13260
13261 vmx_leave_nested(vcpu);
13262 return 0;
13263 }
13264
13265 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13266 return -EINVAL;
13267
13268 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13269 return -EINVAL;
13270
13271 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13272 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13273 return -EINVAL;
13274
13275 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13276 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13277 return -EINVAL;
13278
13279 if (kvm_state->vmx.smm.flags &
13280 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13281 return -EINVAL;
13282
13283 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13284 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13285 return -EINVAL;
13286
13287 vmx_leave_nested(vcpu);
13288 if (kvm_state->vmx.vmxon_pa == -1ull)
13289 return 0;
13290
13291 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13292 ret = enter_vmx_operation(vcpu);
13293 if (ret)
13294 return ret;
13295
13296 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13297
13298 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13299 vmx->nested.smm.vmxon = true;
13300 vmx->nested.vmxon = false;
13301
13302 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13303 vmx->nested.smm.guest_mode = true;
13304 }
13305
13306 vmcs12 = get_vmcs12(vcpu);
13307 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13308 return -EFAULT;
13309
Liran Alon392b2f22018-06-23 02:35:01 +030013310 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013311 return -EINVAL;
13312
13313 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13314 return 0;
13315
13316 vmx->nested.nested_run_pending =
13317 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13318
13319 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13320 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13321 return -EINVAL;
13322
13323 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13324 vmx->nested.nested_run_pending = 1;
13325
13326 vmx->nested.dirty_vmcs12 = true;
13327 ret = enter_vmx_non_root_mode(vcpu, NULL);
13328 if (ret)
13329 return -EINVAL;
13330
13331 return 0;
13332}
13333
Kees Cook404f6aa2016-08-08 16:29:06 -070013334static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013335 .cpu_has_kvm_support = cpu_has_kvm_support,
13336 .disabled_by_bios = vmx_disabled_by_bios,
13337 .hardware_setup = hardware_setup,
13338 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013339 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013340 .hardware_enable = hardware_enable,
13341 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013342 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013343 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013344
Wanpeng Lib31c1142018-03-12 04:53:04 -070013345 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013346 .vm_alloc = vmx_vm_alloc,
13347 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013348
Avi Kivity6aa8b732006-12-10 02:21:36 -080013349 .vcpu_create = vmx_create_vcpu,
13350 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013351 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013352
Avi Kivity04d2cc72007-09-10 18:10:54 +030013353 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013354 .vcpu_load = vmx_vcpu_load,
13355 .vcpu_put = vmx_vcpu_put,
13356
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013357 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013358 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013359 .get_msr = vmx_get_msr,
13360 .set_msr = vmx_set_msr,
13361 .get_segment_base = vmx_get_segment_base,
13362 .get_segment = vmx_get_segment,
13363 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013364 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013365 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013366 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013367 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013368 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013369 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013370 .set_cr3 = vmx_set_cr3,
13371 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013372 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013373 .get_idt = vmx_get_idt,
13374 .set_idt = vmx_set_idt,
13375 .get_gdt = vmx_get_gdt,
13376 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013377 .get_dr6 = vmx_get_dr6,
13378 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013379 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013380 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013381 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013382 .get_rflags = vmx_get_rflags,
13383 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013384
Avi Kivity6aa8b732006-12-10 02:21:36 -080013385 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013386
Avi Kivity6aa8b732006-12-10 02:21:36 -080013387 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013388 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013389 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013390 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13391 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013392 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013393 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013394 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013395 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013396 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013397 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013398 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013399 .get_nmi_mask = vmx_get_nmi_mask,
13400 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013401 .enable_nmi_window = enable_nmi_window,
13402 .enable_irq_window = enable_irq_window,
13403 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013404 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013405 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013406 .get_enable_apicv = vmx_get_enable_apicv,
13407 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013408 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013409 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013410 .hwapic_irr_update = vmx_hwapic_irr_update,
13411 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013412 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13413 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013414
Izik Eiduscbc94022007-10-25 00:29:55 +020013415 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013416 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013417 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013418 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013419
Avi Kivity586f9602010-11-18 13:09:54 +020013420 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013421
Sheng Yang17cc3932010-01-05 19:02:27 +080013422 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013423
13424 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013425
13426 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013427 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013428
13429 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013430
13431 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013432
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013433 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013434 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013435
13436 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013437
13438 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013439 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013440 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013441 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013442 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013443
13444 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013445
13446 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013447
13448 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13449 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13450 .flush_log_dirty = vmx_flush_log_dirty,
13451 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013452 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013453
Feng Wubf9f6ac2015-09-18 22:29:55 +080013454 .pre_block = vmx_pre_block,
13455 .post_block = vmx_post_block,
13456
Wei Huang25462f72015-06-19 15:45:05 +020013457 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013458
13459 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013460
13461#ifdef CONFIG_X86_64
13462 .set_hv_timer = vmx_set_hv_timer,
13463 .cancel_hv_timer = vmx_cancel_hv_timer,
13464#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013465
13466 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013467
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013468 .get_nested_state = vmx_get_nested_state,
13469 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013470 .get_vmcs12_pages = nested_get_vmcs12_pages,
13471
Ladi Prosek72d7b372017-10-11 16:54:41 +020013472 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013473 .pre_enter_smm = vmx_pre_enter_smm,
13474 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013475 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013476};
13477
13478static int __init vmx_init(void)
13479{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013480 int r;
13481
13482#if IS_ENABLED(CONFIG_HYPERV)
13483 /*
13484 * Enlightened VMCS usage should be recommended and the host needs
13485 * to support eVMCS v1 or above. We can also disable eVMCS support
13486 * with module parameter.
13487 */
13488 if (enlightened_vmcs &&
13489 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13490 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13491 KVM_EVMCS_VERSION) {
13492 int cpu;
13493
13494 /* Check that we have assist pages on all online CPUs */
13495 for_each_online_cpu(cpu) {
13496 if (!hv_get_vp_assist_page(cpu)) {
13497 enlightened_vmcs = false;
13498 break;
13499 }
13500 }
13501
13502 if (enlightened_vmcs) {
13503 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13504 static_branch_enable(&enable_evmcs);
13505 }
13506 } else {
13507 enlightened_vmcs = false;
13508 }
13509#endif
13510
13511 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013512 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013513 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013514 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013515
Dave Young2965faa2015-09-09 15:38:55 -070013516#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013517 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13518 crash_vmclear_local_loaded_vmcss);
13519#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013520 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013521
He, Qingfdef3ad2007-04-30 09:45:24 +030013522 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013523}
13524
13525static void __exit vmx_exit(void)
13526{
Dave Young2965faa2015-09-09 15:38:55 -070013527#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013528 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013529 synchronize_rcu();
13530#endif
13531
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013532 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013533
13534#if IS_ENABLED(CONFIG_HYPERV)
13535 if (static_branch_unlikely(&enable_evmcs)) {
13536 int cpu;
13537 struct hv_vp_assist_page *vp_ap;
13538 /*
13539 * Reset everything to support using non-enlightened VMCS
13540 * access later (e.g. when we reload the module with
13541 * enlightened_vmcs=0)
13542 */
13543 for_each_online_cpu(cpu) {
13544 vp_ap = hv_get_vp_assist_page(cpu);
13545
13546 if (!vp_ap)
13547 continue;
13548
13549 vp_ap->current_nested_vmcs = 0;
13550 vp_ap->enlighten_vmentry = 0;
13551 }
13552
13553 static_branch_disable(&enable_evmcs);
13554 }
13555#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013556}
13557
13558module_init(vmx_init)
13559module_exit(vmx_exit)