blob: 48989f78be60e6f6f97a3dd2ce2da1441fc3ab47 [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 Kiszkaf41245002014-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>
David Woodhouse117cc7a2018-01-12 11:11:27 +000054#include <asm/nospec-branch.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 Kiszkaf41245002014-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 Kiszkaf41245002014-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;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000805 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400806 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200807 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300808 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300809 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300810 struct kvm_segment segs[8];
811 } rmode;
812 struct {
813 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300814 struct kvm_save_segment {
815 u16 selector;
816 unsigned long base;
817 u32 limit;
818 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300819 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300820 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800821 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300822 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200823
Andi Kleena0861c02009-06-08 17:37:09 +0800824 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800825
Yang Zhang01e439b2013-04-11 19:25:12 +0800826 /* Posted interrupt descriptor */
827 struct pi_desc pi_desc;
828
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300829 /* Support for a guest hypervisor (nested VMX) */
830 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200831
832 /* Dynamic PLE window. */
833 int ple_window;
834 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800835
836 /* Support for PML */
837#define PML_ENTITY_NUM 512
838 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800839
Yunhong Jiang64672c92016-06-13 14:19:59 -0700840 /* apic deadline value in host tsc */
841 u64 hv_deadline_tsc;
842
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800843 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800844
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800845 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800846
Wanpeng Li74c55932017-11-29 01:31:20 -0800847 unsigned long host_debugctlmsr;
848
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800849 /*
850 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
851 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
852 * in msr_ia32_feature_control_valid_bits.
853 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800854 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800855 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400856};
857
Avi Kivity2fb92db2011-04-27 19:42:18 +0300858enum segment_cache_field {
859 SEG_FIELD_SEL = 0,
860 SEG_FIELD_BASE = 1,
861 SEG_FIELD_LIMIT = 2,
862 SEG_FIELD_AR = 3,
863
864 SEG_FIELD_NR = 4
865};
866
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700867static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
868{
869 return container_of(kvm, struct kvm_vmx, kvm);
870}
871
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400872static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
873{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000874 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400875}
876
Feng Wuefc64402015-09-18 22:29:51 +0800877static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
878{
879 return &(to_vmx(vcpu)->pi_desc);
880}
881
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800882#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300883#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800884#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
885#define FIELD64(number, name) \
886 FIELD(number, name), \
887 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300888
Abel Gordon4607c2d2013-04-18 14:35:55 +0300889
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100890static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100891#define SHADOW_FIELD_RO(x) x,
892#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300893};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400894static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300895 ARRAY_SIZE(shadow_read_only_fields);
896
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100897static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100898#define SHADOW_FIELD_RW(x) x,
899#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300900};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400901static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300902 ARRAY_SIZE(shadow_read_write_fields);
903
Mathias Krause772e0312012-08-30 01:30:19 +0200904static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300905 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800906 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300907 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
908 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
909 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
910 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
911 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
912 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
913 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
914 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800915 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400916 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300917 FIELD(HOST_ES_SELECTOR, host_es_selector),
918 FIELD(HOST_CS_SELECTOR, host_cs_selector),
919 FIELD(HOST_SS_SELECTOR, host_ss_selector),
920 FIELD(HOST_DS_SELECTOR, host_ds_selector),
921 FIELD(HOST_FS_SELECTOR, host_fs_selector),
922 FIELD(HOST_GS_SELECTOR, host_gs_selector),
923 FIELD(HOST_TR_SELECTOR, host_tr_selector),
924 FIELD64(IO_BITMAP_A, io_bitmap_a),
925 FIELD64(IO_BITMAP_B, io_bitmap_b),
926 FIELD64(MSR_BITMAP, msr_bitmap),
927 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
928 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
929 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700930 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300931 FIELD64(TSC_OFFSET, tsc_offset),
932 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
933 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800934 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400935 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300936 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800937 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
938 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
939 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
940 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400941 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700942 FIELD64(VMREAD_BITMAP, vmread_bitmap),
943 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800944 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300945 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
946 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
947 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
948 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
949 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
950 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
951 FIELD64(GUEST_PDPTR0, guest_pdptr0),
952 FIELD64(GUEST_PDPTR1, guest_pdptr1),
953 FIELD64(GUEST_PDPTR2, guest_pdptr2),
954 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100955 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300956 FIELD64(HOST_IA32_PAT, host_ia32_pat),
957 FIELD64(HOST_IA32_EFER, host_ia32_efer),
958 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
959 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
960 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
961 FIELD(EXCEPTION_BITMAP, exception_bitmap),
962 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
963 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
964 FIELD(CR3_TARGET_COUNT, cr3_target_count),
965 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
966 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
967 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
968 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
969 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
970 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
971 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
972 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
973 FIELD(TPR_THRESHOLD, tpr_threshold),
974 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
975 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
976 FIELD(VM_EXIT_REASON, vm_exit_reason),
977 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
978 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
979 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
980 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
981 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
982 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
983 FIELD(GUEST_ES_LIMIT, guest_es_limit),
984 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
985 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
986 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
987 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
988 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
989 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
990 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
991 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
992 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
993 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
994 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
995 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
996 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
997 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
998 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
999 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1000 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1001 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1002 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1003 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1004 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001005 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001006 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1007 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1008 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1009 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1010 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1011 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1012 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1013 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1014 FIELD(EXIT_QUALIFICATION, exit_qualification),
1015 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1016 FIELD(GUEST_CR0, guest_cr0),
1017 FIELD(GUEST_CR3, guest_cr3),
1018 FIELD(GUEST_CR4, guest_cr4),
1019 FIELD(GUEST_ES_BASE, guest_es_base),
1020 FIELD(GUEST_CS_BASE, guest_cs_base),
1021 FIELD(GUEST_SS_BASE, guest_ss_base),
1022 FIELD(GUEST_DS_BASE, guest_ds_base),
1023 FIELD(GUEST_FS_BASE, guest_fs_base),
1024 FIELD(GUEST_GS_BASE, guest_gs_base),
1025 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1026 FIELD(GUEST_TR_BASE, guest_tr_base),
1027 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1028 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1029 FIELD(GUEST_DR7, guest_dr7),
1030 FIELD(GUEST_RSP, guest_rsp),
1031 FIELD(GUEST_RIP, guest_rip),
1032 FIELD(GUEST_RFLAGS, guest_rflags),
1033 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1034 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1035 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1036 FIELD(HOST_CR0, host_cr0),
1037 FIELD(HOST_CR3, host_cr3),
1038 FIELD(HOST_CR4, host_cr4),
1039 FIELD(HOST_FS_BASE, host_fs_base),
1040 FIELD(HOST_GS_BASE, host_gs_base),
1041 FIELD(HOST_TR_BASE, host_tr_base),
1042 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1043 FIELD(HOST_IDTR_BASE, host_idtr_base),
1044 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1045 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1046 FIELD(HOST_RSP, host_rsp),
1047 FIELD(HOST_RIP, host_rip),
1048};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001049
1050static inline short vmcs_field_to_offset(unsigned long field)
1051{
Dan Williams085331d2018-01-31 17:47:03 -08001052 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1053 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001054 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001055
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001056 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001057 return -ENOENT;
1058
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001059 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001060 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001061 return -ENOENT;
1062
Linus Torvalds15303ba2018-02-10 13:16:35 -08001063 index = array_index_nospec(index, size);
1064 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001065 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001066 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001067 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001068}
1069
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001070static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1071{
David Matlack4f2777b2016-07-13 17:16:37 -07001072 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001073}
1074
Peter Feiner995f00a2017-06-30 17:26:32 -07001075static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001076static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001077static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001078static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001079static void vmx_set_segment(struct kvm_vcpu *vcpu,
1080 struct kvm_segment *var, int seg);
1081static void vmx_get_segment(struct kvm_vcpu *vcpu,
1082 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001083static bool guest_state_valid(struct kvm_vcpu *vcpu);
1084static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001085static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001086static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1087static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1088static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1089 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001090static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001091static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1092 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001093
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1095static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001096/*
1097 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1098 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1099 */
1100static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101
Feng Wubf9f6ac2015-09-18 22:29:55 +08001102/*
1103 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1104 * can find which vCPU should be waken up.
1105 */
1106static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1107static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1108
Radim Krčmář23611332016-09-29 22:41:33 +02001109enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001110 VMX_VMREAD_BITMAP,
1111 VMX_VMWRITE_BITMAP,
1112 VMX_BITMAP_NR
1113};
1114
1115static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1116
Radim Krčmář23611332016-09-29 22:41:33 +02001117#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1118#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001119
Avi Kivity110312c2010-12-21 12:54:20 +02001120static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001121static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001122
Sheng Yang2384d2b2008-01-17 15:14:33 +08001123static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1124static DEFINE_SPINLOCK(vmx_vpid_lock);
1125
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001126static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 int size;
1128 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001129 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001131 u32 pin_based_exec_ctrl;
1132 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001133 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001134 u32 vmexit_ctrl;
1135 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001136 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001137} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138
Hannes Ederefff9e52008-11-28 17:02:06 +01001139static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001140 u32 ept;
1141 u32 vpid;
1142} vmx_capability;
1143
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144#define VMX_SEGMENT_FIELD(seg) \
1145 [VCPU_SREG_##seg] = { \
1146 .selector = GUEST_##seg##_SELECTOR, \
1147 .base = GUEST_##seg##_BASE, \
1148 .limit = GUEST_##seg##_LIMIT, \
1149 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1150 }
1151
Mathias Krause772e0312012-08-30 01:30:19 +02001152static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 unsigned selector;
1154 unsigned base;
1155 unsigned limit;
1156 unsigned ar_bytes;
1157} kvm_vmx_segment_fields[] = {
1158 VMX_SEGMENT_FIELD(CS),
1159 VMX_SEGMENT_FIELD(DS),
1160 VMX_SEGMENT_FIELD(ES),
1161 VMX_SEGMENT_FIELD(FS),
1162 VMX_SEGMENT_FIELD(GS),
1163 VMX_SEGMENT_FIELD(SS),
1164 VMX_SEGMENT_FIELD(TR),
1165 VMX_SEGMENT_FIELD(LDTR),
1166};
1167
Avi Kivity26bb0982009-09-07 11:14:12 +03001168static u64 host_efer;
1169
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001170static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1171
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001172/*
Brian Gerst8c065852010-07-17 09:03:26 -04001173 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001174 * away by decrementing the array size.
1175 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001177#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001178 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001180 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001183DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1184
1185#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1186
1187#define KVM_EVMCS_VERSION 1
1188
1189#if IS_ENABLED(CONFIG_HYPERV)
1190static bool __read_mostly enlightened_vmcs = true;
1191module_param(enlightened_vmcs, bool, 0444);
1192
1193static inline void evmcs_write64(unsigned long field, u64 value)
1194{
1195 u16 clean_field;
1196 int offset = get_evmcs_offset(field, &clean_field);
1197
1198 if (offset < 0)
1199 return;
1200
1201 *(u64 *)((char *)current_evmcs + offset) = value;
1202
1203 current_evmcs->hv_clean_fields &= ~clean_field;
1204}
1205
1206static inline void evmcs_write32(unsigned long field, u32 value)
1207{
1208 u16 clean_field;
1209 int offset = get_evmcs_offset(field, &clean_field);
1210
1211 if (offset < 0)
1212 return;
1213
1214 *(u32 *)((char *)current_evmcs + offset) = value;
1215 current_evmcs->hv_clean_fields &= ~clean_field;
1216}
1217
1218static inline void evmcs_write16(unsigned long field, u16 value)
1219{
1220 u16 clean_field;
1221 int offset = get_evmcs_offset(field, &clean_field);
1222
1223 if (offset < 0)
1224 return;
1225
1226 *(u16 *)((char *)current_evmcs + offset) = value;
1227 current_evmcs->hv_clean_fields &= ~clean_field;
1228}
1229
1230static inline u64 evmcs_read64(unsigned long field)
1231{
1232 int offset = get_evmcs_offset(field, NULL);
1233
1234 if (offset < 0)
1235 return 0;
1236
1237 return *(u64 *)((char *)current_evmcs + offset);
1238}
1239
1240static inline u32 evmcs_read32(unsigned long field)
1241{
1242 int offset = get_evmcs_offset(field, NULL);
1243
1244 if (offset < 0)
1245 return 0;
1246
1247 return *(u32 *)((char *)current_evmcs + offset);
1248}
1249
1250static inline u16 evmcs_read16(unsigned long field)
1251{
1252 int offset = get_evmcs_offset(field, NULL);
1253
1254 if (offset < 0)
1255 return 0;
1256
1257 return *(u16 *)((char *)current_evmcs + offset);
1258}
1259
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001260static inline void evmcs_touch_msr_bitmap(void)
1261{
1262 if (unlikely(!current_evmcs))
1263 return;
1264
1265 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1266 current_evmcs->hv_clean_fields &=
1267 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1268}
1269
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001270static void evmcs_load(u64 phys_addr)
1271{
1272 struct hv_vp_assist_page *vp_ap =
1273 hv_get_vp_assist_page(smp_processor_id());
1274
1275 vp_ap->current_nested_vmcs = phys_addr;
1276 vp_ap->enlighten_vmentry = 1;
1277}
1278
1279static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1280{
1281 /*
1282 * Enlightened VMCSv1 doesn't support these:
1283 *
1284 * POSTED_INTR_NV = 0x00000002,
1285 * GUEST_INTR_STATUS = 0x00000810,
1286 * APIC_ACCESS_ADDR = 0x00002014,
1287 * POSTED_INTR_DESC_ADDR = 0x00002016,
1288 * EOI_EXIT_BITMAP0 = 0x0000201c,
1289 * EOI_EXIT_BITMAP1 = 0x0000201e,
1290 * EOI_EXIT_BITMAP2 = 0x00002020,
1291 * EOI_EXIT_BITMAP3 = 0x00002022,
1292 */
1293 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1294 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1295 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1296 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1297 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1298 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1299 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1300
1301 /*
1302 * GUEST_PML_INDEX = 0x00000812,
1303 * PML_ADDRESS = 0x0000200e,
1304 */
1305 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1306
1307 /* VM_FUNCTION_CONTROL = 0x00002018, */
1308 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1309
1310 /*
1311 * EPTP_LIST_ADDRESS = 0x00002024,
1312 * VMREAD_BITMAP = 0x00002026,
1313 * VMWRITE_BITMAP = 0x00002028,
1314 */
1315 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1316
1317 /*
1318 * TSC_MULTIPLIER = 0x00002032,
1319 */
1320 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1321
1322 /*
1323 * PLE_GAP = 0x00004020,
1324 * PLE_WINDOW = 0x00004022,
1325 */
1326 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1327
1328 /*
1329 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1330 */
1331 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1332
1333 /*
1334 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1335 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1336 */
1337 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1338 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1339
1340 /*
1341 * Currently unsupported in KVM:
1342 * GUEST_IA32_RTIT_CTL = 0x00002814,
1343 */
1344}
1345#else /* !IS_ENABLED(CONFIG_HYPERV) */
1346static inline void evmcs_write64(unsigned long field, u64 value) {}
1347static inline void evmcs_write32(unsigned long field, u32 value) {}
1348static inline void evmcs_write16(unsigned long field, u16 value) {}
1349static inline u64 evmcs_read64(unsigned long field) { return 0; }
1350static inline u32 evmcs_read32(unsigned long field) { return 0; }
1351static inline u16 evmcs_read16(unsigned long field) { return 0; }
1352static inline void evmcs_load(u64 phys_addr) {}
1353static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001354static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001355#endif /* IS_ENABLED(CONFIG_HYPERV) */
1356
Jan Kiszka5bb16012016-02-09 20:14:21 +01001357static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358{
1359 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1360 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001361 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1362}
1363
Jan Kiszka6f054852016-02-09 20:15:18 +01001364static inline bool is_debug(u32 intr_info)
1365{
1366 return is_exception_n(intr_info, DB_VECTOR);
1367}
1368
1369static inline bool is_breakpoint(u32 intr_info)
1370{
1371 return is_exception_n(intr_info, BP_VECTOR);
1372}
1373
Jan Kiszka5bb16012016-02-09 20:14:21 +01001374static inline bool is_page_fault(u32 intr_info)
1375{
1376 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377}
1378
Gui Jianfeng31299942010-03-15 17:29:09 +08001379static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001380{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001381 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001382}
1383
Gui Jianfeng31299942010-03-15 17:29:09 +08001384static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001385{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001386 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001387}
1388
Liran Alon9e869482018-03-12 13:12:51 +02001389static inline bool is_gp_fault(u32 intr_info)
1390{
1391 return is_exception_n(intr_info, GP_VECTOR);
1392}
1393
Gui Jianfeng31299942010-03-15 17:29:09 +08001394static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395{
1396 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1397 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1398}
1399
Gui Jianfeng31299942010-03-15 17:29:09 +08001400static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001401{
1402 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1403 INTR_INFO_VALID_MASK)) ==
1404 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1405}
1406
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001407/* Undocumented: icebp/int1 */
1408static inline bool is_icebp(u32 intr_info)
1409{
1410 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1411 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1412}
1413
Gui Jianfeng31299942010-03-15 17:29:09 +08001414static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001415{
Sheng Yang04547152009-04-01 15:52:31 +08001416 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001417}
1418
Gui Jianfeng31299942010-03-15 17:29:09 +08001419static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001420{
Sheng Yang04547152009-04-01 15:52:31 +08001421 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001422}
1423
Paolo Bonzini35754c92015-07-29 12:05:37 +02001424static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001425{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001426 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001430{
Sheng Yang04547152009-04-01 15:52:31 +08001431 return vmcs_config.cpu_based_exec_ctrl &
1432 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001433}
1434
Avi Kivity774ead32007-12-26 13:57:04 +02001435static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001436{
Sheng Yang04547152009-04-01 15:52:31 +08001437 return vmcs_config.cpu_based_2nd_exec_ctrl &
1438 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1439}
1440
Yang Zhang8d146952013-01-25 10:18:50 +08001441static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1442{
1443 return vmcs_config.cpu_based_2nd_exec_ctrl &
1444 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1445}
1446
Yang Zhang83d4c282013-01-25 10:18:49 +08001447static inline bool cpu_has_vmx_apic_register_virt(void)
1448{
1449 return vmcs_config.cpu_based_2nd_exec_ctrl &
1450 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1451}
1452
Yang Zhangc7c9c562013-01-25 10:18:51 +08001453static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1454{
1455 return vmcs_config.cpu_based_2nd_exec_ctrl &
1456 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1457}
1458
Yunhong Jiang64672c92016-06-13 14:19:59 -07001459/*
1460 * Comment's format: document - errata name - stepping - processor name.
1461 * Refer from
1462 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1463 */
1464static u32 vmx_preemption_cpu_tfms[] = {
1465/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
14660x000206E6,
1467/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1468/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1469/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
14700x00020652,
1471/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
14720x00020655,
1473/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1474/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1475/*
1476 * 320767.pdf - AAP86 - B1 -
1477 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1478 */
14790x000106E5,
1480/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
14810x000106A0,
1482/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
14830x000106A1,
1484/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
14850x000106A4,
1486 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1487 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1488 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
14890x000106A5,
1490};
1491
1492static inline bool cpu_has_broken_vmx_preemption_timer(void)
1493{
1494 u32 eax = cpuid_eax(0x00000001), i;
1495
1496 /* Clear the reserved bits */
1497 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001498 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001499 if (eax == vmx_preemption_cpu_tfms[i])
1500 return true;
1501
1502 return false;
1503}
1504
1505static inline bool cpu_has_vmx_preemption_timer(void)
1506{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001507 return vmcs_config.pin_based_exec_ctrl &
1508 PIN_BASED_VMX_PREEMPTION_TIMER;
1509}
1510
Yang Zhang01e439b2013-04-11 19:25:12 +08001511static inline bool cpu_has_vmx_posted_intr(void)
1512{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001513 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1514 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001515}
1516
1517static inline bool cpu_has_vmx_apicv(void)
1518{
1519 return cpu_has_vmx_apic_register_virt() &&
1520 cpu_has_vmx_virtual_intr_delivery() &&
1521 cpu_has_vmx_posted_intr();
1522}
1523
Sheng Yang04547152009-04-01 15:52:31 +08001524static inline bool cpu_has_vmx_flexpriority(void)
1525{
1526 return cpu_has_vmx_tpr_shadow() &&
1527 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001528}
1529
Marcelo Tosattie7997942009-06-11 12:07:40 -03001530static inline bool cpu_has_vmx_ept_execute_only(void)
1531{
Gui Jianfeng31299942010-03-15 17:29:09 +08001532 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001533}
1534
Marcelo Tosattie7997942009-06-11 12:07:40 -03001535static inline bool cpu_has_vmx_ept_2m_page(void)
1536{
Gui Jianfeng31299942010-03-15 17:29:09 +08001537 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001538}
1539
Sheng Yang878403b2010-01-05 19:02:29 +08001540static inline bool cpu_has_vmx_ept_1g_page(void)
1541{
Gui Jianfeng31299942010-03-15 17:29:09 +08001542 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001543}
1544
Sheng Yang4bc9b982010-06-02 14:05:24 +08001545static inline bool cpu_has_vmx_ept_4levels(void)
1546{
1547 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1548}
1549
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001550static inline bool cpu_has_vmx_ept_mt_wb(void)
1551{
1552 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1553}
1554
Yu Zhang855feb62017-08-24 20:27:55 +08001555static inline bool cpu_has_vmx_ept_5levels(void)
1556{
1557 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1558}
1559
Xudong Hao83c3a332012-05-28 19:33:35 +08001560static inline bool cpu_has_vmx_ept_ad_bits(void)
1561{
1562 return vmx_capability.ept & VMX_EPT_AD_BIT;
1563}
1564
Gui Jianfeng31299942010-03-15 17:29:09 +08001565static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001566{
Gui Jianfeng31299942010-03-15 17:29:09 +08001567 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001568}
1569
Gui Jianfeng31299942010-03-15 17:29:09 +08001570static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001571{
Gui Jianfeng31299942010-03-15 17:29:09 +08001572 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001573}
1574
Liran Aloncd9a4912018-05-22 17:16:15 +03001575static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1576{
1577 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1578}
1579
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001580static inline bool cpu_has_vmx_invvpid_single(void)
1581{
1582 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1583}
1584
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001585static inline bool cpu_has_vmx_invvpid_global(void)
1586{
1587 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1588}
1589
Wanpeng Li08d839c2017-03-23 05:30:08 -07001590static inline bool cpu_has_vmx_invvpid(void)
1591{
1592 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1593}
1594
Gui Jianfeng31299942010-03-15 17:29:09 +08001595static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001596{
Sheng Yang04547152009-04-01 15:52:31 +08001597 return vmcs_config.cpu_based_2nd_exec_ctrl &
1598 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001599}
1600
Gui Jianfeng31299942010-03-15 17:29:09 +08001601static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001602{
1603 return vmcs_config.cpu_based_2nd_exec_ctrl &
1604 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1605}
1606
Gui Jianfeng31299942010-03-15 17:29:09 +08001607static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001608{
1609 return vmcs_config.cpu_based_2nd_exec_ctrl &
1610 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1611}
1612
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001613static inline bool cpu_has_vmx_basic_inout(void)
1614{
1615 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1616}
1617
Paolo Bonzini35754c92015-07-29 12:05:37 +02001618static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001619{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001620 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001621}
1622
Gui Jianfeng31299942010-03-15 17:29:09 +08001623static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001624{
Sheng Yang04547152009-04-01 15:52:31 +08001625 return vmcs_config.cpu_based_2nd_exec_ctrl &
1626 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001627}
1628
Gui Jianfeng31299942010-03-15 17:29:09 +08001629static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001630{
1631 return vmcs_config.cpu_based_2nd_exec_ctrl &
1632 SECONDARY_EXEC_RDTSCP;
1633}
1634
Mao, Junjiead756a12012-07-02 01:18:48 +00001635static inline bool cpu_has_vmx_invpcid(void)
1636{
1637 return vmcs_config.cpu_based_2nd_exec_ctrl &
1638 SECONDARY_EXEC_ENABLE_INVPCID;
1639}
1640
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001641static inline bool cpu_has_virtual_nmis(void)
1642{
1643 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1644}
1645
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001646static inline bool cpu_has_vmx_wbinvd_exit(void)
1647{
1648 return vmcs_config.cpu_based_2nd_exec_ctrl &
1649 SECONDARY_EXEC_WBINVD_EXITING;
1650}
1651
Abel Gordonabc4fc52013-04-18 14:35:25 +03001652static inline bool cpu_has_vmx_shadow_vmcs(void)
1653{
1654 u64 vmx_msr;
1655 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1656 /* check if the cpu supports writing r/o exit information fields */
1657 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1658 return false;
1659
1660 return vmcs_config.cpu_based_2nd_exec_ctrl &
1661 SECONDARY_EXEC_SHADOW_VMCS;
1662}
1663
Kai Huang843e4332015-01-28 10:54:28 +08001664static inline bool cpu_has_vmx_pml(void)
1665{
1666 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1667}
1668
Haozhong Zhang64903d62015-10-20 15:39:09 +08001669static inline bool cpu_has_vmx_tsc_scaling(void)
1670{
1671 return vmcs_config.cpu_based_2nd_exec_ctrl &
1672 SECONDARY_EXEC_TSC_SCALING;
1673}
1674
Bandan Das2a499e42017-08-03 15:54:41 -04001675static inline bool cpu_has_vmx_vmfunc(void)
1676{
1677 return vmcs_config.cpu_based_2nd_exec_ctrl &
1678 SECONDARY_EXEC_ENABLE_VMFUNC;
1679}
1680
Sean Christopherson64f7a112018-04-30 10:01:06 -07001681static bool vmx_umip_emulated(void)
1682{
1683 return vmcs_config.cpu_based_2nd_exec_ctrl &
1684 SECONDARY_EXEC_DESC;
1685}
1686
Sheng Yang04547152009-04-01 15:52:31 +08001687static inline bool report_flexpriority(void)
1688{
1689 return flexpriority_enabled;
1690}
1691
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001692static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1693{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001694 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001695}
1696
Jim Mattsonf4160e42018-05-29 09:11:33 -07001697/*
1698 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1699 * to modify any valid field of the VMCS, or are the VM-exit
1700 * information fields read-only?
1701 */
1702static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1703{
1704 return to_vmx(vcpu)->nested.msrs.misc_low &
1705 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1706}
1707
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001708static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1709{
1710 return vmcs12->cpu_based_vm_exec_control & bit;
1711}
1712
1713static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1714{
1715 return (vmcs12->cpu_based_vm_exec_control &
1716 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1717 (vmcs12->secondary_vm_exec_control & bit);
1718}
1719
Jan Kiszkaf41245002014-03-07 20:03:13 +01001720static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1721{
1722 return vmcs12->pin_based_vm_exec_control &
1723 PIN_BASED_VMX_PREEMPTION_TIMER;
1724}
1725
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001726static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1727{
1728 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1729}
1730
1731static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1732{
1733 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1734}
1735
Nadav Har'El155a97a2013-08-05 11:07:16 +03001736static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1737{
1738 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1739}
1740
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001741static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1742{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001743 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001744}
1745
Bandan Dasc5f983f2017-05-05 15:25:14 -04001746static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1747{
1748 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1749}
1750
Wincy Vanf2b93282015-02-03 23:56:03 +08001751static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1752{
1753 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1754}
1755
Wanpeng Li5c614b32015-10-13 09:18:36 -07001756static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1757{
1758 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1759}
1760
Wincy Van82f0dd42015-02-03 23:57:18 +08001761static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1762{
1763 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1764}
1765
Wincy Van608406e2015-02-03 23:57:51 +08001766static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1767{
1768 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1769}
1770
Wincy Van705699a2015-02-03 23:58:17 +08001771static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1772{
1773 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1774}
1775
Bandan Das27c42a12017-08-03 15:54:42 -04001776static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1777{
1778 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1779}
1780
Bandan Das41ab9372017-08-03 15:54:43 -04001781static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1782{
1783 return nested_cpu_has_vmfunc(vmcs12) &&
1784 (vmcs12->vm_function_control &
1785 VMX_VMFUNC_EPTP_SWITCHING);
1786}
1787
Jim Mattsonef85b672016-12-12 11:01:37 -08001788static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001789{
1790 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001791 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001792}
1793
Jan Kiszka533558b2014-01-04 18:47:20 +01001794static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1795 u32 exit_intr_info,
1796 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001797static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1798 struct vmcs12 *vmcs12,
1799 u32 reason, unsigned long qualification);
1800
Rusty Russell8b9cf982007-07-30 16:31:43 +10001801static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001802{
1803 int i;
1804
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001805 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001806 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001807 return i;
1808 return -1;
1809}
1810
Sheng Yang2384d2b2008-01-17 15:14:33 +08001811static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1812{
1813 struct {
1814 u64 vpid : 16;
1815 u64 rsvd : 48;
1816 u64 gva;
1817 } operand = { vpid, 0, gva };
1818
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001819 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001820 /* CF==1 or ZF==1 --> rc = -1 */
1821 "; ja 1f ; ud2 ; 1:"
1822 : : "a"(&operand), "c"(ext) : "cc", "memory");
1823}
1824
Sheng Yang14394422008-04-28 12:24:45 +08001825static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1826{
1827 struct {
1828 u64 eptp, gpa;
1829 } operand = {eptp, gpa};
1830
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001831 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001832 /* CF==1 or ZF==1 --> rc = -1 */
1833 "; ja 1f ; ud2 ; 1:\n"
1834 : : "a" (&operand), "c" (ext) : "cc", "memory");
1835}
1836
Avi Kivity26bb0982009-09-07 11:14:12 +03001837static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001838{
1839 int i;
1840
Rusty Russell8b9cf982007-07-30 16:31:43 +10001841 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001842 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001843 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001844 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001845}
1846
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847static void vmcs_clear(struct vmcs *vmcs)
1848{
1849 u64 phys_addr = __pa(vmcs);
1850 u8 error;
1851
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001852 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001853 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854 : "cc", "memory");
1855 if (error)
1856 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1857 vmcs, phys_addr);
1858}
1859
Nadav Har'Eld462b812011-05-24 15:26:10 +03001860static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1861{
1862 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001863 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1864 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001865 loaded_vmcs->cpu = -1;
1866 loaded_vmcs->launched = 0;
1867}
1868
Dongxiao Xu7725b892010-05-11 18:29:38 +08001869static void vmcs_load(struct vmcs *vmcs)
1870{
1871 u64 phys_addr = __pa(vmcs);
1872 u8 error;
1873
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001874 if (static_branch_unlikely(&enable_evmcs))
1875 return evmcs_load(phys_addr);
1876
Dongxiao Xu7725b892010-05-11 18:29:38 +08001877 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001878 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001879 : "cc", "memory");
1880 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001881 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001882 vmcs, phys_addr);
1883}
1884
Dave Young2965faa2015-09-09 15:38:55 -07001885#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001886/*
1887 * This bitmap is used to indicate whether the vmclear
1888 * operation is enabled on all cpus. All disabled by
1889 * default.
1890 */
1891static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1892
1893static inline void crash_enable_local_vmclear(int cpu)
1894{
1895 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1896}
1897
1898static inline void crash_disable_local_vmclear(int cpu)
1899{
1900 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1901}
1902
1903static inline int crash_local_vmclear_enabled(int cpu)
1904{
1905 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1906}
1907
1908static void crash_vmclear_local_loaded_vmcss(void)
1909{
1910 int cpu = raw_smp_processor_id();
1911 struct loaded_vmcs *v;
1912
1913 if (!crash_local_vmclear_enabled(cpu))
1914 return;
1915
1916 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1917 loaded_vmcss_on_cpu_link)
1918 vmcs_clear(v->vmcs);
1919}
1920#else
1921static inline void crash_enable_local_vmclear(int cpu) { }
1922static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001923#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001924
Nadav Har'Eld462b812011-05-24 15:26:10 +03001925static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001927 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001928 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929
Nadav Har'Eld462b812011-05-24 15:26:10 +03001930 if (loaded_vmcs->cpu != cpu)
1931 return; /* vcpu migration can race with cpu offline */
1932 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001934 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001935 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001936
1937 /*
1938 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1939 * is before setting loaded_vmcs->vcpu to -1 which is done in
1940 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1941 * then adds the vmcs into percpu list before it is deleted.
1942 */
1943 smp_wmb();
1944
Nadav Har'Eld462b812011-05-24 15:26:10 +03001945 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001946 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947}
1948
Nadav Har'Eld462b812011-05-24 15:26:10 +03001949static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001950{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001951 int cpu = loaded_vmcs->cpu;
1952
1953 if (cpu != -1)
1954 smp_call_function_single(cpu,
1955 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001956}
1957
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001958static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001959{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001960 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001961 return;
1962
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001963 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001964 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001965}
1966
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001967static inline void vpid_sync_vcpu_global(void)
1968{
1969 if (cpu_has_vmx_invvpid_global())
1970 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1971}
1972
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001973static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001974{
1975 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001976 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001977 else
1978 vpid_sync_vcpu_global();
1979}
1980
Sheng Yang14394422008-04-28 12:24:45 +08001981static inline void ept_sync_global(void)
1982{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001983 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001984}
1985
1986static inline void ept_sync_context(u64 eptp)
1987{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001988 if (cpu_has_vmx_invept_context())
1989 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1990 else
1991 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001992}
1993
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001994static __always_inline void vmcs_check16(unsigned long field)
1995{
1996 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1997 "16-bit accessor invalid for 64-bit field");
1998 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1999 "16-bit accessor invalid for 64-bit high field");
2000 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2001 "16-bit accessor invalid for 32-bit high field");
2002 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2003 "16-bit accessor invalid for natural width field");
2004}
2005
2006static __always_inline void vmcs_check32(unsigned long field)
2007{
2008 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2009 "32-bit accessor invalid for 16-bit field");
2010 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2011 "32-bit accessor invalid for natural width field");
2012}
2013
2014static __always_inline void vmcs_check64(unsigned long field)
2015{
2016 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2017 "64-bit accessor invalid for 16-bit field");
2018 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2019 "64-bit accessor invalid for 64-bit high field");
2020 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2021 "64-bit accessor invalid for 32-bit field");
2022 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2023 "64-bit accessor invalid for natural width field");
2024}
2025
2026static __always_inline void vmcs_checkl(unsigned long field)
2027{
2028 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2029 "Natural width accessor invalid for 16-bit field");
2030 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2031 "Natural width accessor invalid for 64-bit field");
2032 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2033 "Natural width accessor invalid for 64-bit high field");
2034 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2035 "Natural width accessor invalid for 32-bit field");
2036}
2037
2038static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039{
Avi Kivity5e520e62011-05-15 10:13:12 -04002040 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041
Avi Kivity5e520e62011-05-15 10:13:12 -04002042 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2043 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 return value;
2045}
2046
Avi Kivity96304212011-05-15 10:13:13 -04002047static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002049 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002050 if (static_branch_unlikely(&enable_evmcs))
2051 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002052 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053}
2054
Avi Kivity96304212011-05-15 10:13:13 -04002055static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002057 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002058 if (static_branch_unlikely(&enable_evmcs))
2059 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002060 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061}
2062
Avi Kivity96304212011-05-15 10:13:13 -04002063static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002065 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002066 if (static_branch_unlikely(&enable_evmcs))
2067 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002068#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002069 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002071 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072#endif
2073}
2074
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002075static __always_inline unsigned long vmcs_readl(unsigned long field)
2076{
2077 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002078 if (static_branch_unlikely(&enable_evmcs))
2079 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002080 return __vmcs_readl(field);
2081}
2082
Avi Kivitye52de1b2007-01-05 16:36:56 -08002083static noinline void vmwrite_error(unsigned long field, unsigned long value)
2084{
2085 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2086 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2087 dump_stack();
2088}
2089
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002090static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091{
2092 u8 error;
2093
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002094 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002095 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002096 if (unlikely(error))
2097 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098}
2099
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002100static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002102 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002103 if (static_branch_unlikely(&enable_evmcs))
2104 return evmcs_write16(field, value);
2105
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002106 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107}
2108
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002109static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002111 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002112 if (static_branch_unlikely(&enable_evmcs))
2113 return evmcs_write32(field, value);
2114
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002115 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116}
2117
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002118static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002120 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002121 if (static_branch_unlikely(&enable_evmcs))
2122 return evmcs_write64(field, value);
2123
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002124 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002125#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002127 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128#endif
2129}
2130
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002131static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002132{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002133 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002134 if (static_branch_unlikely(&enable_evmcs))
2135 return evmcs_write64(field, value);
2136
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002137 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002138}
2139
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002140static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002141{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002142 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2143 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002144 if (static_branch_unlikely(&enable_evmcs))
2145 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2146
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002147 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2148}
2149
2150static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2151{
2152 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2153 "vmcs_set_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);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002158}
2159
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002160static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2161{
2162 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2163}
2164
Gleb Natapov2961e8762013-11-25 15:37:13 +02002165static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2166{
2167 vmcs_write32(VM_ENTRY_CONTROLS, val);
2168 vmx->vm_entry_controls_shadow = val;
2169}
2170
2171static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2172{
2173 if (vmx->vm_entry_controls_shadow != val)
2174 vm_entry_controls_init(vmx, val);
2175}
2176
2177static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2178{
2179 return vmx->vm_entry_controls_shadow;
2180}
2181
2182
2183static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2184{
2185 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2186}
2187
2188static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2189{
2190 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2191}
2192
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002193static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2194{
2195 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2196}
2197
Gleb Natapov2961e8762013-11-25 15:37:13 +02002198static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2199{
2200 vmcs_write32(VM_EXIT_CONTROLS, val);
2201 vmx->vm_exit_controls_shadow = val;
2202}
2203
2204static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2205{
2206 if (vmx->vm_exit_controls_shadow != val)
2207 vm_exit_controls_init(vmx, val);
2208}
2209
2210static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2211{
2212 return vmx->vm_exit_controls_shadow;
2213}
2214
2215
2216static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2217{
2218 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2219}
2220
2221static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2222{
2223 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2224}
2225
Avi Kivity2fb92db2011-04-27 19:42:18 +03002226static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2227{
2228 vmx->segment_cache.bitmask = 0;
2229}
2230
2231static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2232 unsigned field)
2233{
2234 bool ret;
2235 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2236
2237 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2238 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2239 vmx->segment_cache.bitmask = 0;
2240 }
2241 ret = vmx->segment_cache.bitmask & mask;
2242 vmx->segment_cache.bitmask |= mask;
2243 return ret;
2244}
2245
2246static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2247{
2248 u16 *p = &vmx->segment_cache.seg[seg].selector;
2249
2250 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2251 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2252 return *p;
2253}
2254
2255static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2256{
2257 ulong *p = &vmx->segment_cache.seg[seg].base;
2258
2259 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2260 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2261 return *p;
2262}
2263
2264static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2265{
2266 u32 *p = &vmx->segment_cache.seg[seg].limit;
2267
2268 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2269 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2270 return *p;
2271}
2272
2273static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2274{
2275 u32 *p = &vmx->segment_cache.seg[seg].ar;
2276
2277 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2278 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2279 return *p;
2280}
2281
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002282static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2283{
2284 u32 eb;
2285
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002286 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002287 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002288 /*
2289 * Guest access to VMware backdoor ports could legitimately
2290 * trigger #GP because of TSS I/O permission bitmap.
2291 * We intercept those #GP and allow access to them anyway
2292 * as VMware does.
2293 */
2294 if (enable_vmware_backdoor)
2295 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002296 if ((vcpu->guest_debug &
2297 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2298 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2299 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002300 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002301 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002302 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002303 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002304
2305 /* When we are running a nested L2 guest and L1 specified for it a
2306 * certain exception bitmap, we must trap the same exceptions and pass
2307 * them to L1. When running L2, we will only handle the exceptions
2308 * specified above if L1 did not want them.
2309 */
2310 if (is_guest_mode(vcpu))
2311 eb |= get_vmcs12(vcpu)->exception_bitmap;
2312
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002313 vmcs_write32(EXCEPTION_BITMAP, eb);
2314}
2315
Ashok Raj15d45072018-02-01 22:59:43 +01002316/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002317 * Check if MSR is intercepted for currently loaded MSR bitmap.
2318 */
2319static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2320{
2321 unsigned long *msr_bitmap;
2322 int f = sizeof(unsigned long);
2323
2324 if (!cpu_has_vmx_msr_bitmap())
2325 return true;
2326
2327 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2328
2329 if (msr <= 0x1fff) {
2330 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2331 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2332 msr &= 0x1fff;
2333 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2334 }
2335
2336 return true;
2337}
2338
2339/*
Ashok Raj15d45072018-02-01 22:59:43 +01002340 * Check if MSR is intercepted for L01 MSR bitmap.
2341 */
2342static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2343{
2344 unsigned long *msr_bitmap;
2345 int f = sizeof(unsigned long);
2346
2347 if (!cpu_has_vmx_msr_bitmap())
2348 return true;
2349
2350 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2351
2352 if (msr <= 0x1fff) {
2353 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2354 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2355 msr &= 0x1fff;
2356 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2357 }
2358
2359 return true;
2360}
2361
Gleb Natapov2961e8762013-11-25 15:37:13 +02002362static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2363 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002364{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002365 vm_entry_controls_clearbit(vmx, entry);
2366 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002367}
2368
Avi Kivity61d2ef22010-04-28 16:40:38 +03002369static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2370{
2371 unsigned i;
2372 struct msr_autoload *m = &vmx->msr_autoload;
2373
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002374 switch (msr) {
2375 case MSR_EFER:
2376 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002377 clear_atomic_switch_msr_special(vmx,
2378 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002379 VM_EXIT_LOAD_IA32_EFER);
2380 return;
2381 }
2382 break;
2383 case MSR_CORE_PERF_GLOBAL_CTRL:
2384 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002385 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002386 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2387 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2388 return;
2389 }
2390 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002391 }
2392
Avi Kivity61d2ef22010-04-28 16:40:38 +03002393 for (i = 0; i < m->nr; ++i)
2394 if (m->guest[i].index == msr)
2395 break;
2396
2397 if (i == m->nr)
2398 return;
2399 --m->nr;
2400 m->guest[i] = m->guest[m->nr];
2401 m->host[i] = m->host[m->nr];
2402 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2403 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2404}
2405
Gleb Natapov2961e8762013-11-25 15:37:13 +02002406static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2407 unsigned long entry, unsigned long exit,
2408 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2409 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002410{
2411 vmcs_write64(guest_val_vmcs, guest_val);
2412 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002413 vm_entry_controls_setbit(vmx, entry);
2414 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002415}
2416
Avi Kivity61d2ef22010-04-28 16:40:38 +03002417static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2418 u64 guest_val, u64 host_val)
2419{
2420 unsigned i;
2421 struct msr_autoload *m = &vmx->msr_autoload;
2422
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002423 switch (msr) {
2424 case MSR_EFER:
2425 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002426 add_atomic_switch_msr_special(vmx,
2427 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002428 VM_EXIT_LOAD_IA32_EFER,
2429 GUEST_IA32_EFER,
2430 HOST_IA32_EFER,
2431 guest_val, host_val);
2432 return;
2433 }
2434 break;
2435 case MSR_CORE_PERF_GLOBAL_CTRL:
2436 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002437 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002438 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2439 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2440 GUEST_IA32_PERF_GLOBAL_CTRL,
2441 HOST_IA32_PERF_GLOBAL_CTRL,
2442 guest_val, host_val);
2443 return;
2444 }
2445 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002446 case MSR_IA32_PEBS_ENABLE:
2447 /* PEBS needs a quiescent period after being disabled (to write
2448 * a record). Disabling PEBS through VMX MSR swapping doesn't
2449 * provide that period, so a CPU could write host's record into
2450 * guest's memory.
2451 */
2452 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002453 }
2454
Avi Kivity61d2ef22010-04-28 16:40:38 +03002455 for (i = 0; i < m->nr; ++i)
2456 if (m->guest[i].index == msr)
2457 break;
2458
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002459 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002460 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002461 "Can't add msr %x\n", msr);
2462 return;
2463 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002464 ++m->nr;
2465 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2466 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2467 }
2468
2469 m->guest[i].index = msr;
2470 m->guest[i].value = guest_val;
2471 m->host[i].index = msr;
2472 m->host[i].value = host_val;
2473}
2474
Avi Kivity92c0d902009-10-29 11:00:16 +02002475static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002476{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002477 u64 guest_efer = vmx->vcpu.arch.efer;
2478 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002479
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002480 if (!enable_ept) {
2481 /*
2482 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2483 * host CPUID is more efficient than testing guest CPUID
2484 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2485 */
2486 if (boot_cpu_has(X86_FEATURE_SMEP))
2487 guest_efer |= EFER_NX;
2488 else if (!(guest_efer & EFER_NX))
2489 ignore_bits |= EFER_NX;
2490 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002491
Avi Kivity51c6cf62007-08-29 03:48:05 +03002492 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002493 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002494 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002495 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002496#ifdef CONFIG_X86_64
2497 ignore_bits |= EFER_LMA | EFER_LME;
2498 /* SCE is meaningful only in long mode on Intel */
2499 if (guest_efer & EFER_LMA)
2500 ignore_bits &= ~(u64)EFER_SCE;
2501#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002502
2503 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002504
2505 /*
2506 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2507 * On CPUs that support "load IA32_EFER", always switch EFER
2508 * atomically, since it's faster than switching it manually.
2509 */
2510 if (cpu_has_load_ia32_efer ||
2511 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002512 if (!(guest_efer & EFER_LMA))
2513 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002514 if (guest_efer != host_efer)
2515 add_atomic_switch_msr(vmx, MSR_EFER,
2516 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002517 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002518 } else {
2519 guest_efer &= ~ignore_bits;
2520 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002521
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002522 vmx->guest_msrs[efer_offset].data = guest_efer;
2523 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2524
2525 return true;
2526 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002527}
2528
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002529#ifdef CONFIG_X86_32
2530/*
2531 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2532 * VMCS rather than the segment table. KVM uses this helper to figure
2533 * out the current bases to poke them into the VMCS before entry.
2534 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002535static unsigned long segment_base(u16 selector)
2536{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002537 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002538 unsigned long v;
2539
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002540 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002541 return 0;
2542
Thomas Garnier45fc8752017-03-14 10:05:08 -07002543 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002544
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002545 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002546 u16 ldt_selector = kvm_read_ldt();
2547
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002548 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002549 return 0;
2550
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002551 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002552 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002553 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002554 return v;
2555}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002556#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002557
Avi Kivity04d2cc72007-09-10 18:10:54 +03002558static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002559{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002560 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002561#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002562 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002563#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002564 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002565
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002566 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002567 return;
2568
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002569 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002570 /*
2571 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2572 * allow segment selectors with cpl > 0 or ti == 1.
2573 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002574 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002575 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002576
2577#ifdef CONFIG_X86_64
2578 save_fsgs_for_kvm();
2579 vmx->host_state.fs_sel = current->thread.fsindex;
2580 vmx->host_state.gs_sel = current->thread.gsindex;
2581#else
Avi Kivity9581d442010-10-19 16:46:55 +02002582 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002583 savesegment(gs, vmx->host_state.gs_sel);
2584#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002585 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002586 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002587 vmx->host_state.fs_reload_needed = 0;
2588 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002589 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002590 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002591 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002592 if (!(vmx->host_state.gs_sel & 7))
2593 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002594 else {
2595 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002596 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002597 }
2598
2599#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002600 savesegment(ds, vmx->host_state.ds_sel);
2601 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002602
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002603 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002604 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002605
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002606 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002607 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002608 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002609#else
2610 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2611 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2612#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002613 if (boot_cpu_has(X86_FEATURE_MPX))
2614 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002615 for (i = 0; i < vmx->save_nmsrs; ++i)
2616 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002617 vmx->guest_msrs[i].data,
2618 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002619}
2620
Avi Kivitya9b21b62008-06-24 11:48:49 +03002621static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002622{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002623 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002624 return;
2625
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002626 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002627 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002628#ifdef CONFIG_X86_64
2629 if (is_long_mode(&vmx->vcpu))
2630 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2631#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002632 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002633 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002634#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002635 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002636#else
2637 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002638#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002639 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002640 if (vmx->host_state.fs_reload_needed)
2641 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002642#ifdef CONFIG_X86_64
2643 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2644 loadsegment(ds, vmx->host_state.ds_sel);
2645 loadsegment(es, vmx->host_state.es_sel);
2646 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002647#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002648 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002649#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002650 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002651#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002652 if (vmx->host_state.msr_host_bndcfgs)
2653 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002654 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002655}
2656
Avi Kivitya9b21b62008-06-24 11:48:49 +03002657static void vmx_load_host_state(struct vcpu_vmx *vmx)
2658{
2659 preempt_disable();
2660 __vmx_load_host_state(vmx);
2661 preempt_enable();
2662}
2663
Feng Wu28b835d2015-09-18 22:29:54 +08002664static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2665{
2666 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2667 struct pi_desc old, new;
2668 unsigned int dest;
2669
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002670 /*
2671 * In case of hot-plug or hot-unplug, we may have to undo
2672 * vmx_vcpu_pi_put even if there is no assigned device. And we
2673 * always keep PI.NDST up to date for simplicity: it makes the
2674 * code easier, and CPU migration is not a fast path.
2675 */
2676 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002677 return;
2678
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002679 /*
2680 * First handle the simple case where no cmpxchg is necessary; just
2681 * allow posting non-urgent interrupts.
2682 *
2683 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2684 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2685 * expects the VCPU to be on the blocked_vcpu_list that matches
2686 * PI.NDST.
2687 */
2688 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2689 vcpu->cpu == cpu) {
2690 pi_clear_sn(pi_desc);
2691 return;
2692 }
2693
2694 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002695 do {
2696 old.control = new.control = pi_desc->control;
2697
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002698 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002699
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002700 if (x2apic_enabled())
2701 new.ndst = dest;
2702 else
2703 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002704
Feng Wu28b835d2015-09-18 22:29:54 +08002705 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002706 } while (cmpxchg64(&pi_desc->control, old.control,
2707 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002708}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002709
Peter Feinerc95ba922016-08-17 09:36:47 -07002710static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2711{
2712 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2713 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2714}
2715
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716/*
2717 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2718 * vcpu mutex is already taken.
2719 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002720static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002722 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002723 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002725 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002726 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002727 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002728 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002729
2730 /*
2731 * Read loaded_vmcs->cpu should be before fetching
2732 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2733 * See the comments in __loaded_vmcs_clear().
2734 */
2735 smp_rmb();
2736
Nadav Har'Eld462b812011-05-24 15:26:10 +03002737 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2738 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002739 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002740 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002741 }
2742
2743 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2744 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2745 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002746 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002747 }
2748
2749 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002750 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002751 unsigned long sysenter_esp;
2752
2753 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002754
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 /*
2756 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002757 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002759 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002760 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002761 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002763 /*
2764 * VM exits change the host TR limit to 0x67 after a VM
2765 * exit. This is okay, since 0x67 covers everything except
2766 * the IO bitmap and have have code to handle the IO bitmap
2767 * being lost after a VM exit.
2768 */
2769 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2770
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2772 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002773
Nadav Har'Eld462b812011-05-24 15:26:10 +03002774 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 }
Feng Wu28b835d2015-09-18 22:29:54 +08002776
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002777 /* Setup TSC multiplier */
2778 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002779 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2780 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002781
Feng Wu28b835d2015-09-18 22:29:54 +08002782 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002783 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002784 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002785}
2786
2787static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2788{
2789 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2790
2791 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002792 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2793 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002794 return;
2795
2796 /* Set SN when the vCPU is preempted */
2797 if (vcpu->preempted)
2798 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799}
2800
2801static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2802{
Feng Wu28b835d2015-09-18 22:29:54 +08002803 vmx_vcpu_pi_put(vcpu);
2804
Avi Kivitya9b21b62008-06-24 11:48:49 +03002805 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806}
2807
Wanpeng Lif244dee2017-07-20 01:11:54 -07002808static bool emulation_required(struct kvm_vcpu *vcpu)
2809{
2810 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2811}
2812
Avi Kivityedcafe32009-12-30 18:07:40 +02002813static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2814
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002815/*
2816 * Return the cr0 value that a nested guest would read. This is a combination
2817 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2818 * its hypervisor (cr0_read_shadow).
2819 */
2820static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2821{
2822 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2823 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2824}
2825static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2826{
2827 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2828 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2829}
2830
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2832{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002833 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002834
Avi Kivity6de12732011-03-07 12:51:22 +02002835 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2836 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2837 rflags = vmcs_readl(GUEST_RFLAGS);
2838 if (to_vmx(vcpu)->rmode.vm86_active) {
2839 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2840 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2841 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2842 }
2843 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002844 }
Avi Kivity6de12732011-03-07 12:51:22 +02002845 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846}
2847
2848static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2849{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002850 unsigned long old_rflags = vmx_get_rflags(vcpu);
2851
Avi Kivity6de12732011-03-07 12:51:22 +02002852 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2853 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002854 if (to_vmx(vcpu)->rmode.vm86_active) {
2855 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002856 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002859
2860 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2861 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862}
2863
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002864static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002865{
2866 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2867 int ret = 0;
2868
2869 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002870 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002871 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002872 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002873
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002874 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002875}
2876
2877static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2878{
2879 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2880 u32 interruptibility = interruptibility_old;
2881
2882 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2883
Jan Kiszka48005f62010-02-19 19:38:07 +01002884 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002885 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002886 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002887 interruptibility |= GUEST_INTR_STATE_STI;
2888
2889 if ((interruptibility != interruptibility_old))
2890 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2891}
2892
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2894{
2895 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002897 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002899 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900
Glauber Costa2809f5d2009-05-12 16:21:05 -04002901 /* skipping an emulated instruction also counts */
2902 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903}
2904
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002905static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2906 unsigned long exit_qual)
2907{
2908 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2909 unsigned int nr = vcpu->arch.exception.nr;
2910 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2911
2912 if (vcpu->arch.exception.has_error_code) {
2913 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2914 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2915 }
2916
2917 if (kvm_exception_is_soft(nr))
2918 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2919 else
2920 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2921
2922 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2923 vmx_get_nmi_mask(vcpu))
2924 intr_info |= INTR_INFO_UNBLOCK_NMI;
2925
2926 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2927}
2928
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002929/*
2930 * KVM wants to inject page-faults which it got to the guest. This function
2931 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002932 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002933static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002934{
2935 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002936 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002937
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002938 if (nr == PF_VECTOR) {
2939 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002940 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002941 return 1;
2942 }
2943 /*
2944 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2945 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2946 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2947 * can be written only when inject_pending_event runs. This should be
2948 * conditional on a new capability---if the capability is disabled,
2949 * kvm_multiple_exception would write the ancillary information to
2950 * CR2 or DR6, for backwards ABI-compatibility.
2951 */
2952 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2953 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002954 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002955 return 1;
2956 }
2957 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002958 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002959 if (nr == DB_VECTOR)
2960 *exit_qual = vcpu->arch.dr6;
2961 else
2962 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002963 return 1;
2964 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002965 }
2966
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002967 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002968}
2969
Wanpeng Licaa057a2018-03-12 04:53:03 -07002970static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2971{
2972 /*
2973 * Ensure that we clear the HLT state in the VMCS. We don't need to
2974 * explicitly skip the instruction because if the HLT state is set,
2975 * then the instruction is already executing and RIP has already been
2976 * advanced.
2977 */
2978 if (kvm_hlt_in_guest(vcpu->kvm) &&
2979 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2980 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2981}
2982
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002983static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002984{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002985 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002986 unsigned nr = vcpu->arch.exception.nr;
2987 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002988 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002989 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002990
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002991 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002992 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002993 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2994 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002995
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002996 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002997 int inc_eip = 0;
2998 if (kvm_exception_is_soft(nr))
2999 inc_eip = vcpu->arch.event_exit_inst_len;
3000 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003001 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003002 return;
3003 }
3004
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003005 WARN_ON_ONCE(vmx->emulation_required);
3006
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003007 if (kvm_exception_is_soft(nr)) {
3008 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3009 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003010 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3011 } else
3012 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3013
3014 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003015
3016 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003017}
3018
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003019static bool vmx_rdtscp_supported(void)
3020{
3021 return cpu_has_vmx_rdtscp();
3022}
3023
Mao, Junjiead756a12012-07-02 01:18:48 +00003024static bool vmx_invpcid_supported(void)
3025{
3026 return cpu_has_vmx_invpcid() && enable_ept;
3027}
3028
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029/*
Eddie Donga75beee2007-05-17 18:55:15 +03003030 * Swap MSR entry in host/guest MSR entry array.
3031 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003032static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003033{
Avi Kivity26bb0982009-09-07 11:14:12 +03003034 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003035
3036 tmp = vmx->guest_msrs[to];
3037 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3038 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003039}
3040
3041/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003042 * Set up the vmcs to automatically save and restore system
3043 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3044 * mode, as fiddling with msrs is very expensive.
3045 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003046static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003047{
Avi Kivity26bb0982009-09-07 11:14:12 +03003048 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003049
Eddie Donga75beee2007-05-17 18:55:15 +03003050 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003051#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003052 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003053 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003054 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003055 move_msr_up(vmx, index, save_nmsrs++);
3056 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003057 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003058 move_msr_up(vmx, index, save_nmsrs++);
3059 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003060 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003061 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003062 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003063 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003064 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003065 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003066 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003067 * if efer.sce is enabled.
3068 */
Brian Gerst8c065852010-07-17 09:03:26 -04003069 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003070 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003071 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003072 }
Eddie Donga75beee2007-05-17 18:55:15 +03003073#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003074 index = __find_msr_index(vmx, MSR_EFER);
3075 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003076 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003077
Avi Kivity26bb0982009-09-07 11:14:12 +03003078 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003079
Yang Zhang8d146952013-01-25 10:18:50 +08003080 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003081 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003082}
3083
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003084static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003086 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003088 if (is_guest_mode(vcpu) &&
3089 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3090 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3091
3092 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093}
3094
3095/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003096 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003098static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003100 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003101 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003102 * We're here if L1 chose not to trap WRMSR to TSC. According
3103 * to the spec, this should set L1's TSC; The offset that L1
3104 * set for L2 remains unchanged, and still needs to be added
3105 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003106 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003107 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003108 /* recalculate vmcs02.TSC_OFFSET: */
3109 vmcs12 = get_vmcs12(vcpu);
3110 vmcs_write64(TSC_OFFSET, offset +
3111 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3112 vmcs12->tsc_offset : 0));
3113 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003114 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3115 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003116 vmcs_write64(TSC_OFFSET, offset);
3117 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118}
3119
Nadav Har'El801d3422011-05-25 23:02:23 +03003120/*
3121 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3122 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3123 * all guests if the "nested" module option is off, and can also be disabled
3124 * for a single guest by disabling its VMX cpuid bit.
3125 */
3126static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3127{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003128 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003129}
3130
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003132 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3133 * returned for the various VMX controls MSRs when nested VMX is enabled.
3134 * The same values should also be used to verify that vmcs12 control fields are
3135 * valid during nested entry from L1 to L2.
3136 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3137 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3138 * bit in the high half is on if the corresponding bit in the control field
3139 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003140 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003141static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003142{
Paolo Bonzini13893092018-02-26 13:40:09 +01003143 if (!nested) {
3144 memset(msrs, 0, sizeof(*msrs));
3145 return;
3146 }
3147
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003148 /*
3149 * Note that as a general rule, the high half of the MSRs (bits in
3150 * the control fields which may be 1) should be initialized by the
3151 * intersection of the underlying hardware's MSR (i.e., features which
3152 * can be supported) and the list of features we want to expose -
3153 * because they are known to be properly supported in our code.
3154 * Also, usually, the low half of the MSRs (bits which must be 1) can
3155 * be set to 0, meaning that L1 may turn off any of these bits. The
3156 * reason is that if one of these bits is necessary, it will appear
3157 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3158 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003159 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003160 * These rules have exceptions below.
3161 */
3162
3163 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003164 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003165 msrs->pinbased_ctls_low,
3166 msrs->pinbased_ctls_high);
3167 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003168 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003169 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003170 PIN_BASED_EXT_INTR_MASK |
3171 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003172 PIN_BASED_VIRTUAL_NMIS |
3173 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003174 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003175 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003176 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003177
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003178 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003179 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003180 msrs->exit_ctls_low,
3181 msrs->exit_ctls_high);
3182 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003183 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003184
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003185 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003186#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003187 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003188#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003189 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003190 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003191 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003192 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003193 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3194
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003195 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003196 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003197
Jan Kiszka2996fca2014-06-16 13:59:43 +02003198 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003199 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003200
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 /* entry controls */
3202 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003203 msrs->entry_ctls_low,
3204 msrs->entry_ctls_high);
3205 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003206 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003207 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003208#ifdef CONFIG_X86_64
3209 VM_ENTRY_IA32E_MODE |
3210#endif
3211 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003212 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003213 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003214 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003215 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003216
Jan Kiszka2996fca2014-06-16 13:59:43 +02003217 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003218 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003219
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003220 /* cpu-based controls */
3221 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003222 msrs->procbased_ctls_low,
3223 msrs->procbased_ctls_high);
3224 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003225 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003226 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003227 CPU_BASED_VIRTUAL_INTR_PENDING |
3228 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003229 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3230 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3231 CPU_BASED_CR3_STORE_EXITING |
3232#ifdef CONFIG_X86_64
3233 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3234#endif
3235 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003236 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3237 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3238 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3239 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003240 /*
3241 * We can allow some features even when not supported by the
3242 * hardware. For example, L1 can specify an MSR bitmap - and we
3243 * can use it to avoid exits to L1 - even when L0 runs L2
3244 * without MSR bitmaps.
3245 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003246 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003247 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003248 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003249
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003250 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003251 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003252 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3253
Paolo Bonzini80154d72017-08-24 13:55:35 +02003254 /*
3255 * secondary cpu-based controls. Do not include those that
3256 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3257 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003258 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003259 msrs->secondary_ctls_low,
3260 msrs->secondary_ctls_high);
3261 msrs->secondary_ctls_low = 0;
3262 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003263 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003264 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003265 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003266 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003267 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003268 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003269
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003270 if (enable_ept) {
3271 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003272 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003273 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003274 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003275 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003276 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003277 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003278 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003279 msrs->ept_caps &= vmx_capability.ept;
3280 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003281 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3282 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003283 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003284 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003285 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003286 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003287 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003288 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003289
Bandan Das27c42a12017-08-03 15:54:42 -04003290 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003291 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003292 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003293 /*
3294 * Advertise EPTP switching unconditionally
3295 * since we emulate it
3296 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003297 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003298 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003299 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003300 }
3301
Paolo Bonzinief697a72016-03-18 16:58:38 +01003302 /*
3303 * Old versions of KVM use the single-context version without
3304 * checking for support, so declare that it is supported even
3305 * though it is treated as global context. The alternative is
3306 * not failing the single-context invvpid, and it is worse.
3307 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003308 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003309 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003310 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003311 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003312 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003313 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003314
Radim Krčmář0790ec12015-03-17 14:02:32 +01003315 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003316 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003317 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3318
Jan Kiszkac18911a2013-03-13 16:06:41 +01003319 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003320 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003321 msrs->misc_low,
3322 msrs->misc_high);
3323 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3324 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003325 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003326 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003327 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003329
3330 /*
3331 * This MSR reports some information about VMX support. We
3332 * should return information about the VMX we emulate for the
3333 * guest, and the VMCS structure we give it - not about the
3334 * VMX support of the underlying hardware.
3335 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003336 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003337 VMCS12_REVISION |
3338 VMX_BASIC_TRUE_CTLS |
3339 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3340 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3341
3342 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003343 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003344
3345 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003346 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003347 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3348 * We picked the standard core2 setting.
3349 */
3350#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3351#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003352 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3353 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003354
3355 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003356 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3357 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003358
3359 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003360 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003361}
3362
David Matlack38991522016-11-29 18:14:08 -08003363/*
3364 * if fixed0[i] == 1: val[i] must be 1
3365 * if fixed1[i] == 0: val[i] must be 0
3366 */
3367static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3368{
3369 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003370}
3371
3372static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3373{
David Matlack38991522016-11-29 18:14:08 -08003374 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003375}
3376
3377static inline u64 vmx_control_msr(u32 low, u32 high)
3378{
3379 return low | ((u64)high << 32);
3380}
3381
David Matlack62cc6b9d2016-11-29 18:14:07 -08003382static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3383{
3384 superset &= mask;
3385 subset &= mask;
3386
3387 return (superset | subset) == superset;
3388}
3389
3390static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3391{
3392 const u64 feature_and_reserved =
3393 /* feature (except bit 48; see below) */
3394 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3395 /* reserved */
3396 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003398
3399 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3400 return -EINVAL;
3401
3402 /*
3403 * KVM does not emulate a version of VMX that constrains physical
3404 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3405 */
3406 if (data & BIT_ULL(48))
3407 return -EINVAL;
3408
3409 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3410 vmx_basic_vmcs_revision_id(data))
3411 return -EINVAL;
3412
3413 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3414 return -EINVAL;
3415
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003416 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003417 return 0;
3418}
3419
3420static int
3421vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3422{
3423 u64 supported;
3424 u32 *lowp, *highp;
3425
3426 switch (msr_index) {
3427 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003428 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3429 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003430 break;
3431 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 lowp = &vmx->nested.msrs.procbased_ctls_low;
3433 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003434 break;
3435 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003436 lowp = &vmx->nested.msrs.exit_ctls_low;
3437 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003438 break;
3439 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003440 lowp = &vmx->nested.msrs.entry_ctls_low;
3441 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003442 break;
3443 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003444 lowp = &vmx->nested.msrs.secondary_ctls_low;
3445 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003446 break;
3447 default:
3448 BUG();
3449 }
3450
3451 supported = vmx_control_msr(*lowp, *highp);
3452
3453 /* Check must-be-1 bits are still 1. */
3454 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3455 return -EINVAL;
3456
3457 /* Check must-be-0 bits are still 0. */
3458 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3459 return -EINVAL;
3460
3461 *lowp = data;
3462 *highp = data >> 32;
3463 return 0;
3464}
3465
3466static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3467{
3468 const u64 feature_and_reserved_bits =
3469 /* feature */
3470 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3471 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3472 /* reserved */
3473 GENMASK_ULL(13, 9) | BIT_ULL(31);
3474 u64 vmx_misc;
3475
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003476 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3477 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003478
3479 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3480 return -EINVAL;
3481
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003482 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003483 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3484 vmx_misc_preemption_timer_rate(data) !=
3485 vmx_misc_preemption_timer_rate(vmx_misc))
3486 return -EINVAL;
3487
3488 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3489 return -EINVAL;
3490
3491 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3492 return -EINVAL;
3493
3494 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3495 return -EINVAL;
3496
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 vmx->nested.msrs.misc_low = data;
3498 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003499
3500 /*
3501 * If L1 has read-only VM-exit information fields, use the
3502 * less permissive vmx_vmwrite_bitmap to specify write
3503 * permissions for the shadow VMCS.
3504 */
3505 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3506 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3507
David Matlack62cc6b9d2016-11-29 18:14:07 -08003508 return 0;
3509}
3510
3511static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3512{
3513 u64 vmx_ept_vpid_cap;
3514
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003515 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3516 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003517
3518 /* Every bit is either reserved or a feature bit. */
3519 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3520 return -EINVAL;
3521
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003522 vmx->nested.msrs.ept_caps = data;
3523 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003524 return 0;
3525}
3526
3527static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3528{
3529 u64 *msr;
3530
3531 switch (msr_index) {
3532 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003534 break;
3535 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003536 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003537 break;
3538 default:
3539 BUG();
3540 }
3541
3542 /*
3543 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3544 * must be 1 in the restored value.
3545 */
3546 if (!is_bitwise_subset(data, *msr, -1ULL))
3547 return -EINVAL;
3548
3549 *msr = data;
3550 return 0;
3551}
3552
3553/*
3554 * Called when userspace is restoring VMX MSRs.
3555 *
3556 * Returns 0 on success, non-0 otherwise.
3557 */
3558static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3559{
3560 struct vcpu_vmx *vmx = to_vmx(vcpu);
3561
Jim Mattsona943ac52018-05-29 09:11:32 -07003562 /*
3563 * Don't allow changes to the VMX capability MSRs while the vCPU
3564 * is in VMX operation.
3565 */
3566 if (vmx->nested.vmxon)
3567 return -EBUSY;
3568
David Matlack62cc6b9d2016-11-29 18:14:07 -08003569 switch (msr_index) {
3570 case MSR_IA32_VMX_BASIC:
3571 return vmx_restore_vmx_basic(vmx, data);
3572 case MSR_IA32_VMX_PINBASED_CTLS:
3573 case MSR_IA32_VMX_PROCBASED_CTLS:
3574 case MSR_IA32_VMX_EXIT_CTLS:
3575 case MSR_IA32_VMX_ENTRY_CTLS:
3576 /*
3577 * The "non-true" VMX capability MSRs are generated from the
3578 * "true" MSRs, so we do not support restoring them directly.
3579 *
3580 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3581 * should restore the "true" MSRs with the must-be-1 bits
3582 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3583 * DEFAULT SETTINGS".
3584 */
3585 return -EINVAL;
3586 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3587 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3588 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3589 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3590 case MSR_IA32_VMX_PROCBASED_CTLS2:
3591 return vmx_restore_control_msr(vmx, msr_index, data);
3592 case MSR_IA32_VMX_MISC:
3593 return vmx_restore_vmx_misc(vmx, data);
3594 case MSR_IA32_VMX_CR0_FIXED0:
3595 case MSR_IA32_VMX_CR4_FIXED0:
3596 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3597 case MSR_IA32_VMX_CR0_FIXED1:
3598 case MSR_IA32_VMX_CR4_FIXED1:
3599 /*
3600 * These MSRs are generated based on the vCPU's CPUID, so we
3601 * do not support restoring them directly.
3602 */
3603 return -EINVAL;
3604 case MSR_IA32_VMX_EPT_VPID_CAP:
3605 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3606 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003608 return 0;
3609 default:
3610 /*
3611 * The rest of the VMX capability MSRs do not support restore.
3612 */
3613 return -EINVAL;
3614 }
3615}
3616
Jan Kiszkacae50132014-01-04 18:47:22 +01003617/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003618static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003619{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003620 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003621 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003622 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003623 break;
3624 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3625 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003626 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 msrs->pinbased_ctls_low,
3628 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003629 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3630 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003631 break;
3632 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3633 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003634 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003635 msrs->procbased_ctls_low,
3636 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003637 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3638 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003639 break;
3640 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3641 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003642 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003643 msrs->exit_ctls_low,
3644 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003645 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3646 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003647 break;
3648 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3649 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003650 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 msrs->entry_ctls_low,
3652 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003653 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3654 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003655 break;
3656 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003657 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->misc_low,
3659 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003660 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003661 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003662 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003663 break;
3664 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003665 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003666 break;
3667 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003669 break;
3670 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003671 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003672 break;
3673 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003674 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003675 break;
3676 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003677 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->secondary_ctls_low,
3679 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003680 break;
3681 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 *pdata = msrs->ept_caps |
3683 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003684 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003685 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003687 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003688 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003689 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003690 }
3691
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003692 return 0;
3693}
3694
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003695static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3696 uint64_t val)
3697{
3698 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3699
3700 return !(val & ~valid_bits);
3701}
3702
Tom Lendacky801e4592018-02-21 13:39:51 -06003703static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3704{
Paolo Bonzini13893092018-02-26 13:40:09 +01003705 switch (msr->index) {
3706 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3707 if (!nested)
3708 return 1;
3709 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3710 default:
3711 return 1;
3712 }
3713
3714 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003715}
3716
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 * Reads an msr value (of 'msr_index') into 'pdata'.
3719 * Returns 0 on success, non-0 otherwise.
3720 * Assumes vcpu_load() was already called.
3721 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003722static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003723{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003724 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003725 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003726
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003727 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003728#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003730 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 break;
3732 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003733 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003735 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003736 vmx_load_host_state(vmx);
3737 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003738 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003739#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003740 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003741 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003742 case MSR_IA32_SPEC_CTRL:
3743 if (!msr_info->host_initiated &&
3744 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3745 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3746 return 1;
3747
3748 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3749 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003750 case MSR_IA32_ARCH_CAPABILITIES:
3751 if (!msr_info->host_initiated &&
3752 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3753 return 1;
3754 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3755 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003757 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758 break;
3759 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003760 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761 break;
3762 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003763 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003765 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003766 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003767 (!msr_info->host_initiated &&
3768 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003769 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003770 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003771 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003772 case MSR_IA32_MCG_EXT_CTL:
3773 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003774 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003775 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003776 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003777 msr_info->data = vcpu->arch.mcg_ext_ctl;
3778 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003779 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003780 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003781 break;
3782 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3783 if (!nested_vmx_allowed(vcpu))
3784 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003785 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3786 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003787 case MSR_IA32_XSS:
3788 if (!vmx_xsaves_supported())
3789 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003790 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003791 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003792 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003793 if (!msr_info->host_initiated &&
3794 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003795 return 1;
3796 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003798 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003799 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003800 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003801 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003803 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804 }
3805
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806 return 0;
3807}
3808
Jan Kiszkacae50132014-01-04 18:47:22 +01003809static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3810
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811/*
3812 * Writes msr value into into the appropriate "register".
3813 * Returns 0 on success, non-0 otherwise.
3814 * Assumes vcpu_load() was already called.
3815 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003816static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003819 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003820 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003821 u32 msr_index = msr_info->index;
3822 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003823
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003825 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003826 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003827 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003828#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003829 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003830 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 vmcs_writel(GUEST_FS_BASE, data);
3832 break;
3833 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003834 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 vmcs_writel(GUEST_GS_BASE, data);
3836 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003837 case MSR_KERNEL_GS_BASE:
3838 vmx_load_host_state(vmx);
3839 vmx->msr_guest_kernel_gs_base = data;
3840 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841#endif
3842 case MSR_IA32_SYSENTER_CS:
3843 vmcs_write32(GUEST_SYSENTER_CS, data);
3844 break;
3845 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003846 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 break;
3848 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003849 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003851 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003852 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003853 (!msr_info->host_initiated &&
3854 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003855 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003856 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003857 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003859 vmcs_write64(GUEST_BNDCFGS, data);
3860 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003861 case MSR_IA32_SPEC_CTRL:
3862 if (!msr_info->host_initiated &&
3863 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3864 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3865 return 1;
3866
3867 /* The STIBP bit doesn't fault even if it's not advertised */
3868 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3869 return 1;
3870
3871 vmx->spec_ctrl = data;
3872
3873 if (!data)
3874 break;
3875
3876 /*
3877 * For non-nested:
3878 * When it's written (to non-zero) for the first time, pass
3879 * it through.
3880 *
3881 * For nested:
3882 * The handling of the MSR bitmap for L2 guests is done in
3883 * nested_vmx_merge_msr_bitmap. We should not touch the
3884 * vmcs02.msr_bitmap here since it gets completely overwritten
3885 * in the merging. We update the vmcs01 here for L1 as well
3886 * since it will end up touching the MSR anyway now.
3887 */
3888 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3889 MSR_IA32_SPEC_CTRL,
3890 MSR_TYPE_RW);
3891 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003892 case MSR_IA32_PRED_CMD:
3893 if (!msr_info->host_initiated &&
3894 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3895 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3896 return 1;
3897
3898 if (data & ~PRED_CMD_IBPB)
3899 return 1;
3900
3901 if (!data)
3902 break;
3903
3904 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3905
3906 /*
3907 * For non-nested:
3908 * When it's written (to non-zero) for the first time, pass
3909 * it through.
3910 *
3911 * For nested:
3912 * The handling of the MSR bitmap for L2 guests is done in
3913 * nested_vmx_merge_msr_bitmap. We should not touch the
3914 * vmcs02.msr_bitmap here since it gets completely overwritten
3915 * in the merging.
3916 */
3917 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3918 MSR_TYPE_W);
3919 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003920 case MSR_IA32_ARCH_CAPABILITIES:
3921 if (!msr_info->host_initiated)
3922 return 1;
3923 vmx->arch_capabilities = data;
3924 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003925 case MSR_IA32_CR_PAT:
3926 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003927 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3928 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003929 vmcs_write64(GUEST_IA32_PAT, data);
3930 vcpu->arch.pat = data;
3931 break;
3932 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003933 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003934 break;
Will Auldba904632012-11-29 12:42:50 -08003935 case MSR_IA32_TSC_ADJUST:
3936 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003937 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003938 case MSR_IA32_MCG_EXT_CTL:
3939 if ((!msr_info->host_initiated &&
3940 !(to_vmx(vcpu)->msr_ia32_feature_control &
3941 FEATURE_CONTROL_LMCE)) ||
3942 (data & ~MCG_EXT_CTL_LMCE_EN))
3943 return 1;
3944 vcpu->arch.mcg_ext_ctl = data;
3945 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003946 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003947 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003948 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003949 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3950 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003951 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003952 if (msr_info->host_initiated && data == 0)
3953 vmx_leave_nested(vcpu);
3954 break;
3955 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003956 if (!msr_info->host_initiated)
3957 return 1; /* they are read-only */
3958 if (!nested_vmx_allowed(vcpu))
3959 return 1;
3960 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003961 case MSR_IA32_XSS:
3962 if (!vmx_xsaves_supported())
3963 return 1;
3964 /*
3965 * The only supported bit as of Skylake is bit 8, but
3966 * it is not supported on KVM.
3967 */
3968 if (data != 0)
3969 return 1;
3970 vcpu->arch.ia32_xss = data;
3971 if (vcpu->arch.ia32_xss != host_xss)
3972 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3973 vcpu->arch.ia32_xss, host_xss);
3974 else
3975 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3976 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003977 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003978 if (!msr_info->host_initiated &&
3979 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003980 return 1;
3981 /* Check reserved bit, higher 32 bits should be zero */
3982 if ((data >> 32) != 0)
3983 return 1;
3984 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003986 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003987 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003988 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003989 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003990 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3991 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003992 ret = kvm_set_shared_msr(msr->index, msr->data,
3993 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003994 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003995 if (ret)
3996 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003997 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003998 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004000 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004001 }
4002
Eddie Dong2cc51562007-05-21 07:28:09 +03004003 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004}
4005
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004006static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004007{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004008 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4009 switch (reg) {
4010 case VCPU_REGS_RSP:
4011 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4012 break;
4013 case VCPU_REGS_RIP:
4014 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4015 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004016 case VCPU_EXREG_PDPTR:
4017 if (enable_ept)
4018 ept_save_pdptrs(vcpu);
4019 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004020 default:
4021 break;
4022 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023}
4024
Avi Kivity6aa8b732006-12-10 02:21:36 -08004025static __init int cpu_has_kvm_support(void)
4026{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004027 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028}
4029
4030static __init int vmx_disabled_by_bios(void)
4031{
4032 u64 msr;
4033
4034 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004035 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004036 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004037 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4038 && tboot_enabled())
4039 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004040 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004041 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004042 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004043 && !tboot_enabled()) {
4044 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004045 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004046 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004047 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004048 /* launched w/o TXT and VMX disabled */
4049 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4050 && !tboot_enabled())
4051 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004052 }
4053
4054 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055}
4056
Dongxiao Xu7725b892010-05-11 18:29:38 +08004057static void kvm_cpu_vmxon(u64 addr)
4058{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004059 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004060 intel_pt_handle_vmx(1);
4061
Dongxiao Xu7725b892010-05-11 18:29:38 +08004062 asm volatile (ASM_VMX_VMXON_RAX
4063 : : "a"(&addr), "m"(addr)
4064 : "memory", "cc");
4065}
4066
Radim Krčmář13a34e02014-08-28 15:13:03 +02004067static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068{
4069 int cpu = raw_smp_processor_id();
4070 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004071 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004073 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004074 return -EBUSY;
4075
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004076 /*
4077 * This can happen if we hot-added a CPU but failed to allocate
4078 * VP assist page for it.
4079 */
4080 if (static_branch_unlikely(&enable_evmcs) &&
4081 !hv_get_vp_assist_page(cpu))
4082 return -EFAULT;
4083
Nadav Har'Eld462b812011-05-24 15:26:10 +03004084 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004085 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4086 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004087
4088 /*
4089 * Now we can enable the vmclear operation in kdump
4090 * since the loaded_vmcss_on_cpu list on this cpu
4091 * has been initialized.
4092 *
4093 * Though the cpu is not in VMX operation now, there
4094 * is no problem to enable the vmclear operation
4095 * for the loaded_vmcss_on_cpu list is empty!
4096 */
4097 crash_enable_local_vmclear(cpu);
4098
Avi Kivity6aa8b732006-12-10 02:21:36 -08004099 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004100
4101 test_bits = FEATURE_CONTROL_LOCKED;
4102 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4103 if (tboot_enabled())
4104 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4105
4106 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004108 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4109 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004110 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004111 if (enable_ept)
4112 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004113
4114 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004115}
4116
Nadav Har'Eld462b812011-05-24 15:26:10 +03004117static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004118{
4119 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004120 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004121
Nadav Har'Eld462b812011-05-24 15:26:10 +03004122 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4123 loaded_vmcss_on_cpu_link)
4124 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004125}
4126
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004127
4128/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4129 * tricks.
4130 */
4131static void kvm_cpu_vmxoff(void)
4132{
4133 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004134
4135 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004136 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004137}
4138
Radim Krčmář13a34e02014-08-28 15:13:03 +02004139static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004141 vmclear_local_loaded_vmcss();
4142 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143}
4144
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004145static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004146 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147{
4148 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004149 u32 ctl = ctl_min | ctl_opt;
4150
4151 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4152
4153 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4154 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4155
4156 /* Ensure minimum (required) set of control bits are supported. */
4157 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004158 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004159
4160 *result = ctl;
4161 return 0;
4162}
4163
Avi Kivity110312c2010-12-21 12:54:20 +02004164static __init bool allow_1_setting(u32 msr, u32 ctl)
4165{
4166 u32 vmx_msr_low, vmx_msr_high;
4167
4168 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4169 return vmx_msr_high & ctl;
4170}
4171
Yang, Sheng002c7f72007-07-31 14:23:01 +03004172static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004173{
4174 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004175 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004176 u32 _pin_based_exec_control = 0;
4177 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004178 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004179 u32 _vmexit_control = 0;
4180 u32 _vmentry_control = 0;
4181
Paolo Bonzini13893092018-02-26 13:40:09 +01004182 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304183 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004184#ifdef CONFIG_X86_64
4185 CPU_BASED_CR8_LOAD_EXITING |
4186 CPU_BASED_CR8_STORE_EXITING |
4187#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004188 CPU_BASED_CR3_LOAD_EXITING |
4189 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004190 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004191 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004192 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004193 CPU_BASED_MWAIT_EXITING |
4194 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004195 CPU_BASED_INVLPG_EXITING |
4196 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004197
Sheng Yangf78e0e22007-10-29 09:40:42 +08004198 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004199 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004200 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004201 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4202 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004203 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004204#ifdef CONFIG_X86_64
4205 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4206 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4207 ~CPU_BASED_CR8_STORE_EXITING;
4208#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004209 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004210 min2 = 0;
4211 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004212 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004213 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004214 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004215 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004216 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004217 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004218 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004219 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004220 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004221 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004222 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004223 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004224 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004225 SECONDARY_EXEC_RDSEED_EXITING |
4226 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004227 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004228 SECONDARY_EXEC_TSC_SCALING |
4229 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004230 if (adjust_vmx_controls(min2, opt2,
4231 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004232 &_cpu_based_2nd_exec_control) < 0)
4233 return -EIO;
4234 }
4235#ifndef CONFIG_X86_64
4236 if (!(_cpu_based_2nd_exec_control &
4237 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4238 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4239#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004240
4241 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4242 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004243 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004244 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4245 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004246
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004247 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4248 &vmx_capability.ept, &vmx_capability.vpid);
4249
Sheng Yangd56f5462008-04-25 10:13:16 +08004250 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004251 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4252 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004253 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4254 CPU_BASED_CR3_STORE_EXITING |
4255 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004256 } else if (vmx_capability.ept) {
4257 vmx_capability.ept = 0;
4258 pr_warn_once("EPT CAP should not exist if not support "
4259 "1-setting enable EPT VM-execution control\n");
4260 }
4261 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4262 vmx_capability.vpid) {
4263 vmx_capability.vpid = 0;
4264 pr_warn_once("VPID CAP should not exist if not support "
4265 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004266 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004267
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004268 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004269#ifdef CONFIG_X86_64
4270 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4271#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004272 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004273 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004274 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4275 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004276 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004277
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004278 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4279 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4280 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004281 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4282 &_pin_based_exec_control) < 0)
4283 return -EIO;
4284
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004285 if (cpu_has_broken_vmx_preemption_timer())
4286 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004287 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004288 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004289 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4290
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004291 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004292 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004293 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4294 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004295 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004296
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004297 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004298
4299 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4300 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004301 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004302
4303#ifdef CONFIG_X86_64
4304 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4305 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004306 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004307#endif
4308
4309 /* Require Write-Back (WB) memory type for VMCS accesses. */
4310 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004311 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004312
Yang, Sheng002c7f72007-07-31 14:23:01 +03004313 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004314 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004315 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004316
4317 /* KVM supports Enlightened VMCS v1 only */
4318 if (static_branch_unlikely(&enable_evmcs))
4319 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4320 else
4321 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004322
Yang, Sheng002c7f72007-07-31 14:23:01 +03004323 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4324 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004325 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004326 vmcs_conf->vmexit_ctrl = _vmexit_control;
4327 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004328
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004329 if (static_branch_unlikely(&enable_evmcs))
4330 evmcs_sanitize_exec_ctrls(vmcs_conf);
4331
Avi Kivity110312c2010-12-21 12:54:20 +02004332 cpu_has_load_ia32_efer =
4333 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4334 VM_ENTRY_LOAD_IA32_EFER)
4335 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4336 VM_EXIT_LOAD_IA32_EFER);
4337
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004338 cpu_has_load_perf_global_ctrl =
4339 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4340 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4341 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4342 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4343
4344 /*
4345 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004346 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004347 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4348 *
4349 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4350 *
4351 * AAK155 (model 26)
4352 * AAP115 (model 30)
4353 * AAT100 (model 37)
4354 * BC86,AAY89,BD102 (model 44)
4355 * BA97 (model 46)
4356 *
4357 */
4358 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4359 switch (boot_cpu_data.x86_model) {
4360 case 26:
4361 case 30:
4362 case 37:
4363 case 44:
4364 case 46:
4365 cpu_has_load_perf_global_ctrl = false;
4366 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4367 "does not work properly. Using workaround\n");
4368 break;
4369 default:
4370 break;
4371 }
4372 }
4373
Borislav Petkov782511b2016-04-04 22:25:03 +02004374 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004375 rdmsrl(MSR_IA32_XSS, host_xss);
4376
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004377 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004378}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004379
4380static struct vmcs *alloc_vmcs_cpu(int cpu)
4381{
4382 int node = cpu_to_node(cpu);
4383 struct page *pages;
4384 struct vmcs *vmcs;
4385
Vlastimil Babka96db8002015-09-08 15:03:50 -07004386 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387 if (!pages)
4388 return NULL;
4389 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004390 memset(vmcs, 0, vmcs_config.size);
4391 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392 return vmcs;
4393}
4394
Avi Kivity6aa8b732006-12-10 02:21:36 -08004395static void free_vmcs(struct vmcs *vmcs)
4396{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004397 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004398}
4399
Nadav Har'Eld462b812011-05-24 15:26:10 +03004400/*
4401 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4402 */
4403static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4404{
4405 if (!loaded_vmcs->vmcs)
4406 return;
4407 loaded_vmcs_clear(loaded_vmcs);
4408 free_vmcs(loaded_vmcs->vmcs);
4409 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004410 if (loaded_vmcs->msr_bitmap)
4411 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004412 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004413}
4414
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004415static struct vmcs *alloc_vmcs(void)
4416{
4417 return alloc_vmcs_cpu(raw_smp_processor_id());
4418}
4419
4420static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4421{
4422 loaded_vmcs->vmcs = alloc_vmcs();
4423 if (!loaded_vmcs->vmcs)
4424 return -ENOMEM;
4425
4426 loaded_vmcs->shadow_vmcs = NULL;
4427 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004428
4429 if (cpu_has_vmx_msr_bitmap()) {
4430 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4431 if (!loaded_vmcs->msr_bitmap)
4432 goto out_vmcs;
4433 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004434
4435 if (static_branch_unlikely(&enable_evmcs) &&
4436 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4437 struct hv_enlightened_vmcs *evmcs =
4438 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4439
4440 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4441 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004442 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004443 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004444
4445out_vmcs:
4446 free_loaded_vmcs(loaded_vmcs);
4447 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004448}
4449
Sam Ravnborg39959582007-06-01 00:47:13 -07004450static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004451{
4452 int cpu;
4453
Zachary Amsden3230bb42009-09-29 11:38:37 -10004454 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004455 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004456 per_cpu(vmxarea, cpu) = NULL;
4457 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458}
4459
Jim Mattsond37f4262017-12-22 12:12:16 -08004460enum vmcs_field_width {
4461 VMCS_FIELD_WIDTH_U16 = 0,
4462 VMCS_FIELD_WIDTH_U64 = 1,
4463 VMCS_FIELD_WIDTH_U32 = 2,
4464 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004465};
4466
Jim Mattsond37f4262017-12-22 12:12:16 -08004467static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004468{
4469 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004470 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004471 return (field >> 13) & 0x3 ;
4472}
4473
4474static inline int vmcs_field_readonly(unsigned long field)
4475{
4476 return (((field >> 10) & 0x3) == 1);
4477}
4478
Bandan Dasfe2b2012014-04-21 15:20:14 -04004479static void init_vmcs_shadow_fields(void)
4480{
4481 int i, j;
4482
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004483 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4484 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004485 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004486 (i + 1 == max_shadow_read_only_fields ||
4487 shadow_read_only_fields[i + 1] != field + 1))
4488 pr_err("Missing field from shadow_read_only_field %x\n",
4489 field + 1);
4490
4491 clear_bit(field, vmx_vmread_bitmap);
4492#ifdef CONFIG_X86_64
4493 if (field & 1)
4494 continue;
4495#endif
4496 if (j < i)
4497 shadow_read_only_fields[j] = field;
4498 j++;
4499 }
4500 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004501
4502 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004503 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004504 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004505 (i + 1 == max_shadow_read_write_fields ||
4506 shadow_read_write_fields[i + 1] != field + 1))
4507 pr_err("Missing field from shadow_read_write_field %x\n",
4508 field + 1);
4509
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004510 /*
4511 * PML and the preemption timer can be emulated, but the
4512 * processor cannot vmwrite to fields that don't exist
4513 * on bare metal.
4514 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004515 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004516 case GUEST_PML_INDEX:
4517 if (!cpu_has_vmx_pml())
4518 continue;
4519 break;
4520 case VMX_PREEMPTION_TIMER_VALUE:
4521 if (!cpu_has_vmx_preemption_timer())
4522 continue;
4523 break;
4524 case GUEST_INTR_STATUS:
4525 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004526 continue;
4527 break;
4528 default:
4529 break;
4530 }
4531
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004532 clear_bit(field, vmx_vmwrite_bitmap);
4533 clear_bit(field, vmx_vmread_bitmap);
4534#ifdef CONFIG_X86_64
4535 if (field & 1)
4536 continue;
4537#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004538 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004539 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004540 j++;
4541 }
4542 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004543}
4544
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545static __init int alloc_kvm_area(void)
4546{
4547 int cpu;
4548
Zachary Amsden3230bb42009-09-29 11:38:37 -10004549 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004550 struct vmcs *vmcs;
4551
4552 vmcs = alloc_vmcs_cpu(cpu);
4553 if (!vmcs) {
4554 free_kvm_area();
4555 return -ENOMEM;
4556 }
4557
4558 per_cpu(vmxarea, cpu) = vmcs;
4559 }
4560 return 0;
4561}
4562
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004563static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004564 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004566 if (!emulate_invalid_guest_state) {
4567 /*
4568 * CS and SS RPL should be equal during guest entry according
4569 * to VMX spec, but in reality it is not always so. Since vcpu
4570 * is in the middle of the transition from real mode to
4571 * protected mode it is safe to assume that RPL 0 is a good
4572 * default value.
4573 */
4574 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004575 save->selector &= ~SEGMENT_RPL_MASK;
4576 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004577 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004578 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004579 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580}
4581
4582static void enter_pmode(struct kvm_vcpu *vcpu)
4583{
4584 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004585 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586
Gleb Natapovd99e4152012-12-20 16:57:45 +02004587 /*
4588 * Update real mode segment cache. It may be not up-to-date if sement
4589 * register was written while vcpu was in a guest mode.
4590 */
4591 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4592 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4593 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4594 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4595 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4596 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4597
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004598 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004599
Avi Kivity2fb92db2011-04-27 19:42:18 +03004600 vmx_segment_cache_clear(vmx);
4601
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004602 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004603
4604 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004605 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4606 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004607 vmcs_writel(GUEST_RFLAGS, flags);
4608
Rusty Russell66aee912007-07-17 23:34:16 +10004609 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4610 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004611
4612 update_exception_bitmap(vcpu);
4613
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004614 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4615 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4616 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4617 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4618 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4619 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620}
4621
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004622static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623{
Mathias Krause772e0312012-08-30 01:30:19 +02004624 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004625 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004626
Gleb Natapovd99e4152012-12-20 16:57:45 +02004627 var.dpl = 0x3;
4628 if (seg == VCPU_SREG_CS)
4629 var.type = 0x3;
4630
4631 if (!emulate_invalid_guest_state) {
4632 var.selector = var.base >> 4;
4633 var.base = var.base & 0xffff0;
4634 var.limit = 0xffff;
4635 var.g = 0;
4636 var.db = 0;
4637 var.present = 1;
4638 var.s = 1;
4639 var.l = 0;
4640 var.unusable = 0;
4641 var.type = 0x3;
4642 var.avl = 0;
4643 if (save->base & 0xf)
4644 printk_once(KERN_WARNING "kvm: segment base is not "
4645 "paragraph aligned when entering "
4646 "protected mode (seg=%d)", seg);
4647 }
4648
4649 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004650 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004651 vmcs_write32(sf->limit, var.limit);
4652 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004653}
4654
4655static void enter_rmode(struct kvm_vcpu *vcpu)
4656{
4657 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004658 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004659 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004661 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4662 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4663 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4664 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4665 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004666 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4667 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004668
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004669 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004670
Gleb Natapov776e58e2011-03-13 12:34:27 +02004671 /*
4672 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004673 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004674 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004675 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004676 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4677 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004678
Avi Kivity2fb92db2011-04-27 19:42:18 +03004679 vmx_segment_cache_clear(vmx);
4680
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004681 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4684
4685 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004686 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004687
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004688 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689
4690 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004691 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692 update_exception_bitmap(vcpu);
4693
Gleb Natapovd99e4152012-12-20 16:57:45 +02004694 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4695 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4696 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4697 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4698 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4699 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004700
Eddie Dong8668a3c2007-10-10 14:26:45 +08004701 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702}
4703
Amit Shah401d10d2009-02-20 22:53:37 +05304704static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4705{
4706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004707 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4708
4709 if (!msr)
4710 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304711
Avi Kivity44ea2b12009-09-06 15:55:37 +03004712 /*
4713 * Force kernel_gs_base reloading before EFER changes, as control
4714 * of this msr depends on is_long_mode().
4715 */
4716 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004717 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304718 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004719 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304720 msr->data = efer;
4721 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004722 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304723
4724 msr->data = efer & ~EFER_LME;
4725 }
4726 setup_msrs(vmx);
4727}
4728
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004729#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730
4731static void enter_lmode(struct kvm_vcpu *vcpu)
4732{
4733 u32 guest_tr_ar;
4734
Avi Kivity2fb92db2011-04-27 19:42:18 +03004735 vmx_segment_cache_clear(to_vmx(vcpu));
4736
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004738 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004739 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4740 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004742 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4743 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744 }
Avi Kivityda38f432010-07-06 11:30:49 +03004745 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746}
4747
4748static void exit_lmode(struct kvm_vcpu *vcpu)
4749{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004750 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004751 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752}
4753
4754#endif
4755
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004756static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4757 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004758{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004759 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004760 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4761 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004762 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004763 } else {
4764 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004765 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004766}
4767
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004768static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004769{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004770 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004771}
4772
Avi Kivitye8467fd2009-12-29 18:43:06 +02004773static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4774{
4775 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4776
4777 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4778 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4779}
4780
Avi Kivityaff48ba2010-12-05 18:56:11 +02004781static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4782{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004783 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004784 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4785 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4786}
4787
Anthony Liguori25c4c272007-04-27 09:29:21 +03004788static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004789{
Avi Kivityfc78f512009-12-07 12:16:48 +02004790 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4791
4792 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4793 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004794}
4795
Sheng Yang14394422008-04-28 12:24:45 +08004796static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4797{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004798 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4799
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004800 if (!test_bit(VCPU_EXREG_PDPTR,
4801 (unsigned long *)&vcpu->arch.regs_dirty))
4802 return;
4803
Sheng Yang14394422008-04-28 12:24:45 +08004804 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004805 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4806 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4807 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4808 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004809 }
4810}
4811
Avi Kivity8f5d5492009-05-31 18:41:29 +03004812static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4813{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004814 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4815
Avi Kivity8f5d5492009-05-31 18:41:29 +03004816 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004817 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4818 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4819 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4820 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004821 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004822
4823 __set_bit(VCPU_EXREG_PDPTR,
4824 (unsigned long *)&vcpu->arch.regs_avail);
4825 __set_bit(VCPU_EXREG_PDPTR,
4826 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004827}
4828
David Matlack38991522016-11-29 18:14:08 -08004829static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4830{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004831 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4832 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004833 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4834
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004835 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004836 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4837 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4838 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4839
4840 return fixed_bits_valid(val, fixed0, fixed1);
4841}
4842
4843static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4844{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004845 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4846 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004847
4848 return fixed_bits_valid(val, fixed0, fixed1);
4849}
4850
4851static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4852{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004853 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4854 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004855
4856 return fixed_bits_valid(val, fixed0, fixed1);
4857}
4858
4859/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4860#define nested_guest_cr4_valid nested_cr4_valid
4861#define nested_host_cr4_valid nested_cr4_valid
4862
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004863static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004864
4865static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4866 unsigned long cr0,
4867 struct kvm_vcpu *vcpu)
4868{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004869 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4870 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004871 if (!(cr0 & X86_CR0_PG)) {
4872 /* From paging/starting to nonpaging */
4873 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004874 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004875 (CPU_BASED_CR3_LOAD_EXITING |
4876 CPU_BASED_CR3_STORE_EXITING));
4877 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004878 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004879 } else if (!is_paging(vcpu)) {
4880 /* From nonpaging to paging */
4881 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004882 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004883 ~(CPU_BASED_CR3_LOAD_EXITING |
4884 CPU_BASED_CR3_STORE_EXITING));
4885 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004886 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004887 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004888
4889 if (!(cr0 & X86_CR0_WP))
4890 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004891}
4892
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4894{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004895 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004896 unsigned long hw_cr0;
4897
Gleb Natapov50378782013-02-04 16:00:28 +02004898 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004899 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004900 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004901 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004902 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004903
Gleb Natapov218e7632013-01-21 15:36:45 +02004904 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4905 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004906
Gleb Natapov218e7632013-01-21 15:36:45 +02004907 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4908 enter_rmode(vcpu);
4909 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004910
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004911#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004912 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004913 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004914 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004915 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 exit_lmode(vcpu);
4917 }
4918#endif
4919
Sean Christophersonb4d18512018-03-05 12:04:40 -08004920 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004921 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4922
Avi Kivity6aa8b732006-12-10 02:21:36 -08004923 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004924 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004925 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004926
4927 /* depends on vcpu->arch.cr0 to be set to a new value */
4928 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929}
4930
Yu Zhang855feb62017-08-24 20:27:55 +08004931static int get_ept_level(struct kvm_vcpu *vcpu)
4932{
4933 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4934 return 5;
4935 return 4;
4936}
4937
Peter Feiner995f00a2017-06-30 17:26:32 -07004938static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004939{
Yu Zhang855feb62017-08-24 20:27:55 +08004940 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004941
Yu Zhang855feb62017-08-24 20:27:55 +08004942 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004943
Peter Feiner995f00a2017-06-30 17:26:32 -07004944 if (enable_ept_ad_bits &&
4945 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004946 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004947 eptp |= (root_hpa & PAGE_MASK);
4948
4949 return eptp;
4950}
4951
Avi Kivity6aa8b732006-12-10 02:21:36 -08004952static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4953{
Sheng Yang14394422008-04-28 12:24:45 +08004954 unsigned long guest_cr3;
4955 u64 eptp;
4956
4957 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004958 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004959 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004960 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004961 if (enable_unrestricted_guest || is_paging(vcpu) ||
4962 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004963 guest_cr3 = kvm_read_cr3(vcpu);
4964 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004965 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004966 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004967 }
4968
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004969 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004970 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971}
4972
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004973static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004975 /*
4976 * Pass through host's Machine Check Enable value to hw_cr4, which
4977 * is in force while we are in guest mode. Do not let guests control
4978 * this bit, even if host CR4.MCE == 0.
4979 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004980 unsigned long hw_cr4;
4981
4982 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4983 if (enable_unrestricted_guest)
4984 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4985 else if (to_vmx(vcpu)->rmode.vm86_active)
4986 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4987 else
4988 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004989
Sean Christopherson64f7a112018-04-30 10:01:06 -07004990 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4991 if (cr4 & X86_CR4_UMIP) {
4992 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02004993 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07004994 hw_cr4 &= ~X86_CR4_UMIP;
4995 } else if (!is_guest_mode(vcpu) ||
4996 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4997 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4998 SECONDARY_EXEC_DESC);
4999 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005000
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005001 if (cr4 & X86_CR4_VMXE) {
5002 /*
5003 * To use VMXON (and later other VMX instructions), a guest
5004 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5005 * So basically the check on whether to allow nested VMX
5006 * is here.
5007 */
5008 if (!nested_vmx_allowed(vcpu))
5009 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005010 }
David Matlack38991522016-11-29 18:14:08 -08005011
5012 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005013 return 1;
5014
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005015 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005016
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005017 if (!enable_unrestricted_guest) {
5018 if (enable_ept) {
5019 if (!is_paging(vcpu)) {
5020 hw_cr4 &= ~X86_CR4_PAE;
5021 hw_cr4 |= X86_CR4_PSE;
5022 } else if (!(cr4 & X86_CR4_PAE)) {
5023 hw_cr4 &= ~X86_CR4_PAE;
5024 }
5025 }
5026
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005027 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005028 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5029 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5030 * to be manually disabled when guest switches to non-paging
5031 * mode.
5032 *
5033 * If !enable_unrestricted_guest, the CPU is always running
5034 * with CR0.PG=1 and CR4 needs to be modified.
5035 * If enable_unrestricted_guest, the CPU automatically
5036 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005037 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005038 if (!is_paging(vcpu))
5039 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5040 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005041
Sheng Yang14394422008-04-28 12:24:45 +08005042 vmcs_writel(CR4_READ_SHADOW, cr4);
5043 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005044 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045}
5046
Avi Kivity6aa8b732006-12-10 02:21:36 -08005047static void vmx_get_segment(struct kvm_vcpu *vcpu,
5048 struct kvm_segment *var, int seg)
5049{
Avi Kivitya9179492011-01-03 14:28:52 +02005050 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 u32 ar;
5052
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005053 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005054 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005055 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005056 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005057 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005058 var->base = vmx_read_guest_seg_base(vmx, seg);
5059 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5060 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005061 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005062 var->base = vmx_read_guest_seg_base(vmx, seg);
5063 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5064 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5065 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005066 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067 var->type = ar & 15;
5068 var->s = (ar >> 4) & 1;
5069 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005070 /*
5071 * Some userspaces do not preserve unusable property. Since usable
5072 * segment has to be present according to VMX spec we can use present
5073 * property to amend userspace bug by making unusable segment always
5074 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5075 * segment as unusable.
5076 */
5077 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078 var->avl = (ar >> 12) & 1;
5079 var->l = (ar >> 13) & 1;
5080 var->db = (ar >> 14) & 1;
5081 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082}
5083
Avi Kivitya9179492011-01-03 14:28:52 +02005084static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5085{
Avi Kivitya9179492011-01-03 14:28:52 +02005086 struct kvm_segment s;
5087
5088 if (to_vmx(vcpu)->rmode.vm86_active) {
5089 vmx_get_segment(vcpu, &s, seg);
5090 return s.base;
5091 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005092 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005093}
5094
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005095static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005096{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005097 struct vcpu_vmx *vmx = to_vmx(vcpu);
5098
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005099 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005100 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005101 else {
5102 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005103 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005104 }
Avi Kivity69c73022011-03-07 15:26:44 +02005105}
5106
Avi Kivity653e3102007-05-07 10:55:37 +03005107static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005108{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005109 u32 ar;
5110
Avi Kivityf0495f92012-06-07 17:06:10 +03005111 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005112 ar = 1 << 16;
5113 else {
5114 ar = var->type & 15;
5115 ar |= (var->s & 1) << 4;
5116 ar |= (var->dpl & 3) << 5;
5117 ar |= (var->present & 1) << 7;
5118 ar |= (var->avl & 1) << 12;
5119 ar |= (var->l & 1) << 13;
5120 ar |= (var->db & 1) << 14;
5121 ar |= (var->g & 1) << 15;
5122 }
Avi Kivity653e3102007-05-07 10:55:37 +03005123
5124 return ar;
5125}
5126
5127static void vmx_set_segment(struct kvm_vcpu *vcpu,
5128 struct kvm_segment *var, int seg)
5129{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005130 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005131 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005132
Avi Kivity2fb92db2011-04-27 19:42:18 +03005133 vmx_segment_cache_clear(vmx);
5134
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005135 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5136 vmx->rmode.segs[seg] = *var;
5137 if (seg == VCPU_SREG_TR)
5138 vmcs_write16(sf->selector, var->selector);
5139 else if (var->s)
5140 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005141 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005142 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005143
Avi Kivity653e3102007-05-07 10:55:37 +03005144 vmcs_writel(sf->base, var->base);
5145 vmcs_write32(sf->limit, var->limit);
5146 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005147
5148 /*
5149 * Fix the "Accessed" bit in AR field of segment registers for older
5150 * qemu binaries.
5151 * IA32 arch specifies that at the time of processor reset the
5152 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005153 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005154 * state vmexit when "unrestricted guest" mode is turned on.
5155 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5156 * tree. Newer qemu binaries with that qemu fix would not need this
5157 * kvm hack.
5158 */
5159 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005160 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005161
Gleb Natapovf924d662012-12-12 19:10:55 +02005162 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005163
5164out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005165 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166}
5167
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5169{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005170 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171
5172 *db = (ar >> 14) & 1;
5173 *l = (ar >> 13) & 1;
5174}
5175
Gleb Natapov89a27f42010-02-16 10:51:48 +02005176static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005178 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5179 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180}
5181
Gleb Natapov89a27f42010-02-16 10:51:48 +02005182static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005183{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005184 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5185 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005186}
5187
Gleb Natapov89a27f42010-02-16 10:51:48 +02005188static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005189{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005190 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5191 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005192}
5193
Gleb Natapov89a27f42010-02-16 10:51:48 +02005194static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005195{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005196 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5197 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198}
5199
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005200static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5201{
5202 struct kvm_segment var;
5203 u32 ar;
5204
5205 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005206 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005207 if (seg == VCPU_SREG_CS)
5208 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005209 ar = vmx_segment_access_rights(&var);
5210
5211 if (var.base != (var.selector << 4))
5212 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005213 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005214 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005215 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005216 return false;
5217
5218 return true;
5219}
5220
5221static bool code_segment_valid(struct kvm_vcpu *vcpu)
5222{
5223 struct kvm_segment cs;
5224 unsigned int cs_rpl;
5225
5226 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005227 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005228
Avi Kivity1872a3f2009-01-04 23:26:52 +02005229 if (cs.unusable)
5230 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005231 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005232 return false;
5233 if (!cs.s)
5234 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005235 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005236 if (cs.dpl > cs_rpl)
5237 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005238 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005239 if (cs.dpl != cs_rpl)
5240 return false;
5241 }
5242 if (!cs.present)
5243 return false;
5244
5245 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5246 return true;
5247}
5248
5249static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5250{
5251 struct kvm_segment ss;
5252 unsigned int ss_rpl;
5253
5254 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005255 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005256
Avi Kivity1872a3f2009-01-04 23:26:52 +02005257 if (ss.unusable)
5258 return true;
5259 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005260 return false;
5261 if (!ss.s)
5262 return false;
5263 if (ss.dpl != ss_rpl) /* DPL != RPL */
5264 return false;
5265 if (!ss.present)
5266 return false;
5267
5268 return true;
5269}
5270
5271static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5272{
5273 struct kvm_segment var;
5274 unsigned int rpl;
5275
5276 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005277 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005278
Avi Kivity1872a3f2009-01-04 23:26:52 +02005279 if (var.unusable)
5280 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005281 if (!var.s)
5282 return false;
5283 if (!var.present)
5284 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005285 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005286 if (var.dpl < rpl) /* DPL < RPL */
5287 return false;
5288 }
5289
5290 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5291 * rights flags
5292 */
5293 return true;
5294}
5295
5296static bool tr_valid(struct kvm_vcpu *vcpu)
5297{
5298 struct kvm_segment tr;
5299
5300 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5301
Avi Kivity1872a3f2009-01-04 23:26:52 +02005302 if (tr.unusable)
5303 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005304 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005305 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005306 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005307 return false;
5308 if (!tr.present)
5309 return false;
5310
5311 return true;
5312}
5313
5314static bool ldtr_valid(struct kvm_vcpu *vcpu)
5315{
5316 struct kvm_segment ldtr;
5317
5318 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5319
Avi Kivity1872a3f2009-01-04 23:26:52 +02005320 if (ldtr.unusable)
5321 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005322 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005323 return false;
5324 if (ldtr.type != 2)
5325 return false;
5326 if (!ldtr.present)
5327 return false;
5328
5329 return true;
5330}
5331
5332static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5333{
5334 struct kvm_segment cs, ss;
5335
5336 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5337 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5338
Nadav Amitb32a9912015-03-29 16:33:04 +03005339 return ((cs.selector & SEGMENT_RPL_MASK) ==
5340 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005341}
5342
5343/*
5344 * Check if guest state is valid. Returns true if valid, false if
5345 * not.
5346 * We assume that registers are always usable
5347 */
5348static bool guest_state_valid(struct kvm_vcpu *vcpu)
5349{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005350 if (enable_unrestricted_guest)
5351 return true;
5352
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005353 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005354 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005355 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5356 return false;
5357 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5358 return false;
5359 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5360 return false;
5361 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5362 return false;
5363 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5364 return false;
5365 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5366 return false;
5367 } else {
5368 /* protected mode guest state checks */
5369 if (!cs_ss_rpl_check(vcpu))
5370 return false;
5371 if (!code_segment_valid(vcpu))
5372 return false;
5373 if (!stack_segment_valid(vcpu))
5374 return false;
5375 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5376 return false;
5377 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5378 return false;
5379 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5380 return false;
5381 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5382 return false;
5383 if (!tr_valid(vcpu))
5384 return false;
5385 if (!ldtr_valid(vcpu))
5386 return false;
5387 }
5388 /* TODO:
5389 * - Add checks on RIP
5390 * - Add checks on RFLAGS
5391 */
5392
5393 return true;
5394}
5395
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005396static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5397{
5398 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5399}
5400
Mike Dayd77c26f2007-10-08 09:02:08 -04005401static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005402{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005403 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005404 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005405 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005406
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005407 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005408 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005409 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5410 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005411 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005412 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005413 r = kvm_write_guest_page(kvm, fn++, &data,
5414 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005415 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005416 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005417 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5418 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005419 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005420 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5421 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005422 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005423 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005424 r = kvm_write_guest_page(kvm, fn, &data,
5425 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5426 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005427out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005428 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005429 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005430}
5431
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005432static int init_rmode_identity_map(struct kvm *kvm)
5433{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005434 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005435 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005436 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005437 u32 tmp;
5438
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005439 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005440 mutex_lock(&kvm->slots_lock);
5441
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005442 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005443 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005444
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005445 if (!kvm_vmx->ept_identity_map_addr)
5446 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5447 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005448
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005449 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005450 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005451 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005452 goto out2;
5453
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005454 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005455 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5456 if (r < 0)
5457 goto out;
5458 /* Set up identity-mapping pagetable for EPT in real mode */
5459 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5460 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5461 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5462 r = kvm_write_guest_page(kvm, identity_map_pfn,
5463 &tmp, i * sizeof(tmp), sizeof(tmp));
5464 if (r < 0)
5465 goto out;
5466 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005467 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005468
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005469out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005470 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005471
5472out2:
5473 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005474 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005475}
5476
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477static void seg_setup(int seg)
5478{
Mathias Krause772e0312012-08-30 01:30:19 +02005479 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005480 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481
5482 vmcs_write16(sf->selector, 0);
5483 vmcs_writel(sf->base, 0);
5484 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005485 ar = 0x93;
5486 if (seg == VCPU_SREG_CS)
5487 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005488
5489 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005490}
5491
Sheng Yangf78e0e22007-10-29 09:40:42 +08005492static int alloc_apic_access_page(struct kvm *kvm)
5493{
Xiao Guangrong44841412012-09-07 14:14:20 +08005494 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005495 int r = 0;
5496
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005497 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005498 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005499 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005500 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5501 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005502 if (r)
5503 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005504
Tang Chen73a6d942014-09-11 13:38:00 +08005505 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005506 if (is_error_page(page)) {
5507 r = -EFAULT;
5508 goto out;
5509 }
5510
Tang Chenc24ae0d2014-09-24 15:57:58 +08005511 /*
5512 * Do not pin the page in memory, so that memory hot-unplug
5513 * is able to migrate it.
5514 */
5515 put_page(page);
5516 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005517out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005518 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005519 return r;
5520}
5521
Wanpeng Li991e7a02015-09-16 17:30:05 +08005522static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005523{
5524 int vpid;
5525
Avi Kivity919818a2009-03-23 18:01:29 +02005526 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005527 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005528 spin_lock(&vmx_vpid_lock);
5529 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005530 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005531 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005532 else
5533 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005534 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005535 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005536}
5537
Wanpeng Li991e7a02015-09-16 17:30:05 +08005538static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005539{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005540 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005541 return;
5542 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005543 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005544 spin_unlock(&vmx_vpid_lock);
5545}
5546
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005547static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5548 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005549{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005550 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005551
5552 if (!cpu_has_vmx_msr_bitmap())
5553 return;
5554
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005555 if (static_branch_unlikely(&enable_evmcs))
5556 evmcs_touch_msr_bitmap();
5557
Sheng Yang25c5f222008-03-28 13:18:56 +08005558 /*
5559 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5560 * have the write-low and read-high bitmap offsets the wrong way round.
5561 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5562 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005563 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005564 if (type & MSR_TYPE_R)
5565 /* read-low */
5566 __clear_bit(msr, msr_bitmap + 0x000 / f);
5567
5568 if (type & MSR_TYPE_W)
5569 /* write-low */
5570 __clear_bit(msr, msr_bitmap + 0x800 / f);
5571
Sheng Yang25c5f222008-03-28 13:18:56 +08005572 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5573 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005574 if (type & MSR_TYPE_R)
5575 /* read-high */
5576 __clear_bit(msr, msr_bitmap + 0x400 / f);
5577
5578 if (type & MSR_TYPE_W)
5579 /* write-high */
5580 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5581
5582 }
5583}
5584
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005585static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5586 u32 msr, int type)
5587{
5588 int f = sizeof(unsigned long);
5589
5590 if (!cpu_has_vmx_msr_bitmap())
5591 return;
5592
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005593 if (static_branch_unlikely(&enable_evmcs))
5594 evmcs_touch_msr_bitmap();
5595
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005596 /*
5597 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5598 * have the write-low and read-high bitmap offsets the wrong way round.
5599 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5600 */
5601 if (msr <= 0x1fff) {
5602 if (type & MSR_TYPE_R)
5603 /* read-low */
5604 __set_bit(msr, msr_bitmap + 0x000 / f);
5605
5606 if (type & MSR_TYPE_W)
5607 /* write-low */
5608 __set_bit(msr, msr_bitmap + 0x800 / f);
5609
5610 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5611 msr &= 0x1fff;
5612 if (type & MSR_TYPE_R)
5613 /* read-high */
5614 __set_bit(msr, msr_bitmap + 0x400 / f);
5615
5616 if (type & MSR_TYPE_W)
5617 /* write-high */
5618 __set_bit(msr, msr_bitmap + 0xc00 / f);
5619
5620 }
5621}
5622
5623static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5624 u32 msr, int type, bool value)
5625{
5626 if (value)
5627 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5628 else
5629 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5630}
5631
Wincy Vanf2b93282015-02-03 23:56:03 +08005632/*
5633 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5634 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5635 */
5636static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5637 unsigned long *msr_bitmap_nested,
5638 u32 msr, int type)
5639{
5640 int f = sizeof(unsigned long);
5641
Wincy Vanf2b93282015-02-03 23:56:03 +08005642 /*
5643 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5644 * have the write-low and read-high bitmap offsets the wrong way round.
5645 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5646 */
5647 if (msr <= 0x1fff) {
5648 if (type & MSR_TYPE_R &&
5649 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5650 /* read-low */
5651 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5652
5653 if (type & MSR_TYPE_W &&
5654 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5655 /* write-low */
5656 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5657
5658 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5659 msr &= 0x1fff;
5660 if (type & MSR_TYPE_R &&
5661 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5662 /* read-high */
5663 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5664
5665 if (type & MSR_TYPE_W &&
5666 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5667 /* write-high */
5668 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5669
5670 }
5671}
5672
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005673static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005674{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005675 u8 mode = 0;
5676
5677 if (cpu_has_secondary_exec_ctrls() &&
5678 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5679 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5680 mode |= MSR_BITMAP_MODE_X2APIC;
5681 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5682 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5683 }
5684
5685 if (is_long_mode(vcpu))
5686 mode |= MSR_BITMAP_MODE_LM;
5687
5688 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005689}
5690
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005691#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5692
5693static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5694 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005695{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005696 int msr;
5697
5698 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5699 unsigned word = msr / BITS_PER_LONG;
5700 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5701 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005702 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005703
5704 if (mode & MSR_BITMAP_MODE_X2APIC) {
5705 /*
5706 * TPR reads and writes can be virtualized even if virtual interrupt
5707 * delivery is not in use.
5708 */
5709 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5710 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5711 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5712 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5713 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5714 }
5715 }
5716}
5717
5718static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5719{
5720 struct vcpu_vmx *vmx = to_vmx(vcpu);
5721 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5722 u8 mode = vmx_msr_bitmap_mode(vcpu);
5723 u8 changed = mode ^ vmx->msr_bitmap_mode;
5724
5725 if (!changed)
5726 return;
5727
5728 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5729 !(mode & MSR_BITMAP_MODE_LM));
5730
5731 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5732 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5733
5734 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005735}
5736
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005737static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005738{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005739 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005740}
5741
David Matlackc9f04402017-08-01 14:00:40 -07005742static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5743{
5744 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5745 gfn_t gfn;
5746
5747 /*
5748 * Don't need to mark the APIC access page dirty; it is never
5749 * written to by the CPU during APIC virtualization.
5750 */
5751
5752 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5753 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5754 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5755 }
5756
5757 if (nested_cpu_has_posted_intr(vmcs12)) {
5758 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5759 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5760 }
5761}
5762
5763
David Hildenbrand6342c502017-01-25 11:58:58 +01005764static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005765{
5766 struct vcpu_vmx *vmx = to_vmx(vcpu);
5767 int max_irr;
5768 void *vapic_page;
5769 u16 status;
5770
David Matlackc9f04402017-08-01 14:00:40 -07005771 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5772 return;
Wincy Van705699a2015-02-03 23:58:17 +08005773
David Matlackc9f04402017-08-01 14:00:40 -07005774 vmx->nested.pi_pending = false;
5775 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5776 return;
Wincy Van705699a2015-02-03 23:58:17 +08005777
David Matlackc9f04402017-08-01 14:00:40 -07005778 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5779 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005780 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005781 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5782 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005783 kunmap(vmx->nested.virtual_apic_page);
5784
5785 status = vmcs_read16(GUEST_INTR_STATUS);
5786 if ((u8)max_irr > ((u8)status & 0xff)) {
5787 status &= ~0xff;
5788 status |= (u8)max_irr;
5789 vmcs_write16(GUEST_INTR_STATUS, status);
5790 }
5791 }
David Matlackc9f04402017-08-01 14:00:40 -07005792
5793 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005794}
5795
Wincy Van06a55242017-04-28 13:13:59 +08005796static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5797 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005798{
5799#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005800 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5801
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005802 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005803 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005804 * The vector of interrupt to be delivered to vcpu had
5805 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005806 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005807 * Following cases will be reached in this block, and
5808 * we always send a notification event in all cases as
5809 * explained below.
5810 *
5811 * Case 1: vcpu keeps in non-root mode. Sending a
5812 * notification event posts the interrupt to vcpu.
5813 *
5814 * Case 2: vcpu exits to root mode and is still
5815 * runnable. PIR will be synced to vIRR before the
5816 * next vcpu entry. Sending a notification event in
5817 * this case has no effect, as vcpu is not in root
5818 * mode.
5819 *
5820 * Case 3: vcpu exits to root mode and is blocked.
5821 * vcpu_block() has already synced PIR to vIRR and
5822 * never blocks vcpu if vIRR is not cleared. Therefore,
5823 * a blocked vcpu here does not wait for any requested
5824 * interrupts in PIR, and sending a notification event
5825 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005826 */
Feng Wu28b835d2015-09-18 22:29:54 +08005827
Wincy Van06a55242017-04-28 13:13:59 +08005828 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005829 return true;
5830 }
5831#endif
5832 return false;
5833}
5834
Wincy Van705699a2015-02-03 23:58:17 +08005835static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5836 int vector)
5837{
5838 struct vcpu_vmx *vmx = to_vmx(vcpu);
5839
5840 if (is_guest_mode(vcpu) &&
5841 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005842 /*
5843 * If a posted intr is not recognized by hardware,
5844 * we will accomplish it in the next vmentry.
5845 */
5846 vmx->nested.pi_pending = true;
5847 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005848 /* the PIR and ON have been set by L1. */
5849 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5850 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005851 return 0;
5852 }
5853 return -1;
5854}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005855/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005856 * Send interrupt to vcpu via posted interrupt way.
5857 * 1. If target vcpu is running(non-root mode), send posted interrupt
5858 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5859 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5860 * interrupt from PIR in next vmentry.
5861 */
5862static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5863{
5864 struct vcpu_vmx *vmx = to_vmx(vcpu);
5865 int r;
5866
Wincy Van705699a2015-02-03 23:58:17 +08005867 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5868 if (!r)
5869 return;
5870
Yang Zhanga20ed542013-04-11 19:25:15 +08005871 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5872 return;
5873
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005874 /* If a previous notification has sent the IPI, nothing to do. */
5875 if (pi_test_and_set_on(&vmx->pi_desc))
5876 return;
5877
Wincy Van06a55242017-04-28 13:13:59 +08005878 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005879 kvm_vcpu_kick(vcpu);
5880}
5881
Avi Kivity6aa8b732006-12-10 02:21:36 -08005882/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005883 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5884 * will not change in the lifetime of the guest.
5885 * Note that host-state that does change is set elsewhere. E.g., host-state
5886 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5887 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005888static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005889{
5890 u32 low32, high32;
5891 unsigned long tmpl;
5892 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005893 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005894
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005895 cr0 = read_cr0();
5896 WARN_ON(cr0 & X86_CR0_TS);
5897 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005898
5899 /*
5900 * Save the most likely value for this task's CR3 in the VMCS.
5901 * We can't use __get_current_cr3_fast() because we're not atomic.
5902 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005903 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005904 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005905 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005906
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005907 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005908 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005909 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005910 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005911
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005912 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005913#ifdef CONFIG_X86_64
5914 /*
5915 * Load null selectors, so we can avoid reloading them in
5916 * __vmx_load_host_state(), in case userspace uses the null selectors
5917 * too (the expected case).
5918 */
5919 vmcs_write16(HOST_DS_SELECTOR, 0);
5920 vmcs_write16(HOST_ES_SELECTOR, 0);
5921#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005922 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5923 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005924#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005925 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5926 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5927
Juergen Gross87930012017-09-04 12:25:27 +02005928 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005929 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005930 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005931
Avi Kivity83287ea422012-09-16 15:10:57 +03005932 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005933
5934 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5935 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5936 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5937 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5938
5939 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5940 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5941 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5942 }
5943}
5944
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005945static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5946{
5947 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5948 if (enable_ept)
5949 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005950 if (is_guest_mode(&vmx->vcpu))
5951 vmx->vcpu.arch.cr4_guest_owned_bits &=
5952 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005953 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5954}
5955
Yang Zhang01e439b2013-04-11 19:25:12 +08005956static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5957{
5958 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5959
Andrey Smetanind62caab2015-11-10 15:36:33 +03005960 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005961 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005962
5963 if (!enable_vnmi)
5964 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5965
Yunhong Jiang64672c92016-06-13 14:19:59 -07005966 /* Enable the preemption timer dynamically */
5967 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005968 return pin_based_exec_ctrl;
5969}
5970
Andrey Smetanind62caab2015-11-10 15:36:33 +03005971static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5972{
5973 struct vcpu_vmx *vmx = to_vmx(vcpu);
5974
5975 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005976 if (cpu_has_secondary_exec_ctrls()) {
5977 if (kvm_vcpu_apicv_active(vcpu))
5978 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5979 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5980 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5981 else
5982 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5983 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5984 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5985 }
5986
5987 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005988 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005989}
5990
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005991static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5992{
5993 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005994
5995 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5996 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5997
Paolo Bonzini35754c92015-07-29 12:05:37 +02005998 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005999 exec_control &= ~CPU_BASED_TPR_SHADOW;
6000#ifdef CONFIG_X86_64
6001 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6002 CPU_BASED_CR8_LOAD_EXITING;
6003#endif
6004 }
6005 if (!enable_ept)
6006 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6007 CPU_BASED_CR3_LOAD_EXITING |
6008 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006009 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6010 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6011 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006012 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6013 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006014 return exec_control;
6015}
6016
Jim Mattson45ec3682017-08-23 16:32:04 -07006017static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006018{
Jim Mattson45ec3682017-08-23 16:32:04 -07006019 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006020 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006021}
6022
Jim Mattson75f4fc82017-08-23 16:32:03 -07006023static bool vmx_rdseed_supported(void)
6024{
6025 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006026 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006027}
6028
Paolo Bonzini80154d72017-08-24 13:55:35 +02006029static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006030{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006031 struct kvm_vcpu *vcpu = &vmx->vcpu;
6032
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006033 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006034
Paolo Bonzini80154d72017-08-24 13:55:35 +02006035 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006036 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6037 if (vmx->vpid == 0)
6038 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6039 if (!enable_ept) {
6040 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6041 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006042 /* Enable INVPCID for non-ept guests may cause performance regression. */
6043 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006044 }
6045 if (!enable_unrestricted_guest)
6046 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006047 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006048 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006049 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006050 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6051 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006052 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006053
6054 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6055 * in vmx_set_cr4. */
6056 exec_control &= ~SECONDARY_EXEC_DESC;
6057
Abel Gordonabc4fc52013-04-18 14:35:25 +03006058 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6059 (handle_vmptrld).
6060 We can NOT enable shadow_vmcs here because we don't have yet
6061 a current VMCS12
6062 */
6063 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006064
6065 if (!enable_pml)
6066 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006067
Paolo Bonzini3db13482017-08-24 14:48:03 +02006068 if (vmx_xsaves_supported()) {
6069 /* Exposing XSAVES only when XSAVE is exposed */
6070 bool xsaves_enabled =
6071 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6072 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6073
6074 if (!xsaves_enabled)
6075 exec_control &= ~SECONDARY_EXEC_XSAVES;
6076
6077 if (nested) {
6078 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006079 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006080 SECONDARY_EXEC_XSAVES;
6081 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006082 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006083 ~SECONDARY_EXEC_XSAVES;
6084 }
6085 }
6086
Paolo Bonzini80154d72017-08-24 13:55:35 +02006087 if (vmx_rdtscp_supported()) {
6088 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6089 if (!rdtscp_enabled)
6090 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6091
6092 if (nested) {
6093 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006094 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006095 SECONDARY_EXEC_RDTSCP;
6096 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006097 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006098 ~SECONDARY_EXEC_RDTSCP;
6099 }
6100 }
6101
6102 if (vmx_invpcid_supported()) {
6103 /* Exposing INVPCID only when PCID is exposed */
6104 bool invpcid_enabled =
6105 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6106 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6107
6108 if (!invpcid_enabled) {
6109 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6110 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6111 }
6112
6113 if (nested) {
6114 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006115 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006116 SECONDARY_EXEC_ENABLE_INVPCID;
6117 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006118 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006119 ~SECONDARY_EXEC_ENABLE_INVPCID;
6120 }
6121 }
6122
Jim Mattson45ec3682017-08-23 16:32:04 -07006123 if (vmx_rdrand_supported()) {
6124 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6125 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006126 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006127
6128 if (nested) {
6129 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006130 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006131 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006132 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006133 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006134 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006135 }
6136 }
6137
Jim Mattson75f4fc82017-08-23 16:32:03 -07006138 if (vmx_rdseed_supported()) {
6139 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6140 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006141 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006142
6143 if (nested) {
6144 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006145 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006146 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006147 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006148 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006149 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006150 }
6151 }
6152
Paolo Bonzini80154d72017-08-24 13:55:35 +02006153 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006154}
6155
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006156static void ept_set_mmio_spte_mask(void)
6157{
6158 /*
6159 * EPT Misconfigurations can be generated if the value of bits 2:0
6160 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006161 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006162 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6163 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006164}
6165
Wanpeng Lif53cd632014-12-02 19:14:58 +08006166#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006167/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006168 * Sets up the vmcs for emulated real mode.
6169 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006170static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006171{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006172#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006173 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006174#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006175 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006176
Abel Gordon4607c2d2013-04-18 14:35:55 +03006177 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006178 /*
6179 * At vCPU creation, "VMWRITE to any supported field
6180 * in the VMCS" is supported, so use the more
6181 * permissive vmx_vmread_bitmap to specify both read
6182 * and write permissions for the shadow VMCS.
6183 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006184 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006185 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006186 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006187 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006188 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006189
Avi Kivity6aa8b732006-12-10 02:21:36 -08006190 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6191
Avi Kivity6aa8b732006-12-10 02:21:36 -08006192 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006193 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006194 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006195
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006196 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006197
Dan Williamsdfa169b2016-06-02 11:17:24 -07006198 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006199 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006200 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006201 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006202 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006203
Andrey Smetanind62caab2015-11-10 15:36:33 +03006204 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006205 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6206 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6207 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6208 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6209
6210 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006211
Li RongQing0bcf2612015-12-03 13:29:34 +08006212 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006213 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006214 }
6215
Wanpeng Lib31c1142018-03-12 04:53:04 -07006216 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006217 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006218 vmx->ple_window = ple_window;
6219 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006220 }
6221
Xiao Guangrongc3707952011-07-12 03:28:04 +08006222 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6223 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006224 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6225
Avi Kivity9581d442010-10-19 16:46:55 +02006226 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6227 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006228 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006229#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006230 rdmsrl(MSR_FS_BASE, a);
6231 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6232 rdmsrl(MSR_GS_BASE, a);
6233 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6234#else
6235 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6236 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6237#endif
6238
Bandan Das2a499e42017-08-03 15:54:41 -04006239 if (cpu_has_vmx_vmfunc())
6240 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6241
Eddie Dong2cc51562007-05-21 07:28:09 +03006242 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6243 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006244 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006245 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006246 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247
Radim Krčmář74545702015-04-27 15:11:25 +02006248 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6249 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006250
Paolo Bonzini03916db2014-07-24 14:21:57 +02006251 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252 u32 index = vmx_msr_index[i];
6253 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006254 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006255
6256 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6257 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006258 if (wrmsr_safe(index, data_low, data_high) < 0)
6259 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006260 vmx->guest_msrs[j].index = i;
6261 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006262 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006263 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006264 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006265
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006266 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6267 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006268
6269 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006270
6271 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006272 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006273
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006274 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6275 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6276
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006277 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006278
Wanpeng Lif53cd632014-12-02 19:14:58 +08006279 if (vmx_xsaves_supported())
6280 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6281
Peter Feiner4e595162016-07-07 14:49:58 -07006282 if (enable_pml) {
6283 ASSERT(vmx->pml_pg);
6284 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6285 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6286 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006287}
6288
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006289static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006290{
6291 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006292 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006293 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006294
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006295 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006296 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006297
Wanpeng Li518e7b92018-02-28 14:03:31 +08006298 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006299 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006300 kvm_set_cr8(vcpu, 0);
6301
6302 if (!init_event) {
6303 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6304 MSR_IA32_APICBASE_ENABLE;
6305 if (kvm_vcpu_is_reset_bsp(vcpu))
6306 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6307 apic_base_msr.host_initiated = true;
6308 kvm_set_apic_base(vcpu, &apic_base_msr);
6309 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006310
Avi Kivity2fb92db2011-04-27 19:42:18 +03006311 vmx_segment_cache_clear(vmx);
6312
Avi Kivity5706be02008-08-20 15:07:31 +03006313 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006314 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006315 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006316
6317 seg_setup(VCPU_SREG_DS);
6318 seg_setup(VCPU_SREG_ES);
6319 seg_setup(VCPU_SREG_FS);
6320 seg_setup(VCPU_SREG_GS);
6321 seg_setup(VCPU_SREG_SS);
6322
6323 vmcs_write16(GUEST_TR_SELECTOR, 0);
6324 vmcs_writel(GUEST_TR_BASE, 0);
6325 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6326 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6327
6328 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6329 vmcs_writel(GUEST_LDTR_BASE, 0);
6330 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6331 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6332
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006333 if (!init_event) {
6334 vmcs_write32(GUEST_SYSENTER_CS, 0);
6335 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6336 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6337 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6338 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006339
Wanpeng Lic37c2872017-11-20 14:52:21 -08006340 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006341 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006342
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006343 vmcs_writel(GUEST_GDTR_BASE, 0);
6344 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6345
6346 vmcs_writel(GUEST_IDTR_BASE, 0);
6347 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6348
Anthony Liguori443381a2010-12-06 10:53:38 -06006349 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006350 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006351 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006352 if (kvm_mpx_supported())
6353 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006354
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006355 setup_msrs(vmx);
6356
Avi Kivity6aa8b732006-12-10 02:21:36 -08006357 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6358
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006359 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006360 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006361 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006362 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006363 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006364 vmcs_write32(TPR_THRESHOLD, 0);
6365 }
6366
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006367 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368
Sheng Yang2384d2b2008-01-17 15:14:33 +08006369 if (vmx->vpid != 0)
6370 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6371
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006372 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006373 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006374 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006375 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006376 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006377
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006378 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006379
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006380 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006381 if (init_event)
6382 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006383}
6384
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006385/*
6386 * In nested virtualization, check if L1 asked to exit on external interrupts.
6387 * For most existing hypervisors, this will always return true.
6388 */
6389static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6390{
6391 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6392 PIN_BASED_EXT_INTR_MASK;
6393}
6394
Bandan Das77b0f5d2014-04-19 18:17:45 -04006395/*
6396 * In nested virtualization, check if L1 has set
6397 * VM_EXIT_ACK_INTR_ON_EXIT
6398 */
6399static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6400{
6401 return get_vmcs12(vcpu)->vm_exit_controls &
6402 VM_EXIT_ACK_INTR_ON_EXIT;
6403}
6404
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006405static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6406{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006407 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006408}
6409
Jan Kiszkac9a79532014-03-07 20:03:15 +01006410static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006411{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006412 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6413 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006414}
6415
Jan Kiszkac9a79532014-03-07 20:03:15 +01006416static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006417{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006418 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006419 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006420 enable_irq_window(vcpu);
6421 return;
6422 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006423
Paolo Bonzini47c01522016-12-19 11:44:07 +01006424 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6425 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006426}
6427
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006428static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006429{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006430 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006431 uint32_t intr;
6432 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006433
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006434 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006435
Avi Kivityfa89a812008-09-01 15:57:51 +03006436 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006437 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006438 int inc_eip = 0;
6439 if (vcpu->arch.interrupt.soft)
6440 inc_eip = vcpu->arch.event_exit_inst_len;
6441 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006442 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006443 return;
6444 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006445 intr = irq | INTR_INFO_VALID_MASK;
6446 if (vcpu->arch.interrupt.soft) {
6447 intr |= INTR_TYPE_SOFT_INTR;
6448 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6449 vmx->vcpu.arch.event_exit_inst_len);
6450 } else
6451 intr |= INTR_TYPE_EXT_INTR;
6452 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006453
6454 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006455}
6456
Sheng Yangf08864b2008-05-15 18:23:25 +08006457static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6458{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006459 struct vcpu_vmx *vmx = to_vmx(vcpu);
6460
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006461 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006462 /*
6463 * Tracking the NMI-blocked state in software is built upon
6464 * finding the next open IRQ window. This, in turn, depends on
6465 * well-behaving guests: They have to keep IRQs disabled at
6466 * least as long as the NMI handler runs. Otherwise we may
6467 * cause NMI nesting, maybe breaking the guest. But as this is
6468 * highly unlikely, we can live with the residual risk.
6469 */
6470 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6471 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6472 }
6473
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006474 ++vcpu->stat.nmi_injections;
6475 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006476
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006477 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006478 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006479 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006480 return;
6481 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006482
Sheng Yangf08864b2008-05-15 18:23:25 +08006483 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6484 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006485
6486 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006487}
6488
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006489static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6490{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006491 struct vcpu_vmx *vmx = to_vmx(vcpu);
6492 bool masked;
6493
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006494 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006495 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006496 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006497 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006498 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6499 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6500 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006501}
6502
6503static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6504{
6505 struct vcpu_vmx *vmx = to_vmx(vcpu);
6506
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006507 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006508 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6509 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6510 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6511 }
6512 } else {
6513 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6514 if (masked)
6515 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6516 GUEST_INTR_STATE_NMI);
6517 else
6518 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6519 GUEST_INTR_STATE_NMI);
6520 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006521}
6522
Jan Kiszka2505dc92013-04-14 12:12:47 +02006523static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6524{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006525 if (to_vmx(vcpu)->nested.nested_run_pending)
6526 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006527
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006528 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006529 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6530 return 0;
6531
Jan Kiszka2505dc92013-04-14 12:12:47 +02006532 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6533 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6534 | GUEST_INTR_STATE_NMI));
6535}
6536
Gleb Natapov78646122009-03-23 12:12:11 +02006537static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6538{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006539 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6540 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006541 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6542 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006543}
6544
Izik Eiduscbc94022007-10-25 00:29:55 +02006545static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6546{
6547 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006548
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006549 if (enable_unrestricted_guest)
6550 return 0;
6551
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006552 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6553 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006554 if (ret)
6555 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006556 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006557 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006558}
6559
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006560static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6561{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006562 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006563 return 0;
6564}
6565
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006566static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006567{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006568 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006569 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006570 /*
6571 * Update instruction length as we may reinject the exception
6572 * from user space while in guest debugging mode.
6573 */
6574 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6575 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006576 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006577 return false;
6578 /* fall through */
6579 case DB_VECTOR:
6580 if (vcpu->guest_debug &
6581 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6582 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006583 /* fall through */
6584 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006585 case OF_VECTOR:
6586 case BR_VECTOR:
6587 case UD_VECTOR:
6588 case DF_VECTOR:
6589 case SS_VECTOR:
6590 case GP_VECTOR:
6591 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006592 return true;
6593 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006594 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006595 return false;
6596}
6597
6598static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6599 int vec, u32 err_code)
6600{
6601 /*
6602 * Instruction with address size override prefix opcode 0x67
6603 * Cause the #SS fault with 0 error code in VM86 mode.
6604 */
6605 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6606 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6607 if (vcpu->arch.halt_request) {
6608 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006609 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006610 }
6611 return 1;
6612 }
6613 return 0;
6614 }
6615
6616 /*
6617 * Forward all other exceptions that are valid in real mode.
6618 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6619 * the required debugging infrastructure rework.
6620 */
6621 kvm_queue_exception(vcpu, vec);
6622 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006623}
6624
Andi Kleena0861c02009-06-08 17:37:09 +08006625/*
6626 * Trigger machine check on the host. We assume all the MSRs are already set up
6627 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6628 * We pass a fake environment to the machine check handler because we want
6629 * the guest to be always treated like user space, no matter what context
6630 * it used internally.
6631 */
6632static void kvm_machine_check(void)
6633{
6634#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6635 struct pt_regs regs = {
6636 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6637 .flags = X86_EFLAGS_IF,
6638 };
6639
6640 do_machine_check(&regs, 0);
6641#endif
6642}
6643
Avi Kivity851ba692009-08-24 11:10:17 +03006644static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006645{
6646 /* already handled by vcpu_run */
6647 return 1;
6648}
6649
Avi Kivity851ba692009-08-24 11:10:17 +03006650static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006651{
Avi Kivity1155f762007-11-22 11:30:47 +02006652 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006653 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006654 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006655 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006656 u32 vect_info;
6657 enum emulation_result er;
6658
Avi Kivity1155f762007-11-22 11:30:47 +02006659 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006660 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006661
Andi Kleena0861c02009-06-08 17:37:09 +08006662 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006663 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006664
Jim Mattsonef85b672016-12-12 11:01:37 -08006665 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006666 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006667
Wanpeng Li082d06e2018-04-03 16:28:48 -07006668 if (is_invalid_opcode(intr_info))
6669 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006670
Avi Kivity6aa8b732006-12-10 02:21:36 -08006671 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006672 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006673 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006674
Liran Alon9e869482018-03-12 13:12:51 +02006675 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6676 WARN_ON_ONCE(!enable_vmware_backdoor);
6677 er = emulate_instruction(vcpu,
6678 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6679 if (er == EMULATE_USER_EXIT)
6680 return 0;
6681 else if (er != EMULATE_DONE)
6682 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6683 return 1;
6684 }
6685
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006686 /*
6687 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6688 * MMIO, it is better to report an internal error.
6689 * See the comments in vmx_handle_exit.
6690 */
6691 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6692 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6693 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6694 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006695 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006696 vcpu->run->internal.data[0] = vect_info;
6697 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006698 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006699 return 0;
6700 }
6701
Avi Kivity6aa8b732006-12-10 02:21:36 -08006702 if (is_page_fault(intr_info)) {
6703 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006704 /* EPT won't cause page fault directly */
6705 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006706 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006707 }
6708
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006709 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006710
6711 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6712 return handle_rmode_exception(vcpu, ex_no, error_code);
6713
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006714 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006715 case AC_VECTOR:
6716 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6717 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006718 case DB_VECTOR:
6719 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6720 if (!(vcpu->guest_debug &
6721 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006722 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006723 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006724 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006725 skip_emulated_instruction(vcpu);
6726
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006727 kvm_queue_exception(vcpu, DB_VECTOR);
6728 return 1;
6729 }
6730 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6731 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6732 /* fall through */
6733 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006734 /*
6735 * Update instruction length as we may reinject #BP from
6736 * user space while in guest debugging mode. Reading it for
6737 * #DB as well causes no harm, it is not used in that case.
6738 */
6739 vmx->vcpu.arch.event_exit_inst_len =
6740 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006741 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006742 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006743 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6744 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006745 break;
6746 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006747 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6748 kvm_run->ex.exception = ex_no;
6749 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006750 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006752 return 0;
6753}
6754
Avi Kivity851ba692009-08-24 11:10:17 +03006755static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006756{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006757 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006758 return 1;
6759}
6760
Avi Kivity851ba692009-08-24 11:10:17 +03006761static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006762{
Avi Kivity851ba692009-08-24 11:10:17 +03006763 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006764 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006765 return 0;
6766}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006767
Avi Kivity851ba692009-08-24 11:10:17 +03006768static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006769{
He, Qingbfdaab02007-09-12 14:18:28 +08006770 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006771 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006772 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006773
He, Qingbfdaab02007-09-12 14:18:28 +08006774 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006775 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006776
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006777 ++vcpu->stat.io_exits;
6778
Sean Christopherson432baf62018-03-08 08:57:26 -08006779 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006780 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006781
6782 port = exit_qualification >> 16;
6783 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006784 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006785
Sean Christophersondca7f122018-03-08 08:57:27 -08006786 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006787}
6788
Ingo Molnar102d8322007-02-19 14:37:47 +02006789static void
6790vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6791{
6792 /*
6793 * Patch in the VMCALL instruction:
6794 */
6795 hypercall[0] = 0x0f;
6796 hypercall[1] = 0x01;
6797 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006798}
6799
Guo Chao0fa06072012-06-28 15:16:19 +08006800/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006801static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6802{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006803 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006804 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6805 unsigned long orig_val = val;
6806
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006807 /*
6808 * We get here when L2 changed cr0 in a way that did not change
6809 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006810 * but did change L0 shadowed bits. So we first calculate the
6811 * effective cr0 value that L1 would like to write into the
6812 * hardware. It consists of the L2-owned bits from the new
6813 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006814 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006815 val = (val & ~vmcs12->cr0_guest_host_mask) |
6816 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6817
David Matlack38991522016-11-29 18:14:08 -08006818 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006819 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006820
6821 if (kvm_set_cr0(vcpu, val))
6822 return 1;
6823 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006824 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006825 } else {
6826 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006827 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006828 return 1;
David Matlack38991522016-11-29 18:14:08 -08006829
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006830 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006831 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006832}
6833
6834static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6835{
6836 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006837 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6838 unsigned long orig_val = val;
6839
6840 /* analogously to handle_set_cr0 */
6841 val = (val & ~vmcs12->cr4_guest_host_mask) |
6842 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6843 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006844 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006845 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006846 return 0;
6847 } else
6848 return kvm_set_cr4(vcpu, val);
6849}
6850
Paolo Bonzini0367f202016-07-12 10:44:55 +02006851static int handle_desc(struct kvm_vcpu *vcpu)
6852{
6853 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6854 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6855}
6856
Avi Kivity851ba692009-08-24 11:10:17 +03006857static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006858{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006859 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006860 int cr;
6861 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006862 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006863 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006864
He, Qingbfdaab02007-09-12 14:18:28 +08006865 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006866 cr = exit_qualification & 15;
6867 reg = (exit_qualification >> 8) & 15;
6868 switch ((exit_qualification >> 4) & 3) {
6869 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006870 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006871 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006872 switch (cr) {
6873 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006874 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006875 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006876 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006877 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006878 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006879 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006880 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006881 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006882 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006883 case 8: {
6884 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006885 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006886 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006887 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006888 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006889 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006890 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006891 return ret;
6892 /*
6893 * TODO: we might be squashing a
6894 * KVM_GUESTDBG_SINGLESTEP-triggered
6895 * KVM_EXIT_DEBUG here.
6896 */
Avi Kivity851ba692009-08-24 11:10:17 +03006897 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006898 return 0;
6899 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006900 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006902 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006903 WARN_ONCE(1, "Guest should always own CR0.TS");
6904 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006905 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006906 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006907 case 1: /*mov from cr*/
6908 switch (cr) {
6909 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006910 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006911 val = kvm_read_cr3(vcpu);
6912 kvm_register_write(vcpu, reg, val);
6913 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006914 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006915 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006916 val = kvm_get_cr8(vcpu);
6917 kvm_register_write(vcpu, reg, val);
6918 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006919 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006920 }
6921 break;
6922 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006923 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006924 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006925 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006926
Kyle Huey6affcbe2016-11-29 12:40:40 -08006927 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006928 default:
6929 break;
6930 }
Avi Kivity851ba692009-08-24 11:10:17 +03006931 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006932 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006933 (int)(exit_qualification >> 4) & 3, cr);
6934 return 0;
6935}
6936
Avi Kivity851ba692009-08-24 11:10:17 +03006937static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006938{
He, Qingbfdaab02007-09-12 14:18:28 +08006939 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006940 int dr, dr7, reg;
6941
6942 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6943 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6944
6945 /* First, if DR does not exist, trigger UD */
6946 if (!kvm_require_dr(vcpu, dr))
6947 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006948
Jan Kiszkaf2483412010-01-20 18:20:20 +01006949 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006950 if (!kvm_require_cpl(vcpu, 0))
6951 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006952 dr7 = vmcs_readl(GUEST_DR7);
6953 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006954 /*
6955 * As the vm-exit takes precedence over the debug trap, we
6956 * need to emulate the latter, either for the host or the
6957 * guest debugging itself.
6958 */
6959 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006960 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006961 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006962 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006963 vcpu->run->debug.arch.exception = DB_VECTOR;
6964 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006965 return 0;
6966 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006967 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006968 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006969 kvm_queue_exception(vcpu, DB_VECTOR);
6970 return 1;
6971 }
6972 }
6973
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006974 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006975 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6976 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006977
6978 /*
6979 * No more DR vmexits; force a reload of the debug registers
6980 * and reenter on this instruction. The next vmexit will
6981 * retrieve the full state of the debug registers.
6982 */
6983 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6984 return 1;
6985 }
6986
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006987 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6988 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006989 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006990
6991 if (kvm_get_dr(vcpu, dr, &val))
6992 return 1;
6993 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006994 } else
Nadav Amit57773922014-06-18 17:19:23 +03006995 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006996 return 1;
6997
Kyle Huey6affcbe2016-11-29 12:40:40 -08006998 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006999}
7000
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007001static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7002{
7003 return vcpu->arch.dr6;
7004}
7005
7006static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7007{
7008}
7009
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007010static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7011{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007012 get_debugreg(vcpu->arch.db[0], 0);
7013 get_debugreg(vcpu->arch.db[1], 1);
7014 get_debugreg(vcpu->arch.db[2], 2);
7015 get_debugreg(vcpu->arch.db[3], 3);
7016 get_debugreg(vcpu->arch.dr6, 6);
7017 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7018
7019 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007020 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007021}
7022
Gleb Natapov020df072010-04-13 10:05:23 +03007023static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7024{
7025 vmcs_writel(GUEST_DR7, val);
7026}
7027
Avi Kivity851ba692009-08-24 11:10:17 +03007028static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007029{
Kyle Huey6a908b62016-11-29 12:40:37 -08007030 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007031}
7032
Avi Kivity851ba692009-08-24 11:10:17 +03007033static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007034{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007035 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007036 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007037
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007038 msr_info.index = ecx;
7039 msr_info.host_initiated = false;
7040 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007041 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007042 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007043 return 1;
7044 }
7045
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007046 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007047
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007049 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7050 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007051 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052}
7053
Avi Kivity851ba692009-08-24 11:10:17 +03007054static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007055{
Will Auld8fe8ab42012-11-29 12:42:12 -08007056 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007057 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7058 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7059 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007060
Will Auld8fe8ab42012-11-29 12:42:12 -08007061 msr.data = data;
7062 msr.index = ecx;
7063 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007064 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007065 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007066 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007067 return 1;
7068 }
7069
Avi Kivity59200272010-01-25 19:47:02 +02007070 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007071 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007072}
7073
Avi Kivity851ba692009-08-24 11:10:17 +03007074static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007075{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007076 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007077 return 1;
7078}
7079
Avi Kivity851ba692009-08-24 11:10:17 +03007080static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007081{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007082 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7083 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007084
Avi Kivity3842d132010-07-27 12:30:24 +03007085 kvm_make_request(KVM_REQ_EVENT, vcpu);
7086
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007087 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007088 return 1;
7089}
7090
Avi Kivity851ba692009-08-24 11:10:17 +03007091static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092{
Avi Kivityd3bef152007-06-05 15:53:05 +03007093 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007094}
7095
Avi Kivity851ba692009-08-24 11:10:17 +03007096static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007097{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007098 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007099}
7100
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007101static int handle_invd(struct kvm_vcpu *vcpu)
7102{
Andre Przywara51d8b662010-12-21 11:12:02 +01007103 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007104}
7105
Avi Kivity851ba692009-08-24 11:10:17 +03007106static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007107{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007108 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007109
7110 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007111 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007112}
7113
Avi Kivityfee84b02011-11-10 14:57:25 +02007114static int handle_rdpmc(struct kvm_vcpu *vcpu)
7115{
7116 int err;
7117
7118 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007119 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007120}
7121
Avi Kivity851ba692009-08-24 11:10:17 +03007122static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007123{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007124 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007125}
7126
Dexuan Cui2acf9232010-06-10 11:27:12 +08007127static int handle_xsetbv(struct kvm_vcpu *vcpu)
7128{
7129 u64 new_bv = kvm_read_edx_eax(vcpu);
7130 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7131
7132 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007133 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007134 return 1;
7135}
7136
Wanpeng Lif53cd632014-12-02 19:14:58 +08007137static int handle_xsaves(struct kvm_vcpu *vcpu)
7138{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007139 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007140 WARN(1, "this should never happen\n");
7141 return 1;
7142}
7143
7144static int handle_xrstors(struct kvm_vcpu *vcpu)
7145{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007146 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007147 WARN(1, "this should never happen\n");
7148 return 1;
7149}
7150
Avi Kivity851ba692009-08-24 11:10:17 +03007151static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007152{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007153 if (likely(fasteoi)) {
7154 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7155 int access_type, offset;
7156
7157 access_type = exit_qualification & APIC_ACCESS_TYPE;
7158 offset = exit_qualification & APIC_ACCESS_OFFSET;
7159 /*
7160 * Sane guest uses MOV to write EOI, with written value
7161 * not cared. So make a short-circuit here by avoiding
7162 * heavy instruction emulation.
7163 */
7164 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7165 (offset == APIC_EOI)) {
7166 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007167 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007168 }
7169 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007170 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007171}
7172
Yang Zhangc7c9c562013-01-25 10:18:51 +08007173static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7174{
7175 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7176 int vector = exit_qualification & 0xff;
7177
7178 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7179 kvm_apic_set_eoi_accelerated(vcpu, vector);
7180 return 1;
7181}
7182
Yang Zhang83d4c282013-01-25 10:18:49 +08007183static int handle_apic_write(struct kvm_vcpu *vcpu)
7184{
7185 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7186 u32 offset = exit_qualification & 0xfff;
7187
7188 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7189 kvm_apic_write_nodecode(vcpu, offset);
7190 return 1;
7191}
7192
Avi Kivity851ba692009-08-24 11:10:17 +03007193static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007194{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007196 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007197 bool has_error_code = false;
7198 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007199 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007200 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007201
7202 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007203 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007204 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007205
7206 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7207
7208 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007209 if (reason == TASK_SWITCH_GATE && idt_v) {
7210 switch (type) {
7211 case INTR_TYPE_NMI_INTR:
7212 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007213 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007214 break;
7215 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007216 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007217 kvm_clear_interrupt_queue(vcpu);
7218 break;
7219 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007220 if (vmx->idt_vectoring_info &
7221 VECTORING_INFO_DELIVER_CODE_MASK) {
7222 has_error_code = true;
7223 error_code =
7224 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7225 }
7226 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007227 case INTR_TYPE_SOFT_EXCEPTION:
7228 kvm_clear_exception_queue(vcpu);
7229 break;
7230 default:
7231 break;
7232 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007233 }
Izik Eidus37817f22008-03-24 23:14:53 +02007234 tss_selector = exit_qualification;
7235
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007236 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7237 type != INTR_TYPE_EXT_INTR &&
7238 type != INTR_TYPE_NMI_INTR))
7239 skip_emulated_instruction(vcpu);
7240
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007241 if (kvm_task_switch(vcpu, tss_selector,
7242 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7243 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007244 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7245 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7246 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007247 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007248 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007249
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007250 /*
7251 * TODO: What about debug traps on tss switch?
7252 * Are we supposed to inject them and update dr6?
7253 */
7254
7255 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007256}
7257
Avi Kivity851ba692009-08-24 11:10:17 +03007258static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007259{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007260 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007261 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007262 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007263
Sheng Yangf9c617f2009-03-25 10:08:52 +08007264 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007265
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007266 /*
7267 * EPT violation happened while executing iret from NMI,
7268 * "blocked by NMI" bit has to be set before next VM entry.
7269 * There are errata that may cause this bit to not be set:
7270 * AAK134, BY25.
7271 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007272 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007273 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007274 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007275 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7276
Sheng Yang14394422008-04-28 12:24:45 +08007277 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007278 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007279
Junaid Shahid27959a42016-12-06 16:46:10 -08007280 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007281 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007282 ? PFERR_USER_MASK : 0;
7283 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007284 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007285 ? PFERR_WRITE_MASK : 0;
7286 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007287 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007288 ? PFERR_FETCH_MASK : 0;
7289 /* ept page table entry is present? */
7290 error_code |= (exit_qualification &
7291 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7292 EPT_VIOLATION_EXECUTABLE))
7293 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007294
Paolo Bonzinieebed242016-11-28 14:39:58 +01007295 error_code |= (exit_qualification & 0x100) != 0 ?
7296 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007297
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007298 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007299 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007300}
7301
Avi Kivity851ba692009-08-24 11:10:17 +03007302static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007303{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007304 gpa_t gpa;
7305
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007306 /*
7307 * A nested guest cannot optimize MMIO vmexits, because we have an
7308 * nGPA here instead of the required GPA.
7309 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007310 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007311 if (!is_guest_mode(vcpu) &&
7312 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007313 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007314 /*
7315 * Doing kvm_skip_emulated_instruction() depends on undefined
7316 * behavior: Intel's manual doesn't mandate
7317 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7318 * occurs and while on real hardware it was observed to be set,
7319 * other hypervisors (namely Hyper-V) don't set it, we end up
7320 * advancing IP with some random value. Disable fast mmio when
7321 * running nested and keep it for real hardware in hope that
7322 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7323 */
7324 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7325 return kvm_skip_emulated_instruction(vcpu);
7326 else
7327 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7328 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007329 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007330
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007331 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007332}
7333
Avi Kivity851ba692009-08-24 11:10:17 +03007334static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007335{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007336 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007337 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7338 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007339 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007340 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007341
7342 return 1;
7343}
7344
Mohammed Gamal80ced182009-09-01 12:48:18 +02007345static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007346{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007347 struct vcpu_vmx *vmx = to_vmx(vcpu);
7348 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007349 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007350 u32 cpu_exec_ctrl;
7351 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007352 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007353
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007354 /*
7355 * We should never reach the point where we are emulating L2
7356 * due to invalid guest state as that means we incorrectly
7357 * allowed a nested VMEntry with an invalid vmcs12.
7358 */
7359 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7360
Avi Kivity49e9d552010-09-19 14:34:08 +02007361 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7362 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007363
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007364 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007365 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007366 return handle_interrupt_window(&vmx->vcpu);
7367
Radim Krčmář72875d82017-04-26 22:32:19 +02007368 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007369 return 1;
7370
Liran Alon9b8ae632017-11-05 16:56:34 +02007371 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007372
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007373 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007374 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007375 ret = 0;
7376 goto out;
7377 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007378
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007379 if (err != EMULATE_DONE)
7380 goto emulation_error;
7381
7382 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7383 vcpu->arch.exception.pending)
7384 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007385
Gleb Natapov8d76c492013-05-08 18:38:44 +03007386 if (vcpu->arch.halt_request) {
7387 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007388 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007389 goto out;
7390 }
7391
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007392 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007393 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007394 if (need_resched())
7395 schedule();
7396 }
7397
Mohammed Gamal80ced182009-09-01 12:48:18 +02007398out:
7399 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007400
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007401emulation_error:
7402 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7403 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7404 vcpu->run->internal.ndata = 0;
7405 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007406}
7407
7408static void grow_ple_window(struct kvm_vcpu *vcpu)
7409{
7410 struct vcpu_vmx *vmx = to_vmx(vcpu);
7411 int old = vmx->ple_window;
7412
Babu Mogerc8e88712018-03-16 16:37:24 -04007413 vmx->ple_window = __grow_ple_window(old, ple_window,
7414 ple_window_grow,
7415 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007416
7417 if (vmx->ple_window != old)
7418 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007419
7420 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007421}
7422
7423static void shrink_ple_window(struct kvm_vcpu *vcpu)
7424{
7425 struct vcpu_vmx *vmx = to_vmx(vcpu);
7426 int old = vmx->ple_window;
7427
Babu Mogerc8e88712018-03-16 16:37:24 -04007428 vmx->ple_window = __shrink_ple_window(old, ple_window,
7429 ple_window_shrink,
7430 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007431
7432 if (vmx->ple_window != old)
7433 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007434
7435 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007436}
7437
7438/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007439 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7440 */
7441static void wakeup_handler(void)
7442{
7443 struct kvm_vcpu *vcpu;
7444 int cpu = smp_processor_id();
7445
7446 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7447 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7448 blocked_vcpu_list) {
7449 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7450
7451 if (pi_test_on(pi_desc) == 1)
7452 kvm_vcpu_kick(vcpu);
7453 }
7454 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7455}
7456
Peng Haoe01bca22018-04-07 05:47:32 +08007457static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007458{
7459 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7460 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7461 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7462 0ull, VMX_EPT_EXECUTABLE_MASK,
7463 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007464 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007465
7466 ept_set_mmio_spte_mask();
7467 kvm_enable_tdp();
7468}
7469
Tiejun Chenf2c76482014-10-28 10:14:47 +08007470static __init int hardware_setup(void)
7471{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007472 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007473
7474 rdmsrl_safe(MSR_EFER, &host_efer);
7475
7476 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7477 kvm_define_shared_msr(i, vmx_msr_index[i]);
7478
Radim Krčmář23611332016-09-29 22:41:33 +02007479 for (i = 0; i < VMX_BITMAP_NR; i++) {
7480 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7481 if (!vmx_bitmap[i])
7482 goto out;
7483 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007484
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007485 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7486 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7487
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007488 if (setup_vmcs_config(&vmcs_config) < 0) {
7489 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007490 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007491 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007492
7493 if (boot_cpu_has(X86_FEATURE_NX))
7494 kvm_enable_efer_bits(EFER_NX);
7495
Wanpeng Li08d839c2017-03-23 05:30:08 -07007496 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7497 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007498 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007499
Tiejun Chenf2c76482014-10-28 10:14:47 +08007500 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007501 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007502 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007503 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007504 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007505
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007506 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007507 enable_ept_ad_bits = 0;
7508
Wanpeng Li8ad81822017-10-09 15:51:53 -07007509 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007510 enable_unrestricted_guest = 0;
7511
Paolo Bonziniad15a292015-01-30 16:18:49 +01007512 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007513 flexpriority_enabled = 0;
7514
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007515 if (!cpu_has_virtual_nmis())
7516 enable_vnmi = 0;
7517
Paolo Bonziniad15a292015-01-30 16:18:49 +01007518 /*
7519 * set_apic_access_page_addr() is used to reload apic access
7520 * page upon invalidation. No need to do anything if not
7521 * using the APIC_ACCESS_ADDR VMCS field.
7522 */
7523 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007524 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007525
7526 if (!cpu_has_vmx_tpr_shadow())
7527 kvm_x86_ops->update_cr8_intercept = NULL;
7528
7529 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7530 kvm_disable_largepages();
7531
Wanpeng Li0f107682017-09-28 18:06:24 -07007532 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007533 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007534 ple_window = 0;
7535 ple_window_grow = 0;
7536 ple_window_max = 0;
7537 ple_window_shrink = 0;
7538 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007539
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007540 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007541 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007542 kvm_x86_ops->sync_pir_to_irr = NULL;
7543 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007544
Haozhong Zhang64903d62015-10-20 15:39:09 +08007545 if (cpu_has_vmx_tsc_scaling()) {
7546 kvm_has_tsc_control = true;
7547 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7548 kvm_tsc_scaling_ratio_frac_bits = 48;
7549 }
7550
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007551 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7552
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007553 if (enable_ept)
7554 vmx_enable_tdp();
7555 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007556 kvm_disable_tdp();
7557
Kai Huang843e4332015-01-28 10:54:28 +08007558 /*
7559 * Only enable PML when hardware supports PML feature, and both EPT
7560 * and EPT A/D bit features are enabled -- PML depends on them to work.
7561 */
7562 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7563 enable_pml = 0;
7564
7565 if (!enable_pml) {
7566 kvm_x86_ops->slot_enable_log_dirty = NULL;
7567 kvm_x86_ops->slot_disable_log_dirty = NULL;
7568 kvm_x86_ops->flush_log_dirty = NULL;
7569 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7570 }
7571
Yunhong Jiang64672c92016-06-13 14:19:59 -07007572 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7573 u64 vmx_msr;
7574
7575 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7576 cpu_preemption_timer_multi =
7577 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7578 } else {
7579 kvm_x86_ops->set_hv_timer = NULL;
7580 kvm_x86_ops->cancel_hv_timer = NULL;
7581 }
7582
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007583 if (!cpu_has_vmx_shadow_vmcs())
7584 enable_shadow_vmcs = 0;
7585 if (enable_shadow_vmcs)
7586 init_vmcs_shadow_fields();
7587
Feng Wubf9f6ac2015-09-18 22:29:55 +08007588 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007589 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007590
Ashok Rajc45dcc72016-06-22 14:59:56 +08007591 kvm_mce_cap_supported |= MCG_LMCE_P;
7592
Tiejun Chenf2c76482014-10-28 10:14:47 +08007593 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007594
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007595out:
Radim Krčmář23611332016-09-29 22:41:33 +02007596 for (i = 0; i < VMX_BITMAP_NR; i++)
7597 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007598
7599 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600}
7601
7602static __exit void hardware_unsetup(void)
7603{
Radim Krčmář23611332016-09-29 22:41:33 +02007604 int i;
7605
7606 for (i = 0; i < VMX_BITMAP_NR; i++)
7607 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007608
Tiejun Chenf2c76482014-10-28 10:14:47 +08007609 free_kvm_area();
7610}
7611
Avi Kivity6aa8b732006-12-10 02:21:36 -08007612/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007613 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7614 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7615 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007616static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007617{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007618 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007619 grow_ple_window(vcpu);
7620
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007621 /*
7622 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7623 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7624 * never set PAUSE_EXITING and just set PLE if supported,
7625 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7626 */
7627 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007628 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007629}
7630
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007631static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007632{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007633 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007634}
7635
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007636static int handle_mwait(struct kvm_vcpu *vcpu)
7637{
7638 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7639 return handle_nop(vcpu);
7640}
7641
Jim Mattson45ec3682017-08-23 16:32:04 -07007642static int handle_invalid_op(struct kvm_vcpu *vcpu)
7643{
7644 kvm_queue_exception(vcpu, UD_VECTOR);
7645 return 1;
7646}
7647
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007648static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7649{
7650 return 1;
7651}
7652
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007653static int handle_monitor(struct kvm_vcpu *vcpu)
7654{
7655 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7656 return handle_nop(vcpu);
7657}
7658
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007659/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007660 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7661 * set the success or error code of an emulated VMX instruction, as specified
7662 * by Vol 2B, VMX Instruction Reference, "Conventions".
7663 */
7664static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7665{
7666 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7667 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7668 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7669}
7670
7671static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7672{
7673 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7674 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7675 X86_EFLAGS_SF | X86_EFLAGS_OF))
7676 | X86_EFLAGS_CF);
7677}
7678
Abel Gordon145c28d2013-04-18 14:36:55 +03007679static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007680 u32 vm_instruction_error)
7681{
7682 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7683 /*
7684 * failValid writes the error number to the current VMCS, which
7685 * can't be done there isn't a current VMCS.
7686 */
7687 nested_vmx_failInvalid(vcpu);
7688 return;
7689 }
7690 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7691 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7692 X86_EFLAGS_SF | X86_EFLAGS_OF))
7693 | X86_EFLAGS_ZF);
7694 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7695 /*
7696 * We don't need to force a shadow sync because
7697 * VM_INSTRUCTION_ERROR is not shadowed
7698 */
7699}
Abel Gordon145c28d2013-04-18 14:36:55 +03007700
Wincy Vanff651cb2014-12-11 08:52:58 +03007701static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7702{
7703 /* TODO: not to reset guest simply here. */
7704 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007705 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007706}
7707
Jan Kiszkaf41245002014-03-07 20:03:13 +01007708static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7709{
7710 struct vcpu_vmx *vmx =
7711 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7712
7713 vmx->nested.preemption_timer_expired = true;
7714 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7715 kvm_vcpu_kick(&vmx->vcpu);
7716
7717 return HRTIMER_NORESTART;
7718}
7719
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007720/*
Bandan Das19677e32014-05-06 02:19:15 -04007721 * Decode the memory-address operand of a vmx instruction, as recorded on an
7722 * exit caused by such an instruction (run by a guest hypervisor).
7723 * On success, returns 0. When the operand is invalid, returns 1 and throws
7724 * #UD or #GP.
7725 */
7726static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7727 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007728 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007729{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007730 gva_t off;
7731 bool exn;
7732 struct kvm_segment s;
7733
Bandan Das19677e32014-05-06 02:19:15 -04007734 /*
7735 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7736 * Execution", on an exit, vmx_instruction_info holds most of the
7737 * addressing components of the operand. Only the displacement part
7738 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7739 * For how an actual address is calculated from all these components,
7740 * refer to Vol. 1, "Operand Addressing".
7741 */
7742 int scaling = vmx_instruction_info & 3;
7743 int addr_size = (vmx_instruction_info >> 7) & 7;
7744 bool is_reg = vmx_instruction_info & (1u << 10);
7745 int seg_reg = (vmx_instruction_info >> 15) & 7;
7746 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7747 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7748 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7749 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7750
7751 if (is_reg) {
7752 kvm_queue_exception(vcpu, UD_VECTOR);
7753 return 1;
7754 }
7755
7756 /* Addr = segment_base + offset */
7757 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007758 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007759 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007760 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007761 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007762 off += kvm_register_read(vcpu, index_reg)<<scaling;
7763 vmx_get_segment(vcpu, &s, seg_reg);
7764 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007765
7766 if (addr_size == 1) /* 32 bit */
7767 *ret &= 0xffffffff;
7768
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007769 /* Checks for #GP/#SS exceptions. */
7770 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007771 if (is_long_mode(vcpu)) {
7772 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7773 * non-canonical form. This is the only check on the memory
7774 * destination for long mode!
7775 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007776 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007777 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007778 /* Protected mode: apply checks for segment validity in the
7779 * following order:
7780 * - segment type check (#GP(0) may be thrown)
7781 * - usability check (#GP(0)/#SS(0))
7782 * - limit check (#GP(0)/#SS(0))
7783 */
7784 if (wr)
7785 /* #GP(0) if the destination operand is located in a
7786 * read-only data segment or any code segment.
7787 */
7788 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7789 else
7790 /* #GP(0) if the source operand is located in an
7791 * execute-only code segment
7792 */
7793 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007794 if (exn) {
7795 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7796 return 1;
7797 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007798 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7799 */
7800 exn = (s.unusable != 0);
7801 /* Protected mode: #GP(0)/#SS(0) if the memory
7802 * operand is outside the segment limit.
7803 */
7804 exn = exn || (off + sizeof(u64) > s.limit);
7805 }
7806 if (exn) {
7807 kvm_queue_exception_e(vcpu,
7808 seg_reg == VCPU_SREG_SS ?
7809 SS_VECTOR : GP_VECTOR,
7810 0);
7811 return 1;
7812 }
7813
Bandan Das19677e32014-05-06 02:19:15 -04007814 return 0;
7815}
7816
Radim Krčmářcbf71272017-05-19 15:48:51 +02007817static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007818{
7819 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007820 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007821
7822 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007823 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007824 return 1;
7825
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007826 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007827 kvm_inject_page_fault(vcpu, &e);
7828 return 1;
7829 }
7830
Bandan Das3573e222014-05-06 02:19:16 -04007831 return 0;
7832}
7833
Jim Mattsone29acc52016-11-30 12:03:43 -08007834static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7835{
7836 struct vcpu_vmx *vmx = to_vmx(vcpu);
7837 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007838 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007839
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007840 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7841 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007842 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007843
7844 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7845 if (!vmx->nested.cached_vmcs12)
7846 goto out_cached_vmcs12;
7847
7848 if (enable_shadow_vmcs) {
7849 shadow_vmcs = alloc_vmcs();
7850 if (!shadow_vmcs)
7851 goto out_shadow_vmcs;
7852 /* mark vmcs as shadow */
7853 shadow_vmcs->revision_id |= (1u << 31);
7854 /* init shadow vmcs */
7855 vmcs_clear(shadow_vmcs);
7856 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7857 }
7858
Jim Mattsone29acc52016-11-30 12:03:43 -08007859 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7860 HRTIMER_MODE_REL_PINNED);
7861 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7862
7863 vmx->nested.vmxon = true;
7864 return 0;
7865
7866out_shadow_vmcs:
7867 kfree(vmx->nested.cached_vmcs12);
7868
7869out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007870 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007871
Jim Mattsonde3a0022017-11-27 17:22:25 -06007872out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007873 return -ENOMEM;
7874}
7875
Bandan Das3573e222014-05-06 02:19:16 -04007876/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007877 * Emulate the VMXON instruction.
7878 * Currently, we just remember that VMX is active, and do not save or even
7879 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7880 * do not currently need to store anything in that guest-allocated memory
7881 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7882 * argument is different from the VMXON pointer (which the spec says they do).
7883 */
7884static int handle_vmon(struct kvm_vcpu *vcpu)
7885{
Jim Mattsone29acc52016-11-30 12:03:43 -08007886 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007887 gpa_t vmptr;
7888 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007889 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007890 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7891 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007892
Jim Mattson70f3aac2017-04-26 08:53:46 -07007893 /*
7894 * The Intel VMX Instruction Reference lists a bunch of bits that are
7895 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7896 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7897 * Otherwise, we should fail with #UD. But most faulting conditions
7898 * have already been checked by hardware, prior to the VM-exit for
7899 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7900 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007901 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007902 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007903 kvm_queue_exception(vcpu, UD_VECTOR);
7904 return 1;
7905 }
7906
Felix Wilhelm727ba742018-06-11 09:43:44 +02007907 /* CPL=0 must be checked manually. */
7908 if (vmx_get_cpl(vcpu)) {
7909 kvm_queue_exception(vcpu, UD_VECTOR);
7910 return 1;
7911 }
7912
Abel Gordon145c28d2013-04-18 14:36:55 +03007913 if (vmx->nested.vmxon) {
7914 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007915 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007916 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007917
Haozhong Zhang3b840802016-06-22 14:59:54 +08007918 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007919 != VMXON_NEEDED_FEATURES) {
7920 kvm_inject_gp(vcpu, 0);
7921 return 1;
7922 }
7923
Radim Krčmářcbf71272017-05-19 15:48:51 +02007924 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007925 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007926
7927 /*
7928 * SDM 3: 24.11.5
7929 * The first 4 bytes of VMXON region contain the supported
7930 * VMCS revision identifier
7931 *
7932 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7933 * which replaces physical address width with 32
7934 */
7935 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7936 nested_vmx_failInvalid(vcpu);
7937 return kvm_skip_emulated_instruction(vcpu);
7938 }
7939
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007940 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7941 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007942 nested_vmx_failInvalid(vcpu);
7943 return kvm_skip_emulated_instruction(vcpu);
7944 }
7945 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7946 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007947 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007948 nested_vmx_failInvalid(vcpu);
7949 return kvm_skip_emulated_instruction(vcpu);
7950 }
7951 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007952 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007953
7954 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007955 ret = enter_vmx_operation(vcpu);
7956 if (ret)
7957 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007958
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007959 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007960 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007961}
7962
7963/*
7964 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7965 * for running VMX instructions (except VMXON, whose prerequisites are
7966 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007967 * Note that many of these exceptions have priority over VM exits, so they
7968 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007969 */
7970static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7971{
Felix Wilhelm727ba742018-06-11 09:43:44 +02007972 if (vmx_get_cpl(vcpu)) {
7973 kvm_queue_exception(vcpu, UD_VECTOR);
7974 return 0;
7975 }
7976
Jim Mattson70f3aac2017-04-26 08:53:46 -07007977 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007978 kvm_queue_exception(vcpu, UD_VECTOR);
7979 return 0;
7980 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007981 return 1;
7982}
7983
David Matlack8ca44e82017-08-01 14:00:39 -07007984static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7985{
7986 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7987 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7988}
7989
Abel Gordone7953d72013-04-18 14:37:55 +03007990static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7991{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007992 if (vmx->nested.current_vmptr == -1ull)
7993 return;
7994
Abel Gordon012f83c2013-04-18 14:39:25 +03007995 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007996 /* copy to memory all shadowed fields in case
7997 they were modified */
7998 copy_shadow_to_vmcs12(vmx);
7999 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008000 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008001 }
Wincy Van705699a2015-02-03 23:58:17 +08008002 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008003
8004 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008005 kvm_vcpu_write_guest_page(&vmx->vcpu,
8006 vmx->nested.current_vmptr >> PAGE_SHIFT,
8007 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008008
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008009 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008010}
8011
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008012/*
8013 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8014 * just stops using VMX.
8015 */
8016static void free_nested(struct vcpu_vmx *vmx)
8017{
Wanpeng Lib7455822017-11-22 14:04:00 -08008018 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008019 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008020
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008021 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008022 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008023 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008024 vmx->nested.posted_intr_nv = -1;
8025 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008026 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008027 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008028 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8029 free_vmcs(vmx->vmcs01.shadow_vmcs);
8030 vmx->vmcs01.shadow_vmcs = NULL;
8031 }
David Matlack4f2777b2016-07-13 17:16:37 -07008032 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008033 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008034 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008035 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008036 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008037 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008038 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008039 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008040 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008041 }
Wincy Van705699a2015-02-03 23:58:17 +08008042 if (vmx->nested.pi_desc_page) {
8043 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008044 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008045 vmx->nested.pi_desc_page = NULL;
8046 vmx->nested.pi_desc = NULL;
8047 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008048
Jim Mattsonde3a0022017-11-27 17:22:25 -06008049 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008050}
8051
8052/* Emulate the VMXOFF instruction */
8053static int handle_vmoff(struct kvm_vcpu *vcpu)
8054{
8055 if (!nested_vmx_check_permission(vcpu))
8056 return 1;
8057 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008058 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008059 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008060}
8061
Nadav Har'El27d6c862011-05-25 23:06:59 +03008062/* Emulate the VMCLEAR instruction */
8063static int handle_vmclear(struct kvm_vcpu *vcpu)
8064{
8065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008066 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008067 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008068
8069 if (!nested_vmx_check_permission(vcpu))
8070 return 1;
8071
Radim Krčmářcbf71272017-05-19 15:48:51 +02008072 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008073 return 1;
8074
Radim Krčmářcbf71272017-05-19 15:48:51 +02008075 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8076 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8077 return kvm_skip_emulated_instruction(vcpu);
8078 }
8079
8080 if (vmptr == vmx->nested.vmxon_ptr) {
8081 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8082 return kvm_skip_emulated_instruction(vcpu);
8083 }
8084
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008085 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008086 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008087
Jim Mattson587d7e722017-03-02 12:41:48 -08008088 kvm_vcpu_write_guest(vcpu,
8089 vmptr + offsetof(struct vmcs12, launch_state),
8090 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008091
Nadav Har'El27d6c862011-05-25 23:06:59 +03008092 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008093 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008094}
8095
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008096static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8097
8098/* Emulate the VMLAUNCH instruction */
8099static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8100{
8101 return nested_vmx_run(vcpu, true);
8102}
8103
8104/* Emulate the VMRESUME instruction */
8105static int handle_vmresume(struct kvm_vcpu *vcpu)
8106{
8107
8108 return nested_vmx_run(vcpu, false);
8109}
8110
Nadav Har'El49f705c2011-05-25 23:08:30 +03008111/*
8112 * Read a vmcs12 field. Since these can have varying lengths and we return
8113 * one type, we chose the biggest type (u64) and zero-extend the return value
8114 * to that size. Note that the caller, handle_vmread, might need to use only
8115 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8116 * 64-bit fields are to be returned).
8117 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008118static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8119 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008120{
8121 short offset = vmcs_field_to_offset(field);
8122 char *p;
8123
8124 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008125 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008126
8127 p = ((char *)(get_vmcs12(vcpu))) + offset;
8128
Jim Mattsond37f4262017-12-22 12:12:16 -08008129 switch (vmcs_field_width(field)) {
8130 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008131 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008132 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008133 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008134 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008135 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008136 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008137 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008138 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008139 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008140 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008141 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008142 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008143 WARN_ON(1);
8144 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008145 }
8146}
8147
Abel Gordon20b97fe2013-04-18 14:36:25 +03008148
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008149static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8150 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008151 short offset = vmcs_field_to_offset(field);
8152 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8153 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008154 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008155
Jim Mattsond37f4262017-12-22 12:12:16 -08008156 switch (vmcs_field_width(field)) {
8157 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008158 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008159 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008160 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008161 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008162 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008163 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008164 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008165 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008166 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008167 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008168 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008169 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008170 WARN_ON(1);
8171 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008172 }
8173
8174}
8175
Jim Mattsonf4160e42018-05-29 09:11:33 -07008176/*
8177 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8178 * they have been modified by the L1 guest. Note that the "read-only"
8179 * VM-exit information fields are actually writable if the vCPU is
8180 * configured to support "VMWRITE to any supported field in the VMCS."
8181 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008182static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8183{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008184 const u16 *fields[] = {
8185 shadow_read_write_fields,
8186 shadow_read_only_fields
8187 };
8188 const int max_fields[] = {
8189 max_shadow_read_write_fields,
8190 max_shadow_read_only_fields
8191 };
8192 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008193 unsigned long field;
8194 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008195 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008196
Jan Kiszka282da872014-10-08 18:05:39 +02008197 preempt_disable();
8198
Abel Gordon16f5b902013-04-18 14:38:25 +03008199 vmcs_load(shadow_vmcs);
8200
Jim Mattsonf4160e42018-05-29 09:11:33 -07008201 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8202 for (i = 0; i < max_fields[q]; i++) {
8203 field = fields[q][i];
8204 field_value = __vmcs_readl(field);
8205 vmcs12_write_any(&vmx->vcpu, field, field_value);
8206 }
8207 /*
8208 * Skip the VM-exit information fields if they are read-only.
8209 */
8210 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8211 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008212 }
8213
8214 vmcs_clear(shadow_vmcs);
8215 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008216
8217 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008218}
8219
Abel Gordonc3114422013-04-18 14:38:55 +03008220static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8221{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008222 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008223 shadow_read_write_fields,
8224 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008225 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008226 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008227 max_shadow_read_write_fields,
8228 max_shadow_read_only_fields
8229 };
8230 int i, q;
8231 unsigned long field;
8232 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008233 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008234
8235 vmcs_load(shadow_vmcs);
8236
Mathias Krausec2bae892013-06-26 20:36:21 +02008237 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008238 for (i = 0; i < max_fields[q]; i++) {
8239 field = fields[q][i];
8240 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008241 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008242 }
8243 }
8244
8245 vmcs_clear(shadow_vmcs);
8246 vmcs_load(vmx->loaded_vmcs->vmcs);
8247}
8248
Nadav Har'El49f705c2011-05-25 23:08:30 +03008249/*
8250 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8251 * used before) all generate the same failure when it is missing.
8252 */
8253static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8254{
8255 struct vcpu_vmx *vmx = to_vmx(vcpu);
8256 if (vmx->nested.current_vmptr == -1ull) {
8257 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008258 return 0;
8259 }
8260 return 1;
8261}
8262
8263static int handle_vmread(struct kvm_vcpu *vcpu)
8264{
8265 unsigned long field;
8266 u64 field_value;
8267 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8268 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8269 gva_t gva = 0;
8270
Kyle Hueyeb277562016-11-29 12:40:39 -08008271 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008272 return 1;
8273
Kyle Huey6affcbe2016-11-29 12:40:40 -08008274 if (!nested_vmx_check_vmcs12(vcpu))
8275 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008276
Nadav Har'El49f705c2011-05-25 23:08:30 +03008277 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008278 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008279 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008280 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008281 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008282 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008283 }
8284 /*
8285 * Now copy part of this value to register or memory, as requested.
8286 * Note that the number of bits actually copied is 32 or 64 depending
8287 * on the guest's mode (32 or 64 bit), not on the given field's length.
8288 */
8289 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008290 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008291 field_value);
8292 } else {
8293 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008294 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008295 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008296 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008297 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8298 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008299 }
8300
8301 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008302 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008303}
8304
8305
8306static int handle_vmwrite(struct kvm_vcpu *vcpu)
8307{
8308 unsigned long field;
8309 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008311 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8312 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008313
Nadav Har'El49f705c2011-05-25 23:08:30 +03008314 /* The value to write might be 32 or 64 bits, depending on L1's long
8315 * mode, and eventually we need to write that into a field of several
8316 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008317 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008318 * bits into the vmcs12 field.
8319 */
8320 u64 field_value = 0;
8321 struct x86_exception e;
8322
Kyle Hueyeb277562016-11-29 12:40:39 -08008323 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008324 return 1;
8325
Kyle Huey6affcbe2016-11-29 12:40:40 -08008326 if (!nested_vmx_check_vmcs12(vcpu))
8327 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008328
Nadav Har'El49f705c2011-05-25 23:08:30 +03008329 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008330 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008331 (((vmx_instruction_info) >> 3) & 0xf));
8332 else {
8333 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008334 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008335 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008336 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8337 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008338 kvm_inject_page_fault(vcpu, &e);
8339 return 1;
8340 }
8341 }
8342
8343
Nadav Amit27e6fb52014-06-18 17:19:26 +03008344 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008345 /*
8346 * If the vCPU supports "VMWRITE to any supported field in the
8347 * VMCS," then the "read-only" fields are actually read/write.
8348 */
8349 if (vmcs_field_readonly(field) &&
8350 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008351 nested_vmx_failValid(vcpu,
8352 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008353 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008354 }
8355
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008356 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008357 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008358 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008359 }
8360
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008361 switch (field) {
8362#define SHADOW_FIELD_RW(x) case x:
8363#include "vmx_shadow_fields.h"
8364 /*
8365 * The fields that can be updated by L1 without a vmexit are
8366 * always updated in the vmcs02, the others go down the slow
8367 * path of prepare_vmcs02.
8368 */
8369 break;
8370 default:
8371 vmx->nested.dirty_vmcs12 = true;
8372 break;
8373 }
8374
Nadav Har'El49f705c2011-05-25 23:08:30 +03008375 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008376 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008377}
8378
Jim Mattsona8bc2842016-11-30 12:03:44 -08008379static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8380{
8381 vmx->nested.current_vmptr = vmptr;
8382 if (enable_shadow_vmcs) {
8383 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8384 SECONDARY_EXEC_SHADOW_VMCS);
8385 vmcs_write64(VMCS_LINK_POINTER,
8386 __pa(vmx->vmcs01.shadow_vmcs));
8387 vmx->nested.sync_shadow_vmcs = true;
8388 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008389 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008390}
8391
Nadav Har'El63846662011-05-25 23:07:29 +03008392/* Emulate the VMPTRLD instruction */
8393static int handle_vmptrld(struct kvm_vcpu *vcpu)
8394{
8395 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008396 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008397
8398 if (!nested_vmx_check_permission(vcpu))
8399 return 1;
8400
Radim Krčmářcbf71272017-05-19 15:48:51 +02008401 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008402 return 1;
8403
Radim Krčmářcbf71272017-05-19 15:48:51 +02008404 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8405 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8406 return kvm_skip_emulated_instruction(vcpu);
8407 }
8408
8409 if (vmptr == vmx->nested.vmxon_ptr) {
8410 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8411 return kvm_skip_emulated_instruction(vcpu);
8412 }
8413
Nadav Har'El63846662011-05-25 23:07:29 +03008414 if (vmx->nested.current_vmptr != vmptr) {
8415 struct vmcs12 *new_vmcs12;
8416 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008417 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8418 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008419 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008420 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008421 }
8422 new_vmcs12 = kmap(page);
8423 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8424 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008425 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008426 nested_vmx_failValid(vcpu,
8427 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008428 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008429 }
Nadav Har'El63846662011-05-25 23:07:29 +03008430
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008431 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008432 /*
8433 * Load VMCS12 from guest memory since it is not already
8434 * cached.
8435 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008436 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8437 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008438 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008439
Jim Mattsona8bc2842016-11-30 12:03:44 -08008440 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008441 }
8442
8443 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008444 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008445}
8446
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008447/* Emulate the VMPTRST instruction */
8448static int handle_vmptrst(struct kvm_vcpu *vcpu)
8449{
8450 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8451 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8452 gva_t vmcs_gva;
8453 struct x86_exception e;
8454
8455 if (!nested_vmx_check_permission(vcpu))
8456 return 1;
8457
8458 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008459 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008460 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008461 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008462 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8463 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8464 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008465 kvm_inject_page_fault(vcpu, &e);
8466 return 1;
8467 }
8468 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008469 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008470}
8471
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008472/* Emulate the INVEPT instruction */
8473static int handle_invept(struct kvm_vcpu *vcpu)
8474{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008475 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008476 u32 vmx_instruction_info, types;
8477 unsigned long type;
8478 gva_t gva;
8479 struct x86_exception e;
8480 struct {
8481 u64 eptp, gpa;
8482 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008483
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008484 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008485 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008486 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008487 kvm_queue_exception(vcpu, UD_VECTOR);
8488 return 1;
8489 }
8490
8491 if (!nested_vmx_check_permission(vcpu))
8492 return 1;
8493
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008494 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008495 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008496
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008497 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008498
Jim Mattson85c856b2016-10-26 08:38:38 -07008499 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008500 nested_vmx_failValid(vcpu,
8501 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008502 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008503 }
8504
8505 /* According to the Intel VMX instruction reference, the memory
8506 * operand is read even if it isn't needed (e.g., for type==global)
8507 */
8508 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008509 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008510 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008511 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008512 kvm_inject_page_fault(vcpu, &e);
8513 return 1;
8514 }
8515
8516 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008517 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008518 /*
8519 * TODO: track mappings and invalidate
8520 * single context requests appropriately
8521 */
8522 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008523 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008524 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008525 nested_vmx_succeed(vcpu);
8526 break;
8527 default:
8528 BUG_ON(1);
8529 break;
8530 }
8531
Kyle Huey6affcbe2016-11-29 12:40:40 -08008532 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008533}
8534
Petr Matouseka642fc32014-09-23 20:22:30 +02008535static int handle_invvpid(struct kvm_vcpu *vcpu)
8536{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008537 struct vcpu_vmx *vmx = to_vmx(vcpu);
8538 u32 vmx_instruction_info;
8539 unsigned long type, types;
8540 gva_t gva;
8541 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008542 struct {
8543 u64 vpid;
8544 u64 gla;
8545 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008546
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008547 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008548 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008549 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008550 kvm_queue_exception(vcpu, UD_VECTOR);
8551 return 1;
8552 }
8553
8554 if (!nested_vmx_check_permission(vcpu))
8555 return 1;
8556
8557 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8558 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8559
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008560 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008561 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008562
Jim Mattson85c856b2016-10-26 08:38:38 -07008563 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008564 nested_vmx_failValid(vcpu,
8565 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008566 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008567 }
8568
8569 /* according to the intel vmx instruction reference, the memory
8570 * operand is read even if it isn't needed (e.g., for type==global)
8571 */
8572 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8573 vmx_instruction_info, false, &gva))
8574 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008575 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008576 kvm_inject_page_fault(vcpu, &e);
8577 return 1;
8578 }
Jim Mattson40352602017-06-28 09:37:37 -07008579 if (operand.vpid >> 16) {
8580 nested_vmx_failValid(vcpu,
8581 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8582 return kvm_skip_emulated_instruction(vcpu);
8583 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008584
8585 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008586 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008587 if (!operand.vpid ||
8588 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008589 nested_vmx_failValid(vcpu,
8590 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8591 return kvm_skip_emulated_instruction(vcpu);
8592 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008593 if (cpu_has_vmx_invvpid_individual_addr() &&
8594 vmx->nested.vpid02) {
8595 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8596 vmx->nested.vpid02, operand.gla);
8597 } else
8598 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8599 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008600 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008601 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008602 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008603 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);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008606 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008607 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008608 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008609 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008610 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008611 break;
8612 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008613 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008614 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008615 }
8616
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008617 nested_vmx_succeed(vcpu);
8618
Kyle Huey6affcbe2016-11-29 12:40:40 -08008619 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008620}
8621
Kai Huang843e4332015-01-28 10:54:28 +08008622static int handle_pml_full(struct kvm_vcpu *vcpu)
8623{
8624 unsigned long exit_qualification;
8625
8626 trace_kvm_pml_full(vcpu->vcpu_id);
8627
8628 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8629
8630 /*
8631 * PML buffer FULL happened while executing iret from NMI,
8632 * "blocked by NMI" bit has to be set before next VM entry.
8633 */
8634 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008635 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008636 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8637 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8638 GUEST_INTR_STATE_NMI);
8639
8640 /*
8641 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8642 * here.., and there's no userspace involvement needed for PML.
8643 */
8644 return 1;
8645}
8646
Yunhong Jiang64672c92016-06-13 14:19:59 -07008647static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8648{
8649 kvm_lapic_expired_hv_timer(vcpu);
8650 return 1;
8651}
8652
Bandan Das41ab9372017-08-03 15:54:43 -04008653static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8654{
8655 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008656 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8657
8658 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008659 switch (address & VMX_EPTP_MT_MASK) {
8660 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008661 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008662 return false;
8663 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008664 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008665 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008666 return false;
8667 break;
8668 default:
8669 return false;
8670 }
8671
David Hildenbrandbb97a012017-08-10 23:15:28 +02008672 /* only 4 levels page-walk length are valid */
8673 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008674 return false;
8675
8676 /* Reserved bits should not be set */
8677 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8678 return false;
8679
8680 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008681 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008682 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008683 return false;
8684 }
8685
8686 return true;
8687}
8688
8689static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8690 struct vmcs12 *vmcs12)
8691{
8692 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8693 u64 address;
8694 bool accessed_dirty;
8695 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8696
8697 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8698 !nested_cpu_has_ept(vmcs12))
8699 return 1;
8700
8701 if (index >= VMFUNC_EPTP_ENTRIES)
8702 return 1;
8703
8704
8705 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8706 &address, index * 8, 8))
8707 return 1;
8708
David Hildenbrandbb97a012017-08-10 23:15:28 +02008709 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008710
8711 /*
8712 * If the (L2) guest does a vmfunc to the currently
8713 * active ept pointer, we don't have to do anything else
8714 */
8715 if (vmcs12->ept_pointer != address) {
8716 if (!valid_ept_address(vcpu, address))
8717 return 1;
8718
8719 kvm_mmu_unload(vcpu);
8720 mmu->ept_ad = accessed_dirty;
8721 mmu->base_role.ad_disabled = !accessed_dirty;
8722 vmcs12->ept_pointer = address;
8723 /*
8724 * TODO: Check what's the correct approach in case
8725 * mmu reload fails. Currently, we just let the next
8726 * reload potentially fail
8727 */
8728 kvm_mmu_reload(vcpu);
8729 }
8730
8731 return 0;
8732}
8733
Bandan Das2a499e42017-08-03 15:54:41 -04008734static int handle_vmfunc(struct kvm_vcpu *vcpu)
8735{
Bandan Das27c42a12017-08-03 15:54:42 -04008736 struct vcpu_vmx *vmx = to_vmx(vcpu);
8737 struct vmcs12 *vmcs12;
8738 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8739
8740 /*
8741 * VMFUNC is only supported for nested guests, but we always enable the
8742 * secondary control for simplicity; for non-nested mode, fake that we
8743 * didn't by injecting #UD.
8744 */
8745 if (!is_guest_mode(vcpu)) {
8746 kvm_queue_exception(vcpu, UD_VECTOR);
8747 return 1;
8748 }
8749
8750 vmcs12 = get_vmcs12(vcpu);
8751 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8752 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008753
8754 switch (function) {
8755 case 0:
8756 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8757 goto fail;
8758 break;
8759 default:
8760 goto fail;
8761 }
8762 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008763
8764fail:
8765 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8766 vmcs_read32(VM_EXIT_INTR_INFO),
8767 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008768 return 1;
8769}
8770
Nadav Har'El0140cae2011-05-25 23:06:28 +03008771/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008772 * The exit handlers return 1 if the exit was handled fully and guest execution
8773 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8774 * to be done to userspace and return 0.
8775 */
Mathias Krause772e0312012-08-30 01:30:19 +02008776static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008777 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8778 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008779 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008780 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008781 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008782 [EXIT_REASON_CR_ACCESS] = handle_cr,
8783 [EXIT_REASON_DR_ACCESS] = handle_dr,
8784 [EXIT_REASON_CPUID] = handle_cpuid,
8785 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8786 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8787 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8788 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008789 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008790 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008791 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008792 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008793 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008794 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008795 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008796 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008797 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008798 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008799 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008800 [EXIT_REASON_VMOFF] = handle_vmoff,
8801 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008802 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8803 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008804 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008805 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008806 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008807 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008808 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008809 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008810 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8811 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008812 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8813 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008814 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008815 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008816 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008817 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008818 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008819 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008820 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008821 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008822 [EXIT_REASON_XSAVES] = handle_xsaves,
8823 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008824 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008825 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008826 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008827};
8828
8829static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008830 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008831
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008832static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8833 struct vmcs12 *vmcs12)
8834{
8835 unsigned long exit_qualification;
8836 gpa_t bitmap, last_bitmap;
8837 unsigned int port;
8838 int size;
8839 u8 b;
8840
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008841 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008842 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008843
8844 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8845
8846 port = exit_qualification >> 16;
8847 size = (exit_qualification & 7) + 1;
8848
8849 last_bitmap = (gpa_t)-1;
8850 b = -1;
8851
8852 while (size > 0) {
8853 if (port < 0x8000)
8854 bitmap = vmcs12->io_bitmap_a;
8855 else if (port < 0x10000)
8856 bitmap = vmcs12->io_bitmap_b;
8857 else
Joe Perches1d804d02015-03-30 16:46:09 -07008858 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008859 bitmap += (port & 0x7fff) / 8;
8860
8861 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008862 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008863 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008864 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008865 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008866
8867 port++;
8868 size--;
8869 last_bitmap = bitmap;
8870 }
8871
Joe Perches1d804d02015-03-30 16:46:09 -07008872 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008873}
8874
Nadav Har'El644d7112011-05-25 23:12:35 +03008875/*
8876 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8877 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8878 * disinterest in the current event (read or write a specific MSR) by using an
8879 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8880 */
8881static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8882 struct vmcs12 *vmcs12, u32 exit_reason)
8883{
8884 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8885 gpa_t bitmap;
8886
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008887 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008888 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008889
8890 /*
8891 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8892 * for the four combinations of read/write and low/high MSR numbers.
8893 * First we need to figure out which of the four to use:
8894 */
8895 bitmap = vmcs12->msr_bitmap;
8896 if (exit_reason == EXIT_REASON_MSR_WRITE)
8897 bitmap += 2048;
8898 if (msr_index >= 0xc0000000) {
8899 msr_index -= 0xc0000000;
8900 bitmap += 1024;
8901 }
8902
8903 /* Then read the msr_index'th bit from this bitmap: */
8904 if (msr_index < 1024*8) {
8905 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008906 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008907 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008908 return 1 & (b >> (msr_index & 7));
8909 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008910 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008911}
8912
8913/*
8914 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8915 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8916 * intercept (via guest_host_mask etc.) the current event.
8917 */
8918static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8919 struct vmcs12 *vmcs12)
8920{
8921 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8922 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008923 int reg;
8924 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008925
8926 switch ((exit_qualification >> 4) & 3) {
8927 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008928 reg = (exit_qualification >> 8) & 15;
8929 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008930 switch (cr) {
8931 case 0:
8932 if (vmcs12->cr0_guest_host_mask &
8933 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008934 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008935 break;
8936 case 3:
8937 if ((vmcs12->cr3_target_count >= 1 &&
8938 vmcs12->cr3_target_value0 == val) ||
8939 (vmcs12->cr3_target_count >= 2 &&
8940 vmcs12->cr3_target_value1 == val) ||
8941 (vmcs12->cr3_target_count >= 3 &&
8942 vmcs12->cr3_target_value2 == val) ||
8943 (vmcs12->cr3_target_count >= 4 &&
8944 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008945 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008946 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008947 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008948 break;
8949 case 4:
8950 if (vmcs12->cr4_guest_host_mask &
8951 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008952 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008953 break;
8954 case 8:
8955 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008956 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008957 break;
8958 }
8959 break;
8960 case 2: /* clts */
8961 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8962 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008963 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008964 break;
8965 case 1: /* mov from cr */
8966 switch (cr) {
8967 case 3:
8968 if (vmcs12->cpu_based_vm_exec_control &
8969 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008970 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008971 break;
8972 case 8:
8973 if (vmcs12->cpu_based_vm_exec_control &
8974 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008975 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008976 break;
8977 }
8978 break;
8979 case 3: /* lmsw */
8980 /*
8981 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8982 * cr0. Other attempted changes are ignored, with no exit.
8983 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008984 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008985 if (vmcs12->cr0_guest_host_mask & 0xe &
8986 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008987 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008988 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8989 !(vmcs12->cr0_read_shadow & 0x1) &&
8990 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008991 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008992 break;
8993 }
Joe Perches1d804d02015-03-30 16:46:09 -07008994 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008995}
8996
8997/*
8998 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8999 * should handle it ourselves in L0 (and then continue L2). Only call this
9000 * when in is_guest_mode (L2).
9001 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009002static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009003{
Nadav Har'El644d7112011-05-25 23:12:35 +03009004 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9005 struct vcpu_vmx *vmx = to_vmx(vcpu);
9006 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9007
Jim Mattson4f350c62017-09-14 16:31:44 -07009008 if (vmx->nested.nested_run_pending)
9009 return false;
9010
9011 if (unlikely(vmx->fail)) {
9012 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9013 vmcs_read32(VM_INSTRUCTION_ERROR));
9014 return true;
9015 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009016
David Matlackc9f04402017-08-01 14:00:40 -07009017 /*
9018 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009019 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9020 * Page). The CPU may write to these pages via their host
9021 * physical address while L2 is running, bypassing any
9022 * address-translation-based dirty tracking (e.g. EPT write
9023 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009024 *
9025 * Mark them dirty on every exit from L2 to prevent them from
9026 * getting out of sync with dirty tracking.
9027 */
9028 nested_mark_vmcs12_pages_dirty(vcpu);
9029
Jim Mattson4f350c62017-09-14 16:31:44 -07009030 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9031 vmcs_readl(EXIT_QUALIFICATION),
9032 vmx->idt_vectoring_info,
9033 intr_info,
9034 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9035 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009036
9037 switch (exit_reason) {
9038 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009039 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009040 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009041 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009042 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009043 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009044 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009045 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009046 else if (is_debug(intr_info) &&
9047 vcpu->guest_debug &
9048 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9049 return false;
9050 else if (is_breakpoint(intr_info) &&
9051 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9052 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009053 return vmcs12->exception_bitmap &
9054 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9055 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009056 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009057 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009058 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009059 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009060 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009061 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009062 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009063 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009064 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009065 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009066 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009067 case EXIT_REASON_HLT:
9068 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9069 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009070 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009071 case EXIT_REASON_INVLPG:
9072 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9073 case EXIT_REASON_RDPMC:
9074 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009075 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009076 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009077 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009078 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009079 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009080 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9081 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9082 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9083 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9084 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9085 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009086 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009087 /*
9088 * VMX instructions trap unconditionally. This allows L1 to
9089 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9090 */
Joe Perches1d804d02015-03-30 16:46:09 -07009091 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009092 case EXIT_REASON_CR_ACCESS:
9093 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9094 case EXIT_REASON_DR_ACCESS:
9095 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9096 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009097 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009098 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9099 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009100 case EXIT_REASON_MSR_READ:
9101 case EXIT_REASON_MSR_WRITE:
9102 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9103 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009104 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009105 case EXIT_REASON_MWAIT_INSTRUCTION:
9106 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009107 case EXIT_REASON_MONITOR_TRAP_FLAG:
9108 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009109 case EXIT_REASON_MONITOR_INSTRUCTION:
9110 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9111 case EXIT_REASON_PAUSE_INSTRUCTION:
9112 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9113 nested_cpu_has2(vmcs12,
9114 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9115 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009116 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009117 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009118 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009119 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009120 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009121 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009122 /*
9123 * The controls for "virtualize APIC accesses," "APIC-
9124 * register virtualization," and "virtual-interrupt
9125 * delivery" only come from vmcs12.
9126 */
Joe Perches1d804d02015-03-30 16:46:09 -07009127 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009128 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009129 /*
9130 * L0 always deals with the EPT violation. If nested EPT is
9131 * used, and the nested mmu code discovers that the address is
9132 * missing in the guest EPT table (EPT12), the EPT violation
9133 * will be injected with nested_ept_inject_page_fault()
9134 */
Joe Perches1d804d02015-03-30 16:46:09 -07009135 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009136 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009137 /*
9138 * L2 never uses directly L1's EPT, but rather L0's own EPT
9139 * table (shadow on EPT) or a merged EPT table that L0 built
9140 * (EPT on EPT). So any problems with the structure of the
9141 * table is L0's fault.
9142 */
Joe Perches1d804d02015-03-30 16:46:09 -07009143 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009144 case EXIT_REASON_INVPCID:
9145 return
9146 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9147 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009148 case EXIT_REASON_WBINVD:
9149 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9150 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009151 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009152 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9153 /*
9154 * This should never happen, since it is not possible to
9155 * set XSS to a non-zero value---neither in L1 nor in L2.
9156 * If if it were, XSS would have to be checked against
9157 * the XSS exit bitmap in vmcs12.
9158 */
9159 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009160 case EXIT_REASON_PREEMPTION_TIMER:
9161 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009162 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009163 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009164 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009165 case EXIT_REASON_VMFUNC:
9166 /* VM functions are emulated through L2->L0 vmexits. */
9167 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009168 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009169 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009170 }
9171}
9172
Paolo Bonzini7313c692017-07-27 10:31:25 +02009173static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9174{
9175 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9176
9177 /*
9178 * At this point, the exit interruption info in exit_intr_info
9179 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9180 * we need to query the in-kernel LAPIC.
9181 */
9182 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9183 if ((exit_intr_info &
9184 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9185 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9186 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9187 vmcs12->vm_exit_intr_error_code =
9188 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9189 }
9190
9191 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9192 vmcs_readl(EXIT_QUALIFICATION));
9193 return 1;
9194}
9195
Avi Kivity586f9602010-11-18 13:09:54 +02009196static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9197{
9198 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9199 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9200}
9201
Kai Huanga3eaa862015-11-04 13:46:05 +08009202static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009203{
Kai Huanga3eaa862015-11-04 13:46:05 +08009204 if (vmx->pml_pg) {
9205 __free_page(vmx->pml_pg);
9206 vmx->pml_pg = NULL;
9207 }
Kai Huang843e4332015-01-28 10:54:28 +08009208}
9209
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009210static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009211{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009212 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009213 u64 *pml_buf;
9214 u16 pml_idx;
9215
9216 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9217
9218 /* Do nothing if PML buffer is empty */
9219 if (pml_idx == (PML_ENTITY_NUM - 1))
9220 return;
9221
9222 /* PML index always points to next available PML buffer entity */
9223 if (pml_idx >= PML_ENTITY_NUM)
9224 pml_idx = 0;
9225 else
9226 pml_idx++;
9227
9228 pml_buf = page_address(vmx->pml_pg);
9229 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9230 u64 gpa;
9231
9232 gpa = pml_buf[pml_idx];
9233 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009234 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009235 }
9236
9237 /* reset PML index */
9238 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9239}
9240
9241/*
9242 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9243 * Called before reporting dirty_bitmap to userspace.
9244 */
9245static void kvm_flush_pml_buffers(struct kvm *kvm)
9246{
9247 int i;
9248 struct kvm_vcpu *vcpu;
9249 /*
9250 * We only need to kick vcpu out of guest mode here, as PML buffer
9251 * is flushed at beginning of all VMEXITs, and it's obvious that only
9252 * vcpus running in guest are possible to have unflushed GPAs in PML
9253 * buffer.
9254 */
9255 kvm_for_each_vcpu(i, vcpu, kvm)
9256 kvm_vcpu_kick(vcpu);
9257}
9258
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009259static void vmx_dump_sel(char *name, uint32_t sel)
9260{
9261 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009262 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009263 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9264 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9265 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9266}
9267
9268static void vmx_dump_dtsel(char *name, uint32_t limit)
9269{
9270 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9271 name, vmcs_read32(limit),
9272 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9273}
9274
9275static void dump_vmcs(void)
9276{
9277 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9278 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9279 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9280 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9281 u32 secondary_exec_control = 0;
9282 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009283 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009284 int i, n;
9285
9286 if (cpu_has_secondary_exec_ctrls())
9287 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9288
9289 pr_err("*** Guest State ***\n");
9290 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9291 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9292 vmcs_readl(CR0_GUEST_HOST_MASK));
9293 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9294 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9295 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9296 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9297 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9298 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009299 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9300 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9301 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9302 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009303 }
9304 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9305 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9306 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9307 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9308 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9309 vmcs_readl(GUEST_SYSENTER_ESP),
9310 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9311 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9312 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9313 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9314 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9315 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9316 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9317 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9318 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9319 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9320 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9321 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9322 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009323 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9324 efer, vmcs_read64(GUEST_IA32_PAT));
9325 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9326 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009327 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009328 if (cpu_has_load_perf_global_ctrl &&
9329 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009330 pr_err("PerfGlobCtl = 0x%016llx\n",
9331 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009332 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009333 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009334 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9335 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9336 vmcs_read32(GUEST_ACTIVITY_STATE));
9337 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9338 pr_err("InterruptStatus = %04x\n",
9339 vmcs_read16(GUEST_INTR_STATUS));
9340
9341 pr_err("*** Host State ***\n");
9342 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9343 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9344 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9345 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9346 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9347 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9348 vmcs_read16(HOST_TR_SELECTOR));
9349 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9350 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9351 vmcs_readl(HOST_TR_BASE));
9352 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9353 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9354 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9355 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9356 vmcs_readl(HOST_CR4));
9357 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9358 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9359 vmcs_read32(HOST_IA32_SYSENTER_CS),
9360 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9361 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009362 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9363 vmcs_read64(HOST_IA32_EFER),
9364 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009365 if (cpu_has_load_perf_global_ctrl &&
9366 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009367 pr_err("PerfGlobCtl = 0x%016llx\n",
9368 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009369
9370 pr_err("*** Control State ***\n");
9371 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9372 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9373 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9374 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9375 vmcs_read32(EXCEPTION_BITMAP),
9376 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9377 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9378 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9379 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9380 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9381 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9382 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9383 vmcs_read32(VM_EXIT_INTR_INFO),
9384 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9385 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9386 pr_err(" reason=%08x qualification=%016lx\n",
9387 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9388 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9389 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9390 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009391 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009392 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009393 pr_err("TSC Multiplier = 0x%016llx\n",
9394 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009395 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9396 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9397 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9398 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9399 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009400 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009401 n = vmcs_read32(CR3_TARGET_COUNT);
9402 for (i = 0; i + 1 < n; i += 4)
9403 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9404 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9405 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9406 if (i < n)
9407 pr_err("CR3 target%u=%016lx\n",
9408 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9409 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9410 pr_err("PLE Gap=%08x Window=%08x\n",
9411 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9412 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9413 pr_err("Virtual processor ID = 0x%04x\n",
9414 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9415}
9416
Avi Kivity6aa8b732006-12-10 02:21:36 -08009417/*
9418 * The guest has exited. See if we can fix it or if we need userspace
9419 * assistance.
9420 */
Avi Kivity851ba692009-08-24 11:10:17 +03009421static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009422{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009423 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009424 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009425 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009426
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009427 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9428
Kai Huang843e4332015-01-28 10:54:28 +08009429 /*
9430 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9431 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9432 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9433 * mode as if vcpus is in root mode, the PML buffer must has been
9434 * flushed already.
9435 */
9436 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009437 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009438
Mohammed Gamal80ced182009-09-01 12:48:18 +02009439 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009440 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009441 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009442
Paolo Bonzini7313c692017-07-27 10:31:25 +02009443 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9444 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009445
Mohammed Gamal51207022010-05-31 22:40:54 +03009446 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009447 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009448 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9449 vcpu->run->fail_entry.hardware_entry_failure_reason
9450 = exit_reason;
9451 return 0;
9452 }
9453
Avi Kivity29bd8a72007-09-10 17:27:03 +03009454 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009455 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9456 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009457 = vmcs_read32(VM_INSTRUCTION_ERROR);
9458 return 0;
9459 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009460
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009461 /*
9462 * Note:
9463 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9464 * delivery event since it indicates guest is accessing MMIO.
9465 * The vm-exit can be triggered again after return to guest that
9466 * will cause infinite loop.
9467 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009468 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009469 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009470 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009471 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009472 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9473 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9474 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009475 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009476 vcpu->run->internal.data[0] = vectoring_info;
9477 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009478 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9479 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9480 vcpu->run->internal.ndata++;
9481 vcpu->run->internal.data[3] =
9482 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9483 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009484 return 0;
9485 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009486
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009487 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009488 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9489 if (vmx_interrupt_allowed(vcpu)) {
9490 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9491 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9492 vcpu->arch.nmi_pending) {
9493 /*
9494 * This CPU don't support us in finding the end of an
9495 * NMI-blocked window if the guest runs with IRQs
9496 * disabled. So we pull the trigger after 1 s of
9497 * futile waiting, but inform the user about this.
9498 */
9499 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9500 "state on VCPU %d after 1 s timeout\n",
9501 __func__, vcpu->vcpu_id);
9502 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9503 }
9504 }
9505
Avi Kivity6aa8b732006-12-10 02:21:36 -08009506 if (exit_reason < kvm_vmx_max_exit_handlers
9507 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009508 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009509 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009510 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9511 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009512 kvm_queue_exception(vcpu, UD_VECTOR);
9513 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009514 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009515}
9516
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009517static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009518{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009519 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9520
9521 if (is_guest_mode(vcpu) &&
9522 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9523 return;
9524
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009525 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009526 vmcs_write32(TPR_THRESHOLD, 0);
9527 return;
9528 }
9529
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009530 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009531}
9532
Jim Mattson8d860bb2018-05-09 16:56:05 -04009533static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009534{
9535 u32 sec_exec_control;
9536
Jim Mattson8d860bb2018-05-09 16:56:05 -04009537 if (!lapic_in_kernel(vcpu))
9538 return;
9539
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009540 /* Postpone execution until vmcs01 is the current VMCS. */
9541 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009542 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009543 return;
9544 }
9545
Paolo Bonzini35754c92015-07-29 12:05:37 +02009546 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009547 return;
9548
9549 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009550 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9551 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009552
Jim Mattson8d860bb2018-05-09 16:56:05 -04009553 switch (kvm_get_apic_mode(vcpu)) {
9554 case LAPIC_MODE_INVALID:
9555 WARN_ONCE(true, "Invalid local APIC state");
9556 case LAPIC_MODE_DISABLED:
9557 break;
9558 case LAPIC_MODE_XAPIC:
9559 if (flexpriority_enabled) {
9560 sec_exec_control |=
9561 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9562 vmx_flush_tlb(vcpu, true);
9563 }
9564 break;
9565 case LAPIC_MODE_X2APIC:
9566 if (cpu_has_vmx_virtualize_x2apic_mode())
9567 sec_exec_control |=
9568 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9569 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009570 }
9571 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9572
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009573 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009574}
9575
Tang Chen38b99172014-09-24 15:57:54 +08009576static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9577{
Jim Mattsonab5df312018-05-09 17:02:03 -04009578 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009579 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009580 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009581 }
Tang Chen38b99172014-09-24 15:57:54 +08009582}
9583
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009584static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009585{
9586 u16 status;
9587 u8 old;
9588
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009589 if (max_isr == -1)
9590 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009591
9592 status = vmcs_read16(GUEST_INTR_STATUS);
9593 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009594 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009595 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009596 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009597 vmcs_write16(GUEST_INTR_STATUS, status);
9598 }
9599}
9600
9601static void vmx_set_rvi(int vector)
9602{
9603 u16 status;
9604 u8 old;
9605
Wei Wang4114c272014-11-05 10:53:43 +08009606 if (vector == -1)
9607 vector = 0;
9608
Yang Zhangc7c9c562013-01-25 10:18:51 +08009609 status = vmcs_read16(GUEST_INTR_STATUS);
9610 old = (u8)status & 0xff;
9611 if ((u8)vector != old) {
9612 status &= ~0xff;
9613 status |= (u8)vector;
9614 vmcs_write16(GUEST_INTR_STATUS, status);
9615 }
9616}
9617
9618static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9619{
Liran Alon851c1a182017-12-24 18:12:56 +02009620 /*
9621 * When running L2, updating RVI is only relevant when
9622 * vmcs12 virtual-interrupt-delivery enabled.
9623 * However, it can be enabled only when L1 also
9624 * intercepts external-interrupts and in that case
9625 * we should not update vmcs02 RVI but instead intercept
9626 * interrupt. Therefore, do nothing when running L2.
9627 */
9628 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009629 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009630}
9631
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009632static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009633{
9634 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009635 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009636 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009637
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009638 WARN_ON(!vcpu->arch.apicv_active);
9639 if (pi_test_on(&vmx->pi_desc)) {
9640 pi_clear_on(&vmx->pi_desc);
9641 /*
9642 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9643 * But on x86 this is just a compiler barrier anyway.
9644 */
9645 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009646 max_irr_updated =
9647 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9648
9649 /*
9650 * If we are running L2 and L1 has a new pending interrupt
9651 * which can be injected, we should re-evaluate
9652 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009653 * If L1 intercepts external-interrupts, we should
9654 * exit from L2 to L1. Otherwise, interrupt should be
9655 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009656 */
Liran Alon851c1a182017-12-24 18:12:56 +02009657 if (is_guest_mode(vcpu) && max_irr_updated) {
9658 if (nested_exit_on_intr(vcpu))
9659 kvm_vcpu_exiting_guest_mode(vcpu);
9660 else
9661 kvm_make_request(KVM_REQ_EVENT, vcpu);
9662 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009663 } else {
9664 max_irr = kvm_lapic_find_highest_irr(vcpu);
9665 }
9666 vmx_hwapic_irr_update(vcpu, max_irr);
9667 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009668}
9669
Andrey Smetanin63086302015-11-10 15:36:32 +03009670static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009671{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009672 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009673 return;
9674
Yang Zhangc7c9c562013-01-25 10:18:51 +08009675 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9676 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9677 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9678 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9679}
9680
Paolo Bonzini967235d2016-12-19 14:03:45 +01009681static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9682{
9683 struct vcpu_vmx *vmx = to_vmx(vcpu);
9684
9685 pi_clear_on(&vmx->pi_desc);
9686 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9687}
9688
Avi Kivity51aa01d2010-07-20 14:31:20 +03009689static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009690{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009691 u32 exit_intr_info = 0;
9692 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009693
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009694 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9695 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009696 return;
9697
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009698 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9699 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9700 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009701
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009702 /* if exit due to PF check for async PF */
9703 if (is_page_fault(exit_intr_info))
9704 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9705
Andi Kleena0861c02009-06-08 17:37:09 +08009706 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009707 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9708 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009709 kvm_machine_check();
9710
Gleb Natapov20f65982009-05-11 13:35:55 +03009711 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009712 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009713 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009714 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009715 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009716 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009717}
Gleb Natapov20f65982009-05-11 13:35:55 +03009718
Yang Zhanga547c6d2013-04-11 19:25:10 +08009719static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9720{
9721 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9722
Yang Zhanga547c6d2013-04-11 19:25:10 +08009723 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9724 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9725 unsigned int vector;
9726 unsigned long entry;
9727 gate_desc *desc;
9728 struct vcpu_vmx *vmx = to_vmx(vcpu);
9729#ifdef CONFIG_X86_64
9730 unsigned long tmp;
9731#endif
9732
9733 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9734 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009735 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009736 asm volatile(
9737#ifdef CONFIG_X86_64
9738 "mov %%" _ASM_SP ", %[sp]\n\t"
9739 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9740 "push $%c[ss]\n\t"
9741 "push %[sp]\n\t"
9742#endif
9743 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009744 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009745 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009746 :
9747#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009748 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009749#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009750 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009751 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009752 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009753 [ss]"i"(__KERNEL_DS),
9754 [cs]"i"(__KERNEL_CS)
9755 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009756 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009757}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009758STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009759
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009760static bool vmx_has_high_real_mode_segbase(void)
9761{
9762 return enable_unrestricted_guest || emulate_invalid_guest_state;
9763}
9764
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009765static bool vmx_mpx_supported(void)
9766{
9767 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9768 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9769}
9770
Wanpeng Li55412b22014-12-02 19:21:30 +08009771static bool vmx_xsaves_supported(void)
9772{
9773 return vmcs_config.cpu_based_2nd_exec_ctrl &
9774 SECONDARY_EXEC_XSAVES;
9775}
9776
Avi Kivity51aa01d2010-07-20 14:31:20 +03009777static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9778{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009779 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009780 bool unblock_nmi;
9781 u8 vector;
9782 bool idtv_info_valid;
9783
9784 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009785
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009786 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009787 if (vmx->loaded_vmcs->nmi_known_unmasked)
9788 return;
9789 /*
9790 * Can't use vmx->exit_intr_info since we're not sure what
9791 * the exit reason is.
9792 */
9793 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9794 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9795 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9796 /*
9797 * SDM 3: 27.7.1.2 (September 2008)
9798 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9799 * a guest IRET fault.
9800 * SDM 3: 23.2.2 (September 2008)
9801 * Bit 12 is undefined in any of the following cases:
9802 * If the VM exit sets the valid bit in the IDT-vectoring
9803 * information field.
9804 * If the VM exit is due to a double fault.
9805 */
9806 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9807 vector != DF_VECTOR && !idtv_info_valid)
9808 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9809 GUEST_INTR_STATE_NMI);
9810 else
9811 vmx->loaded_vmcs->nmi_known_unmasked =
9812 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9813 & GUEST_INTR_STATE_NMI);
9814 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9815 vmx->loaded_vmcs->vnmi_blocked_time +=
9816 ktime_to_ns(ktime_sub(ktime_get(),
9817 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009818}
9819
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009820static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009821 u32 idt_vectoring_info,
9822 int instr_len_field,
9823 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009824{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009825 u8 vector;
9826 int type;
9827 bool idtv_info_valid;
9828
9829 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009830
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009831 vcpu->arch.nmi_injected = false;
9832 kvm_clear_exception_queue(vcpu);
9833 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009834
9835 if (!idtv_info_valid)
9836 return;
9837
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009838 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009839
Avi Kivity668f6122008-07-02 09:28:55 +03009840 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9841 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009842
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009843 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009844 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009845 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009846 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009847 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009848 * Clear bit "block by NMI" before VM entry if a NMI
9849 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009850 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009851 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009852 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009853 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009854 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009855 /* fall through */
9856 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009857 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009858 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009859 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009860 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009861 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009862 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009863 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009864 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009865 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009866 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009867 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009868 break;
9869 default:
9870 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009871 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009872}
9873
Avi Kivity83422e12010-07-20 14:43:23 +03009874static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9875{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009876 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009877 VM_EXIT_INSTRUCTION_LEN,
9878 IDT_VECTORING_ERROR_CODE);
9879}
9880
Avi Kivityb463a6f2010-07-20 15:06:17 +03009881static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9882{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009883 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009884 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9885 VM_ENTRY_INSTRUCTION_LEN,
9886 VM_ENTRY_EXCEPTION_ERROR_CODE);
9887
9888 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9889}
9890
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009891static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9892{
9893 int i, nr_msrs;
9894 struct perf_guest_switch_msr *msrs;
9895
9896 msrs = perf_guest_get_msrs(&nr_msrs);
9897
9898 if (!msrs)
9899 return;
9900
9901 for (i = 0; i < nr_msrs; i++)
9902 if (msrs[i].host == msrs[i].guest)
9903 clear_atomic_switch_msr(vmx, msrs[i].msr);
9904 else
9905 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9906 msrs[i].host);
9907}
9908
Jiang Biao33365e72016-11-03 15:03:37 +08009909static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009910{
9911 struct vcpu_vmx *vmx = to_vmx(vcpu);
9912 u64 tscl;
9913 u32 delta_tsc;
9914
9915 if (vmx->hv_deadline_tsc == -1)
9916 return;
9917
9918 tscl = rdtsc();
9919 if (vmx->hv_deadline_tsc > tscl)
9920 /* sure to be 32 bit only because checked on set_hv_timer */
9921 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9922 cpu_preemption_timer_multi);
9923 else
9924 delta_tsc = 0;
9925
9926 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9927}
9928
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009929static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009930{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009931 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009932 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009933
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009934 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009935 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009936 vmx->loaded_vmcs->soft_vnmi_blocked))
9937 vmx->loaded_vmcs->entry_time = ktime_get();
9938
Avi Kivity104f2262010-11-18 13:12:52 +02009939 /* Don't enter VMX if guest state is invalid, let the exit handler
9940 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009941 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009942 return;
9943
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009944 if (vmx->ple_window_dirty) {
9945 vmx->ple_window_dirty = false;
9946 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9947 }
9948
Abel Gordon012f83c2013-04-18 14:39:25 +03009949 if (vmx->nested.sync_shadow_vmcs) {
9950 copy_vmcs12_to_shadow(vmx);
9951 vmx->nested.sync_shadow_vmcs = false;
9952 }
9953
Avi Kivity104f2262010-11-18 13:12:52 +02009954 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9955 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9956 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9957 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9958
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009959 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009960 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009961 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009962 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009963 }
9964
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009965 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009966 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009967 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009968 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009969 }
9970
Avi Kivity104f2262010-11-18 13:12:52 +02009971 /* When single-stepping over STI and MOV SS, we must clear the
9972 * corresponding interruptibility bits in the guest state. Otherwise
9973 * vmentry fails as it then expects bit 14 (BS) in pending debug
9974 * exceptions being set, but that's not correct for the guest debugging
9975 * case. */
9976 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9977 vmx_set_interrupt_shadow(vcpu, 0);
9978
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009979 if (static_cpu_has(X86_FEATURE_PKU) &&
9980 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9981 vcpu->arch.pkru != vmx->host_pkru)
9982 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009983
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009984 atomic_switch_perf_msrs(vmx);
9985
Yunhong Jiang64672c92016-06-13 14:19:59 -07009986 vmx_arm_hv_timer(vcpu);
9987
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009988 /*
9989 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9990 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9991 * is no need to worry about the conditional branch over the wrmsr
9992 * being speculatively taken.
9993 */
9994 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009995 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009996
Nadav Har'Eld462b812011-05-24 15:26:10 +03009997 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009998
9999 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10000 (unsigned long)&current_evmcs->host_rsp : 0;
10001
Avi Kivity104f2262010-11-18 13:12:52 +020010002 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010003 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010004 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10005 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10006 "push %%" _ASM_CX " \n\t"
10007 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010008 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010009 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010010 /* Avoid VMWRITE when Enlightened VMCS is in use */
10011 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10012 "jz 2f \n\t"
10013 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10014 "jmp 1f \n\t"
10015 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010016 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010017 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010018 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010019 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10020 "mov %%cr2, %%" _ASM_DX " \n\t"
10021 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010022 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010023 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010024 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010025 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010026 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010027 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010028 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10029 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10030 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10031 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10032 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10033 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010034#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010035 "mov %c[r8](%0), %%r8 \n\t"
10036 "mov %c[r9](%0), %%r9 \n\t"
10037 "mov %c[r10](%0), %%r10 \n\t"
10038 "mov %c[r11](%0), %%r11 \n\t"
10039 "mov %c[r12](%0), %%r12 \n\t"
10040 "mov %c[r13](%0), %%r13 \n\t"
10041 "mov %c[r14](%0), %%r14 \n\t"
10042 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010043#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010044 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010045
Avi Kivity6aa8b732006-12-10 02:21:36 -080010046 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010047 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010048 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010049 "jmp 2f \n\t"
10050 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10051 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010052 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010053 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010054 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010055 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010056 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10057 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10058 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10059 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10060 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10061 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10062 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010063#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010064 "mov %%r8, %c[r8](%0) \n\t"
10065 "mov %%r9, %c[r9](%0) \n\t"
10066 "mov %%r10, %c[r10](%0) \n\t"
10067 "mov %%r11, %c[r11](%0) \n\t"
10068 "mov %%r12, %c[r12](%0) \n\t"
10069 "mov %%r13, %c[r13](%0) \n\t"
10070 "mov %%r14, %c[r14](%0) \n\t"
10071 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010072 "xor %%r8d, %%r8d \n\t"
10073 "xor %%r9d, %%r9d \n\t"
10074 "xor %%r10d, %%r10d \n\t"
10075 "xor %%r11d, %%r11d \n\t"
10076 "xor %%r12d, %%r12d \n\t"
10077 "xor %%r13d, %%r13d \n\t"
10078 "xor %%r14d, %%r14d \n\t"
10079 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010080#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010081 "mov %%cr2, %%" _ASM_AX " \n\t"
10082 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010083
Jim Mattson0cb5b302018-01-03 14:31:38 -080010084 "xor %%eax, %%eax \n\t"
10085 "xor %%ebx, %%ebx \n\t"
10086 "xor %%esi, %%esi \n\t"
10087 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010088 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010089 ".pushsection .rodata \n\t"
10090 ".global vmx_return \n\t"
10091 "vmx_return: " _ASM_PTR " 2b \n\t"
10092 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010093 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010094 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010095 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010096 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010097 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10098 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10099 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10100 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10101 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10102 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10103 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010104#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010105 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10106 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10107 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10108 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10109 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10110 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10111 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10112 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010113#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010114 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10115 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010116 : "cc", "memory"
10117#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010118 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010119 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010120#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010121 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010122#endif
10123 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010124
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010125 /*
10126 * We do not use IBRS in the kernel. If this vCPU has used the
10127 * SPEC_CTRL MSR it may have left it on; save the value and
10128 * turn it off. This is much more efficient than blindly adding
10129 * it to the atomic save/restore list. Especially as the former
10130 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10131 *
10132 * For non-nested case:
10133 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10134 * save it.
10135 *
10136 * For nested case:
10137 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10138 * save it.
10139 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010140 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010141 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010142
10143 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010144 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010145
David Woodhouse117cc7a2018-01-12 11:11:27 +000010146 /* Eliminate branch target predictions from guest mode */
10147 vmexit_fill_RSB();
10148
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010149 /* All fields are clean at this point */
10150 if (static_branch_unlikely(&enable_evmcs))
10151 current_evmcs->hv_clean_fields |=
10152 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10153
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010154 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010155 if (vmx->host_debugctlmsr)
10156 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010157
Avi Kivityaa67f602012-08-01 16:48:03 +030010158#ifndef CONFIG_X86_64
10159 /*
10160 * The sysexit path does not restore ds/es, so we must set them to
10161 * a reasonable value ourselves.
10162 *
10163 * We can't defer this to vmx_load_host_state() since that function
10164 * may be executed in interrupt context, which saves and restore segments
10165 * around it, nullifying its effect.
10166 */
10167 loadsegment(ds, __USER_DS);
10168 loadsegment(es, __USER_DS);
10169#endif
10170
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010171 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010172 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010173 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010174 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010175 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010176 vcpu->arch.regs_dirty = 0;
10177
Gleb Natapove0b890d2013-09-25 12:51:33 +030010178 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010179 * eager fpu is enabled if PKEY is supported and CR4 is switched
10180 * back on host, so it is safe to read guest PKRU from current
10181 * XSAVE.
10182 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010183 if (static_cpu_has(X86_FEATURE_PKU) &&
10184 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10185 vcpu->arch.pkru = __read_pkru();
10186 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010187 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010188 }
10189
Gleb Natapove0b890d2013-09-25 12:51:33 +030010190 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010191 vmx->idt_vectoring_info = 0;
10192
10193 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10194 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10195 return;
10196
10197 vmx->loaded_vmcs->launched = 1;
10198 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010199
Avi Kivity51aa01d2010-07-20 14:31:20 +030010200 vmx_complete_atomic_exit(vmx);
10201 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010202 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010203}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010204STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010205
Sean Christopherson434a1e92018-03-20 12:17:18 -070010206static struct kvm *vmx_vm_alloc(void)
10207{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010208 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010209 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010210}
10211
10212static void vmx_vm_free(struct kvm *kvm)
10213{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010214 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010215}
10216
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010217static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010218{
10219 struct vcpu_vmx *vmx = to_vmx(vcpu);
10220 int cpu;
10221
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010222 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010223 return;
10224
10225 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010226 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010227 vmx_vcpu_put(vcpu);
10228 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010229 put_cpu();
10230}
10231
Jim Mattson2f1fe812016-07-08 15:36:06 -070010232/*
10233 * Ensure that the current vmcs of the logical processor is the
10234 * vmcs01 of the vcpu before calling free_nested().
10235 */
10236static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10237{
10238 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010239
Christoffer Dallec7660c2017-12-04 21:35:23 +010010240 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010241 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010242 free_nested(vmx);
10243 vcpu_put(vcpu);
10244}
10245
Avi Kivity6aa8b732006-12-10 02:21:36 -080010246static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10247{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010248 struct vcpu_vmx *vmx = to_vmx(vcpu);
10249
Kai Huang843e4332015-01-28 10:54:28 +080010250 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010251 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010252 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010253 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010254 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010255 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010256 kfree(vmx->guest_msrs);
10257 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010258 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010259}
10260
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010261static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010262{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010263 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010264 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010265 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010266 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010267
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010268 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010269 return ERR_PTR(-ENOMEM);
10270
Wanpeng Li991e7a02015-09-16 17:30:05 +080010271 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010272
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010273 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10274 if (err)
10275 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010276
Peter Feiner4e595162016-07-07 14:49:58 -070010277 err = -ENOMEM;
10278
10279 /*
10280 * If PML is turned on, failure on enabling PML just results in failure
10281 * of creating the vcpu, therefore we can simplify PML logic (by
10282 * avoiding dealing with cases, such as enabling PML partially on vcpus
10283 * for the guest, etc.
10284 */
10285 if (enable_pml) {
10286 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10287 if (!vmx->pml_pg)
10288 goto uninit_vcpu;
10289 }
10290
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010291 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010292 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10293 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010294
Peter Feiner4e595162016-07-07 14:49:58 -070010295 if (!vmx->guest_msrs)
10296 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010297
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010298 err = alloc_loaded_vmcs(&vmx->vmcs01);
10299 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010300 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010301
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010302 msr_bitmap = vmx->vmcs01.msr_bitmap;
10303 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10304 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10305 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10306 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10307 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10308 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10309 vmx->msr_bitmap_mode = 0;
10310
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010311 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010312 cpu = get_cpu();
10313 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010314 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010315 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010316 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010317 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010318 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010319 err = alloc_apic_access_page(kvm);
10320 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010321 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010322 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010323
Sean Christophersone90008d2018-03-05 12:04:37 -080010324 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010325 err = init_rmode_identity_map(kvm);
10326 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010327 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010328 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010329
Wanpeng Li5c614b32015-10-13 09:18:36 -070010330 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010331 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10332 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010333 vmx->nested.vpid02 = allocate_vpid();
10334 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010335
Wincy Van705699a2015-02-03 23:58:17 +080010336 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010337 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010338
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010339 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10340
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010341 /*
10342 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10343 * or POSTED_INTR_WAKEUP_VECTOR.
10344 */
10345 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10346 vmx->pi_desc.sn = 1;
10347
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010348 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010349
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010350free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010351 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010352 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010353free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010354 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010355free_pml:
10356 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010357uninit_vcpu:
10358 kvm_vcpu_uninit(&vmx->vcpu);
10359free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010360 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010361 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010362 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010363}
10364
Wanpeng Lib31c1142018-03-12 04:53:04 -070010365static int vmx_vm_init(struct kvm *kvm)
10366{
10367 if (!ple_gap)
10368 kvm->arch.pause_in_guest = true;
10369 return 0;
10370}
10371
Yang, Sheng002c7f72007-07-31 14:23:01 +030010372static void __init vmx_check_processor_compat(void *rtn)
10373{
10374 struct vmcs_config vmcs_conf;
10375
10376 *(int *)rtn = 0;
10377 if (setup_vmcs_config(&vmcs_conf) < 0)
10378 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010379 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010380 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10381 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10382 smp_processor_id());
10383 *(int *)rtn = -EIO;
10384 }
10385}
10386
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010387static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010388{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010389 u8 cache;
10390 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010391
Sheng Yang522c68c2009-04-27 20:35:43 +080010392 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010393 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010394 * 2. EPT with VT-d:
10395 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010396 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010397 * b. VT-d with snooping control feature: snooping control feature of
10398 * VT-d engine can guarantee the cache correctness. Just set it
10399 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010400 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010401 * consistent with host MTRR
10402 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010403 if (is_mmio) {
10404 cache = MTRR_TYPE_UNCACHABLE;
10405 goto exit;
10406 }
10407
10408 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010409 ipat = VMX_EPT_IPAT_BIT;
10410 cache = MTRR_TYPE_WRBACK;
10411 goto exit;
10412 }
10413
10414 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10415 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010416 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010417 cache = MTRR_TYPE_WRBACK;
10418 else
10419 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010420 goto exit;
10421 }
10422
Xiao Guangrongff536042015-06-15 16:55:22 +080010423 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010424
10425exit:
10426 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010427}
10428
Sheng Yang17cc3932010-01-05 19:02:27 +080010429static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010430{
Sheng Yang878403b2010-01-05 19:02:29 +080010431 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10432 return PT_DIRECTORY_LEVEL;
10433 else
10434 /* For shadow and EPT supported 1GB page */
10435 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010436}
10437
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010438static void vmcs_set_secondary_exec_control(u32 new_ctl)
10439{
10440 /*
10441 * These bits in the secondary execution controls field
10442 * are dynamic, the others are mostly based on the hypervisor
10443 * architecture and the guest's CPUID. Do not touch the
10444 * dynamic bits.
10445 */
10446 u32 mask =
10447 SECONDARY_EXEC_SHADOW_VMCS |
10448 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010449 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10450 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010451
10452 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10453
10454 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10455 (new_ctl & ~mask) | (cur_ctl & mask));
10456}
10457
David Matlack8322ebb2016-11-29 18:14:09 -080010458/*
10459 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10460 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10461 */
10462static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10463{
10464 struct vcpu_vmx *vmx = to_vmx(vcpu);
10465 struct kvm_cpuid_entry2 *entry;
10466
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010467 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10468 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010469
10470#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10471 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010472 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010473} while (0)
10474
10475 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10476 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10477 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10478 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10479 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10480 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10481 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10482 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10483 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10484 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10485 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10486 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10487 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10488 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10489 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10490
10491 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10492 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10493 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10494 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10495 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010496 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010497
10498#undef cr4_fixed1_update
10499}
10500
Sheng Yang0e851882009-12-18 16:48:46 +080010501static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10502{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010503 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010504
Paolo Bonzini80154d72017-08-24 13:55:35 +020010505 if (cpu_has_secondary_exec_ctrls()) {
10506 vmx_compute_secondary_exec_control(vmx);
10507 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010508 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010509
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010510 if (nested_vmx_allowed(vcpu))
10511 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10512 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10513 else
10514 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10515 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010516
10517 if (nested_vmx_allowed(vcpu))
10518 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010519}
10520
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010521static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10522{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010523 if (func == 1 && nested)
10524 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010525}
10526
Yang Zhang25d92082013-08-06 12:00:32 +030010527static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10528 struct x86_exception *fault)
10529{
Jan Kiszka533558b2014-01-04 18:47:20 +010010530 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010531 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010532 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010533 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010534
Bandan Dasc5f983f2017-05-05 15:25:14 -040010535 if (vmx->nested.pml_full) {
10536 exit_reason = EXIT_REASON_PML_FULL;
10537 vmx->nested.pml_full = false;
10538 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10539 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010540 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010541 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010542 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010543
10544 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010545 vmcs12->guest_physical_address = fault->address;
10546}
10547
Peter Feiner995f00a2017-06-30 17:26:32 -070010548static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10549{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010550 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010551}
10552
Nadav Har'El155a97a2013-08-05 11:07:16 +030010553/* Callbacks for nested_ept_init_mmu_context: */
10554
10555static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10556{
10557 /* return the page table to be shadowed - in our case, EPT12 */
10558 return get_vmcs12(vcpu)->ept_pointer;
10559}
10560
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010561static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010562{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010563 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010564 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010565 return 1;
10566
10567 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010568 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010569 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010570 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010571 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010572 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10573 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10574 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10575
10576 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010577 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010578}
10579
10580static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10581{
10582 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10583}
10584
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010585static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10586 u16 error_code)
10587{
10588 bool inequality, bit;
10589
10590 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10591 inequality =
10592 (error_code & vmcs12->page_fault_error_code_mask) !=
10593 vmcs12->page_fault_error_code_match;
10594 return inequality ^ bit;
10595}
10596
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010597static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10598 struct x86_exception *fault)
10599{
10600 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10601
10602 WARN_ON(!is_guest_mode(vcpu));
10603
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010604 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10605 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010606 vmcs12->vm_exit_intr_error_code = fault->error_code;
10607 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10608 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10609 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10610 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010611 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010612 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010613 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010614}
10615
Paolo Bonzinic9923842017-12-13 14:16:30 +010010616static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10617 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010618
10619static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010620 struct vmcs12 *vmcs12)
10621{
10622 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010623 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010624 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010625
10626 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010627 /*
10628 * Translate L1 physical address to host physical
10629 * address for vmcs02. Keep the page pinned, so this
10630 * physical address remains valid. We keep a reference
10631 * to it so we can release it later.
10632 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010633 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010634 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010635 vmx->nested.apic_access_page = NULL;
10636 }
10637 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010638 /*
10639 * If translation failed, no matter: This feature asks
10640 * to exit when accessing the given address, and if it
10641 * can never be accessed, this feature won't do
10642 * anything anyway.
10643 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010644 if (!is_error_page(page)) {
10645 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010646 hpa = page_to_phys(vmx->nested.apic_access_page);
10647 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10648 } else {
10649 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10650 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10651 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010652 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010653
10654 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010655 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010656 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010657 vmx->nested.virtual_apic_page = NULL;
10658 }
10659 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010660
10661 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010662 * If translation failed, VM entry will fail because
10663 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10664 * Failing the vm entry is _not_ what the processor
10665 * does but it's basically the only possibility we
10666 * have. We could still enter the guest if CR8 load
10667 * exits are enabled, CR8 store exits are enabled, and
10668 * virtualize APIC access is disabled; in this case
10669 * the processor would never use the TPR shadow and we
10670 * could simply clear the bit from the execution
10671 * control. But such a configuration is useless, so
10672 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010673 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010674 if (!is_error_page(page)) {
10675 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010676 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10677 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10678 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010679 }
10680
Wincy Van705699a2015-02-03 23:58:17 +080010681 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010682 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10683 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010684 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010685 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010686 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010687 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10688 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010689 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010690 vmx->nested.pi_desc_page = page;
10691 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010692 vmx->nested.pi_desc =
10693 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10694 (unsigned long)(vmcs12->posted_intr_desc_addr &
10695 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010696 vmcs_write64(POSTED_INTR_DESC_ADDR,
10697 page_to_phys(vmx->nested.pi_desc_page) +
10698 (unsigned long)(vmcs12->posted_intr_desc_addr &
10699 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010700 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010701 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010702 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10703 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010704 else
10705 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10706 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010707}
10708
Jan Kiszkaf41245002014-03-07 20:03:13 +010010709static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10710{
10711 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10712 struct vcpu_vmx *vmx = to_vmx(vcpu);
10713
10714 if (vcpu->arch.virtual_tsc_khz == 0)
10715 return;
10716
10717 /* Make sure short timeouts reliably trigger an immediate vmexit.
10718 * hrtimer_start does not guarantee this. */
10719 if (preemption_timeout <= 1) {
10720 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10721 return;
10722 }
10723
10724 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10725 preemption_timeout *= 1000000;
10726 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10727 hrtimer_start(&vmx->nested.preemption_timer,
10728 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10729}
10730
Jim Mattson56a20512017-07-06 16:33:06 -070010731static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10732 struct vmcs12 *vmcs12)
10733{
10734 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10735 return 0;
10736
10737 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10738 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10739 return -EINVAL;
10740
10741 return 0;
10742}
10743
Wincy Van3af18d92015-02-03 23:49:31 +080010744static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10745 struct vmcs12 *vmcs12)
10746{
Wincy Van3af18d92015-02-03 23:49:31 +080010747 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10748 return 0;
10749
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010750 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010751 return -EINVAL;
10752
10753 return 0;
10754}
10755
Jim Mattson712b12d2017-08-24 13:24:47 -070010756static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10757 struct vmcs12 *vmcs12)
10758{
10759 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10760 return 0;
10761
10762 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10763 return -EINVAL;
10764
10765 return 0;
10766}
10767
Wincy Van3af18d92015-02-03 23:49:31 +080010768/*
10769 * Merge L0's and L1's MSR bitmap, return false to indicate that
10770 * we do not use the hardware.
10771 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010772static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10773 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010774{
Wincy Van82f0dd42015-02-03 23:57:18 +080010775 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010776 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010777 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010778 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010779 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010780 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010781 *
10782 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10783 * ensures that we do not accidentally generate an L02 MSR bitmap
10784 * from the L12 MSR bitmap that is too permissive.
10785 * 2. That L1 or L2s have actually used the MSR. This avoids
10786 * unnecessarily merging of the bitmap if the MSR is unused. This
10787 * works properly because we only update the L01 MSR bitmap lazily.
10788 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10789 * updated to reflect this when L1 (or its L2s) actually write to
10790 * the MSR.
10791 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010792 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10793 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010794
Paolo Bonzinic9923842017-12-13 14:16:30 +010010795 /* Nothing to do if the MSR bitmap is not in use. */
10796 if (!cpu_has_vmx_msr_bitmap() ||
10797 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10798 return false;
10799
Ashok Raj15d45072018-02-01 22:59:43 +010010800 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010801 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010802 return false;
10803
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010804 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10805 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010806 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010807
Radim Krčmářd048c092016-08-08 20:16:22 +020010808 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010809 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10810 /*
10811 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10812 * just lets the processor take the value from the virtual-APIC page;
10813 * take those 256 bits directly from the L1 bitmap.
10814 */
10815 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10816 unsigned word = msr / BITS_PER_LONG;
10817 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10818 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010819 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010820 } else {
10821 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10822 unsigned word = msr / BITS_PER_LONG;
10823 msr_bitmap_l0[word] = ~0;
10824 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10825 }
10826 }
10827
10828 nested_vmx_disable_intercept_for_msr(
10829 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010830 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010831 MSR_TYPE_W);
10832
10833 if (nested_cpu_has_vid(vmcs12)) {
10834 nested_vmx_disable_intercept_for_msr(
10835 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010836 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010837 MSR_TYPE_W);
10838 nested_vmx_disable_intercept_for_msr(
10839 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010840 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010841 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010842 }
Ashok Raj15d45072018-02-01 22:59:43 +010010843
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010844 if (spec_ctrl)
10845 nested_vmx_disable_intercept_for_msr(
10846 msr_bitmap_l1, msr_bitmap_l0,
10847 MSR_IA32_SPEC_CTRL,
10848 MSR_TYPE_R | MSR_TYPE_W);
10849
Ashok Raj15d45072018-02-01 22:59:43 +010010850 if (pred_cmd)
10851 nested_vmx_disable_intercept_for_msr(
10852 msr_bitmap_l1, msr_bitmap_l0,
10853 MSR_IA32_PRED_CMD,
10854 MSR_TYPE_W);
10855
Wincy Vanf2b93282015-02-03 23:56:03 +080010856 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010857 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010858
10859 return true;
10860}
10861
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010862static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10863 struct vmcs12 *vmcs12)
10864{
10865 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10866 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10867 return -EINVAL;
10868 else
10869 return 0;
10870}
10871
Wincy Vanf2b93282015-02-03 23:56:03 +080010872static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10873 struct vmcs12 *vmcs12)
10874{
Wincy Van82f0dd42015-02-03 23:57:18 +080010875 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010876 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010877 !nested_cpu_has_vid(vmcs12) &&
10878 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010879 return 0;
10880
10881 /*
10882 * If virtualize x2apic mode is enabled,
10883 * virtualize apic access must be disabled.
10884 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010885 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10886 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010887 return -EINVAL;
10888
Wincy Van608406e2015-02-03 23:57:51 +080010889 /*
10890 * If virtual interrupt delivery is enabled,
10891 * we must exit on external interrupts.
10892 */
10893 if (nested_cpu_has_vid(vmcs12) &&
10894 !nested_exit_on_intr(vcpu))
10895 return -EINVAL;
10896
Wincy Van705699a2015-02-03 23:58:17 +080010897 /*
10898 * bits 15:8 should be zero in posted_intr_nv,
10899 * the descriptor address has been already checked
10900 * in nested_get_vmcs12_pages.
10901 */
10902 if (nested_cpu_has_posted_intr(vmcs12) &&
10903 (!nested_cpu_has_vid(vmcs12) ||
10904 !nested_exit_intr_ack_set(vcpu) ||
10905 vmcs12->posted_intr_nv & 0xff00))
10906 return -EINVAL;
10907
Wincy Vanf2b93282015-02-03 23:56:03 +080010908 /* tpr shadow is needed by all apicv features. */
10909 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10910 return -EINVAL;
10911
10912 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010913}
10914
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010915static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10916 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010917 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010918{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010919 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010920 u64 count, addr;
10921
10922 if (vmcs12_read_any(vcpu, count_field, &count) ||
10923 vmcs12_read_any(vcpu, addr_field, &addr)) {
10924 WARN_ON(1);
10925 return -EINVAL;
10926 }
10927 if (count == 0)
10928 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010929 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010930 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10931 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010932 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010933 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10934 addr_field, maxphyaddr, count, addr);
10935 return -EINVAL;
10936 }
10937 return 0;
10938}
10939
10940static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10941 struct vmcs12 *vmcs12)
10942{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010943 if (vmcs12->vm_exit_msr_load_count == 0 &&
10944 vmcs12->vm_exit_msr_store_count == 0 &&
10945 vmcs12->vm_entry_msr_load_count == 0)
10946 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010947 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010948 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010949 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010950 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010951 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010952 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010953 return -EINVAL;
10954 return 0;
10955}
10956
Bandan Dasc5f983f2017-05-05 15:25:14 -040010957static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10958 struct vmcs12 *vmcs12)
10959{
10960 u64 address = vmcs12->pml_address;
10961 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10962
10963 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10964 if (!nested_cpu_has_ept(vmcs12) ||
10965 !IS_ALIGNED(address, 4096) ||
10966 address >> maxphyaddr)
10967 return -EINVAL;
10968 }
10969
10970 return 0;
10971}
10972
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010973static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10974 struct vmx_msr_entry *e)
10975{
10976 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010977 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010978 return -EINVAL;
10979 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10980 e->index == MSR_IA32_UCODE_REV)
10981 return -EINVAL;
10982 if (e->reserved != 0)
10983 return -EINVAL;
10984 return 0;
10985}
10986
10987static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10988 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010989{
10990 if (e->index == MSR_FS_BASE ||
10991 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010992 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10993 nested_vmx_msr_check_common(vcpu, e))
10994 return -EINVAL;
10995 return 0;
10996}
10997
10998static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10999 struct vmx_msr_entry *e)
11000{
11001 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11002 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011003 return -EINVAL;
11004 return 0;
11005}
11006
11007/*
11008 * Load guest's/host's msr at nested entry/exit.
11009 * return 0 for success, entry index for failure.
11010 */
11011static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11012{
11013 u32 i;
11014 struct vmx_msr_entry e;
11015 struct msr_data msr;
11016
11017 msr.host_initiated = false;
11018 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011019 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11020 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011021 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011022 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11023 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011024 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011025 }
11026 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011027 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011028 "%s check failed (%u, 0x%x, 0x%x)\n",
11029 __func__, i, e.index, e.reserved);
11030 goto fail;
11031 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011032 msr.index = e.index;
11033 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011034 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011035 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011036 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11037 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011038 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011039 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011040 }
11041 return 0;
11042fail:
11043 return i + 1;
11044}
11045
11046static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11047{
11048 u32 i;
11049 struct vmx_msr_entry e;
11050
11051 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011052 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011053 if (kvm_vcpu_read_guest(vcpu,
11054 gpa + i * sizeof(e),
11055 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011056 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011057 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11058 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011059 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011060 }
11061 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011062 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011063 "%s check failed (%u, 0x%x, 0x%x)\n",
11064 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011065 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011066 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011067 msr_info.host_initiated = false;
11068 msr_info.index = e.index;
11069 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011070 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011071 "%s cannot read MSR (%u, 0x%x)\n",
11072 __func__, i, e.index);
11073 return -EINVAL;
11074 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011075 if (kvm_vcpu_write_guest(vcpu,
11076 gpa + i * sizeof(e) +
11077 offsetof(struct vmx_msr_entry, value),
11078 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011079 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011080 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011081 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011082 return -EINVAL;
11083 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011084 }
11085 return 0;
11086}
11087
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011088static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11089{
11090 unsigned long invalid_mask;
11091
11092 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11093 return (val & invalid_mask) == 0;
11094}
11095
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011096/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011097 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11098 * emulating VM entry into a guest with EPT enabled.
11099 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11100 * is assigned to entry_failure_code on failure.
11101 */
11102static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011103 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011104{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011105 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011106 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011107 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11108 return 1;
11109 }
11110
11111 /*
11112 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11113 * must not be dereferenced.
11114 */
11115 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11116 !nested_ept) {
11117 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11118 *entry_failure_code = ENTRY_FAIL_PDPTE;
11119 return 1;
11120 }
11121 }
11122
11123 vcpu->arch.cr3 = cr3;
11124 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11125 }
11126
11127 kvm_mmu_reset_context(vcpu);
11128 return 0;
11129}
11130
Jim Mattson6514dc32018-04-26 16:09:12 -070011131static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011132{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011133 struct vcpu_vmx *vmx = to_vmx(vcpu);
11134
11135 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11136 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11137 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11138 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11139 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11140 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11141 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11142 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11143 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11144 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11145 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11146 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11147 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11148 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11149 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11150 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11151 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11152 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11153 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11154 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11155 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11156 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11157 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11158 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11159 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11160 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11161 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11162 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11163 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11164 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11165 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011166
11167 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11168 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11169 vmcs12->guest_pending_dbg_exceptions);
11170 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11171 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11172
11173 if (nested_cpu_has_xsaves(vmcs12))
11174 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11175 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11176
11177 if (cpu_has_vmx_posted_intr())
11178 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11179
11180 /*
11181 * Whether page-faults are trapped is determined by a combination of
11182 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11183 * If enable_ept, L0 doesn't care about page faults and we should
11184 * set all of these to L1's desires. However, if !enable_ept, L0 does
11185 * care about (at least some) page faults, and because it is not easy
11186 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11187 * to exit on each and every L2 page fault. This is done by setting
11188 * MASK=MATCH=0 and (see below) EB.PF=1.
11189 * Note that below we don't need special code to set EB.PF beyond the
11190 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11191 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11192 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11193 */
11194 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11195 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11196 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11197 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11198
11199 /* All VMFUNCs are currently emulated through L0 vmexits. */
11200 if (cpu_has_vmx_vmfunc())
11201 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11202
11203 if (cpu_has_vmx_apicv()) {
11204 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11205 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11206 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11207 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11208 }
11209
11210 /*
11211 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11212 * Some constant fields are set here by vmx_set_constant_host_state().
11213 * Other fields are different per CPU, and will be set later when
11214 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11215 */
11216 vmx_set_constant_host_state(vmx);
11217
11218 /*
11219 * Set the MSR load/store lists to match L0's settings.
11220 */
11221 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11222 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11223 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11224 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11225 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11226
11227 set_cr4_guest_host_mask(vmx);
11228
11229 if (vmx_mpx_supported())
11230 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11231
11232 if (enable_vpid) {
11233 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11234 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11235 else
11236 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11237 }
11238
11239 /*
11240 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11241 */
11242 if (enable_ept) {
11243 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11244 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11245 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11246 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11247 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011248
11249 if (cpu_has_vmx_msr_bitmap())
11250 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011251}
11252
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011253/*
11254 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11255 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011256 * 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 +030011257 * guest in a way that will both be appropriate to L1's requests, and our
11258 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11259 * function also has additional necessary side-effects, like setting various
11260 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011261 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11262 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011263 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011264static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011265 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011266{
11267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011268 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011269
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011270 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011271 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011272 vmx->nested.dirty_vmcs12 = false;
11273 }
11274
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011275 /*
11276 * First, the fields that are shadowed. This must be kept in sync
11277 * with vmx_shadow_fields.h.
11278 */
11279
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011280 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011281 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011282 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011283 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11284 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011285
11286 /*
11287 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11288 * HOST_FS_BASE, HOST_GS_BASE.
11289 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011290
Jim Mattson6514dc32018-04-26 16:09:12 -070011291 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011292 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011293 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11294 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11295 } else {
11296 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11297 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11298 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011299 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011300 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11301 vmcs12->vm_entry_intr_info_field);
11302 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11303 vmcs12->vm_entry_exception_error_code);
11304 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11305 vmcs12->vm_entry_instruction_len);
11306 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11307 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011308 vmx->loaded_vmcs->nmi_known_unmasked =
11309 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011310 } else {
11311 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11312 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011313 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011314
Jan Kiszkaf41245002014-03-07 20:03:13 +010011315 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011316
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011317 /* Preemption timer setting is only taken from vmcs01. */
11318 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11319 exec_control |= vmcs_config.pin_based_exec_ctrl;
11320 if (vmx->hv_deadline_tsc == -1)
11321 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11322
11323 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011324 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011325 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11326 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011327 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011328 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011329 }
Wincy Van705699a2015-02-03 23:58:17 +080011330
Jan Kiszkaf41245002014-03-07 20:03:13 +010011331 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011332
Jan Kiszkaf41245002014-03-07 20:03:13 +010011333 vmx->nested.preemption_timer_expired = false;
11334 if (nested_cpu_has_preemption_timer(vmcs12))
11335 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011336
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011337 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011338 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011339
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011340 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011341 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011342 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011343 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011344 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011345 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011346 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11347 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011348 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011349 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11350 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11351 ~SECONDARY_EXEC_ENABLE_PML;
11352 exec_control |= vmcs12_exec_ctrl;
11353 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011354
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011355 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011356 vmcs_write16(GUEST_INTR_STATUS,
11357 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011358
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011359 /*
11360 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11361 * nested_get_vmcs12_pages will either fix it up or
11362 * remove the VM execution control.
11363 */
11364 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11365 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11366
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011367 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11368 }
11369
Jim Mattson83bafef2016-10-04 10:48:38 -070011370 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011371 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11372 * entry, but only if the current (host) sp changed from the value
11373 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11374 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11375 * here we just force the write to happen on entry.
11376 */
11377 vmx->host_rsp = 0;
11378
11379 exec_control = vmx_exec_control(vmx); /* L0's desires */
11380 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11381 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11382 exec_control &= ~CPU_BASED_TPR_SHADOW;
11383 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011384
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011385 /*
11386 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11387 * nested_get_vmcs12_pages can't fix it up, the illegal value
11388 * will result in a VM entry failure.
11389 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011390 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011391 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011392 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011393 } else {
11394#ifdef CONFIG_X86_64
11395 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11396 CPU_BASED_CR8_STORE_EXITING;
11397#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011398 }
11399
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011400 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011401 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11402 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011403 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011404 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11405 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11406
11407 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11408
11409 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11410 * bitwise-or of what L1 wants to trap for L2, and what we want to
11411 * trap. Note that CR0.TS also needs updating - we do this later.
11412 */
11413 update_exception_bitmap(vcpu);
11414 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11415 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11416
Nadav Har'El8049d652013-08-05 11:07:06 +030011417 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11418 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11419 * bits are further modified by vmx_set_efer() below.
11420 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011421 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011422
11423 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11424 * emulated by vmx_set_efer(), below.
11425 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011426 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011427 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11428 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011429 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11430
Jim Mattson6514dc32018-04-26 16:09:12 -070011431 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011432 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011433 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011434 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011435 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011436 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011437 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011438
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011439 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11440
Peter Feinerc95ba922016-08-17 09:36:47 -070011441 if (kvm_has_tsc_control)
11442 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011443
11444 if (enable_vpid) {
11445 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011446 * There is no direct mapping between vpid02 and vpid12, the
11447 * vpid02 is per-vCPU for L0 and reused while the value of
11448 * vpid12 is changed w/ one invvpid during nested vmentry.
11449 * The vpid12 is allocated by L1 for L2, so it will not
11450 * influence global bitmap(for vpid01 and vpid02 allocation)
11451 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011452 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011453 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011454 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11455 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011456 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011457 }
11458 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011459 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011460 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011461 }
11462
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011463 if (enable_pml) {
11464 /*
11465 * Conceptually we want to copy the PML address and index from
11466 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11467 * since we always flush the log on each vmexit, this happens
11468 * to be equivalent to simply resetting the fields in vmcs02.
11469 */
11470 ASSERT(vmx->pml_pg);
11471 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11472 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11473 }
11474
Nadav Har'El155a97a2013-08-05 11:07:16 +030011475 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011476 if (nested_ept_init_mmu_context(vcpu)) {
11477 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11478 return 1;
11479 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011480 } else if (nested_cpu_has2(vmcs12,
11481 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011482 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011483 }
11484
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011485 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011486 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11487 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011488 * The CR0_READ_SHADOW is what L2 should have expected to read given
11489 * the specifications by L1; It's not enough to take
11490 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11491 * have more bits than L1 expected.
11492 */
11493 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11494 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11495
11496 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11497 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11498
Jim Mattson6514dc32018-04-26 16:09:12 -070011499 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011500 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011501 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11502 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11503 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11504 else
11505 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11506 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11507 vmx_set_efer(vcpu, vcpu->arch.efer);
11508
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011509 /*
11510 * Guest state is invalid and unrestricted guest is disabled,
11511 * which means L1 attempted VMEntry to L2 with invalid state.
11512 * Fail the VMEntry.
11513 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011514 if (vmx->emulation_required) {
11515 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011516 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011517 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011518
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011519 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011520 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011521 entry_failure_code))
11522 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011523
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011524 if (!enable_ept)
11525 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11526
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011527 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11528 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011529 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011530}
11531
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011532static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11533{
11534 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11535 nested_cpu_has_virtual_nmis(vmcs12))
11536 return -EINVAL;
11537
11538 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11539 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11540 return -EINVAL;
11541
11542 return 0;
11543}
11544
Jim Mattsonca0bde22016-11-30 12:03:46 -080011545static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11546{
11547 struct vcpu_vmx *vmx = to_vmx(vcpu);
11548
11549 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11550 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11551 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11552
Jim Mattson56a20512017-07-06 16:33:06 -070011553 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11554 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11555
Jim Mattsonca0bde22016-11-30 12:03:46 -080011556 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11557 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11558
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011559 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11560 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11561
Jim Mattson712b12d2017-08-24 13:24:47 -070011562 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11563 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11564
Jim Mattsonca0bde22016-11-30 12:03:46 -080011565 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11566 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11567
11568 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11569 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11570
Bandan Dasc5f983f2017-05-05 15:25:14 -040011571 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11572 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11573
Jim Mattsonca0bde22016-11-30 12:03:46 -080011574 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011575 vmx->nested.msrs.procbased_ctls_low,
11576 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011577 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11578 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011579 vmx->nested.msrs.secondary_ctls_low,
11580 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011581 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011582 vmx->nested.msrs.pinbased_ctls_low,
11583 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011584 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011585 vmx->nested.msrs.exit_ctls_low,
11586 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011587 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011588 vmx->nested.msrs.entry_ctls_low,
11589 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011590 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11591
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011592 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011593 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11594
Bandan Das41ab9372017-08-03 15:54:43 -040011595 if (nested_cpu_has_vmfunc(vmcs12)) {
11596 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011597 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011598 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11599
11600 if (nested_cpu_has_eptp_switching(vmcs12)) {
11601 if (!nested_cpu_has_ept(vmcs12) ||
11602 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11603 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11604 }
11605 }
Bandan Das27c42a12017-08-03 15:54:42 -040011606
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011607 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11608 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11609
Jim Mattsonca0bde22016-11-30 12:03:46 -080011610 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11611 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11612 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11613 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11614
11615 return 0;
11616}
11617
11618static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11619 u32 *exit_qual)
11620{
11621 bool ia32e;
11622
11623 *exit_qual = ENTRY_FAIL_DEFAULT;
11624
11625 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11626 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11627 return 1;
11628
11629 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11630 vmcs12->vmcs_link_pointer != -1ull) {
11631 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11632 return 1;
11633 }
11634
11635 /*
11636 * If the load IA32_EFER VM-entry control is 1, the following checks
11637 * are performed on the field for the IA32_EFER MSR:
11638 * - Bits reserved in the IA32_EFER MSR must be 0.
11639 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11640 * the IA-32e mode guest VM-exit control. It must also be identical
11641 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11642 * CR0.PG) is 1.
11643 */
11644 if (to_vmx(vcpu)->nested.nested_run_pending &&
11645 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11646 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11647 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11648 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11649 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11650 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11651 return 1;
11652 }
11653
11654 /*
11655 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11656 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11657 * the values of the LMA and LME bits in the field must each be that of
11658 * the host address-space size VM-exit control.
11659 */
11660 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11661 ia32e = (vmcs12->vm_exit_controls &
11662 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11663 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11664 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11665 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11666 return 1;
11667 }
11668
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011669 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11670 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11671 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11672 return 1;
11673
Jim Mattsonca0bde22016-11-30 12:03:46 -080011674 return 0;
11675}
11676
Jim Mattson6514dc32018-04-26 16:09:12 -070011677static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011678{
11679 struct vcpu_vmx *vmx = to_vmx(vcpu);
11680 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011681 u32 msr_entry_idx;
11682 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011683 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011684
Jim Mattson858e25c2016-11-30 12:03:47 -080011685 enter_guest_mode(vcpu);
11686
11687 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11688 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11689
Jim Mattsonde3a0022017-11-27 17:22:25 -060011690 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011691 vmx_segment_cache_clear(vmx);
11692
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011693 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11694 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11695
11696 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011697 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011698 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011699
11700 nested_get_vmcs12_pages(vcpu, vmcs12);
11701
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011702 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011703 msr_entry_idx = nested_vmx_load_msr(vcpu,
11704 vmcs12->vm_entry_msr_load_addr,
11705 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011706 if (msr_entry_idx)
11707 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011708
Jim Mattson858e25c2016-11-30 12:03:47 -080011709 /*
11710 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11711 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11712 * returned as far as L1 is concerned. It will only return (and set
11713 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11714 */
11715 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011716
11717fail:
11718 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11719 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11720 leave_guest_mode(vcpu);
11721 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11722 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11723 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011724}
11725
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011726/*
11727 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11728 * for running an L2 nested guest.
11729 */
11730static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11731{
11732 struct vmcs12 *vmcs12;
11733 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011734 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011735 u32 exit_qual;
11736 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011737
Kyle Hueyeb277562016-11-29 12:40:39 -080011738 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011739 return 1;
11740
Kyle Hueyeb277562016-11-29 12:40:39 -080011741 if (!nested_vmx_check_vmcs12(vcpu))
11742 goto out;
11743
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011744 vmcs12 = get_vmcs12(vcpu);
11745
Abel Gordon012f83c2013-04-18 14:39:25 +030011746 if (enable_shadow_vmcs)
11747 copy_shadow_to_vmcs12(vmx);
11748
Nadav Har'El7c177932011-05-25 23:12:04 +030011749 /*
11750 * The nested entry process starts with enforcing various prerequisites
11751 * on vmcs12 as required by the Intel SDM, and act appropriately when
11752 * they fail: As the SDM explains, some conditions should cause the
11753 * instruction to fail, while others will cause the instruction to seem
11754 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11755 * To speed up the normal (success) code path, we should avoid checking
11756 * for misconfigurations which will anyway be caught by the processor
11757 * when using the merged vmcs02.
11758 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011759 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11760 nested_vmx_failValid(vcpu,
11761 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11762 goto out;
11763 }
11764
Nadav Har'El7c177932011-05-25 23:12:04 +030011765 if (vmcs12->launch_state == launch) {
11766 nested_vmx_failValid(vcpu,
11767 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11768 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011769 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011770 }
11771
Jim Mattsonca0bde22016-11-30 12:03:46 -080011772 ret = check_vmentry_prereqs(vcpu, vmcs12);
11773 if (ret) {
11774 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011775 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011776 }
11777
Nadav Har'El7c177932011-05-25 23:12:04 +030011778 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011779 * After this point, the trap flag no longer triggers a singlestep trap
11780 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11781 * This is not 100% correct; for performance reasons, we delegate most
11782 * of the checks on host state to the processor. If those fail,
11783 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011784 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011785 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011786
Jim Mattsonca0bde22016-11-30 12:03:46 -080011787 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11788 if (ret) {
11789 nested_vmx_entry_failure(vcpu, vmcs12,
11790 EXIT_REASON_INVALID_STATE, exit_qual);
11791 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011792 }
11793
11794 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011795 * We're finally done with prerequisite checking, and can start with
11796 * the nested entry.
11797 */
11798
Jim Mattson6514dc32018-04-26 16:09:12 -070011799 vmx->nested.nested_run_pending = 1;
11800 ret = enter_vmx_non_root_mode(vcpu);
11801 if (ret) {
11802 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011803 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011804 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011805
Chao Gao135a06c2018-02-11 10:06:30 +080011806 /*
11807 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11808 * by event injection, halt vcpu.
11809 */
11810 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011811 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11812 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011813 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011814 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011815 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011816
11817out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011818 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011819}
11820
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011821/*
11822 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11823 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11824 * This function returns the new value we should put in vmcs12.guest_cr0.
11825 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11826 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11827 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11828 * didn't trap the bit, because if L1 did, so would L0).
11829 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11830 * been modified by L2, and L1 knows it. So just leave the old value of
11831 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11832 * isn't relevant, because if L0 traps this bit it can set it to anything.
11833 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11834 * changed these bits, and therefore they need to be updated, but L0
11835 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11836 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11837 */
11838static inline unsigned long
11839vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11840{
11841 return
11842 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11843 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11844 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11845 vcpu->arch.cr0_guest_owned_bits));
11846}
11847
11848static inline unsigned long
11849vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11850{
11851 return
11852 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11853 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11854 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11855 vcpu->arch.cr4_guest_owned_bits));
11856}
11857
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011858static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11859 struct vmcs12 *vmcs12)
11860{
11861 u32 idt_vectoring;
11862 unsigned int nr;
11863
Wanpeng Li664f8e22017-08-24 03:35:09 -070011864 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011865 nr = vcpu->arch.exception.nr;
11866 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11867
11868 if (kvm_exception_is_soft(nr)) {
11869 vmcs12->vm_exit_instruction_len =
11870 vcpu->arch.event_exit_inst_len;
11871 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11872 } else
11873 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11874
11875 if (vcpu->arch.exception.has_error_code) {
11876 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11877 vmcs12->idt_vectoring_error_code =
11878 vcpu->arch.exception.error_code;
11879 }
11880
11881 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011882 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011883 vmcs12->idt_vectoring_info_field =
11884 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011885 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011886 nr = vcpu->arch.interrupt.nr;
11887 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11888
11889 if (vcpu->arch.interrupt.soft) {
11890 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11891 vmcs12->vm_entry_instruction_len =
11892 vcpu->arch.event_exit_inst_len;
11893 } else
11894 idt_vectoring |= INTR_TYPE_EXT_INTR;
11895
11896 vmcs12->idt_vectoring_info_field = idt_vectoring;
11897 }
11898}
11899
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011900static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11901{
11902 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011903 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011904 bool block_nested_events =
11905 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011906
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011907 if (vcpu->arch.exception.pending &&
11908 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011909 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011910 return -EBUSY;
11911 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011912 return 0;
11913 }
11914
Jan Kiszkaf41245002014-03-07 20:03:13 +010011915 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11916 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011917 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011918 return -EBUSY;
11919 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11920 return 0;
11921 }
11922
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011923 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011924 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011925 return -EBUSY;
11926 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11927 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11928 INTR_INFO_VALID_MASK, 0);
11929 /*
11930 * The NMI-triggered VM exit counts as injection:
11931 * clear this one and block further NMIs.
11932 */
11933 vcpu->arch.nmi_pending = 0;
11934 vmx_set_nmi_mask(vcpu, true);
11935 return 0;
11936 }
11937
11938 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11939 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011940 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011941 return -EBUSY;
11942 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011943 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011944 }
11945
David Hildenbrand6342c502017-01-25 11:58:58 +010011946 vmx_complete_nested_posted_interrupt(vcpu);
11947 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011948}
11949
Jan Kiszkaf41245002014-03-07 20:03:13 +010011950static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11951{
11952 ktime_t remaining =
11953 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11954 u64 value;
11955
11956 if (ktime_to_ns(remaining) <= 0)
11957 return 0;
11958
11959 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11960 do_div(value, 1000000);
11961 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11962}
11963
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011964/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011965 * Update the guest state fields of vmcs12 to reflect changes that
11966 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11967 * VM-entry controls is also updated, since this is really a guest
11968 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011969 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011970static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011971{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011972 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11973 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11974
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011975 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11976 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11977 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11978
11979 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11980 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11981 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11982 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11983 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11984 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11985 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11986 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11987 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11988 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11989 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11990 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11991 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11992 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11993 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11994 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11995 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11996 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11997 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11998 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11999 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12000 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12001 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12002 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12003 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12004 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12005 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12006 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12007 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12008 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12009 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12010 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12011 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12012 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12013 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12014 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12015
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012016 vmcs12->guest_interruptibility_info =
12017 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12018 vmcs12->guest_pending_dbg_exceptions =
12019 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012020 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12021 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12022 else
12023 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012024
Jan Kiszkaf41245002014-03-07 20:03:13 +010012025 if (nested_cpu_has_preemption_timer(vmcs12)) {
12026 if (vmcs12->vm_exit_controls &
12027 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12028 vmcs12->vmx_preemption_timer_value =
12029 vmx_get_preemption_timer_value(vcpu);
12030 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12031 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012032
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012033 /*
12034 * In some cases (usually, nested EPT), L2 is allowed to change its
12035 * own CR3 without exiting. If it has changed it, we must keep it.
12036 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12037 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12038 *
12039 * Additionally, restore L2's PDPTR to vmcs12.
12040 */
12041 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012042 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012043 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12044 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12045 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12046 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12047 }
12048
Jim Mattsond281e132017-06-01 12:44:46 -070012049 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012050
Wincy Van608406e2015-02-03 23:57:51 +080012051 if (nested_cpu_has_vid(vmcs12))
12052 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12053
Jan Kiszkac18911a2013-03-13 16:06:41 +010012054 vmcs12->vm_entry_controls =
12055 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012056 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012057
Jan Kiszka2996fca2014-06-16 13:59:43 +020012058 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12059 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12060 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12061 }
12062
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012063 /* TODO: These cannot have changed unless we have MSR bitmaps and
12064 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012065 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012066 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012067 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12068 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012069 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12070 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12071 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012072 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012073 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012074}
12075
12076/*
12077 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12078 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12079 * and this function updates it to reflect the changes to the guest state while
12080 * L2 was running (and perhaps made some exits which were handled directly by L0
12081 * without going back to L1), and to reflect the exit reason.
12082 * Note that we do not have to copy here all VMCS fields, just those that
12083 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12084 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12085 * which already writes to vmcs12 directly.
12086 */
12087static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12088 u32 exit_reason, u32 exit_intr_info,
12089 unsigned long exit_qualification)
12090{
12091 /* update guest state fields: */
12092 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012093
12094 /* update exit information fields: */
12095
Jan Kiszka533558b2014-01-04 18:47:20 +010012096 vmcs12->vm_exit_reason = exit_reason;
12097 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012098 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012099
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012100 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012101 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12102 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12103
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012104 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012105 vmcs12->launch_state = 1;
12106
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012107 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12108 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012109 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012110
12111 /*
12112 * Transfer the event that L0 or L1 may wanted to inject into
12113 * L2 to IDT_VECTORING_INFO_FIELD.
12114 */
12115 vmcs12_save_pending_event(vcpu, vmcs12);
12116 }
12117
12118 /*
12119 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12120 * preserved above and would only end up incorrectly in L1.
12121 */
12122 vcpu->arch.nmi_injected = false;
12123 kvm_clear_exception_queue(vcpu);
12124 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012125}
12126
Wanpeng Li5af41572017-11-05 16:54:49 -080012127static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12128 struct vmcs12 *vmcs12)
12129{
12130 u32 entry_failure_code;
12131
12132 nested_ept_uninit_mmu_context(vcpu);
12133
12134 /*
12135 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12136 * couldn't have changed.
12137 */
12138 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12139 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12140
12141 if (!enable_ept)
12142 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12143}
12144
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012145/*
12146 * A part of what we need to when the nested L2 guest exits and we want to
12147 * run its L1 parent, is to reset L1's guest state to the host state specified
12148 * in vmcs12.
12149 * This function is to be called not only on normal nested exit, but also on
12150 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12151 * Failures During or After Loading Guest State").
12152 * This function should be called when the active VMCS is L1's (vmcs01).
12153 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012154static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12155 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012156{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012157 struct kvm_segment seg;
12158
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012159 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12160 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012161 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012162 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12163 else
12164 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12165 vmx_set_efer(vcpu, vcpu->arch.efer);
12166
12167 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12168 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012169 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012170 /*
12171 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012172 * actually changed, because vmx_set_cr0 refers to efer set above.
12173 *
12174 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12175 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012176 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012177 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012178 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012179
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012180 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012181 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012182 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012183
Wanpeng Li5af41572017-11-05 16:54:49 -080012184 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012185
Liran Alon6f1e03b2018-05-22 17:16:14 +030012186 /*
12187 * If vmcs01 don't use VPID, CPU flushes TLB on every
12188 * VMEntry/VMExit. Thus, no need to flush TLB.
12189 *
12190 * If vmcs12 uses VPID, TLB entries populated by L2 are
12191 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12192 * with vmx->vpid. Thus, no need to flush TLB.
12193 *
12194 * Therefore, flush TLB only in case vmcs01 uses VPID and
12195 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12196 * are both tagged with vmx->vpid.
12197 */
12198 if (enable_vpid &&
12199 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012200 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012201 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012202
12203 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12204 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12205 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12206 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12207 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020012208 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12209 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012210
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012211 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12212 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12213 vmcs_write64(GUEST_BNDCFGS, 0);
12214
Jan Kiszka44811c02013-08-04 17:17:27 +020012215 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012216 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012217 vcpu->arch.pat = vmcs12->host_ia32_pat;
12218 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012219 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12220 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12221 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012222
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012223 /* Set L1 segment info according to Intel SDM
12224 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12225 seg = (struct kvm_segment) {
12226 .base = 0,
12227 .limit = 0xFFFFFFFF,
12228 .selector = vmcs12->host_cs_selector,
12229 .type = 11,
12230 .present = 1,
12231 .s = 1,
12232 .g = 1
12233 };
12234 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12235 seg.l = 1;
12236 else
12237 seg.db = 1;
12238 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12239 seg = (struct kvm_segment) {
12240 .base = 0,
12241 .limit = 0xFFFFFFFF,
12242 .type = 3,
12243 .present = 1,
12244 .s = 1,
12245 .db = 1,
12246 .g = 1
12247 };
12248 seg.selector = vmcs12->host_ds_selector;
12249 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12250 seg.selector = vmcs12->host_es_selector;
12251 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12252 seg.selector = vmcs12->host_ss_selector;
12253 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12254 seg.selector = vmcs12->host_fs_selector;
12255 seg.base = vmcs12->host_fs_base;
12256 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12257 seg.selector = vmcs12->host_gs_selector;
12258 seg.base = vmcs12->host_gs_base;
12259 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12260 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012261 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012262 .limit = 0x67,
12263 .selector = vmcs12->host_tr_selector,
12264 .type = 11,
12265 .present = 1
12266 };
12267 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12268
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012269 kvm_set_dr(vcpu, 7, 0x400);
12270 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012271
Wincy Van3af18d92015-02-03 23:49:31 +080012272 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012273 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012274
Wincy Vanff651cb2014-12-11 08:52:58 +030012275 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12276 vmcs12->vm_exit_msr_load_count))
12277 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012278}
12279
12280/*
12281 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12282 * and modify vmcs12 to make it see what it would expect to see there if
12283 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12284 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012285static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12286 u32 exit_intr_info,
12287 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012288{
12289 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012290 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12291
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012292 /* trying to cancel vmlaunch/vmresume is a bug */
12293 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12294
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012295 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012296 * The only expected VM-instruction error is "VM entry with
12297 * invalid control field(s)." Anything else indicates a
12298 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012299 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012300 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12301 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12302
12303 leave_guest_mode(vcpu);
12304
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012305 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12306 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12307
Jim Mattson4f350c62017-09-14 16:31:44 -070012308 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012309 if (exit_reason == -1)
12310 sync_vmcs12(vcpu, vmcs12);
12311 else
12312 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12313 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012314
12315 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12316 vmcs12->vm_exit_msr_store_count))
12317 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012318 }
12319
Jim Mattson4f350c62017-09-14 16:31:44 -070012320 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012321 vm_entry_controls_reset_shadow(vmx);
12322 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012323 vmx_segment_cache_clear(vmx);
12324
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012325 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012326 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12327 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012328 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012329 if (vmx->hv_deadline_tsc == -1)
12330 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12331 PIN_BASED_VMX_PREEMPTION_TIMER);
12332 else
12333 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12334 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012335 if (kvm_has_tsc_control)
12336 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012337
Jim Mattson8d860bb2018-05-09 16:56:05 -040012338 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12339 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12340 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012341 } else if (!nested_cpu_has_ept(vmcs12) &&
12342 nested_cpu_has2(vmcs12,
12343 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012344 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012345 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012346
12347 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12348 vmx->host_rsp = 0;
12349
12350 /* Unpin physical memory we referred to in vmcs02 */
12351 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012352 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012353 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012354 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012355 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012356 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012357 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012358 }
Wincy Van705699a2015-02-03 23:58:17 +080012359 if (vmx->nested.pi_desc_page) {
12360 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012361 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012362 vmx->nested.pi_desc_page = NULL;
12363 vmx->nested.pi_desc = NULL;
12364 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012365
12366 /*
Tang Chen38b99172014-09-24 15:57:54 +080012367 * We are now running in L2, mmu_notifier will force to reload the
12368 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12369 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012370 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012371
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012372 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012373 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012374
12375 /* in case we halted in L2 */
12376 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012377
12378 if (likely(!vmx->fail)) {
12379 /*
12380 * TODO: SDM says that with acknowledge interrupt on
12381 * exit, bit 31 of the VM-exit interrupt information
12382 * (valid interrupt) is always set to 1 on
12383 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12384 * need kvm_cpu_has_interrupt(). See the commit
12385 * message for details.
12386 */
12387 if (nested_exit_intr_ack_set(vcpu) &&
12388 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12389 kvm_cpu_has_interrupt(vcpu)) {
12390 int irq = kvm_cpu_get_interrupt(vcpu);
12391 WARN_ON(irq < 0);
12392 vmcs12->vm_exit_intr_info = irq |
12393 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12394 }
12395
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012396 if (exit_reason != -1)
12397 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12398 vmcs12->exit_qualification,
12399 vmcs12->idt_vectoring_info_field,
12400 vmcs12->vm_exit_intr_info,
12401 vmcs12->vm_exit_intr_error_code,
12402 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012403
12404 load_vmcs12_host_state(vcpu, vmcs12);
12405
12406 return;
12407 }
12408
12409 /*
12410 * After an early L2 VM-entry failure, we're now back
12411 * in L1 which thinks it just finished a VMLAUNCH or
12412 * VMRESUME instruction, so we need to set the failure
12413 * flag and the VM-instruction error field of the VMCS
12414 * accordingly.
12415 */
12416 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012417
12418 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12419
Jim Mattson4f350c62017-09-14 16:31:44 -070012420 /*
12421 * The emulated instruction was already skipped in
12422 * nested_vmx_run, but the updated RIP was never
12423 * written back to the vmcs01.
12424 */
12425 skip_emulated_instruction(vcpu);
12426 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012427}
12428
Nadav Har'El7c177932011-05-25 23:12:04 +030012429/*
Jan Kiszka42124922014-01-04 18:47:19 +010012430 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12431 */
12432static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12433{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012434 if (is_guest_mode(vcpu)) {
12435 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012436 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012437 }
Jan Kiszka42124922014-01-04 18:47:19 +010012438 free_nested(to_vmx(vcpu));
12439}
12440
12441/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012442 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12443 * 23.7 "VM-entry failures during or after loading guest state" (this also
12444 * lists the acceptable exit-reason and exit-qualification parameters).
12445 * It should only be called before L2 actually succeeded to run, and when
12446 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12447 */
12448static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12449 struct vmcs12 *vmcs12,
12450 u32 reason, unsigned long qualification)
12451{
12452 load_vmcs12_host_state(vcpu, vmcs12);
12453 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12454 vmcs12->exit_qualification = qualification;
12455 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012456 if (enable_shadow_vmcs)
12457 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012458}
12459
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012460static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12461 struct x86_instruction_info *info,
12462 enum x86_intercept_stage stage)
12463{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012464 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12465 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12466
12467 /*
12468 * RDPID causes #UD if disabled through secondary execution controls.
12469 * Because it is marked as EmulateOnUD, we need to intercept it here.
12470 */
12471 if (info->intercept == x86_intercept_rdtscp &&
12472 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12473 ctxt->exception.vector = UD_VECTOR;
12474 ctxt->exception.error_code_valid = false;
12475 return X86EMUL_PROPAGATE_FAULT;
12476 }
12477
12478 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012479 return X86EMUL_CONTINUE;
12480}
12481
Yunhong Jiang64672c92016-06-13 14:19:59 -070012482#ifdef CONFIG_X86_64
12483/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12484static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12485 u64 divisor, u64 *result)
12486{
12487 u64 low = a << shift, high = a >> (64 - shift);
12488
12489 /* To avoid the overflow on divq */
12490 if (high >= divisor)
12491 return 1;
12492
12493 /* Low hold the result, high hold rem which is discarded */
12494 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12495 "rm" (divisor), "0" (low), "1" (high));
12496 *result = low;
12497
12498 return 0;
12499}
12500
12501static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12502{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012503 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012504 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012505
12506 if (kvm_mwait_in_guest(vcpu->kvm))
12507 return -EOPNOTSUPP;
12508
12509 vmx = to_vmx(vcpu);
12510 tscl = rdtsc();
12511 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12512 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012513 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12514
12515 if (delta_tsc > lapic_timer_advance_cycles)
12516 delta_tsc -= lapic_timer_advance_cycles;
12517 else
12518 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012519
12520 /* Convert to host delta tsc if tsc scaling is enabled */
12521 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12522 u64_shl_div_u64(delta_tsc,
12523 kvm_tsc_scaling_ratio_frac_bits,
12524 vcpu->arch.tsc_scaling_ratio,
12525 &delta_tsc))
12526 return -ERANGE;
12527
12528 /*
12529 * If the delta tsc can't fit in the 32 bit after the multi shift,
12530 * we can't use the preemption timer.
12531 * It's possible that it fits on later vmentries, but checking
12532 * on every vmentry is costly so we just use an hrtimer.
12533 */
12534 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12535 return -ERANGE;
12536
12537 vmx->hv_deadline_tsc = tscl + delta_tsc;
12538 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12539 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012540
12541 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012542}
12543
12544static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12545{
12546 struct vcpu_vmx *vmx = to_vmx(vcpu);
12547 vmx->hv_deadline_tsc = -1;
12548 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12549 PIN_BASED_VMX_PREEMPTION_TIMER);
12550}
12551#endif
12552
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012553static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012554{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012555 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012556 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012557}
12558
Kai Huang843e4332015-01-28 10:54:28 +080012559static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12560 struct kvm_memory_slot *slot)
12561{
12562 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12563 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12564}
12565
12566static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12567 struct kvm_memory_slot *slot)
12568{
12569 kvm_mmu_slot_set_dirty(kvm, slot);
12570}
12571
12572static void vmx_flush_log_dirty(struct kvm *kvm)
12573{
12574 kvm_flush_pml_buffers(kvm);
12575}
12576
Bandan Dasc5f983f2017-05-05 15:25:14 -040012577static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12578{
12579 struct vmcs12 *vmcs12;
12580 struct vcpu_vmx *vmx = to_vmx(vcpu);
12581 gpa_t gpa;
12582 struct page *page = NULL;
12583 u64 *pml_address;
12584
12585 if (is_guest_mode(vcpu)) {
12586 WARN_ON_ONCE(vmx->nested.pml_full);
12587
12588 /*
12589 * Check if PML is enabled for the nested guest.
12590 * Whether eptp bit 6 is set is already checked
12591 * as part of A/D emulation.
12592 */
12593 vmcs12 = get_vmcs12(vcpu);
12594 if (!nested_cpu_has_pml(vmcs12))
12595 return 0;
12596
Dan Carpenter47698862017-05-10 22:43:17 +030012597 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012598 vmx->nested.pml_full = true;
12599 return 1;
12600 }
12601
12602 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12603
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012604 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12605 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012606 return 0;
12607
12608 pml_address = kmap(page);
12609 pml_address[vmcs12->guest_pml_index--] = gpa;
12610 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012611 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012612 }
12613
12614 return 0;
12615}
12616
Kai Huang843e4332015-01-28 10:54:28 +080012617static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12618 struct kvm_memory_slot *memslot,
12619 gfn_t offset, unsigned long mask)
12620{
12621 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12622}
12623
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012624static void __pi_post_block(struct kvm_vcpu *vcpu)
12625{
12626 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12627 struct pi_desc old, new;
12628 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012629
12630 do {
12631 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012632 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12633 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012634
12635 dest = cpu_physical_id(vcpu->cpu);
12636
12637 if (x2apic_enabled())
12638 new.ndst = dest;
12639 else
12640 new.ndst = (dest << 8) & 0xFF00;
12641
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012642 /* set 'NV' to 'notification vector' */
12643 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012644 } while (cmpxchg64(&pi_desc->control, old.control,
12645 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012646
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012647 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12648 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012649 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012650 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012651 vcpu->pre_pcpu = -1;
12652 }
12653}
12654
Feng Wuefc64402015-09-18 22:29:51 +080012655/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012656 * This routine does the following things for vCPU which is going
12657 * to be blocked if VT-d PI is enabled.
12658 * - Store the vCPU to the wakeup list, so when interrupts happen
12659 * we can find the right vCPU to wake up.
12660 * - Change the Posted-interrupt descriptor as below:
12661 * 'NDST' <-- vcpu->pre_pcpu
12662 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12663 * - If 'ON' is set during this process, which means at least one
12664 * interrupt is posted for this vCPU, we cannot block it, in
12665 * this case, return 1, otherwise, return 0.
12666 *
12667 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012668static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012669{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012670 unsigned int dest;
12671 struct pi_desc old, new;
12672 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12673
12674 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012675 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12676 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012677 return 0;
12678
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012679 WARN_ON(irqs_disabled());
12680 local_irq_disable();
12681 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12682 vcpu->pre_pcpu = vcpu->cpu;
12683 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12684 list_add_tail(&vcpu->blocked_vcpu_list,
12685 &per_cpu(blocked_vcpu_on_cpu,
12686 vcpu->pre_pcpu));
12687 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12688 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012689
12690 do {
12691 old.control = new.control = pi_desc->control;
12692
Feng Wubf9f6ac2015-09-18 22:29:55 +080012693 WARN((pi_desc->sn == 1),
12694 "Warning: SN field of posted-interrupts "
12695 "is set before blocking\n");
12696
12697 /*
12698 * Since vCPU can be preempted during this process,
12699 * vcpu->cpu could be different with pre_pcpu, we
12700 * need to set pre_pcpu as the destination of wakeup
12701 * notification event, then we can find the right vCPU
12702 * to wakeup in wakeup handler if interrupts happen
12703 * when the vCPU is in blocked state.
12704 */
12705 dest = cpu_physical_id(vcpu->pre_pcpu);
12706
12707 if (x2apic_enabled())
12708 new.ndst = dest;
12709 else
12710 new.ndst = (dest << 8) & 0xFF00;
12711
12712 /* set 'NV' to 'wakeup vector' */
12713 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012714 } while (cmpxchg64(&pi_desc->control, old.control,
12715 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012716
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012717 /* We should not block the vCPU if an interrupt is posted for it. */
12718 if (pi_test_on(pi_desc) == 1)
12719 __pi_post_block(vcpu);
12720
12721 local_irq_enable();
12722 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012723}
12724
Yunhong Jiangbc225122016-06-13 14:19:58 -070012725static int vmx_pre_block(struct kvm_vcpu *vcpu)
12726{
12727 if (pi_pre_block(vcpu))
12728 return 1;
12729
Yunhong Jiang64672c92016-06-13 14:19:59 -070012730 if (kvm_lapic_hv_timer_in_use(vcpu))
12731 kvm_lapic_switch_to_sw_timer(vcpu);
12732
Yunhong Jiangbc225122016-06-13 14:19:58 -070012733 return 0;
12734}
12735
12736static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012737{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012738 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012739 return;
12740
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012741 WARN_ON(irqs_disabled());
12742 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012743 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012744 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012745}
12746
Yunhong Jiangbc225122016-06-13 14:19:58 -070012747static void vmx_post_block(struct kvm_vcpu *vcpu)
12748{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012749 if (kvm_x86_ops->set_hv_timer)
12750 kvm_lapic_switch_to_hv_timer(vcpu);
12751
Yunhong Jiangbc225122016-06-13 14:19:58 -070012752 pi_post_block(vcpu);
12753}
12754
Feng Wubf9f6ac2015-09-18 22:29:55 +080012755/*
Feng Wuefc64402015-09-18 22:29:51 +080012756 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12757 *
12758 * @kvm: kvm
12759 * @host_irq: host irq of the interrupt
12760 * @guest_irq: gsi of the interrupt
12761 * @set: set or unset PI
12762 * returns 0 on success, < 0 on failure
12763 */
12764static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12765 uint32_t guest_irq, bool set)
12766{
12767 struct kvm_kernel_irq_routing_entry *e;
12768 struct kvm_irq_routing_table *irq_rt;
12769 struct kvm_lapic_irq irq;
12770 struct kvm_vcpu *vcpu;
12771 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012772 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012773
12774 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012775 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12776 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012777 return 0;
12778
12779 idx = srcu_read_lock(&kvm->irq_srcu);
12780 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012781 if (guest_irq >= irq_rt->nr_rt_entries ||
12782 hlist_empty(&irq_rt->map[guest_irq])) {
12783 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12784 guest_irq, irq_rt->nr_rt_entries);
12785 goto out;
12786 }
Feng Wuefc64402015-09-18 22:29:51 +080012787
12788 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12789 if (e->type != KVM_IRQ_ROUTING_MSI)
12790 continue;
12791 /*
12792 * VT-d PI cannot support posting multicast/broadcast
12793 * interrupts to a vCPU, we still use interrupt remapping
12794 * for these kind of interrupts.
12795 *
12796 * For lowest-priority interrupts, we only support
12797 * those with single CPU as the destination, e.g. user
12798 * configures the interrupts via /proc/irq or uses
12799 * irqbalance to make the interrupts single-CPU.
12800 *
12801 * We will support full lowest-priority interrupt later.
12802 */
12803
Radim Krčmář371313132016-07-12 22:09:27 +020012804 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012805 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12806 /*
12807 * Make sure the IRTE is in remapped mode if
12808 * we don't handle it in posted mode.
12809 */
12810 ret = irq_set_vcpu_affinity(host_irq, NULL);
12811 if (ret < 0) {
12812 printk(KERN_INFO
12813 "failed to back to remapped mode, irq: %u\n",
12814 host_irq);
12815 goto out;
12816 }
12817
Feng Wuefc64402015-09-18 22:29:51 +080012818 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012819 }
Feng Wuefc64402015-09-18 22:29:51 +080012820
12821 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12822 vcpu_info.vector = irq.vector;
12823
hu huajun2698d822018-04-11 15:16:40 +080012824 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012825 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12826
12827 if (set)
12828 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080012829 else
Feng Wuefc64402015-09-18 22:29:51 +080012830 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012831
12832 if (ret < 0) {
12833 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12834 __func__);
12835 goto out;
12836 }
12837 }
12838
12839 ret = 0;
12840out:
12841 srcu_read_unlock(&kvm->irq_srcu, idx);
12842 return ret;
12843}
12844
Ashok Rajc45dcc72016-06-22 14:59:56 +080012845static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12846{
12847 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12848 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12849 FEATURE_CONTROL_LMCE;
12850 else
12851 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12852 ~FEATURE_CONTROL_LMCE;
12853}
12854
Ladi Prosek72d7b372017-10-11 16:54:41 +020012855static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12856{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012857 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12858 if (to_vmx(vcpu)->nested.nested_run_pending)
12859 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012860 return 1;
12861}
12862
Ladi Prosek0234bf82017-10-11 16:54:40 +020012863static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12864{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012865 struct vcpu_vmx *vmx = to_vmx(vcpu);
12866
12867 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12868 if (vmx->nested.smm.guest_mode)
12869 nested_vmx_vmexit(vcpu, -1, 0, 0);
12870
12871 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12872 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012873 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012874 return 0;
12875}
12876
12877static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12878{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012879 struct vcpu_vmx *vmx = to_vmx(vcpu);
12880 int ret;
12881
12882 if (vmx->nested.smm.vmxon) {
12883 vmx->nested.vmxon = true;
12884 vmx->nested.smm.vmxon = false;
12885 }
12886
12887 if (vmx->nested.smm.guest_mode) {
12888 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070012889 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012890 vcpu->arch.hflags |= HF_SMM_MASK;
12891 if (ret)
12892 return ret;
12893
12894 vmx->nested.smm.guest_mode = false;
12895 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012896 return 0;
12897}
12898
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012899static int enable_smi_window(struct kvm_vcpu *vcpu)
12900{
12901 return 0;
12902}
12903
Kees Cook404f6aa2016-08-08 16:29:06 -070012904static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012905 .cpu_has_kvm_support = cpu_has_kvm_support,
12906 .disabled_by_bios = vmx_disabled_by_bios,
12907 .hardware_setup = hardware_setup,
12908 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012909 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012910 .hardware_enable = hardware_enable,
12911 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012912 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012913 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012914
Wanpeng Lib31c1142018-03-12 04:53:04 -070012915 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012916 .vm_alloc = vmx_vm_alloc,
12917 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012918
Avi Kivity6aa8b732006-12-10 02:21:36 -080012919 .vcpu_create = vmx_create_vcpu,
12920 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012921 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012922
Avi Kivity04d2cc72007-09-10 18:10:54 +030012923 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012924 .vcpu_load = vmx_vcpu_load,
12925 .vcpu_put = vmx_vcpu_put,
12926
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012927 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012928 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012929 .get_msr = vmx_get_msr,
12930 .set_msr = vmx_set_msr,
12931 .get_segment_base = vmx_get_segment_base,
12932 .get_segment = vmx_get_segment,
12933 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012934 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012935 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012936 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012937 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012938 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012939 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012940 .set_cr3 = vmx_set_cr3,
12941 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012942 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012943 .get_idt = vmx_get_idt,
12944 .set_idt = vmx_set_idt,
12945 .get_gdt = vmx_get_gdt,
12946 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012947 .get_dr6 = vmx_get_dr6,
12948 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012949 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012950 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012951 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012952 .get_rflags = vmx_get_rflags,
12953 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012954
Avi Kivity6aa8b732006-12-10 02:21:36 -080012955 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012956
Avi Kivity6aa8b732006-12-10 02:21:36 -080012957 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012958 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012959 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012960 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12961 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012962 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012963 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012964 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012965 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012966 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012967 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012968 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012969 .get_nmi_mask = vmx_get_nmi_mask,
12970 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012971 .enable_nmi_window = enable_nmi_window,
12972 .enable_irq_window = enable_irq_window,
12973 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040012974 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012975 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012976 .get_enable_apicv = vmx_get_enable_apicv,
12977 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012978 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012979 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012980 .hwapic_irr_update = vmx_hwapic_irr_update,
12981 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012982 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12983 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012984
Izik Eiduscbc94022007-10-25 00:29:55 +020012985 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012986 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012987 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012988 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012989
Avi Kivity586f9602010-11-18 13:09:54 +020012990 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012991
Sheng Yang17cc3932010-01-05 19:02:27 +080012992 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012993
12994 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012995
12996 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012997 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012998
12999 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013000
13001 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013002
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013003 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013004 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013005
13006 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013007
13008 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013009 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013010 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013011 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013012 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013013
13014 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013015
13016 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013017
13018 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13019 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13020 .flush_log_dirty = vmx_flush_log_dirty,
13021 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013022 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013023
Feng Wubf9f6ac2015-09-18 22:29:55 +080013024 .pre_block = vmx_pre_block,
13025 .post_block = vmx_post_block,
13026
Wei Huang25462f72015-06-19 15:45:05 +020013027 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013028
13029 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013030
13031#ifdef CONFIG_X86_64
13032 .set_hv_timer = vmx_set_hv_timer,
13033 .cancel_hv_timer = vmx_cancel_hv_timer,
13034#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013035
13036 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013037
Ladi Prosek72d7b372017-10-11 16:54:41 +020013038 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013039 .pre_enter_smm = vmx_pre_enter_smm,
13040 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013041 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013042};
13043
13044static int __init vmx_init(void)
13045{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013046 int r;
13047
13048#if IS_ENABLED(CONFIG_HYPERV)
13049 /*
13050 * Enlightened VMCS usage should be recommended and the host needs
13051 * to support eVMCS v1 or above. We can also disable eVMCS support
13052 * with module parameter.
13053 */
13054 if (enlightened_vmcs &&
13055 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13056 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13057 KVM_EVMCS_VERSION) {
13058 int cpu;
13059
13060 /* Check that we have assist pages on all online CPUs */
13061 for_each_online_cpu(cpu) {
13062 if (!hv_get_vp_assist_page(cpu)) {
13063 enlightened_vmcs = false;
13064 break;
13065 }
13066 }
13067
13068 if (enlightened_vmcs) {
13069 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13070 static_branch_enable(&enable_evmcs);
13071 }
13072 } else {
13073 enlightened_vmcs = false;
13074 }
13075#endif
13076
13077 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013078 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013079 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013080 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013081
Dave Young2965faa2015-09-09 15:38:55 -070013082#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013083 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13084 crash_vmclear_local_loaded_vmcss);
13085#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013086 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013087
He, Qingfdef3ad2007-04-30 09:45:24 +030013088 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013089}
13090
13091static void __exit vmx_exit(void)
13092{
Dave Young2965faa2015-09-09 15:38:55 -070013093#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013094 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013095 synchronize_rcu();
13096#endif
13097
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013098 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013099
13100#if IS_ENABLED(CONFIG_HYPERV)
13101 if (static_branch_unlikely(&enable_evmcs)) {
13102 int cpu;
13103 struct hv_vp_assist_page *vp_ap;
13104 /*
13105 * Reset everything to support using non-enlightened VMCS
13106 * access later (e.g. when we reload the module with
13107 * enlightened_vmcs=0)
13108 */
13109 for_each_online_cpu(cpu) {
13110 vp_ap = hv_get_vp_assist_page(cpu);
13111
13112 if (!vp_ap)
13113 continue;
13114
13115 vp_ap->current_nested_vmcs = 0;
13116 vp_ap->enlighten_vmentry = 0;
13117 }
13118
13119 static_branch_disable(&enable_evmcs);
13120 }
13121#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013122}
13123
13124module_init(vmx_init)
13125module_exit(vmx_exit)