blob: 4be6486173b7d4195e1681e91e14c4bfdf305c85 [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
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400201struct vmcs {
202 u32 revision_id;
203 u32 abort;
204 char data[0];
205};
206
Nadav Har'Eld462b812011-05-24 15:26:10 +0300207/*
208 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
209 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
210 * loaded on this CPU (so we can clear them if the CPU goes down).
211 */
212struct loaded_vmcs {
213 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700214 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300215 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200216 bool launched;
217 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200218 unsigned long vmcs_host_cr3; /* May not match real cr3 */
219 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100220 /* Support for vnmi-less CPUs */
221 int soft_vnmi_blocked;
222 ktime_t entry_time;
223 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100224 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300225 struct list_head loaded_vmcss_on_cpu_link;
226};
227
Avi Kivity26bb0982009-09-07 11:14:12 +0300228struct shared_msr_entry {
229 unsigned index;
230 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200231 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300232};
233
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300234/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300235 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
236 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
237 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
238 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
239 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
240 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600241 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300242 * underlying hardware which will be used to run L2.
243 * This structure is packed to ensure that its layout is identical across
244 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700245 *
246 * IMPORTANT: Changing the layout of existing fields in this structure
247 * will break save/restore compatibility with older kvm releases. When
248 * adding new fields, either use space in the reserved padding* arrays
249 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300250 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300251typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300252struct __packed vmcs12 {
253 /* According to the Intel spec, a VMCS region must start with the
254 * following two fields. Then follow implementation-specific data.
255 */
256 u32 revision_id;
257 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300258
Nadav Har'El27d6c862011-05-25 23:06:59 +0300259 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
260 u32 padding[7]; /* room for future expansion */
261
Nadav Har'El22bd0352011-05-25 23:05:57 +0300262 u64 io_bitmap_a;
263 u64 io_bitmap_b;
264 u64 msr_bitmap;
265 u64 vm_exit_msr_store_addr;
266 u64 vm_exit_msr_load_addr;
267 u64 vm_entry_msr_load_addr;
268 u64 tsc_offset;
269 u64 virtual_apic_page_addr;
270 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800271 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300272 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800273 u64 eoi_exit_bitmap0;
274 u64 eoi_exit_bitmap1;
275 u64 eoi_exit_bitmap2;
276 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800277 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300278 u64 guest_physical_address;
279 u64 vmcs_link_pointer;
280 u64 guest_ia32_debugctl;
281 u64 guest_ia32_pat;
282 u64 guest_ia32_efer;
283 u64 guest_ia32_perf_global_ctrl;
284 u64 guest_pdptr0;
285 u64 guest_pdptr1;
286 u64 guest_pdptr2;
287 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100288 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300289 u64 host_ia32_pat;
290 u64 host_ia32_efer;
291 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700292 u64 vmread_bitmap;
293 u64 vmwrite_bitmap;
294 u64 vm_function_control;
295 u64 eptp_list_address;
296 u64 pml_address;
297 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300298 /*
299 * To allow migration of L1 (complete with its L2 guests) between
300 * machines of different natural widths (32 or 64 bit), we cannot have
301 * unsigned long fields with no explict size. We use u64 (aliased
302 * natural_width) instead. Luckily, x86 is little-endian.
303 */
304 natural_width cr0_guest_host_mask;
305 natural_width cr4_guest_host_mask;
306 natural_width cr0_read_shadow;
307 natural_width cr4_read_shadow;
308 natural_width cr3_target_value0;
309 natural_width cr3_target_value1;
310 natural_width cr3_target_value2;
311 natural_width cr3_target_value3;
312 natural_width exit_qualification;
313 natural_width guest_linear_address;
314 natural_width guest_cr0;
315 natural_width guest_cr3;
316 natural_width guest_cr4;
317 natural_width guest_es_base;
318 natural_width guest_cs_base;
319 natural_width guest_ss_base;
320 natural_width guest_ds_base;
321 natural_width guest_fs_base;
322 natural_width guest_gs_base;
323 natural_width guest_ldtr_base;
324 natural_width guest_tr_base;
325 natural_width guest_gdtr_base;
326 natural_width guest_idtr_base;
327 natural_width guest_dr7;
328 natural_width guest_rsp;
329 natural_width guest_rip;
330 natural_width guest_rflags;
331 natural_width guest_pending_dbg_exceptions;
332 natural_width guest_sysenter_esp;
333 natural_width guest_sysenter_eip;
334 natural_width host_cr0;
335 natural_width host_cr3;
336 natural_width host_cr4;
337 natural_width host_fs_base;
338 natural_width host_gs_base;
339 natural_width host_tr_base;
340 natural_width host_gdtr_base;
341 natural_width host_idtr_base;
342 natural_width host_ia32_sysenter_esp;
343 natural_width host_ia32_sysenter_eip;
344 natural_width host_rsp;
345 natural_width host_rip;
346 natural_width paddingl[8]; /* room for future expansion */
347 u32 pin_based_vm_exec_control;
348 u32 cpu_based_vm_exec_control;
349 u32 exception_bitmap;
350 u32 page_fault_error_code_mask;
351 u32 page_fault_error_code_match;
352 u32 cr3_target_count;
353 u32 vm_exit_controls;
354 u32 vm_exit_msr_store_count;
355 u32 vm_exit_msr_load_count;
356 u32 vm_entry_controls;
357 u32 vm_entry_msr_load_count;
358 u32 vm_entry_intr_info_field;
359 u32 vm_entry_exception_error_code;
360 u32 vm_entry_instruction_len;
361 u32 tpr_threshold;
362 u32 secondary_vm_exec_control;
363 u32 vm_instruction_error;
364 u32 vm_exit_reason;
365 u32 vm_exit_intr_info;
366 u32 vm_exit_intr_error_code;
367 u32 idt_vectoring_info_field;
368 u32 idt_vectoring_error_code;
369 u32 vm_exit_instruction_len;
370 u32 vmx_instruction_info;
371 u32 guest_es_limit;
372 u32 guest_cs_limit;
373 u32 guest_ss_limit;
374 u32 guest_ds_limit;
375 u32 guest_fs_limit;
376 u32 guest_gs_limit;
377 u32 guest_ldtr_limit;
378 u32 guest_tr_limit;
379 u32 guest_gdtr_limit;
380 u32 guest_idtr_limit;
381 u32 guest_es_ar_bytes;
382 u32 guest_cs_ar_bytes;
383 u32 guest_ss_ar_bytes;
384 u32 guest_ds_ar_bytes;
385 u32 guest_fs_ar_bytes;
386 u32 guest_gs_ar_bytes;
387 u32 guest_ldtr_ar_bytes;
388 u32 guest_tr_ar_bytes;
389 u32 guest_interruptibility_info;
390 u32 guest_activity_state;
391 u32 guest_sysenter_cs;
392 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100393 u32 vmx_preemption_timer_value;
394 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300395 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800396 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300397 u16 guest_es_selector;
398 u16 guest_cs_selector;
399 u16 guest_ss_selector;
400 u16 guest_ds_selector;
401 u16 guest_fs_selector;
402 u16 guest_gs_selector;
403 u16 guest_ldtr_selector;
404 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800405 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300406 u16 host_es_selector;
407 u16 host_cs_selector;
408 u16 host_ss_selector;
409 u16 host_ds_selector;
410 u16 host_fs_selector;
411 u16 host_gs_selector;
412 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700413 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300414};
415
416/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700417 * For save/restore compatibility, the vmcs12 field offsets must not change.
418 */
419#define CHECK_OFFSET(field, loc) \
420 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
421 "Offset of " #field " in struct vmcs12 has changed.")
422
423static inline void vmx_check_vmcs12_offsets(void) {
424 CHECK_OFFSET(revision_id, 0);
425 CHECK_OFFSET(abort, 4);
426 CHECK_OFFSET(launch_state, 8);
427 CHECK_OFFSET(io_bitmap_a, 40);
428 CHECK_OFFSET(io_bitmap_b, 48);
429 CHECK_OFFSET(msr_bitmap, 56);
430 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
431 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
432 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
433 CHECK_OFFSET(tsc_offset, 88);
434 CHECK_OFFSET(virtual_apic_page_addr, 96);
435 CHECK_OFFSET(apic_access_addr, 104);
436 CHECK_OFFSET(posted_intr_desc_addr, 112);
437 CHECK_OFFSET(ept_pointer, 120);
438 CHECK_OFFSET(eoi_exit_bitmap0, 128);
439 CHECK_OFFSET(eoi_exit_bitmap1, 136);
440 CHECK_OFFSET(eoi_exit_bitmap2, 144);
441 CHECK_OFFSET(eoi_exit_bitmap3, 152);
442 CHECK_OFFSET(xss_exit_bitmap, 160);
443 CHECK_OFFSET(guest_physical_address, 168);
444 CHECK_OFFSET(vmcs_link_pointer, 176);
445 CHECK_OFFSET(guest_ia32_debugctl, 184);
446 CHECK_OFFSET(guest_ia32_pat, 192);
447 CHECK_OFFSET(guest_ia32_efer, 200);
448 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
449 CHECK_OFFSET(guest_pdptr0, 216);
450 CHECK_OFFSET(guest_pdptr1, 224);
451 CHECK_OFFSET(guest_pdptr2, 232);
452 CHECK_OFFSET(guest_pdptr3, 240);
453 CHECK_OFFSET(guest_bndcfgs, 248);
454 CHECK_OFFSET(host_ia32_pat, 256);
455 CHECK_OFFSET(host_ia32_efer, 264);
456 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
457 CHECK_OFFSET(vmread_bitmap, 280);
458 CHECK_OFFSET(vmwrite_bitmap, 288);
459 CHECK_OFFSET(vm_function_control, 296);
460 CHECK_OFFSET(eptp_list_address, 304);
461 CHECK_OFFSET(pml_address, 312);
462 CHECK_OFFSET(cr0_guest_host_mask, 344);
463 CHECK_OFFSET(cr4_guest_host_mask, 352);
464 CHECK_OFFSET(cr0_read_shadow, 360);
465 CHECK_OFFSET(cr4_read_shadow, 368);
466 CHECK_OFFSET(cr3_target_value0, 376);
467 CHECK_OFFSET(cr3_target_value1, 384);
468 CHECK_OFFSET(cr3_target_value2, 392);
469 CHECK_OFFSET(cr3_target_value3, 400);
470 CHECK_OFFSET(exit_qualification, 408);
471 CHECK_OFFSET(guest_linear_address, 416);
472 CHECK_OFFSET(guest_cr0, 424);
473 CHECK_OFFSET(guest_cr3, 432);
474 CHECK_OFFSET(guest_cr4, 440);
475 CHECK_OFFSET(guest_es_base, 448);
476 CHECK_OFFSET(guest_cs_base, 456);
477 CHECK_OFFSET(guest_ss_base, 464);
478 CHECK_OFFSET(guest_ds_base, 472);
479 CHECK_OFFSET(guest_fs_base, 480);
480 CHECK_OFFSET(guest_gs_base, 488);
481 CHECK_OFFSET(guest_ldtr_base, 496);
482 CHECK_OFFSET(guest_tr_base, 504);
483 CHECK_OFFSET(guest_gdtr_base, 512);
484 CHECK_OFFSET(guest_idtr_base, 520);
485 CHECK_OFFSET(guest_dr7, 528);
486 CHECK_OFFSET(guest_rsp, 536);
487 CHECK_OFFSET(guest_rip, 544);
488 CHECK_OFFSET(guest_rflags, 552);
489 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
490 CHECK_OFFSET(guest_sysenter_esp, 568);
491 CHECK_OFFSET(guest_sysenter_eip, 576);
492 CHECK_OFFSET(host_cr0, 584);
493 CHECK_OFFSET(host_cr3, 592);
494 CHECK_OFFSET(host_cr4, 600);
495 CHECK_OFFSET(host_fs_base, 608);
496 CHECK_OFFSET(host_gs_base, 616);
497 CHECK_OFFSET(host_tr_base, 624);
498 CHECK_OFFSET(host_gdtr_base, 632);
499 CHECK_OFFSET(host_idtr_base, 640);
500 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
501 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
502 CHECK_OFFSET(host_rsp, 664);
503 CHECK_OFFSET(host_rip, 672);
504 CHECK_OFFSET(pin_based_vm_exec_control, 744);
505 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
506 CHECK_OFFSET(exception_bitmap, 752);
507 CHECK_OFFSET(page_fault_error_code_mask, 756);
508 CHECK_OFFSET(page_fault_error_code_match, 760);
509 CHECK_OFFSET(cr3_target_count, 764);
510 CHECK_OFFSET(vm_exit_controls, 768);
511 CHECK_OFFSET(vm_exit_msr_store_count, 772);
512 CHECK_OFFSET(vm_exit_msr_load_count, 776);
513 CHECK_OFFSET(vm_entry_controls, 780);
514 CHECK_OFFSET(vm_entry_msr_load_count, 784);
515 CHECK_OFFSET(vm_entry_intr_info_field, 788);
516 CHECK_OFFSET(vm_entry_exception_error_code, 792);
517 CHECK_OFFSET(vm_entry_instruction_len, 796);
518 CHECK_OFFSET(tpr_threshold, 800);
519 CHECK_OFFSET(secondary_vm_exec_control, 804);
520 CHECK_OFFSET(vm_instruction_error, 808);
521 CHECK_OFFSET(vm_exit_reason, 812);
522 CHECK_OFFSET(vm_exit_intr_info, 816);
523 CHECK_OFFSET(vm_exit_intr_error_code, 820);
524 CHECK_OFFSET(idt_vectoring_info_field, 824);
525 CHECK_OFFSET(idt_vectoring_error_code, 828);
526 CHECK_OFFSET(vm_exit_instruction_len, 832);
527 CHECK_OFFSET(vmx_instruction_info, 836);
528 CHECK_OFFSET(guest_es_limit, 840);
529 CHECK_OFFSET(guest_cs_limit, 844);
530 CHECK_OFFSET(guest_ss_limit, 848);
531 CHECK_OFFSET(guest_ds_limit, 852);
532 CHECK_OFFSET(guest_fs_limit, 856);
533 CHECK_OFFSET(guest_gs_limit, 860);
534 CHECK_OFFSET(guest_ldtr_limit, 864);
535 CHECK_OFFSET(guest_tr_limit, 868);
536 CHECK_OFFSET(guest_gdtr_limit, 872);
537 CHECK_OFFSET(guest_idtr_limit, 876);
538 CHECK_OFFSET(guest_es_ar_bytes, 880);
539 CHECK_OFFSET(guest_cs_ar_bytes, 884);
540 CHECK_OFFSET(guest_ss_ar_bytes, 888);
541 CHECK_OFFSET(guest_ds_ar_bytes, 892);
542 CHECK_OFFSET(guest_fs_ar_bytes, 896);
543 CHECK_OFFSET(guest_gs_ar_bytes, 900);
544 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
545 CHECK_OFFSET(guest_tr_ar_bytes, 908);
546 CHECK_OFFSET(guest_interruptibility_info, 912);
547 CHECK_OFFSET(guest_activity_state, 916);
548 CHECK_OFFSET(guest_sysenter_cs, 920);
549 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
550 CHECK_OFFSET(vmx_preemption_timer_value, 928);
551 CHECK_OFFSET(virtual_processor_id, 960);
552 CHECK_OFFSET(posted_intr_nv, 962);
553 CHECK_OFFSET(guest_es_selector, 964);
554 CHECK_OFFSET(guest_cs_selector, 966);
555 CHECK_OFFSET(guest_ss_selector, 968);
556 CHECK_OFFSET(guest_ds_selector, 970);
557 CHECK_OFFSET(guest_fs_selector, 972);
558 CHECK_OFFSET(guest_gs_selector, 974);
559 CHECK_OFFSET(guest_ldtr_selector, 976);
560 CHECK_OFFSET(guest_tr_selector, 978);
561 CHECK_OFFSET(guest_intr_status, 980);
562 CHECK_OFFSET(host_es_selector, 982);
563 CHECK_OFFSET(host_cs_selector, 984);
564 CHECK_OFFSET(host_ss_selector, 986);
565 CHECK_OFFSET(host_ds_selector, 988);
566 CHECK_OFFSET(host_fs_selector, 990);
567 CHECK_OFFSET(host_gs_selector, 992);
568 CHECK_OFFSET(host_tr_selector, 994);
569 CHECK_OFFSET(guest_pml_index, 996);
570}
571
572/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300573 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
574 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
575 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700576 *
577 * IMPORTANT: Changing this value will break save/restore compatibility with
578 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300579 */
580#define VMCS12_REVISION 0x11e57ed0
581
582/*
583 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
584 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
585 * current implementation, 4K are reserved to avoid future complications.
586 */
587#define VMCS12_SIZE 0x1000
588
589/*
Jim Mattson5b157062017-12-22 12:11:12 -0800590 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
591 * supported VMCS12 field encoding.
592 */
593#define VMCS12_MAX_FIELD_INDEX 0x17
594
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100595struct nested_vmx_msrs {
596 /*
597 * We only store the "true" versions of the VMX capability MSRs. We
598 * generate the "non-true" versions by setting the must-be-1 bits
599 * according to the SDM.
600 */
601 u32 procbased_ctls_low;
602 u32 procbased_ctls_high;
603 u32 secondary_ctls_low;
604 u32 secondary_ctls_high;
605 u32 pinbased_ctls_low;
606 u32 pinbased_ctls_high;
607 u32 exit_ctls_low;
608 u32 exit_ctls_high;
609 u32 entry_ctls_low;
610 u32 entry_ctls_high;
611 u32 misc_low;
612 u32 misc_high;
613 u32 ept_caps;
614 u32 vpid_caps;
615 u64 basic;
616 u64 cr0_fixed0;
617 u64 cr0_fixed1;
618 u64 cr4_fixed0;
619 u64 cr4_fixed1;
620 u64 vmcs_enum;
621 u64 vmfunc_controls;
622};
623
Jim Mattson5b157062017-12-22 12:11:12 -0800624/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300625 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
626 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
627 */
628struct nested_vmx {
629 /* Has the level1 guest done vmxon? */
630 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400631 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400632 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300633
634 /* The guest-physical address of the current VMCS L1 keeps for L2 */
635 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700636 /*
637 * Cache of the guest's VMCS, existing outside of guest memory.
638 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700639 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700640 */
641 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300642 /*
643 * Indicates if the shadow vmcs must be updated with the
644 * data hold by vmcs12
645 */
646 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100647 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300648
Jim Mattson8d860bb2018-05-09 16:56:05 -0400649 bool change_vmcs01_virtual_apic_mode;
650
Nadav Har'El644d7112011-05-25 23:12:35 +0300651 /* L2 must run next, and mustn't decide to exit to L1. */
652 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600653
654 struct loaded_vmcs vmcs02;
655
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300656 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600657 * Guest pages referred to in the vmcs02 with host-physical
658 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300659 */
660 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800661 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800662 struct page *pi_desc_page;
663 struct pi_desc *pi_desc;
664 bool pi_pending;
665 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100666
667 struct hrtimer preemption_timer;
668 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200669
670 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
671 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800672
Wanpeng Li5c614b32015-10-13 09:18:36 -0700673 u16 vpid02;
674 u16 last_vpid;
675
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100676 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200677
678 /* SMM related state */
679 struct {
680 /* in VMX operation on SMM entry? */
681 bool vmxon;
682 /* in guest mode on SMM entry? */
683 bool guest_mode;
684 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300685};
686
Yang Zhang01e439b2013-04-11 19:25:12 +0800687#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800688#define POSTED_INTR_SN 1
689
Yang Zhang01e439b2013-04-11 19:25:12 +0800690/* Posted-Interrupt Descriptor */
691struct pi_desc {
692 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800693 union {
694 struct {
695 /* bit 256 - Outstanding Notification */
696 u16 on : 1,
697 /* bit 257 - Suppress Notification */
698 sn : 1,
699 /* bit 271:258 - Reserved */
700 rsvd_1 : 14;
701 /* bit 279:272 - Notification Vector */
702 u8 nv;
703 /* bit 287:280 - Reserved */
704 u8 rsvd_2;
705 /* bit 319:288 - Notification Destination */
706 u32 ndst;
707 };
708 u64 control;
709 };
710 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800711} __aligned(64);
712
Yang Zhanga20ed542013-04-11 19:25:15 +0800713static bool pi_test_and_set_on(struct pi_desc *pi_desc)
714{
715 return test_and_set_bit(POSTED_INTR_ON,
716 (unsigned long *)&pi_desc->control);
717}
718
719static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
720{
721 return test_and_clear_bit(POSTED_INTR_ON,
722 (unsigned long *)&pi_desc->control);
723}
724
725static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
726{
727 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
728}
729
Feng Wuebbfc762015-09-18 22:29:46 +0800730static inline void pi_clear_sn(struct pi_desc *pi_desc)
731{
732 return clear_bit(POSTED_INTR_SN,
733 (unsigned long *)&pi_desc->control);
734}
735
736static inline void pi_set_sn(struct pi_desc *pi_desc)
737{
738 return set_bit(POSTED_INTR_SN,
739 (unsigned long *)&pi_desc->control);
740}
741
Paolo Bonziniad361092016-09-20 16:15:05 +0200742static inline void pi_clear_on(struct pi_desc *pi_desc)
743{
744 clear_bit(POSTED_INTR_ON,
745 (unsigned long *)&pi_desc->control);
746}
747
Feng Wuebbfc762015-09-18 22:29:46 +0800748static inline int pi_test_on(struct pi_desc *pi_desc)
749{
750 return test_bit(POSTED_INTR_ON,
751 (unsigned long *)&pi_desc->control);
752}
753
754static inline int pi_test_sn(struct pi_desc *pi_desc)
755{
756 return test_bit(POSTED_INTR_SN,
757 (unsigned long *)&pi_desc->control);
758}
759
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400760struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000761 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300762 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300763 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100764 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300765 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200766 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200767 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300768 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400769 int nmsrs;
770 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800771 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400772#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300773 u64 msr_host_kernel_gs_base;
774 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400775#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100776
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100777 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100778 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100779
Gleb Natapov2961e8762013-11-25 15:37:13 +0200780 u32 vm_entry_controls_shadow;
781 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200782 u32 secondary_exec_control;
783
Nadav Har'Eld462b812011-05-24 15:26:10 +0300784 /*
785 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
786 * non-nested (L1) guest, it always points to vmcs01. For a nested
787 * guest (L2), it points to a different VMCS.
788 */
789 struct loaded_vmcs vmcs01;
790 struct loaded_vmcs *loaded_vmcs;
791 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300792 struct msr_autoload {
793 unsigned nr;
794 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
795 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
796 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400797 struct {
798 int loaded;
799 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300800#ifdef CONFIG_X86_64
801 u16 ds_sel, es_sel;
802#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200803 int gs_ldt_reload_needed;
804 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400805 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200806 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300807 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300808 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300809 struct kvm_segment segs[8];
810 } rmode;
811 struct {
812 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300813 struct kvm_save_segment {
814 u16 selector;
815 unsigned long base;
816 u32 limit;
817 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300818 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300819 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800820 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300821 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200822
Andi Kleena0861c02009-06-08 17:37:09 +0800823 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800824
Yang Zhang01e439b2013-04-11 19:25:12 +0800825 /* Posted interrupt descriptor */
826 struct pi_desc pi_desc;
827
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300828 /* Support for a guest hypervisor (nested VMX) */
829 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200830
831 /* Dynamic PLE window. */
832 int ple_window;
833 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800834
835 /* Support for PML */
836#define PML_ENTITY_NUM 512
837 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800838
Yunhong Jiang64672c92016-06-13 14:19:59 -0700839 /* apic deadline value in host tsc */
840 u64 hv_deadline_tsc;
841
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800842 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800843
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800844 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800845
Wanpeng Li74c55932017-11-29 01:31:20 -0800846 unsigned long host_debugctlmsr;
847
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800848 /*
849 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
850 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
851 * in msr_ia32_feature_control_valid_bits.
852 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800853 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800854 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400855};
856
Avi Kivity2fb92db2011-04-27 19:42:18 +0300857enum segment_cache_field {
858 SEG_FIELD_SEL = 0,
859 SEG_FIELD_BASE = 1,
860 SEG_FIELD_LIMIT = 2,
861 SEG_FIELD_AR = 3,
862
863 SEG_FIELD_NR = 4
864};
865
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700866static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
867{
868 return container_of(kvm, struct kvm_vmx, kvm);
869}
870
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400871static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
872{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000873 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400874}
875
Feng Wuefc64402015-09-18 22:29:51 +0800876static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
877{
878 return &(to_vmx(vcpu)->pi_desc);
879}
880
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800881#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300882#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800883#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
884#define FIELD64(number, name) \
885 FIELD(number, name), \
886 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300887
Abel Gordon4607c2d2013-04-18 14:35:55 +0300888
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100889static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100890#define SHADOW_FIELD_RO(x) x,
891#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300892};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400893static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300894 ARRAY_SIZE(shadow_read_only_fields);
895
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100896static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100897#define SHADOW_FIELD_RW(x) x,
898#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300899};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400900static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300901 ARRAY_SIZE(shadow_read_write_fields);
902
Mathias Krause772e0312012-08-30 01:30:19 +0200903static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300904 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800905 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300906 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
907 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
908 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
909 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
910 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
911 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
912 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
913 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800914 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400915 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300916 FIELD(HOST_ES_SELECTOR, host_es_selector),
917 FIELD(HOST_CS_SELECTOR, host_cs_selector),
918 FIELD(HOST_SS_SELECTOR, host_ss_selector),
919 FIELD(HOST_DS_SELECTOR, host_ds_selector),
920 FIELD(HOST_FS_SELECTOR, host_fs_selector),
921 FIELD(HOST_GS_SELECTOR, host_gs_selector),
922 FIELD(HOST_TR_SELECTOR, host_tr_selector),
923 FIELD64(IO_BITMAP_A, io_bitmap_a),
924 FIELD64(IO_BITMAP_B, io_bitmap_b),
925 FIELD64(MSR_BITMAP, msr_bitmap),
926 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
927 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
928 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700929 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300930 FIELD64(TSC_OFFSET, tsc_offset),
931 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
932 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800933 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400934 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300935 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800936 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
937 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
938 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
939 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400940 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700941 FIELD64(VMREAD_BITMAP, vmread_bitmap),
942 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800943 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300944 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
945 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
946 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
947 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
948 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
949 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
950 FIELD64(GUEST_PDPTR0, guest_pdptr0),
951 FIELD64(GUEST_PDPTR1, guest_pdptr1),
952 FIELD64(GUEST_PDPTR2, guest_pdptr2),
953 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100954 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300955 FIELD64(HOST_IA32_PAT, host_ia32_pat),
956 FIELD64(HOST_IA32_EFER, host_ia32_efer),
957 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
958 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
959 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
960 FIELD(EXCEPTION_BITMAP, exception_bitmap),
961 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
962 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
963 FIELD(CR3_TARGET_COUNT, cr3_target_count),
964 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
965 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
966 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
967 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
968 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
969 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
970 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
971 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
972 FIELD(TPR_THRESHOLD, tpr_threshold),
973 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
974 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
975 FIELD(VM_EXIT_REASON, vm_exit_reason),
976 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
977 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
978 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
979 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
980 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
981 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
982 FIELD(GUEST_ES_LIMIT, guest_es_limit),
983 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
984 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
985 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
986 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
987 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
988 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
989 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
990 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
991 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
992 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
993 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
994 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
995 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
996 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
997 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
998 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
999 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1000 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1001 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1002 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1003 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001004 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001005 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1006 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1007 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1008 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1009 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1010 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1011 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1012 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1013 FIELD(EXIT_QUALIFICATION, exit_qualification),
1014 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1015 FIELD(GUEST_CR0, guest_cr0),
1016 FIELD(GUEST_CR3, guest_cr3),
1017 FIELD(GUEST_CR4, guest_cr4),
1018 FIELD(GUEST_ES_BASE, guest_es_base),
1019 FIELD(GUEST_CS_BASE, guest_cs_base),
1020 FIELD(GUEST_SS_BASE, guest_ss_base),
1021 FIELD(GUEST_DS_BASE, guest_ds_base),
1022 FIELD(GUEST_FS_BASE, guest_fs_base),
1023 FIELD(GUEST_GS_BASE, guest_gs_base),
1024 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1025 FIELD(GUEST_TR_BASE, guest_tr_base),
1026 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1027 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1028 FIELD(GUEST_DR7, guest_dr7),
1029 FIELD(GUEST_RSP, guest_rsp),
1030 FIELD(GUEST_RIP, guest_rip),
1031 FIELD(GUEST_RFLAGS, guest_rflags),
1032 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1033 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1034 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1035 FIELD(HOST_CR0, host_cr0),
1036 FIELD(HOST_CR3, host_cr3),
1037 FIELD(HOST_CR4, host_cr4),
1038 FIELD(HOST_FS_BASE, host_fs_base),
1039 FIELD(HOST_GS_BASE, host_gs_base),
1040 FIELD(HOST_TR_BASE, host_tr_base),
1041 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1042 FIELD(HOST_IDTR_BASE, host_idtr_base),
1043 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1044 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1045 FIELD(HOST_RSP, host_rsp),
1046 FIELD(HOST_RIP, host_rip),
1047};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001048
1049static inline short vmcs_field_to_offset(unsigned long field)
1050{
Dan Williams085331d2018-01-31 17:47:03 -08001051 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1052 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001053 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001054
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001055 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001056 return -ENOENT;
1057
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001058 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001059 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001060 return -ENOENT;
1061
Linus Torvalds15303ba2018-02-10 13:16:35 -08001062 index = array_index_nospec(index, size);
1063 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001064 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001065 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001066 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001067}
1068
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001069static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1070{
David Matlack4f2777b2016-07-13 17:16:37 -07001071 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001072}
1073
Peter Feiner995f00a2017-06-30 17:26:32 -07001074static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001075static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001076static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001077static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001078static void vmx_set_segment(struct kvm_vcpu *vcpu,
1079 struct kvm_segment *var, int seg);
1080static void vmx_get_segment(struct kvm_vcpu *vcpu,
1081 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001082static bool guest_state_valid(struct kvm_vcpu *vcpu);
1083static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001084static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001085static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1086static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1087static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1088 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001089static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001090static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1091 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001092
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1094static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001095/*
1096 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1097 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1098 */
1099static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100
Feng Wubf9f6ac2015-09-18 22:29:55 +08001101/*
1102 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1103 * can find which vCPU should be waken up.
1104 */
1105static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1106static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1107
Radim Krčmář23611332016-09-29 22:41:33 +02001108enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001109 VMX_VMREAD_BITMAP,
1110 VMX_VMWRITE_BITMAP,
1111 VMX_BITMAP_NR
1112};
1113
1114static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1115
Radim Krčmář23611332016-09-29 22:41:33 +02001116#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1117#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001118
Avi Kivity110312c2010-12-21 12:54:20 +02001119static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001120static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001121
Sheng Yang2384d2b2008-01-17 15:14:33 +08001122static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1123static DEFINE_SPINLOCK(vmx_vpid_lock);
1124
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001125static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126 int size;
1127 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001128 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001130 u32 pin_based_exec_ctrl;
1131 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001132 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001133 u32 vmexit_ctrl;
1134 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001135 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001136} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137
Hannes Ederefff9e52008-11-28 17:02:06 +01001138static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001139 u32 ept;
1140 u32 vpid;
1141} vmx_capability;
1142
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143#define VMX_SEGMENT_FIELD(seg) \
1144 [VCPU_SREG_##seg] = { \
1145 .selector = GUEST_##seg##_SELECTOR, \
1146 .base = GUEST_##seg##_BASE, \
1147 .limit = GUEST_##seg##_LIMIT, \
1148 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1149 }
1150
Mathias Krause772e0312012-08-30 01:30:19 +02001151static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 unsigned selector;
1153 unsigned base;
1154 unsigned limit;
1155 unsigned ar_bytes;
1156} kvm_vmx_segment_fields[] = {
1157 VMX_SEGMENT_FIELD(CS),
1158 VMX_SEGMENT_FIELD(DS),
1159 VMX_SEGMENT_FIELD(ES),
1160 VMX_SEGMENT_FIELD(FS),
1161 VMX_SEGMENT_FIELD(GS),
1162 VMX_SEGMENT_FIELD(SS),
1163 VMX_SEGMENT_FIELD(TR),
1164 VMX_SEGMENT_FIELD(LDTR),
1165};
1166
Avi Kivity26bb0982009-09-07 11:14:12 +03001167static u64 host_efer;
1168
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001169static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1170
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001171/*
Brian Gerst8c065852010-07-17 09:03:26 -04001172 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001173 * away by decrementing the array size.
1174 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001176#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001177 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001179 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001182DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1183
1184#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1185
1186#define KVM_EVMCS_VERSION 1
1187
1188#if IS_ENABLED(CONFIG_HYPERV)
1189static bool __read_mostly enlightened_vmcs = true;
1190module_param(enlightened_vmcs, bool, 0444);
1191
1192static inline void evmcs_write64(unsigned long field, u64 value)
1193{
1194 u16 clean_field;
1195 int offset = get_evmcs_offset(field, &clean_field);
1196
1197 if (offset < 0)
1198 return;
1199
1200 *(u64 *)((char *)current_evmcs + offset) = value;
1201
1202 current_evmcs->hv_clean_fields &= ~clean_field;
1203}
1204
1205static inline void evmcs_write32(unsigned long field, u32 value)
1206{
1207 u16 clean_field;
1208 int offset = get_evmcs_offset(field, &clean_field);
1209
1210 if (offset < 0)
1211 return;
1212
1213 *(u32 *)((char *)current_evmcs + offset) = value;
1214 current_evmcs->hv_clean_fields &= ~clean_field;
1215}
1216
1217static inline void evmcs_write16(unsigned long field, u16 value)
1218{
1219 u16 clean_field;
1220 int offset = get_evmcs_offset(field, &clean_field);
1221
1222 if (offset < 0)
1223 return;
1224
1225 *(u16 *)((char *)current_evmcs + offset) = value;
1226 current_evmcs->hv_clean_fields &= ~clean_field;
1227}
1228
1229static inline u64 evmcs_read64(unsigned long field)
1230{
1231 int offset = get_evmcs_offset(field, NULL);
1232
1233 if (offset < 0)
1234 return 0;
1235
1236 return *(u64 *)((char *)current_evmcs + offset);
1237}
1238
1239static inline u32 evmcs_read32(unsigned long field)
1240{
1241 int offset = get_evmcs_offset(field, NULL);
1242
1243 if (offset < 0)
1244 return 0;
1245
1246 return *(u32 *)((char *)current_evmcs + offset);
1247}
1248
1249static inline u16 evmcs_read16(unsigned long field)
1250{
1251 int offset = get_evmcs_offset(field, NULL);
1252
1253 if (offset < 0)
1254 return 0;
1255
1256 return *(u16 *)((char *)current_evmcs + offset);
1257}
1258
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001259static inline void evmcs_touch_msr_bitmap(void)
1260{
1261 if (unlikely(!current_evmcs))
1262 return;
1263
1264 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1265 current_evmcs->hv_clean_fields &=
1266 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1267}
1268
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001269static void evmcs_load(u64 phys_addr)
1270{
1271 struct hv_vp_assist_page *vp_ap =
1272 hv_get_vp_assist_page(smp_processor_id());
1273
1274 vp_ap->current_nested_vmcs = phys_addr;
1275 vp_ap->enlighten_vmentry = 1;
1276}
1277
1278static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1279{
1280 /*
1281 * Enlightened VMCSv1 doesn't support these:
1282 *
1283 * POSTED_INTR_NV = 0x00000002,
1284 * GUEST_INTR_STATUS = 0x00000810,
1285 * APIC_ACCESS_ADDR = 0x00002014,
1286 * POSTED_INTR_DESC_ADDR = 0x00002016,
1287 * EOI_EXIT_BITMAP0 = 0x0000201c,
1288 * EOI_EXIT_BITMAP1 = 0x0000201e,
1289 * EOI_EXIT_BITMAP2 = 0x00002020,
1290 * EOI_EXIT_BITMAP3 = 0x00002022,
1291 */
1292 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1293 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1294 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1295 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1296 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1297 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1298 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1299
1300 /*
1301 * GUEST_PML_INDEX = 0x00000812,
1302 * PML_ADDRESS = 0x0000200e,
1303 */
1304 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1305
1306 /* VM_FUNCTION_CONTROL = 0x00002018, */
1307 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1308
1309 /*
1310 * EPTP_LIST_ADDRESS = 0x00002024,
1311 * VMREAD_BITMAP = 0x00002026,
1312 * VMWRITE_BITMAP = 0x00002028,
1313 */
1314 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1315
1316 /*
1317 * TSC_MULTIPLIER = 0x00002032,
1318 */
1319 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1320
1321 /*
1322 * PLE_GAP = 0x00004020,
1323 * PLE_WINDOW = 0x00004022,
1324 */
1325 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1326
1327 /*
1328 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1329 */
1330 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1331
1332 /*
1333 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1334 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1335 */
1336 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1337 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1338
1339 /*
1340 * Currently unsupported in KVM:
1341 * GUEST_IA32_RTIT_CTL = 0x00002814,
1342 */
1343}
1344#else /* !IS_ENABLED(CONFIG_HYPERV) */
1345static inline void evmcs_write64(unsigned long field, u64 value) {}
1346static inline void evmcs_write32(unsigned long field, u32 value) {}
1347static inline void evmcs_write16(unsigned long field, u16 value) {}
1348static inline u64 evmcs_read64(unsigned long field) { return 0; }
1349static inline u32 evmcs_read32(unsigned long field) { return 0; }
1350static inline u16 evmcs_read16(unsigned long field) { return 0; }
1351static inline void evmcs_load(u64 phys_addr) {}
1352static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001353static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001354#endif /* IS_ENABLED(CONFIG_HYPERV) */
1355
Jan Kiszka5bb16012016-02-09 20:14:21 +01001356static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357{
1358 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1359 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001360 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1361}
1362
Jan Kiszka6f054852016-02-09 20:15:18 +01001363static inline bool is_debug(u32 intr_info)
1364{
1365 return is_exception_n(intr_info, DB_VECTOR);
1366}
1367
1368static inline bool is_breakpoint(u32 intr_info)
1369{
1370 return is_exception_n(intr_info, BP_VECTOR);
1371}
1372
Jan Kiszka5bb16012016-02-09 20:14:21 +01001373static inline bool is_page_fault(u32 intr_info)
1374{
1375 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376}
1377
Gui Jianfeng31299942010-03-15 17:29:09 +08001378static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001379{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001380 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001381}
1382
Gui Jianfeng31299942010-03-15 17:29:09 +08001383static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001384{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001385 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001386}
1387
Liran Alon9e869482018-03-12 13:12:51 +02001388static inline bool is_gp_fault(u32 intr_info)
1389{
1390 return is_exception_n(intr_info, GP_VECTOR);
1391}
1392
Gui Jianfeng31299942010-03-15 17:29:09 +08001393static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394{
1395 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1396 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1397}
1398
Gui Jianfeng31299942010-03-15 17:29:09 +08001399static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001400{
1401 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1402 INTR_INFO_VALID_MASK)) ==
1403 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1404}
1405
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001406/* Undocumented: icebp/int1 */
1407static inline bool is_icebp(u32 intr_info)
1408{
1409 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1410 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1411}
1412
Gui Jianfeng31299942010-03-15 17:29:09 +08001413static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001414{
Sheng Yang04547152009-04-01 15:52:31 +08001415 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001416}
1417
Gui Jianfeng31299942010-03-15 17:29:09 +08001418static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001419{
Sheng Yang04547152009-04-01 15:52:31 +08001420 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001421}
1422
Paolo Bonzini35754c92015-07-29 12:05:37 +02001423static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001424{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001425 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001426}
1427
Gui Jianfeng31299942010-03-15 17:29:09 +08001428static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001429{
Sheng Yang04547152009-04-01 15:52:31 +08001430 return vmcs_config.cpu_based_exec_ctrl &
1431 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001432}
1433
Avi Kivity774ead32007-12-26 13:57:04 +02001434static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001435{
Sheng Yang04547152009-04-01 15:52:31 +08001436 return vmcs_config.cpu_based_2nd_exec_ctrl &
1437 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1438}
1439
Yang Zhang8d146952013-01-25 10:18:50 +08001440static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1441{
1442 return vmcs_config.cpu_based_2nd_exec_ctrl &
1443 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1444}
1445
Yang Zhang83d4c282013-01-25 10:18:49 +08001446static inline bool cpu_has_vmx_apic_register_virt(void)
1447{
1448 return vmcs_config.cpu_based_2nd_exec_ctrl &
1449 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1450}
1451
Yang Zhangc7c9c562013-01-25 10:18:51 +08001452static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1453{
1454 return vmcs_config.cpu_based_2nd_exec_ctrl &
1455 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1456}
1457
Yunhong Jiang64672c92016-06-13 14:19:59 -07001458/*
1459 * Comment's format: document - errata name - stepping - processor name.
1460 * Refer from
1461 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1462 */
1463static u32 vmx_preemption_cpu_tfms[] = {
1464/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
14650x000206E6,
1466/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1467/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1468/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
14690x00020652,
1470/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
14710x00020655,
1472/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1473/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1474/*
1475 * 320767.pdf - AAP86 - B1 -
1476 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1477 */
14780x000106E5,
1479/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
14800x000106A0,
1481/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
14820x000106A1,
1483/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
14840x000106A4,
1485 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1486 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1487 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
14880x000106A5,
1489};
1490
1491static inline bool cpu_has_broken_vmx_preemption_timer(void)
1492{
1493 u32 eax = cpuid_eax(0x00000001), i;
1494
1495 /* Clear the reserved bits */
1496 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001497 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001498 if (eax == vmx_preemption_cpu_tfms[i])
1499 return true;
1500
1501 return false;
1502}
1503
1504static inline bool cpu_has_vmx_preemption_timer(void)
1505{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001506 return vmcs_config.pin_based_exec_ctrl &
1507 PIN_BASED_VMX_PREEMPTION_TIMER;
1508}
1509
Yang Zhang01e439b2013-04-11 19:25:12 +08001510static inline bool cpu_has_vmx_posted_intr(void)
1511{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001512 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1513 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001514}
1515
1516static inline bool cpu_has_vmx_apicv(void)
1517{
1518 return cpu_has_vmx_apic_register_virt() &&
1519 cpu_has_vmx_virtual_intr_delivery() &&
1520 cpu_has_vmx_posted_intr();
1521}
1522
Sheng Yang04547152009-04-01 15:52:31 +08001523static inline bool cpu_has_vmx_flexpriority(void)
1524{
1525 return cpu_has_vmx_tpr_shadow() &&
1526 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001527}
1528
Marcelo Tosattie7997942009-06-11 12:07:40 -03001529static inline bool cpu_has_vmx_ept_execute_only(void)
1530{
Gui Jianfeng31299942010-03-15 17:29:09 +08001531 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001532}
1533
Marcelo Tosattie7997942009-06-11 12:07:40 -03001534static inline bool cpu_has_vmx_ept_2m_page(void)
1535{
Gui Jianfeng31299942010-03-15 17:29:09 +08001536 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001537}
1538
Sheng Yang878403b2010-01-05 19:02:29 +08001539static inline bool cpu_has_vmx_ept_1g_page(void)
1540{
Gui Jianfeng31299942010-03-15 17:29:09 +08001541 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001542}
1543
Sheng Yang4bc9b982010-06-02 14:05:24 +08001544static inline bool cpu_has_vmx_ept_4levels(void)
1545{
1546 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1547}
1548
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001549static inline bool cpu_has_vmx_ept_mt_wb(void)
1550{
1551 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1552}
1553
Yu Zhang855feb62017-08-24 20:27:55 +08001554static inline bool cpu_has_vmx_ept_5levels(void)
1555{
1556 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1557}
1558
Xudong Hao83c3a332012-05-28 19:33:35 +08001559static inline bool cpu_has_vmx_ept_ad_bits(void)
1560{
1561 return vmx_capability.ept & VMX_EPT_AD_BIT;
1562}
1563
Gui Jianfeng31299942010-03-15 17:29:09 +08001564static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001565{
Gui Jianfeng31299942010-03-15 17:29:09 +08001566 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001567}
1568
Gui Jianfeng31299942010-03-15 17:29:09 +08001569static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001570{
Gui Jianfeng31299942010-03-15 17:29:09 +08001571 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001572}
1573
Liran Aloncd9a4912018-05-22 17:16:15 +03001574static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1575{
1576 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1577}
1578
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001579static inline bool cpu_has_vmx_invvpid_single(void)
1580{
1581 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1582}
1583
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001584static inline bool cpu_has_vmx_invvpid_global(void)
1585{
1586 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1587}
1588
Wanpeng Li08d839c2017-03-23 05:30:08 -07001589static inline bool cpu_has_vmx_invvpid(void)
1590{
1591 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1592}
1593
Gui Jianfeng31299942010-03-15 17:29:09 +08001594static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001595{
Sheng Yang04547152009-04-01 15:52:31 +08001596 return vmcs_config.cpu_based_2nd_exec_ctrl &
1597 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001598}
1599
Gui Jianfeng31299942010-03-15 17:29:09 +08001600static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001601{
1602 return vmcs_config.cpu_based_2nd_exec_ctrl &
1603 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1604}
1605
Gui Jianfeng31299942010-03-15 17:29:09 +08001606static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001607{
1608 return vmcs_config.cpu_based_2nd_exec_ctrl &
1609 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1610}
1611
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001612static inline bool cpu_has_vmx_basic_inout(void)
1613{
1614 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1615}
1616
Paolo Bonzini35754c92015-07-29 12:05:37 +02001617static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001618{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001619 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001620}
1621
Gui Jianfeng31299942010-03-15 17:29:09 +08001622static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001623{
Sheng Yang04547152009-04-01 15:52:31 +08001624 return vmcs_config.cpu_based_2nd_exec_ctrl &
1625 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001626}
1627
Gui Jianfeng31299942010-03-15 17:29:09 +08001628static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001629{
1630 return vmcs_config.cpu_based_2nd_exec_ctrl &
1631 SECONDARY_EXEC_RDTSCP;
1632}
1633
Mao, Junjiead756a12012-07-02 01:18:48 +00001634static inline bool cpu_has_vmx_invpcid(void)
1635{
1636 return vmcs_config.cpu_based_2nd_exec_ctrl &
1637 SECONDARY_EXEC_ENABLE_INVPCID;
1638}
1639
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001640static inline bool cpu_has_virtual_nmis(void)
1641{
1642 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1643}
1644
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001645static inline bool cpu_has_vmx_wbinvd_exit(void)
1646{
1647 return vmcs_config.cpu_based_2nd_exec_ctrl &
1648 SECONDARY_EXEC_WBINVD_EXITING;
1649}
1650
Abel Gordonabc4fc52013-04-18 14:35:25 +03001651static inline bool cpu_has_vmx_shadow_vmcs(void)
1652{
1653 u64 vmx_msr;
1654 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1655 /* check if the cpu supports writing r/o exit information fields */
1656 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1657 return false;
1658
1659 return vmcs_config.cpu_based_2nd_exec_ctrl &
1660 SECONDARY_EXEC_SHADOW_VMCS;
1661}
1662
Kai Huang843e4332015-01-28 10:54:28 +08001663static inline bool cpu_has_vmx_pml(void)
1664{
1665 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1666}
1667
Haozhong Zhang64903d62015-10-20 15:39:09 +08001668static inline bool cpu_has_vmx_tsc_scaling(void)
1669{
1670 return vmcs_config.cpu_based_2nd_exec_ctrl &
1671 SECONDARY_EXEC_TSC_SCALING;
1672}
1673
Bandan Das2a499e42017-08-03 15:54:41 -04001674static inline bool cpu_has_vmx_vmfunc(void)
1675{
1676 return vmcs_config.cpu_based_2nd_exec_ctrl &
1677 SECONDARY_EXEC_ENABLE_VMFUNC;
1678}
1679
Sean Christopherson64f7a112018-04-30 10:01:06 -07001680static bool vmx_umip_emulated(void)
1681{
1682 return vmcs_config.cpu_based_2nd_exec_ctrl &
1683 SECONDARY_EXEC_DESC;
1684}
1685
Sheng Yang04547152009-04-01 15:52:31 +08001686static inline bool report_flexpriority(void)
1687{
1688 return flexpriority_enabled;
1689}
1690
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001691static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1692{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001693 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001694}
1695
Jim Mattsonf4160e42018-05-29 09:11:33 -07001696/*
1697 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1698 * to modify any valid field of the VMCS, or are the VM-exit
1699 * information fields read-only?
1700 */
1701static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1702{
1703 return to_vmx(vcpu)->nested.msrs.misc_low &
1704 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1705}
1706
Marc Orr04473782018-06-20 17:21:29 -07001707static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1708{
1709 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1710}
1711
1712static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1713{
1714 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1715 CPU_BASED_MONITOR_TRAP_FLAG;
1716}
1717
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001718static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1719{
1720 return vmcs12->cpu_based_vm_exec_control & bit;
1721}
1722
1723static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1724{
1725 return (vmcs12->cpu_based_vm_exec_control &
1726 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1727 (vmcs12->secondary_vm_exec_control & bit);
1728}
1729
Jan Kiszkaf4124502014-03-07 20:03:13 +01001730static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1731{
1732 return vmcs12->pin_based_vm_exec_control &
1733 PIN_BASED_VMX_PREEMPTION_TIMER;
1734}
1735
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001736static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1737{
1738 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1739}
1740
1741static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1742{
1743 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1744}
1745
Nadav Har'El155a97a2013-08-05 11:07:16 +03001746static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1747{
1748 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1749}
1750
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001751static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1752{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001753 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001754}
1755
Bandan Dasc5f983f2017-05-05 15:25:14 -04001756static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1757{
1758 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1759}
1760
Wincy Vanf2b93282015-02-03 23:56:03 +08001761static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1762{
1763 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1764}
1765
Wanpeng Li5c614b32015-10-13 09:18:36 -07001766static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1767{
1768 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1769}
1770
Wincy Van82f0dd42015-02-03 23:57:18 +08001771static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1772{
1773 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1774}
1775
Wincy Van608406e2015-02-03 23:57:51 +08001776static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1777{
1778 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1779}
1780
Wincy Van705699a2015-02-03 23:58:17 +08001781static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1782{
1783 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1784}
1785
Bandan Das27c42a12017-08-03 15:54:42 -04001786static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1787{
1788 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1789}
1790
Bandan Das41ab9372017-08-03 15:54:43 -04001791static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1792{
1793 return nested_cpu_has_vmfunc(vmcs12) &&
1794 (vmcs12->vm_function_control &
1795 VMX_VMFUNC_EPTP_SWITCHING);
1796}
1797
Jim Mattsonef85b672016-12-12 11:01:37 -08001798static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001799{
1800 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001801 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001802}
1803
Jan Kiszka533558b2014-01-04 18:47:20 +01001804static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1805 u32 exit_intr_info,
1806 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001807static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1808 struct vmcs12 *vmcs12,
1809 u32 reason, unsigned long qualification);
1810
Rusty Russell8b9cf982007-07-30 16:31:43 +10001811static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001812{
1813 int i;
1814
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001815 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001816 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001817 return i;
1818 return -1;
1819}
1820
Sheng Yang2384d2b2008-01-17 15:14:33 +08001821static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1822{
1823 struct {
1824 u64 vpid : 16;
1825 u64 rsvd : 48;
1826 u64 gva;
1827 } operand = { vpid, 0, gva };
1828
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001829 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001830 /* CF==1 or ZF==1 --> rc = -1 */
1831 "; ja 1f ; ud2 ; 1:"
1832 : : "a"(&operand), "c"(ext) : "cc", "memory");
1833}
1834
Sheng Yang14394422008-04-28 12:24:45 +08001835static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1836{
1837 struct {
1838 u64 eptp, gpa;
1839 } operand = {eptp, gpa};
1840
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001841 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001842 /* CF==1 or ZF==1 --> rc = -1 */
1843 "; ja 1f ; ud2 ; 1:\n"
1844 : : "a" (&operand), "c" (ext) : "cc", "memory");
1845}
1846
Avi Kivity26bb0982009-09-07 11:14:12 +03001847static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001848{
1849 int i;
1850
Rusty Russell8b9cf982007-07-30 16:31:43 +10001851 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001852 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001853 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001854 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001855}
1856
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857static void vmcs_clear(struct vmcs *vmcs)
1858{
1859 u64 phys_addr = __pa(vmcs);
1860 u8 error;
1861
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001862 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001863 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 : "cc", "memory");
1865 if (error)
1866 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1867 vmcs, phys_addr);
1868}
1869
Nadav Har'Eld462b812011-05-24 15:26:10 +03001870static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1871{
1872 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001873 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1874 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001875 loaded_vmcs->cpu = -1;
1876 loaded_vmcs->launched = 0;
1877}
1878
Dongxiao Xu7725b892010-05-11 18:29:38 +08001879static void vmcs_load(struct vmcs *vmcs)
1880{
1881 u64 phys_addr = __pa(vmcs);
1882 u8 error;
1883
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001884 if (static_branch_unlikely(&enable_evmcs))
1885 return evmcs_load(phys_addr);
1886
Dongxiao Xu7725b892010-05-11 18:29:38 +08001887 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001888 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001889 : "cc", "memory");
1890 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001891 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001892 vmcs, phys_addr);
1893}
1894
Dave Young2965faa2015-09-09 15:38:55 -07001895#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001896/*
1897 * This bitmap is used to indicate whether the vmclear
1898 * operation is enabled on all cpus. All disabled by
1899 * default.
1900 */
1901static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1902
1903static inline void crash_enable_local_vmclear(int cpu)
1904{
1905 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1906}
1907
1908static inline void crash_disable_local_vmclear(int cpu)
1909{
1910 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1911}
1912
1913static inline int crash_local_vmclear_enabled(int cpu)
1914{
1915 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1916}
1917
1918static void crash_vmclear_local_loaded_vmcss(void)
1919{
1920 int cpu = raw_smp_processor_id();
1921 struct loaded_vmcs *v;
1922
1923 if (!crash_local_vmclear_enabled(cpu))
1924 return;
1925
1926 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1927 loaded_vmcss_on_cpu_link)
1928 vmcs_clear(v->vmcs);
1929}
1930#else
1931static inline void crash_enable_local_vmclear(int cpu) { }
1932static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001933#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001934
Nadav Har'Eld462b812011-05-24 15:26:10 +03001935static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001937 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001938 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939
Nadav Har'Eld462b812011-05-24 15:26:10 +03001940 if (loaded_vmcs->cpu != cpu)
1941 return; /* vcpu migration can race with cpu offline */
1942 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001944 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001945 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001946
1947 /*
1948 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1949 * is before setting loaded_vmcs->vcpu to -1 which is done in
1950 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1951 * then adds the vmcs into percpu list before it is deleted.
1952 */
1953 smp_wmb();
1954
Nadav Har'Eld462b812011-05-24 15:26:10 +03001955 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001956 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957}
1958
Nadav Har'Eld462b812011-05-24 15:26:10 +03001959static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001960{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001961 int cpu = loaded_vmcs->cpu;
1962
1963 if (cpu != -1)
1964 smp_call_function_single(cpu,
1965 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001966}
1967
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001968static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001969{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001970 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001971 return;
1972
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001973 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001974 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001975}
1976
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001977static inline void vpid_sync_vcpu_global(void)
1978{
1979 if (cpu_has_vmx_invvpid_global())
1980 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1981}
1982
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001983static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001984{
1985 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001986 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001987 else
1988 vpid_sync_vcpu_global();
1989}
1990
Sheng Yang14394422008-04-28 12:24:45 +08001991static inline void ept_sync_global(void)
1992{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001993 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001994}
1995
1996static inline void ept_sync_context(u64 eptp)
1997{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001998 if (cpu_has_vmx_invept_context())
1999 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2000 else
2001 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002002}
2003
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002004static __always_inline void vmcs_check16(unsigned long field)
2005{
2006 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2007 "16-bit accessor invalid for 64-bit field");
2008 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2009 "16-bit accessor invalid for 64-bit high field");
2010 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2011 "16-bit accessor invalid for 32-bit high field");
2012 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2013 "16-bit accessor invalid for natural width field");
2014}
2015
2016static __always_inline void vmcs_check32(unsigned long field)
2017{
2018 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2019 "32-bit accessor invalid for 16-bit field");
2020 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2021 "32-bit accessor invalid for natural width field");
2022}
2023
2024static __always_inline void vmcs_check64(unsigned long field)
2025{
2026 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2027 "64-bit accessor invalid for 16-bit field");
2028 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2029 "64-bit accessor invalid for 64-bit high field");
2030 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2031 "64-bit accessor invalid for 32-bit field");
2032 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2033 "64-bit accessor invalid for natural width field");
2034}
2035
2036static __always_inline void vmcs_checkl(unsigned long field)
2037{
2038 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2039 "Natural width accessor invalid for 16-bit field");
2040 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2041 "Natural width accessor invalid for 64-bit field");
2042 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2043 "Natural width accessor invalid for 64-bit high field");
2044 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2045 "Natural width accessor invalid for 32-bit field");
2046}
2047
2048static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049{
Avi Kivity5e520e62011-05-15 10:13:12 -04002050 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051
Avi Kivity5e520e62011-05-15 10:13:12 -04002052 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2053 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 return value;
2055}
2056
Avi Kivity96304212011-05-15 10:13:13 -04002057static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002059 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002060 if (static_branch_unlikely(&enable_evmcs))
2061 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002062 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063}
2064
Avi Kivity96304212011-05-15 10:13:13 -04002065static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002067 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002068 if (static_branch_unlikely(&enable_evmcs))
2069 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002070 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071}
2072
Avi Kivity96304212011-05-15 10:13:13 -04002073static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002075 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002076 if (static_branch_unlikely(&enable_evmcs))
2077 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002078#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002079 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002080#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002081 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082#endif
2083}
2084
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002085static __always_inline unsigned long vmcs_readl(unsigned long field)
2086{
2087 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002088 if (static_branch_unlikely(&enable_evmcs))
2089 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002090 return __vmcs_readl(field);
2091}
2092
Avi Kivitye52de1b2007-01-05 16:36:56 -08002093static noinline void vmwrite_error(unsigned long field, unsigned long value)
2094{
2095 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2096 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2097 dump_stack();
2098}
2099
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002100static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101{
2102 u8 error;
2103
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002104 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002105 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002106 if (unlikely(error))
2107 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108}
2109
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002110static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002112 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002113 if (static_branch_unlikely(&enable_evmcs))
2114 return evmcs_write16(field, value);
2115
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002116 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117}
2118
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002119static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002121 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002122 if (static_branch_unlikely(&enable_evmcs))
2123 return evmcs_write32(field, value);
2124
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002125 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126}
2127
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002128static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002130 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002131 if (static_branch_unlikely(&enable_evmcs))
2132 return evmcs_write64(field, value);
2133
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002134 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002135#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002137 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138#endif
2139}
2140
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002141static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002142{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002143 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002144 if (static_branch_unlikely(&enable_evmcs))
2145 return evmcs_write64(field, value);
2146
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002147 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002148}
2149
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002150static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002151{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002152 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2153 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002154 if (static_branch_unlikely(&enable_evmcs))
2155 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2156
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2158}
2159
2160static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2161{
2162 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2163 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002164 if (static_branch_unlikely(&enable_evmcs))
2165 return evmcs_write32(field, evmcs_read32(field) | mask);
2166
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002167 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002168}
2169
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002170static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2171{
2172 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2173}
2174
Gleb Natapov2961e8762013-11-25 15:37:13 +02002175static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2176{
2177 vmcs_write32(VM_ENTRY_CONTROLS, val);
2178 vmx->vm_entry_controls_shadow = val;
2179}
2180
2181static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2182{
2183 if (vmx->vm_entry_controls_shadow != val)
2184 vm_entry_controls_init(vmx, val);
2185}
2186
2187static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2188{
2189 return vmx->vm_entry_controls_shadow;
2190}
2191
2192
2193static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2194{
2195 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2196}
2197
2198static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2199{
2200 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2201}
2202
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002203static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2204{
2205 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2206}
2207
Gleb Natapov2961e8762013-11-25 15:37:13 +02002208static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2209{
2210 vmcs_write32(VM_EXIT_CONTROLS, val);
2211 vmx->vm_exit_controls_shadow = val;
2212}
2213
2214static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2215{
2216 if (vmx->vm_exit_controls_shadow != val)
2217 vm_exit_controls_init(vmx, val);
2218}
2219
2220static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2221{
2222 return vmx->vm_exit_controls_shadow;
2223}
2224
2225
2226static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2227{
2228 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2229}
2230
2231static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2232{
2233 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2234}
2235
Avi Kivity2fb92db2011-04-27 19:42:18 +03002236static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2237{
2238 vmx->segment_cache.bitmask = 0;
2239}
2240
2241static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2242 unsigned field)
2243{
2244 bool ret;
2245 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2246
2247 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2248 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2249 vmx->segment_cache.bitmask = 0;
2250 }
2251 ret = vmx->segment_cache.bitmask & mask;
2252 vmx->segment_cache.bitmask |= mask;
2253 return ret;
2254}
2255
2256static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2257{
2258 u16 *p = &vmx->segment_cache.seg[seg].selector;
2259
2260 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2261 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2262 return *p;
2263}
2264
2265static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2266{
2267 ulong *p = &vmx->segment_cache.seg[seg].base;
2268
2269 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2270 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2271 return *p;
2272}
2273
2274static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2275{
2276 u32 *p = &vmx->segment_cache.seg[seg].limit;
2277
2278 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2279 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2280 return *p;
2281}
2282
2283static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2284{
2285 u32 *p = &vmx->segment_cache.seg[seg].ar;
2286
2287 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2288 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2289 return *p;
2290}
2291
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002292static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2293{
2294 u32 eb;
2295
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002296 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002297 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002298 /*
2299 * Guest access to VMware backdoor ports could legitimately
2300 * trigger #GP because of TSS I/O permission bitmap.
2301 * We intercept those #GP and allow access to them anyway
2302 * as VMware does.
2303 */
2304 if (enable_vmware_backdoor)
2305 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002306 if ((vcpu->guest_debug &
2307 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2308 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2309 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002310 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002311 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002312 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002313 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002314
2315 /* When we are running a nested L2 guest and L1 specified for it a
2316 * certain exception bitmap, we must trap the same exceptions and pass
2317 * them to L1. When running L2, we will only handle the exceptions
2318 * specified above if L1 did not want them.
2319 */
2320 if (is_guest_mode(vcpu))
2321 eb |= get_vmcs12(vcpu)->exception_bitmap;
2322
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002323 vmcs_write32(EXCEPTION_BITMAP, eb);
2324}
2325
Ashok Raj15d45072018-02-01 22:59:43 +01002326/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002327 * Check if MSR is intercepted for currently loaded MSR bitmap.
2328 */
2329static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2330{
2331 unsigned long *msr_bitmap;
2332 int f = sizeof(unsigned long);
2333
2334 if (!cpu_has_vmx_msr_bitmap())
2335 return true;
2336
2337 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2338
2339 if (msr <= 0x1fff) {
2340 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2341 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2342 msr &= 0x1fff;
2343 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2344 }
2345
2346 return true;
2347}
2348
2349/*
Ashok Raj15d45072018-02-01 22:59:43 +01002350 * Check if MSR is intercepted for L01 MSR bitmap.
2351 */
2352static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2353{
2354 unsigned long *msr_bitmap;
2355 int f = sizeof(unsigned long);
2356
2357 if (!cpu_has_vmx_msr_bitmap())
2358 return true;
2359
2360 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2361
2362 if (msr <= 0x1fff) {
2363 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2364 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2365 msr &= 0x1fff;
2366 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2367 }
2368
2369 return true;
2370}
2371
Gleb Natapov2961e8762013-11-25 15:37:13 +02002372static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2373 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002374{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002375 vm_entry_controls_clearbit(vmx, entry);
2376 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002377}
2378
Avi Kivity61d2ef22010-04-28 16:40:38 +03002379static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2380{
2381 unsigned i;
2382 struct msr_autoload *m = &vmx->msr_autoload;
2383
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002384 switch (msr) {
2385 case MSR_EFER:
2386 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002387 clear_atomic_switch_msr_special(vmx,
2388 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002389 VM_EXIT_LOAD_IA32_EFER);
2390 return;
2391 }
2392 break;
2393 case MSR_CORE_PERF_GLOBAL_CTRL:
2394 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002395 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002396 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2397 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2398 return;
2399 }
2400 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002401 }
2402
Avi Kivity61d2ef22010-04-28 16:40:38 +03002403 for (i = 0; i < m->nr; ++i)
2404 if (m->guest[i].index == msr)
2405 break;
2406
2407 if (i == m->nr)
2408 return;
2409 --m->nr;
2410 m->guest[i] = m->guest[m->nr];
2411 m->host[i] = m->host[m->nr];
2412 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2413 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2414}
2415
Gleb Natapov2961e8762013-11-25 15:37:13 +02002416static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2417 unsigned long entry, unsigned long exit,
2418 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2419 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002420{
2421 vmcs_write64(guest_val_vmcs, guest_val);
2422 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002423 vm_entry_controls_setbit(vmx, entry);
2424 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002425}
2426
Avi Kivity61d2ef22010-04-28 16:40:38 +03002427static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2428 u64 guest_val, u64 host_val)
2429{
2430 unsigned i;
2431 struct msr_autoload *m = &vmx->msr_autoload;
2432
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002433 switch (msr) {
2434 case MSR_EFER:
2435 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002436 add_atomic_switch_msr_special(vmx,
2437 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002438 VM_EXIT_LOAD_IA32_EFER,
2439 GUEST_IA32_EFER,
2440 HOST_IA32_EFER,
2441 guest_val, host_val);
2442 return;
2443 }
2444 break;
2445 case MSR_CORE_PERF_GLOBAL_CTRL:
2446 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002447 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002448 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2449 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2450 GUEST_IA32_PERF_GLOBAL_CTRL,
2451 HOST_IA32_PERF_GLOBAL_CTRL,
2452 guest_val, host_val);
2453 return;
2454 }
2455 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002456 case MSR_IA32_PEBS_ENABLE:
2457 /* PEBS needs a quiescent period after being disabled (to write
2458 * a record). Disabling PEBS through VMX MSR swapping doesn't
2459 * provide that period, so a CPU could write host's record into
2460 * guest's memory.
2461 */
2462 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002463 }
2464
Avi Kivity61d2ef22010-04-28 16:40:38 +03002465 for (i = 0; i < m->nr; ++i)
2466 if (m->guest[i].index == msr)
2467 break;
2468
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002469 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002470 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002471 "Can't add msr %x\n", msr);
2472 return;
2473 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002474 ++m->nr;
2475 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2476 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2477 }
2478
2479 m->guest[i].index = msr;
2480 m->guest[i].value = guest_val;
2481 m->host[i].index = msr;
2482 m->host[i].value = host_val;
2483}
2484
Avi Kivity92c0d902009-10-29 11:00:16 +02002485static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002486{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002487 u64 guest_efer = vmx->vcpu.arch.efer;
2488 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002489
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002490 if (!enable_ept) {
2491 /*
2492 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2493 * host CPUID is more efficient than testing guest CPUID
2494 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2495 */
2496 if (boot_cpu_has(X86_FEATURE_SMEP))
2497 guest_efer |= EFER_NX;
2498 else if (!(guest_efer & EFER_NX))
2499 ignore_bits |= EFER_NX;
2500 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002501
Avi Kivity51c6cf62007-08-29 03:48:05 +03002502 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002503 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002504 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002505 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002506#ifdef CONFIG_X86_64
2507 ignore_bits |= EFER_LMA | EFER_LME;
2508 /* SCE is meaningful only in long mode on Intel */
2509 if (guest_efer & EFER_LMA)
2510 ignore_bits &= ~(u64)EFER_SCE;
2511#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002512
2513 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002514
2515 /*
2516 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2517 * On CPUs that support "load IA32_EFER", always switch EFER
2518 * atomically, since it's faster than switching it manually.
2519 */
2520 if (cpu_has_load_ia32_efer ||
2521 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002522 if (!(guest_efer & EFER_LMA))
2523 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002524 if (guest_efer != host_efer)
2525 add_atomic_switch_msr(vmx, MSR_EFER,
2526 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002527 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002528 } else {
2529 guest_efer &= ~ignore_bits;
2530 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002531
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002532 vmx->guest_msrs[efer_offset].data = guest_efer;
2533 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2534
2535 return true;
2536 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002537}
2538
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002539#ifdef CONFIG_X86_32
2540/*
2541 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2542 * VMCS rather than the segment table. KVM uses this helper to figure
2543 * out the current bases to poke them into the VMCS before entry.
2544 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002545static unsigned long segment_base(u16 selector)
2546{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002547 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002548 unsigned long v;
2549
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002550 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002551 return 0;
2552
Thomas Garnier45fc8752017-03-14 10:05:08 -07002553 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002554
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002555 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002556 u16 ldt_selector = kvm_read_ldt();
2557
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002558 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002559 return 0;
2560
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002561 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002562 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002563 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002564 return v;
2565}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002566#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002567
Avi Kivity04d2cc72007-09-10 18:10:54 +03002568static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002569{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002570 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002571#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002572 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002573 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002574#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002575 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002576
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002577 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002578 return;
2579
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002580 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002581 /*
2582 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2583 * allow segment selectors with cpl > 0 or ti == 1.
2584 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002585 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002586 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002587
2588#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002589 if (likely(is_64bit_mm(current->mm))) {
2590 save_fsgs_for_kvm();
2591 vmx->host_state.fs_sel = current->thread.fsindex;
2592 vmx->host_state.gs_sel = current->thread.gsindex;
2593 fs_base = current->thread.fsbase;
2594 kernel_gs_base = current->thread.gsbase;
2595 } else {
2596#endif
2597 savesegment(fs, vmx->host_state.fs_sel);
2598 savesegment(gs, vmx->host_state.gs_sel);
2599#ifdef CONFIG_X86_64
2600 fs_base = read_msr(MSR_FS_BASE);
2601 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2602 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002603#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002604 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002605 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002606 vmx->host_state.fs_reload_needed = 0;
2607 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002608 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002609 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002610 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002611 if (!(vmx->host_state.gs_sel & 7))
2612 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002613 else {
2614 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002615 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002616 }
2617
2618#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002619 savesegment(ds, vmx->host_state.ds_sel);
2620 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002621
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002622 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002623 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002624
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002625 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002626 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002627 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002628#else
2629 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2630 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2631#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002632 for (i = 0; i < vmx->save_nmsrs; ++i)
2633 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002634 vmx->guest_msrs[i].data,
2635 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002636}
2637
Avi Kivitya9b21b62008-06-24 11:48:49 +03002638static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002639{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002640 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002641 return;
2642
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002643 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002644 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002645#ifdef CONFIG_X86_64
2646 if (is_long_mode(&vmx->vcpu))
2647 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2648#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002649 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002650 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002651#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002652 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002653#else
2654 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002655#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002656 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002657 if (vmx->host_state.fs_reload_needed)
2658 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002659#ifdef CONFIG_X86_64
2660 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2661 loadsegment(ds, vmx->host_state.ds_sel);
2662 loadsegment(es, vmx->host_state.es_sel);
2663 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002664#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002665 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002666#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002667 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002668#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002669 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002670}
2671
Avi Kivitya9b21b62008-06-24 11:48:49 +03002672static void vmx_load_host_state(struct vcpu_vmx *vmx)
2673{
2674 preempt_disable();
2675 __vmx_load_host_state(vmx);
2676 preempt_enable();
2677}
2678
Feng Wu28b835d2015-09-18 22:29:54 +08002679static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2680{
2681 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2682 struct pi_desc old, new;
2683 unsigned int dest;
2684
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002685 /*
2686 * In case of hot-plug or hot-unplug, we may have to undo
2687 * vmx_vcpu_pi_put even if there is no assigned device. And we
2688 * always keep PI.NDST up to date for simplicity: it makes the
2689 * code easier, and CPU migration is not a fast path.
2690 */
2691 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002692 return;
2693
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002694 /*
2695 * First handle the simple case where no cmpxchg is necessary; just
2696 * allow posting non-urgent interrupts.
2697 *
2698 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2699 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2700 * expects the VCPU to be on the blocked_vcpu_list that matches
2701 * PI.NDST.
2702 */
2703 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2704 vcpu->cpu == cpu) {
2705 pi_clear_sn(pi_desc);
2706 return;
2707 }
2708
2709 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002710 do {
2711 old.control = new.control = pi_desc->control;
2712
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002713 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002714
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002715 if (x2apic_enabled())
2716 new.ndst = dest;
2717 else
2718 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002719
Feng Wu28b835d2015-09-18 22:29:54 +08002720 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002721 } while (cmpxchg64(&pi_desc->control, old.control,
2722 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002723}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002724
Peter Feinerc95ba922016-08-17 09:36:47 -07002725static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2726{
2727 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2728 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2729}
2730
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731/*
2732 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2733 * vcpu mutex is already taken.
2734 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002735static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002737 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002738 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002740 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002741 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002742 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002743 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002744
2745 /*
2746 * Read loaded_vmcs->cpu should be before fetching
2747 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2748 * See the comments in __loaded_vmcs_clear().
2749 */
2750 smp_rmb();
2751
Nadav Har'Eld462b812011-05-24 15:26:10 +03002752 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2753 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002754 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002755 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002756 }
2757
2758 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2759 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2760 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002761 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002762 }
2763
2764 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002765 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002766 unsigned long sysenter_esp;
2767
2768 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002769
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 /*
2771 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002772 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002774 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002775 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002776 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002778 /*
2779 * VM exits change the host TR limit to 0x67 after a VM
2780 * exit. This is okay, since 0x67 covers everything except
2781 * the IO bitmap and have have code to handle the IO bitmap
2782 * being lost after a VM exit.
2783 */
2784 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2785
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2787 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002788
Nadav Har'Eld462b812011-05-24 15:26:10 +03002789 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 }
Feng Wu28b835d2015-09-18 22:29:54 +08002791
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002792 /* Setup TSC multiplier */
2793 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002794 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2795 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002796
Feng Wu28b835d2015-09-18 22:29:54 +08002797 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002798 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002799 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002800}
2801
2802static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2803{
2804 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2805
2806 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002807 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2808 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002809 return;
2810
2811 /* Set SN when the vCPU is preempted */
2812 if (vcpu->preempted)
2813 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814}
2815
2816static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2817{
Feng Wu28b835d2015-09-18 22:29:54 +08002818 vmx_vcpu_pi_put(vcpu);
2819
Avi Kivitya9b21b62008-06-24 11:48:49 +03002820 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821}
2822
Wanpeng Lif244dee2017-07-20 01:11:54 -07002823static bool emulation_required(struct kvm_vcpu *vcpu)
2824{
2825 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2826}
2827
Avi Kivityedcafe32009-12-30 18:07:40 +02002828static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2829
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002830/*
2831 * Return the cr0 value that a nested guest would read. This is a combination
2832 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2833 * its hypervisor (cr0_read_shadow).
2834 */
2835static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2836{
2837 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2838 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2839}
2840static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2841{
2842 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2843 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2844}
2845
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2847{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002848 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002849
Avi Kivity6de12732011-03-07 12:51:22 +02002850 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2851 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2852 rflags = vmcs_readl(GUEST_RFLAGS);
2853 if (to_vmx(vcpu)->rmode.vm86_active) {
2854 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2855 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2856 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2857 }
2858 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002859 }
Avi Kivity6de12732011-03-07 12:51:22 +02002860 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861}
2862
2863static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2864{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002865 unsigned long old_rflags = vmx_get_rflags(vcpu);
2866
Avi Kivity6de12732011-03-07 12:51:22 +02002867 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2868 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002869 if (to_vmx(vcpu)->rmode.vm86_active) {
2870 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002871 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002872 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002874
2875 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2876 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877}
2878
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002879static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002880{
2881 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2882 int ret = 0;
2883
2884 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002885 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002886 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002887 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002888
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002889 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002890}
2891
2892static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2893{
2894 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2895 u32 interruptibility = interruptibility_old;
2896
2897 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2898
Jan Kiszka48005f62010-02-19 19:38:07 +01002899 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002900 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002901 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002902 interruptibility |= GUEST_INTR_STATE_STI;
2903
2904 if ((interruptibility != interruptibility_old))
2905 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2906}
2907
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2909{
2910 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002912 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002914 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915
Glauber Costa2809f5d2009-05-12 16:21:05 -04002916 /* skipping an emulated instruction also counts */
2917 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918}
2919
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002920static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2921 unsigned long exit_qual)
2922{
2923 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2924 unsigned int nr = vcpu->arch.exception.nr;
2925 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2926
2927 if (vcpu->arch.exception.has_error_code) {
2928 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2929 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2930 }
2931
2932 if (kvm_exception_is_soft(nr))
2933 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2934 else
2935 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2936
2937 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2938 vmx_get_nmi_mask(vcpu))
2939 intr_info |= INTR_INFO_UNBLOCK_NMI;
2940
2941 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2942}
2943
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002944/*
2945 * KVM wants to inject page-faults which it got to the guest. This function
2946 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002947 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002948static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002949{
2950 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002951 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002952
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002953 if (nr == PF_VECTOR) {
2954 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002955 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002956 return 1;
2957 }
2958 /*
2959 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2960 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2961 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2962 * can be written only when inject_pending_event runs. This should be
2963 * conditional on a new capability---if the capability is disabled,
2964 * kvm_multiple_exception would write the ancillary information to
2965 * CR2 or DR6, for backwards ABI-compatibility.
2966 */
2967 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2968 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002969 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002970 return 1;
2971 }
2972 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002973 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002974 if (nr == DB_VECTOR)
2975 *exit_qual = vcpu->arch.dr6;
2976 else
2977 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002978 return 1;
2979 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002980 }
2981
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002982 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002983}
2984
Wanpeng Licaa057a2018-03-12 04:53:03 -07002985static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2986{
2987 /*
2988 * Ensure that we clear the HLT state in the VMCS. We don't need to
2989 * explicitly skip the instruction because if the HLT state is set,
2990 * then the instruction is already executing and RIP has already been
2991 * advanced.
2992 */
2993 if (kvm_hlt_in_guest(vcpu->kvm) &&
2994 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2995 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2996}
2997
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002998static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002999{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003000 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003001 unsigned nr = vcpu->arch.exception.nr;
3002 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003003 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003004 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003005
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003006 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003007 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003008 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3009 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003010
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003011 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003012 int inc_eip = 0;
3013 if (kvm_exception_is_soft(nr))
3014 inc_eip = vcpu->arch.event_exit_inst_len;
3015 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003016 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003017 return;
3018 }
3019
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003020 WARN_ON_ONCE(vmx->emulation_required);
3021
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003022 if (kvm_exception_is_soft(nr)) {
3023 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3024 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003025 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3026 } else
3027 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3028
3029 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003030
3031 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003032}
3033
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003034static bool vmx_rdtscp_supported(void)
3035{
3036 return cpu_has_vmx_rdtscp();
3037}
3038
Mao, Junjiead756a12012-07-02 01:18:48 +00003039static bool vmx_invpcid_supported(void)
3040{
3041 return cpu_has_vmx_invpcid() && enable_ept;
3042}
3043
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044/*
Eddie Donga75beee2007-05-17 18:55:15 +03003045 * Swap MSR entry in host/guest MSR entry array.
3046 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003047static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003048{
Avi Kivity26bb0982009-09-07 11:14:12 +03003049 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003050
3051 tmp = vmx->guest_msrs[to];
3052 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3053 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003054}
3055
3056/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003057 * Set up the vmcs to automatically save and restore system
3058 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3059 * mode, as fiddling with msrs is very expensive.
3060 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003061static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003062{
Avi Kivity26bb0982009-09-07 11:14:12 +03003063 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003064
Eddie Donga75beee2007-05-17 18:55:15 +03003065 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003066#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003067 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003068 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003069 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003070 move_msr_up(vmx, index, save_nmsrs++);
3071 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003072 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003073 move_msr_up(vmx, index, save_nmsrs++);
3074 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003075 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003076 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003077 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003078 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003079 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003080 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003081 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003082 * if efer.sce is enabled.
3083 */
Brian Gerst8c065852010-07-17 09:03:26 -04003084 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003085 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003086 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003087 }
Eddie Donga75beee2007-05-17 18:55:15 +03003088#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003089 index = __find_msr_index(vmx, MSR_EFER);
3090 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003091 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003092
Avi Kivity26bb0982009-09-07 11:14:12 +03003093 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003094
Yang Zhang8d146952013-01-25 10:18:50 +08003095 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003096 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003097}
3098
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003099static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003101 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003103 if (is_guest_mode(vcpu) &&
3104 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3105 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3106
3107 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108}
3109
3110/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003111 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003113static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003115 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003116 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003117 * We're here if L1 chose not to trap WRMSR to TSC. According
3118 * to the spec, this should set L1's TSC; The offset that L1
3119 * set for L2 remains unchanged, and still needs to be added
3120 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003121 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003122 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003123 /* recalculate vmcs02.TSC_OFFSET: */
3124 vmcs12 = get_vmcs12(vcpu);
3125 vmcs_write64(TSC_OFFSET, offset +
3126 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3127 vmcs12->tsc_offset : 0));
3128 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003129 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3130 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003131 vmcs_write64(TSC_OFFSET, offset);
3132 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133}
3134
Nadav Har'El801d3422011-05-25 23:02:23 +03003135/*
3136 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3137 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3138 * all guests if the "nested" module option is off, and can also be disabled
3139 * for a single guest by disabling its VMX cpuid bit.
3140 */
3141static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3142{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003143 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003144}
3145
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003147 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3148 * returned for the various VMX controls MSRs when nested VMX is enabled.
3149 * The same values should also be used to verify that vmcs12 control fields are
3150 * valid during nested entry from L1 to L2.
3151 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3152 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3153 * bit in the high half is on if the corresponding bit in the control field
3154 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003155 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003156static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003157{
Paolo Bonzini13893092018-02-26 13:40:09 +01003158 if (!nested) {
3159 memset(msrs, 0, sizeof(*msrs));
3160 return;
3161 }
3162
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003163 /*
3164 * Note that as a general rule, the high half of the MSRs (bits in
3165 * the control fields which may be 1) should be initialized by the
3166 * intersection of the underlying hardware's MSR (i.e., features which
3167 * can be supported) and the list of features we want to expose -
3168 * because they are known to be properly supported in our code.
3169 * Also, usually, the low half of the MSRs (bits which must be 1) can
3170 * be set to 0, meaning that L1 may turn off any of these bits. The
3171 * reason is that if one of these bits is necessary, it will appear
3172 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3173 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003174 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003175 * These rules have exceptions below.
3176 */
3177
3178 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003179 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003180 msrs->pinbased_ctls_low,
3181 msrs->pinbased_ctls_high);
3182 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003183 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003184 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003185 PIN_BASED_EXT_INTR_MASK |
3186 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003187 PIN_BASED_VIRTUAL_NMIS |
3188 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003189 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003190 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003191 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003192
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003193 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003194 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003195 msrs->exit_ctls_low,
3196 msrs->exit_ctls_high);
3197 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003198 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003199
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003200 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003202 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003203#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003204 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003205 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003206 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003207 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003208 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3209
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003210 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003211 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003212
Jan Kiszka2996fca2014-06-16 13:59:43 +02003213 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003214 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003215
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003216 /* entry controls */
3217 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003218 msrs->entry_ctls_low,
3219 msrs->entry_ctls_high);
3220 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003221 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003222 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003223#ifdef CONFIG_X86_64
3224 VM_ENTRY_IA32E_MODE |
3225#endif
3226 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003227 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003228 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003229 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003230 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003231
Jan Kiszka2996fca2014-06-16 13:59:43 +02003232 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003233 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003234
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003235 /* cpu-based controls */
3236 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003237 msrs->procbased_ctls_low,
3238 msrs->procbased_ctls_high);
3239 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003240 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003242 CPU_BASED_VIRTUAL_INTR_PENDING |
3243 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003244 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3245 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3246 CPU_BASED_CR3_STORE_EXITING |
3247#ifdef CONFIG_X86_64
3248 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3249#endif
3250 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003251 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3252 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3253 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3254 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003255 /*
3256 * We can allow some features even when not supported by the
3257 * hardware. For example, L1 can specify an MSR bitmap - and we
3258 * can use it to avoid exits to L1 - even when L0 runs L2
3259 * without MSR bitmaps.
3260 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003261 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003262 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003263 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003264
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003265 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003266 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003267 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3268
Paolo Bonzini80154d72017-08-24 13:55:35 +02003269 /*
3270 * secondary cpu-based controls. Do not include those that
3271 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3272 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003273 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003274 msrs->secondary_ctls_low,
3275 msrs->secondary_ctls_high);
3276 msrs->secondary_ctls_low = 0;
3277 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003278 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003279 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003280 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003281 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003282 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003283 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003284
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003285 if (enable_ept) {
3286 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003287 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003288 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003289 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003290 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003291 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003293 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003294 msrs->ept_caps &= vmx_capability.ept;
3295 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003296 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3297 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003298 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003299 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003300 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003301 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003302 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003303 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003304
Bandan Das27c42a12017-08-03 15:54:42 -04003305 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003307 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003308 /*
3309 * Advertise EPTP switching unconditionally
3310 * since we emulate it
3311 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003312 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003313 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003314 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003315 }
3316
Paolo Bonzinief697a72016-03-18 16:58:38 +01003317 /*
3318 * Old versions of KVM use the single-context version without
3319 * checking for support, so declare that it is supported even
3320 * though it is treated as global context. The alternative is
3321 * not failing the single-context invvpid, and it is worse.
3322 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003323 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003324 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003325 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003327 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003328 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003329
Radim Krčmář0790ec12015-03-17 14:02:32 +01003330 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003331 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003332 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3333
Jan Kiszkac18911a2013-03-13 16:06:41 +01003334 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003335 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003336 msrs->misc_low,
3337 msrs->misc_high);
3338 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3339 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003340 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003341 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003342 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003343 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003344
3345 /*
3346 * This MSR reports some information about VMX support. We
3347 * should return information about the VMX we emulate for the
3348 * guest, and the VMCS structure we give it - not about the
3349 * VMX support of the underlying hardware.
3350 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003351 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003352 VMCS12_REVISION |
3353 VMX_BASIC_TRUE_CTLS |
3354 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3355 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3356
3357 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003359
3360 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003361 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003362 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3363 * We picked the standard core2 setting.
3364 */
3365#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3366#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003367 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3368 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003369
3370 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003371 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3372 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003373
3374 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003375 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003376}
3377
David Matlack38991522016-11-29 18:14:08 -08003378/*
3379 * if fixed0[i] == 1: val[i] must be 1
3380 * if fixed1[i] == 0: val[i] must be 0
3381 */
3382static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3383{
3384 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003385}
3386
3387static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3388{
David Matlack38991522016-11-29 18:14:08 -08003389 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003390}
3391
3392static inline u64 vmx_control_msr(u32 low, u32 high)
3393{
3394 return low | ((u64)high << 32);
3395}
3396
David Matlack62cc6b9d2016-11-29 18:14:07 -08003397static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3398{
3399 superset &= mask;
3400 subset &= mask;
3401
3402 return (superset | subset) == superset;
3403}
3404
3405static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3406{
3407 const u64 feature_and_reserved =
3408 /* feature (except bit 48; see below) */
3409 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3410 /* reserved */
3411 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003412 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003413
3414 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3415 return -EINVAL;
3416
3417 /*
3418 * KVM does not emulate a version of VMX that constrains physical
3419 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3420 */
3421 if (data & BIT_ULL(48))
3422 return -EINVAL;
3423
3424 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3425 vmx_basic_vmcs_revision_id(data))
3426 return -EINVAL;
3427
3428 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3429 return -EINVAL;
3430
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003431 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003432 return 0;
3433}
3434
3435static int
3436vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3437{
3438 u64 supported;
3439 u32 *lowp, *highp;
3440
3441 switch (msr_index) {
3442 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003443 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3444 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003445 break;
3446 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003447 lowp = &vmx->nested.msrs.procbased_ctls_low;
3448 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003449 break;
3450 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003451 lowp = &vmx->nested.msrs.exit_ctls_low;
3452 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003453 break;
3454 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003455 lowp = &vmx->nested.msrs.entry_ctls_low;
3456 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003457 break;
3458 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 lowp = &vmx->nested.msrs.secondary_ctls_low;
3460 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003461 break;
3462 default:
3463 BUG();
3464 }
3465
3466 supported = vmx_control_msr(*lowp, *highp);
3467
3468 /* Check must-be-1 bits are still 1. */
3469 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3470 return -EINVAL;
3471
3472 /* Check must-be-0 bits are still 0. */
3473 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3474 return -EINVAL;
3475
3476 *lowp = data;
3477 *highp = data >> 32;
3478 return 0;
3479}
3480
3481static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3482{
3483 const u64 feature_and_reserved_bits =
3484 /* feature */
3485 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3486 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3487 /* reserved */
3488 GENMASK_ULL(13, 9) | BIT_ULL(31);
3489 u64 vmx_misc;
3490
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003491 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3492 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003493
3494 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3495 return -EINVAL;
3496
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003498 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3499 vmx_misc_preemption_timer_rate(data) !=
3500 vmx_misc_preemption_timer_rate(vmx_misc))
3501 return -EINVAL;
3502
3503 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3504 return -EINVAL;
3505
3506 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3507 return -EINVAL;
3508
3509 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3510 return -EINVAL;
3511
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003512 vmx->nested.msrs.misc_low = data;
3513 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003514
3515 /*
3516 * If L1 has read-only VM-exit information fields, use the
3517 * less permissive vmx_vmwrite_bitmap to specify write
3518 * permissions for the shadow VMCS.
3519 */
3520 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3521 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3522
David Matlack62cc6b9d2016-11-29 18:14:07 -08003523 return 0;
3524}
3525
3526static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3527{
3528 u64 vmx_ept_vpid_cap;
3529
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003530 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3531 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003532
3533 /* Every bit is either reserved or a feature bit. */
3534 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3535 return -EINVAL;
3536
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 vmx->nested.msrs.ept_caps = data;
3538 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003539 return 0;
3540}
3541
3542static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3543{
3544 u64 *msr;
3545
3546 switch (msr_index) {
3547 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003549 break;
3550 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003552 break;
3553 default:
3554 BUG();
3555 }
3556
3557 /*
3558 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3559 * must be 1 in the restored value.
3560 */
3561 if (!is_bitwise_subset(data, *msr, -1ULL))
3562 return -EINVAL;
3563
3564 *msr = data;
3565 return 0;
3566}
3567
3568/*
3569 * Called when userspace is restoring VMX MSRs.
3570 *
3571 * Returns 0 on success, non-0 otherwise.
3572 */
3573static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3574{
3575 struct vcpu_vmx *vmx = to_vmx(vcpu);
3576
Jim Mattsona943ac52018-05-29 09:11:32 -07003577 /*
3578 * Don't allow changes to the VMX capability MSRs while the vCPU
3579 * is in VMX operation.
3580 */
3581 if (vmx->nested.vmxon)
3582 return -EBUSY;
3583
David Matlack62cc6b9d2016-11-29 18:14:07 -08003584 switch (msr_index) {
3585 case MSR_IA32_VMX_BASIC:
3586 return vmx_restore_vmx_basic(vmx, data);
3587 case MSR_IA32_VMX_PINBASED_CTLS:
3588 case MSR_IA32_VMX_PROCBASED_CTLS:
3589 case MSR_IA32_VMX_EXIT_CTLS:
3590 case MSR_IA32_VMX_ENTRY_CTLS:
3591 /*
3592 * The "non-true" VMX capability MSRs are generated from the
3593 * "true" MSRs, so we do not support restoring them directly.
3594 *
3595 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3596 * should restore the "true" MSRs with the must-be-1 bits
3597 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3598 * DEFAULT SETTINGS".
3599 */
3600 return -EINVAL;
3601 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3602 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3603 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3604 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3605 case MSR_IA32_VMX_PROCBASED_CTLS2:
3606 return vmx_restore_control_msr(vmx, msr_index, data);
3607 case MSR_IA32_VMX_MISC:
3608 return vmx_restore_vmx_misc(vmx, data);
3609 case MSR_IA32_VMX_CR0_FIXED0:
3610 case MSR_IA32_VMX_CR4_FIXED0:
3611 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3612 case MSR_IA32_VMX_CR0_FIXED1:
3613 case MSR_IA32_VMX_CR4_FIXED1:
3614 /*
3615 * These MSRs are generated based on the vCPU's CPUID, so we
3616 * do not support restoring them directly.
3617 */
3618 return -EINVAL;
3619 case MSR_IA32_VMX_EPT_VPID_CAP:
3620 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3621 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003622 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003623 return 0;
3624 default:
3625 /*
3626 * The rest of the VMX capability MSRs do not support restore.
3627 */
3628 return -EINVAL;
3629 }
3630}
3631
Jan Kiszkacae50132014-01-04 18:47:22 +01003632/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003634{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003635 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003636 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003637 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003638 break;
3639 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3640 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003641 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 msrs->pinbased_ctls_low,
3643 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003644 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3645 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003646 break;
3647 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3648 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003649 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003650 msrs->procbased_ctls_low,
3651 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003652 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3653 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003654 break;
3655 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3656 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003657 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->exit_ctls_low,
3659 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003660 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3661 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003662 break;
3663 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3664 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003665 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 msrs->entry_ctls_low,
3667 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003668 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3669 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003670 break;
3671 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003672 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 msrs->misc_low,
3674 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003675 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003676 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003677 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003678 break;
3679 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003680 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003681 break;
3682 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003684 break;
3685 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003687 break;
3688 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003689 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003690 break;
3691 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003692 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 msrs->secondary_ctls_low,
3694 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003695 break;
3696 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003697 *pdata = msrs->ept_caps |
3698 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003699 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003700 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003701 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003702 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003704 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003705 }
3706
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003707 return 0;
3708}
3709
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003710static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3711 uint64_t val)
3712{
3713 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3714
3715 return !(val & ~valid_bits);
3716}
3717
Tom Lendacky801e4592018-02-21 13:39:51 -06003718static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3719{
Paolo Bonzini13893092018-02-26 13:40:09 +01003720 switch (msr->index) {
3721 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3722 if (!nested)
3723 return 1;
3724 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3725 default:
3726 return 1;
3727 }
3728
3729 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003730}
3731
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003732/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733 * Reads an msr value (of 'msr_index') into 'pdata'.
3734 * Returns 0 on success, non-0 otherwise.
3735 * Assumes vcpu_load() was already called.
3736 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003737static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003738{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003739 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003740 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003741
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003742 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003743#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003745 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746 break;
3747 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003748 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003750 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003751 vmx_load_host_state(vmx);
3752 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003753 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003754#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003756 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003757 case MSR_IA32_SPEC_CTRL:
3758 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003759 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3760 return 1;
3761
3762 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3763 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003764 case MSR_IA32_ARCH_CAPABILITIES:
3765 if (!msr_info->host_initiated &&
3766 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3767 return 1;
3768 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3769 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003770 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003771 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772 break;
3773 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003774 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 break;
3776 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003777 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003779 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003780 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003781 (!msr_info->host_initiated &&
3782 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003783 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003784 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003785 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003786 case MSR_IA32_MCG_EXT_CTL:
3787 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003788 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003789 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003790 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003791 msr_info->data = vcpu->arch.mcg_ext_ctl;
3792 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003793 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003794 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003795 break;
3796 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3797 if (!nested_vmx_allowed(vcpu))
3798 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003799 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3800 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003801 case MSR_IA32_XSS:
3802 if (!vmx_xsaves_supported())
3803 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003804 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003805 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003806 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003807 if (!msr_info->host_initiated &&
3808 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003809 return 1;
3810 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003812 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003813 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003814 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003815 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003817 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818 }
3819
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 return 0;
3821}
3822
Jan Kiszkacae50132014-01-04 18:47:22 +01003823static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3824
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825/*
3826 * Writes msr value into into the appropriate "register".
3827 * Returns 0 on success, non-0 otherwise.
3828 * Assumes vcpu_load() was already called.
3829 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003830static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003832 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003833 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003834 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003835 u32 msr_index = msr_info->index;
3836 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003837
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003839 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003840 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003841 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003842#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003844 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 vmcs_writel(GUEST_FS_BASE, data);
3846 break;
3847 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003848 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849 vmcs_writel(GUEST_GS_BASE, data);
3850 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003851 case MSR_KERNEL_GS_BASE:
3852 vmx_load_host_state(vmx);
3853 vmx->msr_guest_kernel_gs_base = data;
3854 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855#endif
3856 case MSR_IA32_SYSENTER_CS:
3857 vmcs_write32(GUEST_SYSENTER_CS, data);
3858 break;
3859 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003860 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 break;
3862 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003863 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003865 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003866 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003867 (!msr_info->host_initiated &&
3868 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003869 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003870 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003871 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003873 vmcs_write64(GUEST_BNDCFGS, data);
3874 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003875 case MSR_IA32_SPEC_CTRL:
3876 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003877 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3878 return 1;
3879
3880 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003881 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003882 return 1;
3883
3884 vmx->spec_ctrl = data;
3885
3886 if (!data)
3887 break;
3888
3889 /*
3890 * For non-nested:
3891 * When it's written (to non-zero) for the first time, pass
3892 * it through.
3893 *
3894 * For nested:
3895 * The handling of the MSR bitmap for L2 guests is done in
3896 * nested_vmx_merge_msr_bitmap. We should not touch the
3897 * vmcs02.msr_bitmap here since it gets completely overwritten
3898 * in the merging. We update the vmcs01 here for L1 as well
3899 * since it will end up touching the MSR anyway now.
3900 */
3901 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3902 MSR_IA32_SPEC_CTRL,
3903 MSR_TYPE_RW);
3904 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003905 case MSR_IA32_PRED_CMD:
3906 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003907 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3908 return 1;
3909
3910 if (data & ~PRED_CMD_IBPB)
3911 return 1;
3912
3913 if (!data)
3914 break;
3915
3916 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3917
3918 /*
3919 * For non-nested:
3920 * When it's written (to non-zero) for the first time, pass
3921 * it through.
3922 *
3923 * For nested:
3924 * The handling of the MSR bitmap for L2 guests is done in
3925 * nested_vmx_merge_msr_bitmap. We should not touch the
3926 * vmcs02.msr_bitmap here since it gets completely overwritten
3927 * in the merging.
3928 */
3929 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3930 MSR_TYPE_W);
3931 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003932 case MSR_IA32_ARCH_CAPABILITIES:
3933 if (!msr_info->host_initiated)
3934 return 1;
3935 vmx->arch_capabilities = data;
3936 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003937 case MSR_IA32_CR_PAT:
3938 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003939 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3940 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003941 vmcs_write64(GUEST_IA32_PAT, data);
3942 vcpu->arch.pat = data;
3943 break;
3944 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003945 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003946 break;
Will Auldba904632012-11-29 12:42:50 -08003947 case MSR_IA32_TSC_ADJUST:
3948 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003949 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003950 case MSR_IA32_MCG_EXT_CTL:
3951 if ((!msr_info->host_initiated &&
3952 !(to_vmx(vcpu)->msr_ia32_feature_control &
3953 FEATURE_CONTROL_LMCE)) ||
3954 (data & ~MCG_EXT_CTL_LMCE_EN))
3955 return 1;
3956 vcpu->arch.mcg_ext_ctl = data;
3957 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003958 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003959 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003960 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003961 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3962 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003963 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003964 if (msr_info->host_initiated && data == 0)
3965 vmx_leave_nested(vcpu);
3966 break;
3967 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003968 if (!msr_info->host_initiated)
3969 return 1; /* they are read-only */
3970 if (!nested_vmx_allowed(vcpu))
3971 return 1;
3972 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003973 case MSR_IA32_XSS:
3974 if (!vmx_xsaves_supported())
3975 return 1;
3976 /*
3977 * The only supported bit as of Skylake is bit 8, but
3978 * it is not supported on KVM.
3979 */
3980 if (data != 0)
3981 return 1;
3982 vcpu->arch.ia32_xss = data;
3983 if (vcpu->arch.ia32_xss != host_xss)
3984 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3985 vcpu->arch.ia32_xss, host_xss);
3986 else
3987 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3988 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003989 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003990 if (!msr_info->host_initiated &&
3991 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003992 return 1;
3993 /* Check reserved bit, higher 32 bits should be zero */
3994 if ((data >> 32) != 0)
3995 return 1;
3996 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003998 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003999 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004000 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004001 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004002 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4003 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004004 ret = kvm_set_shared_msr(msr->index, msr->data,
4005 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004006 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004007 if (ret)
4008 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004009 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004010 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004012 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 }
4014
Eddie Dong2cc51562007-05-21 07:28:09 +03004015 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004016}
4017
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004018static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004019{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004020 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4021 switch (reg) {
4022 case VCPU_REGS_RSP:
4023 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4024 break;
4025 case VCPU_REGS_RIP:
4026 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4027 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004028 case VCPU_EXREG_PDPTR:
4029 if (enable_ept)
4030 ept_save_pdptrs(vcpu);
4031 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004032 default:
4033 break;
4034 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035}
4036
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037static __init int cpu_has_kvm_support(void)
4038{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004039 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040}
4041
4042static __init int vmx_disabled_by_bios(void)
4043{
4044 u64 msr;
4045
4046 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004047 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004048 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004049 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4050 && tboot_enabled())
4051 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004052 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004053 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004054 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004055 && !tboot_enabled()) {
4056 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004057 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004058 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004059 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004060 /* launched w/o TXT and VMX disabled */
4061 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4062 && !tboot_enabled())
4063 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004064 }
4065
4066 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067}
4068
Dongxiao Xu7725b892010-05-11 18:29:38 +08004069static void kvm_cpu_vmxon(u64 addr)
4070{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004071 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004072 intel_pt_handle_vmx(1);
4073
Dongxiao Xu7725b892010-05-11 18:29:38 +08004074 asm volatile (ASM_VMX_VMXON_RAX
4075 : : "a"(&addr), "m"(addr)
4076 : "memory", "cc");
4077}
4078
Radim Krčmář13a34e02014-08-28 15:13:03 +02004079static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004080{
4081 int cpu = raw_smp_processor_id();
4082 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004083 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004085 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004086 return -EBUSY;
4087
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004088 /*
4089 * This can happen if we hot-added a CPU but failed to allocate
4090 * VP assist page for it.
4091 */
4092 if (static_branch_unlikely(&enable_evmcs) &&
4093 !hv_get_vp_assist_page(cpu))
4094 return -EFAULT;
4095
Nadav Har'Eld462b812011-05-24 15:26:10 +03004096 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004097 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4098 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004099
4100 /*
4101 * Now we can enable the vmclear operation in kdump
4102 * since the loaded_vmcss_on_cpu list on this cpu
4103 * has been initialized.
4104 *
4105 * Though the cpu is not in VMX operation now, there
4106 * is no problem to enable the vmclear operation
4107 * for the loaded_vmcss_on_cpu list is empty!
4108 */
4109 crash_enable_local_vmclear(cpu);
4110
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004112
4113 test_bits = FEATURE_CONTROL_LOCKED;
4114 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4115 if (tboot_enabled())
4116 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4117
4118 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004120 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4121 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004122 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004123 if (enable_ept)
4124 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004125
4126 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127}
4128
Nadav Har'Eld462b812011-05-24 15:26:10 +03004129static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004130{
4131 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004132 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004133
Nadav Har'Eld462b812011-05-24 15:26:10 +03004134 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4135 loaded_vmcss_on_cpu_link)
4136 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004137}
4138
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004139
4140/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4141 * tricks.
4142 */
4143static void kvm_cpu_vmxoff(void)
4144{
4145 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004146
4147 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004148 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004149}
4150
Radim Krčmář13a34e02014-08-28 15:13:03 +02004151static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004153 vmclear_local_loaded_vmcss();
4154 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155}
4156
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004157static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004158 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159{
4160 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004161 u32 ctl = ctl_min | ctl_opt;
4162
4163 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4164
4165 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4166 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4167
4168 /* Ensure minimum (required) set of control bits are supported. */
4169 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004170 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004171
4172 *result = ctl;
4173 return 0;
4174}
4175
Avi Kivity110312c2010-12-21 12:54:20 +02004176static __init bool allow_1_setting(u32 msr, u32 ctl)
4177{
4178 u32 vmx_msr_low, vmx_msr_high;
4179
4180 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4181 return vmx_msr_high & ctl;
4182}
4183
Yang, Sheng002c7f72007-07-31 14:23:01 +03004184static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004185{
4186 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004187 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004188 u32 _pin_based_exec_control = 0;
4189 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004190 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004191 u32 _vmexit_control = 0;
4192 u32 _vmentry_control = 0;
4193
Paolo Bonzini13893092018-02-26 13:40:09 +01004194 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304195 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004196#ifdef CONFIG_X86_64
4197 CPU_BASED_CR8_LOAD_EXITING |
4198 CPU_BASED_CR8_STORE_EXITING |
4199#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004200 CPU_BASED_CR3_LOAD_EXITING |
4201 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004202 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004203 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004204 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004205 CPU_BASED_MWAIT_EXITING |
4206 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004207 CPU_BASED_INVLPG_EXITING |
4208 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004209
Sheng Yangf78e0e22007-10-29 09:40:42 +08004210 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004211 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004212 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004213 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4214 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004215 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004216#ifdef CONFIG_X86_64
4217 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4218 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4219 ~CPU_BASED_CR8_STORE_EXITING;
4220#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004221 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004222 min2 = 0;
4223 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004224 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004225 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004226 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004227 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004228 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004229 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004230 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004231 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004232 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004233 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004234 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004235 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004236 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004237 SECONDARY_EXEC_RDSEED_EXITING |
4238 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004239 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004240 SECONDARY_EXEC_TSC_SCALING |
4241 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004242 if (adjust_vmx_controls(min2, opt2,
4243 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004244 &_cpu_based_2nd_exec_control) < 0)
4245 return -EIO;
4246 }
4247#ifndef CONFIG_X86_64
4248 if (!(_cpu_based_2nd_exec_control &
4249 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4250 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4251#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004252
4253 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4254 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004255 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004256 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4257 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004258
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004259 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4260 &vmx_capability.ept, &vmx_capability.vpid);
4261
Sheng Yangd56f5462008-04-25 10:13:16 +08004262 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004263 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4264 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004265 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4266 CPU_BASED_CR3_STORE_EXITING |
4267 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004268 } else if (vmx_capability.ept) {
4269 vmx_capability.ept = 0;
4270 pr_warn_once("EPT CAP should not exist if not support "
4271 "1-setting enable EPT VM-execution control\n");
4272 }
4273 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4274 vmx_capability.vpid) {
4275 vmx_capability.vpid = 0;
4276 pr_warn_once("VPID CAP should not exist if not support "
4277 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004278 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004279
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004280 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004281#ifdef CONFIG_X86_64
4282 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4283#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004284 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004285 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004286 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4287 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004288 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004289
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004290 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4291 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4292 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004293 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4294 &_pin_based_exec_control) < 0)
4295 return -EIO;
4296
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004297 if (cpu_has_broken_vmx_preemption_timer())
4298 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004299 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004300 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004301 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4302
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004303 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004304 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004305 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4306 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004307 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004309 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004310
4311 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4312 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004313 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004314
4315#ifdef CONFIG_X86_64
4316 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4317 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004318 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004319#endif
4320
4321 /* Require Write-Back (WB) memory type for VMCS accesses. */
4322 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004323 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004324
Yang, Sheng002c7f72007-07-31 14:23:01 +03004325 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004326 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004327 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004328
Liran Alon2307af12018-06-29 22:59:04 +03004329 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004330
Yang, Sheng002c7f72007-07-31 14:23:01 +03004331 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4332 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004333 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 vmcs_conf->vmexit_ctrl = _vmexit_control;
4335 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004336
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004337 if (static_branch_unlikely(&enable_evmcs))
4338 evmcs_sanitize_exec_ctrls(vmcs_conf);
4339
Avi Kivity110312c2010-12-21 12:54:20 +02004340 cpu_has_load_ia32_efer =
4341 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4342 VM_ENTRY_LOAD_IA32_EFER)
4343 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4344 VM_EXIT_LOAD_IA32_EFER);
4345
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004346 cpu_has_load_perf_global_ctrl =
4347 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4348 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4349 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4350 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4351
4352 /*
4353 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004354 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004355 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4356 *
4357 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4358 *
4359 * AAK155 (model 26)
4360 * AAP115 (model 30)
4361 * AAT100 (model 37)
4362 * BC86,AAY89,BD102 (model 44)
4363 * BA97 (model 46)
4364 *
4365 */
4366 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4367 switch (boot_cpu_data.x86_model) {
4368 case 26:
4369 case 30:
4370 case 37:
4371 case 44:
4372 case 46:
4373 cpu_has_load_perf_global_ctrl = false;
4374 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4375 "does not work properly. Using workaround\n");
4376 break;
4377 default:
4378 break;
4379 }
4380 }
4381
Borislav Petkov782511b2016-04-04 22:25:03 +02004382 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004383 rdmsrl(MSR_IA32_XSS, host_xss);
4384
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004385 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004386}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387
4388static struct vmcs *alloc_vmcs_cpu(int cpu)
4389{
4390 int node = cpu_to_node(cpu);
4391 struct page *pages;
4392 struct vmcs *vmcs;
4393
Vlastimil Babka96db8002015-09-08 15:03:50 -07004394 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004395 if (!pages)
4396 return NULL;
4397 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004398 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004399
4400 /* KVM supports Enlightened VMCS v1 only */
4401 if (static_branch_unlikely(&enable_evmcs))
4402 vmcs->revision_id = KVM_EVMCS_VERSION;
4403 else
4404 vmcs->revision_id = vmcs_config.revision_id;
4405
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406 return vmcs;
4407}
4408
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409static void free_vmcs(struct vmcs *vmcs)
4410{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004411 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412}
4413
Nadav Har'Eld462b812011-05-24 15:26:10 +03004414/*
4415 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4416 */
4417static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4418{
4419 if (!loaded_vmcs->vmcs)
4420 return;
4421 loaded_vmcs_clear(loaded_vmcs);
4422 free_vmcs(loaded_vmcs->vmcs);
4423 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004424 if (loaded_vmcs->msr_bitmap)
4425 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004426 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004427}
4428
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004429static struct vmcs *alloc_vmcs(void)
4430{
4431 return alloc_vmcs_cpu(raw_smp_processor_id());
4432}
4433
4434static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4435{
4436 loaded_vmcs->vmcs = alloc_vmcs();
4437 if (!loaded_vmcs->vmcs)
4438 return -ENOMEM;
4439
4440 loaded_vmcs->shadow_vmcs = NULL;
4441 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004442
4443 if (cpu_has_vmx_msr_bitmap()) {
4444 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4445 if (!loaded_vmcs->msr_bitmap)
4446 goto out_vmcs;
4447 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004448
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004449 if (IS_ENABLED(CONFIG_HYPERV) &&
4450 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004451 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4452 struct hv_enlightened_vmcs *evmcs =
4453 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4454
4455 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4456 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004457 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004458 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004459
4460out_vmcs:
4461 free_loaded_vmcs(loaded_vmcs);
4462 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004463}
4464
Sam Ravnborg39959582007-06-01 00:47:13 -07004465static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466{
4467 int cpu;
4468
Zachary Amsden3230bb42009-09-29 11:38:37 -10004469 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004471 per_cpu(vmxarea, cpu) = NULL;
4472 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473}
4474
Jim Mattsond37f4262017-12-22 12:12:16 -08004475enum vmcs_field_width {
4476 VMCS_FIELD_WIDTH_U16 = 0,
4477 VMCS_FIELD_WIDTH_U64 = 1,
4478 VMCS_FIELD_WIDTH_U32 = 2,
4479 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004480};
4481
Jim Mattsond37f4262017-12-22 12:12:16 -08004482static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004483{
4484 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004485 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004486 return (field >> 13) & 0x3 ;
4487}
4488
4489static inline int vmcs_field_readonly(unsigned long field)
4490{
4491 return (((field >> 10) & 0x3) == 1);
4492}
4493
Bandan Dasfe2b2012014-04-21 15:20:14 -04004494static void init_vmcs_shadow_fields(void)
4495{
4496 int i, j;
4497
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004498 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4499 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004500 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004501 (i + 1 == max_shadow_read_only_fields ||
4502 shadow_read_only_fields[i + 1] != field + 1))
4503 pr_err("Missing field from shadow_read_only_field %x\n",
4504 field + 1);
4505
4506 clear_bit(field, vmx_vmread_bitmap);
4507#ifdef CONFIG_X86_64
4508 if (field & 1)
4509 continue;
4510#endif
4511 if (j < i)
4512 shadow_read_only_fields[j] = field;
4513 j++;
4514 }
4515 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004516
4517 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004518 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004519 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004520 (i + 1 == max_shadow_read_write_fields ||
4521 shadow_read_write_fields[i + 1] != field + 1))
4522 pr_err("Missing field from shadow_read_write_field %x\n",
4523 field + 1);
4524
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004525 /*
4526 * PML and the preemption timer can be emulated, but the
4527 * processor cannot vmwrite to fields that don't exist
4528 * on bare metal.
4529 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004530 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004531 case GUEST_PML_INDEX:
4532 if (!cpu_has_vmx_pml())
4533 continue;
4534 break;
4535 case VMX_PREEMPTION_TIMER_VALUE:
4536 if (!cpu_has_vmx_preemption_timer())
4537 continue;
4538 break;
4539 case GUEST_INTR_STATUS:
4540 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004541 continue;
4542 break;
4543 default:
4544 break;
4545 }
4546
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004547 clear_bit(field, vmx_vmwrite_bitmap);
4548 clear_bit(field, vmx_vmread_bitmap);
4549#ifdef CONFIG_X86_64
4550 if (field & 1)
4551 continue;
4552#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004553 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004554 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004555 j++;
4556 }
4557 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004558}
4559
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560static __init int alloc_kvm_area(void)
4561{
4562 int cpu;
4563
Zachary Amsden3230bb42009-09-29 11:38:37 -10004564 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565 struct vmcs *vmcs;
4566
4567 vmcs = alloc_vmcs_cpu(cpu);
4568 if (!vmcs) {
4569 free_kvm_area();
4570 return -ENOMEM;
4571 }
4572
Liran Alon2307af12018-06-29 22:59:04 +03004573 /*
4574 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4575 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4576 * revision_id reported by MSR_IA32_VMX_BASIC.
4577 *
4578 * However, even though not explictly documented by
4579 * TLFS, VMXArea passed as VMXON argument should
4580 * still be marked with revision_id reported by
4581 * physical CPU.
4582 */
4583 if (static_branch_unlikely(&enable_evmcs))
4584 vmcs->revision_id = vmcs_config.revision_id;
4585
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586 per_cpu(vmxarea, cpu) = vmcs;
4587 }
4588 return 0;
4589}
4590
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004591static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004592 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004593{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004594 if (!emulate_invalid_guest_state) {
4595 /*
4596 * CS and SS RPL should be equal during guest entry according
4597 * to VMX spec, but in reality it is not always so. Since vcpu
4598 * is in the middle of the transition from real mode to
4599 * protected mode it is safe to assume that RPL 0 is a good
4600 * default value.
4601 */
4602 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004603 save->selector &= ~SEGMENT_RPL_MASK;
4604 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004605 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004606 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004607 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004608}
4609
4610static void enter_pmode(struct kvm_vcpu *vcpu)
4611{
4612 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004613 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614
Gleb Natapovd99e4152012-12-20 16:57:45 +02004615 /*
4616 * Update real mode segment cache. It may be not up-to-date if sement
4617 * register was written while vcpu was in a guest mode.
4618 */
4619 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4620 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4621 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4622 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4623 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4624 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4625
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004626 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004627
Avi Kivity2fb92db2011-04-27 19:42:18 +03004628 vmx_segment_cache_clear(vmx);
4629
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004630 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004631
4632 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004633 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4634 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635 vmcs_writel(GUEST_RFLAGS, flags);
4636
Rusty Russell66aee912007-07-17 23:34:16 +10004637 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4638 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639
4640 update_exception_bitmap(vcpu);
4641
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004642 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4643 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4644 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4645 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4646 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4647 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004648}
4649
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004650static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004651{
Mathias Krause772e0312012-08-30 01:30:19 +02004652 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004653 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654
Gleb Natapovd99e4152012-12-20 16:57:45 +02004655 var.dpl = 0x3;
4656 if (seg == VCPU_SREG_CS)
4657 var.type = 0x3;
4658
4659 if (!emulate_invalid_guest_state) {
4660 var.selector = var.base >> 4;
4661 var.base = var.base & 0xffff0;
4662 var.limit = 0xffff;
4663 var.g = 0;
4664 var.db = 0;
4665 var.present = 1;
4666 var.s = 1;
4667 var.l = 0;
4668 var.unusable = 0;
4669 var.type = 0x3;
4670 var.avl = 0;
4671 if (save->base & 0xf)
4672 printk_once(KERN_WARNING "kvm: segment base is not "
4673 "paragraph aligned when entering "
4674 "protected mode (seg=%d)", seg);
4675 }
4676
4677 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004678 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004679 vmcs_write32(sf->limit, var.limit);
4680 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681}
4682
4683static void enter_rmode(struct kvm_vcpu *vcpu)
4684{
4685 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004686 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004687 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004688
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004689 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4690 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4691 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4692 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4693 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004694 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4695 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004696
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004697 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004698
Gleb Natapov776e58e2011-03-13 12:34:27 +02004699 /*
4700 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004701 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004702 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004703 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004704 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4705 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004706
Avi Kivity2fb92db2011-04-27 19:42:18 +03004707 vmx_segment_cache_clear(vmx);
4708
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004709 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4712
4713 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004714 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004716 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004717
4718 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004719 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720 update_exception_bitmap(vcpu);
4721
Gleb Natapovd99e4152012-12-20 16:57:45 +02004722 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4723 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4724 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4725 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4726 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4727 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004728
Eddie Dong8668a3c2007-10-10 14:26:45 +08004729 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730}
4731
Amit Shah401d10d2009-02-20 22:53:37 +05304732static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4733{
4734 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004735 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4736
4737 if (!msr)
4738 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304739
Avi Kivity44ea2b12009-09-06 15:55:37 +03004740 /*
4741 * Force kernel_gs_base reloading before EFER changes, as control
4742 * of this msr depends on is_long_mode().
4743 */
4744 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004745 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304746 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004747 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304748 msr->data = efer;
4749 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004750 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304751
4752 msr->data = efer & ~EFER_LME;
4753 }
4754 setup_msrs(vmx);
4755}
4756
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004757#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758
4759static void enter_lmode(struct kvm_vcpu *vcpu)
4760{
4761 u32 guest_tr_ar;
4762
Avi Kivity2fb92db2011-04-27 19:42:18 +03004763 vmx_segment_cache_clear(to_vmx(vcpu));
4764
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004766 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004767 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4768 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004770 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4771 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772 }
Avi Kivityda38f432010-07-06 11:30:49 +03004773 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774}
4775
4776static void exit_lmode(struct kvm_vcpu *vcpu)
4777{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004778 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004779 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780}
4781
4782#endif
4783
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004784static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4785 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004786{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004787 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004788 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4789 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004790 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004791 } else {
4792 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004793 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004794}
4795
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004796static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004797{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004798 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004799}
4800
Avi Kivitye8467fd2009-12-29 18:43:06 +02004801static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4802{
4803 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4804
4805 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4806 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4807}
4808
Avi Kivityaff48ba2010-12-05 18:56:11 +02004809static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4810{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004811 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004812 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4813 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4814}
4815
Anthony Liguori25c4c272007-04-27 09:29:21 +03004816static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004817{
Avi Kivityfc78f512009-12-07 12:16:48 +02004818 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4819
4820 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4821 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004822}
4823
Sheng Yang14394422008-04-28 12:24:45 +08004824static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4825{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004826 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4827
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004828 if (!test_bit(VCPU_EXREG_PDPTR,
4829 (unsigned long *)&vcpu->arch.regs_dirty))
4830 return;
4831
Sheng Yang14394422008-04-28 12:24:45 +08004832 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004833 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4834 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4835 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4836 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004837 }
4838}
4839
Avi Kivity8f5d5492009-05-31 18:41:29 +03004840static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4841{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004842 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4843
Avi Kivity8f5d5492009-05-31 18:41:29 +03004844 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004845 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4846 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4847 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4848 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004849 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004850
4851 __set_bit(VCPU_EXREG_PDPTR,
4852 (unsigned long *)&vcpu->arch.regs_avail);
4853 __set_bit(VCPU_EXREG_PDPTR,
4854 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004855}
4856
David Matlack38991522016-11-29 18:14:08 -08004857static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4858{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004859 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4860 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004861 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4862
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004863 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004864 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4865 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4866 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4867
4868 return fixed_bits_valid(val, fixed0, fixed1);
4869}
4870
4871static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4872{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004873 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4874 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004875
4876 return fixed_bits_valid(val, fixed0, fixed1);
4877}
4878
4879static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4880{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004881 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4882 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004883
4884 return fixed_bits_valid(val, fixed0, fixed1);
4885}
4886
4887/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4888#define nested_guest_cr4_valid nested_cr4_valid
4889#define nested_host_cr4_valid nested_cr4_valid
4890
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004891static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004892
4893static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4894 unsigned long cr0,
4895 struct kvm_vcpu *vcpu)
4896{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004897 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4898 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004899 if (!(cr0 & X86_CR0_PG)) {
4900 /* From paging/starting to nonpaging */
4901 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004902 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004903 (CPU_BASED_CR3_LOAD_EXITING |
4904 CPU_BASED_CR3_STORE_EXITING));
4905 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004906 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004907 } else if (!is_paging(vcpu)) {
4908 /* From nonpaging to paging */
4909 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004910 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004911 ~(CPU_BASED_CR3_LOAD_EXITING |
4912 CPU_BASED_CR3_STORE_EXITING));
4913 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004914 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004915 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004916
4917 if (!(cr0 & X86_CR0_WP))
4918 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004919}
4920
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4922{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004923 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004924 unsigned long hw_cr0;
4925
Gleb Natapov50378782013-02-04 16:00:28 +02004926 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004927 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004928 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004929 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004930 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004931
Gleb Natapov218e7632013-01-21 15:36:45 +02004932 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4933 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934
Gleb Natapov218e7632013-01-21 15:36:45 +02004935 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4936 enter_rmode(vcpu);
4937 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004938
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004939#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004940 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004941 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004942 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004943 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004944 exit_lmode(vcpu);
4945 }
4946#endif
4947
Sean Christophersonb4d18512018-03-05 12:04:40 -08004948 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004949 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4950
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004952 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004953 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004954
4955 /* depends on vcpu->arch.cr0 to be set to a new value */
4956 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957}
4958
Yu Zhang855feb62017-08-24 20:27:55 +08004959static int get_ept_level(struct kvm_vcpu *vcpu)
4960{
4961 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4962 return 5;
4963 return 4;
4964}
4965
Peter Feiner995f00a2017-06-30 17:26:32 -07004966static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004967{
Yu Zhang855feb62017-08-24 20:27:55 +08004968 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004969
Yu Zhang855feb62017-08-24 20:27:55 +08004970 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004971
Peter Feiner995f00a2017-06-30 17:26:32 -07004972 if (enable_ept_ad_bits &&
4973 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004974 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004975 eptp |= (root_hpa & PAGE_MASK);
4976
4977 return eptp;
4978}
4979
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4981{
Sheng Yang14394422008-04-28 12:24:45 +08004982 unsigned long guest_cr3;
4983 u64 eptp;
4984
4985 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004986 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004987 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004988 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004989 if (enable_unrestricted_guest || is_paging(vcpu) ||
4990 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004991 guest_cr3 = kvm_read_cr3(vcpu);
4992 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004993 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004994 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004995 }
4996
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004997 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004998 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004999}
5000
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005001static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005002{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005003 /*
5004 * Pass through host's Machine Check Enable value to hw_cr4, which
5005 * is in force while we are in guest mode. Do not let guests control
5006 * this bit, even if host CR4.MCE == 0.
5007 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005008 unsigned long hw_cr4;
5009
5010 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5011 if (enable_unrestricted_guest)
5012 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5013 else if (to_vmx(vcpu)->rmode.vm86_active)
5014 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5015 else
5016 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005017
Sean Christopherson64f7a112018-04-30 10:01:06 -07005018 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5019 if (cr4 & X86_CR4_UMIP) {
5020 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005021 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005022 hw_cr4 &= ~X86_CR4_UMIP;
5023 } else if (!is_guest_mode(vcpu) ||
5024 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5025 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5026 SECONDARY_EXEC_DESC);
5027 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005028
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005029 if (cr4 & X86_CR4_VMXE) {
5030 /*
5031 * To use VMXON (and later other VMX instructions), a guest
5032 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5033 * So basically the check on whether to allow nested VMX
5034 * is here.
5035 */
5036 if (!nested_vmx_allowed(vcpu))
5037 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005038 }
David Matlack38991522016-11-29 18:14:08 -08005039
5040 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005041 return 1;
5042
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005043 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005044
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005045 if (!enable_unrestricted_guest) {
5046 if (enable_ept) {
5047 if (!is_paging(vcpu)) {
5048 hw_cr4 &= ~X86_CR4_PAE;
5049 hw_cr4 |= X86_CR4_PSE;
5050 } else if (!(cr4 & X86_CR4_PAE)) {
5051 hw_cr4 &= ~X86_CR4_PAE;
5052 }
5053 }
5054
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005055 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005056 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5057 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5058 * to be manually disabled when guest switches to non-paging
5059 * mode.
5060 *
5061 * If !enable_unrestricted_guest, the CPU is always running
5062 * with CR0.PG=1 and CR4 needs to be modified.
5063 * If enable_unrestricted_guest, the CPU automatically
5064 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005065 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005066 if (!is_paging(vcpu))
5067 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5068 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005069
Sheng Yang14394422008-04-28 12:24:45 +08005070 vmcs_writel(CR4_READ_SHADOW, cr4);
5071 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005072 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073}
5074
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075static void vmx_get_segment(struct kvm_vcpu *vcpu,
5076 struct kvm_segment *var, int seg)
5077{
Avi Kivitya9179492011-01-03 14:28:52 +02005078 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 u32 ar;
5080
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005081 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005082 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005083 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005084 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005085 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005086 var->base = vmx_read_guest_seg_base(vmx, seg);
5087 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5088 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005089 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005090 var->base = vmx_read_guest_seg_base(vmx, seg);
5091 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5092 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5093 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005094 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 var->type = ar & 15;
5096 var->s = (ar >> 4) & 1;
5097 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005098 /*
5099 * Some userspaces do not preserve unusable property. Since usable
5100 * segment has to be present according to VMX spec we can use present
5101 * property to amend userspace bug by making unusable segment always
5102 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5103 * segment as unusable.
5104 */
5105 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106 var->avl = (ar >> 12) & 1;
5107 var->l = (ar >> 13) & 1;
5108 var->db = (ar >> 14) & 1;
5109 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005110}
5111
Avi Kivitya9179492011-01-03 14:28:52 +02005112static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5113{
Avi Kivitya9179492011-01-03 14:28:52 +02005114 struct kvm_segment s;
5115
5116 if (to_vmx(vcpu)->rmode.vm86_active) {
5117 vmx_get_segment(vcpu, &s, seg);
5118 return s.base;
5119 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005120 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005121}
5122
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005123static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005124{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005125 struct vcpu_vmx *vmx = to_vmx(vcpu);
5126
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005127 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005128 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005129 else {
5130 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005131 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005132 }
Avi Kivity69c73022011-03-07 15:26:44 +02005133}
5134
Avi Kivity653e3102007-05-07 10:55:37 +03005135static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137 u32 ar;
5138
Avi Kivityf0495f92012-06-07 17:06:10 +03005139 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 ar = 1 << 16;
5141 else {
5142 ar = var->type & 15;
5143 ar |= (var->s & 1) << 4;
5144 ar |= (var->dpl & 3) << 5;
5145 ar |= (var->present & 1) << 7;
5146 ar |= (var->avl & 1) << 12;
5147 ar |= (var->l & 1) << 13;
5148 ar |= (var->db & 1) << 14;
5149 ar |= (var->g & 1) << 15;
5150 }
Avi Kivity653e3102007-05-07 10:55:37 +03005151
5152 return ar;
5153}
5154
5155static void vmx_set_segment(struct kvm_vcpu *vcpu,
5156 struct kvm_segment *var, int seg)
5157{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005158 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005159 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005160
Avi Kivity2fb92db2011-04-27 19:42:18 +03005161 vmx_segment_cache_clear(vmx);
5162
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005163 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5164 vmx->rmode.segs[seg] = *var;
5165 if (seg == VCPU_SREG_TR)
5166 vmcs_write16(sf->selector, var->selector);
5167 else if (var->s)
5168 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005169 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005170 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005171
Avi Kivity653e3102007-05-07 10:55:37 +03005172 vmcs_writel(sf->base, var->base);
5173 vmcs_write32(sf->limit, var->limit);
5174 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005175
5176 /*
5177 * Fix the "Accessed" bit in AR field of segment registers for older
5178 * qemu binaries.
5179 * IA32 arch specifies that at the time of processor reset the
5180 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005181 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005182 * state vmexit when "unrestricted guest" mode is turned on.
5183 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5184 * tree. Newer qemu binaries with that qemu fix would not need this
5185 * kvm hack.
5186 */
5187 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005188 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005189
Gleb Natapovf924d662012-12-12 19:10:55 +02005190 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005191
5192out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005193 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005194}
5195
Avi Kivity6aa8b732006-12-10 02:21:36 -08005196static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5197{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005198 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199
5200 *db = (ar >> 14) & 1;
5201 *l = (ar >> 13) & 1;
5202}
5203
Gleb Natapov89a27f42010-02-16 10:51:48 +02005204static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005205{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005206 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5207 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208}
5209
Gleb Natapov89a27f42010-02-16 10:51:48 +02005210static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005211{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005212 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5213 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005214}
5215
Gleb Natapov89a27f42010-02-16 10:51:48 +02005216static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005218 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5219 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005220}
5221
Gleb Natapov89a27f42010-02-16 10:51:48 +02005222static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005223{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005224 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5225 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005226}
5227
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005228static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5229{
5230 struct kvm_segment var;
5231 u32 ar;
5232
5233 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005234 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005235 if (seg == VCPU_SREG_CS)
5236 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005237 ar = vmx_segment_access_rights(&var);
5238
5239 if (var.base != (var.selector << 4))
5240 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005241 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005242 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005243 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005244 return false;
5245
5246 return true;
5247}
5248
5249static bool code_segment_valid(struct kvm_vcpu *vcpu)
5250{
5251 struct kvm_segment cs;
5252 unsigned int cs_rpl;
5253
5254 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005255 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005256
Avi Kivity1872a3f2009-01-04 23:26:52 +02005257 if (cs.unusable)
5258 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005259 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005260 return false;
5261 if (!cs.s)
5262 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005263 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005264 if (cs.dpl > cs_rpl)
5265 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005266 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005267 if (cs.dpl != cs_rpl)
5268 return false;
5269 }
5270 if (!cs.present)
5271 return false;
5272
5273 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5274 return true;
5275}
5276
5277static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5278{
5279 struct kvm_segment ss;
5280 unsigned int ss_rpl;
5281
5282 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005283 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005284
Avi Kivity1872a3f2009-01-04 23:26:52 +02005285 if (ss.unusable)
5286 return true;
5287 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005288 return false;
5289 if (!ss.s)
5290 return false;
5291 if (ss.dpl != ss_rpl) /* DPL != RPL */
5292 return false;
5293 if (!ss.present)
5294 return false;
5295
5296 return true;
5297}
5298
5299static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5300{
5301 struct kvm_segment var;
5302 unsigned int rpl;
5303
5304 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005305 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005306
Avi Kivity1872a3f2009-01-04 23:26:52 +02005307 if (var.unusable)
5308 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005309 if (!var.s)
5310 return false;
5311 if (!var.present)
5312 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005313 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005314 if (var.dpl < rpl) /* DPL < RPL */
5315 return false;
5316 }
5317
5318 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5319 * rights flags
5320 */
5321 return true;
5322}
5323
5324static bool tr_valid(struct kvm_vcpu *vcpu)
5325{
5326 struct kvm_segment tr;
5327
5328 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5329
Avi Kivity1872a3f2009-01-04 23:26:52 +02005330 if (tr.unusable)
5331 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005332 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005333 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005334 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005335 return false;
5336 if (!tr.present)
5337 return false;
5338
5339 return true;
5340}
5341
5342static bool ldtr_valid(struct kvm_vcpu *vcpu)
5343{
5344 struct kvm_segment ldtr;
5345
5346 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5347
Avi Kivity1872a3f2009-01-04 23:26:52 +02005348 if (ldtr.unusable)
5349 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005350 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005351 return false;
5352 if (ldtr.type != 2)
5353 return false;
5354 if (!ldtr.present)
5355 return false;
5356
5357 return true;
5358}
5359
5360static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5361{
5362 struct kvm_segment cs, ss;
5363
5364 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5365 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5366
Nadav Amitb32a9912015-03-29 16:33:04 +03005367 return ((cs.selector & SEGMENT_RPL_MASK) ==
5368 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005369}
5370
5371/*
5372 * Check if guest state is valid. Returns true if valid, false if
5373 * not.
5374 * We assume that registers are always usable
5375 */
5376static bool guest_state_valid(struct kvm_vcpu *vcpu)
5377{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005378 if (enable_unrestricted_guest)
5379 return true;
5380
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005381 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005382 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005383 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5384 return false;
5385 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5386 return false;
5387 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5388 return false;
5389 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5390 return false;
5391 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5392 return false;
5393 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5394 return false;
5395 } else {
5396 /* protected mode guest state checks */
5397 if (!cs_ss_rpl_check(vcpu))
5398 return false;
5399 if (!code_segment_valid(vcpu))
5400 return false;
5401 if (!stack_segment_valid(vcpu))
5402 return false;
5403 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5404 return false;
5405 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5406 return false;
5407 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5408 return false;
5409 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5410 return false;
5411 if (!tr_valid(vcpu))
5412 return false;
5413 if (!ldtr_valid(vcpu))
5414 return false;
5415 }
5416 /* TODO:
5417 * - Add checks on RIP
5418 * - Add checks on RFLAGS
5419 */
5420
5421 return true;
5422}
5423
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005424static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5425{
5426 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5427}
5428
Mike Dayd77c26f2007-10-08 09:02:08 -04005429static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005430{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005431 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005432 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005433 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005434
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005435 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005436 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005437 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5438 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005439 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005440 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005441 r = kvm_write_guest_page(kvm, fn++, &data,
5442 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005443 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005444 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005445 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5446 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005447 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005448 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5449 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005450 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005451 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005452 r = kvm_write_guest_page(kvm, fn, &data,
5453 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5454 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005455out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005456 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005457 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005458}
5459
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005460static int init_rmode_identity_map(struct kvm *kvm)
5461{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005462 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005463 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005464 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005465 u32 tmp;
5466
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005467 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005468 mutex_lock(&kvm->slots_lock);
5469
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005470 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005471 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005472
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005473 if (!kvm_vmx->ept_identity_map_addr)
5474 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5475 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005476
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005477 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005478 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005479 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005480 goto out2;
5481
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005482 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005483 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5484 if (r < 0)
5485 goto out;
5486 /* Set up identity-mapping pagetable for EPT in real mode */
5487 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5488 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5489 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5490 r = kvm_write_guest_page(kvm, identity_map_pfn,
5491 &tmp, i * sizeof(tmp), sizeof(tmp));
5492 if (r < 0)
5493 goto out;
5494 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005495 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005496
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005497out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005498 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005499
5500out2:
5501 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005502 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005503}
5504
Avi Kivity6aa8b732006-12-10 02:21:36 -08005505static void seg_setup(int seg)
5506{
Mathias Krause772e0312012-08-30 01:30:19 +02005507 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005508 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005509
5510 vmcs_write16(sf->selector, 0);
5511 vmcs_writel(sf->base, 0);
5512 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005513 ar = 0x93;
5514 if (seg == VCPU_SREG_CS)
5515 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005516
5517 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005518}
5519
Sheng Yangf78e0e22007-10-29 09:40:42 +08005520static int alloc_apic_access_page(struct kvm *kvm)
5521{
Xiao Guangrong44841412012-09-07 14:14:20 +08005522 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005523 int r = 0;
5524
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005525 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005526 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005527 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005528 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5529 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005530 if (r)
5531 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005532
Tang Chen73a6d942014-09-11 13:38:00 +08005533 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005534 if (is_error_page(page)) {
5535 r = -EFAULT;
5536 goto out;
5537 }
5538
Tang Chenc24ae0d2014-09-24 15:57:58 +08005539 /*
5540 * Do not pin the page in memory, so that memory hot-unplug
5541 * is able to migrate it.
5542 */
5543 put_page(page);
5544 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005545out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005546 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005547 return r;
5548}
5549
Wanpeng Li991e7a02015-09-16 17:30:05 +08005550static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005551{
5552 int vpid;
5553
Avi Kivity919818a2009-03-23 18:01:29 +02005554 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005555 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005556 spin_lock(&vmx_vpid_lock);
5557 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005558 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005559 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005560 else
5561 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005562 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005563 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005564}
5565
Wanpeng Li991e7a02015-09-16 17:30:05 +08005566static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005567{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005568 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005569 return;
5570 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005571 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005572 spin_unlock(&vmx_vpid_lock);
5573}
5574
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005575static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5576 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005577{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005578 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005579
5580 if (!cpu_has_vmx_msr_bitmap())
5581 return;
5582
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005583 if (static_branch_unlikely(&enable_evmcs))
5584 evmcs_touch_msr_bitmap();
5585
Sheng Yang25c5f222008-03-28 13:18:56 +08005586 /*
5587 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5588 * have the write-low and read-high bitmap offsets the wrong way round.
5589 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5590 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005591 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005592 if (type & MSR_TYPE_R)
5593 /* read-low */
5594 __clear_bit(msr, msr_bitmap + 0x000 / f);
5595
5596 if (type & MSR_TYPE_W)
5597 /* write-low */
5598 __clear_bit(msr, msr_bitmap + 0x800 / f);
5599
Sheng Yang25c5f222008-03-28 13:18:56 +08005600 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5601 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005602 if (type & MSR_TYPE_R)
5603 /* read-high */
5604 __clear_bit(msr, msr_bitmap + 0x400 / f);
5605
5606 if (type & MSR_TYPE_W)
5607 /* write-high */
5608 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5609
5610 }
5611}
5612
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005613static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5614 u32 msr, int type)
5615{
5616 int f = sizeof(unsigned long);
5617
5618 if (!cpu_has_vmx_msr_bitmap())
5619 return;
5620
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005621 if (static_branch_unlikely(&enable_evmcs))
5622 evmcs_touch_msr_bitmap();
5623
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005624 /*
5625 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5626 * have the write-low and read-high bitmap offsets the wrong way round.
5627 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5628 */
5629 if (msr <= 0x1fff) {
5630 if (type & MSR_TYPE_R)
5631 /* read-low */
5632 __set_bit(msr, msr_bitmap + 0x000 / f);
5633
5634 if (type & MSR_TYPE_W)
5635 /* write-low */
5636 __set_bit(msr, msr_bitmap + 0x800 / f);
5637
5638 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5639 msr &= 0x1fff;
5640 if (type & MSR_TYPE_R)
5641 /* read-high */
5642 __set_bit(msr, msr_bitmap + 0x400 / f);
5643
5644 if (type & MSR_TYPE_W)
5645 /* write-high */
5646 __set_bit(msr, msr_bitmap + 0xc00 / f);
5647
5648 }
5649}
5650
5651static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5652 u32 msr, int type, bool value)
5653{
5654 if (value)
5655 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5656 else
5657 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5658}
5659
Wincy Vanf2b93282015-02-03 23:56:03 +08005660/*
5661 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5662 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5663 */
5664static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5665 unsigned long *msr_bitmap_nested,
5666 u32 msr, int type)
5667{
5668 int f = sizeof(unsigned long);
5669
Wincy Vanf2b93282015-02-03 23:56:03 +08005670 /*
5671 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5672 * have the write-low and read-high bitmap offsets the wrong way round.
5673 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5674 */
5675 if (msr <= 0x1fff) {
5676 if (type & MSR_TYPE_R &&
5677 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5678 /* read-low */
5679 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5680
5681 if (type & MSR_TYPE_W &&
5682 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5683 /* write-low */
5684 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5685
5686 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5687 msr &= 0x1fff;
5688 if (type & MSR_TYPE_R &&
5689 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5690 /* read-high */
5691 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5692
5693 if (type & MSR_TYPE_W &&
5694 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5695 /* write-high */
5696 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5697
5698 }
5699}
5700
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005701static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005702{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005703 u8 mode = 0;
5704
5705 if (cpu_has_secondary_exec_ctrls() &&
5706 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5707 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5708 mode |= MSR_BITMAP_MODE_X2APIC;
5709 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5710 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5711 }
5712
5713 if (is_long_mode(vcpu))
5714 mode |= MSR_BITMAP_MODE_LM;
5715
5716 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005717}
5718
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005719#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5720
5721static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5722 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005723{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005724 int msr;
5725
5726 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5727 unsigned word = msr / BITS_PER_LONG;
5728 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5729 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005730 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005731
5732 if (mode & MSR_BITMAP_MODE_X2APIC) {
5733 /*
5734 * TPR reads and writes can be virtualized even if virtual interrupt
5735 * delivery is not in use.
5736 */
5737 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5738 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5739 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5740 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5741 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5742 }
5743 }
5744}
5745
5746static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5747{
5748 struct vcpu_vmx *vmx = to_vmx(vcpu);
5749 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5750 u8 mode = vmx_msr_bitmap_mode(vcpu);
5751 u8 changed = mode ^ vmx->msr_bitmap_mode;
5752
5753 if (!changed)
5754 return;
5755
5756 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5757 !(mode & MSR_BITMAP_MODE_LM));
5758
5759 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5760 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5761
5762 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005763}
5764
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005765static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005766{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005767 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005768}
5769
David Matlackc9f04402017-08-01 14:00:40 -07005770static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5771{
5772 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5773 gfn_t gfn;
5774
5775 /*
5776 * Don't need to mark the APIC access page dirty; it is never
5777 * written to by the CPU during APIC virtualization.
5778 */
5779
5780 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5781 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5782 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5783 }
5784
5785 if (nested_cpu_has_posted_intr(vmcs12)) {
5786 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5787 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5788 }
5789}
5790
5791
David Hildenbrand6342c502017-01-25 11:58:58 +01005792static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005793{
5794 struct vcpu_vmx *vmx = to_vmx(vcpu);
5795 int max_irr;
5796 void *vapic_page;
5797 u16 status;
5798
David Matlackc9f04402017-08-01 14:00:40 -07005799 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5800 return;
Wincy Van705699a2015-02-03 23:58:17 +08005801
David Matlackc9f04402017-08-01 14:00:40 -07005802 vmx->nested.pi_pending = false;
5803 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5804 return;
Wincy Van705699a2015-02-03 23:58:17 +08005805
David Matlackc9f04402017-08-01 14:00:40 -07005806 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5807 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005808 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005809 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5810 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005811 kunmap(vmx->nested.virtual_apic_page);
5812
5813 status = vmcs_read16(GUEST_INTR_STATUS);
5814 if ((u8)max_irr > ((u8)status & 0xff)) {
5815 status &= ~0xff;
5816 status |= (u8)max_irr;
5817 vmcs_write16(GUEST_INTR_STATUS, status);
5818 }
5819 }
David Matlackc9f04402017-08-01 14:00:40 -07005820
5821 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005822}
5823
Wincy Van06a55242017-04-28 13:13:59 +08005824static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5825 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005826{
5827#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005828 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5829
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005830 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005831 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005832 * The vector of interrupt to be delivered to vcpu had
5833 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005834 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005835 * Following cases will be reached in this block, and
5836 * we always send a notification event in all cases as
5837 * explained below.
5838 *
5839 * Case 1: vcpu keeps in non-root mode. Sending a
5840 * notification event posts the interrupt to vcpu.
5841 *
5842 * Case 2: vcpu exits to root mode and is still
5843 * runnable. PIR will be synced to vIRR before the
5844 * next vcpu entry. Sending a notification event in
5845 * this case has no effect, as vcpu is not in root
5846 * mode.
5847 *
5848 * Case 3: vcpu exits to root mode and is blocked.
5849 * vcpu_block() has already synced PIR to vIRR and
5850 * never blocks vcpu if vIRR is not cleared. Therefore,
5851 * a blocked vcpu here does not wait for any requested
5852 * interrupts in PIR, and sending a notification event
5853 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005854 */
Feng Wu28b835d2015-09-18 22:29:54 +08005855
Wincy Van06a55242017-04-28 13:13:59 +08005856 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005857 return true;
5858 }
5859#endif
5860 return false;
5861}
5862
Wincy Van705699a2015-02-03 23:58:17 +08005863static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5864 int vector)
5865{
5866 struct vcpu_vmx *vmx = to_vmx(vcpu);
5867
5868 if (is_guest_mode(vcpu) &&
5869 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005870 /*
5871 * If a posted intr is not recognized by hardware,
5872 * we will accomplish it in the next vmentry.
5873 */
5874 vmx->nested.pi_pending = true;
5875 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005876 /* the PIR and ON have been set by L1. */
5877 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5878 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005879 return 0;
5880 }
5881 return -1;
5882}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005883/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005884 * Send interrupt to vcpu via posted interrupt way.
5885 * 1. If target vcpu is running(non-root mode), send posted interrupt
5886 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5887 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5888 * interrupt from PIR in next vmentry.
5889 */
5890static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5891{
5892 struct vcpu_vmx *vmx = to_vmx(vcpu);
5893 int r;
5894
Wincy Van705699a2015-02-03 23:58:17 +08005895 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5896 if (!r)
5897 return;
5898
Yang Zhanga20ed542013-04-11 19:25:15 +08005899 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5900 return;
5901
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005902 /* If a previous notification has sent the IPI, nothing to do. */
5903 if (pi_test_and_set_on(&vmx->pi_desc))
5904 return;
5905
Wincy Van06a55242017-04-28 13:13:59 +08005906 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005907 kvm_vcpu_kick(vcpu);
5908}
5909
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005911 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5912 * will not change in the lifetime of the guest.
5913 * Note that host-state that does change is set elsewhere. E.g., host-state
5914 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5915 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005916static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005917{
5918 u32 low32, high32;
5919 unsigned long tmpl;
5920 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005921 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005922
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005923 cr0 = read_cr0();
5924 WARN_ON(cr0 & X86_CR0_TS);
5925 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005926
5927 /*
5928 * Save the most likely value for this task's CR3 in the VMCS.
5929 * We can't use __get_current_cr3_fast() because we're not atomic.
5930 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005931 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005932 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005933 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005934
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005935 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005936 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005937 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005938 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005939
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005940 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005941#ifdef CONFIG_X86_64
5942 /*
5943 * Load null selectors, so we can avoid reloading them in
5944 * __vmx_load_host_state(), in case userspace uses the null selectors
5945 * too (the expected case).
5946 */
5947 vmcs_write16(HOST_DS_SELECTOR, 0);
5948 vmcs_write16(HOST_ES_SELECTOR, 0);
5949#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005950 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5951 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005952#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005953 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5954 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5955
Juergen Gross87930012017-09-04 12:25:27 +02005956 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005957 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005958 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005959
Avi Kivity83287ea422012-09-16 15:10:57 +03005960 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005961
5962 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5963 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5964 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5965 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5966
5967 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5968 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5969 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5970 }
5971}
5972
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005973static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5974{
5975 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5976 if (enable_ept)
5977 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005978 if (is_guest_mode(&vmx->vcpu))
5979 vmx->vcpu.arch.cr4_guest_owned_bits &=
5980 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005981 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5982}
5983
Yang Zhang01e439b2013-04-11 19:25:12 +08005984static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5985{
5986 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5987
Andrey Smetanind62caab2015-11-10 15:36:33 +03005988 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005989 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005990
5991 if (!enable_vnmi)
5992 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5993
Yunhong Jiang64672c92016-06-13 14:19:59 -07005994 /* Enable the preemption timer dynamically */
5995 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005996 return pin_based_exec_ctrl;
5997}
5998
Andrey Smetanind62caab2015-11-10 15:36:33 +03005999static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6000{
6001 struct vcpu_vmx *vmx = to_vmx(vcpu);
6002
6003 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006004 if (cpu_has_secondary_exec_ctrls()) {
6005 if (kvm_vcpu_apicv_active(vcpu))
6006 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6007 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6008 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6009 else
6010 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6011 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6012 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6013 }
6014
6015 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006016 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006017}
6018
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006019static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6020{
6021 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006022
6023 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6024 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6025
Paolo Bonzini35754c92015-07-29 12:05:37 +02006026 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006027 exec_control &= ~CPU_BASED_TPR_SHADOW;
6028#ifdef CONFIG_X86_64
6029 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6030 CPU_BASED_CR8_LOAD_EXITING;
6031#endif
6032 }
6033 if (!enable_ept)
6034 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6035 CPU_BASED_CR3_LOAD_EXITING |
6036 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006037 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6038 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6039 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006040 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6041 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006042 return exec_control;
6043}
6044
Jim Mattson45ec3682017-08-23 16:32:04 -07006045static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006046{
Jim Mattson45ec3682017-08-23 16:32:04 -07006047 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006048 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006049}
6050
Jim Mattson75f4fc82017-08-23 16:32:03 -07006051static bool vmx_rdseed_supported(void)
6052{
6053 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006054 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006055}
6056
Paolo Bonzini80154d72017-08-24 13:55:35 +02006057static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006058{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006059 struct kvm_vcpu *vcpu = &vmx->vcpu;
6060
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006061 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006062
Paolo Bonzini80154d72017-08-24 13:55:35 +02006063 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006064 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6065 if (vmx->vpid == 0)
6066 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6067 if (!enable_ept) {
6068 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6069 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006070 /* Enable INVPCID for non-ept guests may cause performance regression. */
6071 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006072 }
6073 if (!enable_unrestricted_guest)
6074 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006075 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006076 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006077 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006078 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6079 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006080 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006081
6082 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6083 * in vmx_set_cr4. */
6084 exec_control &= ~SECONDARY_EXEC_DESC;
6085
Abel Gordonabc4fc52013-04-18 14:35:25 +03006086 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6087 (handle_vmptrld).
6088 We can NOT enable shadow_vmcs here because we don't have yet
6089 a current VMCS12
6090 */
6091 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006092
6093 if (!enable_pml)
6094 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006095
Paolo Bonzini3db13482017-08-24 14:48:03 +02006096 if (vmx_xsaves_supported()) {
6097 /* Exposing XSAVES only when XSAVE is exposed */
6098 bool xsaves_enabled =
6099 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6100 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6101
6102 if (!xsaves_enabled)
6103 exec_control &= ~SECONDARY_EXEC_XSAVES;
6104
6105 if (nested) {
6106 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006107 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006108 SECONDARY_EXEC_XSAVES;
6109 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006110 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006111 ~SECONDARY_EXEC_XSAVES;
6112 }
6113 }
6114
Paolo Bonzini80154d72017-08-24 13:55:35 +02006115 if (vmx_rdtscp_supported()) {
6116 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6117 if (!rdtscp_enabled)
6118 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6119
6120 if (nested) {
6121 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006122 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006123 SECONDARY_EXEC_RDTSCP;
6124 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006125 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006126 ~SECONDARY_EXEC_RDTSCP;
6127 }
6128 }
6129
6130 if (vmx_invpcid_supported()) {
6131 /* Exposing INVPCID only when PCID is exposed */
6132 bool invpcid_enabled =
6133 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6134 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6135
6136 if (!invpcid_enabled) {
6137 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6138 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6139 }
6140
6141 if (nested) {
6142 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006143 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006144 SECONDARY_EXEC_ENABLE_INVPCID;
6145 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006146 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006147 ~SECONDARY_EXEC_ENABLE_INVPCID;
6148 }
6149 }
6150
Jim Mattson45ec3682017-08-23 16:32:04 -07006151 if (vmx_rdrand_supported()) {
6152 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6153 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006154 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006155
6156 if (nested) {
6157 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006158 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006159 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006160 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006161 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006162 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006163 }
6164 }
6165
Jim Mattson75f4fc82017-08-23 16:32:03 -07006166 if (vmx_rdseed_supported()) {
6167 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6168 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006169 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006170
6171 if (nested) {
6172 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006173 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006174 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006175 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006176 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006177 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006178 }
6179 }
6180
Paolo Bonzini80154d72017-08-24 13:55:35 +02006181 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006182}
6183
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006184static void ept_set_mmio_spte_mask(void)
6185{
6186 /*
6187 * EPT Misconfigurations can be generated if the value of bits 2:0
6188 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006189 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006190 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6191 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006192}
6193
Wanpeng Lif53cd632014-12-02 19:14:58 +08006194#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006195/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006196 * Sets up the vmcs for emulated real mode.
6197 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006198static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006199{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006200#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006201 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006202#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006203 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006204
Abel Gordon4607c2d2013-04-18 14:35:55 +03006205 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006206 /*
6207 * At vCPU creation, "VMWRITE to any supported field
6208 * in the VMCS" is supported, so use the more
6209 * permissive vmx_vmread_bitmap to specify both read
6210 * and write permissions for the shadow VMCS.
6211 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006212 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006213 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006214 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006215 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006216 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006217
Avi Kivity6aa8b732006-12-10 02:21:36 -08006218 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6219
Avi Kivity6aa8b732006-12-10 02:21:36 -08006220 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006221 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006222 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006223
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006224 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006225
Dan Williamsdfa169b2016-06-02 11:17:24 -07006226 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006227 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006228 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006229 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006230 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006231
Andrey Smetanind62caab2015-11-10 15:36:33 +03006232 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006233 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6234 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6235 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6236 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6237
6238 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006239
Li RongQing0bcf2612015-12-03 13:29:34 +08006240 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006241 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006242 }
6243
Wanpeng Lib31c1142018-03-12 04:53:04 -07006244 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006245 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006246 vmx->ple_window = ple_window;
6247 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006248 }
6249
Xiao Guangrongc3707952011-07-12 03:28:04 +08006250 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6251 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6253
Avi Kivity9581d442010-10-19 16:46:55 +02006254 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6255 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006256 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006257#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006258 rdmsrl(MSR_FS_BASE, a);
6259 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6260 rdmsrl(MSR_GS_BASE, a);
6261 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6262#else
6263 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6264 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6265#endif
6266
Bandan Das2a499e42017-08-03 15:54:41 -04006267 if (cpu_has_vmx_vmfunc())
6268 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6269
Eddie Dong2cc51562007-05-21 07:28:09 +03006270 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6271 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006272 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006273 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006274 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006275
Radim Krčmář74545702015-04-27 15:11:25 +02006276 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6277 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006278
Paolo Bonzini03916db2014-07-24 14:21:57 +02006279 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006280 u32 index = vmx_msr_index[i];
6281 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006282 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006283
6284 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6285 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006286 if (wrmsr_safe(index, data_low, data_high) < 0)
6287 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006288 vmx->guest_msrs[j].index = i;
6289 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006290 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006291 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006292 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006293
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006294 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6295 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006296
6297 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006298
6299 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006300 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006301
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006302 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6303 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6304
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006305 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006306
Wanpeng Lif53cd632014-12-02 19:14:58 +08006307 if (vmx_xsaves_supported())
6308 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6309
Peter Feiner4e595162016-07-07 14:49:58 -07006310 if (enable_pml) {
6311 ASSERT(vmx->pml_pg);
6312 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6313 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6314 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006315}
6316
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006317static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006318{
6319 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006320 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006321 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006322
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006323 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006324 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006325
Wanpeng Li518e7b92018-02-28 14:03:31 +08006326 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006327 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006328 kvm_set_cr8(vcpu, 0);
6329
6330 if (!init_event) {
6331 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6332 MSR_IA32_APICBASE_ENABLE;
6333 if (kvm_vcpu_is_reset_bsp(vcpu))
6334 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6335 apic_base_msr.host_initiated = true;
6336 kvm_set_apic_base(vcpu, &apic_base_msr);
6337 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006338
Avi Kivity2fb92db2011-04-27 19:42:18 +03006339 vmx_segment_cache_clear(vmx);
6340
Avi Kivity5706be02008-08-20 15:07:31 +03006341 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006342 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006343 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006344
6345 seg_setup(VCPU_SREG_DS);
6346 seg_setup(VCPU_SREG_ES);
6347 seg_setup(VCPU_SREG_FS);
6348 seg_setup(VCPU_SREG_GS);
6349 seg_setup(VCPU_SREG_SS);
6350
6351 vmcs_write16(GUEST_TR_SELECTOR, 0);
6352 vmcs_writel(GUEST_TR_BASE, 0);
6353 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6354 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6355
6356 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6357 vmcs_writel(GUEST_LDTR_BASE, 0);
6358 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6359 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6360
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006361 if (!init_event) {
6362 vmcs_write32(GUEST_SYSENTER_CS, 0);
6363 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6364 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6365 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6366 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006367
Wanpeng Lic37c2872017-11-20 14:52:21 -08006368 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006369 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006370
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006371 vmcs_writel(GUEST_GDTR_BASE, 0);
6372 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6373
6374 vmcs_writel(GUEST_IDTR_BASE, 0);
6375 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6376
Anthony Liguori443381a2010-12-06 10:53:38 -06006377 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006378 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006379 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006380 if (kvm_mpx_supported())
6381 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006382
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006383 setup_msrs(vmx);
6384
Avi Kivity6aa8b732006-12-10 02:21:36 -08006385 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6386
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006387 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006388 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006389 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006390 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006391 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006392 vmcs_write32(TPR_THRESHOLD, 0);
6393 }
6394
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006395 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006396
Sheng Yang2384d2b2008-01-17 15:14:33 +08006397 if (vmx->vpid != 0)
6398 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6399
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006400 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006401 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006402 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006403 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006404 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006405
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006406 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006407
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006408 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006409 if (init_event)
6410 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006411}
6412
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006413/*
6414 * In nested virtualization, check if L1 asked to exit on external interrupts.
6415 * For most existing hypervisors, this will always return true.
6416 */
6417static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6418{
6419 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6420 PIN_BASED_EXT_INTR_MASK;
6421}
6422
Bandan Das77b0f5d2014-04-19 18:17:45 -04006423/*
6424 * In nested virtualization, check if L1 has set
6425 * VM_EXIT_ACK_INTR_ON_EXIT
6426 */
6427static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6428{
6429 return get_vmcs12(vcpu)->vm_exit_controls &
6430 VM_EXIT_ACK_INTR_ON_EXIT;
6431}
6432
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006433static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6434{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006435 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006436}
6437
Jan Kiszkac9a79532014-03-07 20:03:15 +01006438static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006439{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006440 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6441 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006442}
6443
Jan Kiszkac9a79532014-03-07 20:03:15 +01006444static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006445{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006446 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006447 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006448 enable_irq_window(vcpu);
6449 return;
6450 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006451
Paolo Bonzini47c01522016-12-19 11:44:07 +01006452 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6453 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006454}
6455
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006456static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006457{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006458 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006459 uint32_t intr;
6460 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006461
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006462 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006463
Avi Kivityfa89a812008-09-01 15:57:51 +03006464 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006465 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006466 int inc_eip = 0;
6467 if (vcpu->arch.interrupt.soft)
6468 inc_eip = vcpu->arch.event_exit_inst_len;
6469 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006470 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006471 return;
6472 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006473 intr = irq | INTR_INFO_VALID_MASK;
6474 if (vcpu->arch.interrupt.soft) {
6475 intr |= INTR_TYPE_SOFT_INTR;
6476 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6477 vmx->vcpu.arch.event_exit_inst_len);
6478 } else
6479 intr |= INTR_TYPE_EXT_INTR;
6480 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006481
6482 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006483}
6484
Sheng Yangf08864b2008-05-15 18:23:25 +08006485static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6486{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006487 struct vcpu_vmx *vmx = to_vmx(vcpu);
6488
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006489 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006490 /*
6491 * Tracking the NMI-blocked state in software is built upon
6492 * finding the next open IRQ window. This, in turn, depends on
6493 * well-behaving guests: They have to keep IRQs disabled at
6494 * least as long as the NMI handler runs. Otherwise we may
6495 * cause NMI nesting, maybe breaking the guest. But as this is
6496 * highly unlikely, we can live with the residual risk.
6497 */
6498 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6499 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6500 }
6501
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006502 ++vcpu->stat.nmi_injections;
6503 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006504
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006505 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006506 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006507 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006508 return;
6509 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006510
Sheng Yangf08864b2008-05-15 18:23:25 +08006511 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6512 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006513
6514 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006515}
6516
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006517static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6518{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006519 struct vcpu_vmx *vmx = to_vmx(vcpu);
6520 bool masked;
6521
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006522 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006523 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006524 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006525 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006526 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6527 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6528 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006529}
6530
6531static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6532{
6533 struct vcpu_vmx *vmx = to_vmx(vcpu);
6534
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006535 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006536 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6537 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6538 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6539 }
6540 } else {
6541 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6542 if (masked)
6543 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6544 GUEST_INTR_STATE_NMI);
6545 else
6546 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6547 GUEST_INTR_STATE_NMI);
6548 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006549}
6550
Jan Kiszka2505dc92013-04-14 12:12:47 +02006551static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6552{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006553 if (to_vmx(vcpu)->nested.nested_run_pending)
6554 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006555
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006556 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006557 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6558 return 0;
6559
Jan Kiszka2505dc92013-04-14 12:12:47 +02006560 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6561 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6562 | GUEST_INTR_STATE_NMI));
6563}
6564
Gleb Natapov78646122009-03-23 12:12:11 +02006565static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6566{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006567 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6568 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006569 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6570 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006571}
6572
Izik Eiduscbc94022007-10-25 00:29:55 +02006573static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6574{
6575 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006576
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006577 if (enable_unrestricted_guest)
6578 return 0;
6579
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006580 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6581 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006582 if (ret)
6583 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006584 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006585 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006586}
6587
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006588static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6589{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006590 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006591 return 0;
6592}
6593
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006594static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006595{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006596 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006597 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006598 /*
6599 * Update instruction length as we may reinject the exception
6600 * from user space while in guest debugging mode.
6601 */
6602 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6603 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006604 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006605 return false;
6606 /* fall through */
6607 case DB_VECTOR:
6608 if (vcpu->guest_debug &
6609 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6610 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006611 /* fall through */
6612 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006613 case OF_VECTOR:
6614 case BR_VECTOR:
6615 case UD_VECTOR:
6616 case DF_VECTOR:
6617 case SS_VECTOR:
6618 case GP_VECTOR:
6619 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006620 return true;
6621 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006622 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006623 return false;
6624}
6625
6626static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6627 int vec, u32 err_code)
6628{
6629 /*
6630 * Instruction with address size override prefix opcode 0x67
6631 * Cause the #SS fault with 0 error code in VM86 mode.
6632 */
6633 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6634 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6635 if (vcpu->arch.halt_request) {
6636 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006637 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006638 }
6639 return 1;
6640 }
6641 return 0;
6642 }
6643
6644 /*
6645 * Forward all other exceptions that are valid in real mode.
6646 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6647 * the required debugging infrastructure rework.
6648 */
6649 kvm_queue_exception(vcpu, vec);
6650 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006651}
6652
Andi Kleena0861c02009-06-08 17:37:09 +08006653/*
6654 * Trigger machine check on the host. We assume all the MSRs are already set up
6655 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6656 * We pass a fake environment to the machine check handler because we want
6657 * the guest to be always treated like user space, no matter what context
6658 * it used internally.
6659 */
6660static void kvm_machine_check(void)
6661{
6662#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6663 struct pt_regs regs = {
6664 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6665 .flags = X86_EFLAGS_IF,
6666 };
6667
6668 do_machine_check(&regs, 0);
6669#endif
6670}
6671
Avi Kivity851ba692009-08-24 11:10:17 +03006672static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006673{
6674 /* already handled by vcpu_run */
6675 return 1;
6676}
6677
Avi Kivity851ba692009-08-24 11:10:17 +03006678static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006679{
Avi Kivity1155f762007-11-22 11:30:47 +02006680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006681 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006682 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006683 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006684 u32 vect_info;
6685 enum emulation_result er;
6686
Avi Kivity1155f762007-11-22 11:30:47 +02006687 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006688 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006689
Andi Kleena0861c02009-06-08 17:37:09 +08006690 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006691 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006692
Jim Mattsonef85b672016-12-12 11:01:37 -08006693 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006694 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006695
Wanpeng Li082d06e2018-04-03 16:28:48 -07006696 if (is_invalid_opcode(intr_info))
6697 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006698
Avi Kivity6aa8b732006-12-10 02:21:36 -08006699 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006700 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006701 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006702
Liran Alon9e869482018-03-12 13:12:51 +02006703 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6704 WARN_ON_ONCE(!enable_vmware_backdoor);
6705 er = emulate_instruction(vcpu,
6706 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6707 if (er == EMULATE_USER_EXIT)
6708 return 0;
6709 else if (er != EMULATE_DONE)
6710 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6711 return 1;
6712 }
6713
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006714 /*
6715 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6716 * MMIO, it is better to report an internal error.
6717 * See the comments in vmx_handle_exit.
6718 */
6719 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6720 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6721 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6722 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006723 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006724 vcpu->run->internal.data[0] = vect_info;
6725 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006726 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006727 return 0;
6728 }
6729
Avi Kivity6aa8b732006-12-10 02:21:36 -08006730 if (is_page_fault(intr_info)) {
6731 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006732 /* EPT won't cause page fault directly */
6733 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006734 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006735 }
6736
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006737 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006738
6739 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6740 return handle_rmode_exception(vcpu, ex_no, error_code);
6741
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006742 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006743 case AC_VECTOR:
6744 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6745 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006746 case DB_VECTOR:
6747 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6748 if (!(vcpu->guest_debug &
6749 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006750 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006751 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006752 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006753 skip_emulated_instruction(vcpu);
6754
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006755 kvm_queue_exception(vcpu, DB_VECTOR);
6756 return 1;
6757 }
6758 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6759 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6760 /* fall through */
6761 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006762 /*
6763 * Update instruction length as we may reinject #BP from
6764 * user space while in guest debugging mode. Reading it for
6765 * #DB as well causes no harm, it is not used in that case.
6766 */
6767 vmx->vcpu.arch.event_exit_inst_len =
6768 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006769 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006770 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006771 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6772 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006773 break;
6774 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006775 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6776 kvm_run->ex.exception = ex_no;
6777 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006778 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006779 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006780 return 0;
6781}
6782
Avi Kivity851ba692009-08-24 11:10:17 +03006783static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006784{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006785 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006786 return 1;
6787}
6788
Avi Kivity851ba692009-08-24 11:10:17 +03006789static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006790{
Avi Kivity851ba692009-08-24 11:10:17 +03006791 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006792 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006793 return 0;
6794}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006795
Avi Kivity851ba692009-08-24 11:10:17 +03006796static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006797{
He, Qingbfdaab02007-09-12 14:18:28 +08006798 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006799 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006800 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006801
He, Qingbfdaab02007-09-12 14:18:28 +08006802 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006803 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006804
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006805 ++vcpu->stat.io_exits;
6806
Sean Christopherson432baf62018-03-08 08:57:26 -08006807 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006808 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006809
6810 port = exit_qualification >> 16;
6811 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006812 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006813
Sean Christophersondca7f122018-03-08 08:57:27 -08006814 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006815}
6816
Ingo Molnar102d8322007-02-19 14:37:47 +02006817static void
6818vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6819{
6820 /*
6821 * Patch in the VMCALL instruction:
6822 */
6823 hypercall[0] = 0x0f;
6824 hypercall[1] = 0x01;
6825 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006826}
6827
Guo Chao0fa06072012-06-28 15:16:19 +08006828/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006829static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6830{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006831 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006832 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6833 unsigned long orig_val = val;
6834
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006835 /*
6836 * We get here when L2 changed cr0 in a way that did not change
6837 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006838 * but did change L0 shadowed bits. So we first calculate the
6839 * effective cr0 value that L1 would like to write into the
6840 * hardware. It consists of the L2-owned bits from the new
6841 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006842 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006843 val = (val & ~vmcs12->cr0_guest_host_mask) |
6844 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6845
David Matlack38991522016-11-29 18:14:08 -08006846 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006847 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006848
6849 if (kvm_set_cr0(vcpu, val))
6850 return 1;
6851 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006852 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006853 } else {
6854 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006855 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006856 return 1;
David Matlack38991522016-11-29 18:14:08 -08006857
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006858 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006859 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006860}
6861
6862static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6863{
6864 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006865 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6866 unsigned long orig_val = val;
6867
6868 /* analogously to handle_set_cr0 */
6869 val = (val & ~vmcs12->cr4_guest_host_mask) |
6870 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6871 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006872 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006873 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006874 return 0;
6875 } else
6876 return kvm_set_cr4(vcpu, val);
6877}
6878
Paolo Bonzini0367f202016-07-12 10:44:55 +02006879static int handle_desc(struct kvm_vcpu *vcpu)
6880{
6881 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6882 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6883}
6884
Avi Kivity851ba692009-08-24 11:10:17 +03006885static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006886{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006887 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006888 int cr;
6889 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006890 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006891 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006892
He, Qingbfdaab02007-09-12 14:18:28 +08006893 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006894 cr = exit_qualification & 15;
6895 reg = (exit_qualification >> 8) & 15;
6896 switch ((exit_qualification >> 4) & 3) {
6897 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006898 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006899 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006900 switch (cr) {
6901 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006902 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006903 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006905 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006906 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006907 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006908 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006909 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006910 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006911 case 8: {
6912 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006913 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006914 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006915 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006916 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006917 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006918 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006919 return ret;
6920 /*
6921 * TODO: we might be squashing a
6922 * KVM_GUESTDBG_SINGLESTEP-triggered
6923 * KVM_EXIT_DEBUG here.
6924 */
Avi Kivity851ba692009-08-24 11:10:17 +03006925 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006926 return 0;
6927 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006928 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006930 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006931 WARN_ONCE(1, "Guest should always own CR0.TS");
6932 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006933 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006934 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935 case 1: /*mov from cr*/
6936 switch (cr) {
6937 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006938 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006939 val = kvm_read_cr3(vcpu);
6940 kvm_register_write(vcpu, reg, val);
6941 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006942 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006943 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006944 val = kvm_get_cr8(vcpu);
6945 kvm_register_write(vcpu, reg, val);
6946 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006947 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006948 }
6949 break;
6950 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006951 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006952 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006953 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006954
Kyle Huey6affcbe2016-11-29 12:40:40 -08006955 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006956 default:
6957 break;
6958 }
Avi Kivity851ba692009-08-24 11:10:17 +03006959 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006960 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006961 (int)(exit_qualification >> 4) & 3, cr);
6962 return 0;
6963}
6964
Avi Kivity851ba692009-08-24 11:10:17 +03006965static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006966{
He, Qingbfdaab02007-09-12 14:18:28 +08006967 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006968 int dr, dr7, reg;
6969
6970 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6971 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6972
6973 /* First, if DR does not exist, trigger UD */
6974 if (!kvm_require_dr(vcpu, dr))
6975 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006976
Jan Kiszkaf2483412010-01-20 18:20:20 +01006977 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006978 if (!kvm_require_cpl(vcpu, 0))
6979 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006980 dr7 = vmcs_readl(GUEST_DR7);
6981 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006982 /*
6983 * As the vm-exit takes precedence over the debug trap, we
6984 * need to emulate the latter, either for the host or the
6985 * guest debugging itself.
6986 */
6987 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006988 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006989 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006990 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006991 vcpu->run->debug.arch.exception = DB_VECTOR;
6992 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006993 return 0;
6994 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006995 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006996 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006997 kvm_queue_exception(vcpu, DB_VECTOR);
6998 return 1;
6999 }
7000 }
7001
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007002 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007003 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7004 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007005
7006 /*
7007 * No more DR vmexits; force a reload of the debug registers
7008 * and reenter on this instruction. The next vmexit will
7009 * retrieve the full state of the debug registers.
7010 */
7011 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7012 return 1;
7013 }
7014
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007015 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7016 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007017 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007018
7019 if (kvm_get_dr(vcpu, dr, &val))
7020 return 1;
7021 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007022 } else
Nadav Amit57773922014-06-18 17:19:23 +03007023 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007024 return 1;
7025
Kyle Huey6affcbe2016-11-29 12:40:40 -08007026 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007027}
7028
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007029static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7030{
7031 return vcpu->arch.dr6;
7032}
7033
7034static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7035{
7036}
7037
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007038static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7039{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007040 get_debugreg(vcpu->arch.db[0], 0);
7041 get_debugreg(vcpu->arch.db[1], 1);
7042 get_debugreg(vcpu->arch.db[2], 2);
7043 get_debugreg(vcpu->arch.db[3], 3);
7044 get_debugreg(vcpu->arch.dr6, 6);
7045 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7046
7047 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007048 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007049}
7050
Gleb Natapov020df072010-04-13 10:05:23 +03007051static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7052{
7053 vmcs_writel(GUEST_DR7, val);
7054}
7055
Avi Kivity851ba692009-08-24 11:10:17 +03007056static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007057{
Kyle Huey6a908b62016-11-29 12:40:37 -08007058 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059}
7060
Avi Kivity851ba692009-08-24 11:10:17 +03007061static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007062{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007063 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007064 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007065
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007066 msr_info.index = ecx;
7067 msr_info.host_initiated = false;
7068 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007069 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007070 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007071 return 1;
7072 }
7073
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007074 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007075
Avi Kivity6aa8b732006-12-10 02:21:36 -08007076 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007077 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7078 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007079 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080}
7081
Avi Kivity851ba692009-08-24 11:10:17 +03007082static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007083{
Will Auld8fe8ab42012-11-29 12:42:12 -08007084 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007085 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7086 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7087 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007088
Will Auld8fe8ab42012-11-29 12:42:12 -08007089 msr.data = data;
7090 msr.index = ecx;
7091 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007092 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007093 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007094 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007095 return 1;
7096 }
7097
Avi Kivity59200272010-01-25 19:47:02 +02007098 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007099 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007100}
7101
Avi Kivity851ba692009-08-24 11:10:17 +03007102static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007103{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007104 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007105 return 1;
7106}
7107
Avi Kivity851ba692009-08-24 11:10:17 +03007108static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007109{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007110 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7111 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007112
Avi Kivity3842d132010-07-27 12:30:24 +03007113 kvm_make_request(KVM_REQ_EVENT, vcpu);
7114
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007115 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116 return 1;
7117}
7118
Avi Kivity851ba692009-08-24 11:10:17 +03007119static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120{
Avi Kivityd3bef152007-06-05 15:53:05 +03007121 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007122}
7123
Avi Kivity851ba692009-08-24 11:10:17 +03007124static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007125{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007126 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007127}
7128
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007129static int handle_invd(struct kvm_vcpu *vcpu)
7130{
Andre Przywara51d8b662010-12-21 11:12:02 +01007131 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007132}
7133
Avi Kivity851ba692009-08-24 11:10:17 +03007134static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007135{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007136 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007137
7138 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007139 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007140}
7141
Avi Kivityfee84b02011-11-10 14:57:25 +02007142static int handle_rdpmc(struct kvm_vcpu *vcpu)
7143{
7144 int err;
7145
7146 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007147 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007148}
7149
Avi Kivity851ba692009-08-24 11:10:17 +03007150static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007151{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007152 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007153}
7154
Dexuan Cui2acf9232010-06-10 11:27:12 +08007155static int handle_xsetbv(struct kvm_vcpu *vcpu)
7156{
7157 u64 new_bv = kvm_read_edx_eax(vcpu);
7158 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7159
7160 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007161 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007162 return 1;
7163}
7164
Wanpeng Lif53cd632014-12-02 19:14:58 +08007165static int handle_xsaves(struct kvm_vcpu *vcpu)
7166{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007167 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007168 WARN(1, "this should never happen\n");
7169 return 1;
7170}
7171
7172static int handle_xrstors(struct kvm_vcpu *vcpu)
7173{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007174 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007175 WARN(1, "this should never happen\n");
7176 return 1;
7177}
7178
Avi Kivity851ba692009-08-24 11:10:17 +03007179static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007180{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007181 if (likely(fasteoi)) {
7182 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7183 int access_type, offset;
7184
7185 access_type = exit_qualification & APIC_ACCESS_TYPE;
7186 offset = exit_qualification & APIC_ACCESS_OFFSET;
7187 /*
7188 * Sane guest uses MOV to write EOI, with written value
7189 * not cared. So make a short-circuit here by avoiding
7190 * heavy instruction emulation.
7191 */
7192 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7193 (offset == APIC_EOI)) {
7194 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007195 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007196 }
7197 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007198 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007199}
7200
Yang Zhangc7c9c562013-01-25 10:18:51 +08007201static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7202{
7203 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7204 int vector = exit_qualification & 0xff;
7205
7206 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7207 kvm_apic_set_eoi_accelerated(vcpu, vector);
7208 return 1;
7209}
7210
Yang Zhang83d4c282013-01-25 10:18:49 +08007211static int handle_apic_write(struct kvm_vcpu *vcpu)
7212{
7213 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7214 u32 offset = exit_qualification & 0xfff;
7215
7216 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7217 kvm_apic_write_nodecode(vcpu, offset);
7218 return 1;
7219}
7220
Avi Kivity851ba692009-08-24 11:10:17 +03007221static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007222{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007223 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007224 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007225 bool has_error_code = false;
7226 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007227 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007228 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007229
7230 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007231 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007232 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007233
7234 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7235
7236 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007237 if (reason == TASK_SWITCH_GATE && idt_v) {
7238 switch (type) {
7239 case INTR_TYPE_NMI_INTR:
7240 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007241 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007242 break;
7243 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007244 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007245 kvm_clear_interrupt_queue(vcpu);
7246 break;
7247 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007248 if (vmx->idt_vectoring_info &
7249 VECTORING_INFO_DELIVER_CODE_MASK) {
7250 has_error_code = true;
7251 error_code =
7252 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7253 }
7254 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007255 case INTR_TYPE_SOFT_EXCEPTION:
7256 kvm_clear_exception_queue(vcpu);
7257 break;
7258 default:
7259 break;
7260 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007261 }
Izik Eidus37817f22008-03-24 23:14:53 +02007262 tss_selector = exit_qualification;
7263
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007264 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7265 type != INTR_TYPE_EXT_INTR &&
7266 type != INTR_TYPE_NMI_INTR))
7267 skip_emulated_instruction(vcpu);
7268
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007269 if (kvm_task_switch(vcpu, tss_selector,
7270 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7271 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007272 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7273 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7274 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007275 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007276 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007277
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007278 /*
7279 * TODO: What about debug traps on tss switch?
7280 * Are we supposed to inject them and update dr6?
7281 */
7282
7283 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007284}
7285
Avi Kivity851ba692009-08-24 11:10:17 +03007286static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007287{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007288 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007289 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007290 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007291
Sheng Yangf9c617f2009-03-25 10:08:52 +08007292 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007293
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007294 /*
7295 * EPT violation happened while executing iret from NMI,
7296 * "blocked by NMI" bit has to be set before next VM entry.
7297 * There are errata that may cause this bit to not be set:
7298 * AAK134, BY25.
7299 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007300 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007301 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007302 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007303 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7304
Sheng Yang14394422008-04-28 12:24:45 +08007305 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007306 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007307
Junaid Shahid27959a42016-12-06 16:46:10 -08007308 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007309 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007310 ? PFERR_USER_MASK : 0;
7311 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007312 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007313 ? PFERR_WRITE_MASK : 0;
7314 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007315 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007316 ? PFERR_FETCH_MASK : 0;
7317 /* ept page table entry is present? */
7318 error_code |= (exit_qualification &
7319 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7320 EPT_VIOLATION_EXECUTABLE))
7321 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007322
Paolo Bonzinieebed242016-11-28 14:39:58 +01007323 error_code |= (exit_qualification & 0x100) != 0 ?
7324 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007325
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007326 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007327 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007328}
7329
Avi Kivity851ba692009-08-24 11:10:17 +03007330static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007331{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007332 gpa_t gpa;
7333
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007334 /*
7335 * A nested guest cannot optimize MMIO vmexits, because we have an
7336 * nGPA here instead of the required GPA.
7337 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007338 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007339 if (!is_guest_mode(vcpu) &&
7340 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007341 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007342 /*
7343 * Doing kvm_skip_emulated_instruction() depends on undefined
7344 * behavior: Intel's manual doesn't mandate
7345 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7346 * occurs and while on real hardware it was observed to be set,
7347 * other hypervisors (namely Hyper-V) don't set it, we end up
7348 * advancing IP with some random value. Disable fast mmio when
7349 * running nested and keep it for real hardware in hope that
7350 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7351 */
7352 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7353 return kvm_skip_emulated_instruction(vcpu);
7354 else
7355 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7356 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007357 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007358
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007359 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007360}
7361
Avi Kivity851ba692009-08-24 11:10:17 +03007362static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007363{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007364 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007365 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7366 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007367 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007368 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007369
7370 return 1;
7371}
7372
Mohammed Gamal80ced182009-09-01 12:48:18 +02007373static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007374{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007375 struct vcpu_vmx *vmx = to_vmx(vcpu);
7376 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007377 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007378 u32 cpu_exec_ctrl;
7379 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007380 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007381
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007382 /*
7383 * We should never reach the point where we are emulating L2
7384 * due to invalid guest state as that means we incorrectly
7385 * allowed a nested VMEntry with an invalid vmcs12.
7386 */
7387 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7388
Avi Kivity49e9d552010-09-19 14:34:08 +02007389 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7390 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007391
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007392 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007393 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007394 return handle_interrupt_window(&vmx->vcpu);
7395
Radim Krčmář72875d82017-04-26 22:32:19 +02007396 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007397 return 1;
7398
Liran Alon9b8ae632017-11-05 16:56:34 +02007399 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007400
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007401 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007402 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007403 ret = 0;
7404 goto out;
7405 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007406
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007407 if (err != EMULATE_DONE)
7408 goto emulation_error;
7409
7410 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7411 vcpu->arch.exception.pending)
7412 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007413
Gleb Natapov8d76c492013-05-08 18:38:44 +03007414 if (vcpu->arch.halt_request) {
7415 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007416 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007417 goto out;
7418 }
7419
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007420 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007421 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007422 if (need_resched())
7423 schedule();
7424 }
7425
Mohammed Gamal80ced182009-09-01 12:48:18 +02007426out:
7427 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007428
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007429emulation_error:
7430 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7431 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7432 vcpu->run->internal.ndata = 0;
7433 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007434}
7435
7436static void grow_ple_window(struct kvm_vcpu *vcpu)
7437{
7438 struct vcpu_vmx *vmx = to_vmx(vcpu);
7439 int old = vmx->ple_window;
7440
Babu Mogerc8e88712018-03-16 16:37:24 -04007441 vmx->ple_window = __grow_ple_window(old, ple_window,
7442 ple_window_grow,
7443 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007444
7445 if (vmx->ple_window != old)
7446 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007447
7448 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007449}
7450
7451static void shrink_ple_window(struct kvm_vcpu *vcpu)
7452{
7453 struct vcpu_vmx *vmx = to_vmx(vcpu);
7454 int old = vmx->ple_window;
7455
Babu Mogerc8e88712018-03-16 16:37:24 -04007456 vmx->ple_window = __shrink_ple_window(old, ple_window,
7457 ple_window_shrink,
7458 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007459
7460 if (vmx->ple_window != old)
7461 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007462
7463 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007464}
7465
7466/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007467 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7468 */
7469static void wakeup_handler(void)
7470{
7471 struct kvm_vcpu *vcpu;
7472 int cpu = smp_processor_id();
7473
7474 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7475 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7476 blocked_vcpu_list) {
7477 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7478
7479 if (pi_test_on(pi_desc) == 1)
7480 kvm_vcpu_kick(vcpu);
7481 }
7482 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7483}
7484
Peng Haoe01bca22018-04-07 05:47:32 +08007485static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007486{
7487 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7488 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7489 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7490 0ull, VMX_EPT_EXECUTABLE_MASK,
7491 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007492 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007493
7494 ept_set_mmio_spte_mask();
7495 kvm_enable_tdp();
7496}
7497
Tiejun Chenf2c76482014-10-28 10:14:47 +08007498static __init int hardware_setup(void)
7499{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007500 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007501 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007502
7503 rdmsrl_safe(MSR_EFER, &host_efer);
7504
7505 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7506 kvm_define_shared_msr(i, vmx_msr_index[i]);
7507
Radim Krčmář23611332016-09-29 22:41:33 +02007508 for (i = 0; i < VMX_BITMAP_NR; i++) {
7509 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7510 if (!vmx_bitmap[i])
7511 goto out;
7512 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007513
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007514 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7515 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7516
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007517 if (setup_vmcs_config(&vmcs_config) < 0) {
7518 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007519 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007520 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007521
7522 if (boot_cpu_has(X86_FEATURE_NX))
7523 kvm_enable_efer_bits(EFER_NX);
7524
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007525 if (boot_cpu_has(X86_FEATURE_MPX)) {
7526 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7527 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7528 }
7529
Wanpeng Li08d839c2017-03-23 05:30:08 -07007530 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7531 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007532 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007533
Tiejun Chenf2c76482014-10-28 10:14:47 +08007534 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007535 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007536 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007537 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007538 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007539
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007540 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007541 enable_ept_ad_bits = 0;
7542
Wanpeng Li8ad81822017-10-09 15:51:53 -07007543 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007544 enable_unrestricted_guest = 0;
7545
Paolo Bonziniad15a292015-01-30 16:18:49 +01007546 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007547 flexpriority_enabled = 0;
7548
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007549 if (!cpu_has_virtual_nmis())
7550 enable_vnmi = 0;
7551
Paolo Bonziniad15a292015-01-30 16:18:49 +01007552 /*
7553 * set_apic_access_page_addr() is used to reload apic access
7554 * page upon invalidation. No need to do anything if not
7555 * using the APIC_ACCESS_ADDR VMCS field.
7556 */
7557 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007558 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007559
7560 if (!cpu_has_vmx_tpr_shadow())
7561 kvm_x86_ops->update_cr8_intercept = NULL;
7562
7563 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7564 kvm_disable_largepages();
7565
Wanpeng Li0f107682017-09-28 18:06:24 -07007566 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007567 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007568 ple_window = 0;
7569 ple_window_grow = 0;
7570 ple_window_max = 0;
7571 ple_window_shrink = 0;
7572 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007573
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007574 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007575 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007576 kvm_x86_ops->sync_pir_to_irr = NULL;
7577 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007578
Haozhong Zhang64903d62015-10-20 15:39:09 +08007579 if (cpu_has_vmx_tsc_scaling()) {
7580 kvm_has_tsc_control = true;
7581 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7582 kvm_tsc_scaling_ratio_frac_bits = 48;
7583 }
7584
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007585 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7586
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007587 if (enable_ept)
7588 vmx_enable_tdp();
7589 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007590 kvm_disable_tdp();
7591
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007592 if (!nested) {
7593 kvm_x86_ops->get_nested_state = NULL;
7594 kvm_x86_ops->set_nested_state = NULL;
7595 }
7596
Kai Huang843e4332015-01-28 10:54:28 +08007597 /*
7598 * Only enable PML when hardware supports PML feature, and both EPT
7599 * and EPT A/D bit features are enabled -- PML depends on them to work.
7600 */
7601 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7602 enable_pml = 0;
7603
7604 if (!enable_pml) {
7605 kvm_x86_ops->slot_enable_log_dirty = NULL;
7606 kvm_x86_ops->slot_disable_log_dirty = NULL;
7607 kvm_x86_ops->flush_log_dirty = NULL;
7608 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7609 }
7610
Yunhong Jiang64672c92016-06-13 14:19:59 -07007611 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7612 u64 vmx_msr;
7613
7614 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7615 cpu_preemption_timer_multi =
7616 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7617 } else {
7618 kvm_x86_ops->set_hv_timer = NULL;
7619 kvm_x86_ops->cancel_hv_timer = NULL;
7620 }
7621
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007622 if (!cpu_has_vmx_shadow_vmcs())
7623 enable_shadow_vmcs = 0;
7624 if (enable_shadow_vmcs)
7625 init_vmcs_shadow_fields();
7626
Feng Wubf9f6ac2015-09-18 22:29:55 +08007627 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007628 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007629
Ashok Rajc45dcc72016-06-22 14:59:56 +08007630 kvm_mce_cap_supported |= MCG_LMCE_P;
7631
Tiejun Chenf2c76482014-10-28 10:14:47 +08007632 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007633
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007634out:
Radim Krčmář23611332016-09-29 22:41:33 +02007635 for (i = 0; i < VMX_BITMAP_NR; i++)
7636 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007637
7638 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007639}
7640
7641static __exit void hardware_unsetup(void)
7642{
Radim Krčmář23611332016-09-29 22:41:33 +02007643 int i;
7644
7645 for (i = 0; i < VMX_BITMAP_NR; i++)
7646 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007647
Tiejun Chenf2c76482014-10-28 10:14:47 +08007648 free_kvm_area();
7649}
7650
Avi Kivity6aa8b732006-12-10 02:21:36 -08007651/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007652 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7653 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7654 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007655static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007656{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007657 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007658 grow_ple_window(vcpu);
7659
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007660 /*
7661 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7662 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7663 * never set PAUSE_EXITING and just set PLE if supported,
7664 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7665 */
7666 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007667 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007668}
7669
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007670static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007671{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007672 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007673}
7674
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007675static int handle_mwait(struct kvm_vcpu *vcpu)
7676{
7677 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7678 return handle_nop(vcpu);
7679}
7680
Jim Mattson45ec3682017-08-23 16:32:04 -07007681static int handle_invalid_op(struct kvm_vcpu *vcpu)
7682{
7683 kvm_queue_exception(vcpu, UD_VECTOR);
7684 return 1;
7685}
7686
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007687static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7688{
7689 return 1;
7690}
7691
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007692static int handle_monitor(struct kvm_vcpu *vcpu)
7693{
7694 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7695 return handle_nop(vcpu);
7696}
7697
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007698/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007699 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7700 * set the success or error code of an emulated VMX instruction, as specified
7701 * by Vol 2B, VMX Instruction Reference, "Conventions".
7702 */
7703static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7704{
7705 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7706 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7707 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7708}
7709
7710static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7711{
7712 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7713 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7714 X86_EFLAGS_SF | X86_EFLAGS_OF))
7715 | X86_EFLAGS_CF);
7716}
7717
Abel Gordon145c28d2013-04-18 14:36:55 +03007718static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007719 u32 vm_instruction_error)
7720{
7721 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7722 /*
7723 * failValid writes the error number to the current VMCS, which
7724 * can't be done there isn't a current VMCS.
7725 */
7726 nested_vmx_failInvalid(vcpu);
7727 return;
7728 }
7729 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7730 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7731 X86_EFLAGS_SF | X86_EFLAGS_OF))
7732 | X86_EFLAGS_ZF);
7733 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7734 /*
7735 * We don't need to force a shadow sync because
7736 * VM_INSTRUCTION_ERROR is not shadowed
7737 */
7738}
Abel Gordon145c28d2013-04-18 14:36:55 +03007739
Wincy Vanff651cb2014-12-11 08:52:58 +03007740static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7741{
7742 /* TODO: not to reset guest simply here. */
7743 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007744 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007745}
7746
Jan Kiszkaf4124502014-03-07 20:03:13 +01007747static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7748{
7749 struct vcpu_vmx *vmx =
7750 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7751
7752 vmx->nested.preemption_timer_expired = true;
7753 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7754 kvm_vcpu_kick(&vmx->vcpu);
7755
7756 return HRTIMER_NORESTART;
7757}
7758
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007759/*
Bandan Das19677e32014-05-06 02:19:15 -04007760 * Decode the memory-address operand of a vmx instruction, as recorded on an
7761 * exit caused by such an instruction (run by a guest hypervisor).
7762 * On success, returns 0. When the operand is invalid, returns 1 and throws
7763 * #UD or #GP.
7764 */
7765static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7766 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007767 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007768{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007769 gva_t off;
7770 bool exn;
7771 struct kvm_segment s;
7772
Bandan Das19677e32014-05-06 02:19:15 -04007773 /*
7774 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7775 * Execution", on an exit, vmx_instruction_info holds most of the
7776 * addressing components of the operand. Only the displacement part
7777 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7778 * For how an actual address is calculated from all these components,
7779 * refer to Vol. 1, "Operand Addressing".
7780 */
7781 int scaling = vmx_instruction_info & 3;
7782 int addr_size = (vmx_instruction_info >> 7) & 7;
7783 bool is_reg = vmx_instruction_info & (1u << 10);
7784 int seg_reg = (vmx_instruction_info >> 15) & 7;
7785 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7786 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7787 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7788 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7789
7790 if (is_reg) {
7791 kvm_queue_exception(vcpu, UD_VECTOR);
7792 return 1;
7793 }
7794
7795 /* Addr = segment_base + offset */
7796 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007797 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007798 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007799 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007800 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007801 off += kvm_register_read(vcpu, index_reg)<<scaling;
7802 vmx_get_segment(vcpu, &s, seg_reg);
7803 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007804
7805 if (addr_size == 1) /* 32 bit */
7806 *ret &= 0xffffffff;
7807
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007808 /* Checks for #GP/#SS exceptions. */
7809 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007810 if (is_long_mode(vcpu)) {
7811 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7812 * non-canonical form. This is the only check on the memory
7813 * destination for long mode!
7814 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007815 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007816 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007817 /* Protected mode: apply checks for segment validity in the
7818 * following order:
7819 * - segment type check (#GP(0) may be thrown)
7820 * - usability check (#GP(0)/#SS(0))
7821 * - limit check (#GP(0)/#SS(0))
7822 */
7823 if (wr)
7824 /* #GP(0) if the destination operand is located in a
7825 * read-only data segment or any code segment.
7826 */
7827 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7828 else
7829 /* #GP(0) if the source operand is located in an
7830 * execute-only code segment
7831 */
7832 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007833 if (exn) {
7834 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7835 return 1;
7836 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007837 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7838 */
7839 exn = (s.unusable != 0);
7840 /* Protected mode: #GP(0)/#SS(0) if the memory
7841 * operand is outside the segment limit.
7842 */
7843 exn = exn || (off + sizeof(u64) > s.limit);
7844 }
7845 if (exn) {
7846 kvm_queue_exception_e(vcpu,
7847 seg_reg == VCPU_SREG_SS ?
7848 SS_VECTOR : GP_VECTOR,
7849 0);
7850 return 1;
7851 }
7852
Bandan Das19677e32014-05-06 02:19:15 -04007853 return 0;
7854}
7855
Radim Krčmářcbf71272017-05-19 15:48:51 +02007856static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007857{
7858 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007859 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007860
7861 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007862 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007863 return 1;
7864
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007865 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007866 kvm_inject_page_fault(vcpu, &e);
7867 return 1;
7868 }
7869
Bandan Das3573e222014-05-06 02:19:16 -04007870 return 0;
7871}
7872
Jim Mattsone29acc52016-11-30 12:03:43 -08007873static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7874{
7875 struct vcpu_vmx *vmx = to_vmx(vcpu);
7876 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007877 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007878
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007879 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7880 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007881 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007882
7883 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7884 if (!vmx->nested.cached_vmcs12)
7885 goto out_cached_vmcs12;
7886
7887 if (enable_shadow_vmcs) {
7888 shadow_vmcs = alloc_vmcs();
7889 if (!shadow_vmcs)
7890 goto out_shadow_vmcs;
7891 /* mark vmcs as shadow */
7892 shadow_vmcs->revision_id |= (1u << 31);
7893 /* init shadow vmcs */
7894 vmcs_clear(shadow_vmcs);
7895 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7896 }
7897
Jim Mattsone29acc52016-11-30 12:03:43 -08007898 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7899 HRTIMER_MODE_REL_PINNED);
7900 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7901
7902 vmx->nested.vmxon = true;
7903 return 0;
7904
7905out_shadow_vmcs:
7906 kfree(vmx->nested.cached_vmcs12);
7907
7908out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007909 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007910
Jim Mattsonde3a0022017-11-27 17:22:25 -06007911out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007912 return -ENOMEM;
7913}
7914
Bandan Das3573e222014-05-06 02:19:16 -04007915/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007916 * Emulate the VMXON instruction.
7917 * Currently, we just remember that VMX is active, and do not save or even
7918 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7919 * do not currently need to store anything in that guest-allocated memory
7920 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7921 * argument is different from the VMXON pointer (which the spec says they do).
7922 */
7923static int handle_vmon(struct kvm_vcpu *vcpu)
7924{
Jim Mattsone29acc52016-11-30 12:03:43 -08007925 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007926 gpa_t vmptr;
7927 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007929 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7930 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007931
Jim Mattson70f3aac2017-04-26 08:53:46 -07007932 /*
7933 * The Intel VMX Instruction Reference lists a bunch of bits that are
7934 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7935 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7936 * Otherwise, we should fail with #UD. But most faulting conditions
7937 * have already been checked by hardware, prior to the VM-exit for
7938 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7939 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007940 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007941 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007942 kvm_queue_exception(vcpu, UD_VECTOR);
7943 return 1;
7944 }
7945
Felix Wilhelm727ba742018-06-11 09:43:44 +02007946 /* CPL=0 must be checked manually. */
7947 if (vmx_get_cpl(vcpu)) {
7948 kvm_queue_exception(vcpu, UD_VECTOR);
7949 return 1;
7950 }
7951
Abel Gordon145c28d2013-04-18 14:36:55 +03007952 if (vmx->nested.vmxon) {
7953 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007954 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007955 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007956
Haozhong Zhang3b840802016-06-22 14:59:54 +08007957 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007958 != VMXON_NEEDED_FEATURES) {
7959 kvm_inject_gp(vcpu, 0);
7960 return 1;
7961 }
7962
Radim Krčmářcbf71272017-05-19 15:48:51 +02007963 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007964 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007965
7966 /*
7967 * SDM 3: 24.11.5
7968 * The first 4 bytes of VMXON region contain the supported
7969 * VMCS revision identifier
7970 *
7971 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7972 * which replaces physical address width with 32
7973 */
7974 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7975 nested_vmx_failInvalid(vcpu);
7976 return kvm_skip_emulated_instruction(vcpu);
7977 }
7978
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007979 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7980 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007981 nested_vmx_failInvalid(vcpu);
7982 return kvm_skip_emulated_instruction(vcpu);
7983 }
7984 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7985 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007986 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007987 nested_vmx_failInvalid(vcpu);
7988 return kvm_skip_emulated_instruction(vcpu);
7989 }
7990 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007991 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007992
7993 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007994 ret = enter_vmx_operation(vcpu);
7995 if (ret)
7996 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007997
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007998 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007999 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008000}
8001
8002/*
8003 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8004 * for running VMX instructions (except VMXON, whose prerequisites are
8005 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008006 * Note that many of these exceptions have priority over VM exits, so they
8007 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008008 */
8009static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8010{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008011 if (vmx_get_cpl(vcpu)) {
8012 kvm_queue_exception(vcpu, UD_VECTOR);
8013 return 0;
8014 }
8015
Jim Mattson70f3aac2017-04-26 08:53:46 -07008016 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008017 kvm_queue_exception(vcpu, UD_VECTOR);
8018 return 0;
8019 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008020 return 1;
8021}
8022
David Matlack8ca44e82017-08-01 14:00:39 -07008023static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8024{
8025 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8026 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8027}
8028
Abel Gordone7953d72013-04-18 14:37:55 +03008029static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8030{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008031 if (vmx->nested.current_vmptr == -1ull)
8032 return;
8033
Abel Gordon012f83c2013-04-18 14:39:25 +03008034 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008035 /* copy to memory all shadowed fields in case
8036 they were modified */
8037 copy_shadow_to_vmcs12(vmx);
8038 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008039 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008040 }
Wincy Van705699a2015-02-03 23:58:17 +08008041 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008042
8043 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008044 kvm_vcpu_write_guest_page(&vmx->vcpu,
8045 vmx->nested.current_vmptr >> PAGE_SHIFT,
8046 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008047
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008048 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008049}
8050
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008051/*
8052 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8053 * just stops using VMX.
8054 */
8055static void free_nested(struct vcpu_vmx *vmx)
8056{
Wanpeng Lib7455822017-11-22 14:04:00 -08008057 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008058 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008059
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008060 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008061 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008062 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008063 vmx->nested.posted_intr_nv = -1;
8064 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008065 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008066 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008067 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8068 free_vmcs(vmx->vmcs01.shadow_vmcs);
8069 vmx->vmcs01.shadow_vmcs = NULL;
8070 }
David Matlack4f2777b2016-07-13 17:16:37 -07008071 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008072 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008073 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008074 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008075 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008076 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008077 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008078 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008079 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008080 }
Wincy Van705699a2015-02-03 23:58:17 +08008081 if (vmx->nested.pi_desc_page) {
8082 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008083 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008084 vmx->nested.pi_desc_page = NULL;
8085 vmx->nested.pi_desc = NULL;
8086 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008087
Jim Mattsonde3a0022017-11-27 17:22:25 -06008088 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008089}
8090
8091/* Emulate the VMXOFF instruction */
8092static int handle_vmoff(struct kvm_vcpu *vcpu)
8093{
8094 if (!nested_vmx_check_permission(vcpu))
8095 return 1;
8096 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008097 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008098 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008099}
8100
Nadav Har'El27d6c862011-05-25 23:06:59 +03008101/* Emulate the VMCLEAR instruction */
8102static int handle_vmclear(struct kvm_vcpu *vcpu)
8103{
8104 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008105 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008106 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008107
8108 if (!nested_vmx_check_permission(vcpu))
8109 return 1;
8110
Radim Krčmářcbf71272017-05-19 15:48:51 +02008111 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008112 return 1;
8113
Radim Krčmářcbf71272017-05-19 15:48:51 +02008114 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8115 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8116 return kvm_skip_emulated_instruction(vcpu);
8117 }
8118
8119 if (vmptr == vmx->nested.vmxon_ptr) {
8120 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8121 return kvm_skip_emulated_instruction(vcpu);
8122 }
8123
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008124 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008125 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008126
Jim Mattson587d7e722017-03-02 12:41:48 -08008127 kvm_vcpu_write_guest(vcpu,
8128 vmptr + offsetof(struct vmcs12, launch_state),
8129 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008130
Nadav Har'El27d6c862011-05-25 23:06:59 +03008131 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008132 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008133}
8134
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008135static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8136
8137/* Emulate the VMLAUNCH instruction */
8138static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8139{
8140 return nested_vmx_run(vcpu, true);
8141}
8142
8143/* Emulate the VMRESUME instruction */
8144static int handle_vmresume(struct kvm_vcpu *vcpu)
8145{
8146
8147 return nested_vmx_run(vcpu, false);
8148}
8149
Nadav Har'El49f705c2011-05-25 23:08:30 +03008150/*
8151 * Read a vmcs12 field. Since these can have varying lengths and we return
8152 * one type, we chose the biggest type (u64) and zero-extend the return value
8153 * to that size. Note that the caller, handle_vmread, might need to use only
8154 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8155 * 64-bit fields are to be returned).
8156 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008157static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8158 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008159{
8160 short offset = vmcs_field_to_offset(field);
8161 char *p;
8162
8163 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008164 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008165
8166 p = ((char *)(get_vmcs12(vcpu))) + offset;
8167
Jim Mattsond37f4262017-12-22 12:12:16 -08008168 switch (vmcs_field_width(field)) {
8169 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008170 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008171 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008172 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008173 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008174 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008175 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008176 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008177 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008178 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008179 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008180 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008181 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008182 WARN_ON(1);
8183 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008184 }
8185}
8186
Abel Gordon20b97fe2013-04-18 14:36:25 +03008187
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008188static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8189 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008190 short offset = vmcs_field_to_offset(field);
8191 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8192 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008193 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008194
Jim Mattsond37f4262017-12-22 12:12:16 -08008195 switch (vmcs_field_width(field)) {
8196 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008197 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008198 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008199 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008200 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008201 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008202 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008203 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008204 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008205 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008206 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008207 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008208 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008209 WARN_ON(1);
8210 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008211 }
8212
8213}
8214
Jim Mattsonf4160e42018-05-29 09:11:33 -07008215/*
8216 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8217 * they have been modified by the L1 guest. Note that the "read-only"
8218 * VM-exit information fields are actually writable if the vCPU is
8219 * configured to support "VMWRITE to any supported field in the VMCS."
8220 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008221static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8222{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008223 const u16 *fields[] = {
8224 shadow_read_write_fields,
8225 shadow_read_only_fields
8226 };
8227 const int max_fields[] = {
8228 max_shadow_read_write_fields,
8229 max_shadow_read_only_fields
8230 };
8231 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008232 unsigned long field;
8233 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008234 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008235
Jan Kiszka282da872014-10-08 18:05:39 +02008236 preempt_disable();
8237
Abel Gordon16f5b902013-04-18 14:38:25 +03008238 vmcs_load(shadow_vmcs);
8239
Jim Mattsonf4160e42018-05-29 09:11:33 -07008240 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8241 for (i = 0; i < max_fields[q]; i++) {
8242 field = fields[q][i];
8243 field_value = __vmcs_readl(field);
8244 vmcs12_write_any(&vmx->vcpu, field, field_value);
8245 }
8246 /*
8247 * Skip the VM-exit information fields if they are read-only.
8248 */
8249 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8250 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008251 }
8252
8253 vmcs_clear(shadow_vmcs);
8254 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008255
8256 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008257}
8258
Abel Gordonc3114422013-04-18 14:38:55 +03008259static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8260{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008261 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008262 shadow_read_write_fields,
8263 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008264 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008265 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008266 max_shadow_read_write_fields,
8267 max_shadow_read_only_fields
8268 };
8269 int i, q;
8270 unsigned long field;
8271 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008272 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008273
8274 vmcs_load(shadow_vmcs);
8275
Mathias Krausec2bae892013-06-26 20:36:21 +02008276 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008277 for (i = 0; i < max_fields[q]; i++) {
8278 field = fields[q][i];
8279 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008280 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008281 }
8282 }
8283
8284 vmcs_clear(shadow_vmcs);
8285 vmcs_load(vmx->loaded_vmcs->vmcs);
8286}
8287
Nadav Har'El49f705c2011-05-25 23:08:30 +03008288/*
8289 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8290 * used before) all generate the same failure when it is missing.
8291 */
8292static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8293{
8294 struct vcpu_vmx *vmx = to_vmx(vcpu);
8295 if (vmx->nested.current_vmptr == -1ull) {
8296 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008297 return 0;
8298 }
8299 return 1;
8300}
8301
8302static int handle_vmread(struct kvm_vcpu *vcpu)
8303{
8304 unsigned long field;
8305 u64 field_value;
8306 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8307 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8308 gva_t gva = 0;
8309
Kyle Hueyeb277562016-11-29 12:40:39 -08008310 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008311 return 1;
8312
Kyle Huey6affcbe2016-11-29 12:40:40 -08008313 if (!nested_vmx_check_vmcs12(vcpu))
8314 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008315
Nadav Har'El49f705c2011-05-25 23:08:30 +03008316 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008317 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008318 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008319 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008320 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008321 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008322 }
8323 /*
8324 * Now copy part of this value to register or memory, as requested.
8325 * Note that the number of bits actually copied is 32 or 64 depending
8326 * on the guest's mode (32 or 64 bit), not on the given field's length.
8327 */
8328 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008329 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008330 field_value);
8331 } else {
8332 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008333 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008334 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008335 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008336 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8337 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008338 }
8339
8340 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008341 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008342}
8343
8344
8345static int handle_vmwrite(struct kvm_vcpu *vcpu)
8346{
8347 unsigned long field;
8348 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008349 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008350 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8351 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008352
Nadav Har'El49f705c2011-05-25 23:08:30 +03008353 /* The value to write might be 32 or 64 bits, depending on L1's long
8354 * mode, and eventually we need to write that into a field of several
8355 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008356 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008357 * bits into the vmcs12 field.
8358 */
8359 u64 field_value = 0;
8360 struct x86_exception e;
8361
Kyle Hueyeb277562016-11-29 12:40:39 -08008362 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008363 return 1;
8364
Kyle Huey6affcbe2016-11-29 12:40:40 -08008365 if (!nested_vmx_check_vmcs12(vcpu))
8366 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008367
Nadav Har'El49f705c2011-05-25 23:08:30 +03008368 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008369 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008370 (((vmx_instruction_info) >> 3) & 0xf));
8371 else {
8372 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008373 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008374 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008375 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8376 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008377 kvm_inject_page_fault(vcpu, &e);
8378 return 1;
8379 }
8380 }
8381
8382
Nadav Amit27e6fb52014-06-18 17:19:26 +03008383 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008384 /*
8385 * If the vCPU supports "VMWRITE to any supported field in the
8386 * VMCS," then the "read-only" fields are actually read/write.
8387 */
8388 if (vmcs_field_readonly(field) &&
8389 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008390 nested_vmx_failValid(vcpu,
8391 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008392 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008393 }
8394
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008395 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008396 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008397 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008398 }
8399
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008400 switch (field) {
8401#define SHADOW_FIELD_RW(x) case x:
8402#include "vmx_shadow_fields.h"
8403 /*
8404 * The fields that can be updated by L1 without a vmexit are
8405 * always updated in the vmcs02, the others go down the slow
8406 * path of prepare_vmcs02.
8407 */
8408 break;
8409 default:
8410 vmx->nested.dirty_vmcs12 = true;
8411 break;
8412 }
8413
Nadav Har'El49f705c2011-05-25 23:08:30 +03008414 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008415 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008416}
8417
Jim Mattsona8bc2842016-11-30 12:03:44 -08008418static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8419{
8420 vmx->nested.current_vmptr = vmptr;
8421 if (enable_shadow_vmcs) {
8422 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8423 SECONDARY_EXEC_SHADOW_VMCS);
8424 vmcs_write64(VMCS_LINK_POINTER,
8425 __pa(vmx->vmcs01.shadow_vmcs));
8426 vmx->nested.sync_shadow_vmcs = true;
8427 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008428 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008429}
8430
Nadav Har'El63846662011-05-25 23:07:29 +03008431/* Emulate the VMPTRLD instruction */
8432static int handle_vmptrld(struct kvm_vcpu *vcpu)
8433{
8434 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008435 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008436
8437 if (!nested_vmx_check_permission(vcpu))
8438 return 1;
8439
Radim Krčmářcbf71272017-05-19 15:48:51 +02008440 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008441 return 1;
8442
Radim Krčmářcbf71272017-05-19 15:48:51 +02008443 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8444 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8445 return kvm_skip_emulated_instruction(vcpu);
8446 }
8447
8448 if (vmptr == vmx->nested.vmxon_ptr) {
8449 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8450 return kvm_skip_emulated_instruction(vcpu);
8451 }
8452
Nadav Har'El63846662011-05-25 23:07:29 +03008453 if (vmx->nested.current_vmptr != vmptr) {
8454 struct vmcs12 *new_vmcs12;
8455 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008456 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8457 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008458 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008459 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008460 }
8461 new_vmcs12 = kmap(page);
8462 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8463 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008464 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008465 nested_vmx_failValid(vcpu,
8466 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008467 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008468 }
Nadav Har'El63846662011-05-25 23:07:29 +03008469
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008470 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008471 /*
8472 * Load VMCS12 from guest memory since it is not already
8473 * cached.
8474 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008475 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8476 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008477 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008478
Jim Mattsona8bc2842016-11-30 12:03:44 -08008479 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008480 }
8481
8482 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008483 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008484}
8485
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008486/* Emulate the VMPTRST instruction */
8487static int handle_vmptrst(struct kvm_vcpu *vcpu)
8488{
8489 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8490 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8491 gva_t vmcs_gva;
8492 struct x86_exception e;
8493
8494 if (!nested_vmx_check_permission(vcpu))
8495 return 1;
8496
8497 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008498 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008499 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008500 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008501 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8502 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8503 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008504 kvm_inject_page_fault(vcpu, &e);
8505 return 1;
8506 }
8507 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008508 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008509}
8510
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008511/* Emulate the INVEPT instruction */
8512static int handle_invept(struct kvm_vcpu *vcpu)
8513{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008514 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008515 u32 vmx_instruction_info, types;
8516 unsigned long type;
8517 gva_t gva;
8518 struct x86_exception e;
8519 struct {
8520 u64 eptp, gpa;
8521 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008522
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008523 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008524 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008525 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008526 kvm_queue_exception(vcpu, UD_VECTOR);
8527 return 1;
8528 }
8529
8530 if (!nested_vmx_check_permission(vcpu))
8531 return 1;
8532
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008533 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008534 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008535
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008536 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008537
Jim Mattson85c856b2016-10-26 08:38:38 -07008538 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008539 nested_vmx_failValid(vcpu,
8540 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008541 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008542 }
8543
8544 /* According to the Intel VMX instruction reference, the memory
8545 * operand is read even if it isn't needed (e.g., for type==global)
8546 */
8547 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008548 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008549 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008550 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008551 kvm_inject_page_fault(vcpu, &e);
8552 return 1;
8553 }
8554
8555 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008556 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008557 /*
8558 * TODO: track mappings and invalidate
8559 * single context requests appropriately
8560 */
8561 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008562 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008563 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008564 nested_vmx_succeed(vcpu);
8565 break;
8566 default:
8567 BUG_ON(1);
8568 break;
8569 }
8570
Kyle Huey6affcbe2016-11-29 12:40:40 -08008571 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008572}
8573
Petr Matouseka642fc32014-09-23 20:22:30 +02008574static int handle_invvpid(struct kvm_vcpu *vcpu)
8575{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008576 struct vcpu_vmx *vmx = to_vmx(vcpu);
8577 u32 vmx_instruction_info;
8578 unsigned long type, types;
8579 gva_t gva;
8580 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008581 struct {
8582 u64 vpid;
8583 u64 gla;
8584 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008585
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008586 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008587 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008588 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008589 kvm_queue_exception(vcpu, UD_VECTOR);
8590 return 1;
8591 }
8592
8593 if (!nested_vmx_check_permission(vcpu))
8594 return 1;
8595
8596 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8597 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8598
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008599 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008600 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008601
Jim Mattson85c856b2016-10-26 08:38:38 -07008602 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008603 nested_vmx_failValid(vcpu,
8604 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008605 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008606 }
8607
8608 /* according to the intel vmx instruction reference, the memory
8609 * operand is read even if it isn't needed (e.g., for type==global)
8610 */
8611 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8612 vmx_instruction_info, false, &gva))
8613 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008614 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008615 kvm_inject_page_fault(vcpu, &e);
8616 return 1;
8617 }
Jim Mattson40352602017-06-28 09:37:37 -07008618 if (operand.vpid >> 16) {
8619 nested_vmx_failValid(vcpu,
8620 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8621 return kvm_skip_emulated_instruction(vcpu);
8622 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008623
8624 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008625 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008626 if (!operand.vpid ||
8627 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008628 nested_vmx_failValid(vcpu,
8629 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8630 return kvm_skip_emulated_instruction(vcpu);
8631 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008632 if (cpu_has_vmx_invvpid_individual_addr() &&
8633 vmx->nested.vpid02) {
8634 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8635 vmx->nested.vpid02, operand.gla);
8636 } else
8637 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8638 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008639 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008640 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008641 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008642 nested_vmx_failValid(vcpu,
8643 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008644 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008645 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008646 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008647 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008648 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008649 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008650 break;
8651 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008652 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008653 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008654 }
8655
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008656 nested_vmx_succeed(vcpu);
8657
Kyle Huey6affcbe2016-11-29 12:40:40 -08008658 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008659}
8660
Kai Huang843e4332015-01-28 10:54:28 +08008661static int handle_pml_full(struct kvm_vcpu *vcpu)
8662{
8663 unsigned long exit_qualification;
8664
8665 trace_kvm_pml_full(vcpu->vcpu_id);
8666
8667 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8668
8669 /*
8670 * PML buffer FULL happened while executing iret from NMI,
8671 * "blocked by NMI" bit has to be set before next VM entry.
8672 */
8673 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008674 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008675 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8676 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8677 GUEST_INTR_STATE_NMI);
8678
8679 /*
8680 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8681 * here.., and there's no userspace involvement needed for PML.
8682 */
8683 return 1;
8684}
8685
Yunhong Jiang64672c92016-06-13 14:19:59 -07008686static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8687{
8688 kvm_lapic_expired_hv_timer(vcpu);
8689 return 1;
8690}
8691
Bandan Das41ab9372017-08-03 15:54:43 -04008692static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8693{
8694 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008695 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8696
8697 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008698 switch (address & VMX_EPTP_MT_MASK) {
8699 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008700 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008701 return false;
8702 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008703 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008704 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008705 return false;
8706 break;
8707 default:
8708 return false;
8709 }
8710
David Hildenbrandbb97a012017-08-10 23:15:28 +02008711 /* only 4 levels page-walk length are valid */
8712 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008713 return false;
8714
8715 /* Reserved bits should not be set */
8716 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8717 return false;
8718
8719 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008720 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008721 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008722 return false;
8723 }
8724
8725 return true;
8726}
8727
8728static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8729 struct vmcs12 *vmcs12)
8730{
8731 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8732 u64 address;
8733 bool accessed_dirty;
8734 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8735
8736 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8737 !nested_cpu_has_ept(vmcs12))
8738 return 1;
8739
8740 if (index >= VMFUNC_EPTP_ENTRIES)
8741 return 1;
8742
8743
8744 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8745 &address, index * 8, 8))
8746 return 1;
8747
David Hildenbrandbb97a012017-08-10 23:15:28 +02008748 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008749
8750 /*
8751 * If the (L2) guest does a vmfunc to the currently
8752 * active ept pointer, we don't have to do anything else
8753 */
8754 if (vmcs12->ept_pointer != address) {
8755 if (!valid_ept_address(vcpu, address))
8756 return 1;
8757
8758 kvm_mmu_unload(vcpu);
8759 mmu->ept_ad = accessed_dirty;
8760 mmu->base_role.ad_disabled = !accessed_dirty;
8761 vmcs12->ept_pointer = address;
8762 /*
8763 * TODO: Check what's the correct approach in case
8764 * mmu reload fails. Currently, we just let the next
8765 * reload potentially fail
8766 */
8767 kvm_mmu_reload(vcpu);
8768 }
8769
8770 return 0;
8771}
8772
Bandan Das2a499e42017-08-03 15:54:41 -04008773static int handle_vmfunc(struct kvm_vcpu *vcpu)
8774{
Bandan Das27c42a12017-08-03 15:54:42 -04008775 struct vcpu_vmx *vmx = to_vmx(vcpu);
8776 struct vmcs12 *vmcs12;
8777 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8778
8779 /*
8780 * VMFUNC is only supported for nested guests, but we always enable the
8781 * secondary control for simplicity; for non-nested mode, fake that we
8782 * didn't by injecting #UD.
8783 */
8784 if (!is_guest_mode(vcpu)) {
8785 kvm_queue_exception(vcpu, UD_VECTOR);
8786 return 1;
8787 }
8788
8789 vmcs12 = get_vmcs12(vcpu);
8790 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8791 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008792
8793 switch (function) {
8794 case 0:
8795 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8796 goto fail;
8797 break;
8798 default:
8799 goto fail;
8800 }
8801 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008802
8803fail:
8804 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8805 vmcs_read32(VM_EXIT_INTR_INFO),
8806 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008807 return 1;
8808}
8809
Nadav Har'El0140cae2011-05-25 23:06:28 +03008810/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008811 * The exit handlers return 1 if the exit was handled fully and guest execution
8812 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8813 * to be done to userspace and return 0.
8814 */
Mathias Krause772e0312012-08-30 01:30:19 +02008815static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008816 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8817 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008818 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008819 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008820 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008821 [EXIT_REASON_CR_ACCESS] = handle_cr,
8822 [EXIT_REASON_DR_ACCESS] = handle_dr,
8823 [EXIT_REASON_CPUID] = handle_cpuid,
8824 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8825 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8826 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8827 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008828 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008829 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008830 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008831 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008832 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008833 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008834 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008835 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008836 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008837 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008838 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008839 [EXIT_REASON_VMOFF] = handle_vmoff,
8840 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008841 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8842 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008843 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008844 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008845 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008846 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008847 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008848 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008849 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8850 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008851 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8852 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008853 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008854 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008855 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008856 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008857 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008858 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008859 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008860 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008861 [EXIT_REASON_XSAVES] = handle_xsaves,
8862 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008863 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008864 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008865 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008866};
8867
8868static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008869 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008870
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008871static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8872 struct vmcs12 *vmcs12)
8873{
8874 unsigned long exit_qualification;
8875 gpa_t bitmap, last_bitmap;
8876 unsigned int port;
8877 int size;
8878 u8 b;
8879
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008880 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008881 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008882
8883 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8884
8885 port = exit_qualification >> 16;
8886 size = (exit_qualification & 7) + 1;
8887
8888 last_bitmap = (gpa_t)-1;
8889 b = -1;
8890
8891 while (size > 0) {
8892 if (port < 0x8000)
8893 bitmap = vmcs12->io_bitmap_a;
8894 else if (port < 0x10000)
8895 bitmap = vmcs12->io_bitmap_b;
8896 else
Joe Perches1d804d02015-03-30 16:46:09 -07008897 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008898 bitmap += (port & 0x7fff) / 8;
8899
8900 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008901 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008902 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008903 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008904 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008905
8906 port++;
8907 size--;
8908 last_bitmap = bitmap;
8909 }
8910
Joe Perches1d804d02015-03-30 16:46:09 -07008911 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008912}
8913
Nadav Har'El644d7112011-05-25 23:12:35 +03008914/*
8915 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8916 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8917 * disinterest in the current event (read or write a specific MSR) by using an
8918 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8919 */
8920static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8921 struct vmcs12 *vmcs12, u32 exit_reason)
8922{
8923 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8924 gpa_t bitmap;
8925
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008926 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008927 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008928
8929 /*
8930 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8931 * for the four combinations of read/write and low/high MSR numbers.
8932 * First we need to figure out which of the four to use:
8933 */
8934 bitmap = vmcs12->msr_bitmap;
8935 if (exit_reason == EXIT_REASON_MSR_WRITE)
8936 bitmap += 2048;
8937 if (msr_index >= 0xc0000000) {
8938 msr_index -= 0xc0000000;
8939 bitmap += 1024;
8940 }
8941
8942 /* Then read the msr_index'th bit from this bitmap: */
8943 if (msr_index < 1024*8) {
8944 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008945 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008946 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008947 return 1 & (b >> (msr_index & 7));
8948 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008949 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008950}
8951
8952/*
8953 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8954 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8955 * intercept (via guest_host_mask etc.) the current event.
8956 */
8957static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8958 struct vmcs12 *vmcs12)
8959{
8960 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8961 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008962 int reg;
8963 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008964
8965 switch ((exit_qualification >> 4) & 3) {
8966 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008967 reg = (exit_qualification >> 8) & 15;
8968 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008969 switch (cr) {
8970 case 0:
8971 if (vmcs12->cr0_guest_host_mask &
8972 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008973 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008974 break;
8975 case 3:
8976 if ((vmcs12->cr3_target_count >= 1 &&
8977 vmcs12->cr3_target_value0 == val) ||
8978 (vmcs12->cr3_target_count >= 2 &&
8979 vmcs12->cr3_target_value1 == val) ||
8980 (vmcs12->cr3_target_count >= 3 &&
8981 vmcs12->cr3_target_value2 == val) ||
8982 (vmcs12->cr3_target_count >= 4 &&
8983 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008984 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008985 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008986 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008987 break;
8988 case 4:
8989 if (vmcs12->cr4_guest_host_mask &
8990 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008991 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008992 break;
8993 case 8:
8994 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008995 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008996 break;
8997 }
8998 break;
8999 case 2: /* clts */
9000 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9001 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009002 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009003 break;
9004 case 1: /* mov from cr */
9005 switch (cr) {
9006 case 3:
9007 if (vmcs12->cpu_based_vm_exec_control &
9008 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009009 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009010 break;
9011 case 8:
9012 if (vmcs12->cpu_based_vm_exec_control &
9013 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009014 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009015 break;
9016 }
9017 break;
9018 case 3: /* lmsw */
9019 /*
9020 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9021 * cr0. Other attempted changes are ignored, with no exit.
9022 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009023 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009024 if (vmcs12->cr0_guest_host_mask & 0xe &
9025 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009026 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009027 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9028 !(vmcs12->cr0_read_shadow & 0x1) &&
9029 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009030 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009031 break;
9032 }
Joe Perches1d804d02015-03-30 16:46:09 -07009033 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009034}
9035
9036/*
9037 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9038 * should handle it ourselves in L0 (and then continue L2). Only call this
9039 * when in is_guest_mode (L2).
9040 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009041static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009042{
Nadav Har'El644d7112011-05-25 23:12:35 +03009043 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9044 struct vcpu_vmx *vmx = to_vmx(vcpu);
9045 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9046
Jim Mattson4f350c62017-09-14 16:31:44 -07009047 if (vmx->nested.nested_run_pending)
9048 return false;
9049
9050 if (unlikely(vmx->fail)) {
9051 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9052 vmcs_read32(VM_INSTRUCTION_ERROR));
9053 return true;
9054 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009055
David Matlackc9f04402017-08-01 14:00:40 -07009056 /*
9057 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009058 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9059 * Page). The CPU may write to these pages via their host
9060 * physical address while L2 is running, bypassing any
9061 * address-translation-based dirty tracking (e.g. EPT write
9062 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009063 *
9064 * Mark them dirty on every exit from L2 to prevent them from
9065 * getting out of sync with dirty tracking.
9066 */
9067 nested_mark_vmcs12_pages_dirty(vcpu);
9068
Jim Mattson4f350c62017-09-14 16:31:44 -07009069 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9070 vmcs_readl(EXIT_QUALIFICATION),
9071 vmx->idt_vectoring_info,
9072 intr_info,
9073 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9074 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009075
9076 switch (exit_reason) {
9077 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009078 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009079 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009080 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009081 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009082 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009083 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009084 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009085 else if (is_debug(intr_info) &&
9086 vcpu->guest_debug &
9087 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9088 return false;
9089 else if (is_breakpoint(intr_info) &&
9090 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9091 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009092 return vmcs12->exception_bitmap &
9093 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9094 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009095 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009096 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009097 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009098 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009099 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009100 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009101 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009102 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009103 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009104 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009105 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009106 case EXIT_REASON_HLT:
9107 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9108 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009110 case EXIT_REASON_INVLPG:
9111 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9112 case EXIT_REASON_RDPMC:
9113 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009114 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009115 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009116 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009117 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009118 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009119 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9120 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9121 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9122 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9123 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9124 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009125 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009126 /*
9127 * VMX instructions trap unconditionally. This allows L1 to
9128 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9129 */
Joe Perches1d804d02015-03-30 16:46:09 -07009130 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009131 case EXIT_REASON_CR_ACCESS:
9132 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9133 case EXIT_REASON_DR_ACCESS:
9134 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9135 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009136 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009137 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9138 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009139 case EXIT_REASON_MSR_READ:
9140 case EXIT_REASON_MSR_WRITE:
9141 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9142 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009143 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009144 case EXIT_REASON_MWAIT_INSTRUCTION:
9145 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009146 case EXIT_REASON_MONITOR_TRAP_FLAG:
9147 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009148 case EXIT_REASON_MONITOR_INSTRUCTION:
9149 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9150 case EXIT_REASON_PAUSE_INSTRUCTION:
9151 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9152 nested_cpu_has2(vmcs12,
9153 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9154 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009155 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009156 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009157 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009158 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009159 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009160 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009161 /*
9162 * The controls for "virtualize APIC accesses," "APIC-
9163 * register virtualization," and "virtual-interrupt
9164 * delivery" only come from vmcs12.
9165 */
Joe Perches1d804d02015-03-30 16:46:09 -07009166 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009167 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009168 /*
9169 * L0 always deals with the EPT violation. If nested EPT is
9170 * used, and the nested mmu code discovers that the address is
9171 * missing in the guest EPT table (EPT12), the EPT violation
9172 * will be injected with nested_ept_inject_page_fault()
9173 */
Joe Perches1d804d02015-03-30 16:46:09 -07009174 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009175 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009176 /*
9177 * L2 never uses directly L1's EPT, but rather L0's own EPT
9178 * table (shadow on EPT) or a merged EPT table that L0 built
9179 * (EPT on EPT). So any problems with the structure of the
9180 * table is L0's fault.
9181 */
Joe Perches1d804d02015-03-30 16:46:09 -07009182 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009183 case EXIT_REASON_INVPCID:
9184 return
9185 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9186 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 case EXIT_REASON_WBINVD:
9188 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9189 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009190 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009191 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9192 /*
9193 * This should never happen, since it is not possible to
9194 * set XSS to a non-zero value---neither in L1 nor in L2.
9195 * If if it were, XSS would have to be checked against
9196 * the XSS exit bitmap in vmcs12.
9197 */
9198 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009199 case EXIT_REASON_PREEMPTION_TIMER:
9200 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009201 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009202 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009203 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009204 case EXIT_REASON_VMFUNC:
9205 /* VM functions are emulated through L2->L0 vmexits. */
9206 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009207 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009208 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009209 }
9210}
9211
Paolo Bonzini7313c692017-07-27 10:31:25 +02009212static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9213{
9214 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9215
9216 /*
9217 * At this point, the exit interruption info in exit_intr_info
9218 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9219 * we need to query the in-kernel LAPIC.
9220 */
9221 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9222 if ((exit_intr_info &
9223 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9224 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9226 vmcs12->vm_exit_intr_error_code =
9227 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9228 }
9229
9230 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9231 vmcs_readl(EXIT_QUALIFICATION));
9232 return 1;
9233}
9234
Avi Kivity586f9602010-11-18 13:09:54 +02009235static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9236{
9237 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9238 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9239}
9240
Kai Huanga3eaa862015-11-04 13:46:05 +08009241static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009242{
Kai Huanga3eaa862015-11-04 13:46:05 +08009243 if (vmx->pml_pg) {
9244 __free_page(vmx->pml_pg);
9245 vmx->pml_pg = NULL;
9246 }
Kai Huang843e4332015-01-28 10:54:28 +08009247}
9248
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009249static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009250{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009252 u64 *pml_buf;
9253 u16 pml_idx;
9254
9255 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9256
9257 /* Do nothing if PML buffer is empty */
9258 if (pml_idx == (PML_ENTITY_NUM - 1))
9259 return;
9260
9261 /* PML index always points to next available PML buffer entity */
9262 if (pml_idx >= PML_ENTITY_NUM)
9263 pml_idx = 0;
9264 else
9265 pml_idx++;
9266
9267 pml_buf = page_address(vmx->pml_pg);
9268 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9269 u64 gpa;
9270
9271 gpa = pml_buf[pml_idx];
9272 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009273 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009274 }
9275
9276 /* reset PML index */
9277 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9278}
9279
9280/*
9281 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9282 * Called before reporting dirty_bitmap to userspace.
9283 */
9284static void kvm_flush_pml_buffers(struct kvm *kvm)
9285{
9286 int i;
9287 struct kvm_vcpu *vcpu;
9288 /*
9289 * We only need to kick vcpu out of guest mode here, as PML buffer
9290 * is flushed at beginning of all VMEXITs, and it's obvious that only
9291 * vcpus running in guest are possible to have unflushed GPAs in PML
9292 * buffer.
9293 */
9294 kvm_for_each_vcpu(i, vcpu, kvm)
9295 kvm_vcpu_kick(vcpu);
9296}
9297
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009298static void vmx_dump_sel(char *name, uint32_t sel)
9299{
9300 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009301 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009302 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9303 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9304 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9305}
9306
9307static void vmx_dump_dtsel(char *name, uint32_t limit)
9308{
9309 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9310 name, vmcs_read32(limit),
9311 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9312}
9313
9314static void dump_vmcs(void)
9315{
9316 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9317 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9318 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9319 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9320 u32 secondary_exec_control = 0;
9321 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009322 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009323 int i, n;
9324
9325 if (cpu_has_secondary_exec_ctrls())
9326 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9327
9328 pr_err("*** Guest State ***\n");
9329 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9330 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9331 vmcs_readl(CR0_GUEST_HOST_MASK));
9332 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9333 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9334 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9335 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9336 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9337 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009338 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9339 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9340 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9341 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009342 }
9343 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9344 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9345 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9346 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9347 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9348 vmcs_readl(GUEST_SYSENTER_ESP),
9349 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9350 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9351 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9352 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9353 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9354 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9355 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9356 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9357 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9358 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9359 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9360 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9361 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009362 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9363 efer, vmcs_read64(GUEST_IA32_PAT));
9364 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9365 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009366 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009367 if (cpu_has_load_perf_global_ctrl &&
9368 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009369 pr_err("PerfGlobCtl = 0x%016llx\n",
9370 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009371 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009372 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009373 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9374 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9375 vmcs_read32(GUEST_ACTIVITY_STATE));
9376 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9377 pr_err("InterruptStatus = %04x\n",
9378 vmcs_read16(GUEST_INTR_STATUS));
9379
9380 pr_err("*** Host State ***\n");
9381 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9382 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9383 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9384 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9385 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9386 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9387 vmcs_read16(HOST_TR_SELECTOR));
9388 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9389 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9390 vmcs_readl(HOST_TR_BASE));
9391 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9392 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9393 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9394 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9395 vmcs_readl(HOST_CR4));
9396 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9397 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9398 vmcs_read32(HOST_IA32_SYSENTER_CS),
9399 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9400 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009401 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9402 vmcs_read64(HOST_IA32_EFER),
9403 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009404 if (cpu_has_load_perf_global_ctrl &&
9405 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009406 pr_err("PerfGlobCtl = 0x%016llx\n",
9407 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009408
9409 pr_err("*** Control State ***\n");
9410 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9411 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9412 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9413 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9414 vmcs_read32(EXCEPTION_BITMAP),
9415 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9416 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9417 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9418 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9419 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9420 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9421 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9422 vmcs_read32(VM_EXIT_INTR_INFO),
9423 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9424 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9425 pr_err(" reason=%08x qualification=%016lx\n",
9426 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9427 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9428 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9429 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009430 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009431 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009432 pr_err("TSC Multiplier = 0x%016llx\n",
9433 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009434 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9435 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9436 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9437 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9438 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009439 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009440 n = vmcs_read32(CR3_TARGET_COUNT);
9441 for (i = 0; i + 1 < n; i += 4)
9442 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9443 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9444 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9445 if (i < n)
9446 pr_err("CR3 target%u=%016lx\n",
9447 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9448 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9449 pr_err("PLE Gap=%08x Window=%08x\n",
9450 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9451 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9452 pr_err("Virtual processor ID = 0x%04x\n",
9453 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9454}
9455
Avi Kivity6aa8b732006-12-10 02:21:36 -08009456/*
9457 * The guest has exited. See if we can fix it or if we need userspace
9458 * assistance.
9459 */
Avi Kivity851ba692009-08-24 11:10:17 +03009460static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009461{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009462 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009463 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009464 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009465
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009466 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9467
Kai Huang843e4332015-01-28 10:54:28 +08009468 /*
9469 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9470 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9471 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9472 * mode as if vcpus is in root mode, the PML buffer must has been
9473 * flushed already.
9474 */
9475 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009476 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009477
Mohammed Gamal80ced182009-09-01 12:48:18 +02009478 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009479 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009480 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009481
Paolo Bonzini7313c692017-07-27 10:31:25 +02009482 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9483 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009484
Mohammed Gamal51207022010-05-31 22:40:54 +03009485 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009486 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009487 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9488 vcpu->run->fail_entry.hardware_entry_failure_reason
9489 = exit_reason;
9490 return 0;
9491 }
9492
Avi Kivity29bd8a72007-09-10 17:27:03 +03009493 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009494 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9495 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009496 = vmcs_read32(VM_INSTRUCTION_ERROR);
9497 return 0;
9498 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009499
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009500 /*
9501 * Note:
9502 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9503 * delivery event since it indicates guest is accessing MMIO.
9504 * The vm-exit can be triggered again after return to guest that
9505 * will cause infinite loop.
9506 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009507 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009508 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009509 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009510 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009511 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9512 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9513 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009514 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009515 vcpu->run->internal.data[0] = vectoring_info;
9516 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009517 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9518 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9519 vcpu->run->internal.ndata++;
9520 vcpu->run->internal.data[3] =
9521 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9522 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009523 return 0;
9524 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009525
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009526 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009527 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9528 if (vmx_interrupt_allowed(vcpu)) {
9529 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9530 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9531 vcpu->arch.nmi_pending) {
9532 /*
9533 * This CPU don't support us in finding the end of an
9534 * NMI-blocked window if the guest runs with IRQs
9535 * disabled. So we pull the trigger after 1 s of
9536 * futile waiting, but inform the user about this.
9537 */
9538 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9539 "state on VCPU %d after 1 s timeout\n",
9540 __func__, vcpu->vcpu_id);
9541 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9542 }
9543 }
9544
Avi Kivity6aa8b732006-12-10 02:21:36 -08009545 if (exit_reason < kvm_vmx_max_exit_handlers
9546 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009547 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009548 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009549 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9550 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009551 kvm_queue_exception(vcpu, UD_VECTOR);
9552 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009553 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009554}
9555
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009556static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009557{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009558 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9559
9560 if (is_guest_mode(vcpu) &&
9561 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9562 return;
9563
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009564 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009565 vmcs_write32(TPR_THRESHOLD, 0);
9566 return;
9567 }
9568
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009569 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009570}
9571
Jim Mattson8d860bb2018-05-09 16:56:05 -04009572static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009573{
9574 u32 sec_exec_control;
9575
Jim Mattson8d860bb2018-05-09 16:56:05 -04009576 if (!lapic_in_kernel(vcpu))
9577 return;
9578
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009579 /* Postpone execution until vmcs01 is the current VMCS. */
9580 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009581 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009582 return;
9583 }
9584
Paolo Bonzini35754c92015-07-29 12:05:37 +02009585 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009586 return;
9587
9588 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009589 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9590 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009591
Jim Mattson8d860bb2018-05-09 16:56:05 -04009592 switch (kvm_get_apic_mode(vcpu)) {
9593 case LAPIC_MODE_INVALID:
9594 WARN_ONCE(true, "Invalid local APIC state");
9595 case LAPIC_MODE_DISABLED:
9596 break;
9597 case LAPIC_MODE_XAPIC:
9598 if (flexpriority_enabled) {
9599 sec_exec_control |=
9600 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9601 vmx_flush_tlb(vcpu, true);
9602 }
9603 break;
9604 case LAPIC_MODE_X2APIC:
9605 if (cpu_has_vmx_virtualize_x2apic_mode())
9606 sec_exec_control |=
9607 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9608 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009609 }
9610 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9611
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009612 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009613}
9614
Tang Chen38b99172014-09-24 15:57:54 +08009615static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9616{
Jim Mattsonab5df312018-05-09 17:02:03 -04009617 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009618 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009619 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009620 }
Tang Chen38b99172014-09-24 15:57:54 +08009621}
9622
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009623static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009624{
9625 u16 status;
9626 u8 old;
9627
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009628 if (max_isr == -1)
9629 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009630
9631 status = vmcs_read16(GUEST_INTR_STATUS);
9632 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009633 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009634 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009635 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009636 vmcs_write16(GUEST_INTR_STATUS, status);
9637 }
9638}
9639
9640static void vmx_set_rvi(int vector)
9641{
9642 u16 status;
9643 u8 old;
9644
Wei Wang4114c272014-11-05 10:53:43 +08009645 if (vector == -1)
9646 vector = 0;
9647
Yang Zhangc7c9c562013-01-25 10:18:51 +08009648 status = vmcs_read16(GUEST_INTR_STATUS);
9649 old = (u8)status & 0xff;
9650 if ((u8)vector != old) {
9651 status &= ~0xff;
9652 status |= (u8)vector;
9653 vmcs_write16(GUEST_INTR_STATUS, status);
9654 }
9655}
9656
9657static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9658{
Liran Alon851c1a182017-12-24 18:12:56 +02009659 /*
9660 * When running L2, updating RVI is only relevant when
9661 * vmcs12 virtual-interrupt-delivery enabled.
9662 * However, it can be enabled only when L1 also
9663 * intercepts external-interrupts and in that case
9664 * we should not update vmcs02 RVI but instead intercept
9665 * interrupt. Therefore, do nothing when running L2.
9666 */
9667 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009668 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009669}
9670
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009671static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009672{
9673 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009674 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009675 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009676
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009677 WARN_ON(!vcpu->arch.apicv_active);
9678 if (pi_test_on(&vmx->pi_desc)) {
9679 pi_clear_on(&vmx->pi_desc);
9680 /*
9681 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9682 * But on x86 this is just a compiler barrier anyway.
9683 */
9684 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009685 max_irr_updated =
9686 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9687
9688 /*
9689 * If we are running L2 and L1 has a new pending interrupt
9690 * which can be injected, we should re-evaluate
9691 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009692 * If L1 intercepts external-interrupts, we should
9693 * exit from L2 to L1. Otherwise, interrupt should be
9694 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009695 */
Liran Alon851c1a182017-12-24 18:12:56 +02009696 if (is_guest_mode(vcpu) && max_irr_updated) {
9697 if (nested_exit_on_intr(vcpu))
9698 kvm_vcpu_exiting_guest_mode(vcpu);
9699 else
9700 kvm_make_request(KVM_REQ_EVENT, vcpu);
9701 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009702 } else {
9703 max_irr = kvm_lapic_find_highest_irr(vcpu);
9704 }
9705 vmx_hwapic_irr_update(vcpu, max_irr);
9706 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009707}
9708
Andrey Smetanin63086302015-11-10 15:36:32 +03009709static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009710{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009711 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009712 return;
9713
Yang Zhangc7c9c562013-01-25 10:18:51 +08009714 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9715 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9716 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9717 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9718}
9719
Paolo Bonzini967235d2016-12-19 14:03:45 +01009720static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9721{
9722 struct vcpu_vmx *vmx = to_vmx(vcpu);
9723
9724 pi_clear_on(&vmx->pi_desc);
9725 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9726}
9727
Avi Kivity51aa01d2010-07-20 14:31:20 +03009728static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009729{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009730 u32 exit_intr_info = 0;
9731 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009732
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009733 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9734 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009735 return;
9736
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009737 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9738 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9739 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009740
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009741 /* if exit due to PF check for async PF */
9742 if (is_page_fault(exit_intr_info))
9743 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9744
Andi Kleena0861c02009-06-08 17:37:09 +08009745 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009746 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9747 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009748 kvm_machine_check();
9749
Gleb Natapov20f65982009-05-11 13:35:55 +03009750 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009751 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009752 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009753 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009754 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009755 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009756}
Gleb Natapov20f65982009-05-11 13:35:55 +03009757
Yang Zhanga547c6d2013-04-11 19:25:10 +08009758static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9759{
9760 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9761
Yang Zhanga547c6d2013-04-11 19:25:10 +08009762 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9763 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9764 unsigned int vector;
9765 unsigned long entry;
9766 gate_desc *desc;
9767 struct vcpu_vmx *vmx = to_vmx(vcpu);
9768#ifdef CONFIG_X86_64
9769 unsigned long tmp;
9770#endif
9771
9772 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9773 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009774 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009775 asm volatile(
9776#ifdef CONFIG_X86_64
9777 "mov %%" _ASM_SP ", %[sp]\n\t"
9778 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9779 "push $%c[ss]\n\t"
9780 "push %[sp]\n\t"
9781#endif
9782 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009783 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009784 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009785 :
9786#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009787 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009788#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009789 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009790 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009791 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009792 [ss]"i"(__KERNEL_DS),
9793 [cs]"i"(__KERNEL_CS)
9794 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009795 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009796}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009797STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009798
Tom Lendackybc226f02018-05-10 22:06:39 +02009799static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009800{
Tom Lendackybc226f02018-05-10 22:06:39 +02009801 switch (index) {
9802 case MSR_IA32_SMBASE:
9803 /*
9804 * We cannot do SMM unless we can run the guest in big
9805 * real mode.
9806 */
9807 return enable_unrestricted_guest || emulate_invalid_guest_state;
9808 case MSR_AMD64_VIRT_SPEC_CTRL:
9809 /* This is AMD only. */
9810 return false;
9811 default:
9812 return true;
9813 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009814}
9815
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009816static bool vmx_mpx_supported(void)
9817{
9818 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9819 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9820}
9821
Wanpeng Li55412b22014-12-02 19:21:30 +08009822static bool vmx_xsaves_supported(void)
9823{
9824 return vmcs_config.cpu_based_2nd_exec_ctrl &
9825 SECONDARY_EXEC_XSAVES;
9826}
9827
Avi Kivity51aa01d2010-07-20 14:31:20 +03009828static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9829{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009830 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009831 bool unblock_nmi;
9832 u8 vector;
9833 bool idtv_info_valid;
9834
9835 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009836
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009837 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009838 if (vmx->loaded_vmcs->nmi_known_unmasked)
9839 return;
9840 /*
9841 * Can't use vmx->exit_intr_info since we're not sure what
9842 * the exit reason is.
9843 */
9844 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9845 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9846 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9847 /*
9848 * SDM 3: 27.7.1.2 (September 2008)
9849 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9850 * a guest IRET fault.
9851 * SDM 3: 23.2.2 (September 2008)
9852 * Bit 12 is undefined in any of the following cases:
9853 * If the VM exit sets the valid bit in the IDT-vectoring
9854 * information field.
9855 * If the VM exit is due to a double fault.
9856 */
9857 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9858 vector != DF_VECTOR && !idtv_info_valid)
9859 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9860 GUEST_INTR_STATE_NMI);
9861 else
9862 vmx->loaded_vmcs->nmi_known_unmasked =
9863 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9864 & GUEST_INTR_STATE_NMI);
9865 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9866 vmx->loaded_vmcs->vnmi_blocked_time +=
9867 ktime_to_ns(ktime_sub(ktime_get(),
9868 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009869}
9870
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009871static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009872 u32 idt_vectoring_info,
9873 int instr_len_field,
9874 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009875{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009876 u8 vector;
9877 int type;
9878 bool idtv_info_valid;
9879
9880 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009881
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009882 vcpu->arch.nmi_injected = false;
9883 kvm_clear_exception_queue(vcpu);
9884 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009885
9886 if (!idtv_info_valid)
9887 return;
9888
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009889 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009890
Avi Kivity668f6122008-07-02 09:28:55 +03009891 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9892 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009893
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009894 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009895 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009896 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009897 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009898 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009899 * Clear bit "block by NMI" before VM entry if a NMI
9900 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009901 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009902 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009903 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009904 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009905 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009906 /* fall through */
9907 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009908 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009909 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009910 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009911 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009912 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009913 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009914 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009915 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009916 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009917 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009918 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009919 break;
9920 default:
9921 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009922 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009923}
9924
Avi Kivity83422e12010-07-20 14:43:23 +03009925static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9926{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009927 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009928 VM_EXIT_INSTRUCTION_LEN,
9929 IDT_VECTORING_ERROR_CODE);
9930}
9931
Avi Kivityb463a6f2010-07-20 15:06:17 +03009932static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9933{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009934 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009935 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9936 VM_ENTRY_INSTRUCTION_LEN,
9937 VM_ENTRY_EXCEPTION_ERROR_CODE);
9938
9939 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9940}
9941
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009942static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9943{
9944 int i, nr_msrs;
9945 struct perf_guest_switch_msr *msrs;
9946
9947 msrs = perf_guest_get_msrs(&nr_msrs);
9948
9949 if (!msrs)
9950 return;
9951
9952 for (i = 0; i < nr_msrs; i++)
9953 if (msrs[i].host == msrs[i].guest)
9954 clear_atomic_switch_msr(vmx, msrs[i].msr);
9955 else
9956 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9957 msrs[i].host);
9958}
9959
Jiang Biao33365e72016-11-03 15:03:37 +08009960static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009961{
9962 struct vcpu_vmx *vmx = to_vmx(vcpu);
9963 u64 tscl;
9964 u32 delta_tsc;
9965
9966 if (vmx->hv_deadline_tsc == -1)
9967 return;
9968
9969 tscl = rdtsc();
9970 if (vmx->hv_deadline_tsc > tscl)
9971 /* sure to be 32 bit only because checked on set_hv_timer */
9972 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9973 cpu_preemption_timer_multi);
9974 else
9975 delta_tsc = 0;
9976
9977 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9978}
9979
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009980static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009981{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009983 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009984
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009985 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009986 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009987 vmx->loaded_vmcs->soft_vnmi_blocked))
9988 vmx->loaded_vmcs->entry_time = ktime_get();
9989
Avi Kivity104f2262010-11-18 13:12:52 +02009990 /* Don't enter VMX if guest state is invalid, let the exit handler
9991 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009992 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009993 return;
9994
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009995 if (vmx->ple_window_dirty) {
9996 vmx->ple_window_dirty = false;
9997 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9998 }
9999
Abel Gordon012f83c2013-04-18 14:39:25 +030010000 if (vmx->nested.sync_shadow_vmcs) {
10001 copy_vmcs12_to_shadow(vmx);
10002 vmx->nested.sync_shadow_vmcs = false;
10003 }
10004
Avi Kivity104f2262010-11-18 13:12:52 +020010005 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10006 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10007 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10008 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10009
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010010 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010011 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010012 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010013 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010014 }
10015
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010016 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010017 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010018 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010019 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010020 }
10021
Avi Kivity104f2262010-11-18 13:12:52 +020010022 /* When single-stepping over STI and MOV SS, we must clear the
10023 * corresponding interruptibility bits in the guest state. Otherwise
10024 * vmentry fails as it then expects bit 14 (BS) in pending debug
10025 * exceptions being set, but that's not correct for the guest debugging
10026 * case. */
10027 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10028 vmx_set_interrupt_shadow(vcpu, 0);
10029
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010030 if (static_cpu_has(X86_FEATURE_PKU) &&
10031 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10032 vcpu->arch.pkru != vmx->host_pkru)
10033 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010034
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010035 atomic_switch_perf_msrs(vmx);
10036
Yunhong Jiang64672c92016-06-13 14:19:59 -070010037 vmx_arm_hv_timer(vcpu);
10038
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010039 /*
10040 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10041 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10042 * is no need to worry about the conditional branch over the wrmsr
10043 * being speculatively taken.
10044 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010045 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010046
Nadav Har'Eld462b812011-05-24 15:26:10 +030010047 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010048
10049 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10050 (unsigned long)&current_evmcs->host_rsp : 0;
10051
Avi Kivity104f2262010-11-18 13:12:52 +020010052 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010053 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010054 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10055 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10056 "push %%" _ASM_CX " \n\t"
10057 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010058 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010059 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010060 /* Avoid VMWRITE when Enlightened VMCS is in use */
10061 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10062 "jz 2f \n\t"
10063 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10064 "jmp 1f \n\t"
10065 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010066 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010067 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010068 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010069 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10070 "mov %%cr2, %%" _ASM_DX " \n\t"
10071 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010072 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010073 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010074 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010075 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010076 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010077 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010078 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10079 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10080 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10081 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10082 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10083 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010084#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010085 "mov %c[r8](%0), %%r8 \n\t"
10086 "mov %c[r9](%0), %%r9 \n\t"
10087 "mov %c[r10](%0), %%r10 \n\t"
10088 "mov %c[r11](%0), %%r11 \n\t"
10089 "mov %c[r12](%0), %%r12 \n\t"
10090 "mov %c[r13](%0), %%r13 \n\t"
10091 "mov %c[r14](%0), %%r14 \n\t"
10092 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010093#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010094 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010095
Avi Kivity6aa8b732006-12-10 02:21:36 -080010096 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010097 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010098 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010099 "jmp 2f \n\t"
10100 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10101 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010102 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010103 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010104 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010105 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010106 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10107 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10108 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10109 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10110 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10111 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10112 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010113#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010114 "mov %%r8, %c[r8](%0) \n\t"
10115 "mov %%r9, %c[r9](%0) \n\t"
10116 "mov %%r10, %c[r10](%0) \n\t"
10117 "mov %%r11, %c[r11](%0) \n\t"
10118 "mov %%r12, %c[r12](%0) \n\t"
10119 "mov %%r13, %c[r13](%0) \n\t"
10120 "mov %%r14, %c[r14](%0) \n\t"
10121 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010122 "xor %%r8d, %%r8d \n\t"
10123 "xor %%r9d, %%r9d \n\t"
10124 "xor %%r10d, %%r10d \n\t"
10125 "xor %%r11d, %%r11d \n\t"
10126 "xor %%r12d, %%r12d \n\t"
10127 "xor %%r13d, %%r13d \n\t"
10128 "xor %%r14d, %%r14d \n\t"
10129 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010130#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010131 "mov %%cr2, %%" _ASM_AX " \n\t"
10132 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010133
Jim Mattson0cb5b302018-01-03 14:31:38 -080010134 "xor %%eax, %%eax \n\t"
10135 "xor %%ebx, %%ebx \n\t"
10136 "xor %%esi, %%esi \n\t"
10137 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010138 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010139 ".pushsection .rodata \n\t"
10140 ".global vmx_return \n\t"
10141 "vmx_return: " _ASM_PTR " 2b \n\t"
10142 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010143 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010144 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010145 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010146 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010147 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10148 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10149 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10150 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10151 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10152 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10153 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010154#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010155 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10156 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10157 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10158 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10159 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10160 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10161 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10162 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010163#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010164 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10165 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010166 : "cc", "memory"
10167#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010168 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010169 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010170#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010171 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010172#endif
10173 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010174
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010175 /*
10176 * We do not use IBRS in the kernel. If this vCPU has used the
10177 * SPEC_CTRL MSR it may have left it on; save the value and
10178 * turn it off. This is much more efficient than blindly adding
10179 * it to the atomic save/restore list. Especially as the former
10180 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10181 *
10182 * For non-nested case:
10183 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10184 * save it.
10185 *
10186 * For nested case:
10187 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10188 * save it.
10189 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010190 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010191 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010192
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010193 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010194
David Woodhouse117cc7a2018-01-12 11:11:27 +000010195 /* Eliminate branch target predictions from guest mode */
10196 vmexit_fill_RSB();
10197
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010198 /* All fields are clean at this point */
10199 if (static_branch_unlikely(&enable_evmcs))
10200 current_evmcs->hv_clean_fields |=
10201 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10202
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010203 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010204 if (vmx->host_debugctlmsr)
10205 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010206
Avi Kivityaa67f602012-08-01 16:48:03 +030010207#ifndef CONFIG_X86_64
10208 /*
10209 * The sysexit path does not restore ds/es, so we must set them to
10210 * a reasonable value ourselves.
10211 *
10212 * We can't defer this to vmx_load_host_state() since that function
10213 * may be executed in interrupt context, which saves and restore segments
10214 * around it, nullifying its effect.
10215 */
10216 loadsegment(ds, __USER_DS);
10217 loadsegment(es, __USER_DS);
10218#endif
10219
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010220 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010221 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010222 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010223 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010224 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010225 vcpu->arch.regs_dirty = 0;
10226
Gleb Natapove0b890d2013-09-25 12:51:33 +030010227 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010228 * eager fpu is enabled if PKEY is supported and CR4 is switched
10229 * back on host, so it is safe to read guest PKRU from current
10230 * XSAVE.
10231 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010232 if (static_cpu_has(X86_FEATURE_PKU) &&
10233 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10234 vcpu->arch.pkru = __read_pkru();
10235 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010236 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010237 }
10238
Gleb Natapove0b890d2013-09-25 12:51:33 +030010239 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010240 vmx->idt_vectoring_info = 0;
10241
10242 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10243 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10244 return;
10245
10246 vmx->loaded_vmcs->launched = 1;
10247 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010248
Avi Kivity51aa01d2010-07-20 14:31:20 +030010249 vmx_complete_atomic_exit(vmx);
10250 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010251 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010252}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010253STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010254
Sean Christopherson434a1e92018-03-20 12:17:18 -070010255static struct kvm *vmx_vm_alloc(void)
10256{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010257 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010258 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010259}
10260
10261static void vmx_vm_free(struct kvm *kvm)
10262{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010263 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010264}
10265
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010266static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010267{
10268 struct vcpu_vmx *vmx = to_vmx(vcpu);
10269 int cpu;
10270
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010271 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010272 return;
10273
10274 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010275 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010276 vmx_vcpu_put(vcpu);
10277 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010278 put_cpu();
10279}
10280
Jim Mattson2f1fe812016-07-08 15:36:06 -070010281/*
10282 * Ensure that the current vmcs of the logical processor is the
10283 * vmcs01 of the vcpu before calling free_nested().
10284 */
10285static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10286{
10287 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010288
Christoffer Dallec7660c2017-12-04 21:35:23 +010010289 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010290 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010291 free_nested(vmx);
10292 vcpu_put(vcpu);
10293}
10294
Avi Kivity6aa8b732006-12-10 02:21:36 -080010295static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10296{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010297 struct vcpu_vmx *vmx = to_vmx(vcpu);
10298
Kai Huang843e4332015-01-28 10:54:28 +080010299 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010300 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010301 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010302 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010303 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010304 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010305 kfree(vmx->guest_msrs);
10306 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010307 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010308}
10309
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010310static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010311{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010312 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010313 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010314 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010315 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010316
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010317 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010318 return ERR_PTR(-ENOMEM);
10319
Wanpeng Li991e7a02015-09-16 17:30:05 +080010320 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010321
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010322 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10323 if (err)
10324 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010325
Peter Feiner4e595162016-07-07 14:49:58 -070010326 err = -ENOMEM;
10327
10328 /*
10329 * If PML is turned on, failure on enabling PML just results in failure
10330 * of creating the vcpu, therefore we can simplify PML logic (by
10331 * avoiding dealing with cases, such as enabling PML partially on vcpus
10332 * for the guest, etc.
10333 */
10334 if (enable_pml) {
10335 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10336 if (!vmx->pml_pg)
10337 goto uninit_vcpu;
10338 }
10339
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010340 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010341 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10342 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010343
Peter Feiner4e595162016-07-07 14:49:58 -070010344 if (!vmx->guest_msrs)
10345 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010346
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010347 err = alloc_loaded_vmcs(&vmx->vmcs01);
10348 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010349 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010350
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010351 msr_bitmap = vmx->vmcs01.msr_bitmap;
10352 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10353 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10354 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10355 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10356 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10357 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10358 vmx->msr_bitmap_mode = 0;
10359
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010360 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010361 cpu = get_cpu();
10362 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010363 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010364 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010365 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010366 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010367 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010368 err = alloc_apic_access_page(kvm);
10369 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010370 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010371 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010372
Sean Christophersone90008d2018-03-05 12:04:37 -080010373 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010374 err = init_rmode_identity_map(kvm);
10375 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010376 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010377 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010378
Wanpeng Li5c614b32015-10-13 09:18:36 -070010379 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010380 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10381 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010382 vmx->nested.vpid02 = allocate_vpid();
10383 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010384
Wincy Van705699a2015-02-03 23:58:17 +080010385 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010386 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010387
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010388 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10389
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010390 /*
10391 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10392 * or POSTED_INTR_WAKEUP_VECTOR.
10393 */
10394 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10395 vmx->pi_desc.sn = 1;
10396
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010397 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010398
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010399free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010400 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010401 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010402free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010403 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010404free_pml:
10405 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010406uninit_vcpu:
10407 kvm_vcpu_uninit(&vmx->vcpu);
10408free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010409 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010410 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010411 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010412}
10413
Wanpeng Lib31c1142018-03-12 04:53:04 -070010414static int vmx_vm_init(struct kvm *kvm)
10415{
10416 if (!ple_gap)
10417 kvm->arch.pause_in_guest = true;
10418 return 0;
10419}
10420
Yang, Sheng002c7f72007-07-31 14:23:01 +030010421static void __init vmx_check_processor_compat(void *rtn)
10422{
10423 struct vmcs_config vmcs_conf;
10424
10425 *(int *)rtn = 0;
10426 if (setup_vmcs_config(&vmcs_conf) < 0)
10427 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010428 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010429 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10430 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10431 smp_processor_id());
10432 *(int *)rtn = -EIO;
10433 }
10434}
10435
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010436static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010437{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010438 u8 cache;
10439 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010440
Sheng Yang522c68c2009-04-27 20:35:43 +080010441 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010442 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010443 * 2. EPT with VT-d:
10444 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010445 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010446 * b. VT-d with snooping control feature: snooping control feature of
10447 * VT-d engine can guarantee the cache correctness. Just set it
10448 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010449 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010450 * consistent with host MTRR
10451 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010452 if (is_mmio) {
10453 cache = MTRR_TYPE_UNCACHABLE;
10454 goto exit;
10455 }
10456
10457 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010458 ipat = VMX_EPT_IPAT_BIT;
10459 cache = MTRR_TYPE_WRBACK;
10460 goto exit;
10461 }
10462
10463 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10464 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010465 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010466 cache = MTRR_TYPE_WRBACK;
10467 else
10468 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010469 goto exit;
10470 }
10471
Xiao Guangrongff536042015-06-15 16:55:22 +080010472 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010473
10474exit:
10475 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010476}
10477
Sheng Yang17cc3932010-01-05 19:02:27 +080010478static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010479{
Sheng Yang878403b2010-01-05 19:02:29 +080010480 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10481 return PT_DIRECTORY_LEVEL;
10482 else
10483 /* For shadow and EPT supported 1GB page */
10484 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010485}
10486
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010487static void vmcs_set_secondary_exec_control(u32 new_ctl)
10488{
10489 /*
10490 * These bits in the secondary execution controls field
10491 * are dynamic, the others are mostly based on the hypervisor
10492 * architecture and the guest's CPUID. Do not touch the
10493 * dynamic bits.
10494 */
10495 u32 mask =
10496 SECONDARY_EXEC_SHADOW_VMCS |
10497 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010498 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10499 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010500
10501 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10502
10503 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10504 (new_ctl & ~mask) | (cur_ctl & mask));
10505}
10506
David Matlack8322ebb2016-11-29 18:14:09 -080010507/*
10508 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10509 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10510 */
10511static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10512{
10513 struct vcpu_vmx *vmx = to_vmx(vcpu);
10514 struct kvm_cpuid_entry2 *entry;
10515
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010516 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10517 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010518
10519#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10520 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010521 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010522} while (0)
10523
10524 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10525 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10526 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10527 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10528 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10529 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10530 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10531 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10532 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10533 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10534 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10535 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10536 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10537 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10538 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10539
10540 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10541 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10542 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10543 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10544 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010545 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010546
10547#undef cr4_fixed1_update
10548}
10549
Sheng Yang0e851882009-12-18 16:48:46 +080010550static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10551{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010552 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010553
Paolo Bonzini80154d72017-08-24 13:55:35 +020010554 if (cpu_has_secondary_exec_ctrls()) {
10555 vmx_compute_secondary_exec_control(vmx);
10556 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010557 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010558
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010559 if (nested_vmx_allowed(vcpu))
10560 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10561 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10562 else
10563 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10564 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010565
10566 if (nested_vmx_allowed(vcpu))
10567 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010568}
10569
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010570static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10571{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010572 if (func == 1 && nested)
10573 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010574}
10575
Yang Zhang25d92082013-08-06 12:00:32 +030010576static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10577 struct x86_exception *fault)
10578{
Jan Kiszka533558b2014-01-04 18:47:20 +010010579 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010580 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010581 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010582 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010583
Bandan Dasc5f983f2017-05-05 15:25:14 -040010584 if (vmx->nested.pml_full) {
10585 exit_reason = EXIT_REASON_PML_FULL;
10586 vmx->nested.pml_full = false;
10587 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10588 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010589 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010590 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010591 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010592
10593 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010594 vmcs12->guest_physical_address = fault->address;
10595}
10596
Peter Feiner995f00a2017-06-30 17:26:32 -070010597static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10598{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010599 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010600}
10601
Nadav Har'El155a97a2013-08-05 11:07:16 +030010602/* Callbacks for nested_ept_init_mmu_context: */
10603
10604static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10605{
10606 /* return the page table to be shadowed - in our case, EPT12 */
10607 return get_vmcs12(vcpu)->ept_pointer;
10608}
10609
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010610static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010611{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010612 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010613 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010614 return 1;
10615
10616 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010617 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010618 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010619 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010620 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010621 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10622 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10623 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10624
10625 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010626 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010627}
10628
10629static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10630{
10631 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10632}
10633
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010634static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10635 u16 error_code)
10636{
10637 bool inequality, bit;
10638
10639 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10640 inequality =
10641 (error_code & vmcs12->page_fault_error_code_mask) !=
10642 vmcs12->page_fault_error_code_match;
10643 return inequality ^ bit;
10644}
10645
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010646static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10647 struct x86_exception *fault)
10648{
10649 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10650
10651 WARN_ON(!is_guest_mode(vcpu));
10652
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010653 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10654 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010655 vmcs12->vm_exit_intr_error_code = fault->error_code;
10656 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10657 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10658 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10659 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010660 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010661 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010662 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010663}
10664
Paolo Bonzinic9923842017-12-13 14:16:30 +010010665static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10666 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010667
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010668static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010669{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010670 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010671 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010672 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010673 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010674
10675 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010676 /*
10677 * Translate L1 physical address to host physical
10678 * address for vmcs02. Keep the page pinned, so this
10679 * physical address remains valid. We keep a reference
10680 * to it so we can release it later.
10681 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010682 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010683 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010684 vmx->nested.apic_access_page = NULL;
10685 }
10686 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010687 /*
10688 * If translation failed, no matter: This feature asks
10689 * to exit when accessing the given address, and if it
10690 * can never be accessed, this feature won't do
10691 * anything anyway.
10692 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010693 if (!is_error_page(page)) {
10694 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010695 hpa = page_to_phys(vmx->nested.apic_access_page);
10696 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10697 } else {
10698 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10699 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10700 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010701 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010702
10703 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010704 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010705 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010706 vmx->nested.virtual_apic_page = NULL;
10707 }
10708 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010709
10710 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010711 * If translation failed, VM entry will fail because
10712 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10713 * Failing the vm entry is _not_ what the processor
10714 * does but it's basically the only possibility we
10715 * have. We could still enter the guest if CR8 load
10716 * exits are enabled, CR8 store exits are enabled, and
10717 * virtualize APIC access is disabled; in this case
10718 * the processor would never use the TPR shadow and we
10719 * could simply clear the bit from the execution
10720 * control. But such a configuration is useless, so
10721 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010722 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010723 if (!is_error_page(page)) {
10724 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010725 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10726 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10727 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010728 }
10729
Wincy Van705699a2015-02-03 23:58:17 +080010730 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010731 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10732 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010733 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010734 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010735 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010736 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10737 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010738 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010739 vmx->nested.pi_desc_page = page;
10740 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010741 vmx->nested.pi_desc =
10742 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10743 (unsigned long)(vmcs12->posted_intr_desc_addr &
10744 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010745 vmcs_write64(POSTED_INTR_DESC_ADDR,
10746 page_to_phys(vmx->nested.pi_desc_page) +
10747 (unsigned long)(vmcs12->posted_intr_desc_addr &
10748 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010749 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010750 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010751 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10752 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010753 else
10754 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10755 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010756}
10757
Jan Kiszkaf4124502014-03-07 20:03:13 +010010758static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10759{
10760 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10761 struct vcpu_vmx *vmx = to_vmx(vcpu);
10762
10763 if (vcpu->arch.virtual_tsc_khz == 0)
10764 return;
10765
10766 /* Make sure short timeouts reliably trigger an immediate vmexit.
10767 * hrtimer_start does not guarantee this. */
10768 if (preemption_timeout <= 1) {
10769 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10770 return;
10771 }
10772
10773 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10774 preemption_timeout *= 1000000;
10775 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10776 hrtimer_start(&vmx->nested.preemption_timer,
10777 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10778}
10779
Jim Mattson56a20512017-07-06 16:33:06 -070010780static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10781 struct vmcs12 *vmcs12)
10782{
10783 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10784 return 0;
10785
10786 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10787 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10788 return -EINVAL;
10789
10790 return 0;
10791}
10792
Wincy Van3af18d92015-02-03 23:49:31 +080010793static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10794 struct vmcs12 *vmcs12)
10795{
Wincy Van3af18d92015-02-03 23:49:31 +080010796 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10797 return 0;
10798
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010799 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010800 return -EINVAL;
10801
10802 return 0;
10803}
10804
Jim Mattson712b12d2017-08-24 13:24:47 -070010805static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10806 struct vmcs12 *vmcs12)
10807{
10808 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10809 return 0;
10810
10811 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10812 return -EINVAL;
10813
10814 return 0;
10815}
10816
Wincy Van3af18d92015-02-03 23:49:31 +080010817/*
10818 * Merge L0's and L1's MSR bitmap, return false to indicate that
10819 * we do not use the hardware.
10820 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010821static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10822 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010823{
Wincy Van82f0dd42015-02-03 23:57:18 +080010824 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010825 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010826 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010827 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010828 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010829 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010830 *
10831 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10832 * ensures that we do not accidentally generate an L02 MSR bitmap
10833 * from the L12 MSR bitmap that is too permissive.
10834 * 2. That L1 or L2s have actually used the MSR. This avoids
10835 * unnecessarily merging of the bitmap if the MSR is unused. This
10836 * works properly because we only update the L01 MSR bitmap lazily.
10837 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10838 * updated to reflect this when L1 (or its L2s) actually write to
10839 * the MSR.
10840 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010841 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10842 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010843
Paolo Bonzinic9923842017-12-13 14:16:30 +010010844 /* Nothing to do if the MSR bitmap is not in use. */
10845 if (!cpu_has_vmx_msr_bitmap() ||
10846 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10847 return false;
10848
Ashok Raj15d45072018-02-01 22:59:43 +010010849 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010850 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010851 return false;
10852
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010853 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10854 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010855 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010856
Radim Krčmářd048c092016-08-08 20:16:22 +020010857 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010858 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10859 /*
10860 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10861 * just lets the processor take the value from the virtual-APIC page;
10862 * take those 256 bits directly from the L1 bitmap.
10863 */
10864 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10865 unsigned word = msr / BITS_PER_LONG;
10866 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10867 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010868 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010869 } else {
10870 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10871 unsigned word = msr / BITS_PER_LONG;
10872 msr_bitmap_l0[word] = ~0;
10873 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10874 }
10875 }
10876
10877 nested_vmx_disable_intercept_for_msr(
10878 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010879 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010880 MSR_TYPE_W);
10881
10882 if (nested_cpu_has_vid(vmcs12)) {
10883 nested_vmx_disable_intercept_for_msr(
10884 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010885 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010886 MSR_TYPE_W);
10887 nested_vmx_disable_intercept_for_msr(
10888 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010889 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010890 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010891 }
Ashok Raj15d45072018-02-01 22:59:43 +010010892
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010893 if (spec_ctrl)
10894 nested_vmx_disable_intercept_for_msr(
10895 msr_bitmap_l1, msr_bitmap_l0,
10896 MSR_IA32_SPEC_CTRL,
10897 MSR_TYPE_R | MSR_TYPE_W);
10898
Ashok Raj15d45072018-02-01 22:59:43 +010010899 if (pred_cmd)
10900 nested_vmx_disable_intercept_for_msr(
10901 msr_bitmap_l1, msr_bitmap_l0,
10902 MSR_IA32_PRED_CMD,
10903 MSR_TYPE_W);
10904
Wincy Vanf2b93282015-02-03 23:56:03 +080010905 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010906 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010907
10908 return true;
10909}
10910
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010911static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10912 struct vmcs12 *vmcs12)
10913{
10914 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10915 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10916 return -EINVAL;
10917 else
10918 return 0;
10919}
10920
Wincy Vanf2b93282015-02-03 23:56:03 +080010921static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10922 struct vmcs12 *vmcs12)
10923{
Wincy Van82f0dd42015-02-03 23:57:18 +080010924 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010925 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010926 !nested_cpu_has_vid(vmcs12) &&
10927 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010928 return 0;
10929
10930 /*
10931 * If virtualize x2apic mode is enabled,
10932 * virtualize apic access must be disabled.
10933 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010934 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10935 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010936 return -EINVAL;
10937
Wincy Van608406e2015-02-03 23:57:51 +080010938 /*
10939 * If virtual interrupt delivery is enabled,
10940 * we must exit on external interrupts.
10941 */
10942 if (nested_cpu_has_vid(vmcs12) &&
10943 !nested_exit_on_intr(vcpu))
10944 return -EINVAL;
10945
Wincy Van705699a2015-02-03 23:58:17 +080010946 /*
10947 * bits 15:8 should be zero in posted_intr_nv,
10948 * the descriptor address has been already checked
10949 * in nested_get_vmcs12_pages.
10950 */
10951 if (nested_cpu_has_posted_intr(vmcs12) &&
10952 (!nested_cpu_has_vid(vmcs12) ||
10953 !nested_exit_intr_ack_set(vcpu) ||
10954 vmcs12->posted_intr_nv & 0xff00))
10955 return -EINVAL;
10956
Wincy Vanf2b93282015-02-03 23:56:03 +080010957 /* tpr shadow is needed by all apicv features. */
10958 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10959 return -EINVAL;
10960
10961 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010962}
10963
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010964static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10965 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010966 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010967{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010968 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010969 u64 count, addr;
10970
10971 if (vmcs12_read_any(vcpu, count_field, &count) ||
10972 vmcs12_read_any(vcpu, addr_field, &addr)) {
10973 WARN_ON(1);
10974 return -EINVAL;
10975 }
10976 if (count == 0)
10977 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010978 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010979 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10980 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010981 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010982 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10983 addr_field, maxphyaddr, count, addr);
10984 return -EINVAL;
10985 }
10986 return 0;
10987}
10988
10989static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10990 struct vmcs12 *vmcs12)
10991{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010992 if (vmcs12->vm_exit_msr_load_count == 0 &&
10993 vmcs12->vm_exit_msr_store_count == 0 &&
10994 vmcs12->vm_entry_msr_load_count == 0)
10995 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010996 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010997 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010998 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010999 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011000 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011001 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011002 return -EINVAL;
11003 return 0;
11004}
11005
Bandan Dasc5f983f2017-05-05 15:25:14 -040011006static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11007 struct vmcs12 *vmcs12)
11008{
11009 u64 address = vmcs12->pml_address;
11010 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11011
11012 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11013 if (!nested_cpu_has_ept(vmcs12) ||
11014 !IS_ALIGNED(address, 4096) ||
11015 address >> maxphyaddr)
11016 return -EINVAL;
11017 }
11018
11019 return 0;
11020}
11021
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011022static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11023 struct vmx_msr_entry *e)
11024{
11025 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011026 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011027 return -EINVAL;
11028 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11029 e->index == MSR_IA32_UCODE_REV)
11030 return -EINVAL;
11031 if (e->reserved != 0)
11032 return -EINVAL;
11033 return 0;
11034}
11035
11036static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11037 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011038{
11039 if (e->index == MSR_FS_BASE ||
11040 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011041 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11042 nested_vmx_msr_check_common(vcpu, e))
11043 return -EINVAL;
11044 return 0;
11045}
11046
11047static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11048 struct vmx_msr_entry *e)
11049{
11050 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11051 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011052 return -EINVAL;
11053 return 0;
11054}
11055
11056/*
11057 * Load guest's/host's msr at nested entry/exit.
11058 * return 0 for success, entry index for failure.
11059 */
11060static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11061{
11062 u32 i;
11063 struct vmx_msr_entry e;
11064 struct msr_data msr;
11065
11066 msr.host_initiated = false;
11067 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011068 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11069 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011070 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011071 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11072 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011073 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011074 }
11075 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011076 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011077 "%s check failed (%u, 0x%x, 0x%x)\n",
11078 __func__, i, e.index, e.reserved);
11079 goto fail;
11080 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011081 msr.index = e.index;
11082 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011083 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011084 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011085 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11086 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011087 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011088 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011089 }
11090 return 0;
11091fail:
11092 return i + 1;
11093}
11094
11095static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11096{
11097 u32 i;
11098 struct vmx_msr_entry e;
11099
11100 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011101 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011102 if (kvm_vcpu_read_guest(vcpu,
11103 gpa + i * sizeof(e),
11104 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011105 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011106 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11107 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011108 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011109 }
11110 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011111 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011112 "%s check failed (%u, 0x%x, 0x%x)\n",
11113 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011114 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011115 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011116 msr_info.host_initiated = false;
11117 msr_info.index = e.index;
11118 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011119 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011120 "%s cannot read MSR (%u, 0x%x)\n",
11121 __func__, i, e.index);
11122 return -EINVAL;
11123 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011124 if (kvm_vcpu_write_guest(vcpu,
11125 gpa + i * sizeof(e) +
11126 offsetof(struct vmx_msr_entry, value),
11127 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011128 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011129 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011130 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011131 return -EINVAL;
11132 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011133 }
11134 return 0;
11135}
11136
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011137static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11138{
11139 unsigned long invalid_mask;
11140
11141 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11142 return (val & invalid_mask) == 0;
11143}
11144
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011145/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011146 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11147 * emulating VM entry into a guest with EPT enabled.
11148 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11149 * is assigned to entry_failure_code on failure.
11150 */
11151static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011152 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011153{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011154 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011155 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011156 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11157 return 1;
11158 }
11159
11160 /*
11161 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11162 * must not be dereferenced.
11163 */
11164 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11165 !nested_ept) {
11166 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11167 *entry_failure_code = ENTRY_FAIL_PDPTE;
11168 return 1;
11169 }
11170 }
11171
11172 vcpu->arch.cr3 = cr3;
11173 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11174 }
11175
11176 kvm_mmu_reset_context(vcpu);
11177 return 0;
11178}
11179
Jim Mattson6514dc32018-04-26 16:09:12 -070011180static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011181{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011182 struct vcpu_vmx *vmx = to_vmx(vcpu);
11183
11184 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11185 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11186 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11187 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11188 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11189 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11190 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11191 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11192 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11193 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11194 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11195 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11196 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11197 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11198 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11199 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11200 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11201 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11202 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11203 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11204 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11205 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11206 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11207 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11208 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11209 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11210 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11211 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11212 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11213 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11214 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011215
11216 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11217 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11218 vmcs12->guest_pending_dbg_exceptions);
11219 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11220 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11221
11222 if (nested_cpu_has_xsaves(vmcs12))
11223 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11224 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11225
11226 if (cpu_has_vmx_posted_intr())
11227 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11228
11229 /*
11230 * Whether page-faults are trapped is determined by a combination of
11231 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11232 * If enable_ept, L0 doesn't care about page faults and we should
11233 * set all of these to L1's desires. However, if !enable_ept, L0 does
11234 * care about (at least some) page faults, and because it is not easy
11235 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11236 * to exit on each and every L2 page fault. This is done by setting
11237 * MASK=MATCH=0 and (see below) EB.PF=1.
11238 * Note that below we don't need special code to set EB.PF beyond the
11239 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11240 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11241 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11242 */
11243 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11244 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11245 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11246 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11247
11248 /* All VMFUNCs are currently emulated through L0 vmexits. */
11249 if (cpu_has_vmx_vmfunc())
11250 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11251
11252 if (cpu_has_vmx_apicv()) {
11253 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11254 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11255 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11256 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11257 }
11258
11259 /*
11260 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11261 * Some constant fields are set here by vmx_set_constant_host_state().
11262 * Other fields are different per CPU, and will be set later when
11263 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11264 */
11265 vmx_set_constant_host_state(vmx);
11266
11267 /*
11268 * Set the MSR load/store lists to match L0's settings.
11269 */
11270 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11271 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11272 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11273 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11274 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11275
11276 set_cr4_guest_host_mask(vmx);
11277
11278 if (vmx_mpx_supported())
11279 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11280
11281 if (enable_vpid) {
11282 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11283 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11284 else
11285 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11286 }
11287
11288 /*
11289 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11290 */
11291 if (enable_ept) {
11292 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11293 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11294 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11295 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11296 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011297
11298 if (cpu_has_vmx_msr_bitmap())
11299 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011300}
11301
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011302/*
11303 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11304 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011305 * 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 +030011306 * guest in a way that will both be appropriate to L1's requests, and our
11307 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11308 * function also has additional necessary side-effects, like setting various
11309 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011310 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11311 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011312 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011313static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011314 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011315{
11316 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011317 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011318
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011319 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011320 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011321 vmx->nested.dirty_vmcs12 = false;
11322 }
11323
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011324 /*
11325 * First, the fields that are shadowed. This must be kept in sync
11326 * with vmx_shadow_fields.h.
11327 */
11328
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011329 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011330 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011331 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011332 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11333 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011334
11335 /*
11336 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11337 * HOST_FS_BASE, HOST_GS_BASE.
11338 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011339
Jim Mattson6514dc32018-04-26 16:09:12 -070011340 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011341 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011342 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11343 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11344 } else {
11345 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11346 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11347 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011348 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011349 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11350 vmcs12->vm_entry_intr_info_field);
11351 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11352 vmcs12->vm_entry_exception_error_code);
11353 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11354 vmcs12->vm_entry_instruction_len);
11355 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11356 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011357 vmx->loaded_vmcs->nmi_known_unmasked =
11358 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011359 } else {
11360 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11361 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011362 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011363
Jan Kiszkaf4124502014-03-07 20:03:13 +010011364 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011365
Paolo Bonzini93140062016-07-06 13:23:51 +020011366 /* Preemption timer setting is only taken from vmcs01. */
11367 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11368 exec_control |= vmcs_config.pin_based_exec_ctrl;
11369 if (vmx->hv_deadline_tsc == -1)
11370 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11371
11372 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011373 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011374 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11375 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011376 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011377 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011378 }
Wincy Van705699a2015-02-03 23:58:17 +080011379
Jan Kiszkaf4124502014-03-07 20:03:13 +010011380 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011381
Jan Kiszkaf4124502014-03-07 20:03:13 +010011382 vmx->nested.preemption_timer_expired = false;
11383 if (nested_cpu_has_preemption_timer(vmcs12))
11384 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011385
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011386 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011387 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011388
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011389 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011390 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011391 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011392 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011393 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011394 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011395 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11396 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011397 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011398 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11399 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11400 ~SECONDARY_EXEC_ENABLE_PML;
11401 exec_control |= vmcs12_exec_ctrl;
11402 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011403
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011404 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011405 vmcs_write16(GUEST_INTR_STATUS,
11406 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011407
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011408 /*
11409 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11410 * nested_get_vmcs12_pages will either fix it up or
11411 * remove the VM execution control.
11412 */
11413 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11414 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11415
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011416 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11417 }
11418
Jim Mattson83bafef2016-10-04 10:48:38 -070011419 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011420 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11421 * entry, but only if the current (host) sp changed from the value
11422 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11423 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11424 * here we just force the write to happen on entry.
11425 */
11426 vmx->host_rsp = 0;
11427
11428 exec_control = vmx_exec_control(vmx); /* L0's desires */
11429 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11430 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11431 exec_control &= ~CPU_BASED_TPR_SHADOW;
11432 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011433
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011434 /*
11435 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11436 * nested_get_vmcs12_pages can't fix it up, the illegal value
11437 * will result in a VM entry failure.
11438 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011439 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011440 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011441 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011442 } else {
11443#ifdef CONFIG_X86_64
11444 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11445 CPU_BASED_CR8_STORE_EXITING;
11446#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011447 }
11448
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011449 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011450 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11451 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011452 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011453 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11454 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11455
11456 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11457
11458 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11459 * bitwise-or of what L1 wants to trap for L2, and what we want to
11460 * trap. Note that CR0.TS also needs updating - we do this later.
11461 */
11462 update_exception_bitmap(vcpu);
11463 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11464 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11465
Nadav Har'El8049d652013-08-05 11:07:06 +030011466 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11467 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11468 * bits are further modified by vmx_set_efer() below.
11469 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011470 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011471
11472 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11473 * emulated by vmx_set_efer(), below.
11474 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011475 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011476 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11477 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011478 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11479
Jim Mattson6514dc32018-04-26 16:09:12 -070011480 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011481 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011482 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011483 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011484 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011485 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011486 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011487
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011488 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11489
Peter Feinerc95ba922016-08-17 09:36:47 -070011490 if (kvm_has_tsc_control)
11491 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011492
11493 if (enable_vpid) {
11494 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011495 * There is no direct mapping between vpid02 and vpid12, the
11496 * vpid02 is per-vCPU for L0 and reused while the value of
11497 * vpid12 is changed w/ one invvpid during nested vmentry.
11498 * The vpid12 is allocated by L1 for L2, so it will not
11499 * influence global bitmap(for vpid01 and vpid02 allocation)
11500 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011501 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011502 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011503 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11504 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011505 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011506 }
11507 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011508 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011509 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011510 }
11511
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011512 if (enable_pml) {
11513 /*
11514 * Conceptually we want to copy the PML address and index from
11515 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11516 * since we always flush the log on each vmexit, this happens
11517 * to be equivalent to simply resetting the fields in vmcs02.
11518 */
11519 ASSERT(vmx->pml_pg);
11520 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11521 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11522 }
11523
Nadav Har'El155a97a2013-08-05 11:07:16 +030011524 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011525 if (nested_ept_init_mmu_context(vcpu)) {
11526 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11527 return 1;
11528 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011529 } else if (nested_cpu_has2(vmcs12,
11530 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011531 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011532 }
11533
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011534 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011535 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11536 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011537 * The CR0_READ_SHADOW is what L2 should have expected to read given
11538 * the specifications by L1; It's not enough to take
11539 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11540 * have more bits than L1 expected.
11541 */
11542 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11543 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11544
11545 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11546 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11547
Jim Mattson6514dc32018-04-26 16:09:12 -070011548 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011549 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011550 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11551 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11552 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11553 else
11554 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11555 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11556 vmx_set_efer(vcpu, vcpu->arch.efer);
11557
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011558 /*
11559 * Guest state is invalid and unrestricted guest is disabled,
11560 * which means L1 attempted VMEntry to L2 with invalid state.
11561 * Fail the VMEntry.
11562 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011563 if (vmx->emulation_required) {
11564 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011565 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011566 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011567
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011568 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011569 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011570 entry_failure_code))
11571 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011572
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011573 if (!enable_ept)
11574 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11575
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011576 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11577 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011578 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011579}
11580
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011581static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11582{
11583 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11584 nested_cpu_has_virtual_nmis(vmcs12))
11585 return -EINVAL;
11586
11587 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11588 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11589 return -EINVAL;
11590
11591 return 0;
11592}
11593
Jim Mattsonca0bde22016-11-30 12:03:46 -080011594static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11595{
11596 struct vcpu_vmx *vmx = to_vmx(vcpu);
11597
11598 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11599 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11600 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11601
Jim Mattson56a20512017-07-06 16:33:06 -070011602 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11603 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11604
Jim Mattsonca0bde22016-11-30 12:03:46 -080011605 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11606 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11607
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011608 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11609 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11610
Jim Mattson712b12d2017-08-24 13:24:47 -070011611 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11612 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11613
Jim Mattsonca0bde22016-11-30 12:03:46 -080011614 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11615 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11616
11617 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11618 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11619
Bandan Dasc5f983f2017-05-05 15:25:14 -040011620 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11621 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11622
Jim Mattsonca0bde22016-11-30 12:03:46 -080011623 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011624 vmx->nested.msrs.procbased_ctls_low,
11625 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011626 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11627 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011628 vmx->nested.msrs.secondary_ctls_low,
11629 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011630 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011631 vmx->nested.msrs.pinbased_ctls_low,
11632 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011633 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011634 vmx->nested.msrs.exit_ctls_low,
11635 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011636 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011637 vmx->nested.msrs.entry_ctls_low,
11638 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011639 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11640
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011641 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011642 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11643
Bandan Das41ab9372017-08-03 15:54:43 -040011644 if (nested_cpu_has_vmfunc(vmcs12)) {
11645 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011646 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011647 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11648
11649 if (nested_cpu_has_eptp_switching(vmcs12)) {
11650 if (!nested_cpu_has_ept(vmcs12) ||
11651 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11652 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11653 }
11654 }
Bandan Das27c42a12017-08-03 15:54:42 -040011655
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011656 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11657 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11658
Jim Mattsonca0bde22016-11-30 12:03:46 -080011659 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11660 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11661 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11662 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11663
Marc Orr04473782018-06-20 17:21:29 -070011664 /*
11665 * From the Intel SDM, volume 3:
11666 * Fields relevant to VM-entry event injection must be set properly.
11667 * These fields are the VM-entry interruption-information field, the
11668 * VM-entry exception error code, and the VM-entry instruction length.
11669 */
11670 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11671 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11672 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11673 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11674 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11675 bool should_have_error_code;
11676 bool urg = nested_cpu_has2(vmcs12,
11677 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11678 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11679
11680 /* VM-entry interruption-info field: interruption type */
11681 if (intr_type == INTR_TYPE_RESERVED ||
11682 (intr_type == INTR_TYPE_OTHER_EVENT &&
11683 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11684 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11685
11686 /* VM-entry interruption-info field: vector */
11687 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11688 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11689 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11690 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11691
11692 /* VM-entry interruption-info field: deliver error code */
11693 should_have_error_code =
11694 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11695 x86_exception_has_error_code(vector);
11696 if (has_error_code != should_have_error_code)
11697 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11698
11699 /* VM-entry exception error code */
11700 if (has_error_code &&
11701 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11702 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11703
11704 /* VM-entry interruption-info field: reserved bits */
11705 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11706 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11707
11708 /* VM-entry instruction length */
11709 switch (intr_type) {
11710 case INTR_TYPE_SOFT_EXCEPTION:
11711 case INTR_TYPE_SOFT_INTR:
11712 case INTR_TYPE_PRIV_SW_EXCEPTION:
11713 if ((vmcs12->vm_entry_instruction_len > 15) ||
11714 (vmcs12->vm_entry_instruction_len == 0 &&
11715 !nested_cpu_has_zero_length_injection(vcpu)))
11716 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11717 }
11718 }
11719
Jim Mattsonca0bde22016-11-30 12:03:46 -080011720 return 0;
11721}
11722
11723static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11724 u32 *exit_qual)
11725{
11726 bool ia32e;
11727
11728 *exit_qual = ENTRY_FAIL_DEFAULT;
11729
11730 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11731 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11732 return 1;
11733
11734 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11735 vmcs12->vmcs_link_pointer != -1ull) {
11736 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11737 return 1;
11738 }
11739
11740 /*
11741 * If the load IA32_EFER VM-entry control is 1, the following checks
11742 * are performed on the field for the IA32_EFER MSR:
11743 * - Bits reserved in the IA32_EFER MSR must be 0.
11744 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11745 * the IA-32e mode guest VM-exit control. It must also be identical
11746 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11747 * CR0.PG) is 1.
11748 */
11749 if (to_vmx(vcpu)->nested.nested_run_pending &&
11750 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11751 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11752 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11753 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11754 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11755 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11756 return 1;
11757 }
11758
11759 /*
11760 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11761 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11762 * the values of the LMA and LME bits in the field must each be that of
11763 * the host address-space size VM-exit control.
11764 */
11765 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11766 ia32e = (vmcs12->vm_exit_controls &
11767 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11768 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11769 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11770 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11771 return 1;
11772 }
11773
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011774 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11775 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11776 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11777 return 1;
11778
Jim Mattsonca0bde22016-11-30 12:03:46 -080011779 return 0;
11780}
11781
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011782/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011783 * If exit_qual is NULL, this is being called from state restore (either RSM
11784 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011785 */
11786static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080011787{
11788 struct vcpu_vmx *vmx = to_vmx(vcpu);
11789 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011790 bool from_vmentry = !!exit_qual;
11791 u32 dummy_exit_qual;
11792 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011793
Jim Mattson858e25c2016-11-30 12:03:47 -080011794 enter_guest_mode(vcpu);
11795
11796 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11797 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11798
Jim Mattsonde3a0022017-11-27 17:22:25 -060011799 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011800 vmx_segment_cache_clear(vmx);
11801
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011802 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11803 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11804
11805 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011806 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011807 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011808
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011809 if (from_vmentry) {
11810 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011811
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011812 r = EXIT_REASON_MSR_LOAD_FAIL;
11813 *exit_qual = nested_vmx_load_msr(vcpu,
11814 vmcs12->vm_entry_msr_load_addr,
11815 vmcs12->vm_entry_msr_load_count);
11816 if (*exit_qual)
11817 goto fail;
11818 } else {
11819 /*
11820 * The MMU is not initialized to point at the right entities yet and
11821 * "get pages" would need to read data from the guest (i.e. we will
11822 * need to perform gpa to hpa translation). Request a call
11823 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11824 * have already been set at vmentry time and should not be reset.
11825 */
11826 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11827 }
Jim Mattson858e25c2016-11-30 12:03:47 -080011828
Jim Mattson858e25c2016-11-30 12:03:47 -080011829 /*
11830 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11831 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11832 * returned as far as L1 is concerned. It will only return (and set
11833 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11834 */
11835 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011836
11837fail:
11838 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11839 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11840 leave_guest_mode(vcpu);
11841 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011842 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011843}
11844
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011845/*
11846 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11847 * for running an L2 nested guest.
11848 */
11849static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11850{
11851 struct vmcs12 *vmcs12;
11852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011853 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011854 u32 exit_qual;
11855 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011856
Kyle Hueyeb277562016-11-29 12:40:39 -080011857 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011858 return 1;
11859
Kyle Hueyeb277562016-11-29 12:40:39 -080011860 if (!nested_vmx_check_vmcs12(vcpu))
11861 goto out;
11862
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011863 vmcs12 = get_vmcs12(vcpu);
11864
Abel Gordon012f83c2013-04-18 14:39:25 +030011865 if (enable_shadow_vmcs)
11866 copy_shadow_to_vmcs12(vmx);
11867
Nadav Har'El7c177932011-05-25 23:12:04 +030011868 /*
11869 * The nested entry process starts with enforcing various prerequisites
11870 * on vmcs12 as required by the Intel SDM, and act appropriately when
11871 * they fail: As the SDM explains, some conditions should cause the
11872 * instruction to fail, while others will cause the instruction to seem
11873 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11874 * To speed up the normal (success) code path, we should avoid checking
11875 * for misconfigurations which will anyway be caught by the processor
11876 * when using the merged vmcs02.
11877 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011878 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11879 nested_vmx_failValid(vcpu,
11880 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11881 goto out;
11882 }
11883
Nadav Har'El7c177932011-05-25 23:12:04 +030011884 if (vmcs12->launch_state == launch) {
11885 nested_vmx_failValid(vcpu,
11886 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11887 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011888 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011889 }
11890
Jim Mattsonca0bde22016-11-30 12:03:46 -080011891 ret = check_vmentry_prereqs(vcpu, vmcs12);
11892 if (ret) {
11893 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011894 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011895 }
11896
Nadav Har'El7c177932011-05-25 23:12:04 +030011897 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011898 * After this point, the trap flag no longer triggers a singlestep trap
11899 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11900 * This is not 100% correct; for performance reasons, we delegate most
11901 * of the checks on host state to the processor. If those fail,
11902 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011903 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011904 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011905
Jim Mattsonca0bde22016-11-30 12:03:46 -080011906 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11907 if (ret) {
11908 nested_vmx_entry_failure(vcpu, vmcs12,
11909 EXIT_REASON_INVALID_STATE, exit_qual);
11910 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011911 }
11912
11913 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011914 * We're finally done with prerequisite checking, and can start with
11915 * the nested entry.
11916 */
11917
Jim Mattson6514dc32018-04-26 16:09:12 -070011918 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011919 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070011920 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011921 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070011922 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011923 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070011924 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011925
Chao Gao135a06c2018-02-11 10:06:30 +080011926 /*
11927 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11928 * by event injection, halt vcpu.
11929 */
11930 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011931 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11932 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011933 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011934 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011935 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011936
11937out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011938 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011939}
11940
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011941/*
11942 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11943 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11944 * This function returns the new value we should put in vmcs12.guest_cr0.
11945 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11946 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11947 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11948 * didn't trap the bit, because if L1 did, so would L0).
11949 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11950 * been modified by L2, and L1 knows it. So just leave the old value of
11951 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11952 * isn't relevant, because if L0 traps this bit it can set it to anything.
11953 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11954 * changed these bits, and therefore they need to be updated, but L0
11955 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11956 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11957 */
11958static inline unsigned long
11959vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11960{
11961 return
11962 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11963 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11964 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11965 vcpu->arch.cr0_guest_owned_bits));
11966}
11967
11968static inline unsigned long
11969vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11970{
11971 return
11972 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11973 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11974 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11975 vcpu->arch.cr4_guest_owned_bits));
11976}
11977
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011978static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11979 struct vmcs12 *vmcs12)
11980{
11981 u32 idt_vectoring;
11982 unsigned int nr;
11983
Wanpeng Li664f8e22017-08-24 03:35:09 -070011984 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011985 nr = vcpu->arch.exception.nr;
11986 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11987
11988 if (kvm_exception_is_soft(nr)) {
11989 vmcs12->vm_exit_instruction_len =
11990 vcpu->arch.event_exit_inst_len;
11991 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11992 } else
11993 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11994
11995 if (vcpu->arch.exception.has_error_code) {
11996 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11997 vmcs12->idt_vectoring_error_code =
11998 vcpu->arch.exception.error_code;
11999 }
12000
12001 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012002 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012003 vmcs12->idt_vectoring_info_field =
12004 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012005 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012006 nr = vcpu->arch.interrupt.nr;
12007 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12008
12009 if (vcpu->arch.interrupt.soft) {
12010 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12011 vmcs12->vm_entry_instruction_len =
12012 vcpu->arch.event_exit_inst_len;
12013 } else
12014 idt_vectoring |= INTR_TYPE_EXT_INTR;
12015
12016 vmcs12->idt_vectoring_info_field = idt_vectoring;
12017 }
12018}
12019
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012020static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12021{
12022 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012023 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012024 bool block_nested_events =
12025 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012026
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012027 if (vcpu->arch.exception.pending &&
12028 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012029 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012030 return -EBUSY;
12031 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012032 return 0;
12033 }
12034
Jan Kiszkaf4124502014-03-07 20:03:13 +010012035 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12036 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012037 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012038 return -EBUSY;
12039 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12040 return 0;
12041 }
12042
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012043 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012044 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012045 return -EBUSY;
12046 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12047 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12048 INTR_INFO_VALID_MASK, 0);
12049 /*
12050 * The NMI-triggered VM exit counts as injection:
12051 * clear this one and block further NMIs.
12052 */
12053 vcpu->arch.nmi_pending = 0;
12054 vmx_set_nmi_mask(vcpu, true);
12055 return 0;
12056 }
12057
12058 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12059 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012060 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012061 return -EBUSY;
12062 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012063 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012064 }
12065
David Hildenbrand6342c502017-01-25 11:58:58 +010012066 vmx_complete_nested_posted_interrupt(vcpu);
12067 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012068}
12069
Jan Kiszkaf4124502014-03-07 20:03:13 +010012070static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12071{
12072 ktime_t remaining =
12073 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12074 u64 value;
12075
12076 if (ktime_to_ns(remaining) <= 0)
12077 return 0;
12078
12079 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12080 do_div(value, 1000000);
12081 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12082}
12083
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012084/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012085 * Update the guest state fields of vmcs12 to reflect changes that
12086 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12087 * VM-entry controls is also updated, since this is really a guest
12088 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012089 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012090static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012091{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012092 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12093 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12094
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012095 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12096 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12097 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12098
12099 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12100 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12101 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12102 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12103 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12104 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12105 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12106 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12107 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12108 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12109 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12110 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12111 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12112 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12113 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12114 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12115 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12116 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12117 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12118 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12119 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12120 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12121 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12122 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12123 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12124 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12125 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12126 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12127 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12128 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12129 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12130 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12131 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12132 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12133 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12134 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12135
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012136 vmcs12->guest_interruptibility_info =
12137 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12138 vmcs12->guest_pending_dbg_exceptions =
12139 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012140 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12141 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12142 else
12143 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012144
Jan Kiszkaf4124502014-03-07 20:03:13 +010012145 if (nested_cpu_has_preemption_timer(vmcs12)) {
12146 if (vmcs12->vm_exit_controls &
12147 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12148 vmcs12->vmx_preemption_timer_value =
12149 vmx_get_preemption_timer_value(vcpu);
12150 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12151 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012152
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012153 /*
12154 * In some cases (usually, nested EPT), L2 is allowed to change its
12155 * own CR3 without exiting. If it has changed it, we must keep it.
12156 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12157 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12158 *
12159 * Additionally, restore L2's PDPTR to vmcs12.
12160 */
12161 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012162 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012163 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12164 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12165 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12166 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12167 }
12168
Jim Mattsond281e132017-06-01 12:44:46 -070012169 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012170
Wincy Van608406e2015-02-03 23:57:51 +080012171 if (nested_cpu_has_vid(vmcs12))
12172 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12173
Jan Kiszkac18911a2013-03-13 16:06:41 +010012174 vmcs12->vm_entry_controls =
12175 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012176 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012177
Jan Kiszka2996fca2014-06-16 13:59:43 +020012178 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12179 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12180 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12181 }
12182
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012183 /* TODO: These cannot have changed unless we have MSR bitmaps and
12184 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012185 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012186 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012187 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12188 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012189 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12190 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12191 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012192 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012193 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012194}
12195
12196/*
12197 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12198 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12199 * and this function updates it to reflect the changes to the guest state while
12200 * L2 was running (and perhaps made some exits which were handled directly by L0
12201 * without going back to L1), and to reflect the exit reason.
12202 * Note that we do not have to copy here all VMCS fields, just those that
12203 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12204 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12205 * which already writes to vmcs12 directly.
12206 */
12207static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12208 u32 exit_reason, u32 exit_intr_info,
12209 unsigned long exit_qualification)
12210{
12211 /* update guest state fields: */
12212 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012213
12214 /* update exit information fields: */
12215
Jan Kiszka533558b2014-01-04 18:47:20 +010012216 vmcs12->vm_exit_reason = exit_reason;
12217 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012218 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012219
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012220 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012221 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12222 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12223
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012224 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012225 vmcs12->launch_state = 1;
12226
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012227 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12228 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012229 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012230
12231 /*
12232 * Transfer the event that L0 or L1 may wanted to inject into
12233 * L2 to IDT_VECTORING_INFO_FIELD.
12234 */
12235 vmcs12_save_pending_event(vcpu, vmcs12);
12236 }
12237
12238 /*
12239 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12240 * preserved above and would only end up incorrectly in L1.
12241 */
12242 vcpu->arch.nmi_injected = false;
12243 kvm_clear_exception_queue(vcpu);
12244 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012245}
12246
Wanpeng Li5af41572017-11-05 16:54:49 -080012247static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12248 struct vmcs12 *vmcs12)
12249{
12250 u32 entry_failure_code;
12251
12252 nested_ept_uninit_mmu_context(vcpu);
12253
12254 /*
12255 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12256 * couldn't have changed.
12257 */
12258 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12259 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12260
12261 if (!enable_ept)
12262 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12263}
12264
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012265/*
12266 * A part of what we need to when the nested L2 guest exits and we want to
12267 * run its L1 parent, is to reset L1's guest state to the host state specified
12268 * in vmcs12.
12269 * This function is to be called not only on normal nested exit, but also on
12270 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12271 * Failures During or After Loading Guest State").
12272 * This function should be called when the active VMCS is L1's (vmcs01).
12273 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012274static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12275 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012276{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012277 struct kvm_segment seg;
12278
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012279 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12280 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012281 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012282 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12283 else
12284 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12285 vmx_set_efer(vcpu, vcpu->arch.efer);
12286
12287 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12288 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012289 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012290 /*
12291 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012292 * actually changed, because vmx_set_cr0 refers to efer set above.
12293 *
12294 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12295 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012296 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012297 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012298 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012299
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012300 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012301 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012302 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012303
Wanpeng Li5af41572017-11-05 16:54:49 -080012304 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012305
Liran Alon6f1e03b2018-05-22 17:16:14 +030012306 /*
12307 * If vmcs01 don't use VPID, CPU flushes TLB on every
12308 * VMEntry/VMExit. Thus, no need to flush TLB.
12309 *
12310 * If vmcs12 uses VPID, TLB entries populated by L2 are
12311 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12312 * with vmx->vpid. Thus, no need to flush TLB.
12313 *
12314 * Therefore, flush TLB only in case vmcs01 uses VPID and
12315 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12316 * are both tagged with vmx->vpid.
12317 */
12318 if (enable_vpid &&
12319 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012320 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012321 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012322
12323 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12324 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12325 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12326 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12327 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012328 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12329 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012330
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012331 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12332 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12333 vmcs_write64(GUEST_BNDCFGS, 0);
12334
Jan Kiszka44811c02013-08-04 17:17:27 +020012335 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012336 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012337 vcpu->arch.pat = vmcs12->host_ia32_pat;
12338 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012339 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12340 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12341 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012342
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012343 /* Set L1 segment info according to Intel SDM
12344 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12345 seg = (struct kvm_segment) {
12346 .base = 0,
12347 .limit = 0xFFFFFFFF,
12348 .selector = vmcs12->host_cs_selector,
12349 .type = 11,
12350 .present = 1,
12351 .s = 1,
12352 .g = 1
12353 };
12354 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12355 seg.l = 1;
12356 else
12357 seg.db = 1;
12358 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12359 seg = (struct kvm_segment) {
12360 .base = 0,
12361 .limit = 0xFFFFFFFF,
12362 .type = 3,
12363 .present = 1,
12364 .s = 1,
12365 .db = 1,
12366 .g = 1
12367 };
12368 seg.selector = vmcs12->host_ds_selector;
12369 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12370 seg.selector = vmcs12->host_es_selector;
12371 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12372 seg.selector = vmcs12->host_ss_selector;
12373 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12374 seg.selector = vmcs12->host_fs_selector;
12375 seg.base = vmcs12->host_fs_base;
12376 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12377 seg.selector = vmcs12->host_gs_selector;
12378 seg.base = vmcs12->host_gs_base;
12379 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12380 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012381 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012382 .limit = 0x67,
12383 .selector = vmcs12->host_tr_selector,
12384 .type = 11,
12385 .present = 1
12386 };
12387 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12388
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012389 kvm_set_dr(vcpu, 7, 0x400);
12390 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012391
Wincy Van3af18d92015-02-03 23:49:31 +080012392 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012393 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012394
Wincy Vanff651cb2014-12-11 08:52:58 +030012395 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12396 vmcs12->vm_exit_msr_load_count))
12397 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012398}
12399
12400/*
12401 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12402 * and modify vmcs12 to make it see what it would expect to see there if
12403 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12404 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012405static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12406 u32 exit_intr_info,
12407 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012408{
12409 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012410 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12411
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012412 /* trying to cancel vmlaunch/vmresume is a bug */
12413 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12414
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012415 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012416 * The only expected VM-instruction error is "VM entry with
12417 * invalid control field(s)." Anything else indicates a
12418 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012419 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012420 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12421 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12422
12423 leave_guest_mode(vcpu);
12424
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012425 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12426 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12427
Jim Mattson4f350c62017-09-14 16:31:44 -070012428 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012429 if (exit_reason == -1)
12430 sync_vmcs12(vcpu, vmcs12);
12431 else
12432 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12433 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012434
12435 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12436 vmcs12->vm_exit_msr_store_count))
12437 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012438 }
12439
Jim Mattson4f350c62017-09-14 16:31:44 -070012440 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012441 vm_entry_controls_reset_shadow(vmx);
12442 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012443 vmx_segment_cache_clear(vmx);
12444
Paolo Bonzini93140062016-07-06 13:23:51 +020012445 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012446 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12447 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012448 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012449 if (vmx->hv_deadline_tsc == -1)
12450 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12451 PIN_BASED_VMX_PREEMPTION_TIMER);
12452 else
12453 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12454 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012455 if (kvm_has_tsc_control)
12456 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012457
Jim Mattson8d860bb2018-05-09 16:56:05 -040012458 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12459 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12460 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012461 } else if (!nested_cpu_has_ept(vmcs12) &&
12462 nested_cpu_has2(vmcs12,
12463 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012464 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012465 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012466
12467 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12468 vmx->host_rsp = 0;
12469
12470 /* Unpin physical memory we referred to in vmcs02 */
12471 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012472 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012473 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012474 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012475 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012476 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012477 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012478 }
Wincy Van705699a2015-02-03 23:58:17 +080012479 if (vmx->nested.pi_desc_page) {
12480 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012481 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012482 vmx->nested.pi_desc_page = NULL;
12483 vmx->nested.pi_desc = NULL;
12484 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012485
12486 /*
Tang Chen38b99172014-09-24 15:57:54 +080012487 * We are now running in L2, mmu_notifier will force to reload the
12488 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12489 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012490 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012491
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012492 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012493 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012494
12495 /* in case we halted in L2 */
12496 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012497
12498 if (likely(!vmx->fail)) {
12499 /*
12500 * TODO: SDM says that with acknowledge interrupt on
12501 * exit, bit 31 of the VM-exit interrupt information
12502 * (valid interrupt) is always set to 1 on
12503 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12504 * need kvm_cpu_has_interrupt(). See the commit
12505 * message for details.
12506 */
12507 if (nested_exit_intr_ack_set(vcpu) &&
12508 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12509 kvm_cpu_has_interrupt(vcpu)) {
12510 int irq = kvm_cpu_get_interrupt(vcpu);
12511 WARN_ON(irq < 0);
12512 vmcs12->vm_exit_intr_info = irq |
12513 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12514 }
12515
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012516 if (exit_reason != -1)
12517 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12518 vmcs12->exit_qualification,
12519 vmcs12->idt_vectoring_info_field,
12520 vmcs12->vm_exit_intr_info,
12521 vmcs12->vm_exit_intr_error_code,
12522 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012523
12524 load_vmcs12_host_state(vcpu, vmcs12);
12525
12526 return;
12527 }
12528
12529 /*
12530 * After an early L2 VM-entry failure, we're now back
12531 * in L1 which thinks it just finished a VMLAUNCH or
12532 * VMRESUME instruction, so we need to set the failure
12533 * flag and the VM-instruction error field of the VMCS
12534 * accordingly.
12535 */
12536 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012537
12538 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12539
Jim Mattson4f350c62017-09-14 16:31:44 -070012540 /*
12541 * The emulated instruction was already skipped in
12542 * nested_vmx_run, but the updated RIP was never
12543 * written back to the vmcs01.
12544 */
12545 skip_emulated_instruction(vcpu);
12546 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012547}
12548
Nadav Har'El7c177932011-05-25 23:12:04 +030012549/*
Jan Kiszka42124922014-01-04 18:47:19 +010012550 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12551 */
12552static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12553{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012554 if (is_guest_mode(vcpu)) {
12555 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012556 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012557 }
Jan Kiszka42124922014-01-04 18:47:19 +010012558 free_nested(to_vmx(vcpu));
12559}
12560
12561/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012562 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12563 * 23.7 "VM-entry failures during or after loading guest state" (this also
12564 * lists the acceptable exit-reason and exit-qualification parameters).
12565 * It should only be called before L2 actually succeeded to run, and when
12566 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12567 */
12568static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12569 struct vmcs12 *vmcs12,
12570 u32 reason, unsigned long qualification)
12571{
12572 load_vmcs12_host_state(vcpu, vmcs12);
12573 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12574 vmcs12->exit_qualification = qualification;
12575 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012576 if (enable_shadow_vmcs)
12577 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012578}
12579
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012580static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12581 struct x86_instruction_info *info,
12582 enum x86_intercept_stage stage)
12583{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012584 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12585 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12586
12587 /*
12588 * RDPID causes #UD if disabled through secondary execution controls.
12589 * Because it is marked as EmulateOnUD, we need to intercept it here.
12590 */
12591 if (info->intercept == x86_intercept_rdtscp &&
12592 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12593 ctxt->exception.vector = UD_VECTOR;
12594 ctxt->exception.error_code_valid = false;
12595 return X86EMUL_PROPAGATE_FAULT;
12596 }
12597
12598 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012599 return X86EMUL_CONTINUE;
12600}
12601
Yunhong Jiang64672c92016-06-13 14:19:59 -070012602#ifdef CONFIG_X86_64
12603/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12604static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12605 u64 divisor, u64 *result)
12606{
12607 u64 low = a << shift, high = a >> (64 - shift);
12608
12609 /* To avoid the overflow on divq */
12610 if (high >= divisor)
12611 return 1;
12612
12613 /* Low hold the result, high hold rem which is discarded */
12614 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12615 "rm" (divisor), "0" (low), "1" (high));
12616 *result = low;
12617
12618 return 0;
12619}
12620
12621static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12622{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012623 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012624 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012625
12626 if (kvm_mwait_in_guest(vcpu->kvm))
12627 return -EOPNOTSUPP;
12628
12629 vmx = to_vmx(vcpu);
12630 tscl = rdtsc();
12631 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12632 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012633 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12634
12635 if (delta_tsc > lapic_timer_advance_cycles)
12636 delta_tsc -= lapic_timer_advance_cycles;
12637 else
12638 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012639
12640 /* Convert to host delta tsc if tsc scaling is enabled */
12641 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12642 u64_shl_div_u64(delta_tsc,
12643 kvm_tsc_scaling_ratio_frac_bits,
12644 vcpu->arch.tsc_scaling_ratio,
12645 &delta_tsc))
12646 return -ERANGE;
12647
12648 /*
12649 * If the delta tsc can't fit in the 32 bit after the multi shift,
12650 * we can't use the preemption timer.
12651 * It's possible that it fits on later vmentries, but checking
12652 * on every vmentry is costly so we just use an hrtimer.
12653 */
12654 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12655 return -ERANGE;
12656
12657 vmx->hv_deadline_tsc = tscl + delta_tsc;
12658 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12659 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012660
12661 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012662}
12663
12664static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12665{
12666 struct vcpu_vmx *vmx = to_vmx(vcpu);
12667 vmx->hv_deadline_tsc = -1;
12668 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12669 PIN_BASED_VMX_PREEMPTION_TIMER);
12670}
12671#endif
12672
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012673static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012674{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012675 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012676 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012677}
12678
Kai Huang843e4332015-01-28 10:54:28 +080012679static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12680 struct kvm_memory_slot *slot)
12681{
12682 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12683 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12684}
12685
12686static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12687 struct kvm_memory_slot *slot)
12688{
12689 kvm_mmu_slot_set_dirty(kvm, slot);
12690}
12691
12692static void vmx_flush_log_dirty(struct kvm *kvm)
12693{
12694 kvm_flush_pml_buffers(kvm);
12695}
12696
Bandan Dasc5f983f2017-05-05 15:25:14 -040012697static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12698{
12699 struct vmcs12 *vmcs12;
12700 struct vcpu_vmx *vmx = to_vmx(vcpu);
12701 gpa_t gpa;
12702 struct page *page = NULL;
12703 u64 *pml_address;
12704
12705 if (is_guest_mode(vcpu)) {
12706 WARN_ON_ONCE(vmx->nested.pml_full);
12707
12708 /*
12709 * Check if PML is enabled for the nested guest.
12710 * Whether eptp bit 6 is set is already checked
12711 * as part of A/D emulation.
12712 */
12713 vmcs12 = get_vmcs12(vcpu);
12714 if (!nested_cpu_has_pml(vmcs12))
12715 return 0;
12716
Dan Carpenter47698862017-05-10 22:43:17 +030012717 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012718 vmx->nested.pml_full = true;
12719 return 1;
12720 }
12721
12722 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12723
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012724 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12725 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012726 return 0;
12727
12728 pml_address = kmap(page);
12729 pml_address[vmcs12->guest_pml_index--] = gpa;
12730 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012731 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012732 }
12733
12734 return 0;
12735}
12736
Kai Huang843e4332015-01-28 10:54:28 +080012737static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12738 struct kvm_memory_slot *memslot,
12739 gfn_t offset, unsigned long mask)
12740{
12741 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12742}
12743
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012744static void __pi_post_block(struct kvm_vcpu *vcpu)
12745{
12746 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12747 struct pi_desc old, new;
12748 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012749
12750 do {
12751 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012752 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12753 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012754
12755 dest = cpu_physical_id(vcpu->cpu);
12756
12757 if (x2apic_enabled())
12758 new.ndst = dest;
12759 else
12760 new.ndst = (dest << 8) & 0xFF00;
12761
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012762 /* set 'NV' to 'notification vector' */
12763 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012764 } while (cmpxchg64(&pi_desc->control, old.control,
12765 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012766
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012767 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12768 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012769 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012770 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012771 vcpu->pre_pcpu = -1;
12772 }
12773}
12774
Feng Wuefc64402015-09-18 22:29:51 +080012775/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012776 * This routine does the following things for vCPU which is going
12777 * to be blocked if VT-d PI is enabled.
12778 * - Store the vCPU to the wakeup list, so when interrupts happen
12779 * we can find the right vCPU to wake up.
12780 * - Change the Posted-interrupt descriptor as below:
12781 * 'NDST' <-- vcpu->pre_pcpu
12782 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12783 * - If 'ON' is set during this process, which means at least one
12784 * interrupt is posted for this vCPU, we cannot block it, in
12785 * this case, return 1, otherwise, return 0.
12786 *
12787 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012788static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012789{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012790 unsigned int dest;
12791 struct pi_desc old, new;
12792 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12793
12794 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012795 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12796 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012797 return 0;
12798
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012799 WARN_ON(irqs_disabled());
12800 local_irq_disable();
12801 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12802 vcpu->pre_pcpu = vcpu->cpu;
12803 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12804 list_add_tail(&vcpu->blocked_vcpu_list,
12805 &per_cpu(blocked_vcpu_on_cpu,
12806 vcpu->pre_pcpu));
12807 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12808 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012809
12810 do {
12811 old.control = new.control = pi_desc->control;
12812
Feng Wubf9f6ac2015-09-18 22:29:55 +080012813 WARN((pi_desc->sn == 1),
12814 "Warning: SN field of posted-interrupts "
12815 "is set before blocking\n");
12816
12817 /*
12818 * Since vCPU can be preempted during this process,
12819 * vcpu->cpu could be different with pre_pcpu, we
12820 * need to set pre_pcpu as the destination of wakeup
12821 * notification event, then we can find the right vCPU
12822 * to wakeup in wakeup handler if interrupts happen
12823 * when the vCPU is in blocked state.
12824 */
12825 dest = cpu_physical_id(vcpu->pre_pcpu);
12826
12827 if (x2apic_enabled())
12828 new.ndst = dest;
12829 else
12830 new.ndst = (dest << 8) & 0xFF00;
12831
12832 /* set 'NV' to 'wakeup vector' */
12833 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012834 } while (cmpxchg64(&pi_desc->control, old.control,
12835 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012836
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012837 /* We should not block the vCPU if an interrupt is posted for it. */
12838 if (pi_test_on(pi_desc) == 1)
12839 __pi_post_block(vcpu);
12840
12841 local_irq_enable();
12842 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012843}
12844
Yunhong Jiangbc225122016-06-13 14:19:58 -070012845static int vmx_pre_block(struct kvm_vcpu *vcpu)
12846{
12847 if (pi_pre_block(vcpu))
12848 return 1;
12849
Yunhong Jiang64672c92016-06-13 14:19:59 -070012850 if (kvm_lapic_hv_timer_in_use(vcpu))
12851 kvm_lapic_switch_to_sw_timer(vcpu);
12852
Yunhong Jiangbc225122016-06-13 14:19:58 -070012853 return 0;
12854}
12855
12856static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012857{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012858 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012859 return;
12860
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012861 WARN_ON(irqs_disabled());
12862 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012863 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012864 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012865}
12866
Yunhong Jiangbc225122016-06-13 14:19:58 -070012867static void vmx_post_block(struct kvm_vcpu *vcpu)
12868{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012869 if (kvm_x86_ops->set_hv_timer)
12870 kvm_lapic_switch_to_hv_timer(vcpu);
12871
Yunhong Jiangbc225122016-06-13 14:19:58 -070012872 pi_post_block(vcpu);
12873}
12874
Feng Wubf9f6ac2015-09-18 22:29:55 +080012875/*
Feng Wuefc64402015-09-18 22:29:51 +080012876 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12877 *
12878 * @kvm: kvm
12879 * @host_irq: host irq of the interrupt
12880 * @guest_irq: gsi of the interrupt
12881 * @set: set or unset PI
12882 * returns 0 on success, < 0 on failure
12883 */
12884static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12885 uint32_t guest_irq, bool set)
12886{
12887 struct kvm_kernel_irq_routing_entry *e;
12888 struct kvm_irq_routing_table *irq_rt;
12889 struct kvm_lapic_irq irq;
12890 struct kvm_vcpu *vcpu;
12891 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012892 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012893
12894 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012895 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12896 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012897 return 0;
12898
12899 idx = srcu_read_lock(&kvm->irq_srcu);
12900 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012901 if (guest_irq >= irq_rt->nr_rt_entries ||
12902 hlist_empty(&irq_rt->map[guest_irq])) {
12903 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12904 guest_irq, irq_rt->nr_rt_entries);
12905 goto out;
12906 }
Feng Wuefc64402015-09-18 22:29:51 +080012907
12908 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12909 if (e->type != KVM_IRQ_ROUTING_MSI)
12910 continue;
12911 /*
12912 * VT-d PI cannot support posting multicast/broadcast
12913 * interrupts to a vCPU, we still use interrupt remapping
12914 * for these kind of interrupts.
12915 *
12916 * For lowest-priority interrupts, we only support
12917 * those with single CPU as the destination, e.g. user
12918 * configures the interrupts via /proc/irq or uses
12919 * irqbalance to make the interrupts single-CPU.
12920 *
12921 * We will support full lowest-priority interrupt later.
12922 */
12923
Radim Krčmář371313132016-07-12 22:09:27 +020012924 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012925 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12926 /*
12927 * Make sure the IRTE is in remapped mode if
12928 * we don't handle it in posted mode.
12929 */
12930 ret = irq_set_vcpu_affinity(host_irq, NULL);
12931 if (ret < 0) {
12932 printk(KERN_INFO
12933 "failed to back to remapped mode, irq: %u\n",
12934 host_irq);
12935 goto out;
12936 }
12937
Feng Wuefc64402015-09-18 22:29:51 +080012938 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012939 }
Feng Wuefc64402015-09-18 22:29:51 +080012940
12941 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12942 vcpu_info.vector = irq.vector;
12943
hu huajun2698d822018-04-11 15:16:40 +080012944 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012945 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12946
12947 if (set)
12948 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012949 else
Feng Wuefc64402015-09-18 22:29:51 +080012950 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012951
12952 if (ret < 0) {
12953 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12954 __func__);
12955 goto out;
12956 }
12957 }
12958
12959 ret = 0;
12960out:
12961 srcu_read_unlock(&kvm->irq_srcu, idx);
12962 return ret;
12963}
12964
Ashok Rajc45dcc72016-06-22 14:59:56 +080012965static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12966{
12967 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12968 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12969 FEATURE_CONTROL_LMCE;
12970 else
12971 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12972 ~FEATURE_CONTROL_LMCE;
12973}
12974
Ladi Prosek72d7b372017-10-11 16:54:41 +020012975static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12976{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012977 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12978 if (to_vmx(vcpu)->nested.nested_run_pending)
12979 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012980 return 1;
12981}
12982
Ladi Prosek0234bf82017-10-11 16:54:40 +020012983static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12984{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012985 struct vcpu_vmx *vmx = to_vmx(vcpu);
12986
12987 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12988 if (vmx->nested.smm.guest_mode)
12989 nested_vmx_vmexit(vcpu, -1, 0, 0);
12990
12991 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12992 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012993 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012994 return 0;
12995}
12996
12997static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12998{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012999 struct vcpu_vmx *vmx = to_vmx(vcpu);
13000 int ret;
13001
13002 if (vmx->nested.smm.vmxon) {
13003 vmx->nested.vmxon = true;
13004 vmx->nested.smm.vmxon = false;
13005 }
13006
13007 if (vmx->nested.smm.guest_mode) {
13008 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013009 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013010 vcpu->arch.hflags |= HF_SMM_MASK;
13011 if (ret)
13012 return ret;
13013
13014 vmx->nested.smm.guest_mode = false;
13015 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013016 return 0;
13017}
13018
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013019static int enable_smi_window(struct kvm_vcpu *vcpu)
13020{
13021 return 0;
13022}
13023
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013024static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13025 struct kvm_nested_state __user *user_kvm_nested_state,
13026 u32 user_data_size)
13027{
13028 struct vcpu_vmx *vmx;
13029 struct vmcs12 *vmcs12;
13030 struct kvm_nested_state kvm_state = {
13031 .flags = 0,
13032 .format = 0,
13033 .size = sizeof(kvm_state),
13034 .vmx.vmxon_pa = -1ull,
13035 .vmx.vmcs_pa = -1ull,
13036 };
13037
13038 if (!vcpu)
13039 return kvm_state.size + 2 * VMCS12_SIZE;
13040
13041 vmx = to_vmx(vcpu);
13042 vmcs12 = get_vmcs12(vcpu);
13043 if (nested_vmx_allowed(vcpu) &&
13044 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13045 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13046 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13047
13048 if (vmx->nested.current_vmptr != -1ull)
13049 kvm_state.size += VMCS12_SIZE;
13050
13051 if (vmx->nested.smm.vmxon)
13052 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13053
13054 if (vmx->nested.smm.guest_mode)
13055 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13056
13057 if (is_guest_mode(vcpu)) {
13058 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13059
13060 if (vmx->nested.nested_run_pending)
13061 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13062 }
13063 }
13064
13065 if (user_data_size < kvm_state.size)
13066 goto out;
13067
13068 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13069 return -EFAULT;
13070
13071 if (vmx->nested.current_vmptr == -1ull)
13072 goto out;
13073
13074 /*
13075 * When running L2, the authoritative vmcs12 state is in the
13076 * vmcs02. When running L1, the authoritative vmcs12 state is
13077 * in the shadow vmcs linked to vmcs01, unless
13078 * sync_shadow_vmcs is set, in which case, the authoritative
13079 * vmcs12 state is in the vmcs12 already.
13080 */
13081 if (is_guest_mode(vcpu))
13082 sync_vmcs12(vcpu, vmcs12);
13083 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13084 copy_shadow_to_vmcs12(vmx);
13085
13086 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13087 return -EFAULT;
13088
13089out:
13090 return kvm_state.size;
13091}
13092
13093static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13094 struct kvm_nested_state __user *user_kvm_nested_state,
13095 struct kvm_nested_state *kvm_state)
13096{
13097 struct vcpu_vmx *vmx = to_vmx(vcpu);
13098 struct vmcs12 *vmcs12;
13099 u32 exit_qual;
13100 int ret;
13101
13102 if (kvm_state->format != 0)
13103 return -EINVAL;
13104
13105 if (!nested_vmx_allowed(vcpu))
13106 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13107
13108 if (kvm_state->vmx.vmxon_pa == -1ull) {
13109 if (kvm_state->vmx.smm.flags)
13110 return -EINVAL;
13111
13112 if (kvm_state->vmx.vmcs_pa != -1ull)
13113 return -EINVAL;
13114
13115 vmx_leave_nested(vcpu);
13116 return 0;
13117 }
13118
13119 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13120 return -EINVAL;
13121
13122 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13123 return -EINVAL;
13124
13125 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13126 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13127 return -EINVAL;
13128
13129 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13130 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13131 return -EINVAL;
13132
13133 if (kvm_state->vmx.smm.flags &
13134 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13135 return -EINVAL;
13136
13137 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13138 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13139 return -EINVAL;
13140
13141 vmx_leave_nested(vcpu);
13142 if (kvm_state->vmx.vmxon_pa == -1ull)
13143 return 0;
13144
13145 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13146 ret = enter_vmx_operation(vcpu);
13147 if (ret)
13148 return ret;
13149
13150 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13151
13152 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13153 vmx->nested.smm.vmxon = true;
13154 vmx->nested.vmxon = false;
13155
13156 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13157 vmx->nested.smm.guest_mode = true;
13158 }
13159
13160 vmcs12 = get_vmcs12(vcpu);
13161 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13162 return -EFAULT;
13163
13164 if (vmcs12->revision_id != VMCS12_REVISION)
13165 return -EINVAL;
13166
13167 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13168 return 0;
13169
13170 vmx->nested.nested_run_pending =
13171 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13172
13173 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13174 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13175 return -EINVAL;
13176
13177 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13178 vmx->nested.nested_run_pending = 1;
13179
13180 vmx->nested.dirty_vmcs12 = true;
13181 ret = enter_vmx_non_root_mode(vcpu, NULL);
13182 if (ret)
13183 return -EINVAL;
13184
13185 return 0;
13186}
13187
Kees Cook404f6aa2016-08-08 16:29:06 -070013188static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013189 .cpu_has_kvm_support = cpu_has_kvm_support,
13190 .disabled_by_bios = vmx_disabled_by_bios,
13191 .hardware_setup = hardware_setup,
13192 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013193 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013194 .hardware_enable = hardware_enable,
13195 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013196 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013197 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013198
Wanpeng Lib31c1142018-03-12 04:53:04 -070013199 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013200 .vm_alloc = vmx_vm_alloc,
13201 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013202
Avi Kivity6aa8b732006-12-10 02:21:36 -080013203 .vcpu_create = vmx_create_vcpu,
13204 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013205 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013206
Avi Kivity04d2cc72007-09-10 18:10:54 +030013207 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013208 .vcpu_load = vmx_vcpu_load,
13209 .vcpu_put = vmx_vcpu_put,
13210
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013211 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013212 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013213 .get_msr = vmx_get_msr,
13214 .set_msr = vmx_set_msr,
13215 .get_segment_base = vmx_get_segment_base,
13216 .get_segment = vmx_get_segment,
13217 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013218 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013219 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013220 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013221 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013222 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013223 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013224 .set_cr3 = vmx_set_cr3,
13225 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013226 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013227 .get_idt = vmx_get_idt,
13228 .set_idt = vmx_set_idt,
13229 .get_gdt = vmx_get_gdt,
13230 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013231 .get_dr6 = vmx_get_dr6,
13232 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013233 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013234 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013235 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013236 .get_rflags = vmx_get_rflags,
13237 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013238
Avi Kivity6aa8b732006-12-10 02:21:36 -080013239 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013240
Avi Kivity6aa8b732006-12-10 02:21:36 -080013241 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013242 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013243 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013244 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13245 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013246 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013247 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013248 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013249 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013250 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013251 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013252 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013253 .get_nmi_mask = vmx_get_nmi_mask,
13254 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013255 .enable_nmi_window = enable_nmi_window,
13256 .enable_irq_window = enable_irq_window,
13257 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013258 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013259 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013260 .get_enable_apicv = vmx_get_enable_apicv,
13261 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013262 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013263 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013264 .hwapic_irr_update = vmx_hwapic_irr_update,
13265 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013266 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13267 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013268
Izik Eiduscbc94022007-10-25 00:29:55 +020013269 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013270 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013271 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013272 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013273
Avi Kivity586f9602010-11-18 13:09:54 +020013274 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013275
Sheng Yang17cc3932010-01-05 19:02:27 +080013276 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013277
13278 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013279
13280 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013281 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013282
13283 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013284
13285 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013286
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013287 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013288 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013289
13290 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013291
13292 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013293 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013294 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013295 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013296 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013297
13298 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013299
13300 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013301
13302 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13303 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13304 .flush_log_dirty = vmx_flush_log_dirty,
13305 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013306 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013307
Feng Wubf9f6ac2015-09-18 22:29:55 +080013308 .pre_block = vmx_pre_block,
13309 .post_block = vmx_post_block,
13310
Wei Huang25462f72015-06-19 15:45:05 +020013311 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013312
13313 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013314
13315#ifdef CONFIG_X86_64
13316 .set_hv_timer = vmx_set_hv_timer,
13317 .cancel_hv_timer = vmx_cancel_hv_timer,
13318#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013319
13320 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013321
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013322 .get_nested_state = vmx_get_nested_state,
13323 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013324 .get_vmcs12_pages = nested_get_vmcs12_pages,
13325
Ladi Prosek72d7b372017-10-11 16:54:41 +020013326 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013327 .pre_enter_smm = vmx_pre_enter_smm,
13328 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013329 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013330};
13331
13332static int __init vmx_init(void)
13333{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013334 int r;
13335
13336#if IS_ENABLED(CONFIG_HYPERV)
13337 /*
13338 * Enlightened VMCS usage should be recommended and the host needs
13339 * to support eVMCS v1 or above. We can also disable eVMCS support
13340 * with module parameter.
13341 */
13342 if (enlightened_vmcs &&
13343 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13344 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13345 KVM_EVMCS_VERSION) {
13346 int cpu;
13347
13348 /* Check that we have assist pages on all online CPUs */
13349 for_each_online_cpu(cpu) {
13350 if (!hv_get_vp_assist_page(cpu)) {
13351 enlightened_vmcs = false;
13352 break;
13353 }
13354 }
13355
13356 if (enlightened_vmcs) {
13357 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13358 static_branch_enable(&enable_evmcs);
13359 }
13360 } else {
13361 enlightened_vmcs = false;
13362 }
13363#endif
13364
13365 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013366 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013367 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013368 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013369
Dave Young2965faa2015-09-09 15:38:55 -070013370#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013371 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13372 crash_vmclear_local_loaded_vmcss);
13373#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013374 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013375
He, Qingfdef3ad2007-04-30 09:45:24 +030013376 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013377}
13378
13379static void __exit vmx_exit(void)
13380{
Dave Young2965faa2015-09-09 15:38:55 -070013381#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013382 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013383 synchronize_rcu();
13384#endif
13385
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013386 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013387
13388#if IS_ENABLED(CONFIG_HYPERV)
13389 if (static_branch_unlikely(&enable_evmcs)) {
13390 int cpu;
13391 struct hv_vp_assist_page *vp_ap;
13392 /*
13393 * Reset everything to support using non-enlightened VMCS
13394 * access later (e.g. when we reload the module with
13395 * enlightened_vmcs=0)
13396 */
13397 for_each_online_cpu(cpu) {
13398 vp_ap = hv_get_vp_assist_page(cpu);
13399
13400 if (!vp_ap)
13401 continue;
13402
13403 vp_ap->current_nested_vmcs = 0;
13404 vp_ap->enlighten_vmentry = 0;
13405 }
13406
13407 static_branch_disable(&enable_evmcs);
13408 }
13409#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013410}
13411
13412module_init(vmx_init)
13413module_exit(vmx_exit)